Skip to content

fix: deduplicate blockESLint rule groups by comment #2139

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 4, 2025
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
156 changes: 151 additions & 5 deletions src/blocks/blockESLint.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -468,10 +468,14 @@ describe("blockESLint", () => {
specifier: "c",
},
],
rules: {
"a/b": "error",
"a/c": ["error", { d: "e" }],
},
rules: [
{
entries: {
"a/b": "error",
"a/c": ["error", { d: "e" }],
},
},
],
settings: {
react: {
version: "detect",
Expand Down Expand Up @@ -596,7 +600,149 @@ describe("blockESLint", () => {
{ ignores: ["generated", "lib", "node_modules", "pnpm-lock.yaml"] },
{ linterOptions: {"reportUnusedDisableDirectives":"error"} },
eslint.configs.recommended,
a.configs.recommended,{ extends: [b.configs.recommended], files: ["**/*.b"], rules: {"b/c":"error","b/d":["error",{"e":"f"}]}, },{ extends: [c.configs.recommended], rules: {"c/d":"error","c/e":["error",{"f":"g"}]}, },{ extends: [tseslint.configs.strictTypeChecked, tseslint.configs.stylisticTypeChecked], files: ["**/*.{js,ts}"], languageOptions: {"parserOptions":{"projectService":{"allowDefaultProject":["*.config.*s"]},"tsconfigRootDir":import.meta.dirname}}, rules: {"a/b":"error","a/c":["error",{"d":"e"}]}, settings: {"react":{"version":"detect"}}, }
a.configs.recommended,{ extends: [b.configs.recommended], files: ["**/*.b"], rules: {"b/c":"error","b/d":["error",{"e":"f"}]}, },{ extends: [c.configs.recommended], rules: {"c/d":"error","c/e":["error",{"f":"g"}]}, },{ extends: [tseslint.configs.strictTypeChecked, tseslint.configs.stylisticTypeChecked], files: ["**/*.{js,ts}"], languageOptions: {"parserOptions":{"projectService":{"allowDefaultProject":["*.config.*s"]},"tsconfigRootDir":import.meta.dirname}}, rules: {"a/b": "error","a/c": ["error",{"d":"e"}],}, settings: {"react":{"version":"detect"}}, }
);",
},
"scripts": [
{
"commands": [
"pnpm lint --fix",
],
"phase": 3,
},
],
}
`);
});

test("with identical addon rules comments", () => {
const creation = testBlock(blockESLint, {
addons: {
rules: [
{
comment: "Duplicated comment",
entries: { a: "error" },
},
{
comment: "Standalone comment",
entries: { b: "error" },
},
{
comment: "Duplicated comment",
entries: { c: "error" },
},
],
},
options: optionsBase,
});

expect(creation).toMatchInlineSnapshot(`
{
"addons": [
{
"addons": {
"sections": {
"Linting": {
"contents": {
"after": [
"
For example, ESLint can be run with \`--fix\` to auto-fix some lint rule complaints:

\`\`\`shell
pnpm run lint --fix
\`\`\`
",
],
"before": "
This package includes several forms of linting to enforce consistent code quality and styling.
Each should be shown in VS Code, and can be run manually on the command-line:
",
"items": [
"- \`pnpm lint\` ([ESLint](https://eslint.org) with [typescript-eslint](https://typescript-eslint.io)): Lints JavaScript and TypeScript source files",
],
"plural": "Read the individual documentation for each linter to understand how it can be configured and used best.",
},
},
},
},
"block": [Function],
},
{
"addons": {
"jobs": [
{
"name": "Lint",
"steps": [
{
"run": "pnpm lint",
},
],
},
],
},
"block": [Function],
},
{
"addons": {
"properties": {
"devDependencies": {
"@eslint/js": "9.22.0",
"@types/node": "22.13.10",
"eslint": "9.22.0",
"typescript-eslint": "8.26.1",
},
"scripts": {
"lint": "eslint . --max-warnings 0",
},
},
},
"block": [Function],
},
{
"addons": {
"extensions": [
"dbaeumer.vscode-eslint",
],
"settings": {
"editor.codeActionsOnSave": {
"source.fixAll.eslint": "explicit",
},
"eslint.probe": [
"javascript",
"javascriptreact",
"json",
"jsonc",
"markdown",
"typescript",
"typescriptreact",
"yaml",
],
"eslint.rules.customizations": [
{
"rule": "*",
"severity": "warn",
},
],
},
},
"block": [Function],
},
],
"files": {
"eslint.config.js": "import eslint from "@eslint/js";
import tseslint from "typescript-eslint";

export default tseslint.config(
{ ignores: ["lib", "node_modules", "pnpm-lock.yaml"] },
{ linterOptions: {"reportUnusedDisableDirectives":"error"} },
eslint.configs.recommended,
{ extends: [tseslint.configs.strictTypeChecked, tseslint.configs.stylisticTypeChecked], files: ["**/*.{js,ts}"], languageOptions: {"parserOptions":{"projectService":{"allowDefaultProject":["*.config.*s"]},"tsconfigRootDir":import.meta.dirname}}, rules: {

// Duplicated comment
"a": "error","c": "error",

// Standalone comment
"b": "error",}, }
);",
},
"scripts": [
Expand Down
46 changes: 37 additions & 9 deletions src/blocks/blockESLint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,20 @@ const zRuleOptions = z.union([
]),
]);

const zExtensionRuleGroup = z.object({
comment: z.string().optional(),
entries: z.record(z.string(), zRuleOptions),
});

type ExtensionRuleGroup = z.infer<typeof zExtensionRuleGroup>;

const zExtensionRules = z.union([
z.record(z.string(), zRuleOptions),
z.array(
z.object({
comment: z.string().optional(),
entries: z.record(z.string(), zRuleOptions),
}),
),
z.array(zExtensionRuleGroup),
]);

type ExtensionRules = z.infer<typeof zExtensionRules>;

const zExtension = z.object({
extends: z.array(z.string()).optional(),
files: z.array(z.string()).optional(),
Expand All @@ -48,6 +52,8 @@ const zExtension = z.object({
settings: z.record(z.string(), z.unknown()).optional(),
});

type Extension = z.infer<typeof zExtension>;

const zPackageImport = z.object({
source: z.union([
z.string(),
Expand Down Expand Up @@ -292,7 +298,29 @@ export default tseslint.config(
},
});

function printExtension(extension: z.infer<typeof zExtension>) {
function groupByComment(rulesGroups: ExtensionRuleGroup[]) {
const byComment = new Map<string | undefined, ExtensionRuleGroup>();
const grouped: typeof rulesGroups = [];

for (const group of rulesGroups) {
const existing = byComment.get(group.comment);

if (existing) {
existing.entries = {
...existing.entries,
...group.entries,
};
continue;
} else {
byComment.set(group.comment, group);
grouped.push(group);
}
}

return grouped;
}

function printExtension(extension: Extension) {
return [
"{",
extension.extends && `extends: [${extension.extends.join(", ")}],`,
Expand All @@ -311,14 +339,14 @@ function printExtension(extension: z.infer<typeof zExtension>) {
.join(" ");
}

function printExtensionRules(rules: z.infer<typeof zExtensionRules>) {
function printExtensionRules(rules: ExtensionRules) {
if (!Array.isArray(rules)) {
return JSON.stringify(rules);
}

return [
"{",
...rules.flatMap((group) => [
...groupByComment(rules).flatMap((group) => [
printGroupComment(group.comment),
...Object.entries(group.entries).map(
([ruleName, options]) => `"${ruleName}": ${JSON.stringify(options)},`,
Expand Down