| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package com.google.javascript.jscomp; |
| 18 |
|
| 19 |
import com.google.common.base.Predicate; |
| 20 |
import com.google.javascript.jscomp.ControlFlowGraph.Branch; |
| 21 |
import com.google.javascript.jscomp.NodeTraversal.ScopedCallback; |
| 22 |
import com.google.javascript.jscomp.graph.DiGraph.DiGraphEdge; |
| 23 |
import com.google.javascript.rhino.Node; |
| 24 |
import com.google.javascript.rhino.jstype.FunctionType; |
| 25 |
import com.google.javascript.rhino.jstype.JSType; |
| 26 |
import com.google.javascript.rhino.jstype.JSTypeNative; |
| 27 |
import com.google.javascript.rhino.jstype.TernaryValue; |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
class CheckMissingReturn implements ScopedCallback { |
| 36 |
|
| 37 |
static final DiagnosticType MISSING_RETURN_STATEMENT = |
| 38 |
DiagnosticType.warning( |
| 39 |
"JSC_MISSING_RETURN_STATEMENT", |
| 40 |
"Missing return statement. Function expected to return {0}."); |
| 41 |
|
| 42 |
private final AbstractCompiler compiler; |
| 43 |
private final CheckLevel level; |
| 44 |
|
| 45 |
private static final Predicate<Node> IS_RETURN = new Predicate<Node>() { |
| 46 |
@Override |
| 47 |
public boolean apply(Node input) { |
| 48 |
|
| 49 |
|
| 50 |
return input != null && input.isReturn(); |
| 51 |
} |
| 52 |
}; |
| 53 |
|
| 54 |
|
| 55 |
private static final Predicate<DiGraphEdge<Node, ControlFlowGraph.Branch>> |
| 56 |
GOES_THROUGH_TRUE_CONDITION_PREDICATE = |
| 57 |
new Predicate<DiGraphEdge<Node, ControlFlowGraph.Branch>>() { |
| 58 |
@Override |
| 59 |
public boolean apply(DiGraphEdge<Node, ControlFlowGraph.Branch> input) { |
| 60 |
|
| 61 |
Branch branch = input.getValue(); |
| 62 |
if (branch == Branch.ON_EX) { |
| 63 |
return false; |
| 64 |
} else if (branch.isConditional()) { |
| 65 |
Node condition = NodeUtil.getConditionExpression( |
| 66 |
input.getSource().getValue()); |
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
if (condition != null) { |
| 72 |
TernaryValue val = NodeUtil.getImpureBooleanValue(condition); |
| 73 |
if (val != TernaryValue.UNKNOWN) { |
| 74 |
return val.toBoolean(true) == (Branch.ON_TRUE == branch); |
| 75 |
} |
| 76 |
} |
| 77 |
} |
| 78 |
return true; |
| 79 |
} |
| 80 |
}; |
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
CheckMissingReturn(AbstractCompiler compiler, CheckLevel level) { |
| 87 |
this.compiler = compiler; |
| 88 |
this.level = level; |
| 89 |
} |
| 90 |
|
| 91 |
@Override |
| 92 |
public void enterScope(NodeTraversal t) { |
| 93 |
JSType returnType = explicitReturnExpected(t.getScopeRoot()); |
| 94 |
if (returnType == null) { |
| 95 |
return; |
| 96 |
} |
| 97 |
|
| 98 |
if (fastAllPathsReturnCheck(t.getControlFlowGraph())) { |
| 99 |
return; |
| 100 |
} |
| 101 |
|
| 102 |
CheckPathsBetweenNodes<Node, ControlFlowGraph.Branch> test = |
| 103 |
new CheckPathsBetweenNodes<Node, ControlFlowGraph.Branch>( |
| 104 |
t.getControlFlowGraph(), |
| 105 |
t.getControlFlowGraph().getEntry(), |
| 106 |
t.getControlFlowGraph().getImplicitReturn(), |
| 107 |
IS_RETURN, GOES_THROUGH_TRUE_CONDITION_PREDICATE); |
| 108 |
|
| 109 |
if (!test.allPathsSatisfyPredicate()) { |
| 110 |
compiler.report( |
| 111 |
t.makeError(t.getScopeRoot(), level, |
| 112 |
MISSING_RETURN_STATEMENT, returnType.toString())); |
| 113 |
} |
| 114 |
} |
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
private static boolean fastAllPathsReturnCheck(ControlFlowGraph<Node> cfg) { |
| 123 |
for (DiGraphEdge<Node, Branch> s : cfg.getImplicitReturn().getInEdges()) { |
| 124 |
if (!s.getSource().getValue().isReturn()) { |
| 125 |
return false; |
| 126 |
} |
| 127 |
} |
| 128 |
return true; |
| 129 |
} |
| 130 |
|
| 131 |
@Override |
| 132 |
public void exitScope(NodeTraversal t) { |
| 133 |
} |
| 134 |
|
| 135 |
@Override |
| 136 |
public boolean shouldTraverse( |
| 137 |
NodeTraversal nodeTraversal, Node n, Node parent) { |
| 138 |
return true; |
| 139 |
} |
| 140 |
|
| 141 |
@Override |
| 142 |
public void visit(NodeTraversal t, Node n, Node parent) { |
| 143 |
} |
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
private JSType explicitReturnExpected(Node scope) { |
| 151 |
FunctionType scopeType = JSType.toMaybeFunctionType(scope.getJSType()); |
| 152 |
|
| 153 |
if (scopeType == null) { |
| 154 |
return null; |
| 155 |
} |
| 156 |
|
| 157 |
if (isEmptyFunction(scope)) { |
| 158 |
return null; |
| 159 |
} |
| 160 |
|
| 161 |
JSType returnType = scopeType.getReturnType(); |
| 162 |
|
| 163 |
if (returnType == null) { |
| 164 |
return null; |
| 165 |
} |
| 166 |
|
| 167 |
if (!isVoidOrUnknown(returnType)) { |
| 168 |
return returnType; |
| 169 |
} |
| 170 |
|
| 171 |
return null; |
| 172 |
} |
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
private static boolean isEmptyFunction(Node function) { |
| 180 |
return function.getChildCount() == 3 && |
| 181 |
!function.getFirstChild().getNext().getNext().hasChildren(); |
| 182 |
} |
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
private boolean isVoidOrUnknown(JSType returnType) { |
| 189 |
final JSType voidType = |
| 190 |
compiler.getTypeRegistry().getNativeType(JSTypeNative.VOID_TYPE); |
| 191 |
return voidType.isSubtype(returnType); |
| 192 |
} |
| 193 |
} |