Skip to content

Commit 157571a

Browse files
committed
fix(parsers): keep a tagged template's raw text and a __proto__ key in the script compare
1 parent e0077a8 commit 157571a

2 files changed

Lines changed: 8 additions & 4 deletions

File tree

‎packages/parsers/src/scriptShape.test.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ const w = { x: window.innerWidth - 40 }
2828
["an array hole", `f([1, , 2])`, `f([1, 2])`],
2929
["a comma that joins two calls", `if (c) f(), g()`, `if (c) f(); g()`],
3030
["a semicolon that splits a call", `a;\n(b)`, `a\n(b)`],
31+
["a tagged template's raw text", "String.raw`p\\nq`", "String.raw`p\nq`"],
32+
["a prototype written as a key", `({ __proto__: __proto__ })`, `({ __proto__ })`],
3133
])("tells scripts apart that differ only in %s", (_name, a, b) => {
3234
expect(same(a, b)).toBe(false);
3335
});

‎packages/parsers/src/scriptShape.ts‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import * as acorn from "acorn";
22

3-
/** Source details a bundler's re-print changes without changing what the code does. */
4-
const PRINT_ONLY = new Set(["start", "end", "raw", "shorthand"]);
3+
type Shape = { type?: unknown; key?: { name?: unknown } };
54

65
/**
76
* A script's syntax tree as one string, or null when it does not parse. Comments, layout, quote
@@ -14,8 +13,11 @@ export function scriptShape(code: string, maskNumbers = false): string | null {
1413
} catch {
1514
return null;
1615
}
17-
return JSON.stringify(tree, function (this: { type?: unknown }, key: string, value: unknown) {
18-
if (PRINT_ONLY.has(key)) return undefined;
16+
return JSON.stringify(tree, function (this: Shape, key: string, value: unknown) {
17+
// What a re-print changes without changing behaviour; a tagged template reads its raw text.
18+
if (key === "start" || key === "end") return undefined;
19+
if (key === "raw" && this.type === "Literal") return undefined;
20+
if (key === "shorthand" && this.key?.name !== "__proto__") return undefined;
1921
if (typeof value === "bigint") return `${value}n`;
2022
if (maskNumbers && key === "value" && typeof value === "number" && this.type === "Literal")
2123
return "num";

0 commit comments

Comments
 (0)