Skip to content

Commit

Permalink
fix: normalize acorn node
Browse files Browse the repository at this point in the history
  • Loading branch information
ekashida committed Mar 26, 2024
1 parent 2e7baae commit 01179c1
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -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),
});
Expand Down
12 changes: 1 addition & 11 deletions packages/@lwc/template-compiler/src/parser/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 01179c1

Please sign in to comment.