UNPKG

8.72 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 detect bugs and suspicious patterns in your code.
4
5## Rules
6
7### Bug Detection :bug:
8
9Rules in this category aim to find places in code which 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 patters.
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 (>=12.x).
85* ESLint 5.x, 6.x, 7.x or 8.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 you 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` option of your `.eslintrc`:
98
99```json
100{
101 "plugins": ["sonarjs"]
102}
103```
104
105* Add `plugin:sonarjs/recommended` to the `extends` option to enable all recommended rules:
106
107```json
108{
109 "extends": ["plugin:sonarjs/recommended"]
110}
111```
112
113* or enable only some rules manually:
114
115```javascript
116{
117 "rules": {
118 "sonarjs/cognitive-complexity": "error",
119 "sonarjs/no-identical-expressions": "error"
120 // etc.
121 }
122}
123```
124* To enable all rules of this plugin, 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.
125
126## Available Configurations
127
128This plugin provides only `recommended` configuration. Almost all rules are activated in this profile with a few exceptions (check `disabled` tag in the rules list). `recommended` configuration activates rules with `error` severity.
129
130## ESLint and Sonar
131
132This 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 which are not available in ESLint core or other ESLint plugins to be beneficial for ESLint community.
133
134If you are a [SonarQube](https://www.sonarqube.org) or [SonarCloud](https://sonarcloud.io) user, to lint your code locally, we suggest to use [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.
135
136## Contributing
137
138You want to participate in the development of the project? Have a look at our [contributing](./docs/CONTRIBUTING.md) guide!