From c0e579ccc6f8e2185b771fa77668cd34281a6602 Mon Sep 17 00:00:00 2001 From: edge Date: Thu, 2 Feb 2017 17:31:02 -0800 Subject: [PATCH] add startLine option --- README.md | 2 + src/options.js | 4 + src/tokenizer/state.js | 2 +- .../categorized/startline-specified/actual.js | 2 + .../startline-specified/expected.json | 459 ++++++++++++++++++ .../startline-specified/options.json | 3 + 6 files changed, 471 insertions(+), 1 deletion(-) create mode 100644 test/fixtures/core/categorized/startline-specified/actual.js create mode 100644 test/fixtures/core/categorized/startline-specified/expected.json create mode 100644 test/fixtures/core/categorized/startline-specified/options.json diff --git a/README.md b/README.md index 68c136a851..0e8fba187a 100644 --- a/README.md +++ b/README.md @@ -44,6 +44,8 @@ Significant diversions are expected to occur in the future such as streaming, EB - **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. ### Output diff --git a/src/options.js b/src/options.js index 8e39e451e8..72c65f34a6 100755 --- a/src/options.js +++ b/src/options.js @@ -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, diff --git a/src/tokenizer/state.js b/src/tokenizer/state.js index 878bf6b932..371bcdfd7d 100644 --- a/src/tokenizer/state.js +++ b/src/tokenizer/state.js @@ -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; diff --git a/test/fixtures/core/categorized/startline-specified/actual.js b/test/fixtures/core/categorized/startline-specified/actual.js new file mode 100644 index 0000000000..e42b4a07cf --- /dev/null +++ b/test/fixtures/core/categorized/startline-specified/actual.js @@ -0,0 +1,2 @@ +call(1); +run(2); diff --git a/test/fixtures/core/categorized/startline-specified/expected.json b/test/fixtures/core/categorized/startline-specified/expected.json new file mode 100644 index 0000000000..52ce62c781 --- /dev/null +++ b/test/fixtures/core/categorized/startline-specified/expected.json @@ -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 + } + } + } + ] +} diff --git a/test/fixtures/core/categorized/startline-specified/options.json b/test/fixtures/core/categorized/startline-specified/options.json new file mode 100644 index 0000000000..c403343be2 --- /dev/null +++ b/test/fixtures/core/categorized/startline-specified/options.json @@ -0,0 +1,3 @@ +{ + "startLine": 3 +}