Permalink
Browse files

[[FIX]] parse trailing comma in ObjectBindingPattern

Closes #2220
  • Loading branch information...
caitp committed Feb 28, 2015
1 parent d82cf19 commit 7a2b713e9e874910890339660d4f00d16966e99d
Showing with 78 additions and 10 deletions.
  1. +19 −9 src/jshint.js
  2. +2 −1 src/messages.js
  3. +57 −0 tests/unit/parser.js
@@ -3301,15 +3301,15 @@ var JSHINT = (function() {
}
var nextInnerDE = function() {
var ident;
if (_.contains(["[", "{"], state.tokens.next.value)) {
if (checkPunctuators(state.tokens.next, ["[", "{"])) {
ids = destructuringExpression();
for (var id in ids) {
id = ids[id];
identifiers.push({ id: id.id, token: id.token });
}
} else if (state.tokens.next.value === ",") {
} else if (checkPunctuators(state.tokens.next, [","])) {
identifiers.push({ id: null, token: state.tokens.curr });
} else if (state.tokens.next.value === "(") {
} else if (checkPunctuators(state.tokens.next, ["("])) {
advance("(");
nextInnerDE();
advance(")");
@@ -3319,27 +3319,37 @@ var JSHINT = (function() {
identifiers.push({ id: ident, token: state.tokens.curr });
}
};
if (state.tokens.next.value === "[") {
if (checkPunctuators(state.tokens.next, ["["])) {
advance("[");
nextInnerDE();
while (state.tokens.next.value !== "]") {
while (!checkPunctuators(state.tokens.next, ["]"])) {
advance(",");
if (checkPunctuators(state.tokens.next, ["]"])) {
// Trailing commas are not allowed in ArrayBindingPattern
warning("W130", state.tokens.next);
break;
}
nextInnerDE();
}
advance("]");
} else if (state.tokens.next.value === "{") {
} else if (checkPunctuators(state.tokens.next, ["{"])) {
advance("{");
id = identifier();
if (state.tokens.next.value === ":") {
if (checkPunctuators(state.tokens.next, [":"])) {
advance(":");
nextInnerDE();
} else {
identifiers.push({ id: id, token: state.tokens.curr });
}
while (state.tokens.next.value !== "}") {
while (!checkPunctuators(state.tokens.next, ["}"])) {
advance(",");
if (checkPunctuators(state.tokens.next, ["}"])) {
// Trailing comma
// ObjectBindingPattern: { BindingPropertyList , }
break;
}
id = identifier();
if (state.tokens.next.value === ":") {
if (checkPunctuators(state.tokens.next, [":"])) {
advance(":");
nextInnerDE();
} else {
@@ -204,7 +204,8 @@ var warnings = {
W127: "Unexpected use of a comma operator.",
W128: "Empty array elements require elision=true.",
W129: "'{a}' is defined in a future version of JavaScript. Use a " +
"different variable name to avoid migration issues."
"different variable name to avoid migration issues.",
W130: "Trailing ',' is not valid in array destructuring assignment."
};

var info = {
@@ -5494,3 +5494,60 @@ exports.dereferenceDelete = function (test) {

test.done();
};

exports.trailingCommaInObjectBindingPattern = function (test) {
var code = [
'function fn(O) {',
' var {a, b, c,} = O;',
'}',
'fn({ a: 1, b: 2, c: 3 });'
];

TestRun(test)
.test(code, { esnext: true });

test.done();
};


exports.trailingCommaInObjectBindingPatternParameters = function (test) {
var code = [
'function fn({a, b, c,}) { }',
'fn({ a: 1, b: 2, c: 3 });'
];

TestRun(test)
.test(code, { esnext: true });

test.done();
};


exports.trailingCommaInArrayBindingPattern = function (test) {
var code = [
'function fn(O) {',
' var [a, b, c,] = O;',
'}',
'fn([1, 2, 3]);'
];

TestRun(test)
.addError(2, "Trailing ',' is not valid in array destructuring assignment.")
.test(code, { esnext: true });

test.done();
};


exports.trailingCommaInArrayBindingPatternParameters = function (test) {
var code = [
'function fn([a, b, c,]) { }',
'fn([1, 2, 3]);'
];

TestRun(test)
.addError(1, "Trailing ',' is not valid in array destructuring assignment.")
.test(code, { esnext: true });

test.done();
};

0 comments on commit 7a2b713

Please sign in to comment.