| 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.base.Preconditions; |
| 20 |
import com.google.common.collect.ImmutableSet; |
| 21 |
import com.google.common.collect.Lists; |
| 22 |
import com.google.common.collect.Maps; |
| 23 |
import com.google.common.collect.Sets; |
| 24 |
import com.google.javascript.jscomp.NodeTraversal.AbstractPostOrderCallback; |
| 25 |
import com.google.javascript.jscomp.Scope.Var; |
| 26 |
import com.google.javascript.jscomp.graph.FixedPointGraphTraversal; |
| 27 |
import com.google.javascript.jscomp.graph.LinkedDirectedGraph; |
| 28 |
import com.google.javascript.jscomp.graph.FixedPointGraphTraversal.EdgeCallback; |
| 29 |
import com.google.javascript.rhino.Node; |
| 30 |
import com.google.javascript.rhino.Token; |
| 31 |
|
| 32 |
import java.util.ArrayDeque; |
| 33 |
import java.util.Collection; |
| 34 |
import java.util.Deque; |
| 35 |
import java.util.List; |
| 36 |
import java.util.Map; |
| 37 |
import java.util.Set; |
| 38 |
import java.util.Stack; |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
class AnalyzePrototypeProperties implements CompilerPass { |
| 53 |
|
| 54 |
|
| 55 |
private final SymbolType PROPERTY = SymbolType.PROPERTY; |
| 56 |
private final SymbolType VAR = SymbolType.VAR; |
| 57 |
|
| 58 |
private final AbstractCompiler compiler; |
| 59 |
private final boolean canModifyExterns; |
| 60 |
private final boolean anchorUnusedVars; |
| 61 |
private final JSModuleGraph moduleGraph; |
| 62 |
private final JSModule firstModule; |
| 63 |
|
| 64 |
|
| 65 |
private static final Set<String> IMPLICITLY_USED_PROPERTIES = |
| 66 |
ImmutableSet.of("length", "toString", "valueOf"); |
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
private final LinkedDirectedGraph<NameInfo, JSModule> symbolGraph = |
| 85 |
LinkedDirectedGraph.createWithoutAnnotations(); |
| 86 |
|
| 87 |
|
| 88 |
private final NameInfo globalNode = new NameInfo("[global]"); |
| 89 |
|
| 90 |
|
| 91 |
private final NameInfo externNode = new NameInfo("[extern]"); |
| 92 |
|
| 93 |
|
| 94 |
private final NameInfo anonymousNode = new NameInfo("[anonymous]"); |
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
private final Map<String, NameInfo> propertyNameInfo = Maps.newHashMap(); |
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
private final Map<String, NameInfo> varNameInfo = Maps.newHashMap(); |
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
AnalyzePrototypeProperties(AbstractCompiler compiler, |
| 115 |
JSModuleGraph moduleGraph, boolean canModifyExterns, |
| 116 |
boolean anchorUnusedVars) { |
| 117 |
this.compiler = compiler; |
| 118 |
this.moduleGraph = moduleGraph; |
| 119 |
this.canModifyExterns = canModifyExterns; |
| 120 |
this.anchorUnusedVars = anchorUnusedVars; |
| 121 |
|
| 122 |
if (moduleGraph != null) { |
| 123 |
firstModule = moduleGraph.getRootModule(); |
| 124 |
} else { |
| 125 |
firstModule = null; |
| 126 |
} |
| 127 |
|
| 128 |
globalNode.markReference(null); |
| 129 |
externNode.markReference(null); |
| 130 |
symbolGraph.createNode(globalNode); |
| 131 |
symbolGraph.createNode(externNode); |
| 132 |
|
| 133 |
for (String property : IMPLICITLY_USED_PROPERTIES) { |
| 134 |
NameInfo nameInfo = getNameInfoForName(property, PROPERTY); |
| 135 |
if (moduleGraph == null) { |
| 136 |
symbolGraph.connect(externNode, null, nameInfo); |
| 137 |
} else { |
| 138 |
for (JSModule module : moduleGraph.getAllModules()) { |
| 139 |
symbolGraph.connect(externNode, module, nameInfo); |
| 140 |
} |
| 141 |
} |
| 142 |
} |
| 143 |
} |
| 144 |
|
| 145 |
@Override |
| 146 |
public void process(Node externRoot, Node root) { |
| 147 |
if (!canModifyExterns) { |
| 148 |
NodeTraversal.traverse(compiler, externRoot, |
| 149 |
new ProcessExternProperties()); |
| 150 |
} |
| 151 |
|
| 152 |
NodeTraversal.traverse(compiler, root, new ProcessProperties()); |
| 153 |
|
| 154 |
FixedPointGraphTraversal<NameInfo, JSModule> t = |
| 155 |
FixedPointGraphTraversal.newTraversal(new PropagateReferences()); |
| 156 |
t.computeFixedPoint(symbolGraph, |
| 157 |
Sets.newHashSet(externNode, globalNode)); |
| 158 |
} |
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
public Collection<NameInfo> getAllNameInfo() { |
| 164 |
List<NameInfo> result = Lists.newArrayList(propertyNameInfo.values()); |
| 165 |
result.addAll(varNameInfo.values()); |
| 166 |
return result; |
| 167 |
} |
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
private NameInfo getNameInfoForName(String name, SymbolType type) { |
| 177 |
Map<String, NameInfo> map = type == PROPERTY ? |
| 178 |
propertyNameInfo : varNameInfo; |
| 179 |
if (map.containsKey(name)) { |
| 180 |
return map.get(name); |
| 181 |
} else { |
| 182 |
NameInfo nameInfo = new NameInfo(name); |
| 183 |
map.put(name, nameInfo); |
| 184 |
symbolGraph.createNode(nameInfo); |
| 185 |
return nameInfo; |
| 186 |
} |
| 187 |
} |
| 188 |
|
| 189 |
private class ProcessProperties implements NodeTraversal.ScopedCallback { |
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
private Stack<NameContext> symbolStack = new Stack<NameContext>(); |
| 200 |
|
| 201 |
@Override |
| 202 |
public void enterScope(NodeTraversal t) { |
| 203 |
Node n = t.getCurrentNode(); |
| 204 |
if (n.isFunction()) { |
| 205 |
String propName = getPrototypePropertyNameFromRValue(n); |
| 206 |
if (propName != null) { |
| 207 |
symbolStack.push( |
| 208 |
new NameContext( |
| 209 |
getNameInfoForName(propName, PROPERTY), |
| 210 |
t.getScope())); |
| 211 |
} else if (isGlobalFunctionDeclaration(t, n)) { |
| 212 |
Node parent = n.getParent(); |
| 213 |
String name = parent.isName() ? |
| 214 |
parent.getString() : |
| 215 |
n.getFirstChild().getString() ; |
| 216 |
symbolStack.push( |
| 217 |
new NameContext(getNameInfoForName(name, VAR), t.getScope())); |
| 218 |
} else { |
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
symbolStack.push(new NameContext(anonymousNode, t.getScope())); |
| 225 |
} |
| 226 |
} else { |
| 227 |
Preconditions.checkState(t.inGlobalScope()); |
| 228 |
symbolStack.push(new NameContext(globalNode, t.getScope())); |
| 229 |
} |
| 230 |
} |
| 231 |
|
| 232 |
@Override |
| 233 |
public void exitScope(NodeTraversal t) { |
| 234 |
symbolStack.pop(); |
| 235 |
} |
| 236 |
|
| 237 |
@Override |
| 238 |
public boolean shouldTraverse(NodeTraversal t, Node n, Node parent) { |
| 239 |
|
| 240 |
String propName = processNonFunctionPrototypeAssign(n, parent); |
| 241 |
if (propName != null) { |
| 242 |
symbolStack.push( |
| 243 |
new NameContext( |
| 244 |
getNameInfoForName(propName, PROPERTY), null)); |
| 245 |
} |
| 246 |
return true; |
| 247 |
} |
| 248 |
|
| 249 |
@Override |
| 250 |
public void visit(NodeTraversal t, Node n, Node parent) { |
| 251 |
if (n.isGetProp()) { |
| 252 |
String propName = n.getFirstChild().getNext().getString(); |
| 253 |
|
| 254 |
if (n.isQualifiedName()) { |
| 255 |
if (propName.equals("prototype")) { |
| 256 |
if (processPrototypeRef(t, n)) { |
| 257 |
return; |
| 258 |
} |
| 259 |
} else if (compiler.getCodingConvention().isExported(propName)) { |
| 260 |
addGlobalUseOfSymbol(propName, t.getModule(), PROPERTY); |
| 261 |
return; |
| 262 |
} else { |
| 263 |
|
| 264 |
if (n.getParent().isAssign() && n.getNext() != null) { |
| 265 |
String rValueName = getPrototypePropertyNameFromRValue(n); |
| 266 |
if (rValueName != null) { |
| 267 |
return; |
| 268 |
} |
| 269 |
} |
| 270 |
} |
| 271 |
} |
| 272 |
|
| 273 |
addSymbolUse(propName, t.getModule(), PROPERTY); |
| 274 |
} else if (n.isObjectLit()) { |
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
String lValueName = NodeUtil.getBestLValueName( |
| 279 |
NodeUtil.getBestLValue(n)); |
| 280 |
if (lValueName != null && lValueName.endsWith(".prototype")) { |
| 281 |
return; |
| 282 |
} |
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
for (Node propNameNode = n.getFirstChild(); propNameNode != null; |
| 287 |
propNameNode = propNameNode.getNext()) { |
| 288 |
|
| 289 |
if (!propNameNode.isQuotedString()) { |
| 290 |
addSymbolUse(propNameNode.getString(), t.getModule(), PROPERTY); |
| 291 |
} |
| 292 |
} |
| 293 |
} else if (n.isName()) { |
| 294 |
String name = n.getString(); |
| 295 |
|
| 296 |
Var var = t.getScope().getVar(name); |
| 297 |
if (var != null) { |
| 298 |
|
| 299 |
if (var.isGlobal()) { |
| 300 |
if (var.getInitialValue() != null && |
| 301 |
var.getInitialValue().isFunction()) { |
| 302 |
if (t.inGlobalScope()) { |
| 303 |
if (!processGlobalFunctionDeclaration(t, n, var)) { |
| 304 |
addGlobalUseOfSymbol(name, t.getModule(), VAR); |
| 305 |
} |
| 306 |
} else { |
| 307 |
addSymbolUse(name, t.getModule(), VAR); |
| 308 |
} |
| 309 |
} |
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
} else if (var.getScope() != t.getScope()){ |
| 315 |
for (int i = symbolStack.size() - 1; i >= 0; i--) { |
| 316 |
NameContext context = symbolStack.get(i); |
| 317 |
if (context.scope == var.getScope()) { |
| 318 |
break; |
| 319 |
} |
| 320 |
|
| 321 |
context.name.readClosureVariables = true; |
| 322 |
} |
| 323 |
} |
| 324 |
} |
| 325 |
} |
| 326 |
|
| 327 |
|
| 328 |
if (processNonFunctionPrototypeAssign(n, parent) != null) { |
| 329 |
symbolStack.pop(); |
| 330 |
} |
| 331 |
} |
| 332 |
|
| 333 |
private void addSymbolUse(String name, JSModule module, SymbolType type) { |
| 334 |
NameInfo info = getNameInfoForName(name, type); |
| 335 |
NameInfo def = null; |
| 336 |
|
| 337 |
for (int i = symbolStack.size() - 1; i >= 0; i--) { |
| 338 |
def = symbolStack.get(i).name; |
| 339 |
if (def != anonymousNode) { |
| 340 |
break; |
| 341 |
} |
| 342 |
} |
| 343 |
if (!def.equals(info)) { |
| 344 |
symbolGraph.connect(def, module, info); |
| 345 |
} |
| 346 |
} |
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
private String processNonFunctionPrototypeAssign(Node n, Node parent) { |
| 353 |
if (isAssignRValue(n, parent) && !n.isFunction()) { |
| 354 |
return getPrototypePropertyNameFromRValue(n); |
| 355 |
} |
| 356 |
return null; |
| 357 |
} |
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
private boolean isGlobalFunctionDeclaration(NodeTraversal t, Node n) { |
| 364 |
|
| 365 |
|
| 366 |
Scope s = t.getScope(); |
| 367 |
if (!(s.isGlobal() || |
| 368 |
s.getDepth() == 1 && s.getRootNode() == n)) { |
| 369 |
return false; |
| 370 |
} |
| 371 |
|
| 372 |
return NodeUtil.isFunctionDeclaration(n) || |
| 373 |
n.isFunction() && n.getParent().isName(); |
| 374 |
} |
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
private boolean isAssignRValue(Node n, Node parent) { |
| 380 |
return parent != null && parent.isAssign() && parent.getFirstChild() != n; |
| 381 |
} |
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
private String getPrototypePropertyNameFromRValue(Node rValue) { |
| 391 |
Node lValue = NodeUtil.getBestLValue(rValue); |
| 392 |
if (lValue == null || |
| 393 |
lValue.getParent() == null || |
| 394 |
lValue.getParent().getParent() == null || |
| 395 |
!(NodeUtil.isObjectLitKey(lValue) || |
| 396 |
NodeUtil.isExprAssign(lValue.getParent().getParent()))) { |
| 397 |
return null; |
| 398 |
} |
| 399 |
|
| 400 |
String lValueName = |
| 401 |
NodeUtil.getBestLValueName(NodeUtil.getBestLValue(rValue)); |
| 402 |
if (lValueName == null) { |
| 403 |
return null; |
| 404 |
} |
| 405 |
int lastDot = lValueName.lastIndexOf('.'); |
| 406 |
if (lastDot == -1) { |
| 407 |
return null; |
| 408 |
} |
| 409 |
|
| 410 |
String firstPart = lValueName.substring(0, lastDot); |
| 411 |
if (!firstPart.endsWith(".prototype")) { |
| 412 |
return null; |
| 413 |
} |
| 414 |
|
| 415 |
return lValueName.substring(lastDot + 1); |
| 416 |
} |
| 417 |
|
| 418 |
|
| 419 |
|
| 420 |
|
| 421 |
|
| 422 |
private boolean processGlobalFunctionDeclaration(NodeTraversal t, |
| 423 |
Node nameNode, Var v) { |
| 424 |
Node firstChild = nameNode.getFirstChild(); |
| 425 |
Node parent = nameNode.getParent(); |
| 426 |
|
| 427 |
if ( |
| 428 |
isGlobalFunctionDeclaration(t, parent) || |
| 429 |
|
| 430 |
firstChild != null && |
| 431 |
isGlobalFunctionDeclaration(t, firstChild)) { |
| 432 |
String name = nameNode.getString(); |
| 433 |
getNameInfoForName(name, VAR).getDeclarations().add( |
| 434 |
new GlobalFunction(nameNode, v, t.getModule())); |
| 435 |
|
| 436 |
|
| 437 |
|
| 438 |
if (compiler.getCodingConvention().isExported(name) || |
| 439 |
anchorUnusedVars) { |
| 440 |
addGlobalUseOfSymbol(name, t.getModule(), VAR); |
| 441 |
} |
| 442 |
|
| 443 |
return true; |
| 444 |
} |
| 445 |
return false; |
| 446 |
} |
| 447 |
|
| 448 |
|
| 449 |
|
| 450 |
|
| 451 |
|
| 452 |
|
| 453 |
|
| 454 |
private boolean processPrototypeRef(NodeTraversal t, Node ref) { |
| 455 |
Node root = NodeUtil.getRootOfQualifiedName(ref); |
| 456 |
|
| 457 |
Node n = ref.getParent(); |
| 458 |
switch (n.getType()) { |
| 459 |
|
| 460 |
case Token.GETPROP: |
| 461 |
Node dest = n.getFirstChild().getNext(); |
| 462 |
Node parent = n.getParent(); |
| 463 |
Node grandParent = parent.getParent(); |
| 464 |
|
| 465 |
if (dest.isString() && |
| 466 |
NodeUtil.isExprAssign(grandParent) && |
| 467 |
NodeUtil.isVarOrSimpleAssignLhs(n, parent)) { |
| 468 |
String name = dest.getString(); |
| 469 |
Property prop = new AssignmentProperty( |
| 470 |
grandParent, |
| 471 |
maybeGetVar(t, root), |
| 472 |
t.getModule()); |
| 473 |
getNameInfoForName(name, PROPERTY).getDeclarations().add(prop); |
| 474 |
return true; |
| 475 |
} |
| 476 |
break; |
| 477 |
|
| 478 |
|
| 479 |
case Token.ASSIGN: |
| 480 |
Node map = n.getFirstChild().getNext(); |
| 481 |
if (map.isObjectLit()) { |
| 482 |
for (Node key = map.getFirstChild(); |
| 483 |
key != null; key = key.getNext()) { |
| 484 |
|
| 485 |
String name = key.getString(); |
| 486 |
Property prop = new LiteralProperty( |
| 487 |
key, key.getFirstChild(), map, n, |
| 488 |
maybeGetVar(t, root), |
| 489 |
t.getModule()); |
| 490 |
getNameInfoForName(name, PROPERTY).getDeclarations().add(prop); |
| 491 |
} |
| 492 |
return true; |
| 493 |
} |
| 494 |
break; |
| 495 |
} |
| 496 |
return false; |
| 497 |
} |
| 498 |
|
| 499 |
private Var maybeGetVar(NodeTraversal t, Node maybeName) { |
| 500 |
return maybeName.isName() |
| 501 |
? t.getScope().getVar(maybeName.getString()) : null; |
| 502 |
} |
| 503 |
|
| 504 |
private void addGlobalUseOfSymbol(String name, JSModule module, |
| 505 |
SymbolType type) { |
| 506 |
symbolGraph.connect(globalNode, module, getNameInfoForName(name, type)); |
| 507 |
} |
| 508 |
} |
| 509 |
|
| 510 |
private class ProcessExternProperties extends AbstractPostOrderCallback { |
| 511 |
@Override |
| 512 |
public void visit(NodeTraversal t, Node n, Node parent) { |
| 513 |
if (n.isGetProp()) { |
| 514 |
symbolGraph.connect(externNode, firstModule, |
| 515 |
getNameInfoForName(n.getLastChild().getString(), PROPERTY)); |
| 516 |
} |
| 517 |
} |
| 518 |
} |
| 519 |
|
| 520 |
private class PropagateReferences |
| 521 |
implements EdgeCallback<NameInfo, JSModule> { |
| 522 |
@Override |
| 523 |
public boolean traverseEdge(NameInfo start, JSModule edge, NameInfo dest) { |
| 524 |
if (start.isReferenced()) { |
| 525 |
JSModule startModule = start.getDeepestCommonModuleRef(); |
| 526 |
if (startModule != null && |
| 527 |
moduleGraph.dependsOn(startModule, edge)) { |
| 528 |
return dest.markReference(startModule); |
| 529 |
} else { |
| 530 |
return dest.markReference(edge); |
| 531 |
} |
| 532 |
} |
| 533 |
return false; |
| 534 |
} |
| 535 |
} |
| 536 |
|
| 537 |
|
| 538 |
|
| 539 |
|
| 540 |
|
| 541 |
|
| 542 |
|
| 543 |
|
| 544 |
interface Symbol { |
| 545 |
|
| 546 |
|
| 547 |
|
| 548 |
void remove(); |
| 549 |
|
| 550 |
|
| 551 |
|
| 552 |
|
| 553 |
Var getRootVar(); |
| 554 |
|
| 555 |
|
| 556 |
|
| 557 |
|
| 558 |
JSModule getModule(); |
| 559 |
} |
| 560 |
|
| 561 |
private enum SymbolType { |
| 562 |
PROPERTY, |
| 563 |
VAR; |
| 564 |
} |
| 565 |
|
| 566 |
|
| 567 |
|
| 568 |
|
| 569 |
class GlobalFunction implements Symbol { |
| 570 |
private final Node nameNode; |
| 571 |
private final Var var; |
| 572 |
private final JSModule module; |
| 573 |
|
| 574 |
GlobalFunction(Node nameNode, Var var, JSModule module) { |
| 575 |
Node parent = nameNode.getParent(); |
| 576 |
Preconditions.checkState( |
| 577 |
parent.isVar() || |
| 578 |
NodeUtil.isFunctionDeclaration(parent)); |
| 579 |
this.nameNode = nameNode; |
| 580 |
this.var = var; |
| 581 |
this.module = module; |
| 582 |
} |
| 583 |
|
| 584 |
@Override |
| 585 |
public Var getRootVar() { |
| 586 |
return var; |
| 587 |
} |
| 588 |
|
| 589 |
@Override |
| 590 |
public void remove() { |
| 591 |
Node parent = nameNode.getParent(); |
| 592 |
if (parent.isFunction() || parent.hasOneChild()) { |
| 593 |
NodeUtil.removeChild(parent.getParent(), parent); |
| 594 |
} else { |
| 595 |
Preconditions.checkState(parent.isVar()); |
| 596 |
parent.removeChild(nameNode); |
| 597 |
} |
| 598 |
} |
| 599 |
|
| 600 |
@Override |
| 601 |
public JSModule getModule() { |
| 602 |
return module; |
| 603 |
} |
| 604 |
|
| 605 |
public Node getFunctionNode() { |
| 606 |
Node parent = nameNode.getParent(); |
| 607 |
|
| 608 |
if (parent.isFunction()) { |
| 609 |
return parent; |
| 610 |
} else { |
| 611 |
|
| 612 |
return nameNode.getChildAtIndex(1); |
| 613 |
} |
| 614 |
} |
| 615 |
} |
| 616 |
|
| 617 |
|
| 618 |
|
| 619 |
|
| 620 |
|
| 621 |
interface Property extends Symbol { |
| 622 |
|
| 623 |
|
| 624 |
Node getPrototype(); |
| 625 |
|
| 626 |
|
| 627 |
Node getValue(); |
| 628 |
} |
| 629 |
|
| 630 |
|
| 631 |
|
| 632 |
|
| 633 |
|
| 634 |
|
| 635 |
|
| 636 |
static class AssignmentProperty implements Property { |
| 637 |
private final Node exprNode; |
| 638 |
private final Var rootVar; |
| 639 |
private final JSModule module; |
| 640 |
|
| 641 |
|
| 642 |
|
| 643 |
|
| 644 |
AssignmentProperty(Node node, Var rootVar, JSModule module) { |
| 645 |
this.exprNode = node; |
| 646 |
this.rootVar = rootVar; |
| 647 |
this.module = module; |
| 648 |
} |
| 649 |
|
| 650 |
@Override |
| 651 |
public Var getRootVar() { |
| 652 |
return rootVar; |
| 653 |
} |
| 654 |
|
| 655 |
@Override |
| 656 |
public void remove() { |
| 657 |
NodeUtil.removeChild(exprNode.getParent(), exprNode); |
| 658 |
} |
| 659 |
|
| 660 |
@Override |
| 661 |
public Node getPrototype() { |
| 662 |
return getAssignNode().getFirstChild().getFirstChild(); |
| 663 |
} |
| 664 |
|
| 665 |
@Override |
| 666 |
public Node getValue() { |
| 667 |
return getAssignNode().getLastChild(); |
| 668 |
} |
| 669 |
|
| 670 |
private Node getAssignNode() { |
| 671 |
return exprNode.getFirstChild(); |
| 672 |
} |
| 673 |
|
| 674 |
@Override |
| 675 |
public JSModule getModule() { |
| 676 |
return module; |
| 677 |
} |
| 678 |
} |
| 679 |
|
| 680 |
|
| 681 |
|
| 682 |
|
| 683 |
|
| 684 |
|
| 685 |
|
| 686 |
static class LiteralProperty implements Property { |
| 687 |
private final Node key; |
| 688 |
private final Node value; |
| 689 |
private final Node map; |
| 690 |
private final Node assign; |
| 691 |
private final Var rootVar; |
| 692 |
private final JSModule module; |
| 693 |
|
| 694 |
LiteralProperty(Node key, Node value, Node map, Node assign, |
| 695 |
Var rootVar, JSModule module) { |
| 696 |
this.key = key; |
| 697 |
this.value = value; |
| 698 |
this.map = map; |
| 699 |
this.assign = assign; |
| 700 |
this.rootVar = rootVar; |
| 701 |
this.module = module; |
| 702 |
} |
| 703 |
|
| 704 |
@Override |
| 705 |
public Var getRootVar() { |
| 706 |
return rootVar; |
| 707 |
} |
| 708 |
|
| 709 |
@Override |
| 710 |
public void remove() { |
| 711 |
map.removeChild(key); |
| 712 |
} |
| 713 |
|
| 714 |
@Override |
| 715 |
public Node getPrototype() { |
| 716 |
return assign.getFirstChild(); |
| 717 |
} |
| 718 |
|
| 719 |
@Override |
| 720 |
public Node getValue() { |
| 721 |
return value; |
| 722 |
} |
| 723 |
|
| 724 |
@Override |
| 725 |
public JSModule getModule() { |
| 726 |
return module; |
| 727 |
} |
| 728 |
} |
| 729 |
|
| 730 |
|
| 731 |
|
| 732 |
|
| 733 |
|
| 734 |
private class NameContext { |
| 735 |
final NameInfo name; |
| 736 |
|
| 737 |
|
| 738 |
|
| 739 |
final Scope scope; |
| 740 |
|
| 741 |
NameContext(NameInfo name, Scope scope) { |
| 742 |
this.name = name; |
| 743 |
this.scope = scope; |
| 744 |
} |
| 745 |
} |
| 746 |
|
| 747 |
|
| 748 |
|
| 749 |
|
| 750 |
class NameInfo { |
| 751 |
|
| 752 |
final String name; |
| 753 |
|
| 754 |
private boolean referenced = false; |
| 755 |
private final Deque<Symbol> declarations = new ArrayDeque<Symbol>(); |
| 756 |
private JSModule deepestCommonModuleRef = null; |
| 757 |
|
| 758 |
|
| 759 |
|
| 760 |
private boolean readClosureVariables = false; |
| 761 |
|
| 762 |
|
| 763 |
|
| 764 |
|
| 765 |
|
| 766 |
|
| 767 |
NameInfo(String name) { |
| 768 |
this.name = name; |
| 769 |
} |
| 770 |
|
| 771 |
@Override public String toString() { return name; } |
| 772 |
|
| 773 |
|
| 774 |
boolean isReferenced() { |
| 775 |
return referenced; |
| 776 |
} |
| 777 |
|
| 778 |
|
| 779 |
boolean readsClosureVariables() { |
| 780 |
return readClosureVariables; |
| 781 |
} |
| 782 |
|
| 783 |
|
| 784 |
|
| 785 |
|
| 786 |
|
| 787 |
|
| 788 |
|
| 789 |
boolean markReference(JSModule module) { |
| 790 |
boolean hasChanged = false; |
| 791 |
if (!referenced) { |
| 792 |
referenced = true; |
| 793 |
hasChanged = true; |
| 794 |
} |
| 795 |
|
| 796 |
if (moduleGraph != null) { |
| 797 |
JSModule originalDeepestCommon = deepestCommonModuleRef; |
| 798 |
|
| 799 |
if (deepestCommonModuleRef == null) { |
| 800 |
deepestCommonModuleRef = module; |
| 801 |
} else { |
| 802 |
deepestCommonModuleRef = |
| 803 |
moduleGraph.getDeepestCommonDependencyInclusive( |
| 804 |
deepestCommonModuleRef, module); |
| 805 |
} |
| 806 |
|
| 807 |
if (originalDeepestCommon != deepestCommonModuleRef) { |
| 808 |
hasChanged = true; |
| 809 |
} |
| 810 |
} |
| 811 |
|
| 812 |
return hasChanged; |
| 813 |
} |
| 814 |
|
| 815 |
|
| 816 |
|
| 817 |
|
| 818 |
|
| 819 |
JSModule getDeepestCommonModuleRef() { |
| 820 |
return deepestCommonModuleRef; |
| 821 |
} |
| 822 |
|
| 823 |
|
| 824 |
|
| 825 |
|
| 826 |
|
| 827 |
Deque<Symbol> getDeclarations() { |
| 828 |
return declarations; |
| 829 |
} |
| 830 |
} |
| 831 |
} |