From d25155942a80be8123f32693974db6dbc6fced5d Mon Sep 17 00:00:00 2001 From: Francisco Ryan Tolmasky I Date: Mon, 10 Jan 2022 15:56:11 -0800 Subject: [PATCH] Fix some errors that ignore startLine by adding startLine to getLineInfo. Closes #14123. Reviewed by @tolmasky. --- packages/babel-parser/src/parser/error.js | 2 +- packages/babel-parser/src/util/location.js | 8 ++++++-- .../babel-parser/test/unit/util/location.skip-bundled.js | 8 ++++---- 3 files changed, 11 insertions(+), 7 deletions(-) diff --git a/packages/babel-parser/src/parser/error.js b/packages/babel-parser/src/parser/error.js index 4dc30ba98925..39c48f0cb555 100644 --- a/packages/babel-parser/src/parser/error.js +++ b/packages/babel-parser/src/parser/error.js @@ -78,7 +78,7 @@ export default class ParserError extends CommentsParser { else if (pos === this.state.lastTokStart) loc = this.state.lastTokStartLoc; else if (pos === this.state.end) loc = this.state.endLoc; else if (pos === this.state.lastTokEnd) loc = this.state.lastTokEndLoc; - else loc = getLineInfo(this.input, pos); + else loc = getLineInfo(this.state.startLoc.line, this.input, pos); return loc; } diff --git a/packages/babel-parser/src/util/location.js b/packages/babel-parser/src/util/location.js index c2bfd6f4ba9c..daae0e409c48 100644 --- a/packages/babel-parser/src/util/location.js +++ b/packages/babel-parser/src/util/location.js @@ -38,8 +38,12 @@ export class SourceLocation { // offset. `input` should be the code string that the offset refers // into. -export function getLineInfo(input: string, offset: number): Position { - let line = 1; +export function getLineInfo( + startLine: number, + input: string, + offset: number, +): Position { + let line = startLine; let lineStart = 0; let match; lineBreakG.lastIndex = 0; diff --git a/packages/babel-parser/test/unit/util/location.skip-bundled.js b/packages/babel-parser/test/unit/util/location.skip-bundled.js index 463463c88921..38b98264174a 100644 --- a/packages/babel-parser/test/unit/util/location.skip-bundled.js +++ b/packages/babel-parser/test/unit/util/location.skip-bundled.js @@ -4,14 +4,14 @@ describe("getLineInfo", () => { const input = "a\nb\nc\nd\ne\nf\ng\nh\ni"; it("reports correct position", () => { - expect(getLineInfo(input, 7)).toEqual({ + expect(getLineInfo(1, input, 7)).toEqual({ column: 1, line: 4, }); }); it("reports correct position for first line", () => { - expect(getLineInfo(input, 0)).toEqual({ + expect(getLineInfo(1, input, 0)).toEqual({ column: 0, line: 1, }); @@ -22,7 +22,7 @@ describe("getLineInfo", () => { singleCharLineEndings.forEach(ending => { it(`supports ${escape(ending)} line ending`, () => { - expect(getLineInfo(inputArray.join(ending), 7)).toEqual({ + expect(getLineInfo(1, inputArray.join(ending), 7)).toEqual({ column: 1, line: 4, }); @@ -30,7 +30,7 @@ describe("getLineInfo", () => { }); it(`supports ${escape("\r\n")} line ending`, () => { - expect(getLineInfo(inputArray.join("\r\n"), 7)).toEqual({ + expect(getLineInfo(1, inputArray.join("\r\n"), 7)).toEqual({ column: 1, line: 3, });