Skip to content

Commit

Permalink
Report an undefined variable error for "() => y" in VarCheck
Browse files Browse the repository at this point in the history
Previously VarCheck was allowing anything that was the child of a function expression to be an undeclared name, not just the function name. Now it correctly reports an error for "() => y" and adds the undeclared name "y" to the externs.

Fixes #2843

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=188941589
  • Loading branch information
lauraharker authored and blickly committed Mar 14, 2018
1 parent 4a6cbb1 commit 996b562
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/com/google/javascript/jscomp/VarCheck.java
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ public void hotSwapScript(Node scriptRoot, Node originalRoot) {

@Override
public void visit(NodeTraversal t, Node n, Node parent) {
if (n.isName() || (n.isStringKey() && !n.hasChildren())) {
if (n.isName()) {
String varName = n.getString();

// Only a function can have an empty name.
Expand Down Expand Up @@ -200,8 +200,8 @@ public void visit(NodeTraversal t, Node n, Node parent) {
Scope scope = t.getScope();
Var var = scope.getVar(varName);
if (var == null) {
if (NodeUtil.isFunctionExpression(parent)
|| (NodeUtil.isClassExpression(parent) && n == parent.getFirstChild())) {
if ((NodeUtil.isFunctionExpression(parent) || NodeUtil.isClassExpression(parent))
&& n == parent.getFirstChild()) {
// e.g. [ function foo() {} ], it's okay if "foo" isn't defined in the
// current scope.
} else if (NodeUtil.isNonlocalModuleExportName(n)) {
Expand Down
4 changes: 4 additions & 0 deletions test/com/google/javascript/jscomp/VarCheckTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,10 @@ public void testReferencedVarNotDefined() {
testError("x = 0;", VarCheck.UNDEFINED_VAR_ERROR);
}

public void testReferencedVarNotDefined_arrowFunctionBody() {
testError("() => y", VarCheck.UNDEFINED_VAR_ERROR);
}

public void testReferencedLetNotDefined() {
testError("{ let x = 1; } var y = x;", VarCheck.UNDEFINED_VAR_ERROR);
}
Expand Down

0 comments on commit 996b562

Please sign in to comment.