Skip to content

Commit d7d67d1

Browse files
lauraharkercopybara-github
authored andcommitted
Add NodeUtil.isShallowStatementTree method
This replaces existing calls to "n == null || NodeUtil.isStatementBlock(n) || NodeUtil.isControlStructure(n)". It's more useful in the child CL which adds SWITCH_BODY to this method, so that we don't have to individually refactor all the callers. PiperOrigin-RevId: 715001274
1 parent 375f9fd commit d7d67d1

7 files changed

+22
-31
lines changed

src/com/google/javascript/jscomp/DeclaredGlobalExternsOnWindow.java

+5-11
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,12 @@
2424
import java.util.Set;
2525

2626
/**
27-
* A compiler pass to normalize externs by declaring global names on
28-
* the "window" object, if it is declared in externs.
29-
* The new declarations are added to the window instance, not to Window.prototype.
27+
* A compiler pass to normalize externs by declaring global names on the "window" object, if it is
28+
* declared in externs. The new declarations are added to the window instance, not to
29+
* Window.prototype.
3030
*/
31-
class DeclaredGlobalExternsOnWindow implements CompilerPass, NodeTraversal.Callback {
31+
class DeclaredGlobalExternsOnWindow extends NodeTraversal.AbstractShallowStatementCallback
32+
implements CompilerPass {
3233

3334
private static final String WINDOW_NAME = "window";
3435

@@ -111,13 +112,6 @@ private static void addExtern(Node node, boolean defineOnWindow) {
111112
node.getGrandparent().addChildToBack(IR.exprResult(newNode));
112113
}
113114

114-
@Override
115-
public boolean shouldTraverse(NodeTraversal nodeTraversal, Node n, Node parent) {
116-
return parent == null
117-
|| NodeUtil.isControlStructure(parent)
118-
|| NodeUtil.isStatementBlock(parent);
119-
}
120-
121115
@Override
122116
public void visit(NodeTraversal t, Node n, Node parent) {
123117
if (n.isFunction()) {

src/com/google/javascript/jscomp/FindModuleDependencies.java

+2-6
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,7 @@ public boolean shouldTraverse(NodeTraversal nodeTraversal, Node n, Node parent)
7979
return false;
8080
}
8181
// Shallow traversal, since we don't need to inspect within functions or expressions.
82-
if (parent == null
83-
|| NodeUtil.isControlStructure(parent)
84-
|| NodeUtil.isStatementBlock(parent)) {
82+
if (NodeUtil.isShallowStatementTree(parent)) {
8583
if (n.isExprResult()) {
8684
Node maybeGetProp = n.getFirstFirstChild();
8785
if (maybeGetProp != null
@@ -144,9 +142,7 @@ public boolean shouldTraverse(NodeTraversal t, Node n, Node parent) {
144142
@Override
145143
public void visit(NodeTraversal t, Node n, Node parent) {
146144
ModuleLoader.ResolutionMode resolutionMode = compiler.getOptions().moduleResolutionMode;
147-
if (parent == null
148-
|| NodeUtil.isControlStructure(parent)
149-
|| NodeUtil.isStatementBlock(parent)) {
145+
if (NodeUtil.isShallowStatementTree(parent)) {
150146
if (n.isExprResult()) {
151147
Node maybeGetProp = n.getFirstFirstChild();
152148
if (maybeGetProp != null

src/com/google/javascript/jscomp/NodeTraversal.java

+1-3
Original file line numberDiff line numberDiff line change
@@ -264,9 +264,7 @@ public final boolean shouldTraverse(NodeTraversal nodeTraversal, Node n, Node pa
264264
public abstract static class AbstractShallowStatementCallback implements Callback {
265265
@Override
266266
public final boolean shouldTraverse(NodeTraversal nodeTraversal, Node n, Node parent) {
267-
return parent == null
268-
|| NodeUtil.isControlStructure(parent)
269-
|| NodeUtil.isStatementBlock(parent);
267+
return NodeUtil.isShallowStatementTree(parent);
270268
}
271269
}
272270

src/com/google/javascript/jscomp/NodeUtil.java

+11-4
Original file line numberDiff line numberDiff line change
@@ -4526,15 +4526,22 @@ public boolean apply(Node n) {
45264526
static final Predicate<Node> MATCH_ANYTHING_BUT_NON_ARROW_FUNCTION =
45274527
n -> !NodeUtil.isNonArrowFunction(n);
45284528

4529+
/**
4530+
* Whether the given node's subtree may contain statements, excepting nested functions
4531+
*
4532+
* <p>This is useful for traversing all statements in a given script or function without
4533+
* traversing into nested functions, so it will return true for e.g. a BLOCK or SWITCH statement.
4534+
*/
4535+
public static boolean isShallowStatementTree(Node n) {
4536+
return n == null || NodeUtil.isControlStructure(n) || NodeUtil.isStatementBlock(n);
4537+
}
4538+
45294539
/** A predicate for matching statements without exiting the current scope. */
45304540
static class MatchShallowStatement implements Predicate<Node> {
45314541
@Override
45324542
public boolean apply(Node n) {
45334543
Node parent = n.getParent();
4534-
return n.isRoot()
4535-
|| n.isBlock()
4536-
|| (!n.isFunction()
4537-
&& (parent == null || isControlStructure(parent) || isStatementBlock(parent)));
4544+
return n.isRoot() || n.isBlock() || (!n.isFunction() && isShallowStatementTree(parent));
45384545
}
45394546
}
45404547

src/com/google/javascript/jscomp/ProcessCommonJSModules.java

+1-3
Original file line numberDiff line numberDiff line change
@@ -601,9 +601,7 @@ public boolean shouldTraverse(NodeTraversal nodeTraversal, Node n, Node parent)
601601
@Override
602602
public void visit(NodeTraversal t, Node n, Node parent) {
603603
// Check for goog.provide or goog.module statements
604-
if (parent == null
605-
|| NodeUtil.isControlStructure(parent)
606-
|| NodeUtil.isStatementBlock(parent)) {
604+
if (NodeUtil.isShallowStatementTree(parent)) {
607605
if (n.isExprResult()) {
608606
Node maybeGetProp = n.getFirstFirstChild();
609607
if (maybeGetProp != null

src/com/google/javascript/jscomp/SyntacticScopeCreator.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,7 @@ private void scanVars(Node n, @Nullable Scope hoistScope, @Nullable Scope blockS
309309

310310
// Variables can only occur in statement-level nodes, so
311311
// we only need to traverse children in a couple special cases.
312-
if (NodeUtil.isControlStructure(n) || NodeUtil.isStatementBlock(n)) {
312+
if (NodeUtil.isShallowStatementTree(n)) {
313313
for (Node child = n.getFirstChild(); child != null;) {
314314
Node next = child.getNext();
315315
scanVars(child, hoistScope, enteringNewBlock ? null : blockScope);

src/com/google/javascript/jscomp/lint/CheckJSDocStyle.java

+1-3
Original file line numberDiff line numberDiff line change
@@ -438,9 +438,7 @@ public boolean shouldTraverse(NodeTraversal t, Node n, Node parent) {
438438
}
439439

440440
// Shallow traversal, since we don't need to inspect within functions or expressions.
441-
if (parent == null
442-
|| NodeUtil.isControlStructure(parent)
443-
|| NodeUtil.isStatementBlock(parent)) {
441+
if (NodeUtil.isShallowStatementTree(parent)) {
444442
if (n.isReturn() && n.hasChildren()) {
445443
found = true;
446444
return false;

0 commit comments

Comments
 (0)