Skip to content

Commit

Permalink
[[FIX]] Report "Bad assignment." in destructuring
Browse files Browse the repository at this point in the history
During destructuring, the return value of the internal
`checkLeftSideAssign` function was discarded, so JSHint would fail to
report invalid assignment targets in that context.

Move the reporting of Error 031 ("Bad assignment.") into the
`checkLeftSideAssign` to enhance consistency. Improve the
implementation of that internal function by making it aware of the rest
operator (the previous implementation considered this form "invalid" but
failed to report that).
  • Loading branch information
jugglinmike committed Feb 8, 2016
1 parent 08df19e commit fe559ed
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 11 deletions.
20 changes: 9 additions & 11 deletions src/jshint.js
Original file line number Diff line number Diff line change
Expand Up @@ -1335,6 +1335,9 @@ var JSHINT = (function() {
if (nativeObject)
warning("W121", left, nativeObject);
}
if (checkPunctuator(left, "...")) {
left = left.right;
}

if (left.identifier && !left.isMetaProperty) {
// reassign also calls modify
Expand Down Expand Up @@ -1370,10 +1373,7 @@ var JSHINT = (function() {
}

return true;
} else if (left.isMetaProperty) {
error("E031", assignToken);
return true;
} else if (left.identifier && !isReserved(left)) {
} else if (left.identifier && !isReserved(left) && !left.isMetaProperty) {
if (state.funct["(scope)"].labeltype(left.value) === "exception") {
warning("W022", left);
}
Expand All @@ -1383,6 +1383,8 @@ var JSHINT = (function() {

if (left === state.syntax["function"]) {
warning("W023", state.tokens.curr);
} else {
error("E031", assignToken);
}

return false;
Expand All @@ -1392,9 +1394,7 @@ var JSHINT = (function() {
var x = infix(s, typeof f === "function" ? f : function(left, that) {
that.left = left;

if (!checkLeftSideAssign(left, that, { allowDestructuring: true })) {
error("E031", that);
}
checkLeftSideAssign(left, that, { allowDestructuring: true });

that.right = expression(10);

Expand Down Expand Up @@ -1427,9 +1427,7 @@ var JSHINT = (function() {
warning("W016", that, that.id);
}

if (!checkLeftSideAssign(left, that)) {
error("E031", that);
}
checkLeftSideAssign(left, that);

that.right = expression(10);

Expand Down Expand Up @@ -2277,7 +2275,7 @@ var JSHINT = (function() {

error("E030", state.tokens.next, state.tokens.next.value);
}
expression(150);
this.right = expression(150);
return this;
});

Expand Down
6 changes: 6 additions & 0 deletions tests/unit/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -1854,6 +1854,7 @@ exports["destructuring globals with syntax error"] = function (test) {

TestRun(test)
.addError(3, "Expected an identifier and instead saw '1'.")
.addError(3, "Bad assignment.")
.addError(4, "Expected ',' and instead saw ';'.")
.addError(5, "Expected ']' to match '[' from line 5 and instead saw ';'.")
.addError(5, "Missing semicolon.")
Expand Down Expand Up @@ -1895,6 +1896,8 @@ exports["destructuring globals with syntax error"] = function (test) {
.addError(4, "Bad assignment.")
.addError(6, "Do not assign to the exception parameter.")
.addError(7, "Do not assign to the exception parameter.")
.addError(9, "Bad assignment.")
.addError(10, "Bad assignment.")
.test([
"[ Number.prototype.toString ] = [function(){}];",
"function a() {",
Expand All @@ -1904,6 +1907,9 @@ exports["destructuring globals with syntax error"] = function (test) {
" ({e} = {e});",
" [e] = [];",
" }",
" ({ x: null } = {});",
" ({ y: [...this] } = {});",
" ({ y: [...z] } = {});",
"}"], {esnext: true, freeze: true});

test.done();
Expand Down

0 comments on commit fe559ed

Please sign in to comment.