final class RescopeGlobalSymbols extends java.lang.Object implements CompilerPass
var a = 1; function b() { return a }
becomes
NS.a = 1; NS.b = function b() { return NS.a }
This allows splitting code into modules that depend on each other's
global symbols, without using polluting JavaScript's global scope with those
symbols. You typically define just a single global symbol, wrap each module
in a function wrapper, and pass the global symbol around, eg,
var uniqueNs = uniqueNs || {};
(function (NS) { ...your module code here... })(uniqueNs);
This compile step requires moveFunctionDeclarations to be turned on to guarantee semantics.
For lots of examples, see the unit test.
| Modifier and Type | Class and Description |
|---|---|
private class |
RescopeGlobalSymbols.FindCrossModuleNamesCallback
Find all global names that are used in more than one module.
|
private class |
RescopeGlobalSymbols.FindNamesReferencingThis
Builds the maybeReferencesThis set of names that may reference a function
that references this.
|
private class |
RescopeGlobalSymbols.MakeExternsReferenceWindowExplicitly
Rewrites extern names to be explicit children of window instead of only
implicitly referencing it.
|
private class |
RescopeGlobalSymbols.RemoveGlobalVarCallback
Removes every occurrence of var that declares a global variable.
|
private class |
RescopeGlobalSymbols.RewriteGlobalFunctionStatementsToVarAssignmentsCallback
Rewrites function statements to var statements + assignment.
|
private class |
RescopeGlobalSymbols.RewriteScopeCallback
Visits each NAME token and checks whether it refers to a global variable.
|
| Modifier and Type | Field and Description |
|---|---|
private boolean |
addExtern |
private boolean |
assumeCrossModuleNames |
private AbstractCompiler |
compiler |
private java.util.Set<java.lang.String> |
crossModuleNames |
private static java.lang.String |
DISAMBIGUATION_SUFFIX |
private java.lang.String |
globalSymbolNamespace |
private java.util.Set<java.lang.String> |
maybeReferencesThis |
private static java.util.Set<java.lang.String> |
SPECIAL_EXTERNS |
private static java.lang.String |
WINDOW |
| Constructor and Description |
|---|
RescopeGlobalSymbols(AbstractCompiler compiler,
java.lang.String globalSymbolNamespace,
boolean assumeCrossModuleNames)
Constructor for the RescopeGlobalSymbols compiler pass.
|
RescopeGlobalSymbols(AbstractCompiler compiler,
java.lang.String globalSymbolNamespace,
boolean addExtern,
boolean assumeCrossModuleNames)
Constructor for the RescopeGlobalSymbols compiler pass for use in testing.
|
| Modifier and Type | Method and Description |
|---|---|
private void |
addExternForGlobalSymbolNamespace() |
private boolean |
isCrossModuleName(java.lang.String name) |
void |
process(Node externs,
Node root)
Process the JS with root node root.
|
private static final java.lang.String DISAMBIGUATION_SUFFIX
private static final java.lang.String WINDOW
private static final java.util.Set<java.lang.String> SPECIAL_EXTERNS
private final AbstractCompiler compiler
private final java.lang.String globalSymbolNamespace
private final boolean addExtern
private final boolean assumeCrossModuleNames
private final java.util.Set<java.lang.String> crossModuleNames
private final java.util.Set<java.lang.String> maybeReferencesThis
RescopeGlobalSymbols(AbstractCompiler compiler, java.lang.String globalSymbolNamespace, boolean assumeCrossModuleNames)
compiler - The JSCompiler, for reporting code changes.globalSymbolNamespace - Name of namespace into which all global
symbols are transferred.assumeCrossModuleNames - If true, all global symbols will be assumed
cross module boundaries and thus require renaming.RescopeGlobalSymbols(AbstractCompiler compiler, java.lang.String globalSymbolNamespace, boolean addExtern, boolean assumeCrossModuleNames)
compiler - The JSCompiler, for reporting code changes.globalSymbolNamespace - Name of namespace into which all global
symbols are transferred.addExtern - If true, the compiler will consider the
globalSymbolNamespace an extern name.assumeCrossModuleNames - If true, all global symbols will be assumed
cross module boundaries and thus require renaming.
VisibleForTestingprivate boolean isCrossModuleName(java.lang.String name)
private void addExternForGlobalSymbolNamespace()
public void process(Node externs, Node root)
CompilerPassprocess in interface CompilerPassexterns - Top of external JS treeroot - Top of JS tree