add startLine option (#346)

This commit is contained in:
Raphael Mu 2017-02-10 06:06:28 -08:00 committed by Daniel Tschinder
parent 407c97c9c2
commit f25a2fbc78
6 changed files with 471 additions and 1 deletions

View File

@ -50,6 +50,8 @@ mind. When in doubt, use `.parse()`.
- **sourceFilename**: Correlate output AST nodes with their source filename. Useful when generating code and source maps from the ASTs of multiple input files.
- **startLine**: By default, the first line of code parsed is treated as line 1. You can provide a line number to alternatively start with. Useful for integration with other source tools.
- **plugins**: Array containing the plugins that you want to enable.
- **strictMode**: TODO

View File

@ -4,6 +4,7 @@
export const defaultOptions: {
sourceType: string,
sourceFilename: any,
startLine: number,
allowReturnOutsideFunction: boolean,
allowImportExportEverywhere: boolean,
allowSuperOutsideMethod: boolean,
@ -14,6 +15,9 @@ export const defaultOptions: {
sourceType: "script",
// Source filename.
sourceFilename: undefined,
// Line from which to start counting source. Useful for
// integration with other tools.
startLine: 1,
// When enabled, a return at the top level is not considered an
// error.
allowReturnOutsideFunction: false,

View File

@ -34,7 +34,7 @@ export default class State {
this.commentStack = [];
this.pos = this.lineStart = 0;
this.curLine = 1;
this.curLine = options.startLine;
this.type = tt.eof;
this.value = null;

View File

@ -0,0 +1,2 @@
call(1);
run(2);

View File

@ -0,0 +1,459 @@
{
"type": "File",
"start": 0,
"end": 16,
"loc": {
"start": {
"line": 3,
"column": 0
},
"end": {
"line": 4,
"column": 7
}
},
"program": {
"type": "Program",
"start": 0,
"end": 16,
"loc": {
"start": {
"line": 3,
"column": 0
},
"end": {
"line": 4,
"column": 7
}
},
"sourceType": "script",
"body": [
{
"type": "ExpressionStatement",
"start": 0,
"end": 8,
"loc": {
"start": {
"line": 3,
"column": 0
},
"end": {
"line": 3,
"column": 8
}
},
"expression": {
"type": "CallExpression",
"start": 0,
"end": 7,
"loc": {
"start": {
"line": 3,
"column": 0
},
"end": {
"line": 3,
"column": 7
}
},
"callee": {
"type": "Identifier",
"start": 0,
"end": 4,
"loc": {
"start": {
"line": 3,
"column": 0
},
"end": {
"line": 3,
"column": 4
},
"identifierName": "call"
},
"name": "call"
},
"arguments": [
{
"type": "NumericLiteral",
"start": 5,
"end": 6,
"loc": {
"start": {
"line": 3,
"column": 5
},
"end": {
"line": 3,
"column": 6
}
},
"extra": {
"rawValue": 1,
"raw": "1"
},
"value": 1
}
]
}
},
{
"type": "ExpressionStatement",
"start": 9,
"end": 16,
"loc": {
"start": {
"line": 4,
"column": 0
},
"end": {
"line": 4,
"column": 7
}
},
"expression": {
"type": "CallExpression",
"start": 9,
"end": 15,
"loc": {
"start": {
"line": 4,
"column": 0
},
"end": {
"line": 4,
"column": 6
}
},
"callee": {
"type": "Identifier",
"start": 9,
"end": 12,
"loc": {
"start": {
"line": 4,
"column": 0
},
"end": {
"line": 4,
"column": 3
},
"identifierName": "run"
},
"name": "run"
},
"arguments": [
{
"type": "NumericLiteral",
"start": 13,
"end": 14,
"loc": {
"start": {
"line": 4,
"column": 4
},
"end": {
"line": 4,
"column": 5
}
},
"extra": {
"rawValue": 2,
"raw": "2"
},
"value": 2
}
]
}
}
],
"directives": []
},
"comments": [],
"tokens": [
{
"type": {
"label": "name",
"beforeExpr": false,
"startsExpr": true,
"rightAssociative": false,
"isLoop": false,
"isAssign": false,
"prefix": false,
"postfix": false,
"binop": null
},
"value": "call",
"start": 0,
"end": 4,
"loc": {
"start": {
"line": 3,
"column": 0
},
"end": {
"line": 3,
"column": 4
}
}
},
{
"type": {
"label": "(",
"beforeExpr": true,
"startsExpr": true,
"rightAssociative": false,
"isLoop": false,
"isAssign": false,
"prefix": false,
"postfix": false,
"binop": null
},
"start": 4,
"end": 5,
"loc": {
"start": {
"line": 3,
"column": 4
},
"end": {
"line": 3,
"column": 5
}
}
},
{
"type": {
"label": "num",
"beforeExpr": false,
"startsExpr": true,
"rightAssociative": false,
"isLoop": false,
"isAssign": false,
"prefix": false,
"postfix": false,
"binop": null,
"updateContext": null
},
"value": 1,
"start": 5,
"end": 6,
"loc": {
"start": {
"line": 3,
"column": 5
},
"end": {
"line": 3,
"column": 6
}
}
},
{
"type": {
"label": ")",
"beforeExpr": false,
"startsExpr": false,
"rightAssociative": false,
"isLoop": false,
"isAssign": false,
"prefix": false,
"postfix": false,
"binop": null
},
"start": 6,
"end": 7,
"loc": {
"start": {
"line": 3,
"column": 6
},
"end": {
"line": 3,
"column": 7
}
}
},
{
"type": {
"label": ";",
"beforeExpr": true,
"startsExpr": false,
"rightAssociative": false,
"isLoop": false,
"isAssign": false,
"prefix": false,
"postfix": false,
"binop": null,
"updateContext": null
},
"start": 7,
"end": 8,
"loc": {
"start": {
"line": 3,
"column": 7
},
"end": {
"line": 3,
"column": 8
}
}
},
{
"type": {
"label": "name",
"beforeExpr": false,
"startsExpr": true,
"rightAssociative": false,
"isLoop": false,
"isAssign": false,
"prefix": false,
"postfix": false,
"binop": null
},
"value": "run",
"start": 9,
"end": 12,
"loc": {
"start": {
"line": 4,
"column": 0
},
"end": {
"line": 4,
"column": 3
}
}
},
{
"type": {
"label": "(",
"beforeExpr": true,
"startsExpr": true,
"rightAssociative": false,
"isLoop": false,
"isAssign": false,
"prefix": false,
"postfix": false,
"binop": null
},
"start": 12,
"end": 13,
"loc": {
"start": {
"line": 4,
"column": 3
},
"end": {
"line": 4,
"column": 4
}
}
},
{
"type": {
"label": "num",
"beforeExpr": false,
"startsExpr": true,
"rightAssociative": false,
"isLoop": false,
"isAssign": false,
"prefix": false,
"postfix": false,
"binop": null,
"updateContext": null
},
"value": 2,
"start": 13,
"end": 14,
"loc": {
"start": {
"line": 4,
"column": 4
},
"end": {
"line": 4,
"column": 5
}
}
},
{
"type": {
"label": ")",
"beforeExpr": false,
"startsExpr": false,
"rightAssociative": false,
"isLoop": false,
"isAssign": false,
"prefix": false,
"postfix": false,
"binop": null
},
"start": 14,
"end": 15,
"loc": {
"start": {
"line": 4,
"column": 5
},
"end": {
"line": 4,
"column": 6
}
}
},
{
"type": {
"label": ";",
"beforeExpr": true,
"startsExpr": false,
"rightAssociative": false,
"isLoop": false,
"isAssign": false,
"prefix": false,
"postfix": false,
"binop": null,
"updateContext": null
},
"start": 15,
"end": 16,
"loc": {
"start": {
"line": 4,
"column": 6
},
"end": {
"line": 4,
"column": 7
}
}
},
{
"type": {
"label": "eof",
"beforeExpr": false,
"startsExpr": false,
"rightAssociative": false,
"isLoop": false,
"isAssign": false,
"prefix": false,
"postfix": false,
"binop": null,
"updateContext": null
},
"start": 16,
"end": 16,
"loc": {
"start": {
"line": 4,
"column": 7
},
"end": {
"line": 4,
"column": 7
}
}
}
]
}

View File

@ -0,0 +1,3 @@
{
"startLine": 3
}