final class CheckGlobalThis extends java.lang.Object implements NodeTraversal.Callback
this
keyword that are considered
unsafe because they are likely to reference the global this
object
unintentionally.
A use of this
is considered unsafe if it's on the left side of an
assignment or a property access, and not inside one of the following:
@constructor
@this
.
this
annotation.
Note that this check does not track assignments of this
to
variables or objects. The code
function evil() { var a = this; a.useful = undefined; }will not get flagged, even though it is semantically equivalent to
function evil() { this.useful = undefined; }which would get flagged.
Modifier and Type | Field and Description |
---|---|
private Node |
assignLhsChild
If
assignLhsChild != null , then the node being traversed is
a descendant of the first child of an ASSIGN node. |
private AbstractCompiler |
compiler |
(package private) static DiagnosticType |
GLOBAL_THIS |
Constructor and Description |
---|
CheckGlobalThis(AbstractCompiler compiler) |
Modifier and Type | Method and Description |
---|---|
private boolean |
shouldReportThis(Node n) |
boolean |
shouldTraverse(NodeTraversal t,
Node n,
Node parent)
Since this pass reports errors only when a global
this keyword
is encountered, there is no reason to traverse non global contexts. |
void |
visit(NodeTraversal t,
Node n,
Node parent)
Visits a node in postorder (after its children have been visited).
|
static final DiagnosticType GLOBAL_THIS
private final AbstractCompiler compiler
private Node assignLhsChild
assignLhsChild != null
, then the node being traversed is
a descendant of the first child of an ASSIGN node. assignLhsChild's
parent is this ASSIGN node.CheckGlobalThis(AbstractCompiler compiler)
public boolean shouldTraverse(NodeTraversal t, Node n, Node parent)
this
keyword
is encountered, there is no reason to traverse non global contexts.shouldTraverse
in interface NodeTraversal.Callback
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 boolean shouldReportThis(Node n)