Skip to content

Commit

Permalink
fix(tasks/lint_rules): Fix plugin-jest rules collector (#2915)
Browse files Browse the repository at this point in the history
Fixes umbrella issue for
[`plugin-jest`](#492) to render
recommended rules section properly.

- - -

Along with sticking legacy ESLint.
Since `eslint@latest` is now v9 and it removed `Linter#defineRule()` and
`Linter#getRules()` which we use heavily...
  • Loading branch information
leaysgur authored Apr 8, 2024
1 parent 96f02e6 commit a0fe504
Showing 1 changed file with 17 additions and 4 deletions.
21 changes: 17 additions & 4 deletions tasks/lint_rules/src/eslint-rules.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,10 @@ const {
configs: pluginJSXA11yConfigs,
} = require("eslint-plugin-jsx-a11y");
// https://github.com/jest-community/eslint-plugin-jest/blob/main/src/index.ts
const { rules: pluginJestAllRules } = require("eslint-plugin-jest");
const {
rules: pluginJestAllRules,
configs: pluginJestConfigs,
} = require("eslint-plugin-jest");
// https://github.com/jsx-eslint/eslint-plugin-react/blob/master/index.js
const { rules: pluginReactAllRules } = require("eslint-plugin-react");
// https://github.com/facebook/react/blob/main/packages/eslint-plugin-react-hooks/src/index.js
Expand Down Expand Up @@ -156,11 +159,16 @@ const loadPluginJSXA11yRules = (linter) => {

/** @param {import("eslint").Linter} linter */
const loadPluginJestRules = (linter) => {
const pluginJestRecommendedRules = new Map(
// @ts-expect-error: Property 'recommended' does not exist on type '{}'.
Object.entries(pluginJestConfigs.recommended.rules),
);
for (const [name, rule] of Object.entries(pluginJestAllRules)) {
const prefixedName = `jest/${name}`;

// Presented but type is `string | false`
rule.meta.docs.recommended = typeof rule.meta.docs.recommended === "string";
const recommendedValue = pluginJestRecommendedRules.get(prefixedName);
// Presented but type is `string | undefined`
rule.meta.docs.recommended = typeof recommendedValue === "string";

linter.defineRule(prefixedName, rule);
}
Expand Down Expand Up @@ -232,7 +240,12 @@ exports.ALL_TARGET_PLUGINS = new Map([
]);

// All rules(including deprecated, recommended) are loaded initially.
exports.createESLintLinter = () => new Linter();
exports.createESLintLinter = () =>
new Linter({
// XXX: We need to adapt to flat ESLint in near future!
// @ts-expect-error: Type '"eslintrc"' is not assignable to type '"flat"'.
configType: "eslintrc",
});

/** @param {import("eslint").Linter} linter */
exports.loadTargetPluginRules = (linter) => {
Expand Down

0 comments on commit a0fe504

Please sign in to comment.