Example #1
0
  private boolean fsCondition(DefaultSensorDescriptor descriptor) {
    if (!descriptor.languages().isEmpty() || descriptor.type() != null) {
      FilePredicate langPredicate =
          descriptor.languages().isEmpty()
              ? fs.predicates().all()
              : fs.predicates().hasLanguages(descriptor.languages());

      FilePredicate typePredicate =
          descriptor.type() == null
              ? fs.predicates().all()
              : fs.predicates().hasType(descriptor.type());
      return fs.hasFiles(fs.predicates().and(langPredicate, typePredicate));
    }
    return true;
  }
Example #2
0
  @Override
  public void scanFile(JavaFileScannerContext context) {
    sonarFile = fs.inputFile(fs.predicates().is(context.getFile()));
    classTrees.clear();
    methods = 0;
    complexityInMethods = 0;
    accessors = 0;
    classes = 0;
    PublicApiChecker publicApiChecker = PublicApiChecker.newInstanceWithAccessorsHandledAsMethods();
    if (separateAccessorsFromMethods) {
      publicApiChecker = PublicApiChecker.newInstanceWithAccessorsSeparatedFromMethods();
    }
    publicApiChecker.scan(context.getTree());
    methodComplexityDistribution =
        new RangeDistributionBuilder(
            CoreMetrics.FUNCTION_COMPLEXITY_DISTRIBUTION, LIMITS_COMPLEXITY_METHODS);
    CommentLinesVisitor commentLinesVisitor = new CommentLinesVisitor();
    commentLinesVisitor.analyzeCommentLines(context.getTree());
    noSonarFilter.addComponent(
        sensorContext.getResource(sonarFile).getEffectiveKey(), commentLinesVisitor.noSonarLines());
    super.scanFile(context);
    // leave file.
    int fileComplexity = context.getComplexityNodes(context.getTree()).size();
    saveMetricOnFile(CoreMetrics.CLASSES, classes);
    saveMetricOnFile(CoreMetrics.FUNCTIONS, methods);
    saveMetricOnFile(CoreMetrics.ACCESSORS, accessors);
    saveMetricOnFile(CoreMetrics.COMPLEXITY_IN_FUNCTIONS, complexityInMethods);
    saveMetricOnFile(CoreMetrics.COMPLEXITY_IN_CLASSES, fileComplexity);
    saveMetricOnFile(CoreMetrics.COMPLEXITY, fileComplexity);
    saveMetricOnFile(CoreMetrics.PUBLIC_API, publicApiChecker.getPublicApi());
    saveMetricOnFile(
        CoreMetrics.PUBLIC_DOCUMENTED_API_DENSITY,
        publicApiChecker.getDocumentedPublicApiDensity());
    saveMetricOnFile(
        CoreMetrics.PUBLIC_UNDOCUMENTED_API, publicApiChecker.getUndocumentedPublicApi());
    saveMetricOnFile(CoreMetrics.COMMENT_LINES, commentLinesVisitor.commentLinesMetric());
    saveMetricOnFile(
        CoreMetrics.STATEMENTS, new StatementVisitor().numberOfStatements(context.getTree()));
    saveMetricOnFile(CoreMetrics.NCLOC, new LinesOfCodeVisitor().linesOfCode(context.getTree()));

    sensorContext.saveMeasure(
        sonarFile,
        methodComplexityDistribution.build(true).setPersistenceMode(PersistenceMode.MEMORY));

    RangeDistributionBuilder fileComplexityDistribution =
        new RangeDistributionBuilder(
            CoreMetrics.FILE_COMPLEXITY_DISTRIBUTION, LIMITS_COMPLEXITY_FILES);
    sensorContext.saveMeasure(
        sonarFile,
        fileComplexityDistribution
            .add(fileComplexity)
            .build(true)
            .setPersistenceMode(PersistenceMode.MEMORY));
    saveLinesMetric();
  }
  public JavaScriptSquidSensor(
      CheckFactory checkFactory,
      FileLinesContextFactory fileLinesContextFactory,
      FileSystem fileSystem,
      NoSonarFilter noSonarFilter,
      @Nullable CustomJavaScriptRulesDefinition[] customRulesDefinition) {

    this.checks =
        JavaScriptChecks.createJavaScriptCheck(checkFactory)
            .addChecks(CheckList.REPOSITORY_KEY, CheckList.getChecks())
            .addCustomChecks(customRulesDefinition);
    this.fileLinesContextFactory = fileLinesContextFactory;
    this.fileSystem = fileSystem;
    this.noSonarFilter = noSonarFilter;
    this.mainFilePredicate =
        fileSystem
            .predicates()
            .and(
                fileSystem.predicates().hasType(InputFile.Type.MAIN),
                fileSystem.predicates().hasLanguage(JavaScriptLanguage.KEY));
    this.parser = JavaScriptParserBuilder.createParser(getEncoding());
  }
 private long getDevelopmentCost(DecoratorContext context) {
   InputFile file = fs.inputFile(fs.predicates().hasRelativePath(context.getResource().getKey()));
   if (file != null) {
     String language = file.language();
     return getMeasureValue(context, sqaleRatingSettings.getSizeMetric(language, metrics))
         * sqaleRatingSettings.getDevCost(language);
   } else {
     Collection<Measure> childrenMeasures =
         context.getChildrenMeasures(CoreMetrics.DEVELOPMENT_COST);
     Double sum = sum(childrenMeasures);
     return sum.longValue();
   }
 }
 private InputFile inputFromIOFile(File file) {
   return fs.inputFile(fs.predicates().is(file));
 }
 @Override
 public void visitFile(Tree scriptTree) {
   FileSystem fs = sensorContext.fileSystem();
   this.inputFile = fs.inputFile(fs.predicates().is(getContext().getFile()));
   init();
 }
Example #7
0
 @CheckForNull
 private InputFile getInputFile(String filePath) {
   return fs.inputFile(fs.predicates().hasRelativePath(filePath));
 }