Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions release-files.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
23 changes: 22 additions & 1 deletion src/planner/check-narrowing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Record<string, readonly string[]>> = { 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<string>();
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"
Expand Down Expand Up @@ -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));
Expand Down
35 changes: 35 additions & 0 deletions tests/planner/check-narrowing-extensions.test.ts
Original file line number Diff line number Diff line change
@@ -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<string, string>, 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"]);
});
});
Loading