feat: support startColumn option (#13887)

This commit is contained in:
Huáng Jùnliàng 2021-10-28 19:20:32 -04:00 committed by GitHub
parent 718c6cb7de
commit 872086a9a0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 146 additions and 8 deletions

View File

@ -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

View File

@ -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

View File

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

View File

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

View File

@ -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": []
}
}

View File

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

View File

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

View File

@ -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": []
}
}