diff --git a/lib/rules/comma-dangle.js b/lib/rules/comma-dangle.js index 6a737227bac..2c145a18769 100644 --- a/lib/rules/comma-dangle.js +++ b/lib/rules/comma-dangle.js @@ -13,14 +13,17 @@ const lodash = require("lodash"); /** * Checks whether or not a trailing comma is allowed in a given node. - * `ArrayPattern` which has `RestElement` disallows it. + * `ArrayPattern` which has `RestElement` or `ObjectPattern` which has `RestProperty` disallows it. * * @param {ASTNode} node - A node to check. * @param {ASTNode} lastItem - The node of the last element in the given node. * @returns {boolean} `true` if a trailing comma is allowed. */ function isTrailingCommaAllowed(node, lastItem) { - return node.type !== "ArrayPattern" || lastItem.type !== "RestElement"; + return !( + (node.type === "ArrayPattern" && lastItem.type === "RestElement") || + (node.type === "ObjectPattern" && (lastItem.type === "RestProperty" || lastItem.type === "ExperimentalRestProperty")) + ); } //------------------------------------------------------------------------------ diff --git a/tests/lib/rules/comma-dangle.js b/tests/lib/rules/comma-dangle.js index eff4dca2bf4..a4d6e74aee9 100644 --- a/tests/lib/rules/comma-dangle.js +++ b/tests/lib/rules/comma-dangle.js @@ -116,6 +116,13 @@ ruleTester.run("comma-dangle", rule, { options: ["always"] }, + // https://github.com/eslint/eslint/issues/7297 + { + code: "var {foo, ...bar} = baz", + parserOptions: { ecmaVersion: 8, ecmaFeatures: { experimentalObjectRestSpread: true } }, + options: ["always"] + }, + // https://github.com/eslint/eslint/issues/3794 { code: "import {foo,} from 'foo';",