Skip to content

Commit

Permalink
[[FIX]] parse trailing comma in ObjectBindingPattern
Browse files Browse the repository at this point in the history
  • Loading branch information
caitp committed Feb 28, 2015
1 parent d82cf19 commit 7a2b713
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 10 deletions.
28 changes: 19 additions & 9 deletions src/jshint.js
Expand Up @@ -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(")");
Expand All @@ -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 {
Expand Down
3 changes: 2 additions & 1 deletion src/messages.js
Expand Up @@ -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 = {
Expand Down
57 changes: 57 additions & 0 deletions tests/unit/parser.js
Expand Up @@ -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.