Skip to content

Commit 9b0514a

Browse files
getLocation: use more explicit matchAll instead of RegExp.exec (#3105)
1 parent 558b0e0 commit 9b0514a

File tree

2 files changed

+12
-7
lines changed

2 files changed

+12
-7
lines changed

.eslintrc.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -577,7 +577,7 @@ overrides:
577577
'@typescript-eslint/prefer-readonly': error
578578
'@typescript-eslint/prefer-readonly-parameter-types': off # TODO consider
579579
'@typescript-eslint/prefer-reduce-type-parameter': error
580-
'@typescript-eslint/prefer-regexp-exec': error
580+
'@typescript-eslint/prefer-regexp-exec': off
581581
'@typescript-eslint/prefer-ts-expect-error': error
582582
'@typescript-eslint/prefer-string-starts-ends-with': off # TODO switch to error after IE11 drop
583583
'@typescript-eslint/promise-function-async': off

src/language/location.js

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import type { Source } from './source';
22

3+
const LineRegExp = /\r\n|[\n\r]/g;
4+
35
/**
46
* Represents a location in a Source.
57
*/
@@ -13,13 +15,16 @@ export type SourceLocation = {
1315
* line and column as a SourceLocation.
1416
*/
1517
export function getLocation(source: Source, position: number): SourceLocation {
16-
const lineRegexp = /\r\n|[\n\r]/g;
18+
let lastLineStart = 0;
1719
let line = 1;
18-
let column = position + 1;
19-
let match;
20-
while ((match = lineRegexp.exec(source.body)) && match.index < position) {
20+
21+
for (const match of source.body.matchAll(LineRegExp)) {
22+
if (match.index >= position) {
23+
break;
24+
}
25+
lastLineStart = match.index + match[0].length;
2126
line += 1;
22-
column = position + 1 - (match.index + match[0].length);
2327
}
24-
return { line, column };
28+
29+
return { line, column: position + 1 - lastLineStart };
2530
}

0 commit comments

Comments
 (0)