Skip to content

Commit b8a7e7a

Browse files
authored
feat: throw error when column is negative in getIndexFromLoc (#19831)
* fix: handle negative column index in `getIndexFromLoc` and add tests * wip: address comments * wip: add more test cases
1 parent 7ef4cf7 commit b8a7e7a

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

lib/languages/js/source-code/source-code.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -816,6 +816,12 @@ class SourceCode extends TokenStore {
816816
);
817817
}
818818

819+
if (loc.column < 0) {
820+
throw new RangeError(
821+
`Invalid column number (column ${loc.column} requested).`,
822+
);
823+
}
824+
819825
const lineStartIndex = this.lineStartIndices[loc.line - 1];
820826
const lineEndIndex =
821827
loc.line === this.lineStartIndices.length

tests/lib/languages/js/source-code/source-code.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2339,6 +2339,21 @@ describe("SourceCode", () => {
23392339
});
23402340

23412341
it("should throw a useful error if `column` is out of range", () => {
2342+
assert.throws(
2343+
() => sourceCode.getIndexFromLoc({ line: 1, column: -1 }),
2344+
"Invalid column number (column -1 requested).",
2345+
);
2346+
2347+
assert.throws(
2348+
() => sourceCode.getIndexFromLoc({ line: 1, column: -5 }),
2349+
"Invalid column number (column -5 requested).",
2350+
);
2351+
2352+
assert.throws(
2353+
() => sourceCode.getIndexFromLoc({ line: 3, column: -1 }),
2354+
"Invalid column number (column -1 requested).",
2355+
);
2356+
23422357
assert.throws(
23432358
() => sourceCode.getIndexFromLoc({ line: 3, column: 4 }),
23442359
/Column number out of range \(column 4 requested, but the length of line 3 is 4\)\./u,

0 commit comments

Comments
 (0)