Skip to content

Commit

Permalink
Add NodeUtil.isShallowStatementTree method
Browse files Browse the repository at this point in the history
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
  • Loading branch information
lauraharker authored and copybara-github committed Jan 13, 2025
1 parent 375f9fd commit d7d67d1
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,12 @@
import java.util.Set;

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

private static final String WINDOW_NAME = "window";

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

@Override
public boolean shouldTraverse(NodeTraversal nodeTraversal, Node n, Node parent) {
return parent == null
|| NodeUtil.isControlStructure(parent)
|| NodeUtil.isStatementBlock(parent);
}

@Override
public void visit(NodeTraversal t, Node n, Node parent) {
if (n.isFunction()) {
Expand Down
8 changes: 2 additions & 6 deletions src/com/google/javascript/jscomp/FindModuleDependencies.java
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,7 @@ public boolean shouldTraverse(NodeTraversal nodeTraversal, Node n, Node parent)
return false;
}
// Shallow traversal, since we don't need to inspect within functions or expressions.
if (parent == null
|| NodeUtil.isControlStructure(parent)
|| NodeUtil.isStatementBlock(parent)) {
if (NodeUtil.isShallowStatementTree(parent)) {
if (n.isExprResult()) {
Node maybeGetProp = n.getFirstFirstChild();
if (maybeGetProp != null
Expand Down Expand Up @@ -144,9 +142,7 @@ public boolean shouldTraverse(NodeTraversal t, Node n, Node parent) {
@Override
public void visit(NodeTraversal t, Node n, Node parent) {
ModuleLoader.ResolutionMode resolutionMode = compiler.getOptions().moduleResolutionMode;
if (parent == null
|| NodeUtil.isControlStructure(parent)
|| NodeUtil.isStatementBlock(parent)) {
if (NodeUtil.isShallowStatementTree(parent)) {
if (n.isExprResult()) {
Node maybeGetProp = n.getFirstFirstChild();
if (maybeGetProp != null
Expand Down
4 changes: 1 addition & 3 deletions src/com/google/javascript/jscomp/NodeTraversal.java
Original file line number Diff line number Diff line change
Expand Up @@ -264,9 +264,7 @@ public final boolean shouldTraverse(NodeTraversal nodeTraversal, Node n, Node pa
public abstract static class AbstractShallowStatementCallback implements Callback {
@Override
public final boolean shouldTraverse(NodeTraversal nodeTraversal, Node n, Node parent) {
return parent == null
|| NodeUtil.isControlStructure(parent)
|| NodeUtil.isStatementBlock(parent);
return NodeUtil.isShallowStatementTree(parent);
}
}

Expand Down
15 changes: 11 additions & 4 deletions src/com/google/javascript/jscomp/NodeUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -4526,15 +4526,22 @@ public boolean apply(Node n) {
static final Predicate<Node> MATCH_ANYTHING_BUT_NON_ARROW_FUNCTION =
n -> !NodeUtil.isNonArrowFunction(n);

/**
* Whether the given node's subtree may contain statements, excepting nested functions
*
* <p>This is useful for traversing all statements in a given script or function without
* traversing into nested functions, so it will return true for e.g. a BLOCK or SWITCH statement.
*/
public static boolean isShallowStatementTree(Node n) {
return n == null || NodeUtil.isControlStructure(n) || NodeUtil.isStatementBlock(n);
}

/** A predicate for matching statements without exiting the current scope. */
static class MatchShallowStatement implements Predicate<Node> {
@Override
public boolean apply(Node n) {
Node parent = n.getParent();
return n.isRoot()
|| n.isBlock()
|| (!n.isFunction()
&& (parent == null || isControlStructure(parent) || isStatementBlock(parent)));
return n.isRoot() || n.isBlock() || (!n.isFunction() && isShallowStatementTree(parent));
}
}

Expand Down
4 changes: 1 addition & 3 deletions src/com/google/javascript/jscomp/ProcessCommonJSModules.java
Original file line number Diff line number Diff line change
Expand Up @@ -601,9 +601,7 @@ public boolean shouldTraverse(NodeTraversal nodeTraversal, Node n, Node parent)
@Override
public void visit(NodeTraversal t, Node n, Node parent) {
// Check for goog.provide or goog.module statements
if (parent == null
|| NodeUtil.isControlStructure(parent)
|| NodeUtil.isStatementBlock(parent)) {
if (NodeUtil.isShallowStatementTree(parent)) {
if (n.isExprResult()) {
Node maybeGetProp = n.getFirstFirstChild();
if (maybeGetProp != null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ private void scanVars(Node n, @Nullable Scope hoistScope, @Nullable Scope blockS

// Variables can only occur in statement-level nodes, so
// we only need to traverse children in a couple special cases.
if (NodeUtil.isControlStructure(n) || NodeUtil.isStatementBlock(n)) {
if (NodeUtil.isShallowStatementTree(n)) {
for (Node child = n.getFirstChild(); child != null;) {
Node next = child.getNext();
scanVars(child, hoistScope, enteringNewBlock ? null : blockScope);
Expand Down
4 changes: 1 addition & 3 deletions src/com/google/javascript/jscomp/lint/CheckJSDocStyle.java
Original file line number Diff line number Diff line change
Expand Up @@ -438,9 +438,7 @@ public boolean shouldTraverse(NodeTraversal t, Node n, Node parent) {
}

// Shallow traversal, since we don't need to inspect within functions or expressions.
if (parent == null
|| NodeUtil.isControlStructure(parent)
|| NodeUtil.isStatementBlock(parent)) {
if (NodeUtil.isShallowStatementTree(parent)) {
if (n.isReturn() && n.hasChildren()) {
found = true;
return false;
Expand Down

0 comments on commit d7d67d1

Please sign in to comment.