Search in sources :

Example 1 with AbstractEcmascriptRule

use of net.sourceforge.pmd.lang.ecmascript.rule.AbstractEcmascriptRule in project pmd by pmd.

the class EcmascriptParserTest method testLineNumbersWithinEcmascriptRules.

/**
 * https://sourceforge.net/p/pmd/bugs/1149/
 */
@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<>();
    class MyEcmascriptRule extends AbstractEcmascriptRule {

        public Object visit(ASTScope node, Object data) {
            output.add("Scope from " + node.getBeginLine() + " to " + node.getEndLine());
            return super.visit(node, data);
        }
    }
    MyEcmascriptRule rule = new MyEcmascriptRule();
    RuleContext ctx = new RuleContext();
    rule.apply(Arrays.asList(parse(source)), ctx);
    assertEquals("Scope from 2 to 4", output.get(0));
    assertEquals("Scope from 4 to 6", output.get(1));
}
Also used : RuleContext(net.sourceforge.pmd.RuleContext) ArrayList(java.util.ArrayList) AbstractEcmascriptRule(net.sourceforge.pmd.lang.ecmascript.rule.AbstractEcmascriptRule) Test(org.junit.Test)

Example 2 with AbstractEcmascriptRule

use of net.sourceforge.pmd.lang.ecmascript.rule.AbstractEcmascriptRule in project pmd by pmd.

the class ReportTest method testExclusionsInReportWithNOPMDEcmascript.

@Test
public void testExclusionsInReportWithNOPMDEcmascript() throws Exception {
    Report rpt = new Report();
    Rule rule = new AbstractEcmascriptRule() {

        @Override
        public Object visit(ASTFunctionNode node, Object data) {
            EcmascriptRuleViolationFactory.INSTANCE.addViolation((RuleContext) data, this, node, "Test", null);
            return super.visit(node, data);
        }
    };
    String code = "function(x) // NOPMD test suppress\n" + "{ x = 1; }";
    runTestFromString(code, rule, rpt, LanguageRegistry.getLanguage(EcmascriptLanguageModule.NAME).getDefaultVersion());
    assertTrue(rpt.isEmpty());
    assertEquals(1, rpt.getSuppressedRuleViolations().size());
}
Also used : ASTFunctionNode(net.sourceforge.pmd.lang.ecmascript.ast.ASTFunctionNode) AbstractEcmascriptRule(net.sourceforge.pmd.lang.ecmascript.rule.AbstractEcmascriptRule) AbstractEcmascriptRule(net.sourceforge.pmd.lang.ecmascript.rule.AbstractEcmascriptRule) Test(org.junit.Test)

Aggregations

AbstractEcmascriptRule (net.sourceforge.pmd.lang.ecmascript.rule.AbstractEcmascriptRule)2 Test (org.junit.Test)2 ArrayList (java.util.ArrayList)1 RuleContext (net.sourceforge.pmd.RuleContext)1 ASTFunctionNode (net.sourceforge.pmd.lang.ecmascript.ast.ASTFunctionNode)1