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));
}
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());
}
Aggregations