From 01179c1ddf59bf49c3544fcab2624359d5bc66cd Mon Sep 17 00:00:00 2001 From: Eugene Kashida Date: Tue, 26 Mar 2024 00:16:24 -0700 Subject: [PATCH] fix: normalize acorn node --- .../src/parser/expression-complex/html.ts | 19 ++++++++++++++++++- .../template-compiler/src/parser/index.ts | 12 +----------- 2 files changed, 19 insertions(+), 12 deletions(-) diff --git a/packages/@lwc/template-compiler/src/parser/expression-complex/html.ts b/packages/@lwc/template-compiler/src/parser/expression-complex/html.ts index 3660ad546b..5655552733 100644 --- a/packages/@lwc/template-compiler/src/parser/expression-complex/html.ts +++ b/packages/@lwc/template-compiler/src/parser/expression-complex/html.ts @@ -91,6 +91,20 @@ function acornNodeToSourceLocation(node: Node): SourceLocation { }); } +function normalize(node: any) { + if (typeof node === 'object' && node !== null) { + if (node.loc) { + node.location = acornNodeToSourceLocation(node); + } + delete node.loc; + delete node.range; + delete node.sourceFile; + Object.keys(node).forEach((key) => { + normalize(node[key]); + }); + } +} + /** * This class extends `parse5`'s internal tokenizer. * @@ -167,10 +181,13 @@ class TemplateHtmlTokenizer extends Tokenizer { ['expression must end with curly brace.'] ); + const normalized = structuredClone(estreeNode); + normalize(normalized); + // Parsed expressions that are cached here will be later retrieved when the // LWC template AST is being constructed. this.parser.preparsedJsExpressions.set(expressionStart, { - parsedExpression: estreeNode, + parsedExpression: normalized, rawText: expressionTextNodeValue, sourceLocation: acornNodeToSourceLocation(estreeNode), }); diff --git a/packages/@lwc/template-compiler/src/parser/index.ts b/packages/@lwc/template-compiler/src/parser/index.ts index 7704d22be8..165c40dafd 100644 --- a/packages/@lwc/template-compiler/src/parser/index.ts +++ b/packages/@lwc/template-compiler/src/parser/index.ts @@ -503,17 +503,7 @@ function parseText(ctx: ParserCtx, parse5Text: parse5Tools.TextNode): Text[] { throw new Error('Implementation error: cannot find preparsed template expression'); } - const { type, start, end, name } = entry.parsedExpression as any; - - const value = { - type, - start, - end, - name, - location: ast.sourceLocation(location), - }; - - return [ast.text(rawText, value, location)]; + return [ast.text(rawText, entry.parsedExpression, location)]; } // Split the text node content arround expression and create node for each