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

🚀 Flatten Literals in TemplateLiterals #27516

Merged
@@ -0,0 +1,73 @@
/**
* 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.
*/

module.exports = function ({types: t}) {
function whichCloneQuasi(clonedQuasis, index) {
for (let i = index; i >= 0; i--) {
const quasi = clonedQuasis[i];
if (quasi !== null) {
return index;
}
}
}

return {
name: 'flatten-template-literals',
visitor: {
TemplateLiteral(path) {
const {expressions, quasis} = path.node;
let newQuasis = [...quasis.map((quasi) => t.cloneNode(quasi))];
let newExpressions = [
...expressions.map((expression) => t.cloneNode(expression)),
];
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;
}

newQuasis = newQuasis.filter(Boolean);
newExpressions = newExpressions.filter(Boolean);

path.replaceWith(t.templateLiteral(newQuasis, newExpressions));
path.skip();
},
},
};
};
@@ -0,0 +1,24 @@
/**
* 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`;
@@ -0,0 +1,3 @@
{
"plugins": ["../../../../../babel-plugin-transform-templates"]
}
@@ -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`;
@@ -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);
1 change: 1 addition & 0 deletions build-system/compile/build.conf.js
Expand Up @@ -46,6 +46,7 @@ const postCompilationPlugins = (isEsmBuild) =>
? [
localPlugin('transform-minified-comments'),
localPlugin('transform-function-declarations'),
localPlugin('transform-templates'),
kristoferbaxter marked this conversation as resolved.
Show resolved Hide resolved
]
: [];

Expand Down