Skip to content
This repository was archived by the owner on Mar 7, 2025. It is now read-only.

Commit 694223a

Browse files
feat: add eslint-plugin-sonarjs (#161)
Co-authored-by: Julien <[email protected]>
1 parent 423dbb7 commit 694223a

40 files changed

+953
-0
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@
7272
"eslint-plugin-n": "~15.6.0",
7373
"eslint-plugin-node": "~11.1.0",
7474
"eslint-plugin-prettier": "~4.2.1",
75+
"eslint-plugin-sonarjs": "~0.17.0",
7576
"eslint-plugin-spellcheck": "~0.0.20",
7677
"eslint-plugin-unicorn": "~45.0.2",
7778
"eslint-plugin-vue": "~9.8.0",

pnpm-lock.yaml

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

scripts/generate-rule-files/src/plugins-map.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import * as eslintPluginMdx from 'eslint-plugin-mdx';
1010
import eslintPluginNode from 'eslint-plugin-node';
1111
// @ts-expect-error
1212
import eslintPluginN from 'eslint-plugin-n';
13+
import * as eslintPluginSonarJS from 'eslint-plugin-sonarjs';
1314
// @ts-expect-error
1415
import eslintPluginSpellcheck from 'eslint-plugin-spellcheck';
1516
// @ts-expect-error
@@ -71,6 +72,11 @@ export const PLUGIN_REGISTRY: Readonly<Record<string, Plugin>> = {
7172
name: 'Node',
7273
rules: (eslintPluginNode as Plugin).rules,
7374
},
75+
sonarjs: {
76+
name: 'SonarJS',
77+
prefix: 'sonarjs',
78+
rules: eslintPluginSonarJS.rules,
79+
},
7480
spellcheck: {
7581
name: 'Spellcheck',
7682
rules: (eslintPluginSpellcheck as Plugin).rules,
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
/**
2+
* Eslint Sonarjs extensions.
3+
*
4+
* @see [Eslint Sonarjs extensions](https://github.com/SonarSource/eslint-plugin-sonarjs#available-configurations)
5+
*/
6+
export type SonarjsExtensions = 'plugin:sonarjs/recommended';

src/config/extends/index.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import type { MdxExtensions } from './eslint-plugin-mdx';
77
import type { NExtensions } from './eslint-plugin-n';
88
import type { NodeExtensions } from './eslint-plugin-node';
99
import type { PrettierExtensions } from './eslint-plugin-prettier';
10+
import type { SonarjsExtensions } from './eslint-plugin-sonarjs';
1011
import type { UnicornExtensions } from './eslint-plugin-unicorn';
1112
import type { VueExtensions } from './eslint-plugin-vue';
1213
import type { VuePugExtensions } from './eslint-plugin-vue-pug';
@@ -26,6 +27,7 @@ export type KnownExtensions = LiteralUnion<
2627
| NExtensions
2728
| NodeExtensions
2829
| PrettierExtensions
30+
| SonarjsExtensions
2931
| TypescriptEslintExtensions
3032
| UnicornExtensions
3133
| VueExtensions

src/config/plugin.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ export type Plugin = LiteralUnion<
99
| 'jsdoc'
1010
| 'mdx'
1111
| 'prettier'
12+
| 'sonarjs'
1213
| 'spellcheck'
1314
| 'unicorn'
1415
| 'vue'

src/rules/index.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import type { JsoncRules } from './jsonc';
66
import type { NRules } from './n';
77
import type { NodeRules } from './node';
88
import type { RuleConfig } from './rule-config';
9+
import type { SonarJSRules } from './sonarjs';
910
import type { SpellcheckRules } from './spellcheck';
1011
import type { TypeScriptRules } from './typescript-eslint';
1112
import type { UnicornRules } from './unicorn';
@@ -26,6 +27,7 @@ export type Rules = Partial<
2627
JsoncRules &
2728
NodeRules &
2829
NRules &
30+
SonarJSRules &
2931
SpellcheckRules &
3032
TypeScriptRules &
3133
UnicornRules &
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import type { RuleConfig } from '../rule-config';
2+
3+
/**
4+
* Config.
5+
*/
6+
export type CognitiveComplexityConfig = 'sonar-runtime' | 'metric';
7+
8+
/**
9+
* Option.
10+
*/
11+
export type CognitiveComplexityOption = number;
12+
13+
/**
14+
* Options.
15+
*/
16+
export type CognitiveComplexityOptions = [
17+
CognitiveComplexityOption?,
18+
CognitiveComplexityConfig?,
19+
];
20+
21+
/**
22+
* Cognitive Complexity of functions should not be too high.
23+
*
24+
* @see [cognitive-complexity](https://github.com/SonarSource/eslint-plugin-sonarjs/blob/master/docs/rules/cognitive-complexity.md)
25+
*/
26+
export type CognitiveComplexityRuleConfig =
27+
RuleConfig<CognitiveComplexityOptions>;
28+
29+
/**
30+
* Cognitive Complexity of functions should not be too high.
31+
*
32+
* @see [cognitive-complexity](https://github.com/SonarSource/eslint-plugin-sonarjs/blob/master/docs/rules/cognitive-complexity.md)
33+
*/
34+
export interface CognitiveComplexityRule {
35+
/**
36+
* Cognitive Complexity of functions should not be too high.
37+
*
38+
* @see [cognitive-complexity](https://github.com/SonarSource/eslint-plugin-sonarjs/blob/master/docs/rules/cognitive-complexity.md)
39+
*/
40+
'sonarjs/cognitive-complexity': CognitiveComplexityRuleConfig;
41+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import type { RuleConfig } from '../rule-config';
2+
3+
/**
4+
* "if ... else if" constructs should end with "else" clauses.
5+
*
6+
* @see [elseif-without-else](https://github.com/SonarSource/eslint-plugin-sonarjs/blob/master/docs/rules/elseif-without-else.md)
7+
*/
8+
export type ElseifWithoutElseRuleConfig = RuleConfig<[]>;
9+
10+
/**
11+
* "if ... else if" constructs should end with "else" clauses.
12+
*
13+
* @see [elseif-without-else](https://github.com/SonarSource/eslint-plugin-sonarjs/blob/master/docs/rules/elseif-without-else.md)
14+
*/
15+
export interface ElseifWithoutElseRule {
16+
/**
17+
* "if ... else if" constructs should end with "else" clauses.
18+
*
19+
* @see [elseif-without-else](https://github.com/SonarSource/eslint-plugin-sonarjs/blob/master/docs/rules/elseif-without-else.md)
20+
*/
21+
'sonarjs/elseif-without-else': ElseifWithoutElseRuleConfig;
22+
}

src/rules/sonarjs/index.d.ts

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import type { CognitiveComplexityRule } from './cognitive-complexity';
2+
import type { ElseifWithoutElseRule } from './elseif-without-else';
3+
import type { MaxSwitchCasesRule } from './max-switch-cases';
4+
import type { NoAllDuplicatedBranchesRule } from './no-all-duplicated-branches';
5+
import type { NoCollapsibleIfRule } from './no-collapsible-if';
6+
import type { NoCollectionSizeMischeckRule } from './no-collection-size-mischeck';
7+
import type { NoDuplicateStringRule } from './no-duplicate-string';
8+
import type { NoDuplicatedBranchesRule } from './no-duplicated-branches';
9+
import type { NoElementOverwriteRule } from './no-element-overwrite';
10+
import type { NoEmptyCollectionRule } from './no-empty-collection';
11+
import type { NoExtraArgumentsRule } from './no-extra-arguments';
12+
import type { NoGratuitousExpressionsRule } from './no-gratuitous-expressions';
13+
import type { NoIdenticalConditionsRule } from './no-identical-conditions';
14+
import type { NoIdenticalExpressionsRule } from './no-identical-expressions';
15+
import type { NoIdenticalFunctionsRule } from './no-identical-functions';
16+
import type { NoIgnoredReturnRule } from './no-ignored-return';
17+
import type { NoInvertedBooleanCheckRule } from './no-inverted-boolean-check';
18+
import type { NoNestedSwitchRule } from './no-nested-switch';
19+
import type { NoNestedTemplateLiteralsRule } from './no-nested-template-literals';
20+
import type { NoOneIterationLoopRule } from './no-one-iteration-loop';
21+
import type { NoRedundantBooleanRule } from './no-redundant-boolean';
22+
import type { NoRedundantJumpRule } from './no-redundant-jump';
23+
import type { NoSameLineConditionalRule } from './no-same-line-conditional';
24+
import type { NoSmallSwitchRule } from './no-small-switch';
25+
import type { NoUnusedCollectionRule } from './no-unused-collection';
26+
import type { NoUseOfEmptyReturnValueRule } from './no-use-of-empty-return-value';
27+
import type { NoUselessCatchRule } from './no-useless-catch';
28+
import type { NonExistentOperatorRule } from './non-existent-operator';
29+
import type { PreferImmediateReturnRule } from './prefer-immediate-return';
30+
import type { PreferObjectLiteralRule } from './prefer-object-literal';
31+
import type { PreferSingleBooleanReturnRule } from './prefer-single-boolean-return';
32+
import type { PreferWhileRule } from './prefer-while';
33+
34+
/**
35+
* All SonarJS rules.
36+
*/
37+
export type SonarJSRules = CognitiveComplexityRule &
38+
ElseifWithoutElseRule &
39+
MaxSwitchCasesRule &
40+
NoAllDuplicatedBranchesRule &
41+
NoCollapsibleIfRule &
42+
NoCollectionSizeMischeckRule &
43+
NoDuplicateStringRule &
44+
NoDuplicatedBranchesRule &
45+
NoElementOverwriteRule &
46+
NoEmptyCollectionRule &
47+
NoExtraArgumentsRule &
48+
NoGratuitousExpressionsRule &
49+
NoIdenticalConditionsRule &
50+
NoIdenticalExpressionsRule &
51+
NoIdenticalFunctionsRule &
52+
NoIgnoredReturnRule &
53+
NoInvertedBooleanCheckRule &
54+
NoNestedSwitchRule &
55+
NoNestedTemplateLiteralsRule &
56+
NoOneIterationLoopRule &
57+
NoRedundantBooleanRule &
58+
NoRedundantJumpRule &
59+
NoSameLineConditionalRule &
60+
NoSmallSwitchRule &
61+
NoUnusedCollectionRule &
62+
NoUseOfEmptyReturnValueRule &
63+
NoUselessCatchRule &
64+
NonExistentOperatorRule &
65+
PreferImmediateReturnRule &
66+
PreferObjectLiteralRule &
67+
PreferSingleBooleanReturnRule &
68+
PreferWhileRule;

0 commit comments

Comments
 (0)