From 872086a9a04d7ad728819076cd0820ed557c6c72 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hu=C3=A1ng=20J=C3=B9nli=C3=A0ng?= Date: Thu, 28 Oct 2021 19:20:32 -0400 Subject: [PATCH] feat: support `startColumn` option (#13887) --- packages/babel-parser/src/options.js | 6 +- packages/babel-parser/src/tokenizer/state.js | 15 ++--- .../startcolumn-specified/input.js | 2 + .../startcolumn-specified/options.json | 3 + .../startcolumn-specified/output.json | 61 +++++++++++++++++++ .../input.js | 2 + .../options.json | 4 ++ .../output.json | 61 +++++++++++++++++++ 8 files changed, 146 insertions(+), 8 deletions(-) create mode 100644 packages/babel-parser/test/fixtures/core/categorized/startcolumn-specified/input.js create mode 100644 packages/babel-parser/test/fixtures/core/categorized/startcolumn-specified/options.json create mode 100644 packages/babel-parser/test/fixtures/core/categorized/startcolumn-specified/output.json create mode 100644 packages/babel-parser/test/fixtures/core/categorized/startline-and-startcolumn-specified/input.js create mode 100644 packages/babel-parser/test/fixtures/core/categorized/startline-and-startcolumn-specified/options.json create mode 100644 packages/babel-parser/test/fixtures/core/categorized/startline-and-startcolumn-specified/output.json diff --git a/packages/babel-parser/src/options.js b/packages/babel-parser/src/options.js index ce0fff102458..e181d3da38f9 100644 --- a/packages/babel-parser/src/options.js +++ b/packages/babel-parser/src/options.js @@ -10,6 +10,7 @@ export type SourceType = "script" | "module" | "unambiguous"; export type Options = { sourceType: SourceType, sourceFilename?: string, + startColumn: number, startLine: number, allowAwaitOutsideFunction: boolean, allowReturnOutsideFunction: boolean, @@ -30,7 +31,10 @@ export const defaultOptions: Options = { sourceType: "script", // Source filename. sourceFilename: undefined, - // Line from which to start counting source. Useful for + // Column (0-based) from which to start counting source. Useful for + // integration with other tools. + startColumn: 0, + // Line (1-based) from which to start counting source. Useful for // integration with other tools. startLine: 1, // When enabled, await at the top level is not considered an diff --git a/packages/babel-parser/src/tokenizer/state.js b/packages/babel-parser/src/tokenizer/state.js index 2fe8bfd120d2..2e5d3fcfad1d 100644 --- a/packages/babel-parser/src/tokenizer/state.js +++ b/packages/babel-parser/src/tokenizer/state.js @@ -25,22 +25,24 @@ type TopicContextState = { export default class State { strict: boolean; curLine: number; + lineStart: number; // And, if locations are used, the {line, column} object // corresponding to those offsets startLoc: Position; endLoc: Position; - init(options: Options): void { + init({ strictMode, sourceType, startLine, startColumn }: Options): void { this.strict = - options.strictMode === false + strictMode === false ? false - : options.strictMode === true + : strictMode === true ? true - : options.sourceType === "module"; + : sourceType === "module"; - this.curLine = options.startLine; - this.startLoc = this.endLoc = this.curPosition(); + this.curLine = startLine; + this.lineStart = -startColumn; + this.startLoc = this.endLoc = new Position(startLine, startColumn); } errors: ParsingError[] = []; @@ -101,7 +103,6 @@ export default class State { // The current position of the tokenizer in the input. pos: number = 0; - lineStart: number = 0; // Properties of the current token: // Its type diff --git a/packages/babel-parser/test/fixtures/core/categorized/startcolumn-specified/input.js b/packages/babel-parser/test/fixtures/core/categorized/startcolumn-specified/input.js new file mode 100644 index 000000000000..e42b4a07cf76 --- /dev/null +++ b/packages/babel-parser/test/fixtures/core/categorized/startcolumn-specified/input.js @@ -0,0 +1,2 @@ +call(1); +run(2); diff --git a/packages/babel-parser/test/fixtures/core/categorized/startcolumn-specified/options.json b/packages/babel-parser/test/fixtures/core/categorized/startcolumn-specified/options.json new file mode 100644 index 000000000000..a7f3f1e0d608 --- /dev/null +++ b/packages/babel-parser/test/fixtures/core/categorized/startcolumn-specified/options.json @@ -0,0 +1,3 @@ +{ + "startColumn": 3 +} diff --git a/packages/babel-parser/test/fixtures/core/categorized/startcolumn-specified/output.json b/packages/babel-parser/test/fixtures/core/categorized/startcolumn-specified/output.json new file mode 100644 index 000000000000..4dfdc5e4553d --- /dev/null +++ b/packages/babel-parser/test/fixtures/core/categorized/startcolumn-specified/output.json @@ -0,0 +1,61 @@ +{ + "type": "File", + "start":0,"end":16,"loc":{"start":{"line":1,"column":3},"end":{"line":2,"column":7}}, + "program": { + "type": "Program", + "start":0,"end":16,"loc":{"start":{"line":1,"column":3},"end":{"line":2,"column":7}}, + "sourceType": "script", + "interpreter": null, + "body": [ + { + "type": "ExpressionStatement", + "start":0,"end":8,"loc":{"start":{"line":1,"column":3},"end":{"line":1,"column":11}}, + "expression": { + "type": "CallExpression", + "start":0,"end":7,"loc":{"start":{"line":1,"column":3},"end":{"line":1,"column":10}}, + "callee": { + "type": "Identifier", + "start":0,"end":4,"loc":{"start":{"line":1,"column":3},"end":{"line":1,"column":7},"identifierName":"call"}, + "name": "call" + }, + "arguments": [ + { + "type": "NumericLiteral", + "start":5,"end":6,"loc":{"start":{"line":1,"column":8},"end":{"line":1,"column":9}}, + "extra": { + "rawValue": 1, + "raw": "1" + }, + "value": 1 + } + ] + } + }, + { + "type": "ExpressionStatement", + "start":9,"end":16,"loc":{"start":{"line":2,"column":0},"end":{"line":2,"column":7}}, + "expression": { + "type": "CallExpression", + "start":9,"end":15,"loc":{"start":{"line":2,"column":0},"end":{"line":2,"column":6}}, + "callee": { + "type": "Identifier", + "start":9,"end":12,"loc":{"start":{"line":2,"column":0},"end":{"line":2,"column":3},"identifierName":"run"}, + "name": "run" + }, + "arguments": [ + { + "type": "NumericLiteral", + "start":13,"end":14,"loc":{"start":{"line":2,"column":4},"end":{"line":2,"column":5}}, + "extra": { + "rawValue": 2, + "raw": "2" + }, + "value": 2 + } + ] + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/core/categorized/startline-and-startcolumn-specified/input.js b/packages/babel-parser/test/fixtures/core/categorized/startline-and-startcolumn-specified/input.js new file mode 100644 index 000000000000..e42b4a07cf76 --- /dev/null +++ b/packages/babel-parser/test/fixtures/core/categorized/startline-and-startcolumn-specified/input.js @@ -0,0 +1,2 @@ +call(1); +run(2); diff --git a/packages/babel-parser/test/fixtures/core/categorized/startline-and-startcolumn-specified/options.json b/packages/babel-parser/test/fixtures/core/categorized/startline-and-startcolumn-specified/options.json new file mode 100644 index 000000000000..9940c1e559e2 --- /dev/null +++ b/packages/babel-parser/test/fixtures/core/categorized/startline-and-startcolumn-specified/options.json @@ -0,0 +1,4 @@ +{ + "startLine": 3, + "startColumn": 3 +} diff --git a/packages/babel-parser/test/fixtures/core/categorized/startline-and-startcolumn-specified/output.json b/packages/babel-parser/test/fixtures/core/categorized/startline-and-startcolumn-specified/output.json new file mode 100644 index 000000000000..a43f645fca5c --- /dev/null +++ b/packages/babel-parser/test/fixtures/core/categorized/startline-and-startcolumn-specified/output.json @@ -0,0 +1,61 @@ +{ + "type": "File", + "start":0,"end":16,"loc":{"start":{"line":3,"column":3},"end":{"line":4,"column":7}}, + "program": { + "type": "Program", + "start":0,"end":16,"loc":{"start":{"line":3,"column":3},"end":{"line":4,"column":7}}, + "sourceType": "script", + "interpreter": null, + "body": [ + { + "type": "ExpressionStatement", + "start":0,"end":8,"loc":{"start":{"line":3,"column":3},"end":{"line":3,"column":11}}, + "expression": { + "type": "CallExpression", + "start":0,"end":7,"loc":{"start":{"line":3,"column":3},"end":{"line":3,"column":10}}, + "callee": { + "type": "Identifier", + "start":0,"end":4,"loc":{"start":{"line":3,"column":3},"end":{"line":3,"column":7},"identifierName":"call"}, + "name": "call" + }, + "arguments": [ + { + "type": "NumericLiteral", + "start":5,"end":6,"loc":{"start":{"line":3,"column":8},"end":{"line":3,"column":9}}, + "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": [] + } +} \ No newline at end of file