final class ClosureRewriteModule extends java.lang.Object implements NodeTraversal.Callback, HotSwapCompilerPass
goog.module('namespace'); var foo = goog.require('another.namespace'); ...becomes
goog.provide('namespace'); goog.require('another.namespace'); goog.scope(function() { var foo = another.namespace; ... });
Modifier and Type | Class and Description |
---|---|
private class |
ClosureRewriteModule.ModuleDescription |
Modifier and Type | Field and Description |
---|---|
private AbstractCompiler |
compiler |
private ClosureRewriteModule.ModuleDescription |
current |
(package private) static DiagnosticType |
INVALID_EXPORT_COMPUTED_PROPERTY |
(package private) static DiagnosticType |
INVALID_GET_ALIAS |
(package private) static DiagnosticType |
INVALID_GET_CALL_SCOPE |
(package private) static DiagnosticType |
INVALID_GET_IDENTIFIER |
(package private) static DiagnosticType |
INVALID_MODULE_IDENTIFIER |
(package private) static DiagnosticType |
INVALID_REQUIRE_IDENTIFIER |
private static com.google.common.collect.ImmutableSet<java.lang.String> |
USE_STRICT_ONLY |
(package private) static DiagnosticType |
USELESS_USE_STRICT_DIRECTIVE |
Constructor and Description |
---|
ClosureRewriteModule(AbstractCompiler compiler) |
Modifier and Type | Method and Description |
---|---|
private static void |
checkStrictModeDirective(NodeTraversal t,
Node n) |
private java.util.List<java.lang.String> |
collectRoots(ClosureRewriteModule.ModuleDescription module) |
private void |
enterModule(Node n) |
private Node |
getInsertRoot(Node n) |
private Node |
getModuleScopeRootForLoadModuleCall(Node n) |
private Node |
getModuleStatementRootForLoadModuleCall(Node n) |
private java.lang.String |
getRootName(Node n) |
void |
hotSwapScript(Node scriptRoot,
Node originalRoot)
Process the JS with root node root.
|
private boolean |
inModule() |
private static boolean |
isAssignTarget(Node n) |
private static boolean |
isCallTo(Node n,
java.lang.String qname) |
private static boolean |
isExportPropAssign(Node n) |
private static boolean |
isGetModuleCall(Node n) |
private static boolean |
isGetModuleCallAlias(Node n) |
private boolean |
isHeaderNode(Node n) |
private static boolean |
isLoadModuleCall(Node n) |
private void |
maybeUpdateExportDeclToNode(NodeTraversal t,
Node target,
Node value) |
private void |
maybeUpdateExportObjectDecl(NodeTraversal t,
Node n)
For exports like "exports = {prop: value}" update the declarations to enforce
|
private void |
moveChildrenAfter(Node fromNode,
Node targetBlock) |
void |
process(Node externs,
Node root)
Process the JS with root node root.
|
private void |
recordAndUpdateModule(NodeTraversal t,
Node call) |
private void |
recordRequire(NodeTraversal t,
Node call) |
private void |
rewriteGetModuleCall(NodeTraversal t,
Node n) |
private void |
rewriteGetModuleCallAlias(NodeTraversal t,
Node n) |
private void |
rewriteModuleAsScope(Node root) |
boolean |
shouldTraverse(NodeTraversal t,
Node n,
Node parent)
Visits a node in pre order (before visiting its children) and decides
whether this node's children should be traversed.
|
private Node |
skipHeaderNodes(Node script) |
private void |
updateExports(java.util.List<Node> exports) |
private void |
updateRequire(Node call) |
private void |
updateRequires(java.util.List<Node> requires) |
private void |
updateRootShadows(Scope s,
com.google.common.collect.ImmutableSet<java.lang.String> roots) |
void |
visit(NodeTraversal t,
Node n,
Node parent)
Rewrite:
goog.module('foo')
var bar = goog.require('bar');
exports = something;
to:
goog.provide('foo');
goog.require('ns.bar');
goog.scope(function() {
var bar = ns.bar;
foo = something;
});
|
static final DiagnosticType INVALID_MODULE_IDENTIFIER
static final DiagnosticType INVALID_REQUIRE_IDENTIFIER
static final DiagnosticType INVALID_GET_IDENTIFIER
static final DiagnosticType INVALID_GET_CALL_SCOPE
static final DiagnosticType INVALID_GET_ALIAS
static final DiagnosticType INVALID_EXPORT_COMPUTED_PROPERTY
static final DiagnosticType USELESS_USE_STRICT_DIRECTIVE
private static final com.google.common.collect.ImmutableSet<java.lang.String> USE_STRICT_ONLY
private final AbstractCompiler compiler
private ClosureRewriteModule.ModuleDescription current
ClosureRewriteModule(AbstractCompiler compiler)
public void process(Node externs, Node root)
CompilerPass
process
in interface CompilerPass
externs
- Top of external JS treeroot
- Top of JS treepublic void hotSwapScript(Node scriptRoot, Node originalRoot)
HotSwapCompilerPass
hotSwapScript
in interface HotSwapCompilerPass
scriptRoot
- Root node corresponding to the file that is modified,
should be of type Token.SCRIPT
.originalRoot
- Root node corresponding to the original version of the
file that is modified. Should be of type token.SCRIPT
.public boolean shouldTraverse(NodeTraversal t, Node n, Node parent)
NodeTraversal.Callback
Visits a node in pre order (before visiting its children) and decides
whether this node's children should be traversed. If children are
traversed, they will be visited by
NodeTraversal.Callback.visit(NodeTraversal, Node, Node)
in postorder.
Implementations can have side effects (e.g. modifying the parse tree).
shouldTraverse
in interface NodeTraversal.Callback
private static void checkStrictModeDirective(NodeTraversal t, Node n)
private static boolean isCallTo(Node n, java.lang.String qname)
private static boolean isLoadModuleCall(Node n)
private static boolean isGetModuleCall(Node n)
private void rewriteGetModuleCall(NodeTraversal t, Node n)
private void rewriteGetModuleCallAlias(NodeTraversal t, Node n)
private void enterModule(Node n)
private boolean inModule()
private static boolean isGetModuleCallAlias(Node n)
public void visit(NodeTraversal t, Node n, Node parent)
visit
in interface NodeTraversal.Callback
private void maybeUpdateExportObjectDecl(NodeTraversal t, Node n)
private void maybeUpdateExportDeclToNode(NodeTraversal t, Node target, Node value)
private static boolean isExportPropAssign(Node n)
private static boolean isAssignTarget(Node n)
private void recordAndUpdateModule(NodeTraversal t, Node call)
private void recordRequire(NodeTraversal t, Node call)
private void updateRequires(java.util.List<Node> requires)
private void updateRequire(Node call)
private java.util.List<java.lang.String> collectRoots(ClosureRewriteModule.ModuleDescription module)
private java.lang.String getRootName(Node n)
private void rewriteModuleAsScope(Node root)
private void updateExports(java.util.List<Node> exports)
private void updateRootShadows(Scope s, com.google.common.collect.ImmutableSet<java.lang.String> roots)
private Node getModuleStatementRootForLoadModuleCall(Node n)
private boolean isHeaderNode(Node n)