Skip to content

Commit

Permalink
fix: wrong report position in client script with empty lines (#220)
Browse files Browse the repository at this point in the history
* fix: wrong report position in client script with empty lines

* Create forty-files-add.md
  • Loading branch information
ota-meshi authored Jun 24, 2023
1 parent 77ebb8d commit a87b659
Show file tree
Hide file tree
Showing 4 changed files with 160 additions and 5 deletions.
5 changes: 5 additions & 0 deletions .changeset/forty-files-add.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"eslint-plugin-astro": patch
---

fix: wrong report position in client script with empty lines
25 changes: 20 additions & 5 deletions src/shared/client-script/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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("")
Expand Down Expand Up @@ -135,15 +145,20 @@ 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) {
return null
}
return [
this.parsed.getIndexFromLoc(remappedStartLoc),
this.parsed.getIndexFromLoc(remappedEndLoc),
this.parsed.getIndexFromLoc(remappedEndLoc) +
(normalEndLoc.column > 0 ? 0 : 1),
]
}

Expand Down
91 changes: 91 additions & 0 deletions tests/src/integration/linebreak-style.ts
Original file line number Diff line number Diff line change
@@ -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: `
<script define:vars={{ foo: 42, bar: 42 }}>
console.log(foo)
</script>
`,
},
{
// @ts-expect-error -- fine name with processor
filename: {
filename: "foo.astro",
...processor,
},
code: `
<script define:vars={{ bar: 42 }}>
// empty lines
console.log(foo)
</script>
`,
},
],
invalid: [
{
// @ts-expect-error -- fine name with processor
filename: {
filename: "foo.astro",
...processor,
},
code: `{/*eslint linebreak-style:0*/}
<script define:vars={{ bar: 42 }}>
console.log(foo)\r
</script>
`,
output: `{/*eslint linebreak-style:0*/}
<script define:vars={{ bar: 42 }}>
console.log(foo)
</script>
`,
errors: 1,
},
{
// @ts-expect-error -- fine name with processor
filename: {
filename: "foo.astro",
...processor,
},
code: `{/*eslint linebreak-style:0*/}
<script define:vars={{ bar: 42 }}>\r
\r
\r
// empty lines\r
console.log(foo)\r
</script>
`,
output: `{/*eslint linebreak-style:0*/}
<script define:vars={{ bar: 42 }}>\r
\r
// empty lines
console.log(foo)
</script>
`,
errors: 4,
},
],
})
})
44 changes: 44 additions & 0 deletions tests/src/integration/no-undef.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,50 @@ describe("Integration test for no-undef", () => {
},
],
},
{
// @ts-expect-error -- fine name with processor
filename: {
filename: "foo.astro",
...processor,
},
code: `
<script define:vars={{ bar: 42 }}>
// empty lines
console.log(foo)
</script>
`,
errors: [
{
message: "'foo' is not defined.",
line: 6,
column: 15,
},
],
},
{
// @ts-expect-error -- fine name with processor
filename: {
filename: "foo.astro",
...processor,
},
code: `
<script define:vars={{ bar: 42 }}>
// empty lines
console.log(foo)
</script>
`.replace(/\n/gu, "\r\n"),
errors: [
{
message: "'foo' is not defined.",
line: 6,
column: 15,
},
],
},
],
})
})

0 comments on commit a87b659

Please sign in to comment.