Generate names field for identifiers to get correct names mappings (#3658)

This commit is contained in:
Sebastian McKenzie
2016-08-16 18:43:55 +01:00
committed by Henry Zhu
parent aa51dd4a3d
commit 4a19661346
14 changed files with 76 additions and 20 deletions

View File

@@ -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==

View File

@@ -3,4 +3,4 @@
arr.map(function (x) {
return x * MULTIPLIER;
});
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIi4uL3NyYy9mb28uanMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6Ijs7QUFBQSxJQUFJLEdBQUosQ0FBUTtBQUFBLFNBQUssSUFBSSxVQUFUO0FBQUEsQ0FBUiIsImZpbGUiOiJmb28uanMiLCJzb3VyY2VzQ29udGVudCI6WyJhcnIubWFwKHggPT4geCAqIE1VTFRJUExJRVIpOyJdfQ==
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIi4uL3NyYy9mb28uanMiXSwibmFtZXMiOlsiYXJyIiwibWFwIiwieCIsIk1VTFRJUExJRVIiXSwibWFwcGluZ3MiOiI7O0FBQUFBLElBQUlDLEdBQUosQ0FBUTtBQUFBLFNBQUtDLElBQUlDLFVBQVQ7QUFBQSxDQUFSIiwiZmlsZSI6ImZvby5qcyIsInNvdXJjZXNDb250ZW50IjpbImFyci5tYXAoeCA9PiB4ICogTVVMVElQTElFUik7Il19

View File

@@ -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}"]}

View File

@@ -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);"]}

View File

@@ -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",

View File

@@ -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);"]
}

View File

@@ -1,4 +1,4 @@
arr.map(function (x) {
return x * x;
});
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbInNvdXJjZS1tYXBzL2lubGluZS9hY3R1YWwuanMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IkFBQUEsSUFBSSxHQUFKLENBQVE7QUFBQSxTQUFLLElBQUksQ0FBVDtBQUFBLENBQVIiLCJmaWxlIjoic291cmNlLW1hcHMvaW5saW5lL2V4cGVjdGVkLmpzIiwic291cmNlc0NvbnRlbnQiOlsiYXJyLm1hcCh4ID0+IHggKiB4KTsiXX0=
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbInNvdXJjZS1tYXBzL2lubGluZS9hY3R1YWwuanMiXSwibmFtZXMiOlsiYXJyIiwibWFwIiwieCJdLCJtYXBwaW5ncyI6IkFBQUFBLElBQUlDLEdBQUosQ0FBUTtBQUFBLFNBQUtDLElBQUlBLENBQVQ7QUFBQSxDQUFSIiwiZmlsZSI6InNvdXJjZS1tYXBzL2lubGluZS9leHBlY3RlZC5qcyIsInNvdXJjZXNDb250ZW50IjpbImFyci5tYXAoeCA9PiB4ICogeCk7Il19

View File

@@ -20,6 +20,6 @@
},
"devDependencies": {
"babel-helper-fixtures": "^6.9.0",
"babylon": "^6.7.0"
"babylon": "^6.9.0"
}
}

View File

@@ -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 {

View File

@@ -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,

View File

@@ -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}",
"code was incorrectly generated"
);
});
});

View File

@@ -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",

View File

@@ -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",

View File

@@ -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",