org.sonar.javascript.JavaScriptAstScanner Maven / Gradle / Ivy
/*
* Sonar 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.google.common.collect.ImmutableList;
import com.sonar.sslr.api.AstNode;
import com.sonar.sslr.impl.Parser;
import com.sonar.sslr.squid.AstScanner;
import com.sonar.sslr.squid.SourceCodeBuilderCallback;
import com.sonar.sslr.squid.SourceCodeBuilderVisitor;
import com.sonar.sslr.squid.SquidAstVisitor;
import com.sonar.sslr.squid.SquidAstVisitorContextImpl;
import com.sonar.sslr.squid.metrics.CommentsVisitor;
import com.sonar.sslr.squid.metrics.CounterVisitor;
import com.sonar.sslr.squid.metrics.LinesOfCodeVisitor;
import com.sonar.sslr.squid.metrics.LinesVisitor;
import org.sonar.javascript.api.EcmaScriptMetric;
import org.sonar.javascript.api.EcmaScriptTokenType;
import org.sonar.javascript.metrics.ComplexityVisitor;
import org.sonar.javascript.model.TreeVisitor;
import org.sonar.javascript.model.TreeVisitorsBridge;
import org.sonar.javascript.parser.EcmaScriptGrammar;
import org.sonar.javascript.parser.EcmaScriptParser;
import org.sonar.squid.api.SourceCode;
import org.sonar.squid.api.SourceFile;
import org.sonar.squid.api.SourceFunction;
import org.sonar.squid.api.SourceProject;
import org.sonar.squid.indexer.QueryByType;
import org.sonar.sslr.parser.LexerlessGrammar;
import java.io.File;
import java.util.Collection;
public final class JavaScriptAstScanner {
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 = AstScanner. builder(context).setBaseParser(parser);
/* Metrics */
builder.withMetrics(EcmaScriptMetric.values());
/* Comments */
builder.setCommentAnalyser(new EcmaScriptCommentAnalyser());
/* Files */
builder.setFilesMetric(EcmaScriptMetric.FILES);
/* Functions */
builder.withSquidAstVisitor(CounterVisitor. builder()
.setMetricDef(EcmaScriptMetric.FUNCTIONS)
.subscribeTo(EcmaScriptGrammar.FUNCTION_DECLARATION, EcmaScriptGrammar.FUNCTION_EXPRESSION)
.build());
builder.withSquidAstVisitor(new SourceCodeBuilderVisitor(new SourceCodeBuilderCallback() {
public SourceCode createSourceCode(SourceCode parentSourceCode, AstNode astNode) {
AstNode identifier = astNode.getFirstChild(EcmaScriptTokenType.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;
}
}, EcmaScriptGrammar.FUNCTION_DECLARATION, EcmaScriptGrammar.FUNCTION_EXPRESSION));
/* 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(
EcmaScriptGrammar.VARIABLE_STATEMENT,
EcmaScriptGrammar.EMPTY_STATEMENT,
EcmaScriptGrammar.EXPRESSION_STATEMENT,
EcmaScriptGrammar.IF_STATEMENT,
EcmaScriptGrammar.ITERATION_STATEMENT,
EcmaScriptGrammar.CONTINUE_STATEMENT,
EcmaScriptGrammar.BREAK_STATEMENT,
EcmaScriptGrammar.RETURN_STATEMENT,
EcmaScriptGrammar.WITH_STATEMENT,
EcmaScriptGrammar.SWITCH_STATEMENT,
EcmaScriptGrammar.THROW_STATEMENT,
EcmaScriptGrammar.TRY_STATEMENT,
EcmaScriptGrammar.DEBUGGER_STATEMENT)
.build());
builder.withSquidAstVisitor(new ComplexityVisitor());
/* External visitors (typically Check ones) */
ImmutableList.Builder treeVisitors = ImmutableList.builder();
for (SquidAstVisitor visitor : visitors) {
if (visitor instanceof CharsetAwareVisitor) {
((CharsetAwareVisitor) visitor).setCharset(conf.getCharset());
}
if (visitor instanceof TreeVisitor) {
treeVisitors.add((TreeVisitor) visitor);
}
builder.withSquidAstVisitor(visitor);
}
TreeVisitorsBridge treeVisitorsBridge = new TreeVisitorsBridge(treeVisitors.build());
builder.withSquidAstVisitor(treeVisitorsBridge);
return builder.build();
}
}