From c29f1a6b84cc10e07fcfff70775c126eac8209ef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Fournier?= Date: Sat, 17 Aug 2019 09:59:12 +0200 Subject: [PATCH] Remove suppress map --- .../sourceforge/pmd/lang/apex/ApexParser.java | 5 ----- .../sourceforge/pmd/SourceCodeProcessor.java | 4 +--- .../pmd/lang/LanguageVersionHandler.java | 5 ++++- .../java/net/sourceforge/pmd/lang/Parser.java | 4 ---- .../pmd/lang/ecmascript/Ecmascript3Parser.java | 3 ++- .../pmd/lang/ecmascript/ast/ASTAstRoot.java | 16 ++++++++++++++++ .../lang/ecmascript/ast/EcmascriptParser.java | 5 +++-- .../rule/EcmascriptRuleViolationFactory.java | 18 ------------------ .../ecmascript/ast/EcmascriptParserTest.java | 14 +++++++------- 9 files changed, 33 insertions(+), 41 deletions(-) diff --git a/pmd-apex/src/main/java/net/sourceforge/pmd/lang/apex/ApexParser.java b/pmd-apex/src/main/java/net/sourceforge/pmd/lang/apex/ApexParser.java index 5a9df7bbd1..d0e3834635 100644 --- a/pmd-apex/src/main/java/net/sourceforge/pmd/lang/apex/ApexParser.java +++ b/pmd-apex/src/main/java/net/sourceforge/pmd/lang/apex/ApexParser.java @@ -5,7 +5,6 @@ package net.sourceforge.pmd.lang.apex; import java.io.Reader; -import java.util.Map; import net.sourceforge.pmd.lang.AbstractParser; import net.sourceforge.pmd.lang.ParserOptions; @@ -34,8 +33,4 @@ public class ApexParser extends AbstractParser { return apexParser.parse(source); } - @Override - public Map getSuppressMap() { - return apexParser.getSuppressMap(); - } } diff --git a/pmd-core/src/main/java/net/sourceforge/pmd/SourceCodeProcessor.java b/pmd-core/src/main/java/net/sourceforge/pmd/SourceCodeProcessor.java index 64abfb8860..ce579cb0d9 100644 --- a/pmd-core/src/main/java/net/sourceforge/pmd/SourceCodeProcessor.java +++ b/pmd-core/src/main/java/net/sourceforge/pmd/SourceCodeProcessor.java @@ -110,9 +110,7 @@ public class SourceCodeProcessor { private Node parse(RuleContext ctx, Reader sourceCode, Parser parser) { try (TimedOperation to = TimeTracker.startOperation(TimedOperationCategory.PARSER)) { - Node rootNode = parser.parse(String.valueOf(ctx.getSourceCodeFile()), sourceCode); - ctx.getReport().suppress(parser.getSuppressMap()); - return rootNode; + return parser.parse(String.valueOf(ctx.getSourceCodeFile()), sourceCode); } } diff --git a/pmd-core/src/main/java/net/sourceforge/pmd/lang/LanguageVersionHandler.java b/pmd-core/src/main/java/net/sourceforge/pmd/lang/LanguageVersionHandler.java index a87637c1a1..a730f06c64 100644 --- a/pmd-core/src/main/java/net/sourceforge/pmd/lang/LanguageVersionHandler.java +++ b/pmd-core/src/main/java/net/sourceforge/pmd/lang/LanguageVersionHandler.java @@ -11,6 +11,7 @@ import net.sourceforge.pmd.annotation.InternalApi; import net.sourceforge.pmd.lang.ast.AstProcessingStage; import net.sourceforge.pmd.lang.dfa.DFAGraphRule; import net.sourceforge.pmd.lang.metrics.LanguageMetricsProvider; +import net.sourceforge.pmd.lang.rule.AbstractRuleViolationFactory; import net.sourceforge.pmd.lang.rule.RuleViolationFactory; @@ -61,7 +62,9 @@ public interface LanguageVersionHandler { /** * Get the RuleViolationFactory. */ - RuleViolationFactory getRuleViolationFactory(); + default RuleViolationFactory getRuleViolationFactory() { + return new AbstractRuleViolationFactory() {}; + } /** diff --git a/pmd-core/src/main/java/net/sourceforge/pmd/lang/Parser.java b/pmd-core/src/main/java/net/sourceforge/pmd/lang/Parser.java index 5e58354363..7d6991cc9e 100644 --- a/pmd-core/src/main/java/net/sourceforge/pmd/lang/Parser.java +++ b/pmd-core/src/main/java/net/sourceforge/pmd/lang/Parser.java @@ -49,8 +49,4 @@ public interface Parser { Node parse(String fileName, Reader source) throws ParseException; - // TODO Document - default Map getSuppressMap() { - return Collections.emptyMap(); - } } diff --git a/pmd-javascript/src/main/java/net/sourceforge/pmd/lang/ecmascript/Ecmascript3Parser.java b/pmd-javascript/src/main/java/net/sourceforge/pmd/lang/ecmascript/Ecmascript3Parser.java index ee49b73f7f..401776f8a6 100644 --- a/pmd-javascript/src/main/java/net/sourceforge/pmd/lang/ecmascript/Ecmascript3Parser.java +++ b/pmd-javascript/src/main/java/net/sourceforge/pmd/lang/ecmascript/Ecmascript3Parser.java @@ -12,6 +12,7 @@ import net.sourceforge.pmd.lang.ParserOptions; import net.sourceforge.pmd.lang.TokenManager; import net.sourceforge.pmd.lang.ast.Node; import net.sourceforge.pmd.lang.ast.ParseException; +import net.sourceforge.pmd.lang.ecmascript.ast.ASTAstRoot; import net.sourceforge.pmd.lang.ecmascript5.Ecmascript5TokenManager; /** @@ -32,7 +33,7 @@ public class Ecmascript3Parser extends AbstractParser { } @Override - public Node parse(String fileName, Reader source) throws ParseException { + public ASTAstRoot parse(String fileName, Reader source) throws ParseException { return ecmascriptParser.parse(source); } diff --git a/pmd-javascript/src/main/java/net/sourceforge/pmd/lang/ecmascript/ast/ASTAstRoot.java b/pmd-javascript/src/main/java/net/sourceforge/pmd/lang/ecmascript/ast/ASTAstRoot.java index b4976f84e1..8edb11ca14 100644 --- a/pmd-javascript/src/main/java/net/sourceforge/pmd/lang/ecmascript/ast/ASTAstRoot.java +++ b/pmd-javascript/src/main/java/net/sourceforge/pmd/lang/ecmascript/ast/ASTAstRoot.java @@ -4,11 +4,17 @@ package net.sourceforge.pmd.lang.ecmascript.ast; +import java.util.Collections; +import java.util.Map; + import org.mozilla.javascript.ast.AstRoot; import net.sourceforge.pmd.lang.ast.RootNode; public class ASTAstRoot extends AbstractEcmascriptNode implements RootNode { + + private Map noPmdComments = Collections.emptyMap(); + public ASTAstRoot(AstRoot astRoot) { super(astRoot); } @@ -25,6 +31,16 @@ public class ASTAstRoot extends AbstractEcmascriptNode implements RootN return node.getComments() != null ? node.getComments().size() : 0; } + + @Override + public Map getNoPmdComments() { + return noPmdComments; + } + + void setNoPmdComments(Map noPmdComments) { + this.noPmdComments = noPmdComments; + } + public ASTComment getComment(int index) { return (ASTComment) jjtGetChild(jjtGetNumChildren() - 1 - getNumComments() + index); } diff --git a/pmd-javascript/src/main/java/net/sourceforge/pmd/lang/ecmascript/ast/EcmascriptParser.java b/pmd-javascript/src/main/java/net/sourceforge/pmd/lang/ecmascript/ast/EcmascriptParser.java index 0ef196dbb7..b0c16ea59e 100644 --- a/pmd-javascript/src/main/java/net/sourceforge/pmd/lang/ecmascript/ast/EcmascriptParser.java +++ b/pmd-javascript/src/main/java/net/sourceforge/pmd/lang/ecmascript/ast/EcmascriptParser.java @@ -58,13 +58,13 @@ public class EcmascriptParser { return astRoot; } - public EcmascriptNode parse(final Reader reader) { + public ASTAstRoot parse(final Reader reader) { try { final List parseProblems = new ArrayList<>(); final String sourceCode = IOUtils.toString(reader); final AstRoot astRoot = parseEcmascript(sourceCode, parseProblems); final EcmascriptTreeBuilder treeBuilder = new EcmascriptTreeBuilder(sourceCode, parseProblems); - EcmascriptNode tree = treeBuilder.build(astRoot); + ASTAstRoot tree = (ASTAstRoot) treeBuilder.build(astRoot); suppressMap = new HashMap<>(); if (astRoot.getComments() != null) { @@ -77,6 +77,7 @@ public class EcmascriptParser { } } } + tree.setNoPmdComments(suppressMap); return tree; } catch (IOException e) { throw new ParseException(e); diff --git a/pmd-javascript/src/main/java/net/sourceforge/pmd/lang/ecmascript/rule/EcmascriptRuleViolationFactory.java b/pmd-javascript/src/main/java/net/sourceforge/pmd/lang/ecmascript/rule/EcmascriptRuleViolationFactory.java index 3deca0d4e3..1648c02757 100644 --- a/pmd-javascript/src/main/java/net/sourceforge/pmd/lang/ecmascript/rule/EcmascriptRuleViolationFactory.java +++ b/pmd-javascript/src/main/java/net/sourceforge/pmd/lang/ecmascript/rule/EcmascriptRuleViolationFactory.java @@ -4,13 +4,7 @@ package net.sourceforge.pmd.lang.ecmascript.rule; -import net.sourceforge.pmd.Rule; -import net.sourceforge.pmd.RuleContext; -import net.sourceforge.pmd.RuleViolation; -import net.sourceforge.pmd.lang.ast.Node; -import net.sourceforge.pmd.lang.ecmascript.ast.EcmascriptNode; import net.sourceforge.pmd.lang.rule.AbstractRuleViolationFactory; -import net.sourceforge.pmd.lang.rule.ParametricRuleViolation; public final class EcmascriptRuleViolationFactory extends AbstractRuleViolationFactory { @@ -18,16 +12,4 @@ public final class EcmascriptRuleViolationFactory extends AbstractRuleViolationF private EcmascriptRuleViolationFactory() { } - - @SuppressWarnings("rawtypes") - @Override - protected RuleViolation createRuleViolation(Rule rule, RuleContext ruleContext, Node node, String message) { - return new ParametricRuleViolation<>(rule, ruleContext, (EcmascriptNode) node, message); - } - - @Override - protected RuleViolation createRuleViolation(Rule rule, RuleContext ruleContext, Node node, String message, - int beginLine, int endLine) { - return null; // FIXME - } } diff --git a/pmd-javascript/src/test/java/net/sourceforge/pmd/lang/ecmascript/ast/EcmascriptParserTest.java b/pmd-javascript/src/test/java/net/sourceforge/pmd/lang/ecmascript/ast/EcmascriptParserTest.java index 3c6c300e59..e4f3931c6e 100644 --- a/pmd-javascript/src/test/java/net/sourceforge/pmd/lang/ecmascript/ast/EcmascriptParserTest.java +++ b/pmd-javascript/src/test/java/net/sourceforge/pmd/lang/ecmascript/ast/EcmascriptParserTest.java @@ -141,21 +141,21 @@ public class EcmascriptParserTest extends EcmascriptParserTestBase { * not implemented) with ECMAscript */ @Test - public void testSuppresionComment() { + public void testSuppressionComment() { Ecmascript3Parser parser = new Ecmascript3Parser(new EcmascriptParserOptions()); Reader sourceCode = new StringReader("function(x) {\n" + "x = x; //NOPMD I know what I'm doing\n" + "}\n"); - parser.parse("foo", sourceCode); - assertEquals(" I know what I'm doing", parser.getSuppressMap().get(2)); - assertEquals(1, parser.getSuppressMap().size()); + ASTAstRoot root = parser.parse("foo", sourceCode); + assertEquals(" I know what I'm doing", root.getNoPmdComments().get(2)); + assertEquals(1, root.getNoPmdComments().size()); EcmascriptParserOptions parserOptions = new EcmascriptParserOptions(); parserOptions.setSuppressMarker("FOOOO"); parser = new Ecmascript3Parser(parserOptions); sourceCode = new StringReader( "function(x) {\n" + "y = y; //NOPMD xyz\n" + "x = x; //FOOOO I know what I'm doing\n" + "}\n"); - parser.parse("foo", sourceCode); - assertEquals(" I know what I'm doing", parser.getSuppressMap().get(3)); - assertEquals(1, parser.getSuppressMap().size()); + root = parser.parse("foo", sourceCode); + assertEquals(" I know what I'm doing", root.getNoPmdComments().get(3)); + assertEquals(1, root.getNoPmdComments().size()); } /**