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

org.sonar.plugins.javascript.bridge.AnalysisMode Maven / Gradle / Ivy

There is a newer version: 10.17.0.28100
Show newest version
/*
 * SonarQube JavaScript Plugin
 * Copyright (C) 2011-2024 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.sonar.plugins.javascript.bridge;

import static java.util.Collections.emptyList;

import java.util.HashSet;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.sonar.api.batch.fs.InputFile;
import org.sonar.api.batch.sensor.SensorContext;
import org.sonar.api.utils.Version;

public enum AnalysisMode {
  DEFAULT,
  SKIP_UNCHANGED;

  static final String DEFAULT_LINTER_ID = "default";
  static final String UNCHANGED_LINTER_ID = "unchanged";
  private static final Logger LOG = LoggerFactory.getLogger(AnalysisMode.class);

  public static boolean isRuntimeApiCompatible(SensorContext context) {
    return context.runtime().getApiVersion().isGreaterThanOrEqual(Version.create(9, 4));
  }

  static AnalysisMode getMode(SensorContext context, List rules) {
    var logDefaultMode = "Analysis of unchanged files will not be skipped ({})";

    if (!isRuntimeApiCompatible(context)) {
      LOG.debug(logDefaultMode, "runtime API is not compatible");
      return AnalysisMode.DEFAULT;
    }

    var canSkipUnchangedFiles = context.canSkipUnchangedFiles();
    if (!canSkipUnchangedFiles) {
      LOG.debug(logDefaultMode, "current analysis requires all files to be analyzed");
      return AnalysisMode.DEFAULT;
    }

    // This is not a common use case so falling back to default behaviour even if some optimization is possible
    // (possible if all sonar-security rules are deactivated for analysis)
    var containsUcfgRule = EslintRule.containsRuleWithKey(rules, EslintRule.UCFG_ESLINT_KEY);
    if (!containsUcfgRule) {
      LOG.debug(logDefaultMode, "security rules are not available");
      return AnalysisMode.DEFAULT;
    }

    LOG.debug(
      "Files which didn't change will be part of UCFG generation only, other rules will not be executed"
    );
    return AnalysisMode.SKIP_UNCHANGED;
  }

  static List getUnchangedFileRules(List rules) {
    var rule = EslintRule.findFirstRuleWithKey(rules, EslintRule.UCFG_ESLINT_KEY);
    return rule == null ? emptyList() : List.of(rule);
  }

  /**
   * Disable rules that are too noisy in HTML files
   * Only add rules part of the "Sonar Way" profile
   *
   * @param rules
   * @return
   */
  static List getHtmlFileRules(List rules) {
    var blackListRuleKeys = new HashSet();
    blackListRuleKeys.add("no-var");
    blackListRuleKeys.add("ucfg");
    return EslintRule.findAllBut(rules, blackListRuleKeys);
  }

  String getLinterIdFor(InputFile file) {
    if (this == SKIP_UNCHANGED && file.status() == InputFile.Status.SAME) {
      return UNCHANGED_LINTER_ID;
    } else {
      return DEFAULT_LINTER_ID;
    }
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy