Skip to content

Commit

Permalink
feat: add startIndex parser option (#16849)
Browse files Browse the repository at this point in the history
  • Loading branch information
DylanPiercey authored Oct 23, 2024
1 parent b8d6334 commit 77bc5d5
Show file tree
Hide file tree
Showing 23 changed files with 251 additions and 31 deletions.
18 changes: 18 additions & 0 deletions packages/babel-parser/src/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export type SourceType = "script" | "module" | "unambiguous";
export interface Options {
sourceType?: SourceType;
sourceFilename?: string;
startIndex?: number;
startColumn?: number;
startLine?: number;
allowAwaitOutsideFunction?: boolean;
Expand Down Expand Up @@ -35,6 +36,9 @@ function createDefaultOptions(): OptionsWithDefaults {
sourceType: "script",
// Source filename.
sourceFilename: undefined,
// Index (0-based) from which to start counting source. Useful for
// integration with other tools.
startIndex: 0,
// Column (0-based) from which to start counting source. Useful for
// integration with other tools.
startColumn: 0,
Expand Down Expand Up @@ -111,5 +115,19 @@ export function getOptions(opts?: Options | null): OptionsWithDefaults {
if (opts[key] != null) options[key] = opts[key];
}

if (options.startLine === 1) {
if (opts.startIndex == null && options.startColumn > 0) {
options.startIndex = options.startColumn;
} else if (opts.startColumn == null && options.startIndex > 0) {
options.startColumn = options.startIndex;
}
} else if (opts.startColumn == null || opts.startIndex == null) {
if (opts.startIndex != null || process.env.BABEL_8_BREAKING) {
throw new Error(
"With a `startLine > 1` you must also specify `startIndex` and `startColumn`.",
);
}
}

return options;
}
20 changes: 17 additions & 3 deletions packages/babel-parser/src/tokenizer/state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export default class State {

@bit accessor strict = false;

startIndex: number;
curLine: number;
lineStart: number;

Expand All @@ -43,17 +44,28 @@ export default class State {
startLoc: Position;
endLoc: Position;

init({ strictMode, sourceType, startLine, startColumn }: Options): void {
init({
strictMode,
sourceType,
startIndex,
startLine,
startColumn,
}: Options): void {
this.strict =
strictMode === false
? false
: strictMode === true
? true
: sourceType === "module";

this.startIndex = startIndex;
this.curLine = startLine;
this.lineStart = -startColumn;
this.startLoc = this.endLoc = new Position(startLine, startColumn, 0);
this.startLoc = this.endLoc = new Position(
startLine,
startColumn,
startIndex,
);
}

errors: ParseError<any>[] = [];
Expand Down Expand Up @@ -162,12 +174,14 @@ export default class State {
*/

curPosition(): Position {
return new Position(this.curLine, this.pos - this.lineStart, this.pos);
const index = this.pos + this.startIndex;
return new Position(this.curLine, index - this.lineStart, index);
}

clone(): State {
const state = new State();
state.flags = this.flags;
state.startIndex = this.startIndex;
state.curLine = this.curLine;
state.lineStart = this.lineStart;
state.startLoc = this.startLoc;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
call(1);
run(2);
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"startIndex": 3,
"startLine": 2,
"throws": "With a `startLine > 1` you must also specify `startIndex` and `startColumn`."
}
Original file line number Diff line number Diff line change
@@ -1,30 +1,30 @@
{
"type": "File",
"start":0,"end":16,"loc":{"start":{"line":1,"column":3,"index":0},"end":{"line":2,"column":7,"index":16}},
"start":3,"end":19,"loc":{"start":{"line":1,"column":3,"index":3},"end":{"line":2,"column":10,"index":19}},
"program": {
"type": "Program",
"start":0,"end":16,"loc":{"start":{"line":1,"column":3,"index":0},"end":{"line":2,"column":7,"index":16}},
"start":3,"end":19,"loc":{"start":{"line":1,"column":3,"index":3},"end":{"line":2,"column":10,"index":19}},
"sourceType": "script",
"interpreter": null,
"body": [
{
"type": "ExpressionStatement",
"start":0,"end":8,"loc":{"start":{"line":1,"column":3,"index":0},"end":{"line":1,"column":11,"index":8}},
"start":3,"end":11,"loc":{"start":{"line":1,"column":6,"index":3},"end":{"line":1,"column":14,"index":11}},
"expression": {
"type": "CallExpression",
"start":0,"end":7,"loc":{"start":{"line":1,"column":3,"index":0},"end":{"line":1,"column":10,"index":7}},
"start":3,"end":10,"loc":{"start":{"line":1,"column":6,"index":3},"end":{"line":1,"column":13,"index":10}},
"callee": {
"type": "Identifier",
"start":0,"end":4,"loc":{"start":{"line":1,"column":3,"index":0},"end":{"line":1,"column":7,"index":4},"identifierName":"call"},
"start":3,"end":7,"loc":{"start":{"line":1,"column":6,"index":3},"end":{"line":1,"column":10,"index":7},"identifierName":"call"},
"name": "call"
},
"arguments": [
{
"type": "NumericLiteral",
"start":5,"end":6,"loc":{"start":{"line":1,"column":8,"index":5},"end":{"line":1,"column":9,"index":6}},
"start":8,"end":9,"loc":{"start":{"line":1,"column":11,"index":8},"end":{"line":1,"column":12,"index":9}},
"extra": {
"rawValue": 1,
"raw": "1"
"raw": ""
},
"value": 1
}
Expand All @@ -33,22 +33,22 @@
},
{
"type": "ExpressionStatement",
"start":9,"end":16,"loc":{"start":{"line":2,"column":0,"index":9},"end":{"line":2,"column":7,"index":16}},
"start":12,"end":19,"loc":{"start":{"line":2,"column":3,"index":12},"end":{"line":2,"column":10,"index":19}},
"expression": {
"type": "CallExpression",
"start":9,"end":15,"loc":{"start":{"line":2,"column":0,"index":9},"end":{"line":2,"column":6,"index":15}},
"start":12,"end":18,"loc":{"start":{"line":2,"column":3,"index":12},"end":{"line":2,"column":9,"index":18}},
"callee": {
"type": "Identifier",
"start":9,"end":12,"loc":{"start":{"line":2,"column":0,"index":9},"end":{"line":2,"column":3,"index":12},"identifierName":"run"},
"start":12,"end":15,"loc":{"start":{"line":2,"column":3,"index":12},"end":{"line":2,"column":6,"index":15},"identifierName":"run"},
"name": "run"
},
"arguments": [
{
"type": "NumericLiteral",
"start":13,"end":14,"loc":{"start":{"line":2,"column":4,"index":13},"end":{"line":2,"column":5,"index":14}},
"start":16,"end":17,"loc":{"start":{"line":2,"column":7,"index":16},"end":{"line":2,"column":8,"index":17}},
"extra": {
"rawValue": 2,
"raw": "2"
"raw": ""
},
"value": 2
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
call(1);
run(2);
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"startIndex": 3
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
{
"type": "File",
"start":3,"end":19,"loc":{"start":{"line":1,"column":3,"index":3},"end":{"line":2,"column":10,"index":19}},
"program": {
"type": "Program",
"start":3,"end":19,"loc":{"start":{"line":1,"column":3,"index":3},"end":{"line":2,"column":10,"index":19}},
"sourceType": "script",
"interpreter": null,
"body": [
{
"type": "ExpressionStatement",
"start":3,"end":11,"loc":{"start":{"line":1,"column":6,"index":3},"end":{"line":1,"column":14,"index":11}},
"expression": {
"type": "CallExpression",
"start":3,"end":10,"loc":{"start":{"line":1,"column":6,"index":3},"end":{"line":1,"column":13,"index":10}},
"callee": {
"type": "Identifier",
"start":3,"end":7,"loc":{"start":{"line":1,"column":6,"index":3},"end":{"line":1,"column":10,"index":7},"identifierName":"call"},
"name": "call"
},
"arguments": [
{
"type": "NumericLiteral",
"start":8,"end":9,"loc":{"start":{"line":1,"column":11,"index":8},"end":{"line":1,"column":12,"index":9}},
"extra": {
"rawValue": 1,
"raw": ""
},
"value": 1
}
]
}
},
{
"type": "ExpressionStatement",
"start":12,"end":19,"loc":{"start":{"line":2,"column":3,"index":12},"end":{"line":2,"column":10,"index":19}},
"expression": {
"type": "CallExpression",
"start":12,"end":18,"loc":{"start":{"line":2,"column":3,"index":12},"end":{"line":2,"column":9,"index":18}},
"callee": {
"type": "Identifier",
"start":12,"end":15,"loc":{"start":{"line":2,"column":3,"index":12},"end":{"line":2,"column":6,"index":15},"identifierName":"run"},
"name": "run"
},
"arguments": [
{
"type": "NumericLiteral",
"start":16,"end":17,"loc":{"start":{"line":2,"column":7,"index":16},"end":{"line":2,"column":8,"index":17}},
"extra": {
"rawValue": 2,
"raw": ""
},
"value": 2
}
]
}
}
],
"directives": []
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
call(1);
run(2);
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"startIndex": 3,
"startLine": 3,
"startColumn": 3
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
{
"type": "File",
"start":3,"end":19,"loc":{"start":{"line":3,"column":3,"index":3},"end":{"line":4,"column":10,"index":19}},
"program": {
"type": "Program",
"start":3,"end":19,"loc":{"start":{"line":3,"column":3,"index":3},"end":{"line":4,"column":10,"index":19}},
"sourceType": "script",
"interpreter": null,
"body": [
{
"type": "ExpressionStatement",
"start":3,"end":11,"loc":{"start":{"line":3,"column":6,"index":3},"end":{"line":3,"column":14,"index":11}},
"expression": {
"type": "CallExpression",
"start":3,"end":10,"loc":{"start":{"line":3,"column":6,"index":3},"end":{"line":3,"column":13,"index":10}},
"callee": {
"type": "Identifier",
"start":3,"end":7,"loc":{"start":{"line":3,"column":6,"index":3},"end":{"line":3,"column":10,"index":7},"identifierName":"call"},
"name": "call"
},
"arguments": [
{
"type": "NumericLiteral",
"start":8,"end":9,"loc":{"start":{"line":3,"column":11,"index":8},"end":{"line":3,"column":12,"index":9}},
"extra": {
"rawValue": 1,
"raw": ""
},
"value": 1
}
]
}
},
{
"type": "ExpressionStatement",
"start":12,"end":19,"loc":{"start":{"line":4,"column":3,"index":12},"end":{"line":4,"column":10,"index":19}},
"expression": {
"type": "CallExpression",
"start":12,"end":18,"loc":{"start":{"line":4,"column":3,"index":12},"end":{"line":4,"column":9,"index":18}},
"callee": {
"type": "Identifier",
"start":12,"end":15,"loc":{"start":{"line":4,"column":3,"index":12},"end":{"line":4,"column":6,"index":15},"identifierName":"run"},
"name": "run"
},
"arguments": [
{
"type": "NumericLiteral",
"start":16,"end":17,"loc":{"start":{"line":4,"column":7,"index":16},"end":{"line":4,"column":8,"index":17}},
"extra": {
"rawValue": 2,
"raw": ""
},
"value": 2
}
]
}
}
],
"directives": []
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
call(1);
run(2);
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"BABEL_8_BREAKING": false,
"startLine": 3,
"startColumn": 3
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
{
"BABEL_8_BREAKING": true,
"startLine": 3,
"startColumn": 3
"startColumn": 3,
"throws": "With a `startLine > 1` you must also specify `startIndex` and `startColumn`."
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
call(1);
run(2);
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"BABEL_8_BREAKING": false,
"startLine": 3
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
{
"startLine": 3
"BABEL_8_BREAKING": true,
"startLine": 3,
"throws": "With a `startLine > 1` you must also specify `startIndex` and `startColumn`."
}
8 changes: 6 additions & 2 deletions packages/babel-parser/test/helpers/run-fixture-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,12 @@ function runAutogeneratedParseTests(

const expected = deserialize(expect.loc, options, expect.code);
const title = `${prefix}/${task.title}`;
const toStartPosition = ({ startLine = 1, startColumn = 0 }) =>
`(${startLine}, ${startColumn})`;
const toStartPosition = ({
startIndex = 0,
startLine = 1,
startColumn = 0,
}) =>
`(${startIndex === 0 ? "" : `${startIndex}, `}${startLine}, ${startColumn})`;

toFuzzedOptions(options)
.map(([adjust, options], index) => ({
Expand Down
Loading

0 comments on commit 77bc5d5

Please sign in to comment.