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

🚀 Transformer for removing AMP devAssert, working on existing known cases #27821

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
/**
* Copyright 2020 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.
*/

/**
* Is a MemberExpression "thenable", `foo().then(() => {});`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't this just asking if the expression is a promise or not? What else would be "thenable" besides a promise?

*
* @param {babel.MemberExpression} memberExpression
* @return {boolean}
*/
function isThenable(memberExpression) {
return memberExpression.get('property').isIdentifier({name: 'then'});
}

/**
* Does a MemberExpression have a inner CallExpression? Return it.
*
* @param {babel.MemberExpression} memberExpression
* @return {undefined | babel.CallExpression}
*/
function getMatchingInnerCallExpression(memberExpression) {
const memberObject = memberExpression.get('object');

if (
memberObject.isCallExpression() &&
memberObject.get('callee').isIdentifier({name: 'devAssert'})
) {
return memberObject;
}

return undefined;
}

/**
* Extend babel's path.evaluate with extra confidence for very specific scenarios in this transform.
*
* @param {babel.CallExpression} callExpression
* @return {babel.Evaluation}
*/
function evaluateDevAssert(callExpression) {
const argument = callExpression.get('arguments.0');
if (argument.isMemberExpression()) {
return {confident: true};
}
if (argument.isStringLiteral()) {
return {confident: true, value: argument.node.value};
}

return argument.evaluate();
}

module.exports = function ({types: t}) {
/**
* Given a potentially literal value, attempt to create a babel AST Literal Node for it.
*
* @param {*} value to try and create a babel literal for.
* @return {undefined | babel.NumericLiteral | babel.StringLiteral | babel.BooleanLiteral} value
*/
function usableEvaluateValue(value) {
switch (typeof value) {
case 'number':
return t.numericLiteral(value);
case 'string':
return t.stringLiteral(value);
case 'boolean':
return t.booleanLiteral(value);
case 'object':
if (String(value) === 'null') {
return t.nullLiteral();
}
return undefined;
default:
return undefined;
}
}

/**
* Given a wrapping CallExpression and inner MemberExpression.
* Attempt to find and replace the contents of a CallExpression inside the MemberExpression.
*
* @param {babel.CallExpression} callExpression
* @param {babel.MemberExpression} memberExpression
* @return {boolean}
*/
function handleInnerMemberExpression(callExpression, memberExpression) {
const innerCallExpression = getMatchingInnerCallExpression(
memberExpression
);
if (innerCallExpression) {
const evalutedDevAssert = evaluateDevAssert(innerCallExpression);
if (!evalutedDevAssert.confident) {
return;
Copy link
Member

@erwinmombay erwinmombay Apr 17, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we return a boolean here? return false

}

// MemberExpression has an inner CallExpression with a removable `devAssert`.
if (isThenable(memberExpression)) {
// MemberExpression is thenable, so removing the `devAssert` means thenabling the first argument of `devAssert`.
const newCallee = innerCallExpression.get('arguments.0');
const newArguments = callExpression.get('arguments');
const newCallExpression = t.callExpression(
t.cloneDeep(newCallee.node),
[...newArguments.map((arg) => t.cloneDeep(arg.node))]
);

callExpression.replaceWith(newCallExpression);
return true;
}

return false;
}

return true;
}

return {
visitor: {
CallExpression(path) {
const callee = path.get('callee');

if (callee.isMemberExpression()) {
handleInnerMemberExpression(path, callee);
return;
}

if (callee.isIdentifier({name: 'devAssert'})) {
const evaluatedDevAssert = evaluateDevAssert(path);

if (
evaluatedDevAssert.confident &&
evaluatedDevAssert.value !== undefined
) {
const replacement = usableEvaluateValue(evaluatedDevAssert.value);
if (replacement !== undefined) {
if (t.isExpressionStatement(path.parent)) {
// When the direct parent of a `devAssert` is a ExpressionStatement, it can safely be removed.
// This is only true when the replacement was statically analyseable.
path.remove();
return;
}

// Given the replacement can be statically analysed, replace the value with the static replacement.
path.replaceWith(replacement);
return;
}
}

// In other conditions, simply remove the `devAssert()` wrapper and allow the first argument to exist.
path.replaceWith(path.get('arguments.0'));
}
},
},
};
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/**
* 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('');
devAssert(false);
devAssert(null);
devAssert(0);
devAssert(dev().assert(2 + 2));
let result = devAssert(foo, 'hello', 'world');
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"plugins": ["../../../.."]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/**
* 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.
*/
dev().assert(2 + 2);
let result = foo;
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/**
* Copyright 2018 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.
*/

class ThenableSuccess {
refresh() {
return devAssert(this.promise_).then(() => {
console.log('success');
});
}
}

class ThenableSuccessFailure {
refresh() {
return devAssert(this.promise_).then(() => {
console.log('success');
}, () => {
console.log('failure');
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"plugins": ["../../../.."]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/**
* Copyright 2018 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.
*/
class ThenableSuccess {
refresh() {
return this.promise_(() => {
console.log('success');
});
}

}

class ThenableSuccessFailure {
refresh() {
return this.promise_(() => {
console.log('success');
}, () => {
console.log('failure');
});
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/**
* Copyright 2020 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.
*/

const runner = require('@babel/helper-plugin-test-runner').default;

runner(__dirname);