From 5c35af88ac6e0c217fe07e36ec95eb924458627a Mon Sep 17 00:00:00 2001 From: j4k0xb <55899582+j4k0xb@users.noreply.github.com> Date: Mon, 29 Jan 2024 19:29:11 +0100 Subject: [PATCH] perf: avoid template literals getting stuck for big member expression chains --- .../transpile/transforms/template-literals.ts | 32 ++++++------------- 1 file changed, 10 insertions(+), 22 deletions(-) diff --git a/packages/webcrack/src/transpile/transforms/template-literals.ts b/packages/webcrack/src/transpile/transforms/template-literals.ts index 60499901..03104afc 100644 --- a/packages/webcrack/src/transpile/transforms/template-literals.ts +++ b/packages/webcrack/src/transpile/transforms/template-literals.ts @@ -59,17 +59,10 @@ export default { name: 'template-literals', tags: ['unsafe'], visitor() { - const concatMatcher: m.Matcher = 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 { @@ -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++; }