Search in sources :

Example 1 with ASTFunctionNode

use of net.sourceforge.pmd.lang.ecmascript.ast.ASTFunctionNode in project pmd by pmd.

the class ConsistentReturnRule method visit.

@Override
public Object visit(ASTFunctionNode functionNode, Object data) {
    List<ASTReturnStatement> returnStatements = functionNode.findDescendantsOfType(ASTReturnStatement.class);
    Boolean hasResult = null;
    for (ASTReturnStatement returnStatement : returnStatements) {
        // Return for this function?
        if (functionNode == returnStatement.getFirstParentOfType(ASTFunctionNode.class)) {
            if (hasResult == null) {
                hasResult = Boolean.valueOf(returnStatement.hasResult());
            } else {
                // Return has different result from previous return?
                if (hasResult.booleanValue() != returnStatement.hasResult()) {
                    addViolation(data, functionNode);
                    break;
                }
            }
        }
    }
    return data;
}
Also used : ASTFunctionNode(net.sourceforge.pmd.lang.ecmascript.ast.ASTFunctionNode) ASTReturnStatement(net.sourceforge.pmd.lang.ecmascript.ast.ASTReturnStatement)

Example 2 with ASTFunctionNode

use of net.sourceforge.pmd.lang.ecmascript.ast.ASTFunctionNode 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

ASTFunctionNode (net.sourceforge.pmd.lang.ecmascript.ast.ASTFunctionNode)2 ASTReturnStatement (net.sourceforge.pmd.lang.ecmascript.ast.ASTReturnStatement)1 AbstractEcmascriptRule (net.sourceforge.pmd.lang.ecmascript.rule.AbstractEcmascriptRule)1 Test (org.junit.Test)1