From d3b971f5e5d8be94a7e12de9580b35a5b53862e8 Mon Sep 17 00:00:00 2001 From: yosuke ota Date: Sat, 24 Jun 2023 17:52:28 +0900 Subject: [PATCH 1/2] fix: wrong report position in client script with empty lines --- src/shared/client-script/index.ts | 25 +++++-- tests/src/integration/linebreak-style.ts | 91 ++++++++++++++++++++++++ tests/src/integration/no-undef.ts | 44 ++++++++++++ 3 files changed, 155 insertions(+), 5 deletions(-) create mode 100644 tests/src/integration/linebreak-style.ts diff --git a/src/shared/client-script/index.ts b/src/shared/client-script/index.ts index 962c3f9c..0c64c9fb 100644 --- a/src/shared/client-script/index.ts +++ b/src/shared/client-script/index.ts @@ -15,7 +15,7 @@ class Locs { const lineStartIndices = [0] let index = 0 - for (const line of lines) { + for (const line of lines[lines.length - 1] ? lines : lines.slice(0, -1)) { index += line.length lineStartIndices.push(index) } @@ -100,9 +100,19 @@ export class ClientScript { for (let index = 0; index < lines.length; index++) { const line = lines[index] const lineIndent = Math.min(indent, line.length) - textLines.push(line.slice(lineIndent)) + const lineText = line.slice(lineIndent) + if (lineText) { + textLines.push(line.slice(lineIndent)) + remapColumnOffsets.push(lineIndent) + } else if (line.endsWith("\n")) { + const eol = line.endsWith("\r\n") ? "\r\n" : "\n" + textLines.push(eol) + remapColumnOffsets.push(line.length - eol.length) + } else { + textLines.push("") + remapColumnOffsets.push(lineIndent) + } remapLines.push(startLoc.line + index + 1) - remapColumnOffsets.push(lineIndent) } const text = textLines.join("") @@ -135,7 +145,11 @@ export class ClientScript { /** Remap range */ const remapRange = (range: [number, number]): [number, number] | null => { const startLoc = textLocs.getLocFromIndex(range[0]) - const endLoc = textLocs.getLocFromIndex(range[1]) + const normalEndLoc = textLocs.getLocFromIndex(range[1]) + const endLoc = + normalEndLoc.column > 0 + ? normalEndLoc + : textLocs.getLocFromIndex(range[1] - 1) const remappedStartLoc = remapLoc(startLoc) const remappedEndLoc = remapLoc(endLoc) if (remappedStartLoc.line < 0 || remappedEndLoc.line < 0) { @@ -143,7 +157,8 @@ export class ClientScript { } return [ this.parsed.getIndexFromLoc(remappedStartLoc), - this.parsed.getIndexFromLoc(remappedEndLoc), + this.parsed.getIndexFromLoc(remappedEndLoc) + + (normalEndLoc.column > 0 ? 0 : 1), ] } diff --git a/tests/src/integration/linebreak-style.ts b/tests/src/integration/linebreak-style.ts new file mode 100644 index 00000000..6ef84b5a --- /dev/null +++ b/tests/src/integration/linebreak-style.ts @@ -0,0 +1,91 @@ +import { RuleTester, Linter } from "eslint" +import { processor } from "../../../src/processor" + +describe("Integration test for linebreak-style", () => { + const ruleNoUnusedVars = new Linter().getRules().get("linebreak-style")! + const tester = new RuleTester({ + parser: require.resolve("./auto-parser"), + parserOptions: { + ecmaVersion: 2020, + sourceType: "module", + }, + globals: { + console: false, + }, + }) + tester.run("linebreak-style", ruleNoUnusedVars, { + valid: [ + { + // @ts-expect-error -- fine name with processor + filename: { + filename: "foo.astro", + ...processor, + }, + code: ` + + `, + }, + { + // @ts-expect-error -- fine name with processor + filename: { + filename: "foo.astro", + ...processor, + }, + code: ` + + `, + }, + ], + invalid: [ + { + // @ts-expect-error -- fine name with processor + filename: { + filename: "foo.astro", + ...processor, + }, + code: `{/*eslint linebreak-style:0*/} + + `, + output: `{/*eslint linebreak-style:0*/} + + `, + errors: 1, + }, + { + // @ts-expect-error -- fine name with processor + filename: { + filename: "foo.astro", + ...processor, + }, + code: `{/*eslint linebreak-style:0*/} + + `, + output: `{/*eslint linebreak-style:0*/} + + `, + errors: 4, + }, + ], + }) +}) diff --git a/tests/src/integration/no-undef.ts b/tests/src/integration/no-undef.ts index 393da933..9cfc595d 100644 --- a/tests/src/integration/no-undef.ts +++ b/tests/src/integration/no-undef.ts @@ -48,6 +48,50 @@ describe("Integration test for no-undef", () => { }, ], }, + { + // @ts-expect-error -- fine name with processor + filename: { + filename: "foo.astro", + ...processor, + }, + code: ` + + `, + errors: [ + { + message: "'foo' is not defined.", + line: 6, + column: 15, + }, + ], + }, + { + // @ts-expect-error -- fine name with processor + filename: { + filename: "foo.astro", + ...processor, + }, + code: ` + + `.replace(/\n/gu, "\r\n"), + errors: [ + { + message: "'foo' is not defined.", + line: 6, + column: 15, + }, + ], + }, ], }) }) From 7c308e721dd7a2d58d1f4c3704669430923c3734 Mon Sep 17 00:00:00 2001 From: Yosuke Ota Date: Sat, 24 Jun 2023 17:53:46 +0900 Subject: [PATCH 2/2] Create forty-files-add.md --- .changeset/forty-files-add.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/forty-files-add.md diff --git a/.changeset/forty-files-add.md b/.changeset/forty-files-add.md new file mode 100644 index 00000000..cfc195e3 --- /dev/null +++ b/.changeset/forty-files-add.md @@ -0,0 +1,5 @@ +--- +"eslint-plugin-astro": patch +--- + +fix: wrong report position in client script with empty lines