GeezerVison is showing github.com — feat: throw error when column is negative in `getIndexFromLoc` (#19831) · eslint/eslint@b8a7e7a
File tree
Expand file treeCollapse file tree
Open diff view settings
lib/languages/js/source-code tests/lib/languages/js/source-code
Expand file treeCollapse file tree
Open diff view settings
Collapse file
Copy file name to clipboardExpand all lines: lib/languages/js/source-code/source-code.js +6Lines changed: 6 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -816,6 +816,12 @@ class SourceCode extends TokenStore { |
816 | 816 | ); |
817 | 817 | } |
818 | 818 |
|
| 819 | + if (loc.column < 0) { |
| 820 | + throw new RangeError( |
| 821 | + `Invalid column number (column ${loc.column} requested).`, |
| 822 | + ); |
| 823 | + } |
| 824 | + |
819 | 825 | const lineStartIndex = this.lineStartIndices[loc.line - 1]; |
820 | 826 | const lineEndIndex = |
821 | 827 | loc.line === this.lineStartIndices.length |
|
Collapse file
Copy file name to clipboardExpand all lines: tests/lib/languages/js/source-code/source-code.js +15Lines changed: 15 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -2339,6 +2339,21 @@ describe("SourceCode", () => { |
2339 | 2339 | }); |
2340 | 2340 |
|
2341 | 2341 | 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 | + |
2342 | 2357 | assert.throws( |
2343 | 2358 | () => sourceCode.getIndexFromLoc({ line: 3, column: 4 }), |
2344 | 2359 | /Column number out of range \(column 4 requested, but the length of line 3 is 4\)\./u, |
|