-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
CheckSuperTest.java
178 lines (152 loc) · 7.43 KB
/
CheckSuperTest.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
/*
* Copyright 2016 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.javascript.jscomp.CheckSuper.INVALID_SUPER_ACCESS;
import static com.google.javascript.jscomp.CheckSuper.INVALID_SUPER_CALL;
import static com.google.javascript.jscomp.CheckSuper.INVALID_SUPER_CALL_WITH_SUGGESTION;
import static com.google.javascript.jscomp.CheckSuper.INVALID_SUPER_USAGE;
import static com.google.javascript.jscomp.CheckSuper.MISSING_CALL_TO_SUPER;
import static com.google.javascript.jscomp.CheckSuper.THIS_BEFORE_SUPER;
public final class CheckSuperTest extends CompilerTestCase {
@Override
protected CompilerPass getProcessor(Compiler compiler) {
return new CheckSuper(compiler);
}
@Override
protected void setUp() throws Exception {
super.setUp();
}
public void testInvalidUsage() {
// 'super' may only appear directly within a CALL, a GETPROP, or a GETELEM.
testError("class Foo extends Bar { constructor() { super(); super; } }", INVALID_SUPER_USAGE);
testError("class Foo extends Bar { foo() { super; } }", INVALID_SUPER_USAGE);
testError("function f() { super; }", INVALID_SUPER_USAGE);
testError("super;", INVALID_SUPER_USAGE);
}
public void testMissingSuper() {
testError("class C extends D { constructor() {} }", MISSING_CALL_TO_SUPER);
testError("class C extends D { constructor() { super.foo(); } }", MISSING_CALL_TO_SUPER);
}
public void testMissingSuper_nestedClass() {
// Note that super is only called for anonymous class "E", not C.
testError(lines(
"class C extends D {",
" constructor() { ",
" const E = class extends D { constructor() { super(); } };",
" }",
"}"),
MISSING_CALL_TO_SUPER);
}
public void testNoWarning() {
testSame("class C extends D { constructor() { super(); } }");
testSame("class C { constructor() {} }");
testSame("class C extends D {}");
}
public void testThisBeforeSuper() {
testError("class C extends D { constructor() { this.foo(); super(); } }", THIS_BEFORE_SUPER);
}
// We could require that the super() call is the first statement in the constructor, except that
// doing so breaks J2CL-compiled code, which needs to do the static initialization for the class
// before anything else.
public void testNoWarning_J2CL() {
testSame("class C extends D { constructor() { C.init(); super(); } }");
}
// Referencing this within a function before calling super is acceptable at runtime so long as
// it's never executed before the super() has returned. It's also acceptable since this might
// be bound to something other than the class being constructed.
public void testNoWarning_thisWithinFunction() {
testSame("class C extends D { constructor() { const c = () => this; super(); } }");
testSame("class C extends D { constructor() { const c = function() { this; }; super(); } }");
}
public void testOutsideClass() {
testError("var i = super();", INVALID_SUPER_CALL);
testError("var i = super.i;", INVALID_SUPER_ACCESS);
testError("var i = super[x];", INVALID_SUPER_ACCESS);
}
public void testInOrdinaryFunction() {
testError("function f() { super(); }", INVALID_SUPER_CALL);
testError("function f() { var i = super.i; }", INVALID_SUPER_ACCESS);
testError("function f() { var i = super[x]; }", INVALID_SUPER_ACCESS);
}
public void testCallWithNoBaseClass() {
testError("class C { constructor() { super(); }}", INVALID_SUPER_CALL);
testError("class C { constructor() { super(1); }}", INVALID_SUPER_CALL);
}
public void testCallInConstructor() {
testSame("class C extends D { constructor() { super(); }}");
testSame("class C extends D { constructor() { super(1); }}");
}
public void testNestedCallInConstructor() {
testError(
"class C extends D { constructor() { (()=>{ super(); })(); }}", MISSING_CALL_TO_SUPER);
}
public void testCallInMethod() {
test(
srcs("class C extends D { foo() { super(); }}"),
error(INVALID_SUPER_CALL_WITH_SUGGESTION)
.withMessage("super() not allowed here. Did you mean super.foo?"));
test(
srcs("class C extends D { foo() { super(1); }}"),
error(INVALID_SUPER_CALL_WITH_SUGGESTION)
.withMessage("super() not allowed here. Did you mean super.foo?"));
testError("class C { static foo() { super(); }}", INVALID_SUPER_CALL);
testError("class C extends D { foo() { (()=>{ super(); })(); }}", INVALID_SUPER_CALL);
}
public void testPropertyInMethod() {
testSame("class C extends D { foo() { super.foo(); }}");
testSame("class C extends D { foo() { super.foo(1); }}");
// TODO(tbreisacher): Consider warning for this. It's valid but likely indicates a mistake.
testSame("class C extends D { foo() { var x = super.bar; }}");
testSame("class C extends D { foo() { var x = super[y]; }}");
testSame("class C extends D { foo() { super.bar(); }}");
}
public void testNoWarning_withExplicitReturnInConstructor() {
testNoWarning("class C extends D { constructor() { return {}; } }");
testNoWarning("class C extends D { constructor() { return f(); } }");
// the following cases will error at runtime but we don't bother checking them.
testNoWarning("class C extends D { constructor() { return 3; } }");
testNoWarning("class C extends D { constructor() { if (false) return {}; } }");
}
public void testWarning_withInvalidReturnInConstructor() {
// empty return
testError("class C extends D { constructor() { return; } }", MISSING_CALL_TO_SUPER);
// return in arrow function
testError(
"class C extends D { constructor() { () => { return {}; } } }", MISSING_CALL_TO_SUPER);
}
public void testInvalidThisReference_withExplicitReturnInConstructor() {
testError("class C extends D { constructor() { this.x = 3; return {}; } }", THIS_BEFORE_SUPER);
}
public void testPropertyInConstructor() {
// TODO(sdh): See note in testPropertyInMethod - these are valid but questionable.
testSame("class C extends D { constructor() { super(); super.foo(); }}");
testSame("class C extends D { constructor() { super(); super.foo(1); }}");
testSame("class C extends D { constructor() { super(); var x = super.bar; }}");
}
public void testNestedProperty() {
testSame("class C extends D { constructor() { super(); (()=>{ super.foo(); })(); }}");
testSame("class C extends D { foo() { (()=>{ super.foo(); })(); }}");
}
public void testPropertyNoBaseClass() {
// TODO(sdh): See note in testPropertyInMethod - these are valid but questionable.
testSame("class C { constructor() { super.foo(); }}");
testSame("class C { foo() { super.foo(1); }}");
testSame("class C { foo() { super.bar(); }}");
testSame("class C { foo() { super.baz; }}");
testSame("class C { foo() { super[x](); }}");
}
// TODO(sdh): test super.prop access from static methods - this will need runtime tests, too!
}