diff --git a/.eslintrc.yml b/.eslintrc.yml index e1a973e370..604690f21c 100644 --- a/.eslintrc.yml +++ b/.eslintrc.yml @@ -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 diff --git a/src/language/location.js b/src/language/location.js index 4695d52e71..b4d4fa4bb6 100644 --- a/src/language/location.js +++ b/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. */ @@ -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 }; }