diff --git a/docs/pages/release_notes.md b/docs/pages/release_notes.md index a1589b09ed..1adde2f5be 100644 --- a/docs/pages/release_notes.md +++ b/docs/pages/release_notes.md @@ -27,6 +27,8 @@ See [credits](https://pmd.github.io/latest/pmd_projectdocs_credits.html) for our * [#3499](https://github.com/pmd/pmd/pull/3499): \[core] Fix XPath rulechain with combined node tests * java-errorprone * [#3493](https://github.com/pmd/pmd/pull/3493): \[java] AvoidAccessibilityAlteration: add tests and fix rule +* javascript + * [#3516](https://github.com/pmd/pmd/pull/3516): \[javascript] NPE while creating rule violation when specifying explicit line numbers * plsql * [#3487](https://github.com/pmd/pmd/issues/3487): \[plsql] Parsing exception OPEN ref_cursor_name FOR statement * [#3515](https://github.com/pmd/pmd/issues/3515): \[plsql] Parsing exception SELECT...INTO on Associative Arrays Types @@ -35,5 +37,7 @@ See [credits](https://pmd.github.io/latest/pmd_projectdocs_credits.html) for our ### External Contributions +* [#3516](https://github.com/pmd/pmd/pull/3516): \[javascript] NPE while creating rule violation when specifying explicit line numbers - [Kevin Guerra](https://github.com/kevingnet) + {% endtocmaker %} 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 55b3a08c3b..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 null; // FIXME + 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()); + } +}