Attach filename property to node.loc when provided.

This commit is contained in:
Dale Bustad 2016-02-29 11:19:33 -08:00
parent f007b51554
commit 288b3ba559
8 changed files with 125 additions and 11 deletions

View File

@ -66,16 +66,10 @@ import {parse} from 'babylon';
import traverse from "babel-traverse";
import generate from 'babel-generator';
function addFilename (ast, filename) {
traverse.cheap(ast, node => {
if (node.loc) { node.loc.source = sourcePath };
});
}
const a = 'var a = 1;';
const b = 'var b = 2;';
const astA = addFilename(parse(a), 'a.js');
const astB = addFilename(parse(b), 'b.js');
const astA = parse(a, { filename: 'a.js' });
const astB = parse(b, { filename: 'b.js' });
const ast = {
type: 'Program',
body: [].concat(astA.body, ast2.body)

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