Skip to content
Permalink
Browse files

New: allow-multiline option on comma-dangle (fixes #4967)

  • Loading branch information...
gimenete committed Jan 15, 2016
1 parent 86d64ca commit 29392f839346516fb64c17891c526f0f29269570
Showing with 269 additions and 1 deletion.
  1. +57 −0 docs/rules/comma-dangle.md
  2. +19 −1 lib/rules/comma-dangle.js
  3. +193 −0 tests/lib/rules/comma-dangle.js
@@ -19,6 +19,7 @@ This rule takes one argument, which can be one of the following options:

- `"always"` - warn whenever a missing comma is detected.
- `"always-multiline"` - warn if there is a missing trailing comma on arrays or objects that span multiple lines, and warns if there is a trailing comma present on single line arrays or objects.
- `"only-multiline"` - warn whenever a trailing comma is detected on single line nodes.
- `"never"` - warn whenever a trailing comma is detected.

The default value of this option is `"never"`.
@@ -150,6 +151,62 @@ foo({
});
```

The following patterns are considered problems when configured `"only-multiline"`:

```js
/*eslint comma-dangle: [1, "only-multiline"]*/
var foo = { bar: "baz", qux: "quux", }; /*error Unexpected trailing comma.*/
var arr = [1,2,]; /*error Unexpected trailing comma.*/
var arr = [1,
2,]; /*error Unexpected trailing comma.*/
```

The following patterns are not considered problems when configured `"only-multiline"`:

```js
/*eslint comma-dangle: [2, "only-multiline"]*/
var foo = {
bar: "baz",
qux: "quux",
};
var foo = {
bar: "baz",
qux: "quux"
};
var foo = {bar: "baz", qux: "quux"};
var arr = [1,2];
var arr = [1,
2];
var arr = [
1,
2,
];
var arr = [
1,
2
];
foo({
bar: "baz",
qux: "quux",
});
foo({
bar: "baz",
qux: "quux"
});
```

## When Not To Use It

You can turn this rule off if you are not concerned with dangling commas.
@@ -187,12 +187,30 @@ module.exports = function(context) {
}
}

/**
* Only if a given node is not multiline, reports the last element of a given node
* when it does not have a trailing comma.
* Otherwise, reports a trailing comma if it exists.
*
* @param {ASTNode} node - A node to check. Its type is one of
* ObjectExpression, ObjectPattern, ArrayExpression, ArrayPattern,
* ImportDeclaration, and ExportNamedDeclaration.
* @returns {void}
*/
function allowTrailingCommaIfMultiline(node) {
if (!isMultiline(node)) {
forbidTrailingComma(node);
}
}

// Chooses a checking function.
var checkForTrailingComma;
if (mode === "always") {
checkForTrailingComma = forceTrailingComma;
} else if (mode === "always-multiline") {
checkForTrailingComma = forceTrailingCommaIfMultiline;
} else if (mode === "only-multiline") {
checkForTrailingComma = allowTrailingCommaIfMultiline;
} else {
checkForTrailingComma = forbidTrailingComma;
}
@@ -209,6 +227,6 @@ module.exports = function(context) {

module.exports.schema = [
{
"enum": ["always", "always-multiline", "never"]
"enum": ["always", "always-multiline", "only-multiline", "never"]
}
];
Oops, something went wrong.

0 comments on commit 29392f8

Please sign in to comment.
You can’t perform that action at this time.