-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
CheckMissingReturnTest.java
360 lines (297 loc) · 12.4 KB
/
CheckMissingReturnTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
/*
* Copyright 2008 The Closure Compiler Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.javascript.jscomp;
import com.google.javascript.jscomp.CompilerOptions.LanguageMode;
/**
* Tests for {@link CheckMissingReturn}.
*
*/
public final class CheckMissingReturnTest extends CompilerTestCase {
@Override
protected void setUp() throws Exception {
super.setUp();
enableTypeCheck();
}
@Override
protected CompilerPass getProcessor(final Compiler compiler) {
return new CombinedCompilerPass(compiler, new CheckMissingReturn(compiler));
}
public void testMissingReturn() {
// Requires control flow analysis.
testMissing("if (a) { return 1; }");
// Switch statement.
testMissing("switch(1) { case 12: return 5; }");
// Test try catch finally.
testMissing("try { foo() } catch (e) { return 5; } finally { }");
// Nested scope.
testMissing("/** @return {number} */ function f() { var x; }; return 1;");
testMissing("/** @return {number} */ function f() { return 1; };");
}
public void testReturnNotMissing() {
// Empty function body. Ignore this case. The remainder of the functions in
// this test have non-empty bodies.
testNotMissing("");
// Simple cases.
testSame("function f() { var x; }");
testNotMissing("return 1;");
// Returning void and undefined.
testNotMissing("void", "var x;");
testNotMissing("undefined", "var x;");
// Returning a union that includes void or undefined.
testNotMissing("number|undefined", "var x;");
testNotMissing("number|void", "var x;");
testNotMissing("*", "var x;");
// Test try catch finally.
testNotMissing("try { return foo() } catch (e) { } finally { }");
testNotMissing("try {throw e;} catch (e) { return foo() } finally { }");
testNotMissing("try {} catch (e) {} finally {return foo()};");
// Nested function.
testNotMissing(
"/** @return {number} */ function f() { return 1; }; return 1;");
// Strange tests that come up when reviewing closure code.
testNotMissing("try { return 12; } finally { return 62; }");
testNotMissing("try { } finally { return 1; }");
testNotMissing("switch(1) { default: return 1; }");
testNotMissing("switch(g) { case 1: return 1; default: return 2; }");
}
public void testFinallyStatements() {
// The control flow analysis (CFA) treats finally blocks somewhat strangely.
// The CFA might indicate that a finally block implicitly returns. However,
// if entry into the finally block is normally caused by an explicit return
// statement, then a return statement isn't missing:
//
// try {
// return 1;
// } finally {
// // CFA determines implicit return. However, return not missing
// // because of return statement in try block.
// }
//
// Hence extra tests are warranted for various cases involving finally
// blocks.
// Simple finally case.
testNotMissing("try { return 1; } finally { }");
testNotMissing("try { } finally { return 1; }");
testMissing("try { } finally { }");
// Cycles in the CFG within the finally block were causing problems before.
testNotMissing("try { return 1; } finally { while (true) { } }");
testMissing("try { } finally { while (x) { } }");
testMissing("try { } finally { while (x) { if (x) { break; } } }");
testNotMissing(
"try { return 2; } finally { while (x) { if (x) { break; } } }");
// Test various cases with nested try statements.
testMissing("try { } finally { try { } finally { } }");
testNotMissing("try { } finally { try { return 1; } finally { } }");
testNotMissing("try { return 1; } finally { try { } finally { } }");
// Calling a function potentially causes control flow to transfer to finally
// block. However, the function will not return in this case as the
// exception will unwind the stack. Hence this function isn't missing a
// return statement (i.e., the running program will not expect a return
// value from the function if an exception is thrown).
testNotMissing("try { g(); return 1; } finally { }");
// Closures within try ... finally affect missing return statement analysis
// because of the nested scopes. The following tests check for missing
// return statements in the three possible configurations: both scopes
// return; enclosed doesn't return; enclosing doesn't return.
testNotMissing(
"try {"
+ " /** @return {number} */ function f() {"
+ " try { return 1; }"
+ " finally { }"
+ " };"
+ " return 1;"
+ "}"
+ "finally { }");
testMissing(
"try {"
+ " /** @return {number} */ function f() {"
+ " try { }"
+ " finally { }"
+ " };"
+ " return 1;"
+ "}"
+ "finally { }");
testMissing(
"try {"
+ " /** @return {number} */ function f() {"
+ " try { return 1; }"
+ " finally { }"
+ " };"
+ "}"
+ "finally { }");
}
public void testKnownConditions() {
testNotMissing("if (true) return 1");
testMissing("if (true) {} else {return 1}");
testMissing("if (false) return 1");
testNotMissing("if (false) {} else {return 1}");
testNotMissing("if (1) return 1");
testMissing("if (1) {} else {return 1}");
testMissing("if (0) return 1");
testNotMissing("if (0) {} else {return 1}");
testNotMissing("if (3) return 1");
testMissing("if (3) {} else {return 1}");
}
public void testKnownWhileLoop() {
testNotMissing("while (1) return 1");
testNotMissing("while (1) { if (x) {return 1} else {return 1}}");
testNotMissing("while (0) {} return 1");
// TODO(user): The current algorithm will not detect this case. It is
// still computable in most cases.
testNotMissing("while (1) {} return 0");
testMissing("while (false) return 1");
// Not known.
testMissing("while(x) { return 1 }");
}
public void testMultiConditions() {
testMissing("if (a) { } else { while (1) {return 1} }");
testNotMissing("if (a) { return 1} else { while (1) {return 1} }");
}
public void testIssue779() {
testNotMissing(
"var a = f(); try { alert(); if (a > 0) return 1; }" + "finally { a = 5; } return 2;");
}
public void testConstructors() {
testSame("/** @constructor */ function foo() {} ");
final String constructorWithReturn = "/** @constructor \n" +
" * @return {!foo} */ function foo() {" +
" if (!(this instanceof foo)) { return new foo; } }";
testSame(constructorWithReturn);
}
public void testClosureAsserts() {
String closureDefs =
"/** @const */ var goog = {};\n"
+ "goog.asserts = {};\n"
+ "goog.asserts.fail = function(x) {};";
testNotMissing(closureDefs + "goog.asserts.fail('');");
testNotMissing(closureDefs
+ "switch (x) { case 1: return 1; default: goog.asserts.fail(''); }");
}
public void testInfiniteLoops() {
testNotMissing("while (true) { x = y; if (x === 0) { return 1; } }");
testNotMissing("for (;true;) { x = y; if (x === 0) { return 1; } }");
testNotMissing("for (;;) { x = y; if (x === 0) { return 1; } }");
}
private static String createFunction(String returnType, String body) {
return "/** @return {" + returnType + "} */ function foo() {" + body + "}";
}
private static String createShorthandFunctionInObjLit(
String returnType, String body) {
return lines(
"var obj = {",
" /** @return {" + returnType + "} */",
" foo() {", body, "}",
"}");
}
private void testMissingInTraditionalFunction(String returnType, String body) {
String js = createFunction(returnType, body);
testWarning(js, CheckMissingReturn.MISSING_RETURN_STATEMENT);
}
private void testNotMissingInTraditionalFunction(String returnType, String body) {
testSame(createFunction(returnType, body));
}
private void testMissingInShorthandFunction(String returnType, String body) {
setAcceptedLanguage(LanguageMode.ECMASCRIPT_2015);
String js = createShorthandFunctionInObjLit(returnType, body);
testWarning(js, CheckMissingReturn.MISSING_RETURN_STATEMENT);
}
private void testNotMissingInShorthandFunction(String returnType, String body) {
setAcceptedLanguage(LanguageMode.ECMASCRIPT_2015);
testSame(createShorthandFunctionInObjLit(returnType, body));
}
private void testMissing(String returnType, String body) {
testMissingInTraditionalFunction(returnType, body);
testMissingInShorthandFunction(returnType, body);
}
/** Creates function with return type {number} */
private void testMissing(String body) {
testMissing("number", body);
}
private void testNotMissing(String returnType, String body) {
testNotMissingInTraditionalFunction(returnType, body);
testNotMissingInShorthandFunction(returnType, body);
}
/** Creates function with return type {number} */
private void testNotMissing(String body) {
testNotMissing("number", body);
}
public void testArrowFunctions_noReturn() {
setAcceptedLanguage(LanguageMode.ECMASCRIPT_2015);
testNoWarning(lines("/** @return {undefined} */", "() => {}"));
}
public void testArrowFunctions_expressionBody1() {
setAcceptedLanguage(LanguageMode.ECMASCRIPT_2015);
testSame(lines("/** @return {number} */", "() => 1"));
}
public void testArrowFunctions_expressionBody2() {
setAcceptedLanguage(LanguageMode.ECMASCRIPT_2015);
testSame(lines("/** @return {number} */", "(a) => (a > 3) ? 1 : 0"));
}
public void testArrowFunctions_block() {
setAcceptedLanguage(LanguageMode.ECMASCRIPT_2015);
testSame(
lines(
"/** @return {number} */", "(a) => { if (a > 3) { return 1; } else { return 0; }}"));
}
public void testArrowFunctions_blockMissingReturn() {
setAcceptedLanguage(LanguageMode.ECMASCRIPT_2015);
testWarning(
lines("/** @return {number} */", "(a) => { if (a > 3) { return 1; } else { } }"),
CheckMissingReturn.MISSING_RETURN_STATEMENT);
}
public void testArrowFunctions_objectLiteralExpression() {
setAcceptedLanguage(LanguageMode.ECMASCRIPT_2015);
testSame("(a) => ({foo: 1});");
}
public void testGeneratorFunctionDoesntWarn() {
setAcceptedLanguage(LanguageMode.ECMASCRIPT_2015);
testNoWarning("function *gen() {}");
testNoWarning(
lines(
"/** @return {!Generator<number>} */", // no yields is OK
"function *gen() {}"));
testNoWarning(
lines(
"/** @return {!Generator<number>} */", // one yield is OK
"function *gen() {",
" yield 1;",
"}"));
testNoWarning(
lines(
"/** @return {!Object} */", // Return type more vague than Generator is also OK
"function *gen() {}"));
}
public void testAsyncFunction_noJSDoc() {
setAcceptedLanguage(LanguageMode.ECMASCRIPT_2017);
// Note: we add the alert because CheckMissingReturn never warns on functions with empty bodies.
testNoWarning("async function foo() { alert(1); }");
}
public void testAsyncFunction_returnsUndefined() {
setAcceptedLanguage(LanguageMode.ECMASCRIPT_2017);
testNoWarning("/** @return {!Promise<undefined>} */ async function foo() { alert(1); }");
}
public void testAsyncFunction_returnsUnknown() {
setAcceptedLanguage(LanguageMode.ECMASCRIPT_2017);
testNoWarning("/** @return {!Promise<?>} */ async function foo() { alert(1); }");
}
public void testAsyncFunction_returnsNumber() {
setAcceptedLanguage(LanguageMode.ECMASCRIPT_2017);
testNoWarning("/** @return {!Promise<number>} */ async function foo() { return 1; }");
testWarning(
"/** @return {!Promise<string>} */ async function foo() { alert(1); }",
CheckMissingReturn.MISSING_RETURN_STATEMENT);
}
}