Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions lib/languages/js/source-code/source-code.js
Original file line number Diff line number Diff line change
Expand Up @@ -816,6 +816,12 @@ class SourceCode extends TokenStore {
);
}

if (loc.column < 0) {
throw new RangeError(
`Invalid column number (column ${loc.column} requested).`,
);
}

const lineStartIndex = this.lineStartIndices[loc.line - 1];
const lineEndIndex =
loc.line === this.lineStartIndices.length
Expand Down
15 changes: 15 additions & 0 deletions tests/lib/languages/js/source-code/source-code.js
Original file line number Diff line number Diff line change
Expand Up @@ -2339,6 +2339,21 @@ describe("SourceCode", () => {
});

it("should throw a useful error if `column` is out of range", () => {
assert.throws(
() => sourceCode.getIndexFromLoc({ line: 1, column: -1 }),
"Invalid column number (column -1 requested).",
);

assert.throws(
() => sourceCode.getIndexFromLoc({ line: 1, column: -5 }),
"Invalid column number (column -5 requested).",
);

assert.throws(
() => sourceCode.getIndexFromLoc({ line: 3, column: -1 }),
"Invalid column number (column -1 requested).",
);

assert.throws(
() => sourceCode.getIndexFromLoc({ line: 3, column: 4 }),
/Column number out of range \(column 4 requested, but the length of line 3 is 4\)\./u,
Expand Down
Loading