Skip to content

Commit

Permalink
feat: add eslint-plugin-sonarjs (#161)
Browse files Browse the repository at this point in the history
Co-authored-by: Julien <julien@ripouteau.com>
  • Loading branch information
Shinigami92 and Julien-R44 committed Jan 2, 2023
1 parent 423dbb7 commit 694223a
Show file tree
Hide file tree
Showing 40 changed files with 953 additions and 0 deletions.
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -72,6 +72,7 @@
"eslint-plugin-n": "~15.6.0",
"eslint-plugin-node": "~11.1.0",
"eslint-plugin-prettier": "~4.2.1",
"eslint-plugin-sonarjs": "~0.17.0",
"eslint-plugin-spellcheck": "~0.0.20",
"eslint-plugin-unicorn": "~45.0.2",
"eslint-plugin-vue": "~9.8.0",
Expand Down
11 changes: 11 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions scripts/generate-rule-files/src/plugins-map.ts
Expand Up @@ -10,6 +10,7 @@ import * as eslintPluginMdx from 'eslint-plugin-mdx';
import eslintPluginNode from 'eslint-plugin-node';
// @ts-expect-error
import eslintPluginN from 'eslint-plugin-n';
import * as eslintPluginSonarJS from 'eslint-plugin-sonarjs';
// @ts-expect-error
import eslintPluginSpellcheck from 'eslint-plugin-spellcheck';
// @ts-expect-error
Expand Down Expand Up @@ -71,6 +72,11 @@ export const PLUGIN_REGISTRY: Readonly<Record<string, Plugin>> = {
name: 'Node',
rules: (eslintPluginNode as Plugin).rules,
},
sonarjs: {
name: 'SonarJS',
prefix: 'sonarjs',
rules: eslintPluginSonarJS.rules,
},
spellcheck: {
name: 'Spellcheck',
rules: (eslintPluginSpellcheck as Plugin).rules,
Expand Down
6 changes: 6 additions & 0 deletions src/config/extends/eslint-plugin-sonarjs.d.ts
@@ -0,0 +1,6 @@
/**
* Eslint Sonarjs extensions.
*
* @see [Eslint Sonarjs extensions](https://github.com/SonarSource/eslint-plugin-sonarjs#available-configurations)
*/
export type SonarjsExtensions = 'plugin:sonarjs/recommended';
2 changes: 2 additions & 0 deletions src/config/extends/index.d.ts
Expand Up @@ -7,6 +7,7 @@ import type { MdxExtensions } from './eslint-plugin-mdx';
import type { NExtensions } from './eslint-plugin-n';
import type { NodeExtensions } from './eslint-plugin-node';
import type { PrettierExtensions } from './eslint-plugin-prettier';
import type { SonarjsExtensions } from './eslint-plugin-sonarjs';
import type { UnicornExtensions } from './eslint-plugin-unicorn';
import type { VueExtensions } from './eslint-plugin-vue';
import type { VuePugExtensions } from './eslint-plugin-vue-pug';
Expand All @@ -26,6 +27,7 @@ export type KnownExtensions = LiteralUnion<
| NExtensions
| NodeExtensions
| PrettierExtensions
| SonarjsExtensions
| TypescriptEslintExtensions
| UnicornExtensions
| VueExtensions
Expand Down
1 change: 1 addition & 0 deletions src/config/plugin.d.ts
Expand Up @@ -9,6 +9,7 @@ export type Plugin = LiteralUnion<
| 'jsdoc'
| 'mdx'
| 'prettier'
| 'sonarjs'
| 'spellcheck'
| 'unicorn'
| 'vue'
Expand Down
2 changes: 2 additions & 0 deletions src/rules/index.d.ts
Expand Up @@ -6,6 +6,7 @@ import type { JsoncRules } from './jsonc';
import type { NRules } from './n';
import type { NodeRules } from './node';
import type { RuleConfig } from './rule-config';
import type { SonarJSRules } from './sonarjs';
import type { SpellcheckRules } from './spellcheck';
import type { TypeScriptRules } from './typescript-eslint';
import type { UnicornRules } from './unicorn';
Expand All @@ -26,6 +27,7 @@ export type Rules = Partial<
JsoncRules &
NodeRules &
NRules &
SonarJSRules &
SpellcheckRules &
TypeScriptRules &
UnicornRules &
Expand Down
41 changes: 41 additions & 0 deletions src/rules/sonarjs/cognitive-complexity.d.ts
@@ -0,0 +1,41 @@
import type { RuleConfig } from '../rule-config';

/**
* Config.
*/
export type CognitiveComplexityConfig = 'sonar-runtime' | 'metric';

/**
* Option.
*/
export type CognitiveComplexityOption = number;

/**
* Options.
*/
export type CognitiveComplexityOptions = [
CognitiveComplexityOption?,
CognitiveComplexityConfig?,
];

/**
* Cognitive Complexity of functions should not be too high.
*
* @see [cognitive-complexity](https://github.com/SonarSource/eslint-plugin-sonarjs/blob/master/docs/rules/cognitive-complexity.md)
*/
export type CognitiveComplexityRuleConfig =
RuleConfig<CognitiveComplexityOptions>;

/**
* Cognitive Complexity of functions should not be too high.
*
* @see [cognitive-complexity](https://github.com/SonarSource/eslint-plugin-sonarjs/blob/master/docs/rules/cognitive-complexity.md)
*/
export interface CognitiveComplexityRule {
/**
* Cognitive Complexity of functions should not be too high.
*
* @see [cognitive-complexity](https://github.com/SonarSource/eslint-plugin-sonarjs/blob/master/docs/rules/cognitive-complexity.md)
*/
'sonarjs/cognitive-complexity': CognitiveComplexityRuleConfig;
}
22 changes: 22 additions & 0 deletions src/rules/sonarjs/elseif-without-else.d.ts
@@ -0,0 +1,22 @@
import type { RuleConfig } from '../rule-config';

/**
* "if ... else if" constructs should end with "else" clauses.
*
* @see [elseif-without-else](https://github.com/SonarSource/eslint-plugin-sonarjs/blob/master/docs/rules/elseif-without-else.md)
*/
export type ElseifWithoutElseRuleConfig = RuleConfig<[]>;

/**
* "if ... else if" constructs should end with "else" clauses.
*
* @see [elseif-without-else](https://github.com/SonarSource/eslint-plugin-sonarjs/blob/master/docs/rules/elseif-without-else.md)
*/
export interface ElseifWithoutElseRule {
/**
* "if ... else if" constructs should end with "else" clauses.
*
* @see [elseif-without-else](https://github.com/SonarSource/eslint-plugin-sonarjs/blob/master/docs/rules/elseif-without-else.md)
*/
'sonarjs/elseif-without-else': ElseifWithoutElseRuleConfig;
}
68 changes: 68 additions & 0 deletions src/rules/sonarjs/index.d.ts
@@ -0,0 +1,68 @@
import type { CognitiveComplexityRule } from './cognitive-complexity';
import type { ElseifWithoutElseRule } from './elseif-without-else';
import type { MaxSwitchCasesRule } from './max-switch-cases';
import type { NoAllDuplicatedBranchesRule } from './no-all-duplicated-branches';
import type { NoCollapsibleIfRule } from './no-collapsible-if';
import type { NoCollectionSizeMischeckRule } from './no-collection-size-mischeck';
import type { NoDuplicateStringRule } from './no-duplicate-string';
import type { NoDuplicatedBranchesRule } from './no-duplicated-branches';
import type { NoElementOverwriteRule } from './no-element-overwrite';
import type { NoEmptyCollectionRule } from './no-empty-collection';
import type { NoExtraArgumentsRule } from './no-extra-arguments';
import type { NoGratuitousExpressionsRule } from './no-gratuitous-expressions';
import type { NoIdenticalConditionsRule } from './no-identical-conditions';
import type { NoIdenticalExpressionsRule } from './no-identical-expressions';
import type { NoIdenticalFunctionsRule } from './no-identical-functions';
import type { NoIgnoredReturnRule } from './no-ignored-return';
import type { NoInvertedBooleanCheckRule } from './no-inverted-boolean-check';
import type { NoNestedSwitchRule } from './no-nested-switch';
import type { NoNestedTemplateLiteralsRule } from './no-nested-template-literals';
import type { NoOneIterationLoopRule } from './no-one-iteration-loop';
import type { NoRedundantBooleanRule } from './no-redundant-boolean';
import type { NoRedundantJumpRule } from './no-redundant-jump';
import type { NoSameLineConditionalRule } from './no-same-line-conditional';
import type { NoSmallSwitchRule } from './no-small-switch';
import type { NoUnusedCollectionRule } from './no-unused-collection';
import type { NoUseOfEmptyReturnValueRule } from './no-use-of-empty-return-value';
import type { NoUselessCatchRule } from './no-useless-catch';
import type { NonExistentOperatorRule } from './non-existent-operator';
import type { PreferImmediateReturnRule } from './prefer-immediate-return';
import type { PreferObjectLiteralRule } from './prefer-object-literal';
import type { PreferSingleBooleanReturnRule } from './prefer-single-boolean-return';
import type { PreferWhileRule } from './prefer-while';

/**
* All SonarJS rules.
*/
export type SonarJSRules = CognitiveComplexityRule &
ElseifWithoutElseRule &
MaxSwitchCasesRule &
NoAllDuplicatedBranchesRule &
NoCollapsibleIfRule &
NoCollectionSizeMischeckRule &
NoDuplicateStringRule &
NoDuplicatedBranchesRule &
NoElementOverwriteRule &
NoEmptyCollectionRule &
NoExtraArgumentsRule &
NoGratuitousExpressionsRule &
NoIdenticalConditionsRule &
NoIdenticalExpressionsRule &
NoIdenticalFunctionsRule &
NoIgnoredReturnRule &
NoInvertedBooleanCheckRule &
NoNestedSwitchRule &
NoNestedTemplateLiteralsRule &
NoOneIterationLoopRule &
NoRedundantBooleanRule &
NoRedundantJumpRule &
NoSameLineConditionalRule &
NoSmallSwitchRule &
NoUnusedCollectionRule &
NoUseOfEmptyReturnValueRule &
NoUselessCatchRule &
NonExistentOperatorRule &
PreferImmediateReturnRule &
PreferObjectLiteralRule &
PreferSingleBooleanReturnRule &
PreferWhileRule;
32 changes: 32 additions & 0 deletions src/rules/sonarjs/max-switch-cases.d.ts
@@ -0,0 +1,32 @@
import type { RuleConfig } from '../rule-config';

/**
* Option.
*/
export type MaxSwitchCasesOption = number;

/**
* Options.
*/
export type MaxSwitchCasesOptions = [MaxSwitchCasesOption?];

/**
* "switch" statements should not have too many "case" clauses.
*
* @see [max-switch-cases](https://github.com/SonarSource/eslint-plugin-sonarjs/blob/master/docs/rules/max-switch-cases.md)
*/
export type MaxSwitchCasesRuleConfig = RuleConfig<MaxSwitchCasesOptions>;

/**
* "switch" statements should not have too many "case" clauses.
*
* @see [max-switch-cases](https://github.com/SonarSource/eslint-plugin-sonarjs/blob/master/docs/rules/max-switch-cases.md)
*/
export interface MaxSwitchCasesRule {
/**
* "switch" statements should not have too many "case" clauses.
*
* @see [max-switch-cases](https://github.com/SonarSource/eslint-plugin-sonarjs/blob/master/docs/rules/max-switch-cases.md)
*/
'sonarjs/max-switch-cases': MaxSwitchCasesRuleConfig;
}
22 changes: 22 additions & 0 deletions src/rules/sonarjs/no-all-duplicated-branches.d.ts
@@ -0,0 +1,22 @@
import type { RuleConfig } from '../rule-config';

/**
* All branches in a conditional structure should not have exactly the same implementation.
*
* @see [no-all-duplicated-branches](https://github.com/SonarSource/eslint-plugin-sonarjs/blob/master/docs/rules/no-all-duplicated-branches.md)
*/
export type NoAllDuplicatedBranchesRuleConfig = RuleConfig<[]>;

/**
* All branches in a conditional structure should not have exactly the same implementation.
*
* @see [no-all-duplicated-branches](https://github.com/SonarSource/eslint-plugin-sonarjs/blob/master/docs/rules/no-all-duplicated-branches.md)
*/
export interface NoAllDuplicatedBranchesRule {
/**
* All branches in a conditional structure should not have exactly the same implementation.
*
* @see [no-all-duplicated-branches](https://github.com/SonarSource/eslint-plugin-sonarjs/blob/master/docs/rules/no-all-duplicated-branches.md)
*/
'sonarjs/no-all-duplicated-branches': NoAllDuplicatedBranchesRuleConfig;
}
32 changes: 32 additions & 0 deletions src/rules/sonarjs/no-collapsible-if.d.ts
@@ -0,0 +1,32 @@
import type { RuleConfig } from '../rule-config';

/**
* Option.
*/
export type NoCollapsibleIfOption = 'sonar-runtime';

/**
* Options.
*/
export type NoCollapsibleIfOptions = [NoCollapsibleIfOption?];

/**
* Collapsible "if" statements should be merged.
*
* @see [no-collapsible-if](https://github.com/SonarSource/eslint-plugin-sonarjs/blob/master/docs/rules/no-collapsible-if.md)
*/
export type NoCollapsibleIfRuleConfig = RuleConfig<NoCollapsibleIfOptions>;

/**
* Collapsible "if" statements should be merged.
*
* @see [no-collapsible-if](https://github.com/SonarSource/eslint-plugin-sonarjs/blob/master/docs/rules/no-collapsible-if.md)
*/
export interface NoCollapsibleIfRule {
/**
* Collapsible "if" statements should be merged.
*
* @see [no-collapsible-if](https://github.com/SonarSource/eslint-plugin-sonarjs/blob/master/docs/rules/no-collapsible-if.md)
*/
'sonarjs/no-collapsible-if': NoCollapsibleIfRuleConfig;
}
22 changes: 22 additions & 0 deletions src/rules/sonarjs/no-collection-size-mischeck.d.ts
@@ -0,0 +1,22 @@
import type { RuleConfig } from '../rule-config';

/**
* Collection sizes and array length comparisons should make sense.
*
* @see [no-collection-size-mischeck](https://github.com/SonarSource/eslint-plugin-sonarjs/blob/master/docs/rules/no-collection-size-mischeck.md)
*/
export type NoCollectionSizeMischeckRuleConfig = RuleConfig<[]>;

/**
* Collection sizes and array length comparisons should make sense.
*
* @see [no-collection-size-mischeck](https://github.com/SonarSource/eslint-plugin-sonarjs/blob/master/docs/rules/no-collection-size-mischeck.md)
*/
export interface NoCollectionSizeMischeckRule {
/**
* Collection sizes and array length comparisons should make sense.
*
* @see [no-collection-size-mischeck](https://github.com/SonarSource/eslint-plugin-sonarjs/blob/master/docs/rules/no-collection-size-mischeck.md)
*/
'sonarjs/no-collection-size-mischeck': NoCollectionSizeMischeckRuleConfig;
}
40 changes: 40 additions & 0 deletions src/rules/sonarjs/no-duplicate-string.d.ts
@@ -0,0 +1,40 @@
import type { RuleConfig } from '../rule-config';

/**
* Config.
*/
export type NoDuplicateStringConfig = 'sonar-runtime';

/**
* Option.
*/
export type NoDuplicateStringOption = number;

/**
* Options.
*/
export type NoDuplicateStringOptions = [
NoDuplicateStringOption?,
NoDuplicateStringConfig?,
];

/**
* String literals should not be duplicated.
*
* @see [no-duplicate-string](https://github.com/SonarSource/eslint-plugin-sonarjs/blob/master/docs/rules/no-duplicate-string.md)
*/
export type NoDuplicateStringRuleConfig = RuleConfig<NoDuplicateStringOptions>;

/**
* String literals should not be duplicated.
*
* @see [no-duplicate-string](https://github.com/SonarSource/eslint-plugin-sonarjs/blob/master/docs/rules/no-duplicate-string.md)
*/
export interface NoDuplicateStringRule {
/**
* String literals should not be duplicated.
*
* @see [no-duplicate-string](https://github.com/SonarSource/eslint-plugin-sonarjs/blob/master/docs/rules/no-duplicate-string.md)
*/
'sonarjs/no-duplicate-string': NoDuplicateStringRuleConfig;
}

0 comments on commit 694223a

Please sign in to comment.