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

org.sonar.javascript.JavaScriptAstScanner Maven / Gradle / Ivy

/*
 * SonarQube JavaScript Plugin
 * Copyright (C) 2011 SonarSource and Eriks Nukis
 * [email protected]
 *
 * 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  02
 */
package org.sonar.javascript;

import com.google.common.base.Charsets;
import com.sonar.sslr.api.AstNode;
import com.sonar.sslr.impl.Parser;
import org.sonar.javascript.api.EcmaScriptMetric;
import org.sonar.javascript.api.EcmaScriptTokenType;
import org.sonar.javascript.metrics.ComplexityVisitor;
import org.sonar.javascript.metrics.LinesOfCodeVisitor;
import org.sonar.plugins.javascript.api.tree.Tree.Kind;
import org.sonar.javascript.parser.EcmaScriptGrammar;
import org.sonar.javascript.parser.EcmaScriptParser;
import org.sonar.squidbridge.AstScanner;
import org.sonar.squidbridge.ProgressAstScanner;
import org.sonar.squidbridge.SourceCodeBuilderCallback;
import org.sonar.squidbridge.SourceCodeBuilderVisitor;
import org.sonar.squidbridge.SquidAstVisitor;
import org.sonar.squidbridge.SquidAstVisitorContextImpl;
import org.sonar.squidbridge.api.SourceClass;
import org.sonar.squidbridge.api.SourceCode;
import org.sonar.squidbridge.api.SourceFile;
import org.sonar.squidbridge.api.SourceFunction;
import org.sonar.squidbridge.api.SourceProject;
import org.sonar.squidbridge.indexer.QueryByType;
import org.sonar.squidbridge.metrics.CommentsVisitor;
import org.sonar.squidbridge.metrics.CounterVisitor;
import org.sonar.squidbridge.metrics.LinesVisitor;
import org.sonar.sslr.grammar.GrammarRuleKey;
import org.sonar.sslr.parser.LexerlessGrammar;

import java.io.File;
import java.util.Collection;

public final class JavaScriptAstScanner {

  private static final GrammarRuleKey[] FUNCTION_NODES = {
    Kind.FUNCTION_DECLARATION,
    Kind.FUNCTION_EXPRESSION,
    Kind.METHOD,
    Kind.GENERATOR_METHOD,
    Kind.GENERATOR_FUNCTION_EXPRESSION,
    Kind.GENERATOR_DECLARATION};

  private JavaScriptAstScanner() {
  }

  /**
   * Helper method for testing checks without having to deploy them on a Sonar instance.
   */
  public static SourceFile scanSingleFile(File file, SquidAstVisitor... visitors) {
    if (!file.isFile()) {
      throw new IllegalArgumentException("File '" + file + "' not found.");
    }
    AstScanner scanner = create(new EcmaScriptConfiguration(Charsets.UTF_8), visitors);
    scanner.scanFile(file);
    Collection sources = scanner.getIndex().search(new QueryByType(SourceFile.class));
    if (sources.size() != 1) {
      throw new IllegalStateException("Only one SourceFile was expected whereas " + sources.size() + " has been returned.");
    }
    return (SourceFile) sources.iterator().next();
  }

