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
🚀 Flatten Literals in TemplateLiterals #27516
Changes from all commits
b627d0b
c583471
7dba514
5fb343b
415e82c
9af1125
2ad1e56
e587510
c4c5ace
22557df
a1edbb7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,148 @@ | ||
/** | ||
* 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 MERGEABLE_TYPES = ['StringLiteral', 'NumericLiteral', 'TemplateLiteral']; | ||
|
||
module.exports = function ({types: t}) { | ||
const cloneNodes = (nodes) => nodes.map((node) => t.cloneNode(node)); | ||
const escapeStringForTemplateLiteral = (value) => | ||
String(value).replace(/\x60/g, '\\`'); | ||
const canMergeBinaryExpression = (binaryExpression) => | ||
MERGEABLE_TYPES.includes(binaryExpression.left.type) && | ||
MERGEABLE_TYPES.includes(binaryExpression.right.type) && | ||
binaryExpression.operator === '+'; | ||
|
||
function whichCloneQuasi(clonedQuasis, index) { | ||
for (let i = index; i >= 0; i--) { | ||
const quasi = clonedQuasis[i]; | ||
if (quasi !== null) { | ||
return index; | ||
} | ||
} | ||
} | ||
|
||
return { | ||
name: 'flatten-stringish-literals', | ||
visitor: { | ||
BinaryExpression: { | ||
exit(path) { | ||
if (!canMergeBinaryExpression(path.node)) { | ||
return; | ||
} | ||
|
||
const {left, right} = path.node; | ||
if (t.isTemplateLiteral(right)) { | ||
const rightQuasis = cloneNodes(right.quasis); | ||
|
||
if (t.isTemplateLiteral(left)) { | ||
const leftQuasis = cloneNodes(left.quasis); | ||
const finalLeftQuasi = leftQuasis[leftQuasis.length - 1]; | ||
leftQuasis[leftQuasis.length - 1].value = { | ||
raw: finalLeftQuasi.value.raw + rightQuasis[0].value.raw, | ||
cooked: finalLeftQuasi.value.cooked + rightQuasis[0].value.raw, | ||
}; | ||
rightQuasis[0] = null; | ||
|
||
path.replaceWith( | ||
t.templateLiteral( | ||
[...leftQuasis, ...rightQuasis.filter(Boolean)], | ||
[ | ||
...cloneNodes(left.expressions), | ||
...cloneNodes(right.expressions), | ||
] | ||
) | ||
); | ||
return; | ||
} | ||
|
||
// Left is a literal, containing a value to merge into the right. | ||
const leftValue = escapeStringForTemplateLiteral(left.value); | ||
rightQuasis[0].value = { | ||
raw: leftValue + rightQuasis[0].value.raw, | ||
cooked: leftValue + rightQuasis[0].value.cooked, | ||
}; | ||
path.replaceWith( | ||
t.templateLiteral(rightQuasis, cloneNodes(right.expressions)) | ||
); | ||
} | ||
|
||
// Right is a literal containing a value to merge into the left. | ||
if (t.isTemplateLiteral(left)) { | ||
const rightValue = escapeStringForTemplateLiteral(right.value); | ||
const leftQuasis = cloneNodes(left.quasis); | ||
const finalLeftQuasi = leftQuasis[leftQuasis.length - 1]; | ||
leftQuasis[leftQuasis.length - 1].value = { | ||
raw: finalLeftQuasi.value.raw + rightValue, | ||
cooked: finalLeftQuasi.value.cooked + rightValue, | ||
}; | ||
|
||
path.replaceWith( | ||
t.templateLiteral(leftQuasis, cloneNodes(left.expressions)) | ||
); | ||
return; | ||
} | ||
|
||
// Merge two string literals | ||
if (t.isStringLiteral(left) && t.isStringLiteral(right)) { | ||
const newLiteral = t.cloneNode(left); | ||
newLiteral.value = left.value + String(right.value); | ||
path.replaceWith(newLiteral); | ||
} | ||
}, | ||
}, | ||
TemplateLiteral(path) { | ||
const {expressions, quasis} = path.node; | ||
const newQuasis = cloneNodes(quasis); | ||
const newExpressions = cloneNodes(expressions); | ||
let conversions = 0; | ||
|
||
for (let index = expressions.length; index >= 0; index--) { | ||
const expression = expressions[index]; | ||
if ( | ||
t.isStringLiteral(expression) || | ||
t.isNumericLiteral(expression) || | ||
t.isBooleanLiteral(expression) | ||
) { | ||
const {value} = expression; | ||
const readIndex = whichCloneQuasi(newQuasis, index + 1); | ||
const modifyIndex = whichCloneQuasi(newQuasis, index); | ||
const {value: changedValue} = newQuasis[readIndex]; | ||
const {value: previousValue} = newQuasis[modifyIndex]; | ||
|
||
newQuasis[modifyIndex] = t.templateElement({ | ||
raw: previousValue.raw + value + changedValue.raw, | ||
cooked: previousValue.cooked + value + changedValue.cooked, | ||
}); | ||
newQuasis[index + 1] = null; | ||
newExpressions[index] = null; | ||
conversions++; | ||
} | ||
} | ||
|
||
if (conversions === 0) { | ||
return; | ||
} | ||
|
||
path.replaceWith( | ||
t.templateLiteral( | ||
newQuasis.filter(Boolean), | ||
newExpressions.filter(Boolean) | ||
) | ||
); | ||
}, | ||
}, | ||
}; | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/** | ||
* 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. | ||
*/ | ||
|
||
let add = `/rtv` + `/bar`; | ||
let multipleAdd = `/rtv` + `/bar` + `/`; | ||
let subtract = `/rtv` - `r`; | ||
let multiply = `/rtv` * `r`; | ||
let divide = `/rtv` / `r`; | ||
|
||
let numberStart = 1 + `/foo`; | ||
let stringStart = '1' + `/foo`; | ||
let numberEnd = `foo/` + 1; | ||
let stringEnd = `foo/` + '1'; | ||
let illegalCharacter = `Invalid share providers configuration for in bookend. ` + 'Value must be `true` or a params object.'; | ||
|
||
let stringLiterals = '1' + '2'; | ||
let numberLiterals = 1 + 2; | ||
let booleanLiterals = false + true; | ||
|
||
let identifiers = `${foo}` + `${bar}`; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"plugins": ["../../../../../babel-plugin-transform-stringish-literals"] | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
/** | ||
* 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. | ||
*/ | ||
let add = `/rtv/bar`; | ||
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. Terser seems to accomplish all of these. 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. Looking at the output of our build process, it is not for us. Would prefer to have less reliance on terser for these kinds of changes. |
||
let multipleAdd = `/rtv/bar/`; | ||
let subtract = `/rtv` - `r`; | ||
let multiply = `/rtv` * `r`; | ||
let divide = `/rtv` / `r`; | ||
let numberStart = `1/foo`; | ||
let stringStart = `1/foo`; | ||
let numberEnd = `foo/1`; | ||
let stringEnd = `foo/1`; | ||
let illegalCharacter = `Invalid share providers configuration for in bookend. Value must be \`true\` or a params object.`; | ||
let stringLiterals = "12"; | ||
let numberLiterals = 1 + 2; | ||
let booleanLiterals = false + true; | ||
let identifiers = `${foo}${bar}`; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
/** | ||
* 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. | ||
*/ | ||
|
||
let none = `/rtv/100/log-messages.simple.json`; | ||
let start = `${'123'}/foo`; | ||
let middle = `/rtv/${'012003312116250'}/log-messages.simple.json`; | ||
let end = `rtv/${'123'}`; | ||
let number = `${123}/foo`; | ||
let boolean = `${true}/foo`; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"plugins": ["../../../../../babel-plugin-transform-stringish-literals"] | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
/** | ||
* 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. | ||
*/ | ||
let none = `/rtv/100/log-messages.simple.json`; | ||
let start = `123/foo`; | ||
let middle = `/rtv/012003312116250/log-messages.simple.json`; | ||
let end = `rtv/123`; | ||
let number = `123/foo`; | ||
let boolean = `true/foo`; |
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); |
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.
I think we can simplify this a lot using Babel's evaluator: https://astexplorer.net/#/gist/2f15d0fd95e70d31d1eb9314d68d5b94/b6b9de562a4f71911db7585d0564d74aff85bf41
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.
Absolutely. Good idea, will address in a followup.