From 3fd6f52241718b860d898603821c8155be66e6c5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=B2=20Ribaudo?= Date: Thu, 31 Aug 2023 12:10:13 +0200 Subject: [PATCH 1/8] Fix passing filename from `@babel/core` to `@babel/parser` --- packages/babel-core/src/transformation/normalize-opts.ts | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/packages/babel-core/src/transformation/normalize-opts.ts b/packages/babel-core/src/transformation/normalize-opts.ts index 571d1705530d..58e32f9560ed 100644 --- a/packages/babel-core/src/transformation/normalize-opts.ts +++ b/packages/babel-core/src/transformation/normalize-opts.ts @@ -2,6 +2,8 @@ import path from "path"; import type { ResolvedConfig } from "../config/index.ts"; export default function normalizeOptions(config: ResolvedConfig): {} { + // TODO: Everything in this function is basically typed as `any`. Improve it. + const { filename, cwd, @@ -30,7 +32,10 @@ export default function normalizeOptions(config: ResolvedConfig): {} { sourceType: path.extname(filenameRelative) === ".mjs" ? "module" : sourceType, - sourceFileName: filename, + // TODO: @babel/parser uses sourceFilename, while @babel/generator and + // @babel/core use sourceFileName. Eventualy align them. + // https://github.com/babel/babel/pull/13518 + sourceFilename: filename, plugins: [], ...opts.parserOpts, }, From 67771109c93896ceaccecdcc7c5896cbc8eb789e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=B2=20Ribaudo?= Date: Thu, 31 Aug 2023 12:15:53 +0200 Subject: [PATCH 2/8] Use relative filename --- packages/babel-core/src/transformation/normalize-opts.ts | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/packages/babel-core/src/transformation/normalize-opts.ts b/packages/babel-core/src/transformation/normalize-opts.ts index 58e32f9560ed..93905be9274c 100644 --- a/packages/babel-core/src/transformation/normalize-opts.ts +++ b/packages/babel-core/src/transformation/normalize-opts.ts @@ -35,7 +35,10 @@ export default function normalizeOptions(config: ResolvedConfig): {} { // TODO: @babel/parser uses sourceFilename, while @babel/generator and // @babel/core use sourceFileName. Eventualy align them. // https://github.com/babel/babel/pull/13518 - sourceFilename: filename, + // We use the relative filename here so that source maps do not include + // absolute paths, and are thus portable. + sourceFilename: + filenameRelative === "unknown" ? undefined : filenameRelative, plugins: [], ...opts.parserOpts, }, From 8b614436703eae4958210f7038909bf9274dea79 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=B2=20Ribaudo?= Date: Thu, 31 Aug 2023 12:18:23 +0200 Subject: [PATCH 3/8] Update snapshot --- .../test/fixtures/parse/output.json | 47 +++++++++++++------ 1 file changed, 32 insertions(+), 15 deletions(-) diff --git a/packages/babel-core/test/fixtures/parse/output.json b/packages/babel-core/test/fixtures/parse/output.json index 1a400f953757..591ee626ebe9 100644 --- a/packages/babel-core/test/fixtures/parse/output.json +++ b/packages/babel-core/test/fixtures/parse/output.json @@ -2,7 +2,6 @@ "type": "File", "start": 0, "end": 91, - "errors": [], "loc": { "start": { "line": 1, @@ -13,8 +12,10 @@ "line": 7, "column": 0, "index": 91 - } + }, + "filename": "input.js" }, + "errors": [], "program": { "type": "Program", "start": 0, @@ -29,10 +30,11 @@ "line": 7, "column": 0, "index": 91 - } + }, + "filename": "input.js" }, - "interpreter": null, "sourceType": "module", + "interpreter": null, "body": [ { "type": "ClassDeclaration", @@ -48,7 +50,8 @@ "line": 2, "column": 17, "index": 29 - } + }, + "filename": "input.js" }, "decorators": [ { @@ -65,7 +68,8 @@ "line": 1, "column": 11, "index": 11 - } + }, + "filename": "input.js" }, "expression": { "type": "Identifier", @@ -82,6 +86,7 @@ "column": 11, "index": 11 }, + "filename": "input.js", "identifierName": "annotation" }, "name": "annotation" @@ -103,6 +108,7 @@ "column": 13, "index": 25 }, + "filename": "input.js", "identifierName": "MyClass" }, "name": "MyClass" @@ -122,7 +128,8 @@ "line": 2, "column": 17, "index": 29 - } + }, + "filename": "input.js" }, "body": [] } @@ -141,7 +148,8 @@ "line": 6, "column": 1, "index": 90 - } + }, + "filename": "input.js" }, "id": { "type": "Identifier", @@ -158,6 +166,7 @@ "column": 19, "index": 50 }, + "filename": "input.js", "identifierName": "annotation" }, "name": "annotation" @@ -180,6 +189,7 @@ "column": 26, "index": 57 }, + "filename": "input.js", "identifierName": "target" }, "name": "target" @@ -199,7 +209,8 @@ "line": 6, "column": 1, "index": 90 - } + }, + "filename": "input.js" }, "body": [ { @@ -216,7 +227,8 @@ "line": 5, "column": 27, "index": 88 - } + }, + "filename": "input.js" }, "expression": { "type": "AssignmentExpression", @@ -232,7 +244,8 @@ "line": 5, "column": 26, "index": 87 - } + }, + "filename": "input.js" }, "operator": "=", "left": { @@ -249,7 +262,8 @@ "line": 5, "column": 19, "index": 80 - } + }, + "filename": "input.js" }, "object": { "type": "Identifier", @@ -266,10 +280,12 @@ "column": 9, "index": 70 }, + "filename": "input.js", "identifierName": "target" }, "name": "target" }, + "computed": false, "property": { "type": "Identifier", "start": 71, @@ -285,11 +301,11 @@ "column": 19, "index": 80 }, + "filename": "input.js", "identifierName": "annotated" }, "name": "annotated" - }, - "computed": false + } }, "right": { "type": "BooleanLiteral", @@ -305,7 +321,8 @@ "line": 5, "column": 26, "index": 87 - } + }, + "filename": "input.js" }, "value": true } From 4ac75e83745bf65a726fa759706f542bc449186d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=B2=20Ribaudo?= Date: Thu, 31 Aug 2023 12:19:12 +0200 Subject: [PATCH 4/8] Update test (WRONG) --- packages/babel-core/test/api.js | 8 +- .../fixtures/parse/output-with-filename.json | 339 ++++++++++++++++++ .../test/fixtures/parse/output.json | 39 +- packages/babel-core/test/parse.js | 2 +- 4 files changed, 355 insertions(+), 33 deletions(-) create mode 100644 packages/babel-core/test/fixtures/parse/output-with-filename.json diff --git a/packages/babel-core/test/api.js b/packages/babel-core/test/api.js index 79a0d9ae7973..b31f01764ef1 100644 --- a/packages/babel-core/test/api.js +++ b/packages/babel-core/test/api.js @@ -648,14 +648,14 @@ describe("api", function () { }); }); - it("default source map filename", function () { - return transformAsync("var a = 10;", { + it("default source map filename", async function () { + const result = await transformAsync("var a = 10;", { cwd: "/some/absolute", filename: "/some/absolute/file/path.js", sourceMaps: true, - }).then(function (result) { - expect(result.map.sources).toEqual(["path.js"]); }); + + expect(result.map.sources).toEqual(["path.js", "file/path.js"]); }); it("code option false", function () { diff --git a/packages/babel-core/test/fixtures/parse/output-with-filename.json b/packages/babel-core/test/fixtures/parse/output-with-filename.json new file mode 100644 index 000000000000..591ee626ebe9 --- /dev/null +++ b/packages/babel-core/test/fixtures/parse/output-with-filename.json @@ -0,0 +1,339 @@ +{ + "type": "File", + "start": 0, + "end": 91, + "loc": { + "start": { + "line": 1, + "column": 0, + "index": 0 + }, + "end": { + "line": 7, + "column": 0, + "index": 91 + }, + "filename": "input.js" + }, + "errors": [], + "program": { + "type": "Program", + "start": 0, + "end": 91, + "loc": { + "start": { + "line": 1, + "column": 0, + "index": 0 + }, + "end": { + "line": 7, + "column": 0, + "index": 91 + }, + "filename": "input.js" + }, + "sourceType": "module", + "interpreter": null, + "body": [ + { + "type": "ClassDeclaration", + "start": 0, + "end": 29, + "loc": { + "start": { + "line": 1, + "column": 0, + "index": 0 + }, + "end": { + "line": 2, + "column": 17, + "index": 29 + }, + "filename": "input.js" + }, + "decorators": [ + { + "type": "Decorator", + "start": 0, + "end": 11, + "loc": { + "start": { + "line": 1, + "column": 0, + "index": 0 + }, + "end": { + "line": 1, + "column": 11, + "index": 11 + }, + "filename": "input.js" + }, + "expression": { + "type": "Identifier", + "start": 1, + "end": 11, + "loc": { + "start": { + "line": 1, + "column": 1, + "index": 1 + }, + "end": { + "line": 1, + "column": 11, + "index": 11 + }, + "filename": "input.js", + "identifierName": "annotation" + }, + "name": "annotation" + } + } + ], + "id": { + "type": "Identifier", + "start": 18, + "end": 25, + "loc": { + "start": { + "line": 2, + "column": 6, + "index": 18 + }, + "end": { + "line": 2, + "column": 13, + "index": 25 + }, + "filename": "input.js", + "identifierName": "MyClass" + }, + "name": "MyClass" + }, + "superClass": null, + "body": { + "type": "ClassBody", + "start": 26, + "end": 29, + "loc": { + "start": { + "line": 2, + "column": 14, + "index": 26 + }, + "end": { + "line": 2, + "column": 17, + "index": 29 + }, + "filename": "input.js" + }, + "body": [] + } + }, + { + "type": "FunctionDeclaration", + "start": 31, + "end": 90, + "loc": { + "start": { + "line": 4, + "column": 0, + "index": 31 + }, + "end": { + "line": 6, + "column": 1, + "index": 90 + }, + "filename": "input.js" + }, + "id": { + "type": "Identifier", + "start": 40, + "end": 50, + "loc": { + "start": { + "line": 4, + "column": 9, + "index": 40 + }, + "end": { + "line": 4, + "column": 19, + "index": 50 + }, + "filename": "input.js", + "identifierName": "annotation" + }, + "name": "annotation" + }, + "generator": false, + "async": false, + "params": [ + { + "type": "Identifier", + "start": 51, + "end": 57, + "loc": { + "start": { + "line": 4, + "column": 20, + "index": 51 + }, + "end": { + "line": 4, + "column": 26, + "index": 57 + }, + "filename": "input.js", + "identifierName": "target" + }, + "name": "target" + } + ], + "body": { + "type": "BlockStatement", + "start": 59, + "end": 90, + "loc": { + "start": { + "line": 4, + "column": 28, + "index": 59 + }, + "end": { + "line": 6, + "column": 1, + "index": 90 + }, + "filename": "input.js" + }, + "body": [ + { + "type": "ExpressionStatement", + "start": 64, + "end": 88, + "loc": { + "start": { + "line": 5, + "column": 3, + "index": 64 + }, + "end": { + "line": 5, + "column": 27, + "index": 88 + }, + "filename": "input.js" + }, + "expression": { + "type": "AssignmentExpression", + "start": 64, + "end": 87, + "loc": { + "start": { + "line": 5, + "column": 3, + "index": 64 + }, + "end": { + "line": 5, + "column": 26, + "index": 87 + }, + "filename": "input.js" + }, + "operator": "=", + "left": { + "type": "MemberExpression", + "start": 64, + "end": 80, + "loc": { + "start": { + "line": 5, + "column": 3, + "index": 64 + }, + "end": { + "line": 5, + "column": 19, + "index": 80 + }, + "filename": "input.js" + }, + "object": { + "type": "Identifier", + "start": 64, + "end": 70, + "loc": { + "start": { + "line": 5, + "column": 3, + "index": 64 + }, + "end": { + "line": 5, + "column": 9, + "index": 70 + }, + "filename": "input.js", + "identifierName": "target" + }, + "name": "target" + }, + "computed": false, + "property": { + "type": "Identifier", + "start": 71, + "end": 80, + "loc": { + "start": { + "line": 5, + "column": 10, + "index": 71 + }, + "end": { + "line": 5, + "column": 19, + "index": 80 + }, + "filename": "input.js", + "identifierName": "annotated" + }, + "name": "annotated" + } + }, + "right": { + "type": "BooleanLiteral", + "start": 83, + "end": 87, + "loc": { + "start": { + "line": 5, + "column": 22, + "index": 83 + }, + "end": { + "line": 5, + "column": 26, + "index": 87 + }, + "filename": "input.js" + }, + "value": true + } + } + } + ], + "directives": [] + } + } + ], + "directives": [] + }, + "comments": [] +} diff --git a/packages/babel-core/test/fixtures/parse/output.json b/packages/babel-core/test/fixtures/parse/output.json index 591ee626ebe9..1af0d0d0d23c 100644 --- a/packages/babel-core/test/fixtures/parse/output.json +++ b/packages/babel-core/test/fixtures/parse/output.json @@ -12,8 +12,7 @@ "line": 7, "column": 0, "index": 91 - }, - "filename": "input.js" + } }, "errors": [], "program": { @@ -30,8 +29,7 @@ "line": 7, "column": 0, "index": 91 - }, - "filename": "input.js" + } }, "sourceType": "module", "interpreter": null, @@ -50,8 +48,7 @@ "line": 2, "column": 17, "index": 29 - }, - "filename": "input.js" + } }, "decorators": [ { @@ -68,8 +65,7 @@ "line": 1, "column": 11, "index": 11 - }, - "filename": "input.js" + } }, "expression": { "type": "Identifier", @@ -86,7 +82,6 @@ "column": 11, "index": 11 }, - "filename": "input.js", "identifierName": "annotation" }, "name": "annotation" @@ -108,7 +103,6 @@ "column": 13, "index": 25 }, - "filename": "input.js", "identifierName": "MyClass" }, "name": "MyClass" @@ -128,8 +122,7 @@ "line": 2, "column": 17, "index": 29 - }, - "filename": "input.js" + } }, "body": [] } @@ -148,8 +141,7 @@ "line": 6, "column": 1, "index": 90 - }, - "filename": "input.js" + } }, "id": { "type": "Identifier", @@ -166,7 +158,6 @@ "column": 19, "index": 50 }, - "filename": "input.js", "identifierName": "annotation" }, "name": "annotation" @@ -189,7 +180,6 @@ "column": 26, "index": 57 }, - "filename": "input.js", "identifierName": "target" }, "name": "target" @@ -209,8 +199,7 @@ "line": 6, "column": 1, "index": 90 - }, - "filename": "input.js" + } }, "body": [ { @@ -227,8 +216,7 @@ "line": 5, "column": 27, "index": 88 - }, - "filename": "input.js" + } }, "expression": { "type": "AssignmentExpression", @@ -244,8 +232,7 @@ "line": 5, "column": 26, "index": 87 - }, - "filename": "input.js" + } }, "operator": "=", "left": { @@ -262,8 +249,7 @@ "line": 5, "column": 19, "index": 80 - }, - "filename": "input.js" + } }, "object": { "type": "Identifier", @@ -280,7 +266,6 @@ "column": 9, "index": 70 }, - "filename": "input.js", "identifierName": "target" }, "name": "target" @@ -301,7 +286,6 @@ "column": 19, "index": 80 }, - "filename": "input.js", "identifierName": "annotated" }, "name": "annotated" @@ -321,8 +305,7 @@ "line": 5, "column": 26, "index": 87 - }, - "filename": "input.js" + } }, "value": true } diff --git a/packages/babel-core/test/parse.js b/packages/babel-core/test/parse.js index eeec4ce7869d..dcfa1813d4e8 100644 --- a/packages/babel-core/test/parse.js +++ b/packages/babel-core/test/parse.js @@ -18,7 +18,7 @@ function fixture(...args) { describe("parseSync", function () { it("should parse using configuration from .babelrc when a filename is provided", function () { const input = fs.readFileSync(fixture("input.js"), "utf8"); - const output = require(fixture("output")); + const output = require(fixture("output-with-filename.json")); const result = parseSync(input, { filename: fixture("input.js"), From 82cc0e022a54f71ac9c2973cd966ba047e17013d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=B2=20Ribaudo?= Date: Thu, 31 Aug 2023 12:30:27 +0200 Subject: [PATCH 5/8] Pass relative filename to generator also --- packages/babel-core/src/transformation/normalize-opts.ts | 9 ++++----- packages/babel-core/test/api.js | 2 +- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/packages/babel-core/src/transformation/normalize-opts.ts b/packages/babel-core/src/transformation/normalize-opts.ts index 93905be9274c..382b577a855f 100644 --- a/packages/babel-core/src/transformation/normalize-opts.ts +++ b/packages/babel-core/src/transformation/normalize-opts.ts @@ -17,7 +17,9 @@ export default function normalizeOptions(config: ResolvedConfig): {} { ? undefined : config.options.moduleRoot, - sourceFileName = path.basename(filenameRelative), + sourceFileName = filenameRelative === "unknown" + ? undefined + : filenameRelative, comments = true, compact = "auto", @@ -35,10 +37,7 @@ export default function normalizeOptions(config: ResolvedConfig): {} { // TODO: @babel/parser uses sourceFilename, while @babel/generator and // @babel/core use sourceFileName. Eventualy align them. // https://github.com/babel/babel/pull/13518 - // We use the relative filename here so that source maps do not include - // absolute paths, and are thus portable. - sourceFilename: - filenameRelative === "unknown" ? undefined : filenameRelative, + sourceFilename: sourceFileName, plugins: [], ...opts.parserOpts, }, diff --git a/packages/babel-core/test/api.js b/packages/babel-core/test/api.js index b31f01764ef1..c223313b837a 100644 --- a/packages/babel-core/test/api.js +++ b/packages/babel-core/test/api.js @@ -655,7 +655,7 @@ describe("api", function () { sourceMaps: true, }); - expect(result.map.sources).toEqual(["path.js", "file/path.js"]); + expect(result.map.sources).toEqual(["file/path.js"]); }); it("code option false", function () { From a5e38f9b1314d13fae303752c1b36232ffce54c0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=B2=20Ribaudo?= Date: Thu, 31 Aug 2023 14:44:45 +0200 Subject: [PATCH 6/8] Fix behavior with sourceRoot --- packages/babel-core/src/transformation/normalize-opts.ts | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/packages/babel-core/src/transformation/normalize-opts.ts b/packages/babel-core/src/transformation/normalize-opts.ts index 382b577a855f..9e4e89b8c79e 100644 --- a/packages/babel-core/src/transformation/normalize-opts.ts +++ b/packages/babel-core/src/transformation/normalize-opts.ts @@ -58,7 +58,12 @@ export default function normalizeOptions(config: ResolvedConfig): {} { sourceMaps, sourceRoot, - sourceFileName, + sourceFileName: + sourceRoot === undefined + ? sourceFileName + : // @babel/generator will prepend sourceFileName with sourceRoot, + // so we need to remove it here. + path.relative(sourceRoot, sourceFileName), ...opts.generatorOpts, }, }; From 05053431a30730cac2f3338902229f04f0033402 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=B2=20Ribaudo?= Date: Thu, 31 Aug 2023 14:51:02 +0200 Subject: [PATCH 7/8] Fix mappings generation --- packages/babel-core/src/transformation/normalize-opts.ts | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/packages/babel-core/src/transformation/normalize-opts.ts b/packages/babel-core/src/transformation/normalize-opts.ts index 9e4e89b8c79e..212801d8efae 100644 --- a/packages/babel-core/src/transformation/normalize-opts.ts +++ b/packages/babel-core/src/transformation/normalize-opts.ts @@ -59,7 +59,13 @@ export default function normalizeOptions(config: ResolvedConfig): {} { sourceRoot, sourceFileName: - sourceRoot === undefined + // If there is no filename, we use `"unknown"` in the source map + // `sources` array as a fallback. Due to how @babel/generator works, + // if we passed `undefined` there would be no generated mappings. + // Additionally, `undefined` isn't JSON-serializable. + sourceFileName == null + ? "unknown" + : sourceRoot == null ? sourceFileName : // @babel/generator will prepend sourceFileName with sourceRoot, // so we need to remove it here. From 18fb9a88e560d8a8cb6c891c42a922cd21c3bae8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=B2=20Ribaudo?= Date: Thu, 31 Aug 2023 14:53:03 +0200 Subject: [PATCH 8/8] Add TODO comment --- packages/babel-core/src/transformation/normalize-opts.ts | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/packages/babel-core/src/transformation/normalize-opts.ts b/packages/babel-core/src/transformation/normalize-opts.ts index 212801d8efae..50bb169a263b 100644 --- a/packages/babel-core/src/transformation/normalize-opts.ts +++ b/packages/babel-core/src/transformation/normalize-opts.ts @@ -4,6 +4,11 @@ import type { ResolvedConfig } from "../config/index.ts"; export default function normalizeOptions(config: ResolvedConfig): {} { // TODO: Everything in this function is basically typed as `any`. Improve it. + // TODO(@nicolo-ribaudo): Currently, soure map's `sources` is generated taking + // into account both the options passed to the parser and the options passed + // to the generator. If they disagree, both are included. Clean this up, so + // that there is a single source of thruth. + const { filename, cwd,