  public static AstScanner create(EcmaScriptConfiguration conf, SquidAstVisitor... visitors) {
    final SquidAstVisitorContextImpl context = new SquidAstVisitorContextImpl(new SourceProject("JavaScript Project"));
    final Parser parser = EcmaScriptParser.create(conf);

    AstScanner.Builder builder = new ProgressAstScanner.Builder(context).setBaseParser(parser);

    /* Metrics */
    builder.withMetrics(EcmaScriptMetric.values());

    /* Comments */
    builder.setCommentAnalyser(new EcmaScriptCommentAnalyser());

    /* Files */
    builder.setFilesMetric(EcmaScriptMetric.FILES);

    /* Classes */
    builder.withSquidAstVisitor(new SourceCodeBuilderVisitor(new SourceCodeBuilderCallback() {
      private int seq = 0;

      @Override
      public SourceCode createSourceCode(SourceCode parentSourceCode, AstNode astNode) {
        seq++;
        SourceClass cls = new SourceClass("class:" + seq);
        cls.setStartAtLine(astNode.getTokenLine());
        return cls;
      }
    }, Kind.CLASS_DECLARATION, Kind.CLASS_EXPRESSION));

    builder.withSquidAstVisitor(CounterVisitor.builder().setMetricDef(EcmaScriptMetric.CLASSES)
      .subscribeTo(Kind.CLASS_DECLARATION, Kind.CLASS_EXPRESSION)
      .build());

    /* Functions */
    builder.withSquidAstVisitor(CounterVisitor. builder()
        .setMetricDef(EcmaScriptMetric.FUNCTIONS)
        .subscribeTo(FUNCTION_NODES)
        .build());

    builder.withSquidAstVisitor(new SourceCodeBuilderVisitor(new SourceCodeBuilderCallback() {
      @Override
      public SourceCode createSourceCode(SourceCode parentSourceCode, AstNode astNode) {
        AstNode identifier = astNode.getFirstChild(EcmaScriptTokenType.IDENTIFIER, EcmaScriptGrammar.PROPERTY_NAME, Kind.IDENTIFIER);
        final String functionName = identifier == null ? "anonymous" : identifier.getTokenValue();
        final String fileKey = parentSourceCode.isType(SourceFile.class) ? parentSourceCode.getKey() : parentSourceCode.getParent(SourceFile.class).getKey();
        SourceFunction function = new SourceFunction(fileKey + ":" + functionName + ":" + astNode.getToken().getLine() + ":" + astNode.getToken().getColumn());
        function.setStartAtLine(astNode.getTokenLine());
        return function;
      }
    }, FUNCTION_NODES));

    /* Metrics */
    builder.withSquidAstVisitor(new LinesVisitor(EcmaScriptMetric.LINES));
    builder.withSquidAstVisitor(new LinesOfCodeVisitor(EcmaScriptMetric.LINES_OF_CODE));
    builder.withSquidAstVisitor(CommentsVisitor. builder().withCommentMetric(EcmaScriptMetric.COMMENT_LINES)
        .withNoSonar(true)
        .withIgnoreHeaderComment(conf.getIgnoreHeaderComments())
        .build());
    builder.withSquidAstVisitor(CounterVisitor. builder()
        .setMetricDef(EcmaScriptMetric.STATEMENTS)
        .subscribeTo(
            Kind.VARIABLE_STATEMENT,
            Kind.EMPTY_STATEMENT,
            Kind.EXPRESSION_STATEMENT,
            Kind.IF_STATEMENT,
            Kind.DO_WHILE_STATEMENT,
            Kind.WHILE_STATEMENT,
            Kind.FOR_IN_STATEMENT,
            Kind.FOR_OF_STATEMENT,
            Kind.FOR_STATEMENT,
            Kind.CONTINUE_STATEMENT,
            Kind.BREAK_STATEMENT,
            Kind.RETURN_STATEMENT,
            Kind.WITH_STATEMENT,
            Kind.SWITCH_STATEMENT,
            Kind.THROW_STATEMENT,
            Kind.TRY_STATEMENT,
            Kind.DEBUGGER_STATEMENT)
        .build());

    builder.withSquidAstVisitor(CounterVisitor.builder()
      .setMetricDef(EcmaScriptMetric.ACCESSORS)
      .subscribeTo(
        Kind.GET_METHOD,
        Kind.SET_METHOD)
      .build());

    builder.withSquidAstVisitor(new ComplexityVisitor());

    for (SquidAstVisitor visitor : visitors) {
      if (visitor instanceof CharsetAwareVisitor) {
        ((CharsetAwareVisitor) visitor).setCharset(conf.getCharset());
      }
      builder.withSquidAstVisitor(visitor);
    }

    return builder.build();
  }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy