SonarJS rules for ESLint to detect bugs and suspicious patterns in your code.
Bug Detection 🐛
Rules in this category aim to find places in code which have a high chance of being bugs, i.e. don't work as intended.
- All branches in a conditional structure should not have exactly the same implementation (
- Collection elements should not be replaced unconditionally (
- Function calls should not pass extra arguments (
- Related "if/else if" statements should not have the same condition (
- Identical expressions should not be used on both sides of a binary operator (
- Loops with at most one iteration should be refactored (
- The output of functions that don't return anything should not be used (
Code Smell Detection 🐷
Code 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.
- Cognitive Complexity of functions should not be too high (
- "switch" statements should not have too many "case" clauses (
- Collapsible "if" statements should be merged (
- Collection sizes and array length comparisons should make sense (
- String literals should not be duplicated (
- Two branches in a conditional structure should not have exactly the same implementation (
- Functions should not have identical implementations (
- Boolean checks should not be inverted (
- Boolean literals should not be redundant (
- Jump statements should not be redundant (
- Conditionals should start on new lines (
- "switch" statements should have at least 3 "case" clauses (
- Collection and array contents should be used (
- "catch" clauses should do more than rethrow (
- Local variables should not be declared and then immediately returned or thrown (
prefer-immediate-return) (🔧 fixable)
- Object literal syntax should be used (
- Return of boolean expressions should not be wrapped into an "if-then-else" statement (
- A "while" loop should be used instead of a "for" loop (
prefer-while) (🔧 fixable)
- If you don't have ESLint yet configured for your project, follow these instructions.
yarn) for you project or globally:
npm install eslint-plugin-sonarjs --save-dev # install for your projectnpm install eslint-plugin-sonarjs -g # or install globally
pluginsoption of your
extendsoption to enable all recommended rules:
- or enable only some rules manually:
You want to participate in the development of the project? Have a look at our contributing guide!