Changes to js
This commit is contained in:
@ -9,16 +9,35 @@ import java.util.Map;
|
||||
|
||||
import org.mozilla.javascript.ast.AstRoot;
|
||||
|
||||
import net.sourceforge.pmd.lang.LanguageVersion;
|
||||
import net.sourceforge.pmd.lang.Parser.ParserTask;
|
||||
import net.sourceforge.pmd.lang.ast.RootNode;
|
||||
|
||||
public final class ASTAstRoot extends AbstractEcmascriptNode<AstRoot> implements RootNode {
|
||||
|
||||
private Map<Integer, String> noPmdComments = Collections.emptyMap();
|
||||
private LanguageVersion languageVersion;
|
||||
private String filename;
|
||||
|
||||
public ASTAstRoot(AstRoot astRoot) {
|
||||
super(astRoot);
|
||||
}
|
||||
|
||||
@Override
|
||||
public LanguageVersion getLanguageVersion() {
|
||||
return languageVersion;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getSourceCodeFile() {
|
||||
return filename;
|
||||
}
|
||||
|
||||
void addTaskInfo(ParserTask languageVersion) {
|
||||
this.languageVersion = languageVersion.getLanguageVersion();
|
||||
this.filename = languageVersion.getFileDisplayName();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected <P, R> R acceptJsVisitor(EcmascriptVisitor<? super P, ? extends R> visitor, P data) {
|
||||
return visitor.visit(this, data);
|
||||
|
@ -4,14 +4,11 @@
|
||||
|
||||
package net.sourceforge.pmd.lang.ecmascript.ast;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.Reader;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.apache.commons.io.IOUtils;
|
||||
import org.mozilla.javascript.CompilerEnvirons;
|
||||
import org.mozilla.javascript.Parser;
|
||||
import org.mozilla.javascript.ast.AstRoot;
|
||||
@ -20,8 +17,9 @@ import org.mozilla.javascript.ast.ErrorCollector;
|
||||
import org.mozilla.javascript.ast.ParseProblem;
|
||||
|
||||
import net.sourceforge.pmd.internal.util.AssertionUtil;
|
||||
import net.sourceforge.pmd.lang.ParserOptions;
|
||||
import net.sourceforge.pmd.lang.ast.FileAnalysisException;
|
||||
import net.sourceforge.pmd.lang.ast.ParseException;
|
||||
import net.sourceforge.pmd.lang.ast.RootNode;
|
||||
|
||||
public final class EcmascriptParser implements net.sourceforge.pmd.lang.Parser {
|
||||
private final int esVersion;
|
||||
@ -32,13 +30,6 @@ public final class EcmascriptParser implements net.sourceforge.pmd.lang.Parser {
|
||||
this.suppressMarker = AssertionUtil.requireParamNotNull("suppression marker", suppressMarker);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ParserOptions getParserOptions() {
|
||||
ParserOptions options = new ParserOptions();
|
||||
options.setSuppressMarker(suppressMarker);
|
||||
return options;
|
||||
}
|
||||
|
||||
private AstRoot parseEcmascript(final String sourceCode, final List<ParseProblem> parseProblems) throws ParseException {
|
||||
final CompilerEnvirons compilerEnvirons = new CompilerEnvirons();
|
||||
compilerEnvirons.setRecordingComments(true);
|
||||
@ -62,30 +53,26 @@ public final class EcmascriptParser implements net.sourceforge.pmd.lang.Parser {
|
||||
}
|
||||
|
||||
@Override
|
||||
public ASTAstRoot parse(String filename, final Reader reader) throws ParseException {
|
||||
try {
|
||||
final List<ParseProblem> parseProblems = new ArrayList<>();
|
||||
final String sourceCode = IOUtils.toString(reader);
|
||||
final AstRoot astRoot = parseEcmascript(sourceCode, parseProblems);
|
||||
final EcmascriptTreeBuilder treeBuilder = new EcmascriptTreeBuilder(sourceCode, parseProblems);
|
||||
ASTAstRoot tree = (ASTAstRoot) treeBuilder.build(astRoot);
|
||||
public RootNode parse(ParserTask task) throws FileAnalysisException {
|
||||
final List<ParseProblem> parseProblems = new ArrayList<>();
|
||||
final String sourceCode = task.getSourceText();
|
||||
final AstRoot astRoot = parseEcmascript(sourceCode, parseProblems);
|
||||
final EcmascriptTreeBuilder treeBuilder = new EcmascriptTreeBuilder(sourceCode, parseProblems);
|
||||
final ASTAstRoot tree = (ASTAstRoot) treeBuilder.build(astRoot);
|
||||
|
||||
Map<Integer, String> suppressMap = new HashMap<>();
|
||||
if (astRoot.getComments() != null) {
|
||||
for (Comment comment : astRoot.getComments()) {
|
||||
int nopmd = comment.getValue().indexOf(suppressMarker);
|
||||
if (nopmd > -1) {
|
||||
String suppression = comment.getValue().substring(nopmd + suppressMarker.length());
|
||||
EcmascriptNode<Comment> node = treeBuilder.build(comment);
|
||||
suppressMap.put(node.getBeginLine(), suppression);
|
||||
}
|
||||
Map<Integer, String> suppressMap = new HashMap<>();
|
||||
if (astRoot.getComments() != null) {
|
||||
for (Comment comment : astRoot.getComments()) {
|
||||
int nopmd = comment.getValue().indexOf(suppressMarker);
|
||||
if (nopmd > -1) {
|
||||
String suppression = comment.getValue().substring(nopmd + suppressMarker.length());
|
||||
EcmascriptNode<Comment> node = treeBuilder.build(comment);
|
||||
suppressMap.put(node.getBeginLine(), suppression);
|
||||
}
|
||||
}
|
||||
tree.setNoPmdComments(suppressMap);
|
||||
return tree;
|
||||
} catch (IOException e) {
|
||||
throw new ParseException(e);
|
||||
}
|
||||
tree.setNoPmdComments(suppressMap);
|
||||
return tree;
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -15,7 +15,7 @@ import org.junit.Test;
|
||||
import org.mozilla.javascript.ast.AstRoot;
|
||||
|
||||
import net.sourceforge.pmd.PMD;
|
||||
import net.sourceforge.pmd.RuleContext;
|
||||
import net.sourceforge.pmd.Report;
|
||||
import net.sourceforge.pmd.lang.ParserOptions;
|
||||
import net.sourceforge.pmd.lang.ast.Node;
|
||||
import net.sourceforge.pmd.lang.ecmascript.rule.AbstractEcmascriptRule;
|
||||
@ -52,23 +52,29 @@ public class EcmascriptParserTest extends EcmascriptParserTestBase {
|
||||
*/
|
||||
@Test
|
||||
public void testLineNumbersWithinEcmascriptRules() {
|
||||
String source = "function f(x){\n" + " if (x) {\n" + " return 1;\n" + " } else {\n"
|
||||
+ " return 0;\n" + " }\n" + "}";
|
||||
final List<String> output = new ArrayList<>();
|
||||
String source =
|
||||
"function f(x){\n"
|
||||
+ " if (x) {\n"
|
||||
+ " return 1;\n"
|
||||
+ " } else {\n"
|
||||
+ " return 0;\n" + " }\n"
|
||||
+ "}";
|
||||
|
||||
class MyEcmascriptRule extends AbstractEcmascriptRule {
|
||||
|
||||
public Object visit(ASTScope node, Object data) {
|
||||
output.add("Scope from " + node.getBeginLine() + " to " + node.getEndLine());
|
||||
addViolationWithMessage(data, node, "Scope from " + node.getBeginLine() + " to " + node.getEndLine());
|
||||
return super.visit(node, data);
|
||||
}
|
||||
}
|
||||
|
||||
MyEcmascriptRule rule = new MyEcmascriptRule();
|
||||
RuleContext ctx = new RuleContext();
|
||||
rule.apply(js.parse(source), ctx);
|
||||
rule.setLanguage(js.getLanguage());
|
||||
Report report = js.executeRule(rule, source);
|
||||
|
||||
assertEquals("Scope from 2 to 4", output.get(0));
|
||||
assertEquals("Scope from 4 to 6", output.get(1));
|
||||
assertEquals("Expecting 2 violations", 2, report.getViolations().size());
|
||||
assertEquals("Scope from 2 to 4", report.getViolations().get(0).getDescription());
|
||||
assertEquals("Scope from 4 to 6", report.getViolations().get(1).getDescription());
|
||||
}
|
||||
|
||||
/**
|
||||
@ -138,7 +144,9 @@ public class EcmascriptParserTest extends EcmascriptParserTestBase {
|
||||
*/
|
||||
@Test
|
||||
public void testSuppressionComment() {
|
||||
ASTAstRoot root = js.parse("function(x) {\n" + "x = x; //NOPMD I know what I'm doing\n" + "}\n");
|
||||
ASTAstRoot root = js.parse("function(x) {\n"
|
||||
+ "x = x; //NOPMD I know what I'm doing\n"
|
||||
+ "}\n");
|
||||
assertEquals(" I know what I'm doing", root.getNoPmdComments().get(2));
|
||||
assertEquals(1, root.getNoPmdComments().size());
|
||||
|
||||
|
@ -58,7 +58,7 @@ abstract class BaseParsingHelper<Self : BaseParsingHelper<Self, T>, T : RootNode
|
||||
else language.getVersion(version) ?: throw AssertionError("Unsupported version $version for language $language")
|
||||
}
|
||||
|
||||
private val language: Language
|
||||
val language: Language
|
||||
get() = LanguageRegistry.getLanguage(langName)
|
||||
?: throw AssertionError("'$langName' is not a supported language (available ${LanguageRegistry.getLanguages()})")
|
||||
|
||||
|
Reference in New Issue
Block a user