org.sonar.javascript.metrics.FileLinesVisitor 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.metrics;
import java.util.List;
import java.util.Set;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.sonar.api.batch.fs.FileSystem;
import org.sonar.api.measures.CoreMetrics;
import org.sonar.api.measures.FileLinesContext;
import org.sonar.api.measures.FileLinesContextFactory;
import org.sonar.api.resources.File;
import org.sonar.api.scan.filesystem.PathResolver;
import org.sonar.javascript.api.EcmaScriptMetric;
import org.sonar.squidbridge.SquidAstVisitor;
import org.sonar.sslr.parser.LexerlessGrammar;
import com.google.common.collect.Sets;
import com.sonar.sslr.api.AstAndTokenVisitor;
import com.sonar.sslr.api.AstNode;
import com.sonar.sslr.api.GenericTokenType;
import com.sonar.sslr.api.Token;
import com.sonar.sslr.api.Trivia;
/**
* Visitor that computes {@link CoreMetrics#NCLOC_DATA_KEY} and {@link CoreMetrics#COMMENT_LINES_DATA_KEY} metrics used by the DevCockpit.
*/
public class FileLinesVisitor extends SquidAstVisitor implements AstAndTokenVisitor {
private static final Logger LOG = LoggerFactory.getLogger(FileLinesVisitor.class);
private final FileLinesContextFactory fileLinesContextFactory;
private final FileSystem fileSystem;
private final PathResolver pathResolver;
private final Set linesOfCode = Sets.newHashSet();
private final Set linesOfComments = Sets.newHashSet();
public FileLinesVisitor(FileLinesContextFactory fileLinesContextFactory, FileSystem fileSystem, PathResolver pathResolver) {
this.fileLinesContextFactory = fileLinesContextFactory;
this.fileSystem = fileSystem;
this.pathResolver = pathResolver;
}
@Override
public void visitToken(Token token) {
if (token.getType().equals(GenericTokenType.EOF)) {
return;
}
linesOfCode.add(token.getLine());
List trivias = token.getTrivia();
for (Trivia trivia : trivias) {
if (trivia.isComment()) {
linesOfComments.add(trivia.getToken().getLine());
}
}
}
@Override
public void leaveFile(AstNode astNode) {
FileLinesContext fileLinesContext = fileLinesContextFactory.createFor(File.create(pathResolver.relativePath(fileSystem.baseDir(), getContext().getFile().getAbsoluteFile())));
if (fileLinesContext != null) {
int fileLength = getContext().peekSourceCode().getInt(EcmaScriptMetric.LINES);
for (int line = 1; line <= fileLength; line++) {
fileLinesContext.setIntValue(CoreMetrics.NCLOC_DATA_KEY, line, linesOfCode.contains(line) ? 1 : 0);
fileLinesContext.setIntValue(CoreMetrics.COMMENT_LINES_DATA_KEY, line, linesOfComments.contains(line) ? 1 : 0);
}
fileLinesContext.save();
} else {
LOG.warn("Cannot save measures for DevCockpit for file {}. Unable to retrieve the associated sonar resource.", getContext().getFile().getName());
}
linesOfCode.clear();
linesOfComments.clear();
}
}