Giter VIP home page Giter VIP logo

eslint-plugin-sonarjs's Introduction

eslint-plugin-sonarjs npm version Build Status Quality Gate Coverage

SonarJS rules for ESLint to help developers produce Clean Code by detecting bugs and suspicious patterns.

Rules

Bug Detection πŸ›

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.

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 patterns.

Prerequisites

  • Node.js (>=16.x).
  • ESLint 5.x, 6.x, 7.x or 8.x (peer dependency for the plugin).

Usage

  • If you don't have ESLint yet configured for your project, follow these instructions.
  • Install 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
  • Add eslint-plugin-sonarjs to the plugins option of your .eslintrc:
{
  "plugins": ["sonarjs"]
}
  • Add plugin:sonarjs/recommended to the extends option to enable all recommended rules:
{
  "extends": ["plugin:sonarjs/recommended"]
}
  • or enable only some rules manually:
{
  "rules": {
    "sonarjs/cognitive-complexity": "error",
    "sonarjs/no-identical-expressions": "error"
    // etc.
  }
}
  • To enable all rules of this plugin, use @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.

Available Configurations

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.

ESLint and Sonar

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.

Contributing

Do you want to participate in the development of the project? Have a look at our contributing guide!

eslint-plugin-sonarjs's People

Contributors

andrea-guarino-sonarsource avatar brettz9 avatar david-cho-lerat-sonarsource avatar dependabot[bot] avatar elratondefuego avatar ericmorand-sonarsource avatar ferhatelmas avatar francoismora avatar godin avatar ilia-kebets-sonarsource avatar jounqin avatar julien-carsique-sonarsource avatar malena-ebert-sonarsource avatar mcmxcdev avatar rap2hpoutre avatar saberduck avatar slapbox avatar stas-vilchik avatar tomverin avatar vdiez avatar victor-diez-sonarsource avatar vilchik-elena avatar vlad-sonar avatar xiaoxiangmoe avatar yassin-kammoun-sonarsource avatar zglicz avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

eslint-plugin-sonarjs's Issues

"Sonar way Recommended" rules

Bug Detection πŸ›

  • RSPEC-930 Function calls should not pass extra arguments
  • RSPEC-1751 Loops with at most one iteration should be refactored
  • RSPEC-1764 Identical expressions should not be used on both sides of a binary operator (partially covered by no-self-compare)
  • RSPEC-1862 Related "if/else if" statements and "cases" in a "switch" should not have the same condition (partially covered by no-duplicate-case)
  • RSPEC-2123 Values should not be uselessly incremented
  • RSPEC-2201 Return values from functions without side effects should not be ignored
  • RSPEC-2259 Properties of variables with "null" or "undefined" values should not be accessed
  • RSPEC-2432 Setters should not return values
  • RSPEC-2583 Conditionally executed blocks should be reachable
  • RSPEC-2757 Non-existent operators '=+', '=-' and '=!' should not be used
  • RSPEC-2999 "new" operators should be used with functions
  • RSPEC-3403 Strict equality operators should not be used with dissimilar types
  • RSPEC-3616 Comma and logical OR operators should not be used in switch cases
  • RSPEC-3699 The output of functions that don't return anything should not be used
  • RSPEC-3759 Non-existent properties should not be read
  • RSPEC-3760 Arithmetic operators should only have numbers as operands
  • RSPEC-3785 "in" should not be used with primitive types
  • RSPEC-3923 All branches in a conditional structure should not have exactly the same implementation
  • RSPEC-3981 Collection sizes and array length comparisons should make sense
  • RSPEC-4043 Array-mutating methods should not be used misleadingly
  • RSPEC-4143 Collection elements should not be replaced unconditionally
  • RSPEC-4412 Arrays should not be passed as rest arguments

