| 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.ImmutableSet; |
| 20 |
import com.google.common.collect.Multiset; |
| 21 |
import com.google.common.collect.Sets; |
| 22 |
import com.google.common.collect.TreeMultiset; |
| 23 |
import com.google.javascript.jscomp.DefinitionsRemover.Definition; |
| 24 |
import com.google.javascript.jscomp.NodeTraversal.AbstractPostOrderCallback; |
| 25 |
import com.google.javascript.rhino.Node; |
| 26 |
import com.google.javascript.rhino.Token; |
| 27 |
|
| 28 |
import java.util.Collection; |
| 29 |
import java.util.Set; |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
public class SimpleDefinitionFinderTest extends CompilerTestCase { |
| 36 |
Set<String> found = Sets.newTreeSet(); |
| 37 |
|
| 38 |
@Override |
| 39 |
protected int getNumRepetitions() { |
| 40 |
|
| 41 |
return 1; |
| 42 |
} |
| 43 |
|
| 44 |
@Override |
| 45 |
protected void tearDown() throws Exception { |
| 46 |
super.tearDown(); |
| 47 |
found.clear(); |
| 48 |
} |
| 49 |
|
| 50 |
public void testDefineNumber() throws Exception { |
| 51 |
checkDefinitionsInJs( |
| 52 |
"var a = 1", |
| 53 |
ImmutableSet.of("DEF NAME a -> NUMBER")); |
| 54 |
|
| 55 |
checkDefinitionsInJs( |
| 56 |
"a = 1", |
| 57 |
ImmutableSet.of("DEF NAME a -> NUMBER")); |
| 58 |
|
| 59 |
checkDefinitionsInJs( |
| 60 |
"a.b = 1", |
| 61 |
ImmutableSet.of("DEF GETPROP a.b -> NUMBER")); |
| 62 |
|
| 63 |
|
| 64 |
checkDefinitionsInJs( |
| 65 |
"a[\"b\"] = 1", |
| 66 |
ImmutableSet.<String>of()); |
| 67 |
|
| 68 |
checkDefinitionsInJs( |
| 69 |
"f().b = 1", |
| 70 |
ImmutableSet.of("DEF GETPROP null -> NUMBER")); |
| 71 |
|
| 72 |
checkDefinitionsInJs( |
| 73 |
"({a : 1}); o.a", |
| 74 |
ImmutableSet.of("DEF STRING_KEY null -> NUMBER", |
| 75 |
"USE GETPROP o.a -> [NUMBER]")); |
| 76 |
|
| 77 |
|
| 78 |
checkDefinitionsInJs( |
| 79 |
"({'a' : 1}); o['a']", |
| 80 |
ImmutableSet.<String>of("DEF STRING_KEY null -> NUMBER")); |
| 81 |
|
| 82 |
checkDefinitionsInJs( |
| 83 |
"({1 : 1}); o[1]", |
| 84 |
ImmutableSet.<String>of("DEF STRING_KEY null -> NUMBER")); |
| 85 |
|
| 86 |
checkDefinitionsInJs( |
| 87 |
"var a = {b : 1}; a.b", |
| 88 |
ImmutableSet.of("DEF NAME a -> <null>", |
| 89 |
"DEF STRING_KEY null -> NUMBER", |
| 90 |
"USE NAME a -> [<null>]", |
| 91 |
"USE GETPROP a.b -> [NUMBER]")); |
| 92 |
} |
| 93 |
|
| 94 |
public void testDefineGet() throws Exception { |
| 95 |
|
| 96 |
checkDefinitionsInJs( |
| 97 |
"({get a() {}}); o.a", |
| 98 |
ImmutableSet.of("DEF GETTER_DEF null -> FUNCTION", |
| 99 |
"USE GETPROP o.a -> [FUNCTION]")); |
| 100 |
} |
| 101 |
|
| 102 |
public void testDefineSet() throws Exception { |
| 103 |
|
| 104 |
checkDefinitionsInJs( |
| 105 |
"({set a(b) {}}); o.a", |
| 106 |
ImmutableSet.of("DEF NAME b -> <null>", |
| 107 |
"DEF SETTER_DEF null -> FUNCTION", |
| 108 |
"USE GETPROP o.a -> [FUNCTION]")); |
| 109 |
} |
| 110 |
|
| 111 |
public void testDefineFunction() throws Exception { |
| 112 |
checkDefinitionsInJs( |
| 113 |
"var a = function(){}", |
| 114 |
ImmutableSet.of("DEF NAME a -> FUNCTION")); |
| 115 |
|
| 116 |
checkDefinitionsInJs( |
| 117 |
"var a = function f(){}", |
| 118 |
ImmutableSet.of("DEF NAME f -> FUNCTION", "DEF NAME a -> FUNCTION")); |
| 119 |
|
| 120 |
checkDefinitionsInJs( |
| 121 |
"function a(){}", |
| 122 |
ImmutableSet.of("DEF NAME a -> FUNCTION")); |
| 123 |
|
| 124 |
checkDefinitionsInJs( |
| 125 |
"a = function(){}", |
| 126 |
ImmutableSet.of("DEF NAME a -> FUNCTION")); |
| 127 |
|
| 128 |
checkDefinitionsInJs( |
| 129 |
"a.b = function(){}", |
| 130 |
ImmutableSet.of("DEF GETPROP a.b -> FUNCTION")); |
| 131 |
|
| 132 |
|
| 133 |
checkDefinitionsInJs( |
| 134 |
"a[\"b\"] = function(){}", |
| 135 |
ImmutableSet.<String>of()); |
| 136 |
|
| 137 |
checkDefinitionsInJs( |
| 138 |
"f().b = function(){}", |
| 139 |
ImmutableSet.of("DEF GETPROP null -> FUNCTION")); |
| 140 |
} |
| 141 |
|
| 142 |
public void testFunctionArgumentsBasic() throws Exception { |
| 143 |
checkDefinitionsInJs( |
| 144 |
"function f(a){return a}", |
| 145 |
ImmutableSet.of("DEF NAME a -> <null>", |
| 146 |
"USE NAME a -> [<null>]", |
| 147 |
"DEF NAME f -> FUNCTION")); |
| 148 |
|
| 149 |
checkDefinitionsInJs( |
| 150 |
"var a = 1; function f(a){return a}", |
| 151 |
ImmutableSet.of("DEF NAME a -> NUMBER", |
| 152 |
"DEF NAME a -> <null>", |
| 153 |
"USE NAME a -> [<null>, NUMBER]", |
| 154 |
"DEF NAME f -> FUNCTION")); |
| 155 |
} |
| 156 |
|
| 157 |
public void testFunctionArgumentsInExterns() throws Exception { |
| 158 |
final String DEF = "var f = function(arg1, arg2){}"; |
| 159 |
final String USE = "f(1, 2)"; |
| 160 |
|
| 161 |
|
| 162 |
checkDefinitionsInJs( |
| 163 |
DEF + ";" + USE, |
| 164 |
ImmutableSet.of("DEF NAME f -> FUNCTION", |
| 165 |
"DEF NAME arg1 -> <null>", |
| 166 |
"DEF NAME arg2 -> <null>", |
| 167 |
"USE NAME f -> [FUNCTION]")); |
| 168 |
|
| 169 |
|
| 170 |
checkDefinitions( |
| 171 |
DEF, USE, |
| 172 |
ImmutableSet.of("DEF NAME f -> EXTERN FUNCTION", |
| 173 |
"USE NAME f -> [EXTERN FUNCTION]")); |
| 174 |
} |
| 175 |
|
| 176 |
public void testMultipleDefinition() throws Exception { |
| 177 |
checkDefinitionsInJs( |
| 178 |
"a = 1; a = 2; a", |
| 179 |
ImmutableSet.of("DEF NAME a -> NUMBER", |
| 180 |
"USE NAME a -> [NUMBER x 2]")); |
| 181 |
|
| 182 |
checkDefinitionsInJs( |
| 183 |
"a = 1; a = 'a'; a", |
| 184 |
ImmutableSet.of("DEF NAME a -> NUMBER", |
| 185 |
"DEF NAME a -> STRING", |
| 186 |
"USE NAME a -> [NUMBER, STRING]")); |
| 187 |
|
| 188 |
checkDefinitionsInJs( |
| 189 |
"a = 1; b = 2; a = b; a", |
| 190 |
ImmutableSet.of("DEF NAME a -> <null>", |
| 191 |
"DEF NAME a -> NUMBER", |
| 192 |
"DEF NAME b -> NUMBER", |
| 193 |
"USE NAME a -> [<null>, NUMBER]", |
| 194 |
"USE NAME b -> [NUMBER]")); |
| 195 |
|
| 196 |
checkDefinitionsInJs( |
| 197 |
"a = 1; b = 2; c = b; c = a; c", |
| 198 |
ImmutableSet.of("DEF NAME a -> NUMBER", |
| 199 |
"DEF NAME b -> NUMBER", |
| 200 |
"DEF NAME c -> <null>", |
| 201 |
"USE NAME a -> [NUMBER]", |
| 202 |
"USE NAME b -> [NUMBER]", |
| 203 |
"USE NAME c -> [<null> x 2]")); |
| 204 |
|
| 205 |
checkDefinitionsInJs( |
| 206 |
"function f(){} f()", |
| 207 |
ImmutableSet.of("DEF NAME f -> FUNCTION", |
| 208 |
"USE NAME f -> [FUNCTION]")); |
| 209 |
|
| 210 |
checkDefinitionsInJs( |
| 211 |
"function f(){} f.call(null)", |
| 212 |
ImmutableSet.of("DEF NAME f -> FUNCTION", |
| 213 |
"USE NAME f -> [FUNCTION]", |
| 214 |
"USE GETPROP f.call -> [FUNCTION]")); |
| 215 |
|
| 216 |
checkDefinitionsInJs( |
| 217 |
"function f(){} f.apply(null, [])", |
| 218 |
ImmutableSet.of("DEF NAME f -> FUNCTION", |
| 219 |
"USE NAME f -> [FUNCTION]", |
| 220 |
"USE GETPROP f.apply -> [FUNCTION]")); |
| 221 |
|
| 222 |
checkDefinitionsInJs( |
| 223 |
"function f(){} f.foobar()", |
| 224 |
ImmutableSet.of("DEF NAME f -> FUNCTION", |
| 225 |
"USE NAME f -> [FUNCTION]")); |
| 226 |
|
| 227 |
checkDefinitionsInJs( |
| 228 |
"function f(){} f(); f.call(null)", |
| 229 |
ImmutableSet.of("DEF NAME f -> FUNCTION", |
| 230 |
"USE NAME f -> [FUNCTION]", |
| 231 |
"USE GETPROP f.call -> [FUNCTION]")); |
| 232 |
|
| 233 |
} |
| 234 |
|
| 235 |
public void testDefinitionInExterns() throws Exception { |
| 236 |
String externs = "var a = 1"; |
| 237 |
|
| 238 |
checkDefinitionsInExterns( |
| 239 |
externs, |
| 240 |
ImmutableSet.of("DEF NAME a -> EXTERN NUMBER")); |
| 241 |
|
| 242 |
checkDefinitions( |
| 243 |
externs, |
| 244 |
"var b = 1", |
| 245 |
ImmutableSet.of("DEF NAME a -> EXTERN NUMBER", "DEF NAME b -> NUMBER")); |
| 246 |
|
| 247 |
checkDefinitions( |
| 248 |
externs, |
| 249 |
"a = \"foo\"; a", |
| 250 |
ImmutableSet.of("DEF NAME a -> EXTERN NUMBER", |
| 251 |
"DEF NAME a -> STRING", |
| 252 |
"USE NAME a -> [EXTERN NUMBER, STRING]")); |
| 253 |
|
| 254 |
checkDefinitionsInExterns( |
| 255 |
"var a = {}; a.b = 10", |
| 256 |
ImmutableSet.of("DEF GETPROP a.b -> EXTERN NUMBER", |
| 257 |
"DEF NAME a -> EXTERN <null>", |
| 258 |
"USE NAME a -> [EXTERN <null>]")); |
| 259 |
|
| 260 |
checkDefinitionsInExterns( |
| 261 |
"var a = {}; a.b", |
| 262 |
ImmutableSet.of("DEF GETPROP a.b -> EXTERN <null>", |
| 263 |
"DEF NAME a -> EXTERN <null>", |
| 264 |
"USE NAME a -> [EXTERN <null>]")); |
| 265 |
|
| 266 |
checkDefinitions( |
| 267 |
"var a = {}", |
| 268 |
"a.b = 1", |
| 269 |
ImmutableSet.of("DEF GETPROP a.b -> NUMBER", |
| 270 |
"DEF NAME a -> EXTERN <null>", |
| 271 |
"USE NAME a -> [EXTERN <null>]")); |
| 272 |
|
| 273 |
checkDefinitions( |
| 274 |
"var a = {}", |
| 275 |
"a.b", |
| 276 |
ImmutableSet.of("DEF NAME a -> EXTERN <null>", |
| 277 |
"USE NAME a -> [EXTERN <null>]")); |
| 278 |
|
| 279 |
checkDefinitionsInExterns( |
| 280 |
externs, |
| 281 |
ImmutableSet.of("DEF NAME a -> EXTERN NUMBER")); |
| 282 |
} |
| 283 |
|
| 284 |
public void testObjectLitInExterns() { |
| 285 |
checkDefinitions( |
| 286 |
"var goog = {};" + |
| 287 |
"/** @type {number} */ goog.HYBRID;" + |
| 288 |
"/** @enum */ goog.Enum = {HYBRID: 0, ROADMAP: 1};", |
| 289 |
"goog.HYBRID; goog.Enum.ROADMAP;", |
| 290 |
ImmutableSet.of( |
| 291 |
"DEF GETPROP goog.Enum -> EXTERN <null>", |
| 292 |
"DEF GETPROP goog.HYBRID -> EXTERN <null>", |
| 293 |
"DEF NAME goog -> EXTERN <null>", |
| 294 |
"DEF STRING_KEY null -> EXTERN NUMBER", |
| 295 |
"USE GETPROP goog.Enum -> [EXTERN <null>]", |
| 296 |
"USE GETPROP goog.Enum.ROADMAP -> [EXTERN NUMBER]", |
| 297 |
"USE GETPROP goog.HYBRID -> [EXTERN <null>, EXTERN NUMBER]", |
| 298 |
"USE NAME goog -> [EXTERN <null>]")); |
| 299 |
} |
| 300 |
|
| 301 |
public void testCallInExterns() { |
| 302 |
checkDefinitionsInExterns( |
| 303 |
"var goog = {};" + |
| 304 |
"/** @constructor */ goog.Response = function() {};" + |
| 305 |
"goog.Response.prototype.get;" + |
| 306 |
"goog.Response.prototype.get().get;", |
| 307 |
ImmutableSet.of( |
| 308 |
"DEF GETPROP goog.Response -> EXTERN FUNCTION", |
| 309 |
"DEF GETPROP goog.Response.prototype.get -> EXTERN <null>", |
| 310 |
"DEF GETPROP null -> EXTERN <null>", |
| 311 |
"DEF NAME goog -> EXTERN <null>", |
| 312 |
"USE GETPROP goog.Response -> [EXTERN FUNCTION]", |
| 313 |
"USE GETPROP goog.Response.prototype.get -> [EXTERN <null> x 2]", |
| 314 |
"USE NAME goog -> [EXTERN <null>]")); |
| 315 |
} |
| 316 |
|
| 317 |
void checkDefinitionsInExterns(String externs, Set<String> expected) { |
| 318 |
checkDefinitions(externs, "", expected); |
| 319 |
} |
| 320 |
|
| 321 |
void checkDefinitionsInJs(String js, Set<String> expected) { |
| 322 |
checkDefinitions("", js, expected); |
| 323 |
} |
| 324 |
|
| 325 |
void checkDefinitions(String externs, String source, Set<String> expected) { |
| 326 |
testSame(externs, source, null); |
| 327 |
assertEquals(expected, found); |
| 328 |
found.clear(); |
| 329 |
} |
| 330 |
|
| 331 |
@Override |
| 332 |
protected CompilerPass getProcessor(Compiler compiler) { |
| 333 |
return new SimpleDefinitionEnumerator(compiler); |
| 334 |
} |
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
private class SimpleDefinitionEnumerator |
| 340 |
extends AbstractPostOrderCallback implements CompilerPass { |
| 341 |
private final SimpleDefinitionFinder passUnderTest; |
| 342 |
private final Compiler compiler; |
| 343 |
|
| 344 |
SimpleDefinitionEnumerator(Compiler compiler) { |
| 345 |
this.passUnderTest = new SimpleDefinitionFinder(compiler); |
| 346 |
this.compiler = compiler; |
| 347 |
} |
| 348 |
|
| 349 |
@Override |
| 350 |
public void process(Node externs, Node root) { |
| 351 |
passUnderTest.process(externs, root); |
| 352 |
NodeTraversal.traverse(compiler, externs, this); |
| 353 |
NodeTraversal.traverse(compiler, root, this); |
| 354 |
|
| 355 |
for (DefinitionSite defSite : passUnderTest.getDefinitionSites()) { |
| 356 |
Node node = defSite.node; |
| 357 |
Definition definition = defSite.definition; |
| 358 |
StringBuilder sb = new StringBuilder(); |
| 359 |
sb.append("DEF "); |
| 360 |
sb.append(Token.name(node.getType())); |
| 361 |
sb.append(" "); |
| 362 |
sb.append(node.getQualifiedName()); |
| 363 |
sb.append(" -> "); |
| 364 |
|
| 365 |
if (definition.isExtern()) { |
| 366 |
sb.append("EXTERN "); |
| 367 |
} |
| 368 |
|
| 369 |
Node rValue = definition.getRValue(); |
| 370 |
if (rValue != null) { |
| 371 |
sb.append(Token.name(rValue.getType())); |
| 372 |
} else { |
| 373 |
sb.append("<null>"); |
| 374 |
} |
| 375 |
|
| 376 |
found.add(sb.toString()); |
| 377 |
} |
| 378 |
|
| 379 |
} |
| 380 |
|
| 381 |
@Override |
| 382 |
public void visit(NodeTraversal traversal, Node node, Node parent) { |
| 383 |
Collection<Definition> defs = |
| 384 |
passUnderTest.getDefinitionsReferencedAt(node); |
| 385 |
if (defs != null) { |
| 386 |
StringBuilder sb = new StringBuilder(); |
| 387 |
sb.append("USE "); |
| 388 |
sb.append(Token.name(node.getType())); |
| 389 |
sb.append(" "); |
| 390 |
sb.append(node.getQualifiedName()); |
| 391 |
sb.append(" -> "); |
| 392 |
Multiset<String> defstrs = TreeMultiset.create(); |
| 393 |
for (Definition def : defs) { |
| 394 |
String defstr; |
| 395 |
|
| 396 |
Node rValue = def.getRValue(); |
| 397 |
if (rValue != null) { |
| 398 |
defstr = Token.name(rValue.getType()); |
| 399 |
} else { |
| 400 |
defstr = "<null>"; |
| 401 |
} |
| 402 |
|
| 403 |
if (def.isExtern()) { |
| 404 |
defstr = "EXTERN " + defstr; |
| 405 |
} |
| 406 |
|
| 407 |
defstrs.add(defstr); |
| 408 |
} |
| 409 |
|
| 410 |
sb.append(defstrs.toString()); |
| 411 |
found.add(sb.toString()); |
| 412 |
} |
| 413 |
} |
| 414 |
} |
| 415 |
} |