Skip to content

Commit

Permalink
feat: support startColumn option (#13887)
Browse files Browse the repository at this point in the history
  • Loading branch information
JLHwung committed Oct 28, 2021
1 parent 718c6cb commit 872086a
Show file tree
Hide file tree
Showing 8 changed files with 146 additions and 8 deletions.
6 changes: 5 additions & 1 deletion packages/babel-parser/src/options.js
Expand Up @@ -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,
Expand All @@ -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
Expand Down
15 changes: 8 additions & 7 deletions packages/babel-parser/src/tokenizer/state.js
Expand Up @@ -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[] = [];
Expand Down Expand Up @@ -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
Expand Down
@@ -0,0 +1,2 @@
call(1);
run(2);
@@ -0,0 +1,3 @@
{
"startColumn": 3
}
@@ -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": []
}
}
@@ -0,0 +1,2 @@
call(1);
run(2);
@@ -0,0 +1,4 @@
{
"startLine": 3,
"startColumn": 3
}
@@ -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": []
}
}

0 comments on commit 872086a

Please sign in to comment.