1616 */
1717package org .sonar .plugins .javascript ;
1818
19+ import com .google .gson .JsonArray ;
20+ import com .google .gson .JsonObject ;
21+ import com .google .gson .JsonParser ;
22+ import java .io .IOException ;
23+ import java .io .InputStream ;
24+ import java .io .InputStreamReader ;
1925import java .lang .reflect .InvocationTargetException ;
2026import java .lang .reflect .Method ;
27+ import java .nio .charset .StandardCharsets ;
2128import java .util .ArrayList ;
2229import java .util .Collections ;
2330import java .util .EnumMap ;
24- import java .util .HashMap ;
2531import java .util .List ;
2632import java .util .Map ;
2733import java .util .Set ;
@@ -45,15 +51,12 @@ public class JavaScriptProfilesDefinition implements BuiltInQualityProfilesDefin
4551
4652 public static final String RESOURCE_PATH = "org/sonar/l10n/javascript/rules/javascript" ;
4753 public static final String SONAR_WAY_JSON = RESOURCE_PATH + "/Sonar_way_profile.json" ;
54+ public static final String PROFILES_JSON = RESOURCE_PATH + "/profiles.json" ;
4855
49- private static final Map < String , String > PROFILES = new HashMap <> ();
56+ private static final List < ProfileDefinition > PROFILES = loadProfiles ();
5057 static final String SONAR_JASMIN_RULES_CLASS_NAME = "com.sonar.plugins.jasmin.api.JsRules" ;
5158 public static final String SECURITY_RULE_KEYS_METHOD_NAME = "getSecurityRuleKeys" ;
5259
53- static {
54- PROFILES .put (SONAR_WAY , SONAR_WAY_JSON );
55- }
56-
5760 private static final Map <Language , String > REPO_BY_LANGUAGE = new EnumMap <>(Language .class );
5861
5962 static {
@@ -84,15 +87,24 @@ public JavaScriptProfilesDefinition(ProfileRegistrar[] profileRegistrars) {
8487
8588 @ Override
8689 public void define (Context context ) {
87- createSonarWayProfile (JavaScriptLanguage .KEY , context );
88- createSonarWayProfile (TypeScriptLanguage .KEY , context );
90+ createProfiles (JavaScriptLanguage .KEY , context );
91+ createProfiles (TypeScriptLanguage .KEY , context );
8992 }
9093
91- private void createSonarWayProfile (String language , Context context ) {
92- NewBuiltInQualityProfile newProfile = context .createBuiltInQualityProfile (SONAR_WAY , language );
93- activateBuiltInRules (newProfile );
94- activateAdditionalRules (newProfile );
95- activateSecurityRules (newProfile , language );
94+ private void createProfiles (String language , Context context ) {
95+ PROFILES .forEach (profile -> createProfile (profile , language , context ));
96+ }
97+
98+ private void createProfile (ProfileDefinition profile , String language , Context context ) {
99+ NewBuiltInQualityProfile newProfile = context .createBuiltInQualityProfile (
100+ profile .name (),
101+ language
102+ );
103+ activateBuiltInRules (newProfile , profile .path ());
104+ if (SONAR_WAY .equals (profile .name ())) {
105+ activateAdditionalRules (newProfile );
106+ activateSecurityRules (newProfile , language );
107+ }
96108 newProfile .done ();
97109 }
98110
@@ -101,19 +113,57 @@ private void createSonarWayProfile(String language, Context context) {
101113 *
102114 * @param profile profile to activate the rules for
103115 */
104- private static void activateBuiltInRules (NewBuiltInQualityProfile profile ) {
116+ private static void activateBuiltInRules (
117+ NewBuiltInQualityProfile profile ,
118+ String jsonProfilePath
119+ ) {
105120 var language = Language .of (profile .language ());
106121 String repositoryKey = REPO_BY_LANGUAGE .get (language );
107- var jsonProfilePath = PROFILES .get (profile .name ());
122+ Set <String > activeKeys = BuiltInQualityProfileJsonLoader .loadActiveKeysFromJsonProfile (
123+ jsonProfilePath
124+ );
108125
109- Set <String > activeKeysForBothLanguages =
110- BuiltInQualityProfileJsonLoader .loadActiveKeysFromJsonProfile (jsonProfilePath );
111126 ruleKeys (CheckList .getChecksForLanguage (language ))
112127 .stream ()
113- .filter (activeKeysForBothLanguages ::contains )
128+ .filter (activeKeys ::contains )
114129 .forEach (key -> profile .activateRule (repositoryKey , key ));
115130 }
116131
132+ private static List <ProfileDefinition > loadProfiles () {
133+ try (
134+ InputStream inputStream =
135+ JavaScriptProfilesDefinition .class .getClassLoader ().getResourceAsStream (PROFILES_JSON )
136+ ) {
137+ if (inputStream == null ) {
138+ throw new IllegalStateException ("Missing built-in quality profile index: " + PROFILES_JSON );
139+ }
140+ JsonArray profiles = JsonParser .parseReader (
141+ new InputStreamReader (inputStream , StandardCharsets .UTF_8 )
142+ ).getAsJsonArray ();
143+ List <ProfileDefinition > definitions = new ArrayList <>();
144+ profiles .forEach (profile -> {
145+ JsonObject profileJson = profile .getAsJsonObject ();
146+ String name = profileJson .get ("name" ).getAsString ();
147+ String fileName = profileJson .get ("fileName" ).getAsString ();
148+ definitions .add (new ProfileDefinition (name , RESOURCE_PATH + "/" + fileName ));
149+ });
150+ if (
151+ definitions
152+ .stream ()
153+ .noneMatch (
154+ profile -> SONAR_WAY .equals (profile .name ()) && SONAR_WAY_JSON .equals (profile .path ())
155+ )
156+ ) {
157+ throw new IllegalStateException (
158+ "Missing required built-in quality profile in " + PROFILES_JSON + ": " + SONAR_WAY
159+ );
160+ }
161+ return definitions ;
162+ } catch (IOException e ) {
163+ throw new IllegalStateException ("Failed to load built-in quality profile index" , e );
164+ }
165+ }
166+
117167 /**
118168 * Activate additional rules that are provided by other plugins.
119169 *
@@ -175,4 +225,23 @@ private static Set<String> ruleKeys(List<Class<? extends EslintHook>> checks) {
175225 private static String securityRuleMessage (Exception e ) {
176226 return "no security rules added to builtin profile: " + e .getMessage ();
177227 }
228+
229+ private static class ProfileDefinition {
230+
231+ private final String name ;
232+ private final String path ;
233+
234+ private ProfileDefinition (String name , String path ) {
235+ this .name = name ;
236+ this .path = path ;
237+ }
238+
239+ String name () {
240+ return name ;
241+ }
242+
243+ String path () {
244+ return path ;
245+ }
246+ }
178247}
0 commit comments