Skip to content

Commit 9e8c582

Browse files
authored
JS-947 Update DEV.md (#5924)
1 parent 744eb91 commit 9e8c582

File tree

1 file changed

+15
-30
lines changed

1 file changed

+15
-30
lines changed

docs/DEV.md

Lines changed: 15 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -91,66 +91,51 @@ When using this for the ruling tests, make sure that you run them in series (and
9191
1. Create a PR with a rule description in RSPEC repo like described [here](https://github.com/SonarSource/rspec#create-or-modify-a-rule)
9292

9393
- Tag the RSPEC with `type-dependent` if the rule relies partially or fully on type information
94+
- Add a field `dependencies` if your rule should only be executed if it relies on a specific import (example: 'react' or 'jest')
95+
- Add a field `compatibleLanguages` with an array including, which languages you support (`js` and/or `ts`).
9496

9597
2. Link this RSPEC PR to the implementation issue in this repo
9698
3. Make sure the implementation issue title contains the RSPEC number and name
9799

98100
### Implementing a rule
99101

100-
1. Generate rule metadata (JSON and HTML files) from [RSPEC](https://github.com/SonarSource/rspec#4-implement-the-rule), by running this command from the project's root:
101-
102-
- to obtain the 'rule-api-[RELEASE].jar', see here [sonar-rule-api repo](https://github.com/SonarSource/sonar-rule-api)
103-
104-
```sh
105-
java -jar <location of rule-api jar> generate -rule S1234 [-branch <RSPEC branch>]
106-
```
107-
108-
2. Generate other files required for a new rule. Just choose your options in the prompt of the `new-rule` script
102+
1. Generate other files required for a new rule. Just choose your options in the prompt of the `new-rule` script
109103

110104
```sh
111105
npm run new-rule
112106
```
113107

114-
This script:
108+
This script will ask a few questions and:
115109

116-
- generates a Java check class for the rule `S1234.java`
117-
- generates a Java check test class for the rule `S1234Test.java`
118-
- generates a `rules/S1234` folder
119-
- generates a `rules/S1234/index.ts` rule index file
120-
- generates a `rules/S1234/rule.ts` file for the rule implementation
121-
- generates a `rules/S1234/cb.fixture.js` comment-based test file (empty)
122-
- generates a `rules/S1234/cb.test.js` test launcher
110+
- generates a `rules/SXXXX` folder
111+
- generates a `rules/SXXXX/index.ts` rule index file
112+
- generates a `rules/SXXXX/rule.ts` file for the rule implementation
113+
- generates a `rules/SXXXX/cb.fixture.js` comment-based test file (empty)
114+
- generates a `rules/SXXXX/cb.test.js` test launcher
123115

124116
It will also update some files which are not tracked by Git as they are automatically generated:
125117

118+
- generates a Java check class for the rule `SXXXX.java`
126119
- updates the `rules/rules.ts` file to include the new rule
127120
- updates the `rules/plugin-rules.ts` file to include the new rule
128121
- updates the `AllRules.java` to include the new rule
129122

130-
3. Update generated files
123+
2. Update generated files
131124
- Make sure annotations in the Java class specify languages to cover (`@JavaScriptRule` and/or `@TypeScriptRule`)
132125
- If your rule has configurations, or you are using some from an ESLint rule, override the `configurations()` method of the Java check class
133126
- You can use a `MyRuleCheckTest.java` test case to verify how the configurations will be serialized to JSON as shown [here](https://github.com/SonarSource/SonarJS/blob/master/sonar-plugin/javascript-checks/src/test/java/org/sonar/javascript/checks/NoEmptyClassCheckTest.java#L30)
134127
- If writing a rule for the test files, replace `extends Check` with `extends TestFileCheck` in the Java class. This will be done by the `new-rule` script, but make sure you are extending the right base class.
135-
- In the generated metadata JSON file `javascript-checks/src/main/resources/org/sonar/l10n/javascript/rules/javascript/S1234.json`, add (one or both):
136-
```json
137-
"compatibleLanguages": [
138-
"JAVASCRIPT",
139-
"TYPESCRIPT"
140-
]
141-
```
142-
4. Implement the rule logic in `S1234/rule.ts`
128+
3. Implement the rule logic in `S1234/rule.ts`
143129
- Prefer using `meta.messages` to specify messages through `messageId`s. Message can be part of the RSPEC description, like [here](https://sonarsource.github.io/rspec/#/rspec/S4036/javascript#message).
144130
- If writing a regex rule, use [createRegExpRule](https://github.com/SonarSource/SonarJS/blob/master/src/linting/eslint/rules/helpers/regex/rule-template.ts#L52)
145131

146-
5. If possible, implement quick fixes for the rule:
147-
- Add its rule key in `src/linter/quickfixes/rules.ts`
148-
- If the ESLint fix is at the root of the report (and not in a suggestion), add the message for the quick fix in `src/linter/quickfixes/messages.ts`.
132+
4. If possible, implement quick fixes for the rule:
133+
- If the ESLint fix is at the root of the report (and not in a suggestion), add the message for the quick fix in `rules/SXXXX/meta.ts`.
149134
- Add a code fixture that should provide a quickfix in `tests/linter/fixtures/wrapper/quickfixes/<ESLint-style rulekey>.{js,ts}`. The [following test](https://github.com/SonarSource/SonarJS/blob/a99fd9614c4ee3052f8da1cfecbfc05ef16e95d1/tests/linting/eslint/linter/wrapper.test.ts#L334) asserts that the quickfix is enabled.
150135

151136
## Testing a rule
152137

153-
We support 2 kinds of rule unit-tests: ESLint's [RuleTester](https://eslint.org/docs/developer-guide/nodejs-api#ruletester) or our comment-based tests. Prefer comment-based tests as they are more readable!
138+
We support 2 kinds of rule unit-tests: ESLint's [RuleTester](https://eslint.org/docs/developer-guide/nodejs-api#ruletester) or our comment-based tests.
154139

155140
### Comment-based testing
156141

0 commit comments

Comments
 (0)