Skip to content

Commit

Permalink
Clean up of ES6 versions of compute escaped and live variable analysis.
Browse files Browse the repository at this point in the history
Both of them only take in a function scope.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=161822773
  • Loading branch information
lillymliu authored and Tyler Breisacher committed Jul 13, 2017
1 parent 046174e commit 0d2a706
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 36 deletions.
29 changes: 13 additions & 16 deletions src/com/google/javascript/jscomp/DataFlowAnalysis.java
Expand Up @@ -16,6 +16,7 @@


package com.google.javascript.jscomp; 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.checkNotNull;
import static com.google.common.base.Preconditions.checkState; import static com.google.common.base.Preconditions.checkState;


Expand Down Expand Up @@ -593,44 +594,40 @@ public void visit(NodeTraversal t, Node n, Node parent) {
} }


/** /**
* Alternate implementation of compute escaped that accepts the child scope and the current * Alternate implementation of compute escaped for ES6.
* jsScope to help us access both the function and function body scopes. *
* It should only be run when the jsScope is a function scope.
* *
* The definition of escaped are * The definition of escaped are
* 1. Exported variables as they can be needed after the script terminates. * 1. Exported variables as they can be needed after the script terminates.
* 2. Names of named functions because in JavaScript, <i>function foo(){}</i> does not kill * 2. Names of named functions because in JavaScript, function foo(){} does not kill
* <i>foo</i> in the dataflow. * 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 jsScope,
final Scope jsScopeChild,
final Set<Var> escaped, final Set<Var> escaped,
AbstractCompiler compiler, AbstractCompiler compiler,
Es6SyntacticScopeCreator scopeCreator) { Es6SyntacticScopeCreator scopeCreator) {


checkArgument(jsScope.isFunctionScope());

AbstractPostOrderCallback finder = AbstractPostOrderCallback finder =
new AbstractPostOrderCallback() { new AbstractPostOrderCallback() {
@Override @Override
public void visit(NodeTraversal t, Node n, Node parent) { public void visit(NodeTraversal t, Node n, Node parent) {


Node enclosingBlock = NodeUtil.getEnclosingScopeRoot(n); Node enclosingBlock = NodeUtil.getEnclosingFunction(n);
if (jsScope.isFunctionScope()) {
enclosingBlock = NodeUtil.getEnclosingFunction(n);
}
if (jsScope.getRootNode() == enclosingBlock || !n.isName() || parent.isFunction()) { if (jsScope.getRootNode() == enclosingBlock || !n.isName() || parent.isFunction()) {
return; return;
} }


String name = n.getString(); String name = n.getString();
Var var = t.getScope().getVar(name); Var var = t.getScope().getVar(name);
if (var != null) { if (var != null) {
Node enclosingScopeNode = NodeUtil.getEnclosingScopeRoot(var.getNode()); Node enclosingScopeNode = NodeUtil.getEnclosingFunction(var.getNode());
if (jsScope.isFunctionScope()) {
enclosingScopeNode = NodeUtil.getEnclosingFunction(var.getNode());
}
if (enclosingScopeNode == jsScope.getRootNode()) { if (enclosingScopeNode == jsScope.getRootNode()) {
escaped.add(var); escaped.add(var);
} }
Expand Down
24 changes: 6 additions & 18 deletions src/com/google/javascript/jscomp/LiveVariablesAnalysisEs6.java
Expand Up @@ -144,7 +144,7 @@ public int hashCode() {
this.escaped = new HashSet<>(); this.escaped = new HashSet<>();
this.scopeVariables = new HashMap<>(); this.scopeVariables = new HashMap<>();
this.allVarsInFn = new HashMap<>(); this.allVarsInFn = new HashMap<>();
computeEscaped(jsScope, jsScopeChild, escaped, compiler, scopeCreator); computeEscapedEs6(jsScope, escaped, compiler, scopeCreator);
NodeUtil.getAllVarsDeclaredInFunction(allVarsInFn, compiler, scopeCreator, jsScope); NodeUtil.getAllVarsDeclaredInFunction(allVarsInFn, compiler, scopeCreator, jsScope);
addScopeVariables(); addScopeVariables();
} }
Expand Down Expand Up @@ -329,7 +329,7 @@ private void addToSetIfLocal(Node node, BitSet set) {
// to the function body. // to the function body.
if (localScope.isFunctionBlockScope()) { if (localScope.isFunctionBlockScope()) {
local = localScope.isDeclaredInFunctionBlockOrParameter(name); local = localScope.isDeclaredInFunctionBlockOrParameter(name);
} else if (jsScopeChild != null && localScope == jsScope) { } else if (localScope == jsScope) {
local = jsScopeChild.isDeclaredInFunctionBlockOrParameter(name); local = jsScopeChild.isDeclaredInFunctionBlockOrParameter(name);
} else { } else {
local = localScope.isDeclared(name, false); local = localScope.isDeclared(name, false);
Expand All @@ -350,27 +350,15 @@ private void addToSetIfLocal(Node node, BitSet set) {
* escaped set. * escaped set.
*/ */
void markAllParametersEscaped() { void markAllParametersEscaped() {
if (jsScope.isFunctionScope()) { Node paramList = NodeUtil.getFunctionParameters(jsScope.getRootNode());
Node paramList = NodeUtil.getFunctionParameters(jsScope.getRootNode()); for (Node arg = paramList.getFirstChild(); arg != null; arg = arg.getNext()) {
for (Node arg = paramList.getFirstChild(); arg != null; arg = arg.getNext()) { escaped.add(jsScope.getVar(arg.getString()));
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()));
}
} }
} }


