-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
FunctionArgumentInjectorTest.java
579 lines (504 loc) · 17.5 KB
/
FunctionArgumentInjectorTest.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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
/*
* Copyright 2009 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 static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.truth.Truth.assertThat;
import static com.google.javascript.jscomp.CompilerOptions.LanguageMode.ECMASCRIPT_2017;
import com.google.common.base.Supplier;
import com.google.common.collect.ImmutableSet;
import com.google.javascript.rhino.Node;
import java.util.Collections;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;
import junit.framework.TestCase;
/**
* Inline function tests.
* @author johnlenz@google.com (John Lenz)
*/
public final class FunctionArgumentInjectorTest extends TestCase {
// TODO(johnlenz): Add unit tests for:
// inject
// getFunctionCallParameterMap
private static final Set<String> EMPTY_STRING_SET = Collections.emptySet();
public void testFindModifiedParameters0() {
assertThat(
FunctionArgumentInjector.findModifiedParameters(
parseFunction("function f(a){ return a; }"))).isEmpty();
}
public void testFindModifiedParameters1() {
assertThat(
FunctionArgumentInjector.findModifiedParameters(
parseFunction("function f(a){ return a==0; }"))).isEmpty();
}
public void testFindModifiedParameters2() {
assertThat(
FunctionArgumentInjector.findModifiedParameters(parseFunction("function f(a){ b=a }")))
.isEmpty();
}
public void testFindModifiedParameters3() {
assertEquals(ImmutableSet.of("a"),
FunctionArgumentInjector.findModifiedParameters(
parseFunction("function f(a){ a=0 }")));
}
public void testFindModifiedParameters4() {
assertEquals(ImmutableSet.of("a", "b"),
FunctionArgumentInjector.findModifiedParameters(
parseFunction("function f(a,b){ a=0;b=0 }")));
}
public void testFindModifiedParameters5() {
assertEquals(ImmutableSet.of("b"),
FunctionArgumentInjector.findModifiedParameters(
parseFunction("function f(a,b){ a; if (a) b=0 }")));
}
public void testFindModifiedParameters6() {
assertEquals(ImmutableSet.of("a", "b"),
FunctionArgumentInjector.findModifiedParameters(
parseFunction("function f(a,b){ function f(){ a;b; } }")));
}
public void testFindModifiedParameters7() {
assertEquals(ImmutableSet.of("b"),
FunctionArgumentInjector.findModifiedParameters(
parseFunction("function f(a,b){ a; function f(){ b; } }")));
}
public void testFindModifiedParameters8() {
assertEquals(ImmutableSet.of("b"),
FunctionArgumentInjector.findModifiedParameters(
parseFunction(
"function f(a,b){ " +
"a; function f(){ function g() { b; } } }")));
}
public void testFindModifiedParameters9() {
assertEquals(ImmutableSet.of("a", "b"),
FunctionArgumentInjector.findModifiedParameters(
parseFunction("function f(a,b){ (function(){ a;b; }) }")));
}
public void testFindModifiedParameters10() {
assertEquals(ImmutableSet.of("b"),
FunctionArgumentInjector.findModifiedParameters(
parseFunction("function f(a,b){ a; (function (){ b; }) }")));
}
public void testFindModifiedParameters11() {
assertEquals(ImmutableSet.of("b"),
FunctionArgumentInjector.findModifiedParameters(
parseFunction(
"function f(a,b){ " +
"a; (function(){ (function () { b; }) }) }")));
}
public void testFindModifiedParameters12() {
assertThat(
FunctionArgumentInjector.findModifiedParameters(
parseFunction("function f(a=5){ return a;} f(1);"))).isEmpty();
}
public void testMaybeAddTempsForCallArguments1() {
// Parameters with side-effects must be executed
// even if they aren't referenced.
testNeededTemps(
"function foo(a,b){}; foo(goo(),goo());",
"foo",
ImmutableSet.of("a", "b"));
}
public void testMaybeAddTempsForCallArguments2() {
// Unreferenced parameters without side-effects
// can be ignored.
testNeededTemps(
"function foo(a,b){}; foo(1,2);",
"foo",
EMPTY_STRING_SET);
}
public void testMaybeAddTempsForCallArguments3() {
// Referenced parameters without side-effects
// don't need temps.
testNeededTemps(
"function foo(a,b){a;b;}; foo(x,y);",
"foo",
EMPTY_STRING_SET);
}
public void testMaybeAddTempsForCallArguments4() {
// Parameters referenced after side-effect must
// be assigned to temps.
testNeededTemps(
"function foo(a,b){a;goo();b;}; foo(x,y);",
"foo",
ImmutableSet.of("b"));
}
public void testMaybeAddTempsForCallArguments5() {
// Parameters referenced after out-of-scope side-effect must
// be assigned to temps.
testNeededTemps(
"function foo(a,b){x = b; y = a;}; foo(x,y);",
"foo",
ImmutableSet.of("a"));
}
public void testMaybeAddTempsForCallArguments6() {
// Parameter referenced after a out-of-scope side-effect must
// be assigned to a temp.
testNeededTemps(
"function foo(a){x++;a;}; foo(x);",
"foo",
ImmutableSet.of("a"));
}
public void testMaybeAddTempsForCallArguments7() {
// No temp needed after local side-effects.
testNeededTemps(
"function foo(a){var c; c=0; a;}; foo(x);",
"foo",
EMPTY_STRING_SET);
}
public void testMaybeAddTempsForCallArguments8() {
// Temp needed for side-effects to object using local name.
testNeededTemps(
"function foo(a){var c = {}; c.goo=0; a;}; foo(x);",
"foo",
ImmutableSet.of("a"));
}
public void testMaybeAddTempsForCallArguments9() {
// Parameters referenced in a loop with side-effects must
// be assigned to temps.
testNeededTemps(
"function foo(a,b){while(true){a;goo();b;}}; foo(x,y);",
"foo",
ImmutableSet.of("a", "b"));
}
public void testMaybeAddTempsForCallArguments10() {
// No temps for parameters referenced in a loop with no side-effects.
testNeededTemps(
"function foo(a,b){while(true){a;true;b;}}; foo(x,y);",
"foo",
EMPTY_STRING_SET);
}
public void testMaybeAddTempsForCallArguments11() {
// Parameters referenced in a loop with side-effects must
// be assigned to temps.
testNeededTemps(
"function foo(a,b){do{a;b;}while(goo());}; foo(x,y);",
"foo",
ImmutableSet.of("a", "b"));
}
public void testMaybeAddTempsForCallArguments12() {
// Parameters referenced in a loop with side-effects must
// be assigned to temps.
testNeededTemps(
"function foo(a,b){for(;;){a;b;goo();}}; foo(x,y);",
"foo",
ImmutableSet.of("a", "b"));
}
public void testMaybeAddTempsForCallArguments13() {
// Parameters referenced in a inner loop without side-effects must
// be assigned to temps if the outer loop has side-effects.
testNeededTemps(
"function foo(a,b){for(;;){for(;;){a;b;}goo();}}; foo(x,y);",
"foo",
ImmutableSet.of("a", "b"));
}
public void testMaybeAddTempsForCallArguments14() {
// Parameters referenced in a loop must
// be assigned to temps.
testNeededTemps(
"function foo(a,b){goo();for(;;){a;b;}}; foo(x,y);",
"foo",
ImmutableSet.of("a", "b"));
}
public void testMaybeAddTempsForCallArguments20() {
// A long string referenced more than once should have a temp.
testNeededTemps(
"function foo(a){a;a;}; foo(\"blah blah\");",
"foo",
ImmutableSet.of("a"));
}
public void testMaybeAddTempsForCallArguments21() {
// A short string referenced once should not have a temp.
testNeededTemps(
"function foo(a){a;a;}; foo(\"\");",
"foo",
EMPTY_STRING_SET);
}
public void testMaybeAddTempsForCallArguments22() {
// A object literal not referenced.
testNeededTemps(
"function foo(a){}; foo({x:1});",
"foo",
EMPTY_STRING_SET);
// A object literal referenced after side-effect, should have a temp.
testNeededTemps(
"function foo(a){alert('foo');a;}; foo({x:1});",
"foo",
ImmutableSet.of("a"));
// A object literal referenced after side-effect, should have a temp.
testNeededTemps(
"function foo(a,b){b;a;}; foo({x:1},alert('foo'));",
"foo",
ImmutableSet.of("a", "b"));
// A object literal, referenced more than once, should have a temp.
testNeededTemps(
"function foo(a){a;a;}; foo({x:1});",
"foo",
ImmutableSet.of("a"));
}
public void testMaybeAddTempsForCallArguments22b() {
// A object literal not referenced.
testNeededTemps(
"function foo(a){a(this)}; foo.call(f(),g());",
"foo",
ImmutableSet.of("a", "this"));
}
public void testMaybeAddTempsForCallArguments23() {
// A array literal, not referenced.
testNeededTemps(
"function foo(a){}; foo([1,2]);",
"foo",
EMPTY_STRING_SET);
// A array literal, referenced once after side-effect, should have a temp.
testNeededTemps(
"function foo(a){alert('foo');a;}; foo([1,2]);",
"foo",
ImmutableSet.of("a"));
// A array literal, referenced more than once, should have a temp.
testNeededTemps(
"function foo(a){a;a;}; foo([1,2]);",
"foo",
ImmutableSet.of("a"));
}
public void testMaybeAddTempsForCallArguments24() {
// A regex literal, not referenced.
testNeededTemps(
"function foo(a){}; foo(/mac/);",
"foo",
EMPTY_STRING_SET);
// A regex literal, referenced once after side-effect, should have a temp.
testNeededTemps(
"function foo(a){alert('foo');a;}; foo(/mac/);",
"foo",
ImmutableSet.of("a"));
// A regex literal, referenced more than once, should have a temp.
testNeededTemps(
"function foo(a){a;a;}; foo(/mac/);",
"foo",
ImmutableSet.of("a"));
}
public void testMaybeAddTempsForCallArguments25() {
// A side-effect-less constructor, not referenced.
testNeededTemps(
"function foo(a){}; foo(new Date());",
"foo",
EMPTY_STRING_SET);
// A side-effect-less constructor, referenced once after sideeffect, should have a temp.
testNeededTemps(
"function foo(a){alert('foo');a;}; foo(new Date());",
"foo",
ImmutableSet.of("a"));
// A side-effect-less constructor, referenced more than once, should have
// a temp.
testNeededTemps(
"function foo(a){a;a;}; foo(new Date());",
"foo",
ImmutableSet.of("a"));
}
public void testMaybeAddTempsForCallArguments26() {
// A constructor, not referenced.
testNeededTemps(
"function foo(a){}; foo(new Bar());",
"foo",
ImmutableSet.of("a"));
// A constructor, referenced once after a sideeffect, should have a temp.
testNeededTemps(
"function foo(a){alert('foo');a;}; foo(new Bar());",
"foo",
ImmutableSet.of("a"));
// A constructor, referenced more than once, should have a temp.
testNeededTemps(
"function foo(a){a;a;}; foo(new Bar());",
"foo",
ImmutableSet.of("a"));
}
public void testMaybeAddTempsForCallArguments27() {
// Ensure the correct parameter is given a temp, when there is
// a this value in the call.
testNeededTemps(
"function foo(a,b,c){}; foo.call(this,1,goo(),2);",
"foo",
ImmutableSet.of("b"));
}
public void testMaybeAddTempsForCallArguments28() {
// true/false are don't need temps
testNeededTemps(
"function foo(a){a;a;}; foo(true);",
"foo",
EMPTY_STRING_SET);
}
public void testMaybeAddTempsForCallArguments29() {
// true/false are don't need temps
testNeededTemps(
"function foo(a){a;a;}; foo(false);",
"foo",
EMPTY_STRING_SET);
}
public void testMaybeAddTempsForCallArguments30() {
// true/false are don't need temps
testNeededTemps(
"function foo(a){a;a;}; foo(!0);",
"foo",
EMPTY_STRING_SET);
}
public void testMaybeAddTempsForCallArguments31() {
// true/false are don't need temps
testNeededTemps(
"function foo(a){a;a;}; foo(!1);",
"foo",
EMPTY_STRING_SET);
}
public void testMaybeAddTempsForCallArguments32() {
// void 0 doesn't need a temp
testNeededTemps(
"function foo(a){a;a;}; foo(void 0);",
"foo",
EMPTY_STRING_SET);
}
public void testMaybeAddTempsForCallArguments33() {
// doesn't need a temp
testNeededTemps(
"function foo(a){return a;}; foo(new X);",
"foo",
EMPTY_STRING_SET);
}
public void testMaybeAddTempsForCallArgumentsInLoops() {
// A mutable parameter referenced in loop needs a
// temporary.
testNeededTemps(
"function foo(a){for(;;)a;}; foo(new Bar());",
"foo",
ImmutableSet.of("a"));
testNeededTemps(
"function foo(a){while(true)a;}; foo(new Bar());",
"foo",
ImmutableSet.of("a"));
testNeededTemps(
"function foo(a){do{a;}while(true)}; foo(new Bar());",
"foo",
ImmutableSet.of("a"));
}
public void testMaybeAddTempsForCallArgumentsDefault1() {
testNeededTemps("function foo(a = 5){return a;} foo(1);", "foo", EMPTY_STRING_SET);
}
public void testMaybeAddTempsForCallArgumentsDefault2() {
testNeededTemps("function foo(a = 5, b = 2){a; bar(); b;} foo(1, 2);", "foo", EMPTY_STRING_SET);
}
public void testMaybeAddTempsForCallArgumentsRestParam1() {
testNeededTemps("function foo(...args) {return args;} foo(1, 2);", "foo", EMPTY_STRING_SET);
}
public void testMaybeAddTempsForCallArgumentsRestParam2() {
testNeededTemps(
"function foo(x, ...args) {return args;} foo(1, 2);", "foo", ImmutableSet.of("args"));
}
public void testMaybeAddTempsForCallArgumentsObjectLit1() {
testNeededTemps(
"function foo(x, ...{length: length}) {return length;} foo(1, 1, 1);",
"foo",
EMPTY_STRING_SET);
}
public void testArgMapWithDefaultParam1() {
assertArgMapHasKeys(
"function foo(a = 5, b = 2){return a;} foo();", "foo", ImmutableSet.of("this", "a", "b"));
}
public void testArgMapWithRestParam1() {
assertArgMapHasKeys(
"function foo(...args){return args;} foo(1, 2);", "foo", ImmutableSet.of("this", "args"));
}
private void assertArgMapHasKeys(String code, String fnName, Set<String> expectedKeys) {
Node n = parse(code);
Node fn = findFunction(n, fnName);
assertNotNull(fn);
Node call = findCall(n, fnName);
assertNotNull(call);
LinkedHashMap<String, Node> actualMap =
FunctionArgumentInjector.getFunctionCallParameterMap(fn, call, getNameSupplier());
assertThat(actualMap.keySet()).isEqualTo(expectedKeys);
}
private void testNeededTemps(String code, String fnName, Set<String> expectedTemps) {
Node n = parse(code);
Node fn = findFunction(n, fnName);
assertNotNull(fn);
Node call = findCall(n, fnName);
assertNotNull(call);
Map<String, Node> args =
FunctionArgumentInjector.getFunctionCallParameterMap(
fn, call, getNameSupplier());
Set<String> actualTemps = new HashSet<>();
FunctionArgumentInjector.maybeAddTempsForCallArguments(
fn, args, actualTemps, new ClosureCodingConvention());
assertEquals(expectedTemps, actualTemps);
}
private static Supplier<String> getNameSupplier() {
return new Supplier<String>() {
int i = 0;
@Override
public String get() {
return String.valueOf(i++);
}
};
}
private static Node findCall(Node n, String name) {
if (n.isCall()) {
Node callee;
if (NodeUtil.isGet(n.getFirstChild())) {
callee = n.getFirstFirstChild();
Node prop = callee.getNext();
// Only "call" is support at this point.
checkArgument(prop.isString() && prop.getString().equals("call"));
} else {
callee = n.getFirstChild();
}
if (callee.isName()
&& callee.getString().equals(name)) {
return n;
}
}
for (Node c : n.children()) {
Node result = findCall(c, name);
if (result != null) {
return result;
}
}
return null;
}
private static Node findFunction(Node n, String name) {
if (n.isFunction()) {
if (n.getFirstChild().getString().equals(name)) {
return n;
}
}
for (Node c : n.children()) {
Node result = findFunction(c, name);
if (result != null) {
return result;
}
}
return null;
}
private static Node parseFunction(String js) {
return parse(js).getFirstChild();
}
private static Node parse(String js) {
Compiler compiler = new Compiler();
CompilerOptions options = new CompilerOptions();
options.setLanguageIn(ECMASCRIPT_2017);
compiler.initOptions(options);
Node n = compiler.parseTestCode(js);
assertEquals(0, compiler.getErrorCount());
return n;
}
}