Skip to content

Commit b5f51dc

Browse files
fix(js): give the import of import.meta its start location
The owned parseExprImport built the meta identifier without the start position acorn passes along, so with locations on it carried only an end. The build never tracks locations, which is why the parity sweeps missed it.
1 parent 4f13d80 commit b5f51dc

2 files changed

Lines changed: 24 additions & 1 deletion

File tree

‎lib/javascript/syntax.js‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10384,7 +10384,10 @@ class WebpackParser extends BaseParser {
1038410384
if (this.type === tokTypes.parenL && !forNew) {
1038510385
node = this.parseDynamicImport(start);
1038610386
} else if (this.type === tokTypes.dot) {
10387-
const meta = this.startNodeAt(start.start, undefined);
10387+
const meta = this.startNodeAt(
10388+
start.start,
10389+
start.loc ? start.loc.start : undefined
10390+
);
1038810391
meta.name = "import";
1038910392
start.meta = this.finishNode(meta, "Identifier");
1039010393
node = this.parseImportMeta(start);

‎test/WebpackParser.unittest.js‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1801,6 +1801,26 @@ describe("WebpackParser", () => {
18011801
}).ast.body[0].type
18021802
).toBe("ImportDeclaration");
18031803
});
1804+
1805+
it("should locate the `import` of import.meta when acorn tracks locations", () => {
1806+
for (const importPhases of [true, false]) {
1807+
const declaration =
1808+
/** @type {import("estree").VariableDeclaration} */ (
1809+
parse("const y = import.meta;", {
1810+
sourceType: "module",
1811+
importPhases,
1812+
locations: true
1813+
}).ast.body[0]
1814+
);
1815+
const meta = /** @type {import("estree").MetaProperty} */ (
1816+
declaration.declarations[0].init
1817+
);
1818+
expect(meta.meta.loc).toEqual({
1819+
start: { line: 1, column: 10 },
1820+
end: { line: 1, column: 16 }
1821+
});
1822+
}
1823+
});
18041824
});
18051825

18061826
describe("import attributes (owned with/assert clause)", () => {

0 commit comments

Comments
 (0)