Skip to content

Commit

Permalink
perf: avoid template literals getting stuck for big member expression…
Browse files Browse the repository at this point in the history
… chains
  • Loading branch information
j4k0xb committed Jan 29, 2024
1 parent 2d889c6 commit 5c35af8
Showing 1 changed file with 10 additions and 22 deletions.
32 changes: 10 additions & 22 deletions packages/webcrack/src/transpile/transforms/template-literals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,17 +59,10 @@ export default {
name: 'template-literals',
tags: ['unsafe'],
visitor() {
const concatMatcher: m.Matcher<t.CallExpression> = m.or(
m.callExpression(
constMemberExpression(
m.or(
m.stringLiteral(),
m.matcher((node) => concatMatcher.match(node)),
),
'concat',
),
m.arrayOf(m.anyExpression()),
),
const string = m.capture(m.or(m.stringLiteral(), m.templateLiteral()));
const concatMatcher = m.callExpression(
constMemberExpression(string, 'concat'),
m.arrayOf(m.anyExpression()),
);

return {
Expand All @@ -93,22 +86,17 @@ export default {
},
CallExpression: {
exit(path) {
if (
concatMatcher.match(path.node) &&
!concatMatcher.match(path.parentPath.parent)
) {
if (concatMatcher.match(path.node)) {
const template = t.templateLiteral(
[t.templateElement({ raw: '' })],
[],
);
let current: t.Expression = path.node;
while (current.type === 'CallExpression') {
for (const arg of current.arguments.reverse()) {
unshift(template, arg as t.Expression);
}
current = (current.callee as t.MemberExpression).object;
push(template, string.current!);

for (const arg of path.node.arguments) {
push(template, arg as t.Expression);
}
unshift(template, current);

path.replaceWith(template);
this.changes++;
}
Expand Down

0 comments on commit 5c35af8

Please sign in to comment.