ConsistentReturnRule.java
01 /**
02  * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
03  */
04 package net.sourceforge.pmd.lang.ecmascript.rule.basic;
05 
06 import java.util.List;
07 
08 import net.sourceforge.pmd.lang.ecmascript.ast.ASTFunctionNode;
09 import net.sourceforge.pmd.lang.ecmascript.ast.ASTReturnStatement;
10 import net.sourceforge.pmd.lang.ecmascript.rule.AbstractEcmascriptRule;
11 
12 public class ConsistentReturnRule extends AbstractEcmascriptRule {
13 
14     public ConsistentReturnRule() {
15   addRuleChainVisit(ASTFunctionNode.class);
16     }
17 
18     @Override
19     public Object visit(ASTFunctionNode functionNode, Object data) {
20   List<ASTReturnStatement> returnStatements = functionNode.findDescendantsOfType(ASTReturnStatement.class);
21   Boolean hasResult = null;
22   for (ASTReturnStatement returnStatement : returnStatements) {
23       // Return for this function?
24       if (functionNode == returnStatement.getFirstParentOfType(ASTFunctionNode.class)) {
25     if (hasResult == null) {
26         hasResult = Boolean.valueOf(returnStatement.hasResult());
27     else {
28         // Return has different result from previous return?
29         if (hasResult.booleanValue() != returnStatement.hasResult()) {
30       addViolation(data, functionNode);
31       break;
32         }
33     }
34       }
35   }
36   return data;
37     }
38 }