@@ -9,7 +9,7 @@ This document describes the recommended approach for creating custom rules that
99The ` EslintHook ` interface is the modern way to define custom rules in SonarJS. Rules implementing this interface:
1010
1111- ** Can raise Sonar issues** when registered via ` CustomRuleRepository `
12- - ** Are activated through quality profiles** - only run when enabled by users
12+ - ** Are activated through quality profiles** - by default, run only when enabled in the active quality profile
1313- ** Provide full control** over analysis modes, file types, and configurations
1414
1515## Components
@@ -21,6 +21,7 @@ A complete custom rules integration requires:
21213 . ** Rules Bundle** - packages the ESLint-side JavaScript code
22224 . ** Rules Definition** - defines rule metadata for SonarQube
23235 . ** Plugin Class** - registers all components
24+ 6 . ** Default Profile Registrar (optional)** - implements ` ProfileRegistrar ` to activate rules in built-in default profile (` Sonar way ` )
2425
2526## Implementation Guide
2627
@@ -284,6 +285,51 @@ Add the SonarJS API dependency to your `pom.xml`:
284285</dependency >
285286```
286287
288+ ### 9. Optional: Activate Rules in the Default Built-In Profile
289+
290+ If you want your rules to be active by default in the built-in default quality profile (` Sonar way ` ), implement ` ProfileRegistrar ` :
291+
292+ ``` java
293+ package com.example.plugin ;
294+
295+ import java.util.List ;
296+ import org.sonar.api.rule.RuleKey ;
297+ import org.sonar.plugins.javascript.api.Language ;
298+ import org.sonar.plugins.javascript.api.ProfileRegistrar ;
299+
300+ public class MyProfileRegistrar implements ProfileRegistrar {
301+
302+ @Override
303+ public void register (RegistrarContext registrarContext ) {
304+ registrarContext. registerDefaultQualityProfileRules(
305+ Language . JAVASCRIPT ,
306+ List . of(RuleKey . of(MyRuleRepository . REPOSITORY_KEY , " S1234" ))
307+ );
308+ registrarContext. registerDefaultQualityProfileRules(
309+ Language . TYPESCRIPT ,
310+ List . of(RuleKey . of(MyRuleRepository . REPOSITORY_KEY , " S1234" ))
311+ );
312+ }
313+ }
314+ ```
315+
316+ Register it in your plugin:
317+
318+ ``` java
319+ context. addExtensions(
320+ MyRulesBundle . class,
321+ MyRuleRepository . class,
322+ MyRulesDefinition . class,
323+ MyProfileRegistrar . class
324+ );
325+ ```
326+
327+ Notes:
328+
329+ - This is ` @ServerSide ` API and is not used in SonarLint.
330+ - It contributes to the built-in default profile (` Sonar way ` ) only.
331+ - Users can still activate/deactivate your rules in any quality profile.
332+
287333## API Reference
288334
289335### EslintHook Interface
@@ -305,6 +351,13 @@ Add the SonarJS API dependency to your `pom.xml`:
305351| ` checkClasses() ` | List of check classes implementing EslintHook |
306352| ` compatibleLanguages() ` | Languages this repository supports (JS/TS) |
307353
354+ ### ProfileRegistrar Interface (Optional)
355+
356+ | Method | Description |
357+ | ----------------------------------------- | -------------------------------------------------------------------------- |
358+ | ` register(RegistrarContext) ` | Called on server side to contribute additional default-profile activations |
359+ | ` registerDefaultQualityProfileRules(...) ` | Adds rule keys to the built-in default profile (` Sonar way ` ) for JS or TS |
360+
308361## Best Practices
309362
3103631 . ** Use ` @JavaScriptRule ` and ` @TypeScriptRule ` annotations** - These control which languages your rule applies to.
@@ -658,12 +711,14 @@ This is why `CustomRuleRepository` rules can raise issues: their `eslintKey` is
658711
659712### Key Integration Points
660713
661- | Component | File | Purpose |
662- | ------------------------------ | ---------------------------------------------------------- | ------------------------------------ |
663- | ` EslintHook ` | ` sonar-plugin/api/.../EslintHook.java ` | Rule interface |
664- | ` CustomRuleRepository ` | ` sonar-plugin/api/.../CustomRuleRepository.java ` | Interface for rule repositories |
665- | ` RulesBundle ` | ` sonar-plugin/api/.../RulesBundle.java ` | Interface for JS bundle location |
666- | ` JsTsChecks.addCustomChecks() ` | ` sonar-plugin/sonar-javascript-plugin/.../JsTsChecks.java ` | Processes custom repositories |
667- | ` JsTsChecks.doAddChecks() ` | ` sonar-plugin/sonar-javascript-plugin/.../JsTsChecks.java ` | Instantiates checks via CheckFactory |
668- | ` RulesBundles ` | ` sonar-plugin/bridge/.../RulesBundles.java ` | Deploys JS bundles |
669- | ` Linter ` | ` packages/jsts/src/linter/linter.ts ` | Loads and executes rules |
714+ | Component | File | Purpose |
715+ | ------------------------------ | ---------------------------------------------------------------------------- | ------------------------------------- |
716+ | ` EslintHook ` | ` sonar-plugin/api/.../EslintHook.java ` | Rule interface |
717+ | ` CustomRuleRepository ` | ` sonar-plugin/api/.../CustomRuleRepository.java ` | Interface for rule repositories |
718+ | ` ProfileRegistrar ` | ` sonar-plugin/api/.../ProfileRegistrar.java ` | Optional default-profile activations |
719+ | ` RulesBundle ` | ` sonar-plugin/api/.../RulesBundle.java ` | Interface for JS bundle location |
720+ | ` JsTsChecks.addCustomChecks() ` | ` sonar-plugin/sonar-javascript-plugin/.../JsTsChecks.java ` | Processes custom repositories |
721+ | ` JsTsChecks.doAddChecks() ` | ` sonar-plugin/sonar-javascript-plugin/.../JsTsChecks.java ` | Instantiates checks via CheckFactory |
722+ | ` JavaScriptProfilesDefinition ` | ` sonar-plugin/sonar-javascript-plugin/.../JavaScriptProfilesDefinition.java ` | Applies default-profile contributions |
723+ | ` RulesBundles ` | ` sonar-plugin/bridge/.../RulesBundles.java ` | Deploys JS bundles |
724+ | ` Linter ` | ` packages/jsts/src/linter/linter.ts ` | Loads and executes rules |
0 commit comments