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 7c44c97342..2101b6c9d7 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 @@ -5,4 +5,4 @@ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Cons var Test = function Test() { _classCallCheck(this, Test); }; -//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIi4uLy4uL3NyYy9iYXIvYmFyLmpzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiI7Ozs7SUFBTSxJQUFJLFlBQUosSUFBSTt3QkFBSixJQUFJIiwiZmlsZSI6ImJhci5qcyIsInNvdXJjZXNDb250ZW50IjpbImNsYXNzIFRlc3Qge1xuXG59Il19 +//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIi4uLy4uL3NyYy9iYXIvYmFyLmpzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiI7Ozs7SUFBTSIsImZpbGUiOiJiYXIuanMiLCJzb3VyY2VzQ29udGVudCI6WyJjbGFzcyBUZXN0IHtcblxufSJdfQ== 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 3537587fad..d9c86c7221 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;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIi4uL3NyYy9mb28uanMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6Ijs7QUFBQSxHQUFHLENBQUMsR0FBRyxDQUFDLFVBQUEsQ0FBQztTQUFJLENBQUMsR0FBRyxVQUFVO0NBQUEsQ0FBQyxDQUFDIiwiZmlsZSI6ImZvby5qcyIsInNvdXJjZXNDb250ZW50IjpbImFyci5tYXAoeCA9PiB4ICogTVVMVElQTElFUik7Il19 +//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIi4uL3NyYy9mb28uanMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6Ijs7QUFBQSxJQUFJLEdBQUosQ0FBUTtTQUFLLElBQUksVUFBSjtDQUFMLENBQVIiLCJmaWxlIjoiZm9vLmpzIiwic291cmNlc0NvbnRlbnQiOlsiYXJyLm1hcCh4ID0+IHggKiBNVUxUSVBMSUVSKTsiXX0= 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 d2f66bb31f..e44ef77215 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,"sources":["../../src/bar/bar.js"],"names":[],"mappings":";;;;IAAM,IAAI,YAAJ,IAAI;wBAAJ,IAAI","file":"bar.js","sourcesContent":["class Test {\n\n}"]} +{"version":3,"sources":["../../src/bar/bar.js"],"names":[],"mappings":";;;;IAAM","file":"bar.js","sourcesContent":["class Test {\n\n}"]} \ 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 77300ca29a..96682885b2 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,"sources":["../src/foo.js"],"names":[],"mappings":";;AAAA,GAAG,CAAC,GAAG,CAAC,UAAA,CAAC;SAAI,CAAC,GAAG,UAAU;CAAA,CAAC,CAAC","file":"foo.js","sourcesContent":["arr.map(x => x * MULTIPLIER);"]} +{"version":3,"sources":["../src/foo.js"],"names":[],"mappings":";;AAAA,IAAI,GAAJ,CAAQ;SAAK,IAAI,UAAJ;CAAL,CAAR","file":"foo.js","sourcesContent":["arr.map(x => x * MULTIPLIER);"]} \ 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 31da0b4f1a..7e591ab7bb 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;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbInNjcmlwdC5qcyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiOztBQUFBLEdBQUcsQ0FBQyxHQUFHLENBQUMsVUFBQSxDQUFDO1NBQUksQ0FBQyxHQUFHLFVBQVU7Q0FBQSxDQUFDLENBQUMiLCJmaWxlIjoic2NyaXB0Mi5qcyIsInNvdXJjZXNDb250ZW50IjpbImFyci5tYXAoeCA9PiB4ICogTVVMVElQTElFUik7Il19 +//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbInNjcmlwdC5qcyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiOztBQUFBLElBQUksR0FBSixDQUFRO1NBQUssSUFBSSxVQUFKO0NBQUwsQ0FBUiIsImZpbGUiOiJzY3JpcHQyLmpzIiwic291cmNlc0NvbnRlbnQiOlsiYXJyLm1hcCh4ID0+IHggKiBNVUxUSVBMSUVSKTsiXX0= 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 a32030e61a..c1fc202b90 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 @@ -11,4 +11,4 @@ arr.map(function (x) { return x * MULTIPLIER; }); -//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbInNjcmlwdC5qcyIsInNjcmlwdDIuanMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6Ijs7OztJQUFNLElBQUksWUFBSixJQUFJO3dCQUFKLElBQUk7Ozs7O0FDQVYsR0FBRyxDQUFDLEdBQUcsQ0FBQyxVQUFBLENBQUM7U0FBSSxDQUFDLEdBQUcsVUFBVTtDQUFBLENBQUMsQ0FBQyIsImZpbGUiOiJzY3JpcHQzLmpzIiwic291cmNlc0NvbnRlbnQiOlsiY2xhc3MgVGVzdCB7XG5cbn0iLCJhcnIubWFwKHggPT4geCAqIE1VTFRJUExJRVIpOyJdfQ== +//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbInNjcmlwdC5qcyIsInNjcmlwdDIuanMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6Ijs7OztJQUFNOzs7Ozs7QUNBTixJQUFJLEdBQUosQ0FBUTtTQUFLLElBQUksVUFBSjtDQUFMLENBQVIiLCJmaWxlIjoic2NyaXB0My5qcyIsInNvdXJjZXNDb250ZW50IjpbImNsYXNzIFRlc3Qge1xuXG59IiwiYXJyLm1hcCh4ID0+IHggKiBNVUxUSVBMSUVSKTsiXX0= 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 941e54cb54..4443440920 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,"sources":["script.js","script2.js"],"names":[],"mappings":";;;;IAAM,IAAI,YAAJ,IAAI;wBAAJ,IAAI;;;;;ACAV,GAAG,CAAC,GAAG,CAAC,UAAA,CAAC;SAAI,CAAC,GAAG,UAAU;CAAA,CAAC,CAAC","file":"script3.js","sourcesContent":["class Test {\n\n}","arr.map(x => x * MULTIPLIER);"]} +{"version":3,"sources":["script.js","script2.js"],"names":[],"mappings":";;;;IAAM;;;;;;ACAN,IAAI,GAAJ,CAAQ;SAAK,IAAI,UAAJ;CAAL,CAAR","file":"script3.js","sourcesContent":["class Test {\n\n}","arr.map(x => x * MULTIPLIER);"]} \ No newline at end of file diff --git a/packages/babel-core/test/fixtures/transformation/source-maps/arrow-function/source-mappings.json b/packages/babel-core/test/fixtures/transformation/source-maps/arrow-function/source-mappings.json index a1d1d31e4a..47d5aab7a2 100644 --- a/packages/babel-core/test/fixtures/transformation/source-maps/arrow-function/source-mappings.json +++ b/packages/babel-core/test/fixtures/transformation/source-maps/arrow-function/source-mappings.json @@ -1,7 +1,7 @@ [{ "original": { "line": 1, - "column": 14 + "column": 13 }, "generated": { "line": 2, 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 b2b351d8bb..35d93706a8 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 @@ -3,6 +3,6 @@ "file": "source-maps/full/expected.js", "sources": ["source-maps/full/actual.js"], "names": [], - "mappings": "AAAA,GAAG,CAAC,GAAG,CAAC,UAAA,CAAC;SAAI,CAAC,GAAG,CAAC;CAAA,CAAC,CAAC", + "mappings": "AAAA,IAAI,GAAJ,CAAQ;SAAK,IAAI,CAAJ;CAAL,CAAR", "sourcesContent": ["arr.map(x => x * x);"] } diff --git a/packages/babel-core/test/fixtures/transformation/source-maps/inline/expected.js b/packages/babel-core/test/fixtures/transformation/source-maps/inline/expected.js index f93d2fbb34..6e490e6707 100644 --- a/packages/babel-core/test/fixtures/transformation/source-maps/inline/expected.js +++ b/packages/babel-core/test/fixtures/transformation/source-maps/inline/expected.js @@ -1,4 +1,4 @@ arr.map(function (x) { return x * x; }); -//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbInNvdXJjZS1tYXBzL2lubGluZS9hY3R1YWwuanMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IkFBQUEsR0FBRyxDQUFDLEdBQUcsQ0FBQyxVQUFBLENBQUM7U0FBSSxDQUFDLEdBQUcsQ0FBQztDQUFBLENBQUMsQ0FBQyIsImZpbGUiOiJzb3VyY2UtbWFwcy9pbmxpbmUvZXhwZWN0ZWQuanMiLCJzb3VyY2VzQ29udGVudCI6WyJhcnIubWFwKHggPT4geCAqIHgpOyJdfQ== +//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbInNvdXJjZS1tYXBzL2lubGluZS9hY3R1YWwuanMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IkFBQUEsSUFBSSxHQUFKLENBQVE7U0FBSyxJQUFJLENBQUo7Q0FBTCxDQUFSIiwiZmlsZSI6InNvdXJjZS1tYXBzL2lubGluZS9leHBlY3RlZC5qcyIsInNvdXJjZXNDb250ZW50IjpbImFyci5tYXAoeCA9PiB4ICogeCk7Il19 diff --git a/packages/babel-generator/src/generators/expressions.js b/packages/babel-generator/src/generators/expressions.js index b5d9ecb860..ce96213e38 100644 --- a/packages/babel-generator/src/generators/expressions.js +++ b/packages/babel-generator/src/generators/expressions.js @@ -92,6 +92,7 @@ export function Decorator(node: Object) { export function CallExpression(node: Object) { this.print(node.callee, node); + if (node.loc) this.printAuxAfterComment(); this.push("("); diff --git a/packages/babel-generator/src/printer.js b/packages/babel-generator/src/printer.js index 7e0749d46b..7c8315372a 100644 --- a/packages/babel-generator/src/printer.js +++ b/packages/babel-generator/src/printer.js @@ -49,7 +49,7 @@ export default class Printer extends Buffer { if (opts.before) opts.before(); - this.map.mark(node, "start"); + this.map.mark(node); this._print(node, parent); @@ -62,7 +62,7 @@ export default class Printer extends Buffer { // end this._printStack.pop(); - this.map.mark(node, "end"); + if (parent) this.map.mark(parent); if (opts.after) opts.after(); this.format.concise = oldConcise; @@ -134,6 +134,10 @@ export default class Printer extends Buffer { opts.iterator(node, i); } + if (opts.separator && parent.loc) { + this.printAuxAfterComment(); + } + if (opts.separator && i < len - 1) { this.push(opts.separator); } diff --git a/packages/babel-generator/src/source-map.js b/packages/babel-generator/src/source-map.js index f1b3ee3727..942f931b9f 100644 --- a/packages/babel-generator/src/source-map.js +++ b/packages/babel-generator/src/source-map.js @@ -9,6 +9,7 @@ export default class SourceMap { constructor(position, opts, code) { this.position = position; this.opts = opts; + this.last = {generated: {}, original: {}}; if (opts.sourceMaps) { this.map = new sourceMap.SourceMapGenerator({ @@ -39,7 +40,7 @@ export default class SourceMap { * Mark a node's generated position, and add it to the sourcemap. */ - mark(node, type) { + mark(node) { let loc = node.loc; if (!loc) return; // no location info @@ -55,12 +56,27 @@ export default class SourceMap { column: position.column }; - let original = loc[type]; + let original = loc.start; - map.addMapping({ + // Avoid emitting duplicates on either side. Duplicated + // original values creates unnecesssarily large source maps + // and increases compile time. Duplicates on the generated + // side can lead to incorrect mappings. + if (comparePosition(original, this.last.original) + || comparePosition(generated, this.last.generated)) { + return; + } + + this.last = { source: this.opts.sourceFileName, generated: generated, original: original - }); + }; + + map.addMapping(this.last); } } + +function comparePosition(a, b) { + return a.line === b.line && a.column === b.column; +} diff --git a/packages/babel-plugin-transform-es2015-modules-commonjs/src/index.js b/packages/babel-plugin-transform-es2015-modules-commonjs/src/index.js index ed744a5815..73e165e732 100644 --- a/packages/babel-plugin-transform-es2015-modules-commonjs/src/index.js +++ b/packages/babel-plugin-transform-es2015-modules-commonjs/src/index.js @@ -330,7 +330,7 @@ export default function () { ])); } } - remaps[specifier.local.name] = t.memberExpression(target, specifier.imported); + remaps[specifier.local.name] = t.memberExpression(target, t.cloneWithoutLoc(specifier.imported)); } } } else { diff --git a/packages/babel-plugin-transform-es2015-modules-commonjs/test/fixtures/auxiliary-comment/overview/actual.js b/packages/babel-plugin-transform-es2015-modules-commonjs/test/fixtures/auxiliary-comment/overview/actual.js index df37d9e5a5..cdcfa374df 100644 --- a/packages/babel-plugin-transform-es2015-modules-commonjs/test/fixtures/auxiliary-comment/overview/actual.js +++ b/packages/babel-plugin-transform-es2015-modules-commonjs/test/fixtures/auxiliary-comment/overview/actual.js @@ -9,7 +9,7 @@ import {foo as bar2} from "foo5"; export {test}; export var test = 5; -bar(foo); +bar(foo, bar2); /* my comment */ bar2; diff --git a/packages/babel-plugin-transform-es2015-modules-commonjs/test/fixtures/auxiliary-comment/overview/expected.js b/packages/babel-plugin-transform-es2015-modules-commonjs/test/fixtures/auxiliary-comment/overview/expected.js index c7b4852b07..f821389ada 100644 --- a/packages/babel-plugin-transform-es2015-modules-commonjs/test/fixtures/auxiliary-comment/overview/expected.js +++ b/packages/babel-plugin-transform-es2015-modules-commonjs/test/fixtures/auxiliary-comment/overview/expected.js @@ -26,8 +26,8 @@ var _foo5 = require("foo5"); exports. /*after*/test = test; var test = /*before*/exports. /*after*/test = 5; -/*before*/(0, _foo4. /*after*/bar)( /*before*/_foo2.default) /*after*/; +/*before*/(0, _foo4.bar) /*after*/( /*before*/_foo2.default /*after*/, /*before*/_foo5.foo /*after*/); /* my comment */ -/*before*/_foo5. /*after*/foo; +/*before*/_foo5.foo; /*after*/ /*before*/_foo2.default; /*after*/ diff --git a/packages/babel-plugin-transform-es2015-modules-commonjs/test/fixtures/strict/import/actual.js b/packages/babel-plugin-transform-es2015-modules-commonjs/test/fixtures/strict/import/actual.js index ffb7f73072..5b84f44e66 100644 --- a/packages/babel-plugin-transform-es2015-modules-commonjs/test/fixtures/strict/import/actual.js +++ b/packages/babel-plugin-transform-es2015-modules-commonjs/test/fixtures/strict/import/actual.js @@ -6,3 +6,4 @@ import * as foo4 from "foo"; foo; foo2; foo3; +foo3(); diff --git a/packages/babel-plugin-transform-es2015-modules-commonjs/test/fixtures/strict/import/expected.js b/packages/babel-plugin-transform-es2015-modules-commonjs/test/fixtures/strict/import/expected.js index e042c60eb4..260f28745b 100644 --- a/packages/babel-plugin-transform-es2015-modules-commonjs/test/fixtures/strict/import/expected.js +++ b/packages/babel-plugin-transform-es2015-modules-commonjs/test/fixtures/strict/import/expected.js @@ -4,4 +4,5 @@ var _foo = require("foo"); _foo.default; _foo.default; -_foo.foo3; \ No newline at end of file +_foo.foo3; +(0, _foo.foo3)(); diff --git a/packages/babel-plugin-transform-es2015-modules-commonjs/test/fixtures/strict/import/source-mappings.json b/packages/babel-plugin-transform-es2015-modules-commonjs/test/fixtures/strict/import/source-mappings.json new file mode 100644 index 0000000000..0db931042d --- /dev/null +++ b/packages/babel-plugin-transform-es2015-modules-commonjs/test/fixtures/strict/import/source-mappings.json @@ -0,0 +1,43 @@ +[{ + "original": { + "line": 6, "column": 0 + }, + "generated": { + "line": 5, "column": 0 + } +},{ + "original": { + "line": 6, "column": 0 + }, + "generated": { + "line": 5, "column": 12 + } +},{ + "original": { + "line": 8, "column": 0 + }, + "generated": { + "line": 7, "column": 0 + } +},{ + "original": { + "line": 8, "column": 0 + }, + "generated": { + "line": 7, "column": 9 + } +},{ + "original": { + "line": 9, "column": 0 + }, + "generated": { + "line": 8, "column": 0 + } +},{ + "original": { + "line": 9, "column": 0 + }, + "generated": { + "line": 8, "column": 9 + } +}] diff --git a/packages/babel-types/src/index.js b/packages/babel-types/src/index.js index 50abbb4ecf..3046db78db 100644 --- a/packages/babel-types/src/index.js +++ b/packages/babel-types/src/index.js @@ -239,6 +239,16 @@ export function clone(node: Object): Object { return newNode; } +/** + * Create a shallow clone of a `node` excluding `_private` and location properties. + */ + +export function cloneWithoutLoc(node: Object): Object { + let newNode = clone(node); + delete newNode.loc; + return newNode; +} + /** * Create a deep clone of a `node` and all of it's child nodes * exluding `_private` properties.