private boolean isArgumentsName(Node n) { private boolean isArgumentsName(Node n) {
boolean childDeclared; boolean childDeclared;
if (jsScopeChild != null) { childDeclared = jsScopeChild.isDeclared(ARGUMENT_ARRAY_ALIAS, false);
childDeclared = jsScopeChild.isDeclared(ARGUMENT_ARRAY_ALIAS, false);
} else {
childDeclared = true;
}
return n.isName() return n.isName()
&& n.getString().equals(ARGUMENT_ARRAY_ALIAS) && n.getString().equals(ARGUMENT_ARRAY_ALIAS)
&& (!jsScope.isDeclared(ARGUMENT_ARRAY_ALIAS, false) || !childDeclared); && (!jsScope.isDeclared(ARGUMENT_ARRAY_ALIAS, false) || !childDeclared);
Expand Down
Expand Up @@ -61,7 +61,7 @@ class MaybeReachingVariableUse extends


// TODO(user): Maybe compute it somewhere else and re-use the escape // TODO(user): Maybe compute it somewhere else and re-use the escape
// local set here. // local set here.
computeEscaped(jsScope.getParent(), jsScope, escaped, compiler, scopeCreator); computeEscapedEs6(jsScope.getParent(), escaped, compiler, scopeCreator);
NodeUtil.getAllVarsDeclaredInFunction(allVarsInFn, compiler, scopeCreator, jsScope.getParent()); NodeUtil.getAllVarsDeclaredInFunction(allVarsInFn, compiler, scopeCreator, jsScope.getParent());
} }


Expand Down
Expand Up @@ -56,7 +56,7 @@ final class MustBeReachingVariableDef extends
this.compiler = compiler; this.compiler = compiler;
this.escaped = new HashSet<>(); this.escaped = new HashSet<>();
this.allVarsInFn = new HashMap<>(); this.allVarsInFn = new HashMap<>();
computeEscaped(jsScope.getParent(), jsScope, escaped, compiler, scopeCreator); computeEscapedEs6(jsScope.getParent(), escaped, compiler, scopeCreator);
NodeUtil.getAllVarsDeclaredInFunction( NodeUtil.getAllVarsDeclaredInFunction(
allVarsInFn, compiler, scopeCreator, jsScope.getParent()); allVarsInFn, compiler, scopeCreator, jsScope.getParent());
} }
Expand Down

0 comments on commit 0d2a706

Please sign in to comment.