From d557f0e70894c8b93b43a1d063fa1bba608e9a77 Mon Sep 17 00:00:00 2001 From: Miles Date: Tue, 7 May 2024 01:42:37 +0800 Subject: [PATCH] Fix bug in findLoc function --- packages/eslint-plugin/src/utils/find-loc.ts | 32 +++++++++++--------- 1 file changed, 17 insertions(+), 15 deletions(-) diff --git a/packages/eslint-plugin/src/utils/find-loc.ts b/packages/eslint-plugin/src/utils/find-loc.ts index 7f39c6fa0..945b06aec 100644 --- a/packages/eslint-plugin/src/utils/find-loc.ts +++ b/packages/eslint-plugin/src/utils/find-loc.ts @@ -8,26 +8,28 @@ export default function findLoc(text, lines, startLine, endLine) { for (let i = startLine; i <= endLine; i++) { const sourceCodeLine = lines[i - 1] - - const index = sourceCodeLine.indexOf(targetLines[checkingTargetLine].replace(/\r\n|\n/, '')) + const content = targetLines[checkingTargetLine].replace(/\r\n|\n/, '') + const index = sourceCodeLine.indexOf(content) if (index !== -1) { - if (checkingTargetLine === 0) { - resultStart = { - line: i, - column: index - } - } - if (checkingTargetLine === targetLines.length - 1) { - return { - start: resultStart, - end: { + if (index === 0 || sourceCodeLine[index - 1].match(/^|[\s"'']/)) { + if (checkingTargetLine === 0) { + resultStart = { line: i, - column: index + text.length + column: index + } + } + if (checkingTargetLine === targetLines.length - 1 && (sourceCodeLine[index + content.length - 1].match(/$|[\s"'']/) || sourceCodeLine[index + content.length].match(/[\s"'']/))) { + return { + start: resultStart, + end: { + line: i, + column: index + text.length + } } } + checking = true + checkingTargetLine++ } - checking = true - checkingTargetLine++ } else { if (checking) { checking = false