| 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.DefaultPassConfig.HotSwapPassFactory; |
| 22 |
import com.google.javascript.jscomp.GlobalVarReferenceMap.GlobalVarRefCleanupPass; |
| 23 |
import com.google.javascript.jscomp.Scope.Var; |
| 24 |
import com.google.javascript.rhino.Node; |
| 25 |
import com.google.javascript.rhino.jstype.FunctionType; |
| 26 |
import com.google.javascript.rhino.jstype.JSType; |
| 27 |
|
| 28 |
import java.util.List; |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
class CleanupPasses extends PassConfig { |
| 37 |
|
| 38 |
private State state; |
| 39 |
|
| 40 |
public CleanupPasses(CompilerOptions options) { |
| 41 |
super(options); |
| 42 |
} |
| 43 |
|
| 44 |
@Override |
| 45 |
protected List<PassFactory> getChecks() { |
| 46 |
List<PassFactory> checks = Lists.newArrayList(); |
| 47 |
checks.add(fieldCleanupPassFactory); |
| 48 |
checks.add(scopeCleanupPassFactory); |
| 49 |
checks.add(globalVarRefCleanupPassFactory); |
| 50 |
return checks; |
| 51 |
} |
| 52 |
|
| 53 |
@Override |
| 54 |
protected State getIntermediateState() { |
| 55 |
return state; |
| 56 |
} |
| 57 |
|
| 58 |
@Override |
| 59 |
protected List<PassFactory> getOptimizations() { |
| 60 |
return ImmutableList.of(); |
| 61 |
} |
| 62 |
|
| 63 |
@Override |
| 64 |
protected void setIntermediateState(State state) { |
| 65 |
this.state = state; |
| 66 |
} |
| 67 |
|
| 68 |
final PassFactory fieldCleanupPassFactory = |
| 69 |
new HotSwapPassFactory("FieldCleaupPassFactory", false) { |
| 70 |
@Override |
| 71 |
protected HotSwapCompilerPass create( |
| 72 |
AbstractCompiler compiler) { |
| 73 |
return new FieldCleanupPass(compiler); |
| 74 |
} |
| 75 |
}; |
| 76 |
|
| 77 |
final PassFactory scopeCleanupPassFactory = |
| 78 |
new HotSwapPassFactory("ScopeCleanupPassFactory", false) { |
| 79 |
@Override |
| 80 |
protected HotSwapCompilerPass create( |
| 81 |
AbstractCompiler compiler) { |
| 82 |
return new MemoizedScopeCleanupPass(compiler); |
| 83 |
} |
| 84 |
}; |
| 85 |
|
| 86 |
final PassFactory globalVarRefCleanupPassFactory = |
| 87 |
new HotSwapPassFactory("GlobalVarRefCleanupPassFactory", false) { |
| 88 |
@Override |
| 89 |
protected HotSwapCompilerPass create( |
| 90 |
AbstractCompiler compiler) { |
| 91 |
return new GlobalVarRefCleanupPass(compiler); |
| 92 |
} |
| 93 |
}; |
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
static class MemoizedScopeCleanupPass implements HotSwapCompilerPass { |
| 103 |
|
| 104 |
private final AbstractCompiler compiler; |
| 105 |
|
| 106 |
public MemoizedScopeCleanupPass(AbstractCompiler compiler) { |
| 107 |
this.compiler = compiler; |
| 108 |
} |
| 109 |
|
| 110 |
@Override |
| 111 |
public void hotSwapScript(Node scriptRoot, Node originalRoot) { |
| 112 |
ScopeCreator creator = compiler.getTypedScopeCreator(); |
| 113 |
if (creator instanceof MemoizedScopeCreator) { |
| 114 |
MemoizedScopeCreator scopeCreator = (MemoizedScopeCreator) creator; |
| 115 |
String newSrc = scriptRoot.getSourceFileName(); |
| 116 |
for (Var var : scopeCreator.getAllSymbols()) { |
| 117 |
JSType type = var.getType(); |
| 118 |
if (type != null) { |
| 119 |
FunctionType fnType = type.toMaybeFunctionType(); |
| 120 |
if (fnType != null |
| 121 |
&& newSrc.equals(NodeUtil.getSourceName(fnType.getSource()))) { |
| 122 |
fnType.setSource(null); |
| 123 |
} |
| 124 |
} |
| 125 |
} |
| 126 |
scopeCreator.removeScopesForScript(originalRoot.getSourceFileName()); |
| 127 |
} |
| 128 |
} |
| 129 |
|
| 130 |
@Override |
| 131 |
public void process(Node externs, Node root) { |
| 132 |
|
| 133 |
} |
| 134 |
} |
| 135 |
} |