Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* 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.squidbridge.AstScanner;
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.SourceCode;
import org.sonar.squidbridge.api.SourceClass;
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.LinesOfCodeVisitor;
import org.sonar.squidbridge.metrics.LinesVisitor;
import org.sonar.javascript.api.EcmaScriptMetric;
import org.sonar.javascript.api.EcmaScriptTokenType;
import org.sonar.javascript.metrics.ComplexityVisitor;
import org.sonar.javascript.parser.EcmaScriptGrammar;
import org.sonar.javascript.parser.EcmaScriptParser;
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 = {
EcmaScriptGrammar.FUNCTION_DECLARATION,
EcmaScriptGrammar.FUNCTION_EXPRESSION,
EcmaScriptGrammar.METHOD,
EcmaScriptGrammar.GENERATOR_METHOD,
EcmaScriptGrammar.GENERATOR_EXPRESSION,
EcmaScriptGrammar.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;
}
}, EcmaScriptGrammar.CLASS_DECLARATION, EcmaScriptGrammar.CLASS_EXPRESSION));
builder.withSquidAstVisitor(CounterVisitor.builder().setMetricDef(EcmaScriptMetric.CLASSES)
.subscribeTo(EcmaScriptGrammar.CLASS_DECLARATION, EcmaScriptGrammar.CLASS_EXPRESSION)
.build());
/* Functions */
builder.withSquidAstVisitor(CounterVisitor. builder()
.setMetricDef(EcmaScriptMetric.FUNCTIONS)
.subscribeTo(FUNCTION_NODES)
.build());
builder.withSquidAstVisitor(new SourceCodeBuilderVisitor(new SourceCodeBuilderCallback() {
public SourceCode createSourceCode(SourceCode parentSourceCode, AstNode astNode) {
AstNode identifier = astNode.getFirstChild(EcmaScriptTokenType.IDENTIFIER, EcmaScriptGrammar.PROPERTY_NAME, EcmaScriptGrammar.BINDING_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(
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(CounterVisitor.builder()
.setMetricDef(EcmaScriptMetric.ACCESSORS)
.subscribeTo(
EcmaScriptGrammar.GETTER_METHOD,
EcmaScriptGrammar.SETTER_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();
}
}