Skip to content

Commit

Permalink
fix: client-side scripts not being able to linting in flat configs (#334
Browse files Browse the repository at this point in the history
)

* fix: client-side scripts not being able to linting in flat configs

* Create moody-pumpkins-carry.md
  • Loading branch information
ota-meshi committed Mar 25, 2024
1 parent cc3c7f8 commit a84fe26
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 6 deletions.
5 changes: 5 additions & 0 deletions .changeset/moody-pumpkins-carry.md
@@ -0,0 +1,5 @@
---
"eslint-plugin-astro": patch
---

fix: client-side scripts not being able to linting in flat configs
8 changes: 7 additions & 1 deletion src/configs/flat/base.ts
Expand Up @@ -4,7 +4,10 @@
import globals from "globals"
import type { ESLint } from "eslint"
import * as parser from "astro-eslint-parser"
import { tsESLintParser } from "../has-typescript-eslint-parser"
import {
tsESLintParser,
hasTypescriptEslintParser,
} from "../has-typescript-eslint-parser"
import { environments } from "../../environments/index"
export default [
{
Expand Down Expand Up @@ -34,6 +37,9 @@ export default [
// eslint-plugin-astro rules
// Enable base rules
},
processor: hasTypescriptEslintParser
? "astro/client-side-ts"
: "astro/astro",
},
{
// Define the configuration for `<script>` tag.
Expand Down
71 changes: 71 additions & 0 deletions tests/src/integration/client-javascript.ts
@@ -0,0 +1,71 @@
import { ESLint } from "eslint"
import astroPlugin from "../../../src/index"
import assert from "assert"
import Module from "module"
import semver from "semver"

describe("Integration test for client-side script", () => {
// @ts-expect-error -- ignore
const originalLoad = Module._load
before(() => {
// @ts-expect-error -- ignore
Module._load = function (name, ...args) {
if (name === "eslint-plugin-astro") {
return astroPlugin
}
return originalLoad(name, ...args)
}
})
after(() => {
// @ts-expect-error -- ignore
Module._load = originalLoad
})
it("should work with astro processor", async () => {
const eslint = semver.lt(ESLint.version, "9.0.0-0")
? new ESLint({
plugins: {
astro: astroPlugin as any,
},
useEslintrc: false,
overrideConfig: {
extends: ["plugin:astro/base"],
rules: {
"no-restricted-syntax": ["error", "Identifier[name='id']"],
} as Record<string, any>,
},
})
: new ESLint({
overrideConfigFile: true as any,
// @ts-expect-error -- typing bug
overrideConfig: [
...astroPlugin.configs["flat/base"],
{
rules: {
"no-restricted-syntax": ["error", "Identifier[name='id']"],
} as Record<string, any>,
},
],
})

const result = await eslint.lintText(
`
<script>
let id
</script>
`,
{ filePath: "path/to/test.astro" },
)

assert.deepStrictEqual(
result
.flatMap((r) => r.messages)
.map((m) => ({ ruleId: m.ruleId, message: m.message })),
[
{
message: "Using 'Identifier[name='id']' is not allowed.",
ruleId: "no-restricted-syntax",
},
],
)
})
})
6 changes: 2 additions & 4 deletions tests/src/integration/client-typescript.ts
Expand Up @@ -60,10 +60,8 @@ describe("Integration test for client-side ts", () => {
rules: {
"no-restricted-syntax": ["error", "TSTypeAnnotation"],
} as Record<string, any>,
},
{
files: ["*.astro", "**/*.astro"],
processor: "astro/client-side-ts",
// Auto detect the processor
// processor: "astro/client-side-ts",
},
],
})
Expand Down
3 changes: 2 additions & 1 deletion tools/update-rulesets.ts
Expand Up @@ -103,7 +103,7 @@ void formatAndSave(
import globals from "globals"
import type { ESLint } from "eslint"
import * as parser from "astro-eslint-parser"
import { tsESLintParser } from "../has-typescript-eslint-parser"
import { tsESLintParser, hasTypescriptEslintParser } from "../has-typescript-eslint-parser"
import { environments } from "../../environments/index"
export default [
{
Expand Down Expand Up @@ -139,6 +139,7 @@ export default [
})
.join(",\n ")}
},
processor: hasTypescriptEslintParser ? 'astro/client-side-ts' : 'astro/astro'
},
{
// Define the configuration for \`<script>\` tag.
Expand Down

0 comments on commit a84fe26

Please sign in to comment.