You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/DEV.md
+15-30Lines changed: 15 additions & 30 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -91,66 +91,51 @@ When using this for the ruling tests, make sure that you run them in series (and
91
91
1. Create a PR with a rule description in RSPEC repo like described [here](https://github.com/SonarSource/rspec#create-or-modify-a-rule)
92
92
93
93
- 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`).
94
96
95
97
2. Link this RSPEC PR to the implementation issue in this repo
96
98
3. Make sure the implementation issue title contains the RSPEC number and name
97
99
98
100
### Implementing a rule
99
101
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)
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
109
103
110
104
```sh
111
105
npm run new-rule
112
106
```
113
107
114
-
This script:
108
+
This script will ask a few questions and:
115
109
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
123
115
124
116
It will also update some files which are not tracked by Git as they are automatically generated:
125
117
118
+
- generates a Java check class for the rule `SXXXX.java`
126
119
- updates the `rules/rules.ts` file to include the new rule
127
120
- updates the `rules/plugin-rules.ts` file to include the new rule
128
121
- updates the `AllRules.java` to include the new rule
129
122
130
-
3. Update generated files
123
+
2. Update generated files
131
124
- Make sure annotations in the Java class specify languages to cover (`@JavaScriptRule` and/or `@TypeScriptRule`)
132
125
- If your rule has configurations, or you are using some from an ESLint rule, override the `configurations()` method of the Java check class
133
126
- 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)
134
127
- 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`
143
129
- 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).
144
130
- If writing a regex rule, use [createRegExpRule](https://github.com/SonarSource/SonarJS/blob/master/src/linting/eslint/rules/helpers/regex/rule-template.ts#L52)
145
131
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`.
149
134
- 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.
150
135
151
136
## Testing a rule
152
137
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.
0 commit comments