Skip to content

Commit

Permalink
Small fix in NodeUtil.getRValueOfLValue:
Browse files Browse the repository at this point in the history
In ES5 a var statement looks like

VAR
  NAME
    rhs

so the RHS is the first (and only) child of the LHS. But in ES6 it can also be

VAR
  DESTRUCTURING_LHS
    OBJECT_PATTERN or ARRAY_PATTERN
    rhs

in which case the RHS is the last child of the LHS

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=177348132
  • Loading branch information
tbreisacher authored and brad4d committed Nov 30, 2017
1 parent 2628003 commit 96437eb
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 3 deletions.
3 changes: 3 additions & 0 deletions src/com/google/javascript/jscomp/NodeUtil.java
Expand Up @@ -5108,6 +5108,9 @@ static Node getRValueOfLValue(Node n) {
case VAR:
case LET:
case CONST:
return n.getLastChild();
case DESTRUCTURING_LHS:
return parent.getLastChild();
case OBJECTLIT:
return n.getFirstChild();
case FUNCTION:
Expand Down
67 changes: 64 additions & 3 deletions test/com/google/javascript/jscomp/NodeUtilTest.java
Expand Up @@ -43,6 +43,7 @@
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.annotation.Nullable;
import junit.framework.TestCase;

/**
Expand Down Expand Up @@ -2288,10 +2289,10 @@ public void testGetRValueOfLValue() {
}

/**
* When the left side is a destructuring pattern, generally it's not possible to identify the
* RHS for a specific name on the LHS.
* When the left side is a destructuring pattern, generally it's not possible to identify the RHS
* for a specific name on the LHS.
*/
public void testGetRValueOfLValueDestructuring() {
public void testGetRValueOfLValueInDestructuringPattern() {
assertThat(NodeUtil.getRValueOfLValue(getNameNode(parse("var [x] = rhs;"), "x"))).isNull();
assertThat(NodeUtil.getRValueOfLValue(getNameNode(parse("var [x, y] = rhs;"), "x"))).isNull();
assertThat(NodeUtil.getRValueOfLValue(getNameNode(parse("var [y, x] = rhs;"), "x"))).isNull();
Expand All @@ -2307,6 +2308,36 @@ public void testGetRValueOfLValueDestructuring() {
assertThat(NodeUtil.getRValueOfLValue(x)).isNull();
}

public void testGetRValueOfLValueDestructuringPattern() {
assertNode(NodeUtil.getRValueOfLValue(getPattern(parse("var [x] = 'rhs';"))))
.hasType(Token.STRING);
assertNode(NodeUtil.getRValueOfLValue(getPattern(parse("var [x, y] = 'rhs';"))))
.hasType(Token.STRING);
assertNode(NodeUtil.getRValueOfLValue(getPattern(parse("var [y, x] = 'rhs';"))))
.hasType(Token.STRING);
assertNode(NodeUtil.getRValueOfLValue(getPattern(parse("var {x: x} = 'rhs';"))))
.hasType(Token.STRING);
assertNode(NodeUtil.getRValueOfLValue(getPattern(parse("var {y: x} = 'rhs';"))))
.hasType(Token.STRING);
assertNode(NodeUtil.getRValueOfLValue(getPattern(parse("var {x} = 'rhs';"))))
.hasType(Token.STRING);
}

public void testGetRValueOfLValueDestructuringLhs() {
assertNode(NodeUtil.getRValueOfLValue(getDestructuringLhs(parse("var [x] = 'rhs';"))))
.hasType(Token.STRING);
assertNode(NodeUtil.getRValueOfLValue(getDestructuringLhs(parse("var [x, y] = 'rhs';"))))
.hasType(Token.STRING);
assertNode(NodeUtil.getRValueOfLValue(getDestructuringLhs(parse("var [y, x] = 'rhs';"))))
.hasType(Token.STRING);
assertNode(NodeUtil.getRValueOfLValue(getDestructuringLhs(parse("var {x: x} = 'rhs';"))))
.hasType(Token.STRING);
assertNode(NodeUtil.getRValueOfLValue(getDestructuringLhs(parse("var {y: x} = 'rhs';"))))
.hasType(Token.STRING);
assertNode(NodeUtil.getRValueOfLValue(getDestructuringLhs(parse("var {x} = 'rhs';"))))
.hasType(Token.STRING);
}

public void testIsNaN() {
assertTrue(NodeUtil.isNaN(getNode("NaN")));
assertFalse(NodeUtil.isNaN(getNode("Infinity")));
Expand Down Expand Up @@ -3207,6 +3238,36 @@ private static Node getNameNode(Node n, String name) {
return null;
}

/** @return The first node in {@code tree} that is an array pattern or object pattern. */
@Nullable
private static Node getPattern(Node tree) {
if (tree.isDestructuringPattern()) {
return tree;
}
for (Node c : tree.children()) {
Node result = getPattern(c);
if (result != null) {
return result;
}
}
return null;
}

/** @return The first node in {@code tree} that is a DESTRUCTURING_LHS. */
@Nullable
private static Node getDestructuringLhs(Node tree) {
if (tree.isDestructuringLhs()) {
return tree;
}
for (Node c : tree.children()) {
Node result = getDestructuringLhs(c);
if (result != null) {
return result;
}
}
return null;
}

private static boolean isValidPropertyName(String s) {
return NodeUtil.isValidPropertyName(FeatureSet.ES3, s);
}
Expand Down

0 comments on commit 96437eb

Please sign in to comment.