Skip to content

Commit

Permalink
Change isDeclaredSloppy to be more specific to only be when you are i…
Browse files Browse the repository at this point in the history
…n a function block scope and want to know if its declared either in the function block or parameter.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=160653566
  • Loading branch information
lillymliu authored and brad4d committed Jul 5, 2017
1 parent 362d8fe commit 40967b4
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 27 deletions.
Expand Up @@ -236,7 +236,7 @@ private void tryRemoveAssignment(NodeTraversal t, Node n, Node exprRoot,
String name = lhs.getString();
checkState(t.getScope().isFunctionBlockScope());
Scope functionBlockScope = t.getScope();
if (!functionBlockScope.isDeclaredSloppy(name, false)) {
if (!functionBlockScope.isDeclaredInFunctionBlockOrParameter(name)) {
return;
}
Var var = functionBlockScope.getVar(name);
Expand Down
2 changes: 1 addition & 1 deletion src/com/google/javascript/jscomp/Es6RenameReferences.java
Expand Up @@ -64,7 +64,7 @@ private void renameReference(NodeTraversal t, Node n) {
n.setString(newName);
t.reportCodeChange();
return;
} else if (current.isDeclaredSloppy(oldName, false)) {
} else if (current.isDeclared(oldName, false)) {
return;
} else {
current = current.getParent();
Expand Down
4 changes: 2 additions & 2 deletions src/com/google/javascript/jscomp/LiveVariablesAnalysis.java
Expand Up @@ -279,7 +279,7 @@ private void computeGenKill(Node n, BitSet gen, BitSet kill,
private void addToSetIfLocal(Node node, BitSet set) {
checkState(node.isName());
String name = node.getString();
if (!jsScope.isDeclaredSloppy(name, false)) {
if (!jsScope.isDeclared(name, false)) {
return;
}
Var var = jsScope.getVar(name);
Expand All @@ -301,6 +301,6 @@ void markAllParametersEscaped() {

private boolean isArgumentsName(Node n) {
return n.isName() && n.getString().equals(ARGUMENT_ARRAY_ALIAS)
&& !jsScope.isDeclaredSloppy(ARGUMENT_ARRAY_ALIAS, false);
&& !jsScope.isDeclared(ARGUMENT_ARRAY_ALIAS, false);
}
}
10 changes: 8 additions & 2 deletions src/com/google/javascript/jscomp/LiveVariablesAnalysisEs6.java
Expand Up @@ -334,10 +334,16 @@ private void computeGenKill(Node n, BitSet gen, BitSet kill, boolean conditional
private void addToSetIfLocal(Node node, BitSet set) {
checkState(node.isName(), node);
String name = node.getString();
boolean local;

// add to the local set if the variable is declared in the function or function body because
// ES6 separates the scope but hoists variables to the function scope
if (jsScope.isDeclaredSloppy(name, false)) {
if (jsScope.isFunctionBlockScope()) {
local = jsScope.isDeclaredInFunctionBlockOrParameter(name);
} else {
local = jsScope.isDeclared(name, false);
}
if (local) {
Var var = jsScope.getVar(name);
if (!escaped.contains(var)) {
set.set(getVarIndex(var.getName()));
Expand Down Expand Up @@ -383,6 +389,6 @@ private boolean isArgumentsName(Node n) {
}
return n.isName()
&& n.getString().equals(ARGUMENT_ARRAY_ALIAS)
&& (!jsScope.isDeclaredSloppy(ARGUMENT_ARRAY_ALIAS, false) || !childDeclared);
&& (!jsScope.isDeclared(ARGUMENT_ARRAY_ALIAS, false) || !childDeclared);
}
}
17 changes: 5 additions & 12 deletions src/com/google/javascript/jscomp/Scope.java
Expand Up @@ -188,22 +188,15 @@ public Var getArgumentsVar() {
}

/**
* @deprecated use #isDeclared instead
* Use only when in a function block scope and want to tell if a name is either at the top of the
* function block scope or the function parameter scope
*/
@Deprecated
public boolean isDeclaredSloppy(String name, boolean recurse) {
public boolean isDeclaredInFunctionBlockOrParameter(String name) {
// In ES6, we create a separate "function parameter scope" above the function block scope to
// handle default parameters. Since nothing in the function block scope is allowed to shadow
// the variables in the function scope, we treat the two scopes as one in this method.
checkState(recurse == false);
if (!isDeclared(name, false)) {
if (parent != null && isFunctionBlockScope()) {
return parent.isDeclared(name, false);
}
return false;
} else {
return true;
}
checkState(isFunctionBlockScope());
return isDeclared(name, false) || parent.isDeclared(name, false);
}

/**
Expand Down
9 changes: 3 additions & 6 deletions src/com/google/javascript/jscomp/TypedScope.java
Expand Up @@ -201,13 +201,10 @@ public Var getArgumentsVar() {
throw new IllegalStateException("Method getArgumentsVar cannot be called on typed scopes.");
}

/**
* @deprecated use #isDeclared instead
*/
@Deprecated
@Override
public boolean isDeclaredSloppy(String name, boolean recurse) {
return isDeclared(name, false);
public boolean isDeclaredInFunctionBlockOrParameter(String name) {
throw new IllegalStateException(
"Method isDeclaredInFunctionBlockOrParameter cannot be called on typed scopes.");
}

@Override
Expand Down
Expand Up @@ -557,7 +557,7 @@ public void testFunctionArgument() {
Scope fBlockScope = scopeCreator.createScope(functionBlock, functionScope);

assertFalse(fBlockScope.isDeclared("x", false));
assertTrue(fBlockScope.isDeclaredSloppy("x", false));
assertTrue(fBlockScope.isDeclaredInFunctionBlockOrParameter("x"));
assertFalse(fBlockScope.isDeclared("y", false));

Node ifBlock = functionBlock.getLastChild().getLastChild();
Expand Down Expand Up @@ -699,7 +699,7 @@ public void testSimpleFunctionParam() {
Node fBlock = NodeUtil.getFunctionBody(fNode);
Scope fBlockScope = scopeCreator.createScope(fBlock, fScope);
assertFalse(fBlockScope.isDeclared("x", false));
assertTrue(fBlockScope.isDeclaredSloppy("x", false));
assertTrue(fBlockScope.isDeclaredInFunctionBlockOrParameter("x"));
}

public void testOnlyOneDeclaration() {
Expand All @@ -713,7 +713,7 @@ public void testOnlyOneDeclaration() {
Node fBlock = fNode.getLastChild();
Scope fBlockScope = scopeCreator.createScope(fBlock, fScope);
assertFalse(fBlockScope.isDeclared("x", false));
assertTrue(fBlockScope.isDeclaredSloppy("x", false));
assertTrue(fBlockScope.isDeclaredInFunctionBlockOrParameter("x"));

Node ifBlock = fBlock.getFirstChild().getLastChild();
Scope ifBlockScope = scopeCreator.createScope(ifBlock, fBlockScope);
Expand Down

0 comments on commit 40967b4

Please sign in to comment.