Skip to content

Commit

Permalink
Fix some errors that ignore startLine by adding startLine to getLineI…
Browse files Browse the repository at this point in the history
…nfo.

Closes #14123.

Reviewed by @tolmasky.
  • Loading branch information
tolmasky committed Jan 10, 2022
1 parent 6870e7f commit d251559
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 7 deletions.
2 changes: 1 addition & 1 deletion packages/babel-parser/src/parser/error.js
Expand Up @@ -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;
}
Expand Down
8 changes: 6 additions & 2 deletions packages/babel-parser/src/util/location.js
Expand Up @@ -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;
Expand Down
8 changes: 4 additions & 4 deletions packages/babel-parser/test/unit/util/location.skip-bundled.js
Expand Up @@ -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,
});
Expand All @@ -22,15 +22,15 @@ 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,
});
});
});

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,
});
Expand Down

0 comments on commit d251559

Please sign in to comment.