Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update babel-plugin-transform-amp-asserts with devAssert removal. #23377

Merged
merged 18 commits into from
Aug 14, 2019
Merged
Original file line number Diff line number Diff line change
Expand Up @@ -51,38 +51,52 @@ module.exports = function(babel) {
const {node} = path;
const {callee} = node;
const {parenthesized} = node.extra || {};

const isDirectCallExpression = t.isIdentifier(callee, {
name: 'devAssert',
});
const isMemberAndCallExpression =
t.isMemberExpression(callee) && t.isCallExpression(callee.object);

if (!isMemberAndCallExpression) {
if (!(isDirectCallExpression || isMemberAndCallExpression)) {
return;
}

const logCallee = callee.object.callee;
const {property} = callee;
const isRemovableDevCall =
t.isIdentifier(logCallee, {name: 'dev'}) &&
isRemovableMethod(t, property, removableDevAsserts);
// `property` here is the identifier we want to evaluate such as the
// `devAssert` in a direct call or the `assert` in a `dev().assert()`
// call.
let property = null;
let args = null;
// `type` is left null in a direct call expression like `devAssert`
let type = null;

const isRemovableUserCall =
t.isIdentifier(logCallee, {name: 'user'}) &&
isRemovableMethod(t, property, removableUserAsserts);
if (isDirectCallExpression) {
property = node.callee;
args = node.arguments[0];
} else if (isMemberAndCallExpression) {
property = callee.property;

if (!(isRemovableDevCall || isRemovableUserCall)) {
return;
}
const logCallee = callee.object.callee;
const isRemovableDevCall =
t.isIdentifier(logCallee, {name: 'dev'}) &&
isRemovableMethod(t, property, removableDevAsserts);

// We assume the return is always the resolved expression value.
// This might not be the case like in assertEnum which we currently
// don't remove.
const args = path.node.arguments[0];
const type = typeMap[property.name];
const isRemovableUserCall =
t.isIdentifier(logCallee, {name: 'user'}) &&
isRemovableMethod(t, property, removableUserAsserts);

if (!(isRemovableDevCall || isRemovableUserCall)) {
return;
}
args = path.node.arguments[0];
type = typeMap[property.name];
}

if (args) {
if (parenthesized) {
path.replaceWith(t.parenthesizedExpression(args));
path.skip();
// If is not an assert type, we won't need to do type annotation.
// If it is not an assert type, we won't need to do type annotation.
// If it has no type that we can cast to, then we also won't need to
// do type annotation.
} else if (!property.name.startsWith('assert') || !type) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/**
* Copyright 2019 The AMP HTML Authors. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS-IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
userAssert(1 + 1);
userAssert(dev().assert(2 + 2));
userAssert();
let result = userAssert(dev(), 'hello', 'world');
let result2 = userAssert();

Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"plugins": ["../../../../../babel-plugin-transform-amp-asserts"]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/**
* Copyright 2019 The AMP HTML Authors. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS-IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
userAssert(1 + 1);
userAssert(2 + 2);
userAssert();
let result = userAssert(dev(), 'hello', 'world');
let result2 = userAssert();
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/**
* Copyright 2019 The AMP HTML Authors. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS-IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
devAssert(1 + 1);
devAssert(dev().assert(2 + 2));
devAssert();
let result = devAssert(dev(), 'hello', 'world');
let result2 = devAssert();
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"plugins": ["../../../../../babel-plugin-transform-amp-asserts"]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/**
* Copyright 2019 The AMP HTML Authors. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS-IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
1 + 1;
2 + 2;
undefined;
let result = dev();
let result2 = undefined;