Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 1 commit into from Aug 16, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/babel-core/package.json
Expand Up @@ -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",
Expand Down
Expand Up @@ -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);"]
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/babel-generator/package.json
Expand Up @@ -20,6 +20,6 @@
},
"devDependencies": {
"babel-helper-fixtures": "^6.9.0",
"babylon": "^6.7.0"
"babylon": "^6.9.0"
}
}
16 changes: 10 additions & 6 deletions packages/babel-generator/src/buffer.js
Expand Up @@ -25,6 +25,7 @@ export default class Buffer {
column: 0,
};
_sourcePosition: Object = {
identifierName: null,
line: null,
column: null,
filename: null,
Expand All @@ -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);
}

/**
Expand All @@ -61,19 +62,19 @@ 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 {
let item;
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);
Expand Down Expand Up @@ -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;
Expand All @@ -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);

Expand All @@ -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 {
Expand Down
10 changes: 9 additions & 1 deletion packages/babel-generator/src/source-map.js
Expand Up @@ -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;

Expand All @@ -50,6 +57,7 @@ export default class SourceMap {
this._lastSourceColumn = column;

this._map.addMapping({
name: identifierName,
generated: {
line: generatedLine,
column: generatedColumn,
Expand Down
42 changes: 41 additions & 1 deletion packages/babel-generator/test/index.js
Expand Up @@ -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'
Expand All @@ -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"
);
});
});


Expand Down
2 changes: 1 addition & 1 deletion packages/babel-plugin-transform-regenerator/package.json
Expand Up @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion packages/babel-template/package.json
Expand Up @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion packages/babel-traverse/package.json
Expand Up @@ -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",
Expand Down