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

🚀 Move from custom logic to babel evaluate #27564

Closed
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
Expand Up @@ -14,134 +14,18 @@
* 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) {
'BinaryExpression|TemplateLiteral': function (path) {
const e = path.evaluate();
if (!e.confident) {
return;
}

path.replaceWith(
t.templateLiteral(
newQuasis.filter(Boolean),
newExpressions.filter(Boolean)
)
);
path.replaceWith(t.valueToNode(e.value));
path.skip();
},
},
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,17 @@
* 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 add = "/rtv/bar";
let multipleAdd = "/rtv/bar/";
let subtract = 0 / 0;
let multiply = 0 / 0;
let divide = 0 / 0;
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}`;
let numberLiterals = 3;
let booleanLiterals = 1;
let identifiers = `${foo}` + `${bar}`;
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@
* 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`;
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";