diff --git a/src/com/google/javascript/jscomp/DataFlowAnalysis.java b/src/com/google/javascript/jscomp/DataFlowAnalysis.java index 4255d183d29..c04d25454d4 100644 --- a/src/com/google/javascript/jscomp/DataFlowAnalysis.java +++ b/src/com/google/javascript/jscomp/DataFlowAnalysis.java @@ -16,6 +16,7 @@ package com.google.javascript.jscomp; +import static com.google.common.base.Preconditions.checkArgument; import static com.google.common.base.Preconditions.checkNotNull; import static com.google.common.base.Preconditions.checkState; @@ -593,33 +594,31 @@ public void visit(NodeTraversal t, Node n, Node parent) { } /** - * Alternate implementation of compute escaped that accepts the child scope and the current - * jsScope to help us access both the function and function body scopes. + * Alternate implementation of compute escaped for ES6. + * + * It should only be run when the jsScope is a function scope. * * The definition of escaped are * 1. Exported variables as they can be needed after the script terminates. - * 2. Names of named functions because in JavaScript, function foo(){} does not kill - * foo in the dataflow. + * 2. Names of named functions because in JavaScript, function foo(){} does not kill + * foo in the dataflow. + * * - * @param jsScopeChild If jsScope is a function scope, jsScopeChild is the scope for the body of - * that function. If not, jsScopeChild is null. */ - static void computeEscaped( + static void computeEscapedEs6( final Scope jsScope, - final Scope jsScopeChild, final Set escaped, AbstractCompiler compiler, Es6SyntacticScopeCreator scopeCreator) { + checkArgument(jsScope.isFunctionScope()); + AbstractPostOrderCallback finder = new AbstractPostOrderCallback() { @Override public void visit(NodeTraversal t, Node n, Node parent) { - Node enclosingBlock = NodeUtil.getEnclosingScopeRoot(n); - if (jsScope.isFunctionScope()) { - enclosingBlock = NodeUtil.getEnclosingFunction(n); - } + Node enclosingBlock = NodeUtil.getEnclosingFunction(n); if (jsScope.getRootNode() == enclosingBlock || !n.isName() || parent.isFunction()) { return; } @@ -627,10 +626,8 @@ public void visit(NodeTraversal t, Node n, Node parent) { String name = n.getString(); Var var = t.getScope().getVar(name); if (var != null) { - Node enclosingScopeNode = NodeUtil.getEnclosingScopeRoot(var.getNode()); - if (jsScope.isFunctionScope()) { - enclosingScopeNode = NodeUtil.getEnclosingFunction(var.getNode()); - } + Node enclosingScopeNode = NodeUtil.getEnclosingFunction(var.getNode()); + if (enclosingScopeNode == jsScope.getRootNode()) { escaped.add(var); } diff --git a/src/com/google/javascript/jscomp/LiveVariablesAnalysisEs6.java b/src/com/google/javascript/jscomp/LiveVariablesAnalysisEs6.java index 1123202c769..bac35ae9d85 100644 --- a/src/com/google/javascript/jscomp/LiveVariablesAnalysisEs6.java +++ b/src/com/google/javascript/jscomp/LiveVariablesAnalysisEs6.java @@ -144,7 +144,7 @@ public int hashCode() { this.escaped = new HashSet<>(); this.scopeVariables = new HashMap<>(); this.allVarsInFn = new HashMap<>(); - computeEscaped(jsScope, jsScopeChild, escaped, compiler, scopeCreator); + computeEscapedEs6(jsScope, escaped, compiler, scopeCreator); NodeUtil.getAllVarsDeclaredInFunction(allVarsInFn, compiler, scopeCreator, jsScope); addScopeVariables(); } @@ -329,7 +329,7 @@ private void addToSetIfLocal(Node node, BitSet set) { // to the function body. if (localScope.isFunctionBlockScope()) { local = localScope.isDeclaredInFunctionBlockOrParameter(name); - } else if (jsScopeChild != null && localScope == jsScope) { + } else if (localScope == jsScope) { local = jsScopeChild.isDeclaredInFunctionBlockOrParameter(name); } else { local = localScope.isDeclared(name, false); @@ -350,27 +350,15 @@ private void addToSetIfLocal(Node node, BitSet set) { * escaped set. */ void markAllParametersEscaped() { - if (jsScope.isFunctionScope()) { - Node paramList = NodeUtil.getFunctionParameters(jsScope.getRootNode()); - for (Node arg = paramList.getFirstChild(); arg != null; arg = arg.getNext()) { - escaped.add(jsScope.getVar(arg.getString())); - } - } else { - Node enclosingFunction = NodeUtil.getEnclosingFunction(jsScope.getRootNode()); - Node paramList = NodeUtil.getFunctionParameters(enclosingFunction); - for (Node arg = paramList.getFirstChild(); arg != null; arg = arg.getNext()) { - escaped.add(jsScope.getVar(arg.getString())); - } + Node paramList = NodeUtil.getFunctionParameters(jsScope.getRootNode()); + for (Node arg = paramList.getFirstChild(); arg != null; arg = arg.getNext()) { + escaped.add(jsScope.getVar(arg.getString())); } } private boolean isArgumentsName(Node n) { boolean childDeclared; - if (jsScopeChild != null) { - childDeclared = jsScopeChild.isDeclared(ARGUMENT_ARRAY_ALIAS, false); - } else { - childDeclared = true; - } + childDeclared = jsScopeChild.isDeclared(ARGUMENT_ARRAY_ALIAS, false); return n.isName() && n.getString().equals(ARGUMENT_ARRAY_ALIAS) && (!jsScope.isDeclared(ARGUMENT_ARRAY_ALIAS, false) || !childDeclared); diff --git a/src/com/google/javascript/jscomp/MaybeReachingVariableUse.java b/src/com/google/javascript/jscomp/MaybeReachingVariableUse.java index db39a943666..950fb12601a 100644 --- a/src/com/google/javascript/jscomp/MaybeReachingVariableUse.java +++ b/src/com/google/javascript/jscomp/MaybeReachingVariableUse.java @@ -61,7 +61,7 @@ class MaybeReachingVariableUse extends // TODO(user): Maybe compute it somewhere else and re-use the escape // local set here. - computeEscaped(jsScope.getParent(), jsScope, escaped, compiler, scopeCreator); + computeEscapedEs6(jsScope.getParent(), escaped, compiler, scopeCreator); NodeUtil.getAllVarsDeclaredInFunction(allVarsInFn, compiler, scopeCreator, jsScope.getParent()); } diff --git a/src/com/google/javascript/jscomp/MustBeReachingVariableDef.java b/src/com/google/javascript/jscomp/MustBeReachingVariableDef.java index 79f8ab50499..eee7168b66f 100644 --- a/src/com/google/javascript/jscomp/MustBeReachingVariableDef.java +++ b/src/com/google/javascript/jscomp/MustBeReachingVariableDef.java @@ -56,7 +56,7 @@ final class MustBeReachingVariableDef extends this.compiler = compiler; this.escaped = new HashSet<>(); this.allVarsInFn = new HashMap<>(); - computeEscaped(jsScope.getParent(), jsScope, escaped, compiler, scopeCreator); + computeEscapedEs6(jsScope.getParent(), escaped, compiler, scopeCreator); NodeUtil.getAllVarsDeclaredInFunction( allVarsInFn, compiler, scopeCreator, jsScope.getParent()); }