From 518e1a6de4e05613ebf3aaf2c35e598e1dbcdb63 Mon Sep 17 00:00:00 2001 From: Justin Ridgewell Date: Fri, 29 Apr 2022 11:36:08 -0400 Subject: [PATCH] Do not add sourcemap markings for indentation (#14506) --- .../out-files/lib/bar/bar.js | 2 +- .../out-files/lib/foo.js | 2 +- .../out-files/lib/bar/bar.js.map | 2 +- .../out-files/lib/foo.js.map | 2 +- .../out-files/lib/index.js | 2 +- .../out-files/lib/main.js | 2 +- .../out-files/script2.js | 2 +- .../out-files/script2.js | 2 +- .../out-files/script3.js | 2 +- .../out-files/script3.js.map | 2 +- .../out-files/test.js.map | 2 +- .../stdin --source-maps inline/stdout.txt | 2 +- packages/babel-cli/test/index.js | 43 ++++--- .../source-maps/full/source-map.json | 12 +- .../source-maps/inline/output.js | 2 +- .../input-source-map-complex/source-map.json | 14 ++- .../source-mappings.json | 10 +- .../source-map.json | 8 +- packages/babel-generator/src/buffer.ts | 111 ++++++++++-------- packages/babel-generator/src/printer.ts | 2 +- packages/babel-generator/src/source-map.ts | 2 +- .../call-identifiers/source-map.json | 16 ++- packages/babel-generator/test/index.js | 28 ++--- packages/babel-helper-fixtures/src/index.ts | 7 ++ .../src/index.ts | 12 +- 25 files changed, 175 insertions(+), 116 deletions(-) diff --git a/packages/babel-cli/test/fixtures/babel/dir --out-dir --source-maps inline/out-files/lib/bar/bar.js b/packages/babel-cli/test/fixtures/babel/dir --out-dir --source-maps inline/out-files/lib/bar/bar.js index 6972e60eadda..475e871fca10 100644 --- a/packages/babel-cli/test/fixtures/babel/dir --out-dir --source-maps inline/out-files/lib/bar/bar.js +++ b/packages/babel-cli/test/fixtures/babel/dir --out-dir --source-maps inline/out-files/lib/bar/bar.js @@ -3,4 +3,4 @@ (function () { return 42; }); -//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJuYW1lcyI6W10sInNvdXJjZXMiOlsiLi4vLi4vc3JjL2Jhci9iYXIuanMiXSwic291cmNlc0NvbnRlbnQiOlsiKCgpID0+IDQyKSJdLCJtYXBwaW5ncyI6Ijs7QUFBQSxDQUFDO0FBQUEsU0FBTSxFQUFOO0FBQUEsQ0FBRCJ9 \ No newline at end of file +//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJuYW1lcyI6W10sInNvdXJjZXMiOlsiLi4vLi4vc3JjL2Jhci9iYXIuanMiXSwic291cmNlc0NvbnRlbnQiOlsiKCgpID0+IDQyKSJdLCJtYXBwaW5ncyI6Ijs7QUFBQSxDQUFDO0VBQUEsT0FBTSxFQUFOO0FBQUEsQ0FBRCJ9 \ No newline at end of file diff --git a/packages/babel-cli/test/fixtures/babel/dir --out-dir --source-maps inline/out-files/lib/foo.js b/packages/babel-cli/test/fixtures/babel/dir --out-dir --source-maps inline/out-files/lib/foo.js index fb877b64c666..6915f5c70848 100644 --- a/packages/babel-cli/test/fixtures/babel/dir --out-dir --source-maps inline/out-files/lib/foo.js +++ b/packages/babel-cli/test/fixtures/babel/dir --out-dir --source-maps inline/out-files/lib/foo.js @@ -3,4 +3,4 @@ arr.map(function (x) { return x * MULTIPLIER; }); -//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJuYW1lcyI6WyJhcnIiLCJtYXAiLCJ4IiwiTVVMVElQTElFUiJdLCJzb3VyY2VzIjpbIi4uL3NyYy9mb28uanMiXSwic291cmNlc0NvbnRlbnQiOlsiYXJyLm1hcCh4ID0+IHggKiBNVUxUSVBMSUVSKTsiXSwibWFwcGluZ3MiOiI7O0FBQUFBLEdBQUcsQ0FBQ0MsR0FBSixDQUFRLFVBQUFDLENBQUM7QUFBQSxTQUFJQSxDQUFDLEdBQUdDLFVBQVI7QUFBQSxDQUFUIn0= \ No newline at end of file +//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJuYW1lcyI6WyJhcnIiLCJtYXAiLCJ4IiwiTVVMVElQTElFUiJdLCJzb3VyY2VzIjpbIi4uL3NyYy9mb28uanMiXSwic291cmNlc0NvbnRlbnQiOlsiYXJyLm1hcCh4ID0+IHggKiBNVUxUSVBMSUVSKTsiXSwibWFwcGluZ3MiOiI7O0FBQUFBLEdBQUcsQ0FBQ0MsR0FBSixDQUFRLFVBQUFDLENBQUM7RUFBQSxPQUFJQSxDQUFDLEdBQUdDLFVBQVI7QUFBQSxDQUFUIn0= \ No newline at end of file diff --git a/packages/babel-cli/test/fixtures/babel/dir --out-dir --source-maps/out-files/lib/bar/bar.js.map b/packages/babel-cli/test/fixtures/babel/dir --out-dir --source-maps/out-files/lib/bar/bar.js.map index 0947e19fc08d..f7447a76f04e 100644 --- a/packages/babel-cli/test/fixtures/babel/dir --out-dir --source-maps/out-files/lib/bar/bar.js.map +++ b/packages/babel-cli/test/fixtures/babel/dir --out-dir --source-maps/out-files/lib/bar/bar.js.map @@ -1 +1 @@ -{"version":3,"file":"bar.js","names":[],"sources":["../../src/bar/bar.js"],"sourcesContent":["(() => 42)"],"mappings":";;AAAA,CAAC;AAAA,SAAM,EAAN;AAAA,CAAD"} \ No newline at end of file +{"version":3,"file":"bar.js","names":[],"sources":["../../src/bar/bar.js"],"sourcesContent":["(() => 42)"],"mappings":";;AAAA,CAAC;EAAA,OAAM,EAAN;AAAA,CAAD"} \ No newline at end of file diff --git a/packages/babel-cli/test/fixtures/babel/dir --out-dir --source-maps/out-files/lib/foo.js.map b/packages/babel-cli/test/fixtures/babel/dir --out-dir --source-maps/out-files/lib/foo.js.map index 60390d68c410..7e7ba2c03a92 100644 --- a/packages/babel-cli/test/fixtures/babel/dir --out-dir --source-maps/out-files/lib/foo.js.map +++ b/packages/babel-cli/test/fixtures/babel/dir --out-dir --source-maps/out-files/lib/foo.js.map @@ -1 +1 @@ -{"version":3,"file":"foo.js","names":["arr","map","x","MULTIPLIER"],"sources":["../src/foo.js"],"sourcesContent":["arr.map(x => x * MULTIPLIER);"],"mappings":";;AAAAA,GAAG,CAACC,GAAJ,CAAQ,UAAAC,CAAC;AAAA,SAAIA,CAAC,GAAGC,UAAR;AAAA,CAAT"} \ No newline at end of file +{"version":3,"file":"foo.js","names":["arr","map","x","MULTIPLIER"],"sources":["../src/foo.js"],"sourcesContent":["arr.map(x => x * MULTIPLIER);"],"mappings":";;AAAAA,GAAG,CAACC,GAAJ,CAAQ,UAAAC,CAAC;EAAA,OAAIA,CAAC,GAAGC,UAAR;AAAA,CAAT"} \ No newline at end of file diff --git a/packages/babel-cli/test/fixtures/babel/dir --out-dir --watch --verbose with external dependencies/out-files/lib/index.js b/packages/babel-cli/test/fixtures/babel/dir --out-dir --watch --verbose with external dependencies/out-files/lib/index.js index 1b260711254f..5e6c07e5fe54 100644 --- a/packages/babel-cli/test/fixtures/babel/dir --out-dir --watch --verbose with external dependencies/out-files/lib/index.js +++ b/packages/babel-cli/test/fixtures/babel/dir --out-dir --watch --verbose with external dependencies/out-files/lib/index.js @@ -1 +1 @@ -let str = "Updated!"; +let str = "Updated!"; \ No newline at end of file diff --git a/packages/babel-cli/test/fixtures/babel/dir --out-dir --watch --verbose with external dependencies/out-files/lib/main.js b/packages/babel-cli/test/fixtures/babel/dir --out-dir --watch --verbose with external dependencies/out-files/lib/main.js index 3ffbe342fad3..971ae69d37b9 100644 --- a/packages/babel-cli/test/fixtures/babel/dir --out-dir --watch --verbose with external dependencies/out-files/lib/main.js +++ b/packages/babel-cli/test/fixtures/babel/dir --out-dir --watch --verbose with external dependencies/out-files/lib/main.js @@ -1 +1 @@ -console.log("Updated!"); +console.log("Updated!"); \ No newline at end of file diff --git a/packages/babel-cli/test/fixtures/babel/filename --out-file --source-maps inline/out-files/script2.js b/packages/babel-cli/test/fixtures/babel/filename --out-file --source-maps inline/out-files/script2.js index 9e931cf42c64..fe9df475388f 100644 --- a/packages/babel-cli/test/fixtures/babel/filename --out-file --source-maps inline/out-files/script2.js +++ b/packages/babel-cli/test/fixtures/babel/filename --out-file --source-maps inline/out-files/script2.js @@ -4,4 +4,4 @@ arr.map(function (x) { return x * MULTIPLIER; }); -//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic2NyaXB0Mi5qcyIsIm5hbWVzIjpbImFyciIsIm1hcCIsIngiLCJNVUxUSVBMSUVSIl0sInNvdXJjZXMiOlsic2NyaXB0LmpzIl0sInNvdXJjZXNDb250ZW50IjpbImFyci5tYXAoeCA9PiB4ICogTVVMVElQTElFUik7Il0sIm1hcHBpbmdzIjoiOztBQUFBQSxHQUFHLENBQUNDLEdBQUosQ0FBUSxVQUFBQyxDQUFDO0FBQUEsU0FBSUEsQ0FBQyxHQUFHQyxVQUFSO0FBQUEsQ0FBVCJ9 +//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic2NyaXB0Mi5qcyIsIm5hbWVzIjpbImFyciIsIm1hcCIsIngiLCJNVUxUSVBMSUVSIl0sInNvdXJjZXMiOlsic2NyaXB0LmpzIl0sInNvdXJjZXNDb250ZW50IjpbImFyci5tYXAoeCA9PiB4ICogTVVMVElQTElFUik7Il0sIm1hcHBpbmdzIjoiOztBQUFBQSxHQUFHLENBQUNDLEdBQUosQ0FBUSxVQUFBQyxDQUFDO0VBQUEsT0FBSUEsQ0FBQyxHQUFHQyxVQUFSO0FBQUEsQ0FBVCJ9 \ No newline at end of file diff --git a/packages/babel-cli/test/fixtures/babel/filename-sourcemap --out-file --source-maps inline/out-files/script2.js b/packages/babel-cli/test/fixtures/babel/filename-sourcemap --out-file --source-maps inline/out-files/script2.js index 353e170820ea..6d70837d0ea2 100644 --- a/packages/babel-cli/test/fixtures/babel/filename-sourcemap --out-file --source-maps inline/out-files/script2.js +++ b/packages/babel-cli/test/fixtures/babel/filename-sourcemap --out-file --source-maps inline/out-files/script2.js @@ -4,4 +4,4 @@ arr.map(function (x) { return x * MULTIPLIER; }); -//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic2NyaXB0Mi5qcyIsIm5hbWVzIjpbImFyciIsIngiXSwic291cmNlcyI6WyJvcmlnaW5hbC5qcyJdLCJzb3VyY2VzQ29udGVudCI6WyJ2YXIgZm9vID0gKCkgPT4gNDsiXSwibWFwcGluZ3MiOiI7O0FBQUFBO0FBQUEsU0FBVUMsY0FBVjtBQUFBIn0= +//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic2NyaXB0Mi5qcyIsIm5hbWVzIjpbImFyciIsIngiXSwic291cmNlcyI6WyJvcmlnaW5hbC5qcyJdLCJzb3VyY2VzQ29udGVudCI6WyJ2YXIgZm9vID0gKCkgPT4gNDsiXSwibWFwcGluZ3MiOiI7O0FBQUFBO0VBQUEsT0FBVUMsY0FBVjtBQUFBIn0= \ No newline at end of file diff --git a/packages/babel-cli/test/fixtures/babel/filenames --out-file --source-maps inline/out-files/script3.js b/packages/babel-cli/test/fixtures/babel/filenames --out-file --source-maps inline/out-files/script3.js index f6ef082a2de3..6ec5a3e83f21 100644 --- a/packages/babel-cli/test/fixtures/babel/filenames --out-file --source-maps inline/out-files/script3.js +++ b/packages/babel-cli/test/fixtures/babel/filenames --out-file --source-maps inline/out-files/script3.js @@ -9,4 +9,4 @@ arr.map(function (x) { return x * MULTIPLIER; }); -//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic2NyaXB0My5qcyIsIm5hbWVzIjpbImFyciIsIm1hcCIsIngiLCJNVUxUSVBMSUVSIl0sInNvdXJjZXMiOlsic2NyaXB0LmpzIiwic2NyaXB0Mi5qcyJdLCJzb3VyY2VzQ29udGVudCI6WyIoKCkgPT4gNDIpIiwiYXJyLm1hcCh4ID0+IHggKiBNVUxUSVBMSUVSKTsiXSwibWFwcGluZ3MiOiI7O0FBQUEsQ0FBQztBQUFBLFNBQU0sRUFBTjtBQUFBLENBQUQ7OztBQ0FBQSxHQUFHLENBQUNDLEdBQUosQ0FBUSxVQUFBQyxDQUFDO0FBQUEsU0FBSUEsQ0FBQyxHQUFHQyxVQUFSO0FBQUEsQ0FBVCJ9 +//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic2NyaXB0My5qcyIsIm5hbWVzIjpbImFyciIsIm1hcCIsIngiLCJNVUxUSVBMSUVSIl0sInNvdXJjZXMiOlsic2NyaXB0LmpzIiwic2NyaXB0Mi5qcyJdLCJzb3VyY2VzQ29udGVudCI6WyIoKCkgPT4gNDIpIiwiYXJyLm1hcCh4ID0+IHggKiBNVUxUSVBMSUVSKTsiXSwibWFwcGluZ3MiOiI7O0FBQUEsQ0FBQztFQUFBLE9BQU0sRUFBTjtBQUFBLENBQUQ7OztBQ0FBQSxHQUFHLENBQUNDLEdBQUosQ0FBUSxVQUFBQyxDQUFDO0VBQUEsT0FBSUEsQ0FBQyxHQUFHQyxVQUFSO0FBQUEsQ0FBVCJ9 \ No newline at end of file diff --git a/packages/babel-cli/test/fixtures/babel/filenames --out-file --source-maps/out-files/script3.js.map b/packages/babel-cli/test/fixtures/babel/filenames --out-file --source-maps/out-files/script3.js.map index aef8c507ba3c..a17b280ac01a 100644 --- a/packages/babel-cli/test/fixtures/babel/filenames --out-file --source-maps/out-files/script3.js.map +++ b/packages/babel-cli/test/fixtures/babel/filenames --out-file --source-maps/out-files/script3.js.map @@ -1 +1 @@ -{"version":3,"file":"script3.js","names":["arr","map","x","MULTIPLIER"],"sources":["script.js","script2.js"],"sourcesContent":["(() => 42)\n\n// some comments","arr.map(x => x * MULTIPLIER);"],"mappings":";;AAAA,CAAC;AAAA,SAAM,EAAN;AAAA,CAAD,E,CAEA;;;ACFAA,GAAG,CAACC,GAAJ,CAAQ,UAAAC,CAAC;AAAA,SAAIA,CAAC,GAAGC,UAAR;AAAA,CAAT"} +{"version":3,"file":"script3.js","names":["arr","map","x","MULTIPLIER"],"sources":["script.js","script2.js"],"sourcesContent":["(() => 42)\n\n// some comments","arr.map(x => x * MULTIPLIER);"],"mappings":";;AAAA,CAAC;EAAA,OAAM,EAAN;AAAA,CAAD,E,CAEA;;;ACFAA,GAAG,CAACC,GAAJ,CAAQ,UAAAC,CAAC;EAAA,OAAIA,CAAC,GAAGC,UAAR;AAAA,CAAT"} \ No newline at end of file diff --git a/packages/babel-cli/test/fixtures/babel/stdin --out-file --source-maps/out-files/test.js.map b/packages/babel-cli/test/fixtures/babel/stdin --out-file --source-maps/out-files/test.js.map index 6900e9668855..e0a6d6c85f4a 100644 --- a/packages/babel-cli/test/fixtures/babel/stdin --out-file --source-maps/out-files/test.js.map +++ b/packages/babel-cli/test/fixtures/babel/stdin --out-file --source-maps/out-files/test.js.map @@ -1 +1 @@ -{"version":3,"file":"test.js","names":["arr","map","x"],"sources":["stdin"],"sourcesContent":["arr.map(x => x * x);"],"mappings":";;AAAAA,GAAG,CAACC,GAAJ,CAAQ,UAAAC,CAAC;AAAA,SAAIA,CAAC,GAAGA,CAAR;AAAA,CAAT"} +{"version":3,"file":"test.js","names":["arr","map","x"],"sources":["stdin"],"sourcesContent":["arr.map(x => x * x);"],"mappings":";;AAAAA,GAAG,CAACC,GAAJ,CAAQ,UAAAC,CAAC;EAAA,OAAIA,CAAC,GAAGA,CAAR;AAAA,CAAT"} \ No newline at end of file diff --git a/packages/babel-cli/test/fixtures/babel/stdin --source-maps inline/stdout.txt b/packages/babel-cli/test/fixtures/babel/stdin --source-maps inline/stdout.txt index d6a91c7ace1f..db57d03f3c0a 100644 --- a/packages/babel-cli/test/fixtures/babel/stdin --source-maps inline/stdout.txt +++ b/packages/babel-cli/test/fixtures/babel/stdin --source-maps inline/stdout.txt @@ -3,6 +3,6 @@ arr.map(function (x) { return x * x; }); -//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJuYW1lcyI6WyJhcnIiLCJtYXAiLCJ4Il0sInNvdXJjZXMiOlsic3RkaW4iXSwic291cmNlc0NvbnRlbnQiOlsiYXJyLm1hcCh4ID0+IHggKiB4KTsiXSwibWFwcGluZ3MiOiI7O0FBQUFBLEdBQUcsQ0FBQ0MsR0FBSixDQUFRLFVBQUFDLENBQUM7QUFBQSxTQUFJQSxDQUFDLEdBQUdBLENBQVI7QUFBQSxDQUFUIn0= +//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJuYW1lcyI6WyJhcnIiLCJtYXAiLCJ4Il0sInNvdXJjZXMiOlsic3RkaW4iXSwic291cmNlc0NvbnRlbnQiOlsiYXJyLm1hcCh4ID0+IHggKiB4KTsiXSwibWFwcGluZ3MiOiI7O0FBQUFBLEdBQUcsQ0FBQ0MsR0FBSixDQUFRLFVBQUFDLENBQUM7RUFBQSxPQUFJQSxDQUFDLEdBQUdBLENBQVI7QUFBQSxDQUFUIn0= //# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic3Rkb3V0IiwibmFtZXMiOltdLCJzb3VyY2VzIjpbXSwic291cmNlc0NvbnRlbnQiOltdLCJtYXBwaW5ncyI6IiJ9 diff --git a/packages/babel-cli/test/index.js b/packages/babel-cli/test/index.js index 43a1499fec1b..8ad01cdfa4c8 100644 --- a/packages/babel-cli/test/index.js +++ b/packages/babel-cli/test/index.js @@ -79,29 +79,40 @@ const assertTest = function (stdout, stderr, opts, cwd) { const expectStderr = opts.stderr.trim(); stderr = stderr.trim(); - if (opts.stderr) { - if (opts.stderrContains) { - expect(stderr).toContain(expectStderr); - } else { - expect(stderr).toBe(expectStderr); + try { + if (opts.stderr) { + if (opts.stderrContains) { + expect(stderr).toContain(expectStderr); + } else { + expect(stderr).toBe(expectStderr); + } + } else if (stderr) { + throw new Error("stderr:\n" + stderr); } - } else if (stderr) { - throw new Error("stderr:\n" + stderr); + } catch (e) { + if (!process.env.OVERWRITE) throw e; + console.log(`Updated test file: ${opts.stderrPath}`); + outputFileSync(opts.stderrPath, stderr + "\n"); } const expectStdout = opts.stdout.trim(); stdout = stdout.trim(); stdout = stdout.replace(/\\/g, "/"); - if (opts.stdout) { - if (opts.stdoutContains) { - expect(stdout).toContain(expectStdout); - } else { - fs.writeFileSync(opts.stdoutPath, stdout + "\n"); - expect(stdout).toBe(expectStdout); + try { + if (opts.stdout) { + if (opts.stdoutContains) { + expect(stdout).toContain(expectStdout); + } else { + expect(stdout).toBe(expectStdout); + } + } else if (stdout) { + throw new Error("stdout:\n" + stdout); } - } else if (stdout) { - throw new Error("stdout:\n" + stdout); + } catch (e) { + if (!process.env.OVERWRITE) throw e; + console.log(`Updated test file: ${opts.stdoutPath}`); + outputFileSync(opts.stdoutPath, stdout + "\n"); } if (opts.outFiles) { @@ -127,7 +138,7 @@ const assertTest = function (stdout, stderr, opts, cwd) { } const expectedLoc = path.join(opts.testLoc, "out-files", filename); console.log(`Updated test file: ${expectedLoc}`); - fs.writeFileSync(expectedLoc, actualFiles[filename]); + outputFileSync(expectedLoc, actualFiles[filename]); } }); diff --git a/packages/babel-core/test/fixtures/transformation/source-maps/full/source-map.json b/packages/babel-core/test/fixtures/transformation/source-maps/full/source-map.json index f0138605edc1..b5dfb95a4e3f 100644 --- a/packages/babel-core/test/fixtures/transformation/source-maps/full/source-map.json +++ b/packages/babel-core/test/fixtures/transformation/source-maps/full/source-map.json @@ -1,11 +1,15 @@ { "version": 3, - "sources": ["source-maps/full/input.js"], "names": [ "arr", "map", "x" ], - "mappings": "AAAAA,GAAG,CAACC,GAAJ,CAAQ,UAAAC,CAAC;AAAA,SAAIA,CAAC,GAAGA,CAAR;AAAA,CAAT", - "sourcesContent": ["arr.map(x => x * x);"] -} + "sources": [ + "source-maps/full/input.js" + ], + "sourcesContent": [ + "arr.map(x => x * x);" + ], + "mappings": "AAAAA,GAAG,CAACC,GAAJ,CAAQ,UAAAC,CAAC;EAAA,OAAIA,CAAC,GAAGA,CAAR;AAAA,CAAT" +} \ No newline at end of file diff --git a/packages/babel-core/test/fixtures/transformation/source-maps/inline/output.js b/packages/babel-core/test/fixtures/transformation/source-maps/inline/output.js index 3d471519da83..5b37b2b86a1d 100644 --- a/packages/babel-core/test/fixtures/transformation/source-maps/inline/output.js +++ b/packages/babel-core/test/fixtures/transformation/source-maps/inline/output.js @@ -1,4 +1,4 @@ arr.map(function (x) { return x * x; }); -//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJuYW1lcyI6WyJhcnIiLCJtYXAiLCJ4Il0sInNvdXJjZXMiOlsic291cmNlLW1hcHMvaW5saW5lL2lucHV0LmpzIl0sInNvdXJjZXNDb250ZW50IjpbImFyci5tYXAoeCA9PiB4ICogeCk7Il0sIm1hcHBpbmdzIjoiQUFBQUEsR0FBRyxDQUFDQyxHQUFKLENBQVEsVUFBQUMsQ0FBQztBQUFBLFNBQUlBLENBQUMsR0FBR0EsQ0FBUjtBQUFBLENBQVQifQ== +//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJuYW1lcyI6WyJhcnIiLCJtYXAiLCJ4Il0sInNvdXJjZXMiOlsic291cmNlLW1hcHMvaW5saW5lL2lucHV0LmpzIl0sInNvdXJjZXNDb250ZW50IjpbImFyci5tYXAoeCA9PiB4ICogeCk7Il0sIm1hcHBpbmdzIjoiQUFBQUEsR0FBRyxDQUFDQyxHQUFKLENBQVEsVUFBQUMsQ0FBQztFQUFBLE9BQUlBLENBQUMsR0FBR0EsQ0FBUjtBQUFBLENBQVQifQ== diff --git a/packages/babel-core/test/fixtures/transformation/source-maps/input-source-map-complex/source-map.json b/packages/babel-core/test/fixtures/transformation/source-maps/input-source-map-complex/source-map.json index 2f4cbac45ede..24e9b62b706d 100644 --- a/packages/babel-core/test/fixtures/transformation/source-maps/input-source-map-complex/source-map.json +++ b/packages/babel-core/test/fixtures/transformation/source-maps/input-source-map-complex/source-map.json @@ -1,10 +1,16 @@ { "version": 3, - "sources": ["HelloWorld.vue"], - "names": ["name", "data", "msg"], - "mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;eAsFA;AACAA,oBADA;;AAEAC;AACA;AACAC;AADA;AAGA;;AANA,C", + "mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;eAsFA;EACAA,kBADA;;EAEAC;IACA;MACAC;IADA;EAGA;;AANA,C", + "names": [ + "name", + "data", + "msg" + ], "sourceRoot": "src/components", + "sources": [ + "HelloWorld.vue" + ], "sourcesContent": [ "\n\n\n\n\n\n" ] -} +} \ No newline at end of file diff --git a/packages/babel-core/test/fixtures/transformation/source-maps/input-source-map-complex/source-mappings.json b/packages/babel-core/test/fixtures/transformation/source-maps/input-source-map-complex/source-mappings.json index de55dca585a9..fe0fa3321505 100644 --- a/packages/babel-core/test/fixtures/transformation/source-maps/input-source-map-complex/source-mappings.json +++ b/packages/babel-core/test/fixtures/transformation/source-maps/input-source-map-complex/source-mappings.json @@ -12,7 +12,7 @@ { "generated": { "line": 93, - "column": 0 + "column": 2 }, "original": { "line": 88, @@ -32,7 +32,7 @@ { "generated": { "line": 96, - "column": 0 + "column": 4 }, "original": { "line": 90, @@ -42,7 +42,7 @@ { "generated": { "line": 97, - "column": 0 + "column": 6 }, "original": { "line": 91, @@ -52,7 +52,7 @@ { "generated": { "line": 98, - "column": 0 + "column": 4 }, "original": { "line": 90, @@ -62,7 +62,7 @@ { "generated": { "line": 99, - "column": 0 + "column": 2 }, "original": { "line": 93, diff --git a/packages/babel-core/test/fixtures/transformation/source-maps/input-source-map-multiple-output-sources/source-map.json b/packages/babel-core/test/fixtures/transformation/source-maps/input-source-map-multiple-output-sources/source-map.json index c896cd9f05a6..bd5a5b693bd9 100644 --- a/packages/babel-core/test/fixtures/transformation/source-maps/input-source-map-multiple-output-sources/source-map.json +++ b/packages/babel-core/test/fixtures/transformation/source-maps/input-source-map-multiple-output-sources/source-map.json @@ -1,5 +1,6 @@ { - "mappings": "AAAC;;ACCD,SAASA,GAAT,CAAaC,GAAb,EAAwB;AACpB,QAAM,IAAIC,KAAJ,CAAU,cAAV,CAAN;AACH", + "version": 3, + "mappings": "AAAC;;ACCD,SAASA,GAAT,CAAaC,GAAb,EAAwB;EACpB,MAAM,IAAIC,KAAJ,CAAU,cAAV,CAAN;AACH", "names": [ "foo", "bar", @@ -12,6 +13,5 @@ "sourcesContent": [ "", "foo(1);\nfunction foo(bar: number): never {\n throw new Error('Intentional.');\n}" - ], - "version": 3 -} + ] +} \ No newline at end of file diff --git a/packages/babel-generator/src/buffer.ts b/packages/babel-generator/src/buffer.ts index a6f6132c2b3f..fff0a0760756 100644 --- a/packages/babel-generator/src/buffer.ts +++ b/packages/babel-generator/src/buffer.ts @@ -2,6 +2,34 @@ import type SourceMap from "./source-map"; import type * as t from "@babel/types"; import * as charcodes from "charcodes"; +type Pos = { + line: number; + column: number; +}; +type Loc = { + start?: Pos; + end?: Pos; + identifierName?: string; + filename?: string; +}; +type SourcePos = { + identifierName: string | undefined; + line: number | undefined; + column: number | undefined; + filename: string | undefined; + force: boolean; +}; + +function SourcePos(): SourcePos { + return { + identifierName: undefined, + line: undefined, + column: undefined, + filename: undefined, + force: false, + }; +} + const SPACES_RE = /^[ \t]+$/; export default class Buffer { constructor(map?: SourceMap | null) { @@ -9,30 +37,16 @@ export default class Buffer { } _map: SourceMap = null; - _buf: string = ""; - _last: number = 0; - _queue: Array< - [ - str: string, - line: number, - column: number, - identifierName: string | null, - filename: string | null | undefined, - force: boolean | undefined, - ] - > = []; - - _position: any = { + _buf = ""; + _last = 0; + _queue: Parameters[] = []; + + _position = { line: 1, column: 0, }; - _sourcePosition: any = { - identifierName: null, - line: null, - column: null, - filename: null, - }; - _disallowedPop: any | null = null; + _sourcePosition = SourcePos(); + _disallowedPop: SourcePos | null = null; /** * Get the final string output from the buffer, along with the sourcemap if one exists. @@ -82,7 +96,6 @@ export default class Buffer { /** * Add a string to the buffer than can be reverted. */ - queue(str: string): void { // Drop trailing spaces when a newline is inserted. if (str === "\n") { @@ -96,15 +109,22 @@ export default class Buffer { this._queue.unshift([str, line, column, identifierName, filename, force]); } + /** + * Same as queue, but this indentation will never have a sourcmap marker. + */ + queueIndentation(str: string): void { + this._queue.unshift([ + str, + undefined, + undefined, + undefined, + undefined, + false, + ]); + } + _flush(): void { - let item: [ - string, - number, - number, - string | null | undefined, - string | null | undefined, - boolean | undefined, - ]; + let item: Parameters; while ((item = this._queue.pop())) { this._append(...item); } @@ -114,9 +134,9 @@ export default class Buffer { str: string, line: number, column: number, - identifierName?: string | null, - filename?: string | null, - force?: boolean, + identifierName: string | undefined, + filename: string | undefined, + force: boolean, ): void { this._buf += str; this._last = str.charCodeAt(str.length - 1); @@ -323,23 +343,22 @@ export default class Buffer { _disallowPop(prop: string, loc: t.SourceLocation) { if (prop && !loc) return; - this._disallowedPop = this._normalizePosition(prop, loc); + this._disallowedPop = this._normalizePosition( + prop, + loc, + SourcePos(), + false, + ); } - _normalizePosition(prop: string, loc: any, targetObj?: any, force?: boolean) { + _normalizePosition( + prop: string, + loc: Loc | undefined | null, + targetObj: SourcePos, + force: boolean, + ) { const pos = loc ? loc[prop] : null; - if (targetObj === undefined) { - // Initialize with fields so that the object doesn't change shape. - targetObj = { - identifierName: null, - line: null, - column: null, - filename: null, - force: false, - }; - } - const origLine = targetObj.line; const origColumn = targetObj.column; const origFilename = targetObj.filename; diff --git a/packages/babel-generator/src/printer.ts b/packages/babel-generator/src/printer.ts index bbdcc5b1ba14..2ec2046f531b 100644 --- a/packages/babel-generator/src/printer.ts +++ b/packages/babel-generator/src/printer.ts @@ -273,7 +273,7 @@ class Printer { this.endsWith(charCodes.lineFeed) && str.charCodeAt(0) !== charCodes.lineFeed ) { - this._buf.queue(this._getIndent()); + this._buf.queueIndentation(this._getIndent()); } } diff --git a/packages/babel-generator/src/source-map.ts b/packages/babel-generator/src/source-map.ts index 4dcaed846f34..c3b0d549e9c1 100644 --- a/packages/babel-generator/src/source-map.ts +++ b/packages/babel-generator/src/source-map.ts @@ -82,7 +82,7 @@ export default class SourceMap { const generatedLine = generated.line; // Adding an empty mapping at the start of a generated line just clutters the map. - if (this._lastGenLine !== generatedLine && line === null) return; + if (this._lastGenLine !== generatedLine && line == null) return; // If this mapping points to the same source location as the last one, we can ignore it since // the previous one covers it. diff --git a/packages/babel-generator/test/fixtures/sourcemaps/call-identifiers/source-map.json b/packages/babel-generator/test/fixtures/sourcemaps/call-identifiers/source-map.json index 27cb26d0eec3..31627242a891 100644 --- a/packages/babel-generator/test/fixtures/sourcemaps/call-identifiers/source-map.json +++ b/packages/babel-generator/test/fixtures/sourcemaps/call-identifiers/source-map.json @@ -1,9 +1,15 @@ { - "mappings": "AAAAA,GAAG;AACHA,GAAG;AACHA,GAAG,GAAGC,GAAN;AACAC,GAAG,CAACF,GAAJ;AACAE,GAAG,CAACF,GAAJ;AACAE,GAAG,CAACF,GAAJ,CAAQC,GAAR;AACAC,GAAG,CAACF,GAAJ,GAAUC,GAAV;AACA;AACED,EAAAA,GAAG;AACHA,EAAAA,GAAG;AACHA,EAAAA,GAAG,GAAGC,GAAN;AACAC,EAAAA,GAAG,CAACF,GAAJ;AACAE,EAAAA,GAAG,CAACF,GAAJ;AACAE,EAAAA,GAAG,CAACF,GAAJ,CAAQC,GAAR;AACAC,EAAAA,GAAG,CAACF,GAAJ,GAAUC,GAAV;AACD", - "names": ["foo", "bar", "obj"], - "sources": ["fixtures/sourcemaps/call-identifiers/input.js"], + "version": 3, + "names": [ + "foo", + "bar", + "obj" + ], + "sources": [ + "fixtures/sourcemaps/call-identifiers/input.js" + ], "sourcesContent": [ "foo;\nfoo();\nfoo().bar;\nobj.foo;\nobj.foo();\nobj.foo.bar;\nobj.foo().bar;\n{\n foo;\n foo();\n foo().bar;\n obj.foo;\n obj.foo();\n obj.foo.bar;\n obj.foo().bar;\n}" ], - "version": 3 -} + "mappings": "AAAAA,GAAG;AACHA,GAAG;AACHA,GAAG,GAAGC,GAAN;AACAC,GAAG,CAACF,GAAJ;AACAE,GAAG,CAACF,GAAJ;AACAE,GAAG,CAACF,GAAJ,CAAQC,GAAR;AACAC,GAAG,CAACF,GAAJ,GAAUC,GAAV;AACA;EACED,GAAG;EACHA,GAAG;EACHA,GAAG,GAAGC,GAAN;EACAC,GAAG,CAACF,GAAJ;EACAE,GAAG,CAACF,GAAJ;EACAE,GAAG,CAACF,GAAJ,CAAQC,GAAR;EACAC,GAAG,CAACF,GAAJ,GAAUC,GAAV;AACD" +} \ No newline at end of file diff --git a/packages/babel-generator/test/index.js b/packages/babel-generator/test/index.js index c1215d73d63e..d8b07522264a 100644 --- a/packages/babel-generator/test/index.js +++ b/packages/babel-generator/test/index.js @@ -54,8 +54,7 @@ describe("generation", function () { version: 3, sources: ["a.js", "b.js"], mappings: - // eslint-disable-next-line max-len - "AAAA,SAASA,EAAT,CAAaC,GAAb,EAAkB;AAAEC,EAAAA,OAAO,CAACC,GAAR,CAAYF,GAAZ;AAAmB;;ACAvCD,EAAE,CAAC,OAAD,CAAF", + "AAAA,SAASA,EAAT,CAAaC,GAAb,EAAkB;EAAEC,OAAO,CAACC,GAAR,CAAYF,GAAZ;AAAmB;;ACAvCD,EAAE,CAAC,OAAD,CAAF", names: ["hi", "msg", "console", "log"], sourcesContent: [ "function hi (msg) { console.log(msg); }\n", @@ -103,12 +102,6 @@ describe("generation", function () { source: "a.js", original: { line: 1, column: 18 }, }, - { - name: "console", - generated: { line: 2, column: 0 }, - source: "a.js", - original: { line: 1, column: 20 }, - }, { name: "console", generated: { line: 2, column: 2 }, @@ -219,7 +212,7 @@ describe("generation", function () { version: 3, sources: ["inline"], names: ["foo", "bar"], - mappings: "AAAA,SAASA,IAAT,GAAe;AAAEC,EAAAA,IAAG;AAAG", + mappings: "AAAA,SAASA,IAAT,GAAe;EAAEC,IAAG;AAAG", sourcesContent: ["function foo() { bar; }\n"], }, "sourcemap was incorrectly generated", @@ -251,12 +244,6 @@ describe("generation", function () { source: "inline", original: { line: 1, column: 15 }, }, - { - name: "bar", - generated: { line: 2, column: 0 }, - source: "inline", - original: { line: 1, column: 17 }, - }, { name: "bar", generated: { line: 2, column: 2 }, @@ -867,7 +854,16 @@ suites.forEach(function (testSuite) { const result = run(); if (options.sourceMaps) { - expect(result.map).toEqual(task.sourceMap); + try { + expect(result.map).toEqual(task.sourceMap); + } catch (e) { + if (!process.env.OVERWRITE || !task.sourceMapFile) throw e; + console.log(`Updated test file: ${task.sourceMapFile.loc}`); + fs.writeFileSync( + task.sourceMapFile.loc, + JSON.stringify(result.map, null, 2), + ); + } } if ( diff --git a/packages/babel-helper-fixtures/src/index.ts b/packages/babel-helper-fixtures/src/index.ts index ade006ccb250..7a4482ad1c7b 100644 --- a/packages/babel-helper-fixtures/src/index.ts +++ b/packages/babel-helper-fixtures/src/index.ts @@ -29,6 +29,7 @@ type Test = { // todo(flow->ts): improve types here sourceMappings; sourceMap; + sourceMapFile: TestFile; }; type Suite = { @@ -165,6 +166,7 @@ function pushTask(taskName, taskDir, suite, suiteName) { }, sourceMappings: undefined, sourceMap: undefined, + sourceMapFile: undefined, inputSourceMap: undefined, }; @@ -225,6 +227,11 @@ function pushTask(taskName, taskDir, suite, suiteName) { const sourceMapLoc = taskDir + "/source-map.json"; if (fs.existsSync(sourceMapLoc)) { test.sourceMap = JSON.parse(readFile(sourceMapLoc)); + test.sourceMapFile = { + loc: sourceMapLoc, + code: test.sourceMap, + filename: "", + }; } const inputMapLoc = taskDir + "/input-source-map.json"; diff --git a/packages/babel-helper-transform-fixture-test-runner/src/index.ts b/packages/babel-helper-transform-fixture-test-runner/src/index.ts index 37b136ea8ace..4ab10710722a 100644 --- a/packages/babel-helper-transform-fixture-test-runner/src/index.ts +++ b/packages/babel-helper-transform-fixture-test-runner/src/index.ts @@ -341,7 +341,17 @@ function run(task) { } if (task.sourceMap) { - expect(result.map).toEqual(task.sourceMap); + try { + expect(result.map).toEqual(task.sourceMap); + } catch (e) { + if (!process.env.OVERWRITE || !task.sourceMapFile) throw e; + + console.log(`Updated test file: ${task.sourceMapFile.loc}`); + fs.writeFileSync( + task.sourceMapFile.loc, + JSON.stringify(result.map, null, 2), + ); + } } if (task.sourceMappings) {