All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.sonarsource.sonarlint.core.container.standalone.rule.StandaloneActiveRules Maven / Gradle / Ivy

/*
 * SonarLint Core - Implementation
 * Copyright (C) 2016-2021 SonarSource SA
 * mailto:info AT sonarsource DOT com
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 3 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */
package org.sonarsource.sonarlint.core.container.standalone.rule;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import javax.annotation.CheckForNull;
import org.sonar.api.batch.rule.ActiveRules;
import org.sonar.api.rule.RuleKey;
import org.sonar.api.utils.log.Logger;
import org.sonar.api.utils.log.Loggers;
import org.sonarsource.sonarlint.core.client.api.standalone.StandaloneRuleDetails;
import org.sonarsource.sonarlint.core.container.global.DefaultActiveRules;

import static java.util.stream.Collectors.toList;

public class StandaloneActiveRules {
  private static final Logger LOG = Loggers.get(StandaloneActiveRules.class);

  private Map rulesByKey;

  public StandaloneActiveRules(List rules) {
    rulesByKey = rules.stream().collect(Collectors.toMap(r -> r.key().toString(), r -> r));
  }

  public ActiveRules filtered(Set excludedRules, Set includedRules, Map> params) {
    Collection filteredActiveRules = new ArrayList<>();

    filteredActiveRules.addAll(rulesByKey.values().stream()
      .filter(StandaloneRule::isActiveByDefault)
      .filter(isExcludedByConfiguration(excludedRules))
      .collect(Collectors.toList()));
    filteredActiveRules.addAll(rulesByKey.values().stream()
      .filter(r -> !r.isActiveByDefault())
      .filter(isIncludedByConfiguration(includedRules))
      .collect(Collectors.toList()));

    return new DefaultActiveRules(
      filteredActiveRules.stream()
        .map(r -> new StandaloneActiveRuleAdapter(r, params.get(r.getKey())))
        .collect(toList()));
  }

  private static Predicate isExcludedByConfiguration(Set excludedRules) {
    return r -> {
      if (excludedRules.contains(r.getKey())) {
        return false;
      }
      for (RuleKey deprecatedKey : r.getDeprecatedKeys()) {
        if (excludedRules.contains(deprecatedKey.toString())) {
          LOG.warn("Rule '{}' was excluded using its deprecated key '{}'. Please fix your configuration.", r.key(), deprecatedKey);
          return false;
        }
      }
      return true;
    };
  }

  private static Predicate isIncludedByConfiguration(Set includedRules) {
    return r -> {
      if (includedRules.contains(r.getKey())) {
        return true;
      }
      for (RuleKey deprecatedKey : r.getDeprecatedKeys()) {
        if (includedRules.contains(deprecatedKey.toString())) {
          LOG.warn("Rule '{}' was included using its deprecated key '{}'. Please fix your configuration.", r.key(), deprecatedKey);
          return true;
        }
      }
      return false;
    };
  }

  boolean isActiveByDefault(RuleKey ruleKey) {
    return rulesByKey.get(ruleKey.toString()).isActiveByDefault();
  }

  @CheckForNull
  public StandaloneRuleDetails ruleDetails(String ruleKeyStr) {
    return rulesByKey.get(ruleKeyStr);
  }

  public Collection allRuleDetails() {
    return rulesByKey.values().stream().map(r -> (StandaloneRuleDetails) r).collect(toList());
  }

  public Collection getActiveRuleKeys() {
    return rulesByKey.values().stream()
      .filter(StandaloneRule::isActiveByDefault)
      .map(StandaloneRule::getKey)
      .collect(toList());
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy