From ee83dc1f23ac3b23c846bd64adc0f11bf5762863 Mon Sep 17 00:00:00 2001 From: Sebastian McKenzie Date: Tue, 16 Aug 2016 15:33:19 +0100 Subject: [PATCH] Generate names field for identifiers to get correct names mappings --- .../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 +- packages/babel-core/package.json | 2 +- .../source-maps/full/source-map.json | 8 +++- .../source-maps/inline/expected.js | 2 +- packages/babel-generator/package.json | 2 +- packages/babel-generator/src/buffer.js | 16 ++++--- packages/babel-generator/src/source-map.js | 10 ++++- packages/babel-generator/test/index.js | 42 ++++++++++++++++++- .../package.json | 2 +- packages/babel-template/package.json | 2 +- packages/babel-traverse/package.json | 2 +- 14 files changed, 76 insertions(+), 20 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 a2ea94ca9e0e..0a88d0eed3c2 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,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIi4uLy4uL3NyYy9iYXIvYmFyLmpzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiI7Ozs7SUFBTSxJIiwiZmlsZSI6ImJhci5qcyIsInNvdXJjZXNDb250ZW50IjpbImNsYXNzIFRlc3Qge1xuXG59Il19 +//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIi4uLy4uL3NyYy9iYXIvYmFyLmpzIl0sIm5hbWVzIjpbIlRlc3QiXSwibWFwcGluZ3MiOiI7Ozs7SUFBTUEsSSIsImZpbGUiOiJiYXIuanMiLCJzb3VyY2VzQ29udGVudCI6WyJjbGFzcyBUZXN0IHtcblxufSJdfQ== 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 0a833dbc8bcc..b6d274498daa 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,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIi4uL3NyYy9mb28uanMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6Ijs7QUFBQSxJQUFJLEdBQUosQ0FBUTtBQUFBLFNBQUssSUFBSSxVQUFUO0FBQUEsQ0FBUiIsImZpbGUiOiJmb28uanMiLCJzb3VyY2VzQ29udGVudCI6WyJhcnIubWFwKHggPT4geCAqIE1VTFRJUExJRVIpOyJdfQ== +//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIi4uL3NyYy9mb28uanMiXSwibmFtZXMiOlsiYXJyIiwibWFwIiwieCIsIk1VTFRJUExJRVIiXSwibWFwcGluZ3MiOiI7O0FBQUFBLElBQUlDLEdBQUosQ0FBUTtBQUFBLFNBQUtDLElBQUlDLFVBQVQ7QUFBQSxDQUFSIiwiZmlsZSI6ImZvby5qcyIsInNvdXJjZXNDb250ZW50IjpbImFyci5tYXAoeCA9PiB4ICogTVVMVElQTElFUik7Il19 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 19eab2f949cf..19ecc2250e97 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,I","file":"bar.js","sourcesContent":["class Test {\n\n}"]} +{"version":3,"sources":["../../src/bar/bar.js"],"names":["Test"],"mappings":";;;;IAAMA,I","file":"bar.js","sourcesContent":["class Test {\n\n}"]} 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 b8d69ed4f87b..a97ad9e88f10 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,IAAI,GAAJ,CAAQ;AAAA,SAAK,IAAI,UAAT;AAAA,CAAR","file":"foo.js","sourcesContent":["arr.map(x => x * MULTIPLIER);"]} +{"version":3,"sources":["../src/foo.js"],"names":["arr","map","x","MULTIPLIER"],"mappings":";;AAAAA,IAAIC,GAAJ,CAAQ;AAAA,SAAKC,IAAIC,UAAT;AAAA,CAAR","file":"foo.js","sourcesContent":["arr.map(x => x * MULTIPLIER);"]} diff --git a/packages/babel-core/package.json b/packages/babel-core/package.json index 242e752f69e3..3acdba316070 100644 --- a/packages/babel-core/package.json +++ b/packages/babel-core/package.json @@ -33,7 +33,7 @@ "babel-register": "^6.9.0", "babel-traverse": "^6.13.0", "babel-types": "^6.13.0", - "babylon": "^6.7.0", + "babylon": "^6.9.0", "convert-source-map": "^1.1.0", "debug": "^2.1.1", "json5": "^0.4.0", 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 cf4f823e4ac0..115087ad2c3a 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 @@ -2,7 +2,11 @@ "version": 3, "file": "source-maps/full/expected.js", "sources": ["source-maps/full/actual.js"], - "names": [], - "mappings": "AAAA,IAAI,GAAJ,CAAQ;AAAA,SAAK,IAAI,CAAT;AAAA,CAAR", + "names": [ + "arr", + "map", + "x" + ], + "mappings": "AAAAA,IAAIC,GAAJ,CAAQ;AAAA,SAAKC,IAAIA,CAAT;AAAA,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 5e0d972ab00e..00ae47b8755d 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,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbInNvdXJjZS1tYXBzL2lubGluZS9hY3R1YWwuanMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IkFBQUEsSUFBSSxHQUFKLENBQVE7QUFBQSxTQUFLLElBQUksQ0FBVDtBQUFBLENBQVIiLCJmaWxlIjoic291cmNlLW1hcHMvaW5saW5lL2V4cGVjdGVkLmpzIiwic291cmNlc0NvbnRlbnQiOlsiYXJyLm1hcCh4ID0+IHggKiB4KTsiXX0= +//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbInNvdXJjZS1tYXBzL2lubGluZS9hY3R1YWwuanMiXSwibmFtZXMiOlsiYXJyIiwibWFwIiwieCJdLCJtYXBwaW5ncyI6IkFBQUFBLElBQUlDLEdBQUosQ0FBUTtBQUFBLFNBQUtDLElBQUlBLENBQVQ7QUFBQSxDQUFSIiwiZmlsZSI6InNvdXJjZS1tYXBzL2lubGluZS9leHBlY3RlZC5qcyIsInNvdXJjZXNDb250ZW50IjpbImFyci5tYXAoeCA9PiB4ICogeCk7Il19 diff --git a/packages/babel-generator/package.json b/packages/babel-generator/package.json index 5e873281b1db..297e2c36a9a3 100644 --- a/packages/babel-generator/package.json +++ b/packages/babel-generator/package.json @@ -20,6 +20,6 @@ }, "devDependencies": { "babel-helper-fixtures": "^6.9.0", - "babylon": "^6.7.0" + "babylon": "^6.9.0" } } diff --git a/packages/babel-generator/src/buffer.js b/packages/babel-generator/src/buffer.js index a1390c176214..51ddd6a95398 100644 --- a/packages/babel-generator/src/buffer.js +++ b/packages/babel-generator/src/buffer.js @@ -25,6 +25,7 @@ export default class Buffer { column: 0, }; _sourcePosition: Object = { + identifierName: null, line: null, column: null, filename: null, @@ -49,8 +50,8 @@ export default class Buffer { append(str: string): void { this._flush(); - const { line, column, filename } = this._sourcePosition; - this._append(str, line, column, filename); + const { line, column, filename, identifierName } = this._sourcePosition; + this._append(str, line, column, identifierName, filename); } /** @@ -61,8 +62,8 @@ export default class Buffer { // Drop trailing spaces when a newline is inserted. if (str === "\n") while (this._queue.length > 0 && SPACES_RE.test(this._queue[0][0])) this._queue.shift(); - const { line, column, filename } = this._sourcePosition; - this._queue.unshift([str, line, column, filename]); + const { line, column, filename, identifierName } = this._sourcePosition; + this._queue.unshift([str, line, column, identifierName, filename]); } _flush(): void { @@ -70,10 +71,10 @@ export default class Buffer { while (item = this._queue.pop()) this._append(...item); } - _append(str: string, line: number, column: number, filename: ?string): void { + _append(str: string, line: number, column: number, identifierName: ?string, filename: ?string): void { // If there the line is ending, adding a new mapping marker is redundant if (this._map && str[0] !== "\n") { - this._map.mark(this._position.line, this._position.column, line, column, filename); + this._map.mark(this._position.line, this._position.column, line, column, identifierName, filename); } this._buf.push(str); @@ -135,6 +136,7 @@ export default class Buffer { let pos = loc ? loc[prop] : null; + this._sourcePosition.identifierName = loc && loc.identifierName || null; this._sourcePosition.line = pos ? pos.line : null; this._sourcePosition.column = pos ? pos.column : null; this._sourcePosition.filename = loc && loc.filename || null; @@ -151,6 +153,7 @@ export default class Buffer { let originalLine = this._sourcePosition.line; let originalColumn = this._sourcePosition.column; let originalFilename = this._sourcePosition.filename; + let originalIdentifierName = this._sourcePosition.identifierName; this.source(prop, loc); @@ -159,6 +162,7 @@ export default class Buffer { this._sourcePosition.line = originalLine; this._sourcePosition.column = originalColumn; this._sourcePosition.filename = originalFilename; + this._sourcePosition.identifierName = originalIdentifierName; } getCurrentColumn(): number { diff --git a/packages/babel-generator/src/source-map.js b/packages/babel-generator/src/source-map.js index 4486bbf737ec..dc839c654ed7 100644 --- a/packages/babel-generator/src/source-map.js +++ b/packages/babel-generator/src/source-map.js @@ -34,7 +34,14 @@ export default class SourceMap { * values to insert a mapping to nothing. */ - mark(generatedLine: number, generatedColumn: number, line: number, column: number, filename: ?string) { + mark( + generatedLine: number, + generatedColumn: number, + line: number, + column: number, + identifierName: ?string, + filename: ?string, + ) { // Adding an empty mapping at the start of a generated line just clutters the map. if (this._lastGenLine !== generatedLine && line === null) return; @@ -50,6 +57,7 @@ export default class SourceMap { this._lastSourceColumn = column; this._map.addMapping({ + name: identifierName, generated: { line: generatedLine, column: generatedColumn, diff --git a/packages/babel-generator/test/index.js b/packages/babel-generator/test/index.js index b6c6f4469c36..827b52b7d190 100644 --- a/packages/babel-generator/test/index.js +++ b/packages/babel-generator/test/index.js @@ -44,7 +44,13 @@ suite("generation", function () { version: 3, sources: [ 'a.js', 'b.js' ], names: [], - mappings: 'AAAA,SAAS,EAAT,CAAa,GAAb,EAAkB;AAAE,UAAQ,GAAR,CAAY,GAAZ;AAAmB;;ACAvC,GAAG,OAAH', + mappings: 'AAAA,SAASA,EAAT,CAAaC,GAAb,EAAkB;AAAEC,UAAQC,GAAR,CAAYF,GAAZ;AAAmB;;ACAvCD,GAAG,OAAH', + names: [ + 'hi', + 'msg', + 'console', + 'log', + ], sourcesContent: [ 'function hi (msg) { console.log(msg); }\n', 'hi(\'hello\');\n' @@ -56,6 +62,40 @@ suite("generation", function () { "code was incorrectly generated" ); }); + + test("identifierName", function () { + var code = "function foo() { bar; }\n"; + + var ast = parse(code, { filename: "inline" }).program; + var fn = ast.body[0]; + + var id = fn.id; + id.name += "2"; + id.loc.identifierName = "foo"; + + var id2 = fn.body.body[0].expression; + id2.name += "2"; + id2.loc.identiferName = "bar"; + + var generated = generate.default(ast, { + filename: "inline", + sourceFileName: "inline", + sourceMaps: true + }, code); + + chai.expect(generated.map).to.deep.equal({ + version: 3, + sources: ["inline"], + names: ["foo", "bar" ], + mappings: "AAAA,SAASA,IAAT,GAAe;AAAEC;AAAM", + sourcesContent: [ "function foo() { bar; }\n" ] + }, "sourcemap was incorrectly generated"); + + chai.expect(generated.code).to.equal( + "function foo2() {\n bar2;\n}\n", + "code was incorrectly generated" + ); + }); }); diff --git a/packages/babel-plugin-transform-regenerator/package.json b/packages/babel-plugin-transform-regenerator/package.json index 72d914be5770..48e5deace042 100644 --- a/packages/babel-plugin-transform-regenerator/package.json +++ b/packages/babel-plugin-transform-regenerator/package.json @@ -14,7 +14,7 @@ "babel-runtime": "^6.9.0", "babel-traverse": "^6.11.4", "babel-types": "^6.9.0", - "babylon": "^6.6.5", + "babylon": "^6.9.0", "private": "~0.1.5" }, "license": "BSD", diff --git a/packages/babel-template/package.json b/packages/babel-template/package.json index a14e7bbeb2bd..61124f31c70a 100644 --- a/packages/babel-template/package.json +++ b/packages/babel-template/package.json @@ -8,7 +8,7 @@ "repository": "https://github.com/babel/babel/tree/master/packages/babel-template", "main": "lib/index.js", "dependencies": { - "babylon": "^6.7.0", + "babylon": "^6.9.0", "babel-traverse": "^6.9.0", "babel-types": "^6.9.0", "babel-runtime": "^6.9.0", diff --git a/packages/babel-traverse/package.json b/packages/babel-traverse/package.json index 28b2ad2e6790..8ccbc776c229 100644 --- a/packages/babel-traverse/package.json +++ b/packages/babel-traverse/package.json @@ -12,7 +12,7 @@ "babel-messages": "^6.8.0", "babel-runtime": "^6.9.0", "babel-types": "^6.13.0", - "babylon": "^6.7.0", + "babylon": "^6.9.0", "debug": "^2.2.0", "globals": "^8.3.0", "invariant": "^2.2.0",