-
Notifications
You must be signed in to change notification settings - Fork 3.9k
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
kristoferbaxter
wants to merge
2
commits into
ampproject:master
from
kristoferbaxter:babel-plugin-devassert
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
165 changes: 165 additions & 0 deletions
165
build-system/babel-plugins/babel-plugin-amp-dev-assert-transformer/index.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(() => {});` | ||
* | ||
* @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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should we return a boolean here? |
||
} | ||
|
||
// 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')); | ||
} | ||
}, | ||
}, | ||
}; | ||
}; |
22 changes: 22 additions & 0 deletions
22
...bel-plugin-amp-dev-assert-transformer/test/fixtures/transform-assertions/invoked/input.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); |
3 changes: 3 additions & 0 deletions
3
...plugin-amp-dev-assert-transformer/test/fixtures/transform-assertions/invoked/options.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"plugins": ["../../../.."] | ||
} |
17 changes: 17 additions & 0 deletions
17
...el-plugin-amp-dev-assert-transformer/test/fixtures/transform-assertions/invoked/output.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
33 changes: 33 additions & 0 deletions
33
...el-plugin-amp-dev-assert-transformer/test/fixtures/transform-assertions/thenable/input.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
}); | ||
} | ||
} |
3 changes: 3 additions & 0 deletions
3
...lugin-amp-dev-assert-transformer/test/fixtures/transform-assertions/thenable/options.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"plugins": ["../../../.."] | ||
} |
34 changes: 34 additions & 0 deletions
34
...l-plugin-amp-dev-assert-transformer/test/fixtures/transform-assertions/thenable/output.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
}); | ||
} | ||
|
||
} |
19 changes: 19 additions & 0 deletions
19
build-system/babel-plugins/babel-plugin-amp-dev-assert-transformer/test/index.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?