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
|
/*
* 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 com.google.common.collect.ImmutableList;
import com.google.common.collect.Lists;
import com.google.javascript.jscomp.NodeTraversal.AbstractPostOrderCallback;
import static com.google.javascript.jscomp.MarkNoSideEffectCalls.INVALID_NO_SIDE_EFFECT_ANNOTATION;
import com.google.javascript.rhino.Node;
import java.util.Collections;
import java.util.List;
/**
* Tests for {@link MarkNoSideEffectCalls}
*
*/
public class MarkNoSideEffectCallsTest extends CompilerTestCase {
List<String> noSideEffectCalls = Lists.newArrayList();
private static String kExterns =
"function externSef1(){}" +
"/**@nosideeffects*/function externNsef1(){}" +
"var externSef2 = function(){};" +
"/**@nosideeffects*/var externNsef2 = function(){};" +
"var externNsef3 = /**@nosideeffects*/function(){};" +
"var externObj;" +
"externObj.sef1 = function(){};" +
"/**@nosideeffects*/externObj.nsef1 = function(){};" +
"externObj.nsef2 = /**@nosideeffects*/function(){};" +
"externObj.sef2;" +
"/**@nosideeffects*/externObj.nsef3;";
public MarkNoSideEffectCallsTest() {
super(kExterns);
}
@Override
protected int getNumRepetitions() {
// run pass once.
return 1;
}
@Override
protected void tearDown() throws Exception {
super.tearDown();
noSideEffectCalls.clear();
}
public void testFunctionAnnotation() throws Exception {
testMarkCalls("/**@nosideeffects*/function f(){}", "f()",
ImmutableList.of("f"));
testMarkCalls("/**@nosideeffects*/var f = function(){};", "f()",
ImmutableList.of("f"));
testMarkCalls("var f = /**@nosideeffects*/function(){};", "f()",
ImmutableList.of("f"));
testMarkCalls("var f; f = /**@nosideeffects*/function(){};", "f()",
ImmutableList.of("f"));
testMarkCalls("var f; /**@nosideeffects*/ f = function(){};", "f()",
ImmutableList.of("f"));
// no annotation
testMarkCalls("function f(){}", Collections.<String>emptyList());
testMarkCalls("function f(){} f()", Collections.<String>emptyList());
// 2 annotations
testMarkCalls("/**@nosideeffects*/var f = " +
"/**@nosideeffects*/function(){};",
"f()",
ImmutableList.of("f"));
}
public void testNamespaceAnnotation() throws Exception {
testMarkCalls("var o = {}; o.f = /**@nosideeffects*/function(){};",
"o.f()", ImmutableList.of("o.f"));
testMarkCalls("var o = {}; o.f = /**@nosideeffects*/function(){};",
"o.f()", ImmutableList.of("o.f"));
testMarkCalls("var o = {}; o.f = function(){}; o.f()",
Collections.<String>emptyList());
}
public void testConstructorAnnotation() throws Exception {
testMarkCalls("/**@nosideeffects*/function c(){};", "new c",
ImmutableList.of("c"));
testMarkCalls("var c = /**@nosideeffects*/function(){};", "new c",
ImmutableList.of("c"));
testMarkCalls("/**@nosideeffects*/var c = function(){};", "new c",
ImmutableList.of("c"));
testMarkCalls("function c(){}; new c", Collections.<String>emptyList());
}
public void testMultipleDefinition() throws Exception {
testMarkCalls("/**@nosideeffects*/function f(){}" +
"/**@nosideeffects*/f = function(){};",
"f()",
ImmutableList.of("f"));
testMarkCalls("function f(){}" +
"/**@nosideeffects*/f = function(){};",
"f()",
Collections.<String>emptyList());
testMarkCalls("/**@nosideeffects*/function f(){}",
"f = function(){};" +
"f()",
Collections.<String>emptyList());
}
public void testAssignNoFunction() throws Exception {
testMarkCalls("/**@nosideeffects*/function f(){}", "f = 1; f()",
ImmutableList.of("f"));
testMarkCalls("/**@nosideeffects*/function f(){}", "f = 1 || 2; f()",
Collections.<String>emptyList());
}
public void testPrototype() throws Exception {
testMarkCalls("function c(){};" +
"/**@nosideeffects*/c.prototype.g = function(){};",
"var o = new c; o.g()",
ImmutableList.of("o.g"));
testMarkCalls("function c(){};" +
"/**@nosideeffects*/c.prototype.g = function(){};",
"function f(){}" +
"var o = new c; o.g(); f()",
ImmutableList.of("o.g"));
// replace o.f with a function that has side effects
testMarkCalls("function c(){};" +
"/**@nosideeffects*/c.prototype.g = function(){};",
"var o = new c;" +
"o.g = function(){};" +
"o.g()",
ImmutableList.<String>of());
// two classes with same property; neither has side effects
testMarkCalls("function c1(){};" +
"/**@nosideeffects*/c1.prototype.f = function(){};" +
"function c2(){};" +
"/**@nosideeffects*/c2.prototype.f = function(){};",
"var o = new c1;" +
"o.f()",
ImmutableList.of("o.f"));
// two classes with same property; one has side effects
testMarkCalls("function c1(){};" +
"/**@nosideeffects*/c1.prototype.f = function(){};",
"function c2(){};" +
"c2.prototype.f = function(){};" +
"var o = new c1;" +
"o.f()",
Collections.<String>emptyList());
}
public void testAnnotationInExterns() throws Exception {
testMarkCalls("externSef1()", Collections.<String>emptyList());
testMarkCalls("externSef2()", Collections.<String>emptyList());
testMarkCalls("externNsef1()", ImmutableList.of("externNsef1"));
testMarkCalls("externNsef2()", ImmutableList.of("externNsef2"));
testMarkCalls("externNsef3()", ImmutableList.of("externNsef3"));
}
public void testNamespaceAnnotationInExterns() throws Exception {
testMarkCalls("externObj.sef1()", Collections.<String>emptyList());
testMarkCalls("externObj.sef2()", Collections.<String>emptyList());
testMarkCalls("externObj.nsef1()", ImmutableList.of("externObj.nsef1"));
testMarkCalls("externObj.nsef2()", ImmutableList.of("externObj.nsef2"));
testMarkCalls("externObj.nsef3()", ImmutableList.of("externObj.nsef3"));
}
public void testOverrideDefinitionInSource() throws Exception {
// both have side effects.
testMarkCalls("var obj = {}; obj.sef1 = function(){}; obj.sef1()",
Collections.<String>emptyList());
// extern has side effects.
testMarkCalls("var obj = {};" +
"/**@nosideeffects*/obj.sef1 = function(){};",
"obj.sef1()",
Collections.<String>emptyList());
// override in source also has side effects.
testMarkCalls("var obj = {}; obj.nsef1 = function(){}; obj.nsef1()",
Collections.<String>emptyList());
// override in source also has no side effects.
testMarkCalls("var obj = {};" +
"/**@nosideeffects*/obj.nsef1 = function(){};",
"obj.nsef1()",
ImmutableList.of("obj.nsef1"));
}
public void testApply1() throws Exception {
testMarkCalls("/**@nosideeffects*/ var f = function() {}",
"f.apply()",
ImmutableList.of("f.apply"));
}
public void testApply2() throws Exception {
testMarkCalls("var f = function() {}",
"f.apply()",
ImmutableList.<String>of());
}
public void testCall1() throws Exception {
testMarkCalls("/**@nosideeffects*/ var f = function() {}",
"f.call()",
ImmutableList.of("f.call"));
}
public void testCall2() throws Exception {
testMarkCalls("var f = function() {}",
"f.call()",
ImmutableList.<String>of());
}
public void testInvalidAnnotation1() throws Exception {
test("/** @nosideeffects */ function foo() {}",
null, INVALID_NO_SIDE_EFFECT_ANNOTATION);
}
public void testInvalidAnnotation2() throws Exception {
test("var f = /** @nosideeffects */ function() {}",
null, INVALID_NO_SIDE_EFFECT_ANNOTATION);
}
public void testInvalidAnnotation3() throws Exception {
test("/** @nosideeffects */ var f = function() {}",
null, INVALID_NO_SIDE_EFFECT_ANNOTATION);
}
public void testInvalidAnnotation4() throws Exception {
test("var f = function() {};" +
"/** @nosideeffects */ f.x = function() {}",
null, INVALID_NO_SIDE_EFFECT_ANNOTATION);
}
public void testInvalidAnnotation5() throws Exception {
test("var f = function() {};" +
"f.x = /** @nosideeffects */ function() {}",
null, INVALID_NO_SIDE_EFFECT_ANNOTATION);
}
void testMarkCalls(String source, List<String> expected) {
testMarkCalls("", source, expected);
}
void testMarkCalls(
String extraExterns, String source, List<String> expected) {
testSame(kExterns + extraExterns, source, null);
assertEquals(expected, noSideEffectCalls);
noSideEffectCalls.clear();
}
@Override
protected CompilerPass getProcessor(Compiler compiler) {
return new NoSideEffectCallEnumerator(compiler);
}
/**
* Run MarkNoSideEffectCalls, then gather a list of calls that are
* marked as having no side effects.
*/
private class NoSideEffectCallEnumerator
extends AbstractPostOrderCallback implements CompilerPass {
private final MarkNoSideEffectCalls passUnderTest;
private final Compiler compiler;
NoSideEffectCallEnumerator(Compiler compiler) {
this.passUnderTest = new MarkNoSideEffectCalls(compiler);
this.compiler = compiler;
}
@Override
public void process(Node externs, Node root) {
passUnderTest.process(externs, root);
NodeTraversal.traverse(compiler, externs, this);
NodeTraversal.traverse(compiler, root, this);
}
@Override
public void visit(NodeTraversal t, Node n, Node parent) {
if (n.isNew()) {
if (!NodeUtil.constructorCallHasSideEffects(n)) {
noSideEffectCalls.add(n.getFirstChild().getQualifiedName());
}
} else if (n.isCall()) {
if (!NodeUtil.functionCallHasSideEffects(n)) {
noSideEffectCalls.add(n.getFirstChild().getQualifiedName());
}
}
}
}
}
|