Attach filename property to node.loc when provided.

This commit is contained in:
Dale Bustad 2016-02-29 11:19:33 -08:00
parent aca8010438
commit bb4919500c
7 changed files with 123 additions and 3 deletions

View File

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

View File

@ -3,6 +3,7 @@
export const defaultOptions: {
sourceType: string,
sourceFilename: any,
allowReturnOutsideFunction: boolean,
allowImportExportEverywhere: boolean,
allowSuperOutsideMethod: boolean,
@ -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,

View File

@ -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] === "!") {

View File

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

View File

@ -0,0 +1 @@
var node = "shouldHaveFilenameLocProp";

View File

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

View File

@ -0,0 +1,3 @@
{
"sourceFilename": "path/to/input-file.js"
}