Skip to content

Commit

Permalink
update babel-plugin-transform-amp-asserts with devAssert removal. (
Browse files Browse the repository at this point in the history
…#23377)

* temp

* temp

* temp

* add babel pipeline to multipass build

* temp

* temp

* temp

* temp

* add pre build code to execute before copying occurs

* apply lint fixes

* additional cleanup

* additional lint cleanup

* consolidate src globs location

* more lint fixes

* remove references to isCommonJsModule

* fix assert transformer to handle devAssert and userAssert

* fix tests

* lint fix
  • Loading branch information
erwinmombay authored and jridgewell committed Aug 14, 2019
1 parent aa43d27 commit aa7c50f
Show file tree
Hide file tree
Showing 7 changed files with 119 additions and 18 deletions.
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;

0 comments on commit aa7c50f

Please sign in to comment.