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

org.sonar.css.plugin.metrics.MetricSensor Maven / Gradle / Ivy

The newest version!
/*
 * SonarCSS
 * Copyright (C) 2018-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.sonar.css.plugin.metrics;

import java.io.IOException;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import org.sonar.api.batch.fs.FileSystem;
import org.sonar.api.batch.fs.InputFile;
import org.sonar.api.batch.sensor.Sensor;
import org.sonar.api.batch.sensor.SensorContext;
import org.sonar.api.batch.sensor.SensorDescriptor;
import org.sonar.api.batch.sensor.highlighting.NewHighlighting;
import org.sonar.api.batch.sensor.highlighting.TypeOfText;
import org.sonar.api.measures.CoreMetrics;
import org.sonar.api.measures.FileLinesContext;
import org.sonar.api.measures.FileLinesContextFactory;
import org.sonar.api.utils.log.Logger;
import org.sonar.api.utils.log.Loggers;
import org.sonar.css.plugin.CssLanguage;

public class MetricSensor implements Sensor {

  private static final Logger LOG = Loggers.get(MetricSensor.class);

  private final FileLinesContextFactory fileLinesContextFactory;

  public MetricSensor(FileLinesContextFactory fileLinesContextFactory) {
    this.fileLinesContextFactory = fileLinesContextFactory;
  }

  @Override
  public void describe(SensorDescriptor descriptor) {
    descriptor
      .name("CSS Metrics")
      .onlyOnLanguage(CssLanguage.KEY);
  }

  @Override
  public void execute(SensorContext context) {
    FileSystem fileSystem = context.fileSystem();
    Iterable inputFiles = fileSystem.inputFiles(fileSystem.predicates().hasLanguage(CssLanguage.KEY));

    Tokenizer tokenizer = new Tokenizer();

    for (InputFile file : inputFiles) {
      try {
        List tokenList = tokenizer.tokenize(file.contents());

        saveHighlights(context, file, tokenList);
        saveLineTypes(context, file, tokenList);

      } catch (IOException e) {
        LOG.error(String.format("Failed to read file '%s'", file.toString()), e);
      }
    }
  }

  private static void saveHighlights(SensorContext context, InputFile file, List tokenList) {
    NewHighlighting highlighting = context.newHighlighting().onFile(file);

    for (int i = 0; i < tokenList.size(); i++) {
      CssToken currentToken = tokenList.get(i);
      CssToken nextToken = i + 1 < tokenList.size() ? tokenList.get(i + 1) : null;

      TypeOfText highlightingType = null;
      switch (currentToken.type) {
        case COMMENT:
          highlightingType = TypeOfText.COMMENT;
          break;

        case STRING:
          highlightingType = TypeOfText.STRING;
          break;

        case NUMBER:
          highlightingType = TypeOfText.CONSTANT;
          break;

        case AT_IDENTIFIER:
          highlightingType = TypeOfText.ANNOTATION;
          break;

        case DOLLAR_IDENTIFIER:
          highlightingType = TypeOfText.KEYWORD;
          break;

        case HASH_IDENTIFIER:
          if (currentToken.text.matches("^#[0-9a-fA-F]+$")) {
            highlightingType = TypeOfText.CONSTANT;
          } else {
            highlightingType = TypeOfText.KEYWORD;
          }
          break;

        case IDENTIFIER:
          // We want to highlight the property key of a css/scss/less file and as the tokenizer is putting the ':' into another token
          // we need to look for identifier followed by a PUNCTUATOR token with text ':'.
          if (nextToken != null && nextToken.text.equals(":")) {
            highlightingType = TypeOfText.KEYWORD_LIGHT;
          }
          break;

        default:
          highlightingType = null;
      }

      if (highlightingType != null) {
        highlighting.highlight(currentToken.startLine, currentToken.startColumn, currentToken.endLine, currentToken.endColumn, highlightingType);
      }
    }

    highlighting.save();
  }

  private void saveLineTypes(SensorContext context, InputFile file, List tokenList) {
    // collect line types
    Set linesOfCode = new HashSet<>();
    Set linesOfComment = new HashSet<>();

    for (CssToken token : tokenList) {
      for (int line = token.startLine; line <= token.endLine; line++) {
        if (token.type.equals(CssTokenType.COMMENT)) {
          linesOfComment.add(line);
        } else {
          linesOfCode.add(line);
        }
      }
    }

    context.newMeasure().on(file).forMetric(CoreMetrics.NCLOC).withValue(linesOfCode.size()).save();
    context.newMeasure().on(file).forMetric(CoreMetrics.COMMENT_LINES).withValue(linesOfComment.size()).save();

    FileLinesContext fileLinesContext = fileLinesContextFactory.createFor(file);
    linesOfCode.forEach(line -> fileLinesContext.setIntValue(CoreMetrics.NCLOC_DATA_KEY, line, 1));
    fileLinesContext.save();
  }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy