Skip to content

Commit f2cc454

Browse files
committed
fix(plugin-eslint): exclude rules which are turned off
1 parent 9552643 commit f2cc454

File tree

3 files changed

+34
-172
lines changed

3 files changed

+34
-172
lines changed

packages/plugin-eslint/src/lib/__snapshots__/eslint-plugin.spec.ts.snap

-162
Original file line numberDiff line numberDiff line change
@@ -3,30 +3,6 @@
33
exports[`eslintPlugin > should initialize ESLint plugin for Nx project 1`] = `
44
{
55
"audits": [
6-
{
7-
"description": "ESLint rule **explicit-member-accessibility**, from _@typescript-eslint_ plugin.",
8-
"docsUrl": "https://typescript-eslint.io/rules/explicit-member-accessibility",
9-
"slug": "typescript-eslint-explicit-member-accessibility",
10-
"title": "Require explicit accessibility modifiers on class properties and methods",
11-
},
12-
{
13-
"description": "ESLint rule **explicit-module-boundary-types**, from _@typescript-eslint_ plugin.",
14-
"docsUrl": "https://typescript-eslint.io/rules/explicit-module-boundary-types",
15-
"slug": "typescript-eslint-explicit-module-boundary-types",
16-
"title": "Require explicit return and argument types on exported functions' and classes' public class methods",
17-
},
18-
{
19-
"description": "ESLint rule **explicit-function-return-type**, from _@typescript-eslint_ plugin.",
20-
"docsUrl": "https://typescript-eslint.io/rules/explicit-function-return-type",
21-
"slug": "typescript-eslint-explicit-function-return-type",
22-
"title": "Require explicit return types on functions and class methods",
23-
},
24-
{
25-
"description": "ESLint rule **no-parameter-properties**, from _@typescript-eslint_ plugin.",
26-
"docsUrl": "https://typescript-eslint.io/rules/no-parameter-properties",
27-
"slug": "typescript-eslint-no-parameter-properties",
28-
"title": "Disallow the use of parameter properties in class constructors",
29-
},
306
{
317
"description": "ESLint rule **adjacent-overload-signatures**, from _@typescript-eslint_ plugin.",
328
"docsUrl": "https://typescript-eslint.io/rules/adjacent-overload-signatures",
@@ -45,24 +21,12 @@ exports[`eslintPlugin > should initialize ESLint plugin for Nx project 1`] = `
4521
"slug": "typescript-eslint-ban-types",
4622
"title": "Disallow certain types",
4723
},
48-
{
49-
"description": "ESLint rule **no-array-constructor**.",
50-
"docsUrl": "https://eslint.org/docs/latest/rules/no-array-constructor",
51-
"slug": "no-array-constructor",
52-
"title": "Disallow \`Array\` constructors",
53-
},
5424
{
5525
"description": "ESLint rule **no-array-constructor**, from _@typescript-eslint_ plugin.",
5626
"docsUrl": "https://typescript-eslint.io/rules/no-array-constructor",
5727
"slug": "typescript-eslint-no-array-constructor",
5828
"title": "Disallow generic \`Array\` constructors",
5929
},
60-
{
61-
"description": "ESLint rule **no-empty-function**.",
62-
"docsUrl": "https://eslint.org/docs/latest/rules/no-empty-function",
63-
"slug": "no-empty-function",
64-
"title": "Disallow empty functions",
65-
},
6630
{
6731
"description": "ESLint rule **no-empty-function**, from _@typescript-eslint_ plugin.",
6832
"docsUrl": "https://typescript-eslint.io/rules/no-empty-function",
@@ -87,12 +51,6 @@ exports[`eslintPlugin > should initialize ESLint plugin for Nx project 1`] = `
8751
"slug": "typescript-eslint-no-extra-non-null-assertion",
8852
"title": "Disallow extra non-null assertions",
8953
},
90-
{
91-
"description": "ESLint rule **no-extra-semi**.",
92-
"docsUrl": "https://eslint.org/docs/latest/rules/no-extra-semi",
93-
"slug": "no-extra-semi",
94-
"title": "Disallow unnecessary semicolons",
95-
},
9654
{
9755
"description": "ESLint rule **no-extra-semi**, from _@typescript-eslint_ plugin.",
9856
"docsUrl": "https://typescript-eslint.io/rules/no-extra-semi",
@@ -105,12 +63,6 @@ exports[`eslintPlugin > should initialize ESLint plugin for Nx project 1`] = `
10563
"slug": "typescript-eslint-no-inferrable-types",
10664
"title": "Disallow explicit type declarations for variables or parameters initialized to a number, string, or boolean",
10765
},
108-
{
109-
"description": "ESLint rule **no-loss-of-precision**.",
110-
"docsUrl": "https://eslint.org/docs/latest/rules/no-loss-of-precision",
111-
"slug": "no-loss-of-precision",
112-
"title": "Disallow literal numbers that lose precision",
113-
},
11466
{
11567
"description": "ESLint rule **no-loss-of-precision**, from _@typescript-eslint_ plugin.",
11668
"docsUrl": "https://typescript-eslint.io/rules/no-loss-of-precision",
@@ -153,12 +105,6 @@ exports[`eslintPlugin > should initialize ESLint plugin for Nx project 1`] = `
153105
"slug": "typescript-eslint-no-unnecessary-type-constraint",
154106
"title": "Disallow unnecessary constraints on generic types",
155107
},
156-
{
157-
"description": "ESLint rule **no-unused-vars**.",
158-
"docsUrl": "https://eslint.org/docs/latest/rules/no-unused-vars",
159-
"slug": "no-unused-vars",
160-
"title": "Disallow unused variables",
161-
},
162108
{
163109
"description": "ESLint rule **no-unused-vars**, from _@typescript-eslint_ plugin.",
164110
"docsUrl": "https://typescript-eslint.io/rules/no-unused-vars",
@@ -189,102 +135,6 @@ exports[`eslintPlugin > should initialize ESLint plugin for Nx project 1`] = `
189135
"slug": "typescript-eslint-triple-slash-reference",
190136
"title": "Disallow certain triple slash directives in favor of ES6-style import declarations",
191137
},
192-
{
193-
"description": "ESLint rule **constructor-super**.",
194-
"docsUrl": "https://eslint.org/docs/latest/rules/constructor-super",
195-
"slug": "constructor-super",
196-
"title": "Require \`super()\` calls in constructors",
197-
},
198-
{
199-
"description": "ESLint rule **getter-return**.",
200-
"docsUrl": "https://eslint.org/docs/latest/rules/getter-return",
201-
"slug": "getter-return",
202-
"title": "Enforce \`return\` statements in getters",
203-
},
204-
{
205-
"description": "ESLint rule **no-const-assign**.",
206-
"docsUrl": "https://eslint.org/docs/latest/rules/no-const-assign",
207-
"slug": "no-const-assign",
208-
"title": "Disallow reassigning \`const\` variables",
209-
},
210-
{
211-
"description": "ESLint rule **no-dupe-args**.",
212-
"docsUrl": "https://eslint.org/docs/latest/rules/no-dupe-args",
213-
"slug": "no-dupe-args",
214-
"title": "Disallow duplicate arguments in \`function\` definitions",
215-
},
216-
{
217-
"description": "ESLint rule **no-dupe-class-members**.",
218-
"docsUrl": "https://eslint.org/docs/latest/rules/no-dupe-class-members",
219-
"slug": "no-dupe-class-members",
220-
"title": "Disallow duplicate class members",
221-
},
222-
{
223-
"description": "ESLint rule **no-dupe-keys**.",
224-
"docsUrl": "https://eslint.org/docs/latest/rules/no-dupe-keys",
225-
"slug": "no-dupe-keys",
226-
"title": "Disallow duplicate keys in object literals",
227-
},
228-
{
229-
"description": "ESLint rule **no-func-assign**.",
230-
"docsUrl": "https://eslint.org/docs/latest/rules/no-func-assign",
231-
"slug": "no-func-assign",
232-
"title": "Disallow reassigning \`function\` declarations",
233-
},
234-
{
235-
"description": "ESLint rule **no-import-assign**.",
236-
"docsUrl": "https://eslint.org/docs/latest/rules/no-import-assign",
237-
"slug": "no-import-assign",
238-
"title": "Disallow assigning to imported bindings",
239-
},
240-
{
241-
"description": "ESLint rule **no-new-symbol**.",
242-
"docsUrl": "https://eslint.org/docs/latest/rules/no-new-symbol",
243-
"slug": "no-new-symbol",
244-
"title": "Disallow \`new\` operators with the \`Symbol\` object",
245-
},
246-
{
247-
"description": "ESLint rule **no-obj-calls**.",
248-
"docsUrl": "https://eslint.org/docs/latest/rules/no-obj-calls",
249-
"slug": "no-obj-calls",
250-
"title": "Disallow calling global object properties as functions",
251-
},
252-
{
253-
"description": "ESLint rule **no-redeclare**.",
254-
"docsUrl": "https://eslint.org/docs/latest/rules/no-redeclare",
255-
"slug": "no-redeclare",
256-
"title": "Disallow variable redeclaration",
257-
},
258-
{
259-
"description": "ESLint rule **no-setter-return**.",
260-
"docsUrl": "https://eslint.org/docs/latest/rules/no-setter-return",
261-
"slug": "no-setter-return",
262-
"title": "Disallow returning values from setters",
263-
},
264-
{
265-
"description": "ESLint rule **no-this-before-super**.",
266-
"docsUrl": "https://eslint.org/docs/latest/rules/no-this-before-super",
267-
"slug": "no-this-before-super",
268-
"title": "Disallow \`this\`/\`super\` before calling \`super()\` in constructors",
269-
},
270-
{
271-
"description": "ESLint rule **no-undef**.",
272-
"docsUrl": "https://eslint.org/docs/latest/rules/no-undef",
273-
"slug": "no-undef",
274-
"title": "Disallow the use of undeclared variables unless mentioned in \`/*global */\` comments",
275-
},
276-
{
277-
"description": "ESLint rule **no-unreachable**.",
278-
"docsUrl": "https://eslint.org/docs/latest/rules/no-unreachable",
279-
"slug": "no-unreachable",
280-
"title": "Disallow unreachable code after \`return\`, \`throw\`, \`continue\`, and \`break\` statements",
281-
},
282-
{
283-
"description": "ESLint rule **no-unsafe-negation**.",
284-
"docsUrl": "https://eslint.org/docs/latest/rules/no-unsafe-negation",
285-
"slug": "no-unsafe-negation",
286-
"title": "Disallow negating the left operand of relational operators",
287-
},
288138
{
289139
"description": "ESLint rule **no-var**.",
290140
"docsUrl": "https://eslint.org/docs/latest/rules/no-var",
@@ -309,12 +159,6 @@ exports[`eslintPlugin > should initialize ESLint plugin for Nx project 1`] = `
309159
"slug": "prefer-spread",
310160
"title": "Require spread operators instead of \`.apply()\`",
311161
},
312-
{
313-
"description": "ESLint rule **valid-typeof**.",
314-
"docsUrl": "https://eslint.org/docs/latest/rules/valid-typeof",
315-
"slug": "valid-typeof",
316-
"title": "Enforce comparing \`typeof\` expressions against valid strings",
317-
},
318162
{
319163
"description": "ESLint rule **for-direction**.",
320164
"docsUrl": "https://eslint.org/docs/latest/rules/for-direction",
@@ -889,12 +733,6 @@ exports[`eslintPlugin > should initialize ESLint plugin for React application 1`
889733
"slug": "react-no-unknown-property",
890734
"title": "Disallow usage of unknown DOM property",
891735
},
892-
{
893-
"description": "ESLint rule **no-unsafe**, from _react_ plugin.",
894-
"docsUrl": "https://github.com/jsx-eslint/eslint-plugin-react/tree/master/docs/rules/no-unsafe.md",
895-
"slug": "react-no-unsafe",
896-
"title": "Disallow usage of unsafe lifecycle methods",
897-
},
898736
{
899737
"description": "ESLint rule **require-render-return**, from _react_ plugin.",
900738
"docsUrl": "https://github.com/jsx-eslint/eslint-plugin-react/tree/master/docs/rules/require-render-return.md",

packages/plugin-eslint/src/lib/meta/rules.spec.ts

+18-10
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ describe('listRules', () => {
3939
});
4040

4141
it('should list expected number of rules', async () => {
42-
await expect(listRules(eslint, patterns)).resolves.toHaveLength(48);
42+
await expect(listRules(eslint, patterns)).resolves.toHaveLength(47);
4343
});
4444

4545
it('should include explicitly set built-in rule', async () => {
@@ -105,7 +105,7 @@ describe('listRules', () => {
105105
});
106106

107107
it('should list expected number of rules', async () => {
108-
await expect(listRules(eslint, patterns)).resolves.toHaveLength(95);
108+
await expect(listRules(eslint, patterns)).resolves.toHaveLength(69);
109109
});
110110

111111
it('should include explicitly set plugin rule with custom options', async () => {
@@ -147,26 +147,27 @@ describe('listRules', () => {
147147

148148
it('should include built-in rule set implicitly by extending recommended config', async () => {
149149
await expect(listRules(eslint, patterns)).resolves.toContainEqual({
150+
ruleId: 'no-var',
150151
meta: {
151152
docs: {
152-
description: 'Disallow unnecessary semicolons',
153-
recommended: true,
154-
url: 'https://eslint.org/docs/latest/rules/no-extra-semi',
153+
description: 'Require `let` or `const` instead of `var`',
154+
recommended: false,
155+
url: 'https://eslint.org/docs/latest/rules/no-var',
155156
},
156157
fixable: 'code',
157158
messages: {
158-
unexpected: 'Unnecessary semicolon.',
159+
unexpectedVar: 'Unexpected var, use let or const instead.',
159160
},
160161
schema: [],
161162
type: 'suggestion',
162163
},
163164
options: [],
164-
ruleId: 'no-extra-semi',
165-
} satisfies RuleData);
165+
} as RuleData);
166166
});
167167

168168
it('should include plugin rule set implicitly by extending recommended config', async () => {
169169
await expect(listRules(eslint, patterns)).resolves.toContainEqual({
170+
ruleId: '@typescript-eslint/no-extra-semi',
170171
meta: {
171172
docs: {
172173
description: 'Disallow unnecessary semicolons',
@@ -184,12 +185,20 @@ describe('listRules', () => {
184185
type: 'suggestion',
185186
},
186187
options: [],
187-
ruleId: '@typescript-eslint/no-extra-semi',
188188
} satisfies RuleData);
189189
});
190190

191+
it('should not include rule which was turned off', async () => {
192+
await expect(listRules(eslint, patterns)).resolves.not.toContainEqual(
193+
expect.objectContaining({
194+
ruleId: 'no-extra-semi',
195+
} satisfies Partial<RuleData>),
196+
);
197+
});
198+
191199
it('should include rule added by to root config by project config', async () => {
192200
await expect(listRules(eslint, patterns)).resolves.toContainEqual({
201+
ruleId: '@nx/dependency-checks',
193202
meta: {
194203
docs: {
195204
description:
@@ -226,7 +235,6 @@ describe('listRules', () => {
226235
type: 'suggestion',
227236
},
228237
options: [],
229-
ruleId: '@nx/dependency-checks',
230238
} satisfies RuleData);
231239
});
232240
});

packages/plugin-eslint/src/lib/meta/rules.ts

+16
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ export async function listRules(
3232

3333
const rulesMap = configs
3434
.flatMap(config => Object.entries(config.rules ?? {}))
35+
.filter(([, ruleEntry]) => ruleEntry != null && !isRuleOff(ruleEntry))
3536
.reduce<Record<string, Record<string, RuleData>>>(
3637
(acc, [ruleId, ruleEntry]) => {
3738
const meta = rulesMeta[ruleId];
@@ -59,3 +60,18 @@ export async function listRules(
5960

6061
return Object.values(rulesMap).flatMap(Object.values);
6162
}
63+
64+
function isRuleOff(entry: Linter.RuleEntry<unknown[]>): boolean {
65+
const level: Linter.RuleLevel = Array.isArray(entry) ? entry[0] : entry;
66+
67+
switch (level) {
68+
case 0:
69+
case 'off':
70+
return true;
71+
case 1:
72+
case 2:
73+
case 'warn':
74+
case 'error':
75+
return false;
76+
}
77+
}

0 commit comments

Comments
 (0)