Skip to content

Commit a84fe26

Browse files
authored
fix: client-side scripts not being able to linting in flat configs (#334)
* fix: client-side scripts not being able to linting in flat configs * Create moody-pumpkins-carry.md
1 parent cc3c7f8 commit a84fe26

File tree

5 files changed

+87
-6
lines changed

5 files changed

+87
-6
lines changed

.changeset/moody-pumpkins-carry.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"eslint-plugin-astro": patch
3+
---
4+
5+
fix: client-side scripts not being able to linting in flat configs

src/configs/flat/base.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@
44
import globals from "globals"
55
import type { ESLint } from "eslint"
66
import * as parser from "astro-eslint-parser"
7-
import { tsESLintParser } from "../has-typescript-eslint-parser"
7+
import {
8+
tsESLintParser,
9+
hasTypescriptEslintParser,
10+
} from "../has-typescript-eslint-parser"
811
import { environments } from "../../environments/index"
912
export default [
1013
{
@@ -34,6 +37,9 @@ export default [
3437
// eslint-plugin-astro rules
3538
// Enable base rules
3639
},
40+
processor: hasTypescriptEslintParser
41+
? "astro/client-side-ts"
42+
: "astro/astro",
3743
},
3844
{
3945
// Define the configuration for `<script>` tag.
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
import { ESLint } from "eslint"
2+
import astroPlugin from "../../../src/index"
3+
import assert from "assert"
4+
import Module from "module"
5+
import semver from "semver"
6+
7+
describe("Integration test for client-side script", () => {
8+
// @ts-expect-error -- ignore
9+
const originalLoad = Module._load
10+
before(() => {
11+
// @ts-expect-error -- ignore
12+
Module._load = function (name, ...args) {
13+
if (name === "eslint-plugin-astro") {
14+
return astroPlugin
15+
}
16+
return originalLoad(name, ...args)
17+
}
18+
})
19+
after(() => {
20+
// @ts-expect-error -- ignore
21+
Module._load = originalLoad
22+
})
23+
it("should work with astro processor", async () => {
24+
const eslint = semver.lt(ESLint.version, "9.0.0-0")
25+
? new ESLint({
26+
plugins: {
27+
astro: astroPlugin as any,
28+
},
29+
useEslintrc: false,
30+
overrideConfig: {
31+
extends: ["plugin:astro/base"],
32+
rules: {
33+
"no-restricted-syntax": ["error", "Identifier[name='id']"],
34+
} as Record<string, any>,
35+
},
36+
})
37+
: new ESLint({
38+
overrideConfigFile: true as any,
39+
// @ts-expect-error -- typing bug
40+
overrideConfig: [
41+
...astroPlugin.configs["flat/base"],
42+
{
43+
rules: {
44+
"no-restricted-syntax": ["error", "Identifier[name='id']"],
45+
} as Record<string, any>,
46+
},
47+
],
48+
})
49+
50+
const result = await eslint.lintText(
51+
`
52+
<script>
53+
let id
54+
</script>
55+
`,
56+
{ filePath: "path/to/test.astro" },
57+
)
58+
59+
assert.deepStrictEqual(
60+
result
61+
.flatMap((r) => r.messages)
62+
.map((m) => ({ ruleId: m.ruleId, message: m.message })),
63+
[
64+
{
65+
message: "Using 'Identifier[name='id']' is not allowed.",
66+
ruleId: "no-restricted-syntax",
67+
},
68+
],
69+
)
70+
})
71+
})

tests/src/integration/client-typescript.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,8 @@ describe("Integration test for client-side ts", () => {
6060
rules: {
6161
"no-restricted-syntax": ["error", "TSTypeAnnotation"],
6262
} as Record<string, any>,
63-
},
64-
{
65-
files: ["*.astro", "**/*.astro"],
66-
processor: "astro/client-side-ts",
63+
// Auto detect the processor
64+
// processor: "astro/client-side-ts",
6765
},
6866
],
6967
})

tools/update-rulesets.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ void formatAndSave(
103103
import globals from "globals"
104104
import type { ESLint } from "eslint"
105105
import * as parser from "astro-eslint-parser"
106-
import { tsESLintParser } from "../has-typescript-eslint-parser"
106+
import { tsESLintParser, hasTypescriptEslintParser } from "../has-typescript-eslint-parser"
107107
import { environments } from "../../environments/index"
108108
export default [
109109
{
@@ -139,6 +139,7 @@ export default [
139139
})
140140
.join(",\n ")}
141141
},
142+
processor: hasTypescriptEslintParser ? 'astro/client-side-ts' : 'astro/astro'
142143
},
143144
{
144145
// Define the configuration for \`<script>\` tag.

0 commit comments

Comments
 (0)