Please read
common CONTRIBUTING.md
in ecoCode-common
repository first.
Also check the starter pack to
have the basic information before starting.
The JavaScript plugin is divided into 2 modules:
eslint-plugin
contains the ESLint plugin partsonar-plugin
contains the SonarQube plugin implementation
The ESLint plugin can be standalone and is packed inside SonarQube plugin during the build phase.
- Java JDK 11+
- Maven 3.8 or later
SonarQube provides a documentation to learn plugin basics.
- Node.js 18.x or newer
- Yarn (installation guide)
- ESLint plugin maintenance skills
- ESLint custom rule creation skills
The ESLint documentation is very detailed and provides a useful starting point.
Check more here: https://eslint.org/docs/latest/extend/custom-rules
It is possible to write and test the rules directly in this project which provides unit tests.
But it can be useful to prepare a test project to check the correct execution of the rules.
- Clone the Git repository
- Run
yarn install
inside eslint-plugin directory - Synchronize dependencies using Maven inside sonar-plugin directory
- You are good to go! 🚀
Before even starting to implement the rule, it must have been discussed, mesured and documented in the ecoCode referential. Please follow the dedicated documentation to find out how to proceed.
The file structure inside eslint-plugin package provides an ESLint-compliant project.
So, the rule creation follows the recommended standards.
It may be useful to take a look at rules already written to use the same structure and conventions.
Also please provide license header inside files with source code (there is an example inside filelib/rule-list.js
).
Example set of files to add a rule called "my-awesome-rule":
lib/rules/my-awesome-rule.js
: main file with the rule metadata and algorithmdocs/rules/my-awesome-rule.md
: documentation file written in Markdown to explain the ruletests/lib/rules/my-awesome-rule.js
: testing file written using ESLint RuleTester
The project itself uses ESLint to helps linting rule algorithms.
Now that the rule has been implemented in the ecoCode referential and its implementation written in the ESLint plugin, all that remains is to reference it in the SonarQube plugin. Here are the simple steps:
- Create a Java class in
src/main/java/io/ecocode/javascript/checks/
with the declaration of the SonarQube key and the ESLint key (check other classes to have an example) - Reference created in method
getAllChecks()
of Java classCheckList
(please use alphabetical order) - Add the SonarQube key of the rule in the appropriate profile Json file.
Rule tests also follow the ESLint standards and
use ESLint RuleTester.
They are executed with the mocha test framework.
Please add as many valid and invalid uses cases as necessary for a each rule.
This will allow a large code coverage and avoid false positives.
Run the following script to start all test suites: yarn run test
.
To display tests coverage, use yarn run test:cov
.
ℹ You don't need to add a test in the Sonar plugin, the class will be tested automatically.
A tool called eslint-doc-generator is used in the project to update
documentation of rules.
It allows to update rule doc file and main README based on metadata of rules.
Two npm scripts are available:
yarn run update:eslint-docs
to update Markdown files with data of rulesyarn run lint:eslint-docs
to verify auto-updated data of rules in Markdown files
All the generated code is between commented lines with "auto-generated" in the text.
Please run update script after a rule creation.
The last step is to open a PR on this project with the implementation of the rule, and a second one on the test project with an exemple of a code not following rule needs. Keep an eye on the coverage of your rule implementation 👀
This is the end of this guide, thank you for reading this far and contributing to the project 🙏.