| 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.collect.ImmutableList; |
| 20 |
import com.google.common.collect.Lists; |
| 21 |
import com.google.javascript.jscomp.NodeTraversal.AbstractPostOrderCallback; |
| 22 |
import static com.google.javascript.jscomp.MarkNoSideEffectCalls.INVALID_NO_SIDE_EFFECT_ANNOTATION; |
| 23 |
import com.google.javascript.rhino.Node; |
| 24 |
import java.util.Collections; |
| 25 |
import java.util.List; |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
public class MarkNoSideEffectCallsTest extends CompilerTestCase { |
| 32 |
List<String> noSideEffectCalls = Lists.newArrayList(); |
| 33 |
|
| 34 |
private static String kExterns = |
| 35 |
"function externSef1(){}" + |
| 36 |
"/**@nosideeffects*/function externNsef1(){}" + |
| 37 |
"var externSef2 = function(){};" + |
| 38 |
"/**@nosideeffects*/var externNsef2 = function(){};" + |
| 39 |
"var externNsef3 = /**@nosideeffects*/function(){};" + |
| 40 |
"var externObj;" + |
| 41 |
"externObj.sef1 = function(){};" + |
| 42 |
"/**@nosideeffects*/externObj.nsef1 = function(){};" + |
| 43 |
"externObj.nsef2 = /**@nosideeffects*/function(){};" + |
| 44 |
"externObj.sef2;" + |
| 45 |
"/**@nosideeffects*/externObj.nsef3;"; |
| 46 |
|
| 47 |
public MarkNoSideEffectCallsTest() { |
| 48 |
super(kExterns); |
| 49 |
} |
| 50 |
|
| 51 |
@Override |
| 52 |
protected int getNumRepetitions() { |
| 53 |
|
| 54 |
return 1; |
| 55 |
} |
| 56 |
|
| 57 |
@Override |
| 58 |
protected void tearDown() throws Exception { |
| 59 |
super.tearDown(); |
| 60 |
noSideEffectCalls.clear(); |
| 61 |
} |
| 62 |
|
| 63 |
public void testFunctionAnnotation() throws Exception { |
| 64 |
testMarkCalls("/**@nosideeffects*/function f(){}", "f()", |
| 65 |
ImmutableList.of("f")); |
| 66 |
testMarkCalls("/**@nosideeffects*/var f = function(){};", "f()", |
| 67 |
ImmutableList.of("f")); |
| 68 |
testMarkCalls("var f = /**@nosideeffects*/function(){};", "f()", |
| 69 |
ImmutableList.of("f")); |
| 70 |
testMarkCalls("var f; f = /**@nosideeffects*/function(){};", "f()", |
| 71 |
ImmutableList.of("f")); |
| 72 |
testMarkCalls("var f; /**@nosideeffects*/ f = function(){};", "f()", |
| 73 |
ImmutableList.of("f")); |
| 74 |
|
| 75 |
|
| 76 |
testMarkCalls("function f(){}", Collections.<String>emptyList()); |
| 77 |
testMarkCalls("function f(){} f()", Collections.<String>emptyList()); |
| 78 |
|
| 79 |
|
| 80 |
testMarkCalls("/**@nosideeffects*/var f = " + |
| 81 |
"/**@nosideeffects*/function(){};", |
| 82 |
"f()", |
| 83 |
ImmutableList.of("f")); |
| 84 |
} |
| 85 |
|
| 86 |
public void testNamespaceAnnotation() throws Exception { |
| 87 |
testMarkCalls("var o = {}; o.f = /**@nosideeffects*/function(){};", |
| 88 |
"o.f()", ImmutableList.of("o.f")); |
| 89 |
testMarkCalls("var o = {}; o.f = /**@nosideeffects*/function(){};", |
| 90 |
"o.f()", ImmutableList.of("o.f")); |
| 91 |
testMarkCalls("var o = {}; o.f = function(){}; o.f()", |
| 92 |
Collections.<String>emptyList()); |
| 93 |
} |
| 94 |
|
| 95 |
public void testConstructorAnnotation() throws Exception { |
| 96 |
testMarkCalls("/**@nosideeffects*/function c(){};", "new c", |
| 97 |
ImmutableList.of("c")); |
| 98 |
testMarkCalls("var c = /**@nosideeffects*/function(){};", "new c", |
| 99 |
ImmutableList.of("c")); |
| 100 |
testMarkCalls("/**@nosideeffects*/var c = function(){};", "new c", |
| 101 |
ImmutableList.of("c")); |
| 102 |
testMarkCalls("function c(){}; new c", Collections.<String>emptyList()); |
| 103 |
} |
| 104 |
|
| 105 |
public void testMultipleDefinition() throws Exception { |
| 106 |
testMarkCalls("/**@nosideeffects*/function f(){}" + |
| 107 |
"/**@nosideeffects*/f = function(){};", |
| 108 |
"f()", |
| 109 |
ImmutableList.of("f")); |
| 110 |
testMarkCalls("function f(){}" + |
| 111 |
"/**@nosideeffects*/f = function(){};", |
| 112 |
"f()", |
| 113 |
Collections.<String>emptyList()); |
| 114 |
testMarkCalls("/**@nosideeffects*/function f(){}", |
| 115 |
"f = function(){};" + |
| 116 |
"f()", |
| 117 |
Collections.<String>emptyList()); |
| 118 |
} |
| 119 |
|
| 120 |
public void testAssignNoFunction() throws Exception { |
| 121 |
testMarkCalls("/**@nosideeffects*/function f(){}", "f = 1; f()", |
| 122 |
ImmutableList.of("f")); |
| 123 |
testMarkCalls("/**@nosideeffects*/function f(){}", "f = 1 || 2; f()", |
| 124 |
Collections.<String>emptyList()); |
| 125 |
} |
| 126 |
|
| 127 |
public void testPrototype() throws Exception { |
| 128 |
testMarkCalls("function c(){};" + |
| 129 |
"/**@nosideeffects*/c.prototype.g = function(){};", |
| 130 |
"var o = new c; o.g()", |
| 131 |
ImmutableList.of("o.g")); |
| 132 |
testMarkCalls("function c(){};" + |
| 133 |
"/**@nosideeffects*/c.prototype.g = function(){};", |
| 134 |
"function f(){}" + |
| 135 |
"var o = new c; o.g(); f()", |
| 136 |
ImmutableList.of("o.g")); |
| 137 |
|
| 138 |
|
| 139 |
testMarkCalls("function c(){};" + |
| 140 |
"/**@nosideeffects*/c.prototype.g = function(){};", |
| 141 |
"var o = new c;" + |
| 142 |
"o.g = function(){};" + |
| 143 |
"o.g()", |
| 144 |
ImmutableList.<String>of()); |
| 145 |
|
| 146 |
testMarkCalls("function c1(){};" + |
| 147 |
"/**@nosideeffects*/c1.prototype.f = function(){};" + |
| 148 |
"function c2(){};" + |
| 149 |
"/**@nosideeffects*/c2.prototype.f = function(){};", |
| 150 |
"var o = new c1;" + |
| 151 |
"o.f()", |
| 152 |
ImmutableList.of("o.f")); |
| 153 |
|
| 154 |
|
| 155 |
testMarkCalls("function c1(){};" + |
| 156 |
"/**@nosideeffects*/c1.prototype.f = function(){};", |
| 157 |
"function c2(){};" + |
| 158 |
"c2.prototype.f = function(){};" + |
| 159 |
"var o = new c1;" + |
| 160 |
"o.f()", |
| 161 |
Collections.<String>emptyList()); |
| 162 |
} |
| 163 |
|
| 164 |
public void testAnnotationInExterns() throws Exception { |
| 165 |
testMarkCalls("externSef1()", Collections.<String>emptyList()); |
| 166 |
testMarkCalls("externSef2()", Collections.<String>emptyList()); |
| 167 |
testMarkCalls("externNsef1()", ImmutableList.of("externNsef1")); |
| 168 |
testMarkCalls("externNsef2()", ImmutableList.of("externNsef2")); |
| 169 |
testMarkCalls("externNsef3()", ImmutableList.of("externNsef3")); |
| 170 |
} |
| 171 |
|
| 172 |
public void testNamespaceAnnotationInExterns() throws Exception { |
| 173 |
testMarkCalls("externObj.sef1()", Collections.<String>emptyList()); |
| 174 |
testMarkCalls("externObj.sef2()", Collections.<String>emptyList()); |
| 175 |
testMarkCalls("externObj.nsef1()", ImmutableList.of("externObj.nsef1")); |
| 176 |
testMarkCalls("externObj.nsef2()", ImmutableList.of("externObj.nsef2")); |
| 177 |
|
| 178 |
testMarkCalls("externObj.nsef3()", ImmutableList.of("externObj.nsef3")); |
| 179 |
} |
| 180 |
|
| 181 |
public void testOverrideDefinitionInSource() throws Exception { |
| 182 |
|
| 183 |
testMarkCalls("var obj = {}; obj.sef1 = function(){}; obj.sef1()", |
| 184 |
Collections.<String>emptyList()); |
| 185 |
|
| 186 |
|
| 187 |
testMarkCalls("var obj = {};" + |
| 188 |
"/**@nosideeffects*/obj.sef1 = function(){};", |
| 189 |
"obj.sef1()", |
| 190 |
Collections.<String>emptyList()); |
| 191 |
|
| 192 |
|
| 193 |
testMarkCalls("var obj = {}; obj.nsef1 = function(){}; obj.nsef1()", |
| 194 |
Collections.<String>emptyList()); |
| 195 |
|
| 196 |
|
| 197 |
testMarkCalls("var obj = {};" + |
| 198 |
"/**@nosideeffects*/obj.nsef1 = function(){};", |
| 199 |
"obj.nsef1()", |
| 200 |
ImmutableList.of("obj.nsef1")); |
| 201 |
} |
| 202 |
|
| 203 |
public void testApply1() throws Exception { |
| 204 |
testMarkCalls("/**@nosideeffects*/ var f = function() {}", |
| 205 |
"f.apply()", |
| 206 |
ImmutableList.of("f.apply")); |
| 207 |
} |
| 208 |
|
| 209 |
public void testApply2() throws Exception { |
| 210 |
testMarkCalls("var f = function() {}", |
| 211 |
"f.apply()", |
| 212 |
ImmutableList.<String>of()); |
| 213 |
} |
| 214 |
|
| 215 |
public void testCall1() throws Exception { |
| 216 |
testMarkCalls("/**@nosideeffects*/ var f = function() {}", |
| 217 |
"f.call()", |
| 218 |
ImmutableList.of("f.call")); |
| 219 |
} |
| 220 |
|
| 221 |
public void testCall2() throws Exception { |
| 222 |
testMarkCalls("var f = function() {}", |
| 223 |
"f.call()", |
| 224 |
ImmutableList.<String>of()); |
| 225 |
} |
| 226 |
|
| 227 |
public void testInvalidAnnotation1() throws Exception { |
| 228 |
test("/** @nosideeffects */ function foo() {}", |
| 229 |
null, INVALID_NO_SIDE_EFFECT_ANNOTATION); |
| 230 |
} |
| 231 |
|
| 232 |
public void testInvalidAnnotation2() throws Exception { |
| 233 |
test("var f = /** @nosideeffects */ function() {}", |
| 234 |
null, INVALID_NO_SIDE_EFFECT_ANNOTATION); |
| 235 |
} |
| 236 |
|
| 237 |
public void testInvalidAnnotation3() throws Exception { |
| 238 |
test("/** @nosideeffects */ var f = function() {}", |
| 239 |
null, INVALID_NO_SIDE_EFFECT_ANNOTATION); |
| 240 |
} |
| 241 |
|
| 242 |
public void testInvalidAnnotation4() throws Exception { |
| 243 |
test("var f = function() {};" + |
| 244 |
"/** @nosideeffects */ f.x = function() {}", |
| 245 |
null, INVALID_NO_SIDE_EFFECT_ANNOTATION); |
| 246 |
} |
| 247 |
|
| 248 |
public void testInvalidAnnotation5() throws Exception { |
| 249 |
test("var f = function() {};" + |
| 250 |
"f.x = /** @nosideeffects */ function() {}", |
| 251 |
null, INVALID_NO_SIDE_EFFECT_ANNOTATION); |
| 252 |
} |
| 253 |
|
| 254 |
void testMarkCalls(String source, List<String> expected) { |
| 255 |
testMarkCalls("", source, expected); |
| 256 |
} |
| 257 |
|
| 258 |
void testMarkCalls( |
| 259 |
String extraExterns, String source, List<String> expected) { |
| 260 |
testSame(kExterns + extraExterns, source, null); |
| 261 |
assertEquals(expected, noSideEffectCalls); |
| 262 |
noSideEffectCalls.clear(); |
| 263 |
} |
| 264 |
|
| 265 |
@Override |
| 266 |
protected CompilerPass getProcessor(Compiler compiler) { |
| 267 |
return new NoSideEffectCallEnumerator(compiler); |
| 268 |
} |
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
private class NoSideEffectCallEnumerator |
| 275 |
extends AbstractPostOrderCallback implements CompilerPass { |
| 276 |
private final MarkNoSideEffectCalls passUnderTest; |
| 277 |
private final Compiler compiler; |
| 278 |
|
| 279 |
NoSideEffectCallEnumerator(Compiler compiler) { |
| 280 |
this.passUnderTest = new MarkNoSideEffectCalls(compiler); |
| 281 |
this.compiler = compiler; |
| 282 |
} |
| 283 |
|
| 284 |
@Override |
| 285 |
public void process(Node externs, Node root) { |
| 286 |
passUnderTest.process(externs, root); |
| 287 |
NodeTraversal.traverse(compiler, externs, this); |
| 288 |
NodeTraversal.traverse(compiler, root, this); |
| 289 |
} |
| 290 |
|
| 291 |
@Override |
| 292 |
public void visit(NodeTraversal t, Node n, Node parent) { |
| 293 |
if (n.isNew()) { |
| 294 |
if (!NodeUtil.constructorCallHasSideEffects(n)) { |
| 295 |
noSideEffectCalls.add(n.getFirstChild().getQualifiedName()); |
| 296 |
} |
| 297 |
} else if (n.isCall()) { |
| 298 |
if (!NodeUtil.functionCallHasSideEffects(n)) { |
| 299 |
noSideEffectCalls.add(n.getFirstChild().getQualifiedName()); |
| 300 |
} |
| 301 |
} |
| 302 |
} |
| 303 |
} |
| 304 |
} |