SonarJS rules for ESLint
LGPL-3.0 License
This repository contains eslint-plugin-sonarjs
up to version ^1.0.0
. For versions >=2.0.0
please go to the repository of the SonarJS analyzer. The new versions of eslint-plugin-sonarjs
makes all SonarJS rules available for ESLint users, instead of a subset as it was with ^1.0.0
living here.
No new changes are expected to land here.
SonarJS rules for ESLint to help developers produce Clean Code by detecting bugs and suspicious patterns.
Rules in this category aim to find places in code that have a high chance of being bugs, i.e. don't work as intended.
no-all-duplicated-branches
)no-element-overwrite
)no-empty-collection
)no-extra-arguments
)no-identical-conditions
)no-identical-expressions
)no-ignored-return
) (uses-types)no-one-iteration-loop
)no-use-of-empty-return-value
)non-existent-operator
) (🔧 fixable)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 patterns.
cognitive-complexity
)elseif-without-else
) (disabled)max-switch-cases
)no-collapsible-if
)no-collection-size-mischeck
) (🔧 fixable, uses-types)no-duplicate-string
)no-duplicated-branches
)no-gratuitous-expressions
)no-identical-functions
)no-inverted-boolean-check
) (🔧 fixable, disabled)no-nested-switch
)no-nested-template-literals
)no-redundant-boolean
)no-redundant-jump
) (🔧 fixable)no-same-line-conditional
) (🔧 fixable)no-small-switch
)no-unused-collection
)no-useless-catch
)prefer-immediate-return
) (🔧 fixable)prefer-object-literal
)prefer-single-boolean-return
) (🔧 fixable)prefer-while
) (🔧 fixable)eslint-plugin-sonarjs
using npm
(or yarn
) for your project or globally:npm install eslint-plugin-sonarjs --save-dev # install for your project
npm install eslint-plugin-sonarjs -g # or install globally
eslint-plugin-sonarjs
to the plugins of your ESLint config.For ESLint 9: add plugins
option to your eslint.config.js
and include the recommended config to enable all rules:
import sonarjs from "eslint-plugin-sonarjs";
[
sonarjs.configs.recommended,
{
"plugins": {
sonarjs,
}
}
]
For ESLint 8: add plugins
option to your .eslintrc
and plugin:sonarjs/recommended-legacy
to the extends
option to enable all recommended rules:
{
"plugins": ["sonarjs"],
"extends": ["plugin:sonarjs/recommended-legacy"]
}
{
"rules": {
"sonarjs/cognitive-complexity": "error",
"sonarjs/no-identical-expressions": "error"
// etc.
}
}
@typescript-eslint/parser
as a parser for ESLint (like we do) and set the parserOptions.project option. Thanks to it, type information is available, which is beneficial or even essential for some rules.This 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.
We include a recommended-legacy
configuration to be backward compatible with ESLint v8, with the same rule configuration..
This plugin exposes to ESLint users a subset of JS/TS rules from Sonar-* products (aka SonarJS). We extracted the rules that are not available in ESLint core or other ESLint plugins to be beneficial for the ESLint community.
If you are a SonarQube or SonarCloud user, to lint your code locally, we suggest using SonarLint 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.
Do you want to participate in the development of the project? Have a look at our contributing guide!