Skip to content

Commit

Permalink
getLocation: use more explicit matchAll instead of RegExp.exec (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
IvanGoncharov committed May 16, 2021
1 parent 558b0e0 commit 9b0514a
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 7 deletions.
2 changes: 1 addition & 1 deletion .eslintrc.yml
Expand Up @@ -577,7 +577,7 @@ overrides:
'@typescript-eslint/prefer-readonly': error
'@typescript-eslint/prefer-readonly-parameter-types': off # TODO consider
'@typescript-eslint/prefer-reduce-type-parameter': error
'@typescript-eslint/prefer-regexp-exec': error
'@typescript-eslint/prefer-regexp-exec': off
'@typescript-eslint/prefer-ts-expect-error': error
'@typescript-eslint/prefer-string-starts-ends-with': off # TODO switch to error after IE11 drop
'@typescript-eslint/promise-function-async': off
Expand Down
17 changes: 11 additions & 6 deletions src/language/location.js
@@ -1,5 +1,7 @@
import type { Source } from './source';

const LineRegExp = /\r\n|[\n\r]/g;

/**
* Represents a location in a Source.
*/
Expand All @@ -13,13 +15,16 @@ export type SourceLocation = {
* line and column as a SourceLocation.
*/
export function getLocation(source: Source, position: number): SourceLocation {
const lineRegexp = /\r\n|[\n\r]/g;
let lastLineStart = 0;
let line = 1;
let column = position + 1;
let match;
while ((match = lineRegexp.exec(source.body)) && match.index < position) {

for (const match of source.body.matchAll(LineRegExp)) {
if (match.index >= position) {
break;
}
lastLineStart = match.index + match[0].length;
line += 1;
column = position + 1 - (match.index + match[0].length);
}
return { line, column };

return { line, column: position + 1 - lastLineStart };
}

0 comments on commit 9b0514a

Please sign in to comment.