Skip to content

Commit

Permalink
Merge pull request babel#3323 from divmain/master
Browse files Browse the repository at this point in the history
Source-map support for multiple input source files
  • Loading branch information
amasad authored and JacopKane committed Jan 11, 2018
1 parent 85a7fc5 commit 0f8c008
Show file tree
Hide file tree
Showing 7 changed files with 123 additions and 3 deletions.
2 changes: 2 additions & 0 deletions packages/babylon/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ Significant diversions are expected to occur in the future such as streaming, EB
- **sourceType**: Indicate the mode the code should be parsed in. Can be
either `"script"` or `"module"`.

- **sourceFilename**: Correlate output AST nodes with their source filename. Useful when generating code and source maps from the ASTs of multiple input files.

- **plugins**: Array containing the plugins that you want to enable.

### Example
Expand Down
3 changes: 3 additions & 0 deletions packages/babylon/src/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

export const defaultOptions: {
sourceType: string,
sourceFilename: any,
allowReturnOutsideFunction: boolean,
allowImportExportEverywhere: boolean,
allowSuperOutsideMethod: boolean,
Expand All @@ -11,6 +12,8 @@ export const defaultOptions: {
} = {
// Source type ("script" or "module") for different semantics
sourceType: "script",
// Source filename.
sourceFilename: undefined,
// When enabled, a return at the top level is not considered an
// error.
allowReturnOutsideFunction: false,
Expand Down
1 change: 1 addition & 0 deletions packages/babylon/src/parser/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export default class Parser extends Tokenizer {
this.isReservedWord = reservedWords[6];
this.input = input;
this.plugins = this.loadPlugins(this.options.plugins);
this.filename = options.sourceFilename;

// If enabled, skip leading hashbang line.
if (this.state.pos === 0 && this.input[0] === "#" && this.input[1] === "!") {
Expand Down
7 changes: 4 additions & 3 deletions packages/babylon/src/parser/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@ import { SourceLocation } from "../util/location";
const pp = Parser.prototype;

class Node {
constructor(pos?: number, loc?: SourceLocation) {
constructor(pos?: number, loc?: SourceLocation, filename?: string) {
this.type = "";
this.start = pos;
this.end = 0;
this.loc = new SourceLocation(loc);
if (filename) this.loc.filename = filename;
}

type: string;
Expand All @@ -26,11 +27,11 @@ class Node {
}

pp.startNode = function () {
return new Node(this.state.start, this.state.startLoc);
return new Node(this.state.start, this.state.startLoc, this.filename);
};

pp.startNodeAt = function (pos, loc) {
return new Node(pos, loc);
return new Node(pos, loc, this.filename);
};

function finishNodeAt(node, type, pos, loc) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
var node = "shouldHaveFilenameLocProp";
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
{
"type": "File",
"start": 0,
"end": 39,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 39
},
"filename": "path/to/input-file.js"
},
"program": {
"type": "Program",
"start": 0,
"end": 39,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 39
},
"filename": "path/to/input-file.js"
},
"sourceType": "script",
"body": [
{
"type": "VariableDeclaration",
"start": 0,
"end": 39,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 39
},
"filename": "path/to/input-file.js"
},
"declarations": [
{
"type": "VariableDeclarator",
"start": 4,
"end": 38,
"loc": {
"start": {
"line": 1,
"column": 4
},
"end": {
"line": 1,
"column": 38
},
"filename": "path/to/input-file.js"
},
"id": {
"type": "Identifier",
"start": 4,
"end": 8,
"loc": {
"start": {
"line": 1,
"column": 4
},
"end": {
"line": 1,
"column": 8
},
"filename": "path/to/input-file.js"
},
"name": "node"
},
"init": {
"type": "StringLiteral",
"start": 11,
"end": 38,
"loc": {
"start": {
"line": 1,
"column": 11
},
"end": {
"line": 1,
"column": 38
},
"filename": "path/to/input-file.js"
},
"extra": {
"rawValue": "shouldHaveFilenameLocProp",
"raw": "\"shouldHaveFilenameLocProp\""
},
"value": "shouldHaveFilenameLocProp"
}
}
],
"kind": "var"
}
],
"directives": []
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"sourceFilename": "path/to/input-file.js"
}

0 comments on commit 0f8c008

Please sign in to comment.