diff --git a/release-files.json b/release-files.json index 685f2f19..62f3db93 100644 --- a/release-files.json +++ b/release-files.json @@ -232,6 +232,7 @@ "tests/git/git-diff.temp-repo.test.ts", "tests/measurement.test.ts", "tests/planner/path-baseline.test.ts", + "tests/planner/check-narrowing-extensions.test.ts", "tests/planner/check-narrowing.test.ts", "tests/planner/pipeline-steps.test.ts", "tests/planner/repo-agnostic-engine.test.ts", diff --git a/src/planner/check-narrowing.ts b/src/planner/check-narrowing.ts index 3b015fbf..6f0153c7 100644 --- a/src/planner/check-narrowing.ts +++ b/src/planner/check-narrowing.ts @@ -161,6 +161,26 @@ function covers(target: string, path: string, extensions: readonly string[]): bo return inside && extensions.includes(posix.extname(path).slice(1)); } +/** Other names configuration uses for a file type: `@eslint/markdown` lints `.md`, `eslint-plugin-yml` `.yaml`. */ +const EXTENSION_ALIASES: Readonly> = { md: ["markdown"], yml: ["yaml"], yaml: ["yml"], jsonc: ["json"], json5: ["json"] }; + +/** + * Extensions of changed files that the ESLint configuration names (2026-11-17). A directory target lints + * every file a flat config's `files` (or an eslintrc `overrides[].files`) reaches, not only JavaScript and + * TypeScript: `...vue.configs["flat/recommended"]` makes `eslint src` lint `src/App.vue`. A changed file's + * extension that appears in the configuration text, or a plugin named for it, therefore counts as linted. + */ +function configuredExtensions(changes: readonly ChangedPath[], texts: readonly string[]): string[] { + const found = new Set(); + for (const { path } of changes) { + const extension = posix.extname(path).slice(1).toLowerCase(); + if (!/^\w+$/.test(extension) || ESLINT_EXTENSIONS.includes(extension) || found.has(extension)) continue; + const names = [extension, ...(EXTENSION_ALIASES[extension] ?? [])]; + if (texts.some((text) => names.some((name) => new RegExp(`\\b${name}\\b`, "i").test(text)))) found.add(extension); + } + return [...found]; +} + /** Root configuration files of the tool that exist, and the paths each one names relatively. */ function configReferences(tool: Tool, input: NarrowingInput, explicit: string | undefined): { texts: string[]; referenced: string[] } { const candidates = explicit ? [posix.normalize(explicit)] : tool.name === "eslint" @@ -224,7 +244,8 @@ function decide(check: GateCheck, input: NarrowingInput, cache: RootTextCache): const extensions = tool.name === "eslint" && values.has("--ext") ? [...ESLINT_EXTENSIONS, ...values.get("--ext")!.split(",").map((ext) => ext.replace(/^\./, ""))] - : tool.extensions; + : tool.name === "eslint" ? [...ESLINT_EXTENSIONS, ...configuredExtensions(input.changes, texts)] + : tool.extensions; if (tool.name === "eslint") { const nested = input.files?.find((path) => path.includes("/") && ESLINT_CONFIG.test(posix.basename(path)) && !posix.basename(path).endsWith("ignore")); const importRules = texts.some((text) => IMPORT_RULES.test(text)); diff --git a/tests/planner/check-narrowing-extensions.test.ts b/tests/planner/check-narrowing-extensions.test.ts new file mode 100644 index 00000000..f5f7573f --- /dev/null +++ b/tests/planner/check-narrowing-extensions.test.ts @@ -0,0 +1,35 @@ +// SPDX-License-Identifier: MIT +import { strict as assert } from "node:assert"; +import { describe, it } from "node:test"; +import { narrowGateChecks } from "../../src/planner/check-narrowing.js"; +import { discoverGateChecks } from "../../src/planner/pipeline-steps.js"; + +function decide(config: Record, changed: string, lint = "eslint src") { + const scripts = { lint }; + const [check] = narrowGateChecks(discoverGateChecks(scripts).checks, { + scripts, + changes: [{ path: changed, status: "modified" }], + read: (path) => config[path], + }); + return [check!.decision, check!.command]; +} + +const VUE = 'import js from "@eslint/js";\nimport vue from "eslint-plugin-vue";\nexport default [js.configs.recommended, ...vue.configs["flat/recommended"]];\n'; + +describe("ESLint directory targets lint the file types their configuration adds", () => { + it("does not skip `eslint src` when a .vue file changed and the config uses eslint-plugin-vue", () => { + assert.deepEqual(decide({ "eslint.config.js": VUE }, "src/App.vue"), ["NARROWED", "eslint src/App.vue"]); + }); + + it("counts a type the config names by an alias, or in an eslintrc override", () => { + const markdown = 'import markdown from "@eslint/markdown";\nexport default [...markdown.configs.recommended];'; + assert.deepEqual(decide({ "eslint.config.mjs": markdown }, "src/guide.md"), ["NARROWED", "eslint src/guide.md"]); + const svelte = '{ "overrides": [{ "files": ["*.svelte"], "parser": "svelte-eslint-parser" }] }'; + assert.deepEqual(decide({ ".eslintrc.json": svelte }, "src/App.svelte"), ["NARROWED", "eslint src/App.svelte"]); + }); + + it("still skips types the configuration never mentions", () => { + assert.deepEqual(decide({ "eslint.config.js": VUE }, "src/style.css"), ["SKIP", "eslint src"]); + assert.deepEqual(decide({ "eslint.config.js": VUE }, "docs/App.vue"), ["SKIP", "eslint src"]); + }); +});