Search in sources :

Example 1 with SonarLintRuleDefinition

use of org.sonarsource.sonarlint.core.rule.extractor.SonarLintRuleDefinition in project sonarlint-core by SonarSource.

the class StandaloneSonarLintEngineImpl method identifyActiveRules.

private Collection<ActiveRule> identifyActiveRules(StandaloneAnalysisConfiguration configuration) {
    Set<String> excludedRules = configuration.excludedRules().stream().map(RuleKey::toString).collect(toSet());
    Set<String> includedRules = configuration.includedRules().stream().map(RuleKey::toString).filter(r -> !excludedRules.contains(r)).collect(toSet());
    Collection<SonarLintRuleDefinition> filteredActiveRules = new ArrayList<>();
    filteredActiveRules.addAll(allRulesDefinitionsByKey.values().stream().filter(SonarLintRuleDefinition::isActiveByDefault).filter(isExcludedByConfiguration(excludedRules)).collect(Collectors.toList()));
    filteredActiveRules.addAll(allRulesDefinitionsByKey.values().stream().filter(r -> !r.isActiveByDefault()).filter(isIncludedByConfiguration(includedRules)).collect(Collectors.toList()));
    return filteredActiveRules.stream().map(rd -> {
        var activeRule = new ActiveRule(rd.getKey(), rd.getLanguage().getLanguageKey());
        Map<String, String> effectiveParams = new HashMap<>(rd.getDefaultParams());
        Optional.ofNullable(configuration.ruleParameters().get(RuleKey.parse(rd.getKey()))).ifPresent(effectiveParams::putAll);
        activeRule.setParams(effectiveParams);
        return activeRule;
    }).collect(Collectors.toList());
}
Also used : ClientLogOutput(org.sonarsource.sonarlint.core.commons.log.ClientLogOutput) AnalysisResults(org.sonarsource.sonarlint.core.analysis.api.AnalysisResults) DefaultClientIssue(org.sonarsource.sonarlint.core.client.api.common.analysis.DefaultClientIssue) Configuration(org.sonarsource.sonarlint.core.plugin.commons.PluginInstancesRepository.Configuration) HashMap(java.util.HashMap) StandaloneRuleDetails(org.sonarsource.sonarlint.core.client.api.standalone.StandaloneRuleDetails) SonarLintRuleDefinition(org.sonarsource.sonarlint.core.rule.extractor.SonarLintRuleDefinition) AnalysisEngine(org.sonarsource.sonarlint.core.analysis.AnalysisEngine) AnalyzeCommand(org.sonarsource.sonarlint.core.analysis.command.AnalyzeCommand) ArrayList(java.util.ArrayList) IssueListener(org.sonarsource.sonarlint.core.client.api.common.analysis.IssueListener) StandaloneGlobalConfiguration(org.sonarsource.sonarlint.core.client.api.standalone.StandaloneGlobalConfiguration) Map(java.util.Map) Objects.requireNonNull(java.util.Objects.requireNonNull) Nullable(javax.annotation.Nullable) ActiveRule(org.sonarsource.sonarlint.core.analysis.api.ActiveRule) StandaloneSonarLintEngine(org.sonarsource.sonarlint.core.client.api.standalone.StandaloneSonarLintEngine) Collectors.toSet(java.util.stream.Collectors.toSet) AnalysisConfiguration(org.sonarsource.sonarlint.core.analysis.api.AnalysisConfiguration) Predicate(java.util.function.Predicate) PluginInstancesRepository(org.sonarsource.sonarlint.core.plugin.commons.PluginInstancesRepository) Collection(java.util.Collection) PluginDetails(org.sonarsource.sonarlint.core.client.api.common.PluginDetails) Set(java.util.Set) ClientProgressMonitor(org.sonarsource.sonarlint.core.commons.progress.ClientProgressMonitor) Collectors(java.util.stream.Collectors) StandaloneRule(org.sonarsource.sonarlint.core.container.standalone.rule.StandaloneRule) StandaloneAnalysisConfiguration(org.sonarsource.sonarlint.core.client.api.standalone.StandaloneAnalysisConfiguration) SonarLintWrappedException(org.sonarsource.sonarlint.core.client.api.exceptions.SonarLintWrappedException) AnalysisEngineConfiguration(org.sonarsource.sonarlint.core.analysis.api.AnalysisEngineConfiguration) RuleKey(org.sonarsource.sonarlint.core.client.api.common.RuleKey) Optional(java.util.Optional) ProgressMonitor(org.sonarsource.sonarlint.core.commons.progress.ProgressMonitor) SonarLintRuleDefinition(org.sonarsource.sonarlint.core.rule.extractor.SonarLintRuleDefinition) RuleKey(org.sonarsource.sonarlint.core.client.api.common.RuleKey) ActiveRule(org.sonarsource.sonarlint.core.analysis.api.ActiveRule) ArrayList(java.util.ArrayList) HashMap(java.util.HashMap) Map(java.util.Map)

