From 51a0caa8a2dac2651856035760413d76fb422d77 Mon Sep 17 00:00:00 2001 From: Federico Ciardi Date: Thu, 19 Aug 2021 21:54:53 +0200 Subject: [PATCH] fix: set parserOpts.sourceFilename from root sourceFileName (#13532) * Partial #13518 * Update tests --- .../src/transformation/normalize-opts.ts | 2 +- .../{ => filename-provided}/.babelignore | 0 .../parse/{ => filename-provided}/.babelrc | 0 .../parse/{ => filename-provided}/input.js | 0 .../parse/filename-provided/output.json | 298 ++++++++++++++++++ .../parse/passed-in-configuration/input.js | 6 + .../{ => passed-in-configuration}/output.json | 0 .../sourceFileName-provided/.babelignore | 0 .../parse/sourceFileName-provided/.babelrc | 5 + .../parse/sourceFileName-provided/input.js | 6 + .../parse/sourceFileName-provided/output.json | 298 ++++++++++++++++++ packages/babel-core/test/parse.js | 31 +- 12 files changed, 640 insertions(+), 6 deletions(-) rename packages/babel-core/test/fixtures/parse/{ => filename-provided}/.babelignore (100%) rename packages/babel-core/test/fixtures/parse/{ => filename-provided}/.babelrc (100%) rename packages/babel-core/test/fixtures/parse/{ => filename-provided}/input.js (100%) create mode 100644 packages/babel-core/test/fixtures/parse/filename-provided/output.json create mode 100644 packages/babel-core/test/fixtures/parse/passed-in-configuration/input.js rename packages/babel-core/test/fixtures/parse/{ => passed-in-configuration}/output.json (100%) create mode 100644 packages/babel-core/test/fixtures/parse/sourceFileName-provided/.babelignore create mode 100644 packages/babel-core/test/fixtures/parse/sourceFileName-provided/.babelrc create mode 100644 packages/babel-core/test/fixtures/parse/sourceFileName-provided/input.js create mode 100644 packages/babel-core/test/fixtures/parse/sourceFileName-provided/output.json diff --git a/packages/babel-core/src/transformation/normalize-opts.ts b/packages/babel-core/src/transformation/normalize-opts.ts index bf6cda2d5193..e0dbf06f2e71 100644 --- a/packages/babel-core/src/transformation/normalize-opts.ts +++ b/packages/babel-core/src/transformation/normalize-opts.ts @@ -30,7 +30,7 @@ export default function normalizeOptions(config: ResolvedConfig): {} { sourceType: path.extname(filenameRelative) === ".mjs" ? "module" : sourceType, - sourceFileName: filename, + ...(sourceFileName !== "unknown" && { sourceFilename: sourceFileName }), plugins: [], ...opts.parserOpts, }, diff --git a/packages/babel-core/test/fixtures/parse/.babelignore b/packages/babel-core/test/fixtures/parse/filename-provided/.babelignore similarity index 100% rename from packages/babel-core/test/fixtures/parse/.babelignore rename to packages/babel-core/test/fixtures/parse/filename-provided/.babelignore diff --git a/packages/babel-core/test/fixtures/parse/.babelrc b/packages/babel-core/test/fixtures/parse/filename-provided/.babelrc similarity index 100% rename from packages/babel-core/test/fixtures/parse/.babelrc rename to packages/babel-core/test/fixtures/parse/filename-provided/.babelrc diff --git a/packages/babel-core/test/fixtures/parse/input.js b/packages/babel-core/test/fixtures/parse/filename-provided/input.js similarity index 100% rename from packages/babel-core/test/fixtures/parse/input.js rename to packages/babel-core/test/fixtures/parse/filename-provided/input.js diff --git a/packages/babel-core/test/fixtures/parse/filename-provided/output.json b/packages/babel-core/test/fixtures/parse/filename-provided/output.json new file mode 100644 index 000000000000..b87f1b654189 --- /dev/null +++ b/packages/babel-core/test/fixtures/parse/filename-provided/output.json @@ -0,0 +1,298 @@ +{ + "type": "File", + "start": 0, + "end": 91, + "errors": [], + "loc": { + "filename": "input.js", + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 7, + "column": 0 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 91, + "loc": { + "filename": "input.js", + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 7, + "column": 0 + } + }, + "interpreter": null, + "sourceType": "module", + "body": [{ + "type": "ClassDeclaration", + "start": 0, + "end": 29, + "loc": { + "filename": "input.js", + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 2, + "column": 17 + } + }, + "decorators": [{ + "type": "Decorator", + "start": 0, + "end": 11, + "loc": { + "filename": "input.js", + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 11 + } + }, + "expression": { + "type": "Identifier", + "start": 1, + "end": 11, + "loc": { + "filename": "input.js", + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 11 + }, + "identifierName": "annotation" + }, + "name": "annotation" + } + }], + "id": { + "type": "Identifier", + "start": 18, + "end": 25, + "loc": { + "filename": "input.js", + "start": { + "line": 2, + "column": 6 + }, + "end": { + "line": 2, + "column": 13 + }, + "identifierName": "MyClass" + }, + "name": "MyClass" + }, + "superClass": null, + "body": { + "type": "ClassBody", + "start": 26, + "end": 29, + "loc": { + "filename": "input.js", + "start": { + "line": 2, + "column": 14 + }, + "end": { + "line": 2, + "column": 17 + } + }, + "body": [] + } + }, + { + "type": "FunctionDeclaration", + "start": 31, + "end": 90, + "loc": { + "filename": "input.js", + "start": { + "line": 4, + "column": 0 + }, + "end": { + "line": 6, + "column": 1 + } + }, + "id": { + "type": "Identifier", + "start": 40, + "end": 50, + "loc": { + "filename": "input.js", + "start": { + "line": 4, + "column": 9 + }, + "end": { + "line": 4, + "column": 19 + }, + "identifierName": "annotation" + }, + "name": "annotation" + }, + "generator": false, + "async": false, + "params": [{ + "type": "Identifier", + "start": 51, + "end": 57, + "loc": { + "filename": "input.js", + "start": { + "line": 4, + "column": 20 + }, + "end": { + "line": 4, + "column": 26 + }, + "identifierName": "target" + }, + "name": "target" + }], + "body": { + "type": "BlockStatement", + "start": 59, + "end": 90, + "loc": { + "filename": "input.js", + "start": { + "line": 4, + "column": 28 + }, + "end": { + "line": 6, + "column": 1 + } + }, + "body": [{ + "type": "ExpressionStatement", + "start": 64, + "end": 88, + "loc": { + "filename": "input.js", + "start": { + "line": 5, + "column": 3 + }, + "end": { + "line": 5, + "column": 27 + } + }, + "expression": { + "type": "AssignmentExpression", + "start": 64, + "end": 87, + "loc": { + "filename": "input.js", + "start": { + "line": 5, + "column": 3 + }, + "end": { + "line": 5, + "column": 26 + } + }, + "operator": "=", + "left": { + "type": "MemberExpression", + "start": 64, + "end": 80, + "loc": { + "filename": "input.js", + "start": { + "line": 5, + "column": 3 + }, + "end": { + "line": 5, + "column": 19 + } + }, + "object": { + "type": "Identifier", + "start": 64, + "end": 70, + "loc": { + "filename": "input.js", + "start": { + "line": 5, + "column": 3 + }, + "end": { + "line": 5, + "column": 9 + }, + "identifierName": "target" + }, + "name": "target" + }, + "property": { + "type": "Identifier", + "start": 71, + "end": 80, + "loc": { + "filename": "input.js", + "start": { + "line": 5, + "column": 10 + }, + "end": { + "line": 5, + "column": 19 + }, + "identifierName": "annotated" + }, + "name": "annotated" + }, + "computed": false + }, + "right": { + "type": "BooleanLiteral", + "start": 83, + "end": 87, + "loc": { + "filename": "input.js", + "start": { + "line": 5, + "column": 22 + }, + "end": { + "line": 5, + "column": 26 + } + }, + "value": true + } + } + }], + "directives": [] + } + } + ], + "directives": [] + }, + "comments": [] +} diff --git a/packages/babel-core/test/fixtures/parse/passed-in-configuration/input.js b/packages/babel-core/test/fixtures/parse/passed-in-configuration/input.js new file mode 100644 index 000000000000..e8d44fe09050 --- /dev/null +++ b/packages/babel-core/test/fixtures/parse/passed-in-configuration/input.js @@ -0,0 +1,6 @@ +@annotation +class MyClass { } + +function annotation(target) { + target.annotated = true; +} diff --git a/packages/babel-core/test/fixtures/parse/output.json b/packages/babel-core/test/fixtures/parse/passed-in-configuration/output.json similarity index 100% rename from packages/babel-core/test/fixtures/parse/output.json rename to packages/babel-core/test/fixtures/parse/passed-in-configuration/output.json diff --git a/packages/babel-core/test/fixtures/parse/sourceFileName-provided/.babelignore b/packages/babel-core/test/fixtures/parse/sourceFileName-provided/.babelignore new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/packages/babel-core/test/fixtures/parse/sourceFileName-provided/.babelrc b/packages/babel-core/test/fixtures/parse/sourceFileName-provided/.babelrc new file mode 100644 index 000000000000..a4834e838136 --- /dev/null +++ b/packages/babel-core/test/fixtures/parse/sourceFileName-provided/.babelrc @@ -0,0 +1,5 @@ +{ + "parserOpts": { + "plugins": ["decorators-legacy"] + } +} diff --git a/packages/babel-core/test/fixtures/parse/sourceFileName-provided/input.js b/packages/babel-core/test/fixtures/parse/sourceFileName-provided/input.js new file mode 100644 index 000000000000..e8d44fe09050 --- /dev/null +++ b/packages/babel-core/test/fixtures/parse/sourceFileName-provided/input.js @@ -0,0 +1,6 @@ +@annotation +class MyClass { } + +function annotation(target) { + target.annotated = true; +} diff --git a/packages/babel-core/test/fixtures/parse/sourceFileName-provided/output.json b/packages/babel-core/test/fixtures/parse/sourceFileName-provided/output.json new file mode 100644 index 000000000000..b87f1b654189 --- /dev/null +++ b/packages/babel-core/test/fixtures/parse/sourceFileName-provided/output.json @@ -0,0 +1,298 @@ +{ + "type": "File", + "start": 0, + "end": 91, + "errors": [], + "loc": { + "filename": "input.js", + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 7, + "column": 0 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 91, + "loc": { + "filename": "input.js", + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 7, + "column": 0 + } + }, + "interpreter": null, + "sourceType": "module", + "body": [{ + "type": "ClassDeclaration", + "start": 0, + "end": 29, + "loc": { + "filename": "input.js", + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 2, + "column": 17 + } + }, + "decorators": [{ + "type": "Decorator", + "start": 0, + "end": 11, + "loc": { + "filename": "input.js", + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 11 + } + }, + "expression": { + "type": "Identifier", + "start": 1, + "end": 11, + "loc": { + "filename": "input.js", + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 11 + }, + "identifierName": "annotation" + }, + "name": "annotation" + } + }], + "id": { + "type": "Identifier", + "start": 18, + "end": 25, + "loc": { + "filename": "input.js", + "start": { + "line": 2, + "column": 6 + }, + "end": { + "line": 2, + "column": 13 + }, + "identifierName": "MyClass" + }, + "name": "MyClass" + }, + "superClass": null, + "body": { + "type": "ClassBody", + "start": 26, + "end": 29, + "loc": { + "filename": "input.js", + "start": { + "line": 2, + "column": 14 + }, + "end": { + "line": 2, + "column": 17 + } + }, + "body": [] + } + }, + { + "type": "FunctionDeclaration", + "start": 31, + "end": 90, + "loc": { + "filename": "input.js", + "start": { + "line": 4, + "column": 0 + }, + "end": { + "line": 6, + "column": 1 + } + }, + "id": { + "type": "Identifier", + "start": 40, + "end": 50, + "loc": { + "filename": "input.js", + "start": { + "line": 4, + "column": 9 + }, + "end": { + "line": 4, + "column": 19 + }, + "identifierName": "annotation" + }, + "name": "annotation" + }, + "generator": false, + "async": false, + "params": [{ + "type": "Identifier", + "start": 51, + "end": 57, + "loc": { + "filename": "input.js", + "start": { + "line": 4, + "column": 20 + }, + "end": { + "line": 4, + "column": 26 + }, + "identifierName": "target" + }, + "name": "target" + }], + "body": { + "type": "BlockStatement", + "start": 59, + "end": 90, + "loc": { + "filename": "input.js", + "start": { + "line": 4, + "column": 28 + }, + "end": { + "line": 6, + "column": 1 + } + }, + "body": [{ + "type": "ExpressionStatement", + "start": 64, + "end": 88, + "loc": { + "filename": "input.js", + "start": { + "line": 5, + "column": 3 + }, + "end": { + "line": 5, + "column": 27 + } + }, + "expression": { + "type": "AssignmentExpression", + "start": 64, + "end": 87, + "loc": { + "filename": "input.js", + "start": { + "line": 5, + "column": 3 + }, + "end": { + "line": 5, + "column": 26 + } + }, + "operator": "=", + "left": { + "type": "MemberExpression", + "start": 64, + "end": 80, + "loc": { + "filename": "input.js", + "start": { + "line": 5, + "column": 3 + }, + "end": { + "line": 5, + "column": 19 + } + }, + "object": { + "type": "Identifier", + "start": 64, + "end": 70, + "loc": { + "filename": "input.js", + "start": { + "line": 5, + "column": 3 + }, + "end": { + "line": 5, + "column": 9 + }, + "identifierName": "target" + }, + "name": "target" + }, + "property": { + "type": "Identifier", + "start": 71, + "end": 80, + "loc": { + "filename": "input.js", + "start": { + "line": 5, + "column": 10 + }, + "end": { + "line": 5, + "column": 19 + }, + "identifierName": "annotated" + }, + "name": "annotated" + }, + "computed": false + }, + "right": { + "type": "BooleanLiteral", + "start": 83, + "end": 87, + "loc": { + "filename": "input.js", + "start": { + "line": 5, + "column": 22 + }, + "end": { + "line": 5, + "column": 26 + } + }, + "value": true + } + } + }], + "directives": [] + } + } + ], + "directives": [] + }, + "comments": [] +} diff --git a/packages/babel-core/test/parse.js b/packages/babel-core/test/parse.js index b22ff86b4a90..6e41019b8fcc 100644 --- a/packages/babel-core/test/parse.js +++ b/packages/babel-core/test/parse.js @@ -17,19 +17,40 @@ function fixture(...args) { describe("parse", 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 input = fs.readFileSync( + fixture("filename-provided", "input.js"), + "utf8", + ); + const output = require(fixture("filename-provided", "output.json")); const result = parse(input, { - filename: fixture("input.js"), + filename: fixture("filename-provided", "input.js"), + cwd: fixture(), + }); + expect(JSON.parse(JSON.stringify(result))).toEqual(output); + }); + + it("should parse using the specified sourceFileName", function () { + const input = fs.readFileSync( + fixture("sourceFileName-provided", "input.js"), + "utf8", + ); + const output = require(fixture("sourceFileName-provided", "output.json")); + + const result = parse(input, { + filename: fixture("sourceFileName-provided", "input.js"), + sourceFileName: "input.js", cwd: fixture(), }); expect(JSON.parse(JSON.stringify(result))).toEqual(output); }); it("should parse using passed in configuration", function () { - const input = fs.readFileSync(fixture("input.js"), "utf8"); - const output = require(fixture("output.json")); + const input = fs.readFileSync( + fixture("passed-in-configuration", "input.js"), + "utf8", + ); + const output = require(fixture("passed-in-configuration", "output.json")); const result = parse(input, { parserOpts: {