diff --git a/pmd-core/src/main/java/net/sourceforge/pmd/lang/rule/ParametricRuleViolation.java b/pmd-core/src/main/java/net/sourceforge/pmd/lang/rule/ParametricRuleViolation.java index 64e825494a..a41319dfac 100644 --- a/pmd-core/src/main/java/net/sourceforge/pmd/lang/rule/ParametricRuleViolation.java +++ b/pmd-core/src/main/java/net/sourceforge/pmd/lang/rule/ParametricRuleViolation.java @@ -66,13 +66,6 @@ public class ParametricRuleViolation implements RuleViolation { } - public ParametricRuleViolation(Rule theRule, RuleContext ctx, T node, String message, - int beginLine, int endLine) { - this(theRule, ctx, node, message); - this.beginLine = beginLine; - this.endLine = endLine; - } - private void setSuppression(Rule rule, T node) { String regex = rule.getProperty(Rule.VIOLATION_SUPPRESS_REGEX_DESCRIPTOR); // Regex 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 785f64ff66..93970b852a 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 @@ -26,15 +26,16 @@ 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); + 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 new ParametricRuleViolation<>(rule, ruleContext, (EcmascriptNode) node, message, beginLine, endLine); + ParametricRuleViolation> ruleViolation = new ParametricRuleViolation<>(rule, ruleContext, (EcmascriptNode) node, message); + ruleViolation.setLines(beginLine, endLine); + return ruleViolation; } } diff --git a/pmd-javascript/src/test/java/net/sourceforge/pmd/lang/ecmascript/EcmasccriptLanguageModuleTest.java b/pmd-javascript/src/test/java/net/sourceforge/pmd/lang/ecmascript/EcmasccriptLanguageModuleTest.java new file mode 100644 index 0000000000..60d698ebe5 --- /dev/null +++ b/pmd-javascript/src/test/java/net/sourceforge/pmd/lang/ecmascript/EcmasccriptLanguageModuleTest.java @@ -0,0 +1,46 @@ +/* + * BSD-style license; for more info see http://pmd.sourceforge.net/license.html + */ + +package net.sourceforge.pmd.lang.ecmascript; + +import org.junit.Assert; +import org.junit.Test; + +import net.sourceforge.pmd.Rule; +import net.sourceforge.pmd.RuleContext; +import net.sourceforge.pmd.RuleViolation; +import net.sourceforge.pmd.lang.LanguageRegistry; +import net.sourceforge.pmd.lang.LanguageVersion; +import net.sourceforge.pmd.lang.LanguageVersionHandler; +import net.sourceforge.pmd.lang.ecmascript.ast.ASTAstRoot; +import net.sourceforge.pmd.lang.ecmascript.ast.JsParsingHelper; +import net.sourceforge.pmd.lang.ecmascript.rule.AbstractEcmascriptRule; +import net.sourceforge.pmd.lang.rule.RuleViolationFactory; + +public class EcmasccriptLanguageModuleTest { + private Rule rule = new AbstractEcmascriptRule() { }; + private ASTAstRoot node = JsParsingHelper.DEFAULT.parse("function a() {}"); + private LanguageVersion js = LanguageRegistry.getLanguage(EcmascriptLanguageModule.NAME).getDefaultVersion(); + private LanguageVersionHandler languageVersionHandler = js.getLanguageVersionHandler(); + private RuleViolationFactory ruleViolationFactory = languageVersionHandler.getRuleViolationFactory(); + + @Test + public void canCreateRuleViolation() { + RuleContext context = new RuleContext(); + ruleViolationFactory.addViolation(context, rule, node, "the message", new Object[0]); + Assert.assertEquals(1, context.getReport().getViolations().size()); + RuleViolation ruleViolation = context.getReport().getViolations().get(0); + Assert.assertEquals(1, ruleViolation.getBeginLine()); + } + + @Test + public void canCreateRuleViolationWithLineNumbers() { + RuleContext context = new RuleContext(); + ruleViolationFactory.addViolation(context, rule, node, "the message", 5, 7, new Object[0]); + Assert.assertEquals(1, context.getReport().getViolations().size()); + RuleViolation ruleViolation = context.getReport().getViolations().get(0); + Assert.assertEquals(5, ruleViolation.getBeginLine()); + Assert.assertEquals(7, ruleViolation.getEndLine()); + } +}