Example 2 with SonarLintRuleDefinition

use of org.sonarsource.sonarlint.core.rule.extractor.SonarLintRuleDefinition in project sonarlint-core by SonarSource.

the class RuleExtractorMediumTests method onlyLoadRulesOfEnabledLanguages.

@Test
void onlyLoadRulesOfEnabledLanguages() throws Exception {
    Set<Language> enabledLanguages = EnumSet.of(Language.JAVA, // Enable JS but not TS
    Language.JS, Language.PHP, Language.PYTHON);
    if (COMMERCIAL_ENABLED) {
        // Enable C but not C++
        enabledLanguages.add(Language.C);
    }
    var config = new PluginInstancesRepository.Configuration(allJars, enabledLanguages, empty());
    try (var pluginInstancesRepository = new PluginInstancesRepository(config)) {
        var allRules = new RulesDefinitionExtractor().extractRules(pluginInstancesRepository, enabledLanguages, false);
        assertThat(allRules.stream().map(SonarLintRuleDefinition::getLanguage)).hasSameElementsAs(enabledLanguages);
    }
}
Also used : SonarLintRuleDefinition(org.sonarsource.sonarlint.core.rule.extractor.SonarLintRuleDefinition) Language(org.sonarsource.sonarlint.core.commons.Language) PluginInstancesRepository(org.sonarsource.sonarlint.core.plugin.commons.PluginInstancesRepository) RulesDefinitionExtractor(org.sonarsource.sonarlint.core.rule.extractor.RulesDefinitionExtractor) Test(org.junit.jupiter.api.Test)

Aggregations

PluginInstancesRepository (org.sonarsource.sonarlint.core.plugin.commons.PluginInstancesRepository)2 SonarLintRuleDefinition (org.sonarsource.sonarlint.core.rule.extractor.SonarLintRuleDefinition)2 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Objects.requireNonNull (java.util.Objects.requireNonNull)1 Optional (java.util.Optional)1 Set (java.util.Set)1 Predicate (java.util.function.Predicate)1 Collectors (java.util.stream.Collectors)1 Collectors.toSet (java.util.stream.Collectors.toSet)1 Nullable (javax.annotation.Nullable)1 Test (org.junit.jupiter.api.Test)1 AnalysisEngine (org.sonarsource.sonarlint.core.analysis.AnalysisEngine)1 ActiveRule (org.sonarsource.sonarlint.core.analysis.api.ActiveRule)1 AnalysisConfiguration (org.sonarsource.sonarlint.core.analysis.api.AnalysisConfiguration)1 AnalysisEngineConfiguration (org.sonarsource.sonarlint.core.analysis.api.AnalysisEngineConfiguration)1 AnalysisResults (org.sonarsource.sonarlint.core.analysis.api.AnalysisResults)1 AnalyzeCommand (org.sonarsource.sonarlint.core.analysis.command.AnalyzeCommand)1