Skip to content

Commit

Permalink
[[FIX]] Correctly parse empty destructuring
Browse files Browse the repository at this point in the history
Example:
  let {} = {};
  function x([]) {}

Fixes #2513
  • Loading branch information
nicolo-ribaudo authored and lukeapage committed Jul 17, 2015
1 parent d261071 commit 97c188b
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 16 deletions.
27 changes: 12 additions & 15 deletions src/jshint.js
Original file line number Diff line number Diff line change
Expand Up @@ -3260,12 +3260,16 @@ var JSHINT = (function() {
};
if (checkPunctuators(state.tokens.next, ["["])) {
advance("[");
var element_after_rest = false;
if (nextInnerDE() && checkPunctuators(state.tokens.next, [","])) {
warning("W130", state.tokens.next);
element_after_rest = true;
if (checkPunctuators(state.tokens.next, ["]"])) {
warning("W137", state.tokens.curr);
}
var element_after_rest = false;
while (!checkPunctuators(state.tokens.next, ["]"])) {
if (nextInnerDE() && !element_after_rest &&
checkPunctuators(state.tokens.next, [","])) {
warning("W130", state.tokens.next);
element_after_rest = true;
}
if (checkPunctuators(state.tokens.next, ["="])) {
if (checkPunctuators(state.tokens.prev, ["..."])) {
advance("]");
Expand All @@ -3279,22 +3283,16 @@ var JSHINT = (function() {
}
if (!checkPunctuators(state.tokens.next, ["]"])) {
advance(",");
if (checkPunctuators(state.tokens.next, ["]"])) {
// Trailing comma
break;
}
if (nextInnerDE() && checkPunctuators(state.tokens.next, [","]) &&
!element_after_rest) {
warning("W130", state.tokens.next);
element_after_rest = true;
}
}
}
advance("]");
} else if (checkPunctuators(state.tokens.next, ["{"])) {
advance("{");
assignmentProperty();
if (checkPunctuators(state.tokens.next, ["}"])) {
warning("W137", state.tokens.curr);
}
while (!checkPunctuators(state.tokens.next, ["}"])) {
assignmentProperty();
if (checkPunctuators(state.tokens.next, ["="])) {
advance("=");
if (state.tokens.next.id === "undefined") {
Expand All @@ -3309,7 +3307,6 @@ var JSHINT = (function() {
// ObjectBindingPattern: { BindingPropertyList , }
break;
}
assignmentProperty();
}
}
advance("}");
Expand Down
3 changes: 2 additions & 1 deletion src/messages.js
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,8 @@ var warnings = {
W133: "Invalid for-{a} loop left-hand-side: {b}.",
W134: "The '{a}' option is only available when linting ECMAScript {b} code.",
W135: "{a} may not be supported by non-browser environments.",
W136: "'{a}' must be in function scope."
W136: "'{a}' must be in function scope.",
W137: "Empty destructuring."
};

var info = {
Expand Down
20 changes: 20 additions & 0 deletions tests/unit/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -1811,6 +1811,26 @@ exports["non-identifier PropertyNames in object destructuring"] = function (test
test.done();
};

exports["empty destructuring"] = function (test) {
var code = [
"var {} = {};",
"var [] = [];",
"function a({}, []) {}",
"var b = ({}) => ([]) => ({});"
];

TestRun(test)
.addError(1, "Empty destructuring.")
.addError(2, "Empty destructuring.")
.addError(3, "Empty destructuring.")
.addError(3, "Empty destructuring.")
.addError(4, "Empty destructuring.")
.addError(4, "Empty destructuring.")
.test(code, { esnext: true });

test.done();
};

exports["array element assignment inside array"] = function (test) {
var code = [
"var a1 = {};",
Expand Down

0 comments on commit 97c188b

Please sign in to comment.