class AngularPass extends NodeTraversal.AbstractPostOrderCallback implements HotSwapCompilerPass
$inject
\
properties for functions (class constructors, wrappers, etc) annotated withFor example, the following code:
/** @ngInject * /
function Controller(dependency1, dependency2) {
// do something
}
will be transformed into:
function Controller(dependency1, dependency2) {
// do something
}
Controller.$inject = ['dependency1', 'dependency2'];
This pass also supports assignments of function expressions to variables
like:
/** @ngInject * /
var filter = function(a, b) {};
var ns = {};
/** @ngInject * /
ns.method = function(a,b,c) {};
/** @ngInject * /
var shorthand = ns.method2 = function(a,b,c,) {}
Modifier and Type | Class and Description |
---|---|
(package private) static class |
AngularPass.NodeContext |
Modifier and Type | Field and Description |
---|---|
(package private) AbstractCompiler |
compiler |
(package private) static DiagnosticType |
INJECT_IN_NON_GLOBAL_OR_BLOCK_ERROR |
(package private) static DiagnosticType |
INJECT_NON_FUNCTION_ERROR |
static java.lang.String |
INJECT_PROPERTY_NAME |
private java.util.List<AngularPass.NodeContext> |
injectables
Nodes annotated with @ngInject
|
(package private) static DiagnosticType |
INJECTED_FUNCTION_HAS_DEFAULT_VALUE |
(package private) static DiagnosticType |
INJECTED_FUNCTION_HAS_DESTRUCTURED_PARAM |
Constructor and Description |
---|
AngularPass(AbstractCompiler compiler) |
Modifier and Type | Method and Description |
---|---|
private void |
addNode(Node n,
NodeTraversal t)
Add node to the list of injectables.
|
private java.util.List<Node> |
createDependenciesList(Node n)
Given a FUNCTION node returns array of STRING nodes representing function
parameters.
|
private java.util.List<Node> |
createStringsFromParamList(Node params)
Given a PARAM_LIST node creates an array of corresponding STRING nodes.
|
private static Node |
getDeclarationRValue(Node n)
Given a VAR node (variable declaration) returns the node of initial value.
|
void |
hotSwapScript(Node scriptRoot,
Node originalRoot)
Process the JS with root node root.
|
void |
process(Node externs,
Node root)
Process the JS with root node root.
|
void |
visit(NodeTraversal t,
Node n,
Node parent)
Visits a node in postorder (after its children have been visited).
|
shouldTraverse
final AbstractCompiler compiler
private final java.util.List<AngularPass.NodeContext> injectables
public static final java.lang.String INJECT_PROPERTY_NAME
static final DiagnosticType INJECT_IN_NON_GLOBAL_OR_BLOCK_ERROR
static final DiagnosticType INJECT_NON_FUNCTION_ERROR
static final DiagnosticType INJECTED_FUNCTION_HAS_DESTRUCTURED_PARAM
static final DiagnosticType INJECTED_FUNCTION_HAS_DEFAULT_VALUE
public AngularPass(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
.private java.util.List<Node> createDependenciesList(Node n)
n
- the FUNCTION node.private java.util.List<Node> createStringsFromParamList(Node params)
params
- PARAM_LIST node.public void visit(NodeTraversal t, Node n, Node parent)
NodeTraversal.Callback
Visits a node in postorder (after its children have been visited).
A node is visited only if all its parents should be traversed
(NodeTraversal.Callback.shouldTraverse(NodeTraversal, Node, Node)
).
Implementations can have side effects (e.g. modifying the parse tree).
visit
in interface NodeTraversal.Callback
private void addNode(Node n, NodeTraversal t)
n
- node to add.t
- node traversal instance.private static Node getDeclarationRValue(Node n)
var x; // null
var y = "value"; // STRING "value" node
var z = x = y = function() {}; // FUNCTION node
n
- VAR node.