From 6dcd65964eae4b8ebebf99586b2bd6e09b08cc9c Mon Sep 17 00:00:00 2001 From: FoxeyeRinx Date: Fri, 25 Oct 2024 14:07:58 +0700 Subject: [PATCH] chore: unify `load-rules.ts` and `utils/rules.ts` to `rules/index.ts` - The `load-rules.ts` and `utils/rules.ts` are working like an index, using an index file in `src/rules/` allows us to remove them - The `update-rules.ts` is the generator of `utils/rules.ts` so it can be removed too --- .../src/components/eslint/scripts/linter.mts | 2 +- docs-build/src/config.ts | 2 +- src/configs/all.ts | 2 +- src/configs/flat/all.ts | 2 +- src/plugin.ts | 2 +- src/rules/index.ts | 39 +++++++++++++++++ src/utils/rules.ts | 43 ------------------- tests/src/rules/index.test.ts | 39 +++++++++++++++++ tests/src/rules/jsx-a11y/aria-proptypes.ts | 2 +- .../src/rules/jsx-a11y/heading-has-content.ts | 2 +- .../jsx-a11y/label-has-associated-control.ts | 2 +- tests/src/rules/jsx-a11y/no-autofocus.ts | 2 +- tools/lib/load-rules.ts | 21 --------- tools/render-rules-table.ts | 2 +- tools/update-docs.ts | 2 +- tools/update-rules.ts | 37 ---------------- tools/update-rulesets.ts | 2 +- tools/update.ts | 1 - 18 files changed, 90 insertions(+), 114 deletions(-) create mode 100644 src/rules/index.ts delete mode 100644 src/utils/rules.ts create mode 100644 tests/src/rules/index.test.ts delete mode 100644 tools/lib/load-rules.ts delete mode 100644 tools/update-rules.ts diff --git a/docs-build/src/components/eslint/scripts/linter.mts b/docs-build/src/components/eslint/scripts/linter.mts index c28d2b6e..88bed926 100644 --- a/docs-build/src/components/eslint/scripts/linter.mts +++ b/docs-build/src/components/eslint/scripts/linter.mts @@ -1,4 +1,4 @@ -import { rules as pluginRules } from "../../../../../src/utils/rules.js" +import { rules as pluginRules } from "../../../../../src/rules" import type { Linter, Rule } from "eslint" import { builtinRules } from "eslint/use-at-your-own-risk" import * as astroEslintParser from "astro-eslint-parser" diff --git a/docs-build/src/config.ts b/docs-build/src/config.ts index f2987638..3ba81361 100644 --- a/docs-build/src/config.ts +++ b/docs-build/src/config.ts @@ -1,4 +1,4 @@ -import { rules } from "../../src/utils/rules" +import { rules } from "../../src/rules" const categories = [ "Possible Errors", diff --git a/src/configs/all.ts b/src/configs/all.ts index 0d1c76b9..100cd33b 100644 --- a/src/configs/all.ts +++ b/src/configs/all.ts @@ -1,5 +1,5 @@ import recommended from "./recommended" -import { rules } from "../utils/rules" +import { rules } from "../rules" const all: Record = {} for (const rule of rules.filter( diff --git a/src/configs/flat/all.ts b/src/configs/flat/all.ts index 2d02a96c..ae21f96c 100644 --- a/src/configs/flat/all.ts +++ b/src/configs/flat/all.ts @@ -1,5 +1,5 @@ import recommended from "./recommended" -import { rules } from "../../utils/rules" +import { rules } from "../../rules" const all: Record = {} for (const rule of rules.filter( diff --git a/src/plugin.ts b/src/plugin.ts index bad12849..da545726 100644 --- a/src/plugin.ts +++ b/src/plugin.ts @@ -1,4 +1,4 @@ -import { rules as ruleList } from "./utils/rules" +import { rules as ruleList } from "./rules" import * as processorsDefines from "./processor" import type { Rule } from "eslint" import { name, version } from "./meta" diff --git a/src/rules/index.ts b/src/rules/index.ts new file mode 100644 index 00000000..b04f6ebd --- /dev/null +++ b/src/rules/index.ts @@ -0,0 +1,39 @@ +import missingClientOnlyDirectiveValue from "./missing-client-only-directive-value" +import noConflictSetDirectives from "./no-conflict-set-directives" +import noDeprecatedAstroCanonicalUrl from "./no-deprecated-astro-canonicalurl" +import noDeprecatedAstroFetchContent from "./no-deprecated-astro-fetchcontent" +import noDeprecatedAstroResolve from "./no-deprecated-astro-resolve" +import noDeprecatedGetEntryBySlug from "./no-deprecated-getentrybyslug" +import noExportsFromComponents from "./no-exports-from-components" +import noSetHtmlDirective from "./no-set-html-directive" +import noSetTextDirective from "./no-set-text-directive" +import noUnusedCssSelector from "./no-unused-css-selector" +import noUnusedDefineVarsInStyle from "./no-unused-define-vars-in-style" +import preferClassListDirective from "./prefer-class-list-directive" +import preferObjectClassList from "./prefer-object-class-list" +import preferSplitClassList from "./prefer-split-class-list" +import semi from "./semi" +import sortAttributes from "./sort-attributes" +import validCompile from "./valid-compile" +import { buildA11yRules } from "../a11y" + +export const originalRules = [ + missingClientOnlyDirectiveValue, + noConflictSetDirectives, + noDeprecatedAstroCanonicalUrl, + noDeprecatedAstroFetchContent, + noDeprecatedAstroResolve, + noDeprecatedGetEntryBySlug, + noExportsFromComponents, + noSetHtmlDirective, + noSetTextDirective, + noUnusedCssSelector, + noUnusedDefineVarsInStyle, + preferClassListDirective, + preferObjectClassList, + preferSplitClassList, + semi, + sortAttributes, + validCompile, +] +export const rules = [...originalRules, ...buildA11yRules()] diff --git a/src/utils/rules.ts b/src/utils/rules.ts deleted file mode 100644 index f780fdd9..00000000 --- a/src/utils/rules.ts +++ /dev/null @@ -1,43 +0,0 @@ -// IMPORTANT! -// This file has been automatically generated, -// in order to update its content execute "npm run update" -import type { RuleModule } from "../types" -import missingClientOnlyDirectiveValue from "../rules/missing-client-only-directive-value" -import noConflictSetDirectives from "../rules/no-conflict-set-directives" -import noDeprecatedAstroCanonicalurl from "../rules/no-deprecated-astro-canonicalurl" -import noDeprecatedAstroFetchcontent from "../rules/no-deprecated-astro-fetchcontent" -import noDeprecatedAstroResolve from "../rules/no-deprecated-astro-resolve" -import noDeprecatedGetentrybyslug from "../rules/no-deprecated-getentrybyslug" -import noExportsFromComponents from "../rules/no-exports-from-components" -import noSetHtmlDirective from "../rules/no-set-html-directive" -import noSetTextDirective from "../rules/no-set-text-directive" -import noUnusedCssSelector from "../rules/no-unused-css-selector" -import noUnusedDefineVarsInStyle from "../rules/no-unused-define-vars-in-style" -import preferClassListDirective from "../rules/prefer-class-list-directive" -import preferObjectClassList from "../rules/prefer-object-class-list" -import preferSplitClassList from "../rules/prefer-split-class-list" -import semi from "../rules/semi" -import sortAttributes from "../rules/sort-attributes" -import validCompile from "../rules/valid-compile" -import { buildA11yRules } from "../a11y" - -export const rules = [ - missingClientOnlyDirectiveValue, - noConflictSetDirectives, - noDeprecatedAstroCanonicalurl, - noDeprecatedAstroFetchcontent, - noDeprecatedAstroResolve, - noDeprecatedGetentrybyslug, - noExportsFromComponents, - noSetHtmlDirective, - noSetTextDirective, - noUnusedCssSelector, - noUnusedDefineVarsInStyle, - preferClassListDirective, - preferObjectClassList, - preferSplitClassList, - semi, - sortAttributes, - validCompile, - ...buildA11yRules(), -] as RuleModule[] diff --git a/tests/src/rules/index.test.ts b/tests/src/rules/index.test.ts new file mode 100644 index 00000000..d7fc4ff0 --- /dev/null +++ b/tests/src/rules/index.test.ts @@ -0,0 +1,39 @@ +import { readdirSync } from "fs" +import { join } from "path" +import assert from "assert" +import { rules } from "../../../src/rules/index" +import { buildA11yRules } from "src/a11y" + +describe("Test src/rules/index.ts", () => { + function getImportedRuleNames() { + return rules.map((rule) => rule.meta.docs.ruleName) + } + + function assertRulesImported(rulesToCheck: string[]) { + const importedRules = getImportedRuleNames() + rulesToCheck.forEach((ruleName) => { + assert( + importedRules.includes(ruleName), + `Imported rules should include ${ruleName}`, + ) + }) + } + + it("should import all rule files", () => { + const rulesDir = join(__dirname, "../../../src/rules") + const ruleFiles = readdirSync(rulesDir) + .filter( + (file) => + file.endsWith(".ts") && + file !== "index.ts" && + !file.endsWith(".test.ts"), + ) + .map((file) => file.replace(".ts", "")) + assertRulesImported(ruleFiles) + }) + + it("should import all a11y rules", () => { + const a11yRules = buildA11yRules().map((rule) => rule.meta.docs.ruleName) + assertRulesImported(a11yRules) + }) +}) diff --git a/tests/src/rules/jsx-a11y/aria-proptypes.ts b/tests/src/rules/jsx-a11y/aria-proptypes.ts index cdb49518..aed222bb 100644 --- a/tests/src/rules/jsx-a11y/aria-proptypes.ts +++ b/tests/src/rules/jsx-a11y/aria-proptypes.ts @@ -1,5 +1,5 @@ import { RuleTester } from "../../../utils/eslint-compat" -import { rules } from "../../../../src/utils/rules" +import { rules } from "../../../../src/rules" import { loadTestCases } from "../../../utils/utils" const rule = rules.find( diff --git a/tests/src/rules/jsx-a11y/heading-has-content.ts b/tests/src/rules/jsx-a11y/heading-has-content.ts index 4806db9f..4c7db6a8 100644 --- a/tests/src/rules/jsx-a11y/heading-has-content.ts +++ b/tests/src/rules/jsx-a11y/heading-has-content.ts @@ -1,5 +1,5 @@ import { RuleTester } from "../../../utils/eslint-compat" -import { rules } from "../../../../src/utils/rules" +import { rules } from "../../../../src/rules" import { loadTestCases } from "../../../utils/utils" const rule = rules.find( diff --git a/tests/src/rules/jsx-a11y/label-has-associated-control.ts b/tests/src/rules/jsx-a11y/label-has-associated-control.ts index bf4ecba5..7eee401d 100644 --- a/tests/src/rules/jsx-a11y/label-has-associated-control.ts +++ b/tests/src/rules/jsx-a11y/label-has-associated-control.ts @@ -1,5 +1,5 @@ import { RuleTester } from "../../../utils/eslint-compat" -import { rules } from "../../../../src/utils/rules" +import { rules } from "../../../../src/rules" import { loadTestCases } from "../../../utils/utils" const rule = rules.find( diff --git a/tests/src/rules/jsx-a11y/no-autofocus.ts b/tests/src/rules/jsx-a11y/no-autofocus.ts index 5dbe6255..bdd54992 100644 --- a/tests/src/rules/jsx-a11y/no-autofocus.ts +++ b/tests/src/rules/jsx-a11y/no-autofocus.ts @@ -1,5 +1,5 @@ import { RuleTester } from "../../../utils/eslint-compat" -import { rules } from "../../../../src/utils/rules" +import { rules } from "../../../../src/rules" import { loadTestCases } from "../../../utils/utils" const rule = rules.find( diff --git a/tools/lib/load-rules.ts b/tools/lib/load-rules.ts deleted file mode 100644 index 590e21e8..00000000 --- a/tools/lib/load-rules.ts +++ /dev/null @@ -1,21 +0,0 @@ -import path from "path" -import fs from "fs" -import { createRequire } from "module" -import type { RuleModule } from "../../src/types" - -const url = import.meta.url -const require = createRequire(url) - -/** - * Import all rules from `src/rules` and return them as an array. - * @returns {RuleModule[]} - */ -function readRules(): RuleModule[] { - const rulesPath = path.resolve(__dirname, "../../src/rules") - return fs - .readdirSync(rulesPath) - .filter((n) => n.endsWith(".ts")) - .map((fileName) => require(path.join(rulesPath, fileName)).default) -} - -export const rules = readRules() diff --git a/tools/render-rules-table.ts b/tools/render-rules-table.ts index 8e45b360..75d93707 100644 --- a/tools/render-rules-table.ts +++ b/tools/render-rules-table.ts @@ -1,7 +1,7 @@ // eslint-disable-next-line @eslint-community/eslint-comments/disable-enable-pair -- ignore /* eslint-disable func-style -- Arrow functions are better when returning string */ import type { RuleCategory, RuleModule } from "../src/types" -import { rules } from "../src/utils/rules" +import { rules } from "../src/rules" const categories: RuleCategory[] = [ "Possible Errors", diff --git a/tools/update-docs.ts b/tools/update-docs.ts index fa11e063..d80772a5 100644 --- a/tools/update-docs.ts +++ b/tools/update-docs.ts @@ -1,6 +1,6 @@ import path from "path" import fs from "fs" -import { rules } from "../src/utils/rules" +import { rules } from "../src/rules" import type { RuleModule } from "../src/types" import { getNewVersion } from "./lib/changesets-util" import { formatAndSave } from "./lib/utils" diff --git a/tools/update-rules.ts b/tools/update-rules.ts deleted file mode 100644 index 22c3aeaf..00000000 --- a/tools/update-rules.ts +++ /dev/null @@ -1,37 +0,0 @@ -import path from "path" -import { rules } from "./lib/load-rules" -import { formatAndSave } from "./lib/utils" - -/** - * Convert text to camelCase - */ -function camelCase(str: string) { - return str.replace(/[-_](\w)/gu, (_, c) => (c ? c.toUpperCase() : "")) -} - -const content = `/* - * IMPORTANT! - * This file has been automatically generated, - * in order to update its content execute "npm run update" - */ -import type { RuleModule } from "../types" -${rules - .map( - (rule) => - `import ${camelCase(rule.meta.docs.ruleName)} from "../rules/${ - rule.meta.docs.ruleName - }"`, - ) - .join("\n")} -import { buildA11yRules } from "../a11y" - -export const rules = [ - ${rules.map((rule) => camelCase(rule.meta.docs.ruleName)).join(",")}, - ...buildA11yRules() -] as RuleModule[] -` - -const filePath = path.resolve(__dirname, "../src/utils/rules.ts") - -// Update file. -void formatAndSave(filePath, content) diff --git a/tools/update-rulesets.ts b/tools/update-rulesets.ts index c6fb7a43..9cf88584 100644 --- a/tools/update-rulesets.ts +++ b/tools/update-rulesets.ts @@ -1,5 +1,5 @@ import path from "path" -import { rules } from "./lib/load-rules" +import { originalRules as rules } from "../src/rules" import { formatAndSave } from "./lib/utils" const baseRules = rules.filter( diff --git a/tools/update.ts b/tools/update.ts index 441b6cb9..c3e8d407 100644 --- a/tools/update.ts +++ b/tools/update.ts @@ -1,4 +1,3 @@ -import "./update-rules" import "./update-rulesets" import "./update-docs" import "./update-readme"