Debian Codegraph @

test/com/google/javascript/jscomp/CheckMissingReturnTest.java

raw symbols SCIP
1 /*
2 * Copyright 2008 The Closure Compiler Authors.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 package com.google.javascript.jscomp;
18
19 import com.google.javascript.jscomp.CheckLevel;
20
21 /**
22 * Tests for {@link CheckMissingReturn}.
23 *
24 */
25 public class CheckMissingReturnTest extends CompilerTestCase {
26
27 public CheckMissingReturnTest() {
28 enableTypeCheck(CheckLevel.OFF);
29 }
30
31 @Override
32 protected CompilerPass getProcessor(final Compiler compiler) {
33 return new CombinedCompilerPass(compiler,
34 new CheckMissingReturn(compiler, CheckLevel.ERROR));
35 }
36
37 public void testMissingReturn() {
38 // Requires control flow analysis.
39 testMissing("if (a) { return 1; }");
40
41 // Switch statement.
42 testMissing("switch(1) { case 12: return 5; }");
43
44 // Test try catch finally.
45 testMissing("try { foo() } catch (e) { return 5; } finally { }");
46
47 // Nested scope.
48 testMissing("/** @return {number} */ function f() { var x; }; return 1;");
49 testMissing("/** @return {number} */ function f() { return 1; };");
50 }
51
52 public void testReturnNotMissing() {
53 // Empty function body. Ignore this case. The remainder of the functions in
54 // this test have non-empty bodies.
55 testNotMissing("");
56
57 // Simple cases.
58 testSame("function f() { var x; }");
59 testNotMissing("return 1;");
60
61 // Returning void and undefined.
62 testNotMissing("void", "var x;");
63 testNotMissing("undefined", "var x;");
64
65 // Returning a union that includes void or undefined.
66 testNotMissing("number|undefined", "var x;");
67 testNotMissing("number|void", "var x;");
68 testNotMissing("(number,void)", "var x;");
69 testNotMissing("(number,undefined)", "var x;");
70 testNotMissing("*", "var x;");
71
72 // Test try catch finally.
73 testNotMissing("try { return foo() } catch (e) { } finally { }");
74
75 // Nested function.
76 testNotMissing(
77 "/** @return {number} */ function f() { return 1; }; return 1;");
78
79 // Strange tests that come up when reviewing closure code.
80 testNotMissing("try { return 12; } finally { return 62; }");
81 testNotMissing("try { } finally { return 1; }");
82 testNotMissing("switch(1) { default: return 1; }");
83 testNotMissing("switch(g) { case 1: return 1; default: return 2; }");
84 }
85
86 public void testFinallyStatements() {
87 // The control flow analysis (CFA) treats finally blocks somewhat strangely.
88 // The CFA might indicate that a finally block implicitly returns. However,
89 // if entry into the finally block is normally caused by an explicit return
90 // statement, then a return statement isn't missing:
91 //
92 // try {
93 // return 1;
94 // } finally {
95 // // CFA determines implicit return. However, return not missing
96 // // because of return statement in try block.
97 // }
98 //
99 // Hence extra tests are warranted for various cases involving finally
100 // blocks.
101
102 // Simple finally case.
103 testNotMissing("try { return 1; } finally { }");
104 testNotMissing("try { } finally { return 1; }");
105 testMissing("try { } finally { }");
106
107 // Cycles in the CFG within the finally block were causing problems before.
108 testNotMissing("try { return 1; } finally { while (true) { } }");
109 testMissing("try { } finally { while (x) { } }");
110 testMissing("try { } finally { while (x) { if (x) { break; } } }");
111 testNotMissing(
112 "try { return 2; } finally { while (x) { if (x) { break; } } }");
113
114 // Test various cases with nested try statements.
115 testMissing("try { } finally { try { } finally { } }");
116 testNotMissing("try { } finally { try { return 1; } finally { } }");
117 testNotMissing("try { return 1; } finally { try { } finally { } }");
118
119 // Calling a function potentially causes control flow to transfer to finally
120 // block. However, the function will not return in this case as the
121 // exception will unwind the stack. Hence this function isn't missing a
122 // return statement (i.e., the running program will not expect a return
123 // value from the function if an exception is thrown).
124 testNotMissing("try { g(); return 1; } finally { }");
125
126 // Closures within try ... finally affect missing return statement analysis
127 // because of the nested scopes. The following tests check for missing
128 // return statements in the three possible configurations: both scopes
129 // return; enclosed doesn't return; enclosing doesn't return.
130 testNotMissing(
131 "try {" +
132 " /** @return {number} */ function f() {" +
133 " try { return 1; }" +
134 " finally { }" +
135 " };" +
136 " return 1;" +
137 "}" +
138 "finally { }");
139 testMissing(
140 "try {" +
141 " /** @return {number} */ function f() {" +
142 " try { }" +
143 " finally { }" +
144 " };" +
145 " return 1;" +
146 "}" +
147 "finally { }");
148 testMissing(
149 "try {" +
150 " /** @return {number} */ function f() {" +
151 " try { return 1; }" +
152 " finally { }" +
153 " };" +
154 "}" +
155 "finally { }");
156 }
157
158 public void testKnownConditions() {
159 testNotMissing("if (true) return 1");
160 testMissing("if (true) {} else {return 1}");
161
162 testMissing("if (false) return 1");
163 testNotMissing("if (false) {} else {return 1}");
164
165 testNotMissing("if (1) return 1");
166 testMissing("if (1) {} else {return 1}");
167
168 testMissing("if (0) return 1");
169 testNotMissing("if (0) {} else {return 1}");
170
171 testNotMissing("if (3) return 1");
172 testMissing("if (3) {} else {return 1}");
173 }
174
175 public void testKnownWhileLoop() {
176 testNotMissing("while (1) return 1");
177 testNotMissing("while (1) { if (x) {return 1} else {return 1}}");
178 testNotMissing("while (0) {} return 1");
179
180 // TODO(user): The current algorithm will not detect this case. It is
181 // still computable in most cases.
182 testNotMissing("while (1) {} return 0");
183 testMissing("while (false) return 1");
184
185 // Not known.
186 testMissing("while(x) { return 1 }");
187 }
188
189 public void testMultiConditions() {
190 testMissing("if (a) { } else { while (1) {return 1} }");
191 testNotMissing("if (a) { return 1} else { while (1) {return 1} }");
192 }
193
194 public void testIssue779() {
195 testNotMissing(
196 "var a = f(); try { alert(); if (a > 0) return 1; }" +
197 "finally { a = 5; } return 2;");
198 }
199
200 private static String createFunction(String returnType, String body) {
201 return "/** @return {" + returnType + "} */ function foo() {" + body + "}";
202 }
203
204 private void testMissing(String returnType, String body) {
205 String js = createFunction(returnType, body);
206 test(js, js, CheckMissingReturn.MISSING_RETURN_STATEMENT);
207 }
208
209 private void testNotMissing(String returnType, String body) {
210 testSame(createFunction(returnType, body));
211 }
212
213 /** Creates function with return type {number} */
214 private void testNotMissing(String body) {
215 testNotMissing("number", body);
216 }
217
218 /** Creates function with return type {number} */
219 private void testMissing(String body) {
220 testMissing("number", body);
221 }
222 }