| 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.Sets; |
| 21 |
import com.google.javascript.jscomp.NodeTraversal.AbstractPostOrderCallback; |
| 22 |
import com.google.javascript.rhino.Node; |
| 23 |
import java.util.Set; |
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
class GatherRawExports extends AbstractPostOrderCallback |
| 32 |
implements CompilerPass { |
| 33 |
|
| 34 |
private final AbstractCompiler compiler; |
| 35 |
|
| 36 |
private static final String GLOBAL_THIS_NAMES[] = { "window", "top" }; |
| 37 |
|
| 38 |
private final Set<String> exportedVariables = Sets.newHashSet(); |
| 39 |
|
| 40 |
GatherRawExports(AbstractCompiler compiler) { |
| 41 |
this.compiler = compiler; |
| 42 |
} |
| 43 |
|
| 44 |
@Override |
| 45 |
public void process(Node externs, Node root) { |
| 46 |
Preconditions.checkState(compiler.getLifeCycleStage().isNormalized()); |
| 47 |
NodeTraversal.traverse(compiler, root, this); |
| 48 |
} |
| 49 |
|
| 50 |
@Override |
| 51 |
public void visit(NodeTraversal t, Node n, Node parent) { |
| 52 |
Node sibling = n.getNext(); |
| 53 |
if (sibling != null |
| 54 |
&& sibling.isString() |
| 55 |
&& NodeUtil.isGet(parent)) { |
| 56 |
|
| 57 |
|
| 58 |
if (isGlobalThisObject(t, n)) { |
| 59 |
exportedVariables.add(sibling.getString()); |
| 60 |
} |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
private boolean isGlobalThisObject(NodeTraversal t, Node n) { |
| 65 |
if (n.isThis()) { |
| 66 |
return t.inGlobalScope(); |
| 67 |
} else if (n.isName()) { |
| 68 |
String varName = n.getString(); |
| 69 |
int items = GLOBAL_THIS_NAMES.length; |
| 70 |
for (int i = 0; i < items; i++) { |
| 71 |
if (varName.equals(GLOBAL_THIS_NAMES[i])) { |
| 72 |
return true; |
| 73 |
} |
| 74 |
} |
| 75 |
} |
| 76 |
return false; |
| 77 |
} |
| 78 |
|
| 79 |
public Set<String> getExportedVariableNames() { |
| 80 |
return exportedVariables; |
| 81 |
} |
| 82 |
} |