Code Smell Detection 🐷

  • RSPEC-101 Class names should comply with a naming convention
  • RSPEC-125 Sections of code should not be "commented out"
  • RSPEC-1125 Boolean literals should not be redundant
  • RSPEC-1126 Return of boolean expressions should not be wrapped into an "if-then-else" statement
  • RSPEC-1264 A "while" loop should be used instead of a "for" loop
  • RSPEC-1301 "switch" statements should have at least 3 "case" clauses
  • RSPEC-1472 Function call arguments should not start on new lines
  • RSPEC-1488 Local variables should not be declared and then immediately returned or thrown
  • RSPEC-1528 Array constructors should not be used (partially covered by no-array-constructor)
  • RSPEC-3626 Jump statements should not be redundant
  • RSPEC-1854 Dead stores should be removed
  • RSPEC-1871 Two branches in a conditional structure should not have exactly the same implementation
  • RSPEC-1994 "for" loop increment clauses should modify the loops' counters
  • RSPEC-2310 Loop counters should not be assigned to from within the loop body
  • RSPEC-2589 Boolean expressions should not be gratuitousΒ 
  • RSPEC-2681 Multiline blocks should be enclosed in curly braces
  • RSPEC-2692 "indexOf" checks should not be for positive numbers
  • RSPEC-2870 "delete" should not be used on arrays
  • RSPEC-3002 Unary operators "+" and "-" should not be used with objects
  • RSPEC-3003 Comparison operators should not be used with strings
  • RSPEC-3509 Default parameters should not cause side effects
  • RSPEC-3516 Function returns should not be invariant
  • RSPEC-3579 Array indexes should be numeric
  • RSPEC-3686 Functions should not be called both with and without "new"
  • RSPEC-3757 Arithmetic operations should not result in "NaN"
  • RSPEC-3758 Values not convertible to numbers should not be used in numeric comparisons
  • RSPEC-3776 Cognitive Complexity of functions should not be too high
  • RSPEC-3782 Arguments to built-in functions should match documented types
  • RSPEC-3800 Functions should always return the same type
  • RSPEC-3972 Conditionals should start on new lines
  • RSPEC-3973 Conditionally executed code should be denoted by either indentation or curly braces
  • RSPEC-4030 Collection and array contents should be used
  • RSPEC-4123 "await" should only be used with promises
  • RSPEC-4139 "for in" should not be used with iterables
  • RSPEC-4144 Functions should not have identical implementations
  • RSPEC-4165 Assignments should not be redundant

Plugin do not scan my project and rules are not applied

My current version of node is 8.12.0.
I've been successfully installed npm install eslint-plugin-sonarjs, then import at my plugins "sonarjs" located in .eslintrc file, and finally at extends "plugin:sonarjs/recommended" but there are no rules applied to my code from linter.

Incorrect error. Function doesn't return anything

I want to report a bug

Remove this use of the output from "flat"; "flat" doesn't return anything.

Reproducer

function getIds(objects) {
  function* flat(items) {
      for (const item of items) {
        yield* flat(item.children);
        yield item.id;
      }
    }
    return Array.from(flat(objects));
}

Expected behavior

No error

0.3

Bug Detection πŸ›

  • RSPEC-2201 Return values from functions without side effects should not be ignored
  • RSPEC-3616 Comma and logical OR operators should not be used in switch cases
  • RSPEC-2432 Setters should not return values
  • RSPEC-2123 Values should not be uselessly incremented
  • RSPEC-3828 "yield" expressions should not be used outside generators
  • RSPEC-3981 Collection sizes and array length comparisons should make sense
  • πŸ“‘ RSPEC-2189 Loops should not be infinite

Code Smell Detection 🐷

  • RSPEC-125 Sections of code should not be "commented out"
  • RSPEC-1264 A "while" loop should be used instead of a "for" loop
  • RSPEC-1472 Function call arguments should not start on new lines
  • RSPEC-1528 Array constructors should not be used (partially covered by no-array-constructor)
  • 🍭 RSPEC-3626 Jump statements should not be redundant
  • πŸ“‘ 🍭 RSPEC-1854 Dead stores should be removed
  • πŸ“‘ RSPEC-2310 Loop counters should not be assigned to from within the loop body
  • RSPEC-3509 Default parameters should not cause side effects
  • RSPEC-3972 Conditionals should start on new lines
  • ? RSPEC-4030 Collection and array contents should be used

🍭 - CFG
πŸ“‘ - ST

Release 0.1

Bug Detection πŸ›

  • πŸ“‘ RSPEC-930 Function calls should not pass extra arguments
  • RSPEC-1764 Identical expressions should not be used on both sides of a binary operator (partially covered by no-self-compare)
  • RSPEC-1862 Related "if/else if" statements and "cases" in a "switch" should not have the same condition (partially covered by no-duplicate-case, so implement only for ifs)
  • πŸ“‘ RSPEC-3699 The output of functions that don't return anything should not be used
  • RSPEC-3923 All branches in a conditional structure should not have exactly the same implementation
  • RSPEC-4143 Collection elements should not be replaced unconditionally

Code Smell Detection 🐷

  • RSPEC-1125 Boolean literals should not be redundant (partially covered by no-unneeded-ternary, so implement only for ifs)
  • RSPEC-1126 Return of boolean expressions should not be wrapped into an "if-then-else" statement
  • RSPEC-1301 "switch" statements should have at least 3 "case" clauses
  • RSPEC-1488 Local variables should not be declared and then immediately returned or thrown
  • 🍭 RSPEC-1751 Jump statements should not be used unconditionally
  • RSPEC-1871 Two branches in a conditional structure should not have exactly the same implementation
  • RSPEC-3776 Cognitive Complexity of functions should not be too high
  • RSPEC-4144 Functions should not have identical implementations

🍭 - CFG
πŸ“‘ - ST

ESLint 5 support

I want to request a feature.

ESLint 5 isn't supported but it seems no issues and can be enabled.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.