UNPKG

9.23 kBMarkdownView Raw
1# eslint-plugin-sonarjs [![npm version](https://badge.fury.io/js/eslint-plugin-sonarjs.svg)](https://badge.fury.io/js/eslint-plugin-sonarjs) [![Build Status](https://api.cirrus-ci.com/github/SonarSource/eslint-plugin-sonarjs.svg?branch=master)](https://cirrus-ci.com/github/SonarSource/eslint-plugin-sonarjs) [![Quality Gate](https://sonarcloud.io/api/project_badges/measure?project=eslint-plugin-sonarjs&metric=alert_status)](https://sonarcloud.io/dashboard?id=eslint-plugin-sonarjs) [![Coverage](https://sonarcloud.io/api/project_badges/measure?project=eslint-plugin-sonarjs&metric=coverage)](https://sonarcloud.io/dashboard?id=eslint-plugin-sonarjs)
2
3SonarJS rules for ESLint to help developers produce [Clean Code](https://www.sonarsource.com/solutions/clean-code/) by detecting bugs and suspicious patterns.
4
5## Rules
6
7### Bug Detection :bug:
8
9Rules in this category aim to find places in code that have a high chance of being bugs, i.e. don't work as intended.
10
11- All branches in a conditional structure should not have exactly the same implementation ([`no-all-duplicated-branches`])
12- Collection elements should not be replaced unconditionally ([`no-element-overwrite`])
13- Empty collections should not be accessed or iterated ([`no-empty-collection`])
14- Function calls should not pass extra arguments ([`no-extra-arguments`])
15- Related "if/else if" statements should not have the same condition ([`no-identical-conditions`])
16- Identical expressions should not be used on both sides of a binary operator ([`no-identical-expressions`])
17- Return values from functions without side effects should not be ignored ([`no-ignored-return`]) (_uses-types_)
18- Loops with at most one iteration should be refactored ([`no-one-iteration-loop`])
19- The output of functions that don't return anything should not be used ([`no-use-of-empty-return-value`])
20- Non-existent operators '=+', '=-' and '=!' should not be used ([`non-existent-operator`]) (:wrench: _fixable_)
21
22### Code Smell Detection :pig:
23
24Code Smells, or maintainability issues, are raised for places of code which might be costly to change in the future. These rules also help to keep the high code quality and readability. And finally, some rules report issues on different suspicious code patterns.
25
26- Cognitive Complexity of functions should not be too high ([`cognitive-complexity`])
27- "if ... else if" constructs should end with "else" clauses ([`elseif-without-else`]) (_disabled_)
28- "switch" statements should not have too many "case" clauses ([`max-switch-cases`])
29- Collapsible "if" statements should be merged ([`no-collapsible-if`])
30- Collection sizes and array length comparisons should make sense ([`no-collection-size-mischeck`]) (:wrench: _fixable_, _uses-types_)
31- String literals should not be duplicated ([`no-duplicate-string`])
32- Two branches in a conditional structure should not have exactly the same implementation ([`no-duplicated-branches`])
33- Boolean expressions should not be gratuitous ([`no-gratuitous-expressions`])
34- Functions should not have identical implementations ([`no-identical-functions`])
35- Boolean checks should not be inverted ([`no-inverted-boolean-check`]) (:wrench: _fixable_, _disabled_)
36- "switch" statements should not be nested ([`no-nested-switch`])
37- Template literals should not be nested ([`no-nested-template-literals`])
38- Boolean literals should not be redundant ([`no-redundant-boolean`])
39- Jump statements should not be redundant ([`no-redundant-jump`]) (:wrench: _fixable_)
40- Conditionals should start on new lines ([`no-same-line-conditional`]) (:wrench: _fixable_)
41- "switch" statements should have at least 3 "case" clauses ([`no-small-switch`])
42- Collection and array contents should be used ([`no-unused-collection`])
43- "catch" clauses should do more than rethrow ([`no-useless-catch`])
44- Local variables should not be declared and then immediately returned or thrown ([`prefer-immediate-return`]) (:wrench: _fixable_)
45- Object literal syntax should be used ([`prefer-object-literal`])
46- Return of boolean expressions should not be wrapped into an "if-then-else" statement ([`prefer-single-boolean-return`]) (:wrench: _fixable_)
47- A "while" loop should be used instead of a "for" loop ([`prefer-while`]) (:wrench: _fixable_)
48
49[`cognitive-complexity`]: ./docs/rules/cognitive-complexity.md
50[`elseif-without-else`]: ./docs/rules/elseif-without-else.md
51[`max-switch-cases`]: ./docs/rules/max-switch-cases.md
52[`no-all-duplicated-branches`]: ./docs/rules/no-all-duplicated-branches.md
53[`no-collapsible-if`]: ./docs/rules/no-collapsible-if.md
54[`no-collection-size-mischeck`]: ./docs/rules/no-collection-size-mischeck.md
55[`no-duplicate-string`]: ./docs/rules/no-duplicate-string.md
56[`no-duplicated-branches`]: ./docs/rules/no-duplicated-branches.md
57[`no-element-overwrite`]: ./docs/rules/no-element-overwrite.md
58[`no-empty-collection`]: ./docs/rules/no-empty-collection.md
59[`no-extra-arguments`]: ./docs/rules/no-extra-arguments.md
60[`no-gratuitous-expressions`]: ./docs/rules/no-gratuitous-expressions.md
61[`no-identical-conditions`]: ./docs/rules/no-identical-conditions.md
62[`no-identical-expressions`]: ./docs/rules/no-identical-expressions.md
63[`no-identical-functions`]: ./docs/rules/no-identical-functions.md
64[`no-ignored-return`]: ./docs/rules/no-ignored-return.md
65[`no-inverted-boolean-check`]: ./docs/rules/no-inverted-boolean-check.md
66[`no-nested-switch`]: ./docs/rules/no-nested-switch.md
67[`no-nested-template-literals`]: ./docs/rules/no-nested-template-literals.md
68[`no-one-iteration-loop`]: ./docs/rules/no-one-iteration-loop.md
69[`no-redundant-boolean`]: ./docs/rules/no-redundant-boolean.md
70[`no-redundant-jump`]: ./docs/rules/no-redundant-jump.md
71[`no-same-line-conditional`]: ./docs/rules/no-same-line-conditional.md
72[`no-small-switch`]: ./docs/rules/no-small-switch.md
73[`no-use-of-empty-return-value`]: ./docs/rules/no-use-of-empty-return-value.md
74[`no-unused-collection`]: ./docs/rules/no-unused-collection.md
75[`no-useless-catch`]: ./docs/rules/no-useless-catch.md
76[`non-existent-operator`]: ./docs/rules/non-existent-operator.md
77[`prefer-immediate-return`]: ./docs/rules/prefer-immediate-return.md
78[`prefer-object-literal`]: ./docs/rules/prefer-object-literal.md
79[`prefer-single-boolean-return`]: ./docs/rules/prefer-single-boolean-return.md
80[`prefer-while`]: ./docs/rules/prefer-while.md
81
82## Prerequisites
83
84- Node.js (>=16.x).
85- ESLint 8.x or 9.x (peer dependency for the plugin).
86
87## Usage
88
89- If you don't have ESLint yet configured for your project, follow [these instructions](https://github.com/eslint/eslint#installation-and-usage).
90- Install `eslint-plugin-sonarjs` using `npm` (or `yarn`) for your project or globally:
91
92```sh
93npm install eslint-plugin-sonarjs --save-dev # install for your project
94npm install eslint-plugin-sonarjs -g # or install globally
95```
96
97- Add `eslint-plugin-sonarjs` to the plugins of your ESLint config.
98
99For ESLint 9: add `plugins` option to your `eslint.config.js` and include the recommended config to enable all rules:
100
101```code
102import sonarjs from "eslint-plugin-sonarjs";
103
104[
105 sonarjs.configs.recommended,
106 {
107 "plugins": {
108 sonarjs,
109 }
110 }
111]
112```
113
114For ESLint 8: add `plugins` option to your `.eslintrc` and `plugin:sonarjs/recommended` to the `extends` option to enable all recommended rules:
115
116```json
117{
118 "plugins": ["sonarjs"],
119 "extends": ["plugin:sonarjs/recommended-legacy"]
120}
121```
122
123- or enable only some rules manually:
124
125```json
126{
127 "rules": {
128 "sonarjs/cognitive-complexity": "error",
129 "sonarjs/no-identical-expressions": "error"
130 // etc.
131 }
132}
133```
134
135- To allow each of the rules to fully perform, use `@typescript-eslint/parser` as a parser for ESLint ([like we do](https://github.com/SonarSource/eslint-plugin-sonarjs/blob/6e06d59a233e07b28fbbd6398e08b9b0c63b18f9/.eslintrc.js#L4)) and set the [parserOptions.project](https://github.com/typescript-eslint/typescript-eslint/tree/master/packages/parser#parseroptionsproject) option. Thanks to it, type information is available, which is beneficial or even essential for some rules.
136
137## Available Configurations
138
139This plugin provides only a `recommended` configuration. Almost all rules are activated in this profile with a few exceptions (check the `disabled` tag in the rules list). The `recommended` configuration activates rules with `error` severity.
140We include a `recommended-legacy` configuration to be backward compatible with ESLint v8, with the same rule configuration..
141
142## ESLint and Sonar
143
144This plugin exposes to ESLint users a subset of JS/TS rules from Sonar-\* products (aka [SonarJS](https://github.com/SonarSource/SonarJS)). We extracted the rules that are not available in ESLint core or other ESLint plugins to be beneficial for the ESLint community.
145
146If you are a [SonarQube](https://www.sonarqube.org) or [SonarCloud](https://sonarcloud.io) user, to lint your code locally, we suggest using [SonarLint](https://www.sonarlint.org) IDE extension (available for VSCode, JetBrains IDEs and Eclipse). You can connect SonarLint to your SonarQube/SonarCloud project to synchronize rules configuration, issue statuses, etc.
147
148## Contributing
149
150Do you want to participate in the development of the project? Have a look at our [contributing](./docs/CONTRIBUTING.md) guide!