Skip to content

Commit

Permalink
Simplify no-unreadable-array-destructuring
Browse files Browse the repository at this point in the history
  • Loading branch information
fisker committed Mar 17, 2020
1 parent 74ddbf9 commit 1f25046
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 13 deletions.
17 changes: 6 additions & 11 deletions rules/no-unreadable-array-destructuring.js
@@ -1,22 +1,17 @@
'use strict';
const getDocumentationUrl = require('./utils/get-documentation-url');

const isCommaFollowedWithComma = (element, index, array) => {
return element === null && array[index + 1] === null;
};
const message = 'Array destructuring may not contain consecutive ignored values.';
const isCommaFollowedWithComma = (element, index, array) =>
element === null && array[index + 1] === null;

const create = context => {
return {
ArrayPattern(node) {
const {elements} = node;
if (!elements || elements.length === 0) {
return;
}

if (elements.some(isCommaFollowedWithComma)) {
'ArrayPattern[elements.length>1]': node => {
if (node.elements.some(isCommaFollowedWithComma)) {
context.report({
node,
message: 'Array destructuring may not contain consecutive ignored values.'
message
});
}
}
Expand Down
11 changes: 9 additions & 2 deletions test/no-unreadable-array-destructuring.js
Expand Up @@ -30,7 +30,9 @@ ruleTester.run('no-unreadable-array-destructuring', rule, {
'function foo([bar,]) {}',
'function foo([bar,,]) {}',
'function foo([bar,, baz,, qux]) {}',
'const [, ...rest] = parts;'
'const [, ...rest] = parts;',
// This is stupid, but valid code
'const [,] = parts;'
],
invalid: [
{
Expand Down Expand Up @@ -68,6 +70,11 @@ ruleTester.run('no-unreadable-array-destructuring', rule, {
{
code: 'const [,,...rest] = parts;',
errors
}
},
// This is stupid, but valid code
{
code: 'const [,,] = parts;',
errors
},
]
});

0 comments on commit 1f25046

Please sign in to comment.