Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add no-unreadable-array-destructuring rule #199

Merged
merged 7 commits into from
Dec 15, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions docs/rules/no-unreadable-array-destructuring.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Disallow unreadable array destructuring

Destructuring is very useful, but it can also make some code harder to read.
sindresorhus marked this conversation as resolved.
Show resolved Hide resolved

## Fail

```js
const [,, foo] = parts;
const [,,, foo] = parts;
const [,,,, foo] = parts;
```


## Pass

```js
const [, foo] = parts;
const [foo] = parts;
const foo = parts[3];
```
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ module.exports = {
'unicorn/filename-case': ['error', {case: 'kebabCase'}],
'unicorn/no-abusive-eslint-disable': 'error',
'unicorn/no-process-exit': 'error',
'unicorn/no-unreadable-array-destructuring': 'error',
sindresorhus marked this conversation as resolved.
Show resolved Hide resolved
'unicorn/throw-new-error': 'error',
'unicorn/number-literal-case': 'error',
'unicorn/escape-case': 'error',
Expand Down
35 changes: 35 additions & 0 deletions rules/no-unreadable-array-destructuring.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
'use strict';
const getDocsUrl = require('./utils/get-docs-url');

const isCommaFollwedWithComma = (el, index, array) => {
sindresorhus marked this conversation as resolved.
Show resolved Hide resolved
if (el === null) {
return array[index + 1] === el;
sindresorhus marked this conversation as resolved.
Show resolved Hide resolved
}
};

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

if (elements.some(isCommaFollwedWithComma)) {
sindresorhus marked this conversation as resolved.
Show resolved Hide resolved
context.report({
node,
message: 'Only one ignored value in series allowed in array destructuring.'
sindresorhus marked this conversation as resolved.
Show resolved Hide resolved
});
}
}
};
};

module.exports = {
create,
meta: {
docs: {
url: getDocsUrl(__filename)
}
}
};
39 changes: 39 additions & 0 deletions test/no-unreadable-array-destructuring.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import test from 'ava';
import avaRuleTester from 'eslint-ava-rule-tester';
import rule from '../rules/no-unreadable-array-destructuring';

const ruleTester = avaRuleTester(test, {
env: {
es6: true
}
});

ruleTester.run('no-unreadable-array-destructuring', rule, {
valid: [
'const [, foo] = parts;',
'const [foo] = parts;',
'const [foo,,bar] = parts;',
'const [foo, , bar] = parts;',
'const [foo,] = parts;',
'const [foo,,] = parts;',
'const [foo,, bar,, baz] = parts;'
],
invalid: [
{
code: 'const [,, foo] = parts;',
errors: [{message: 'Only one ignored value in series allowed in array destructuring.'}]
},
{
code: 'const [foo,,, bar] = parts;',
errors: [{message: 'Only one ignored value in series allowed in array destructuring.'}]
},
{
code: 'const [foo,,,] = parts;',
errors: [{message: 'Only one ignored value in series allowed in array destructuring.'}]
},
{
code: 'const [foo, bar,, baz ,,, qux] = parts;',
errors: [{message: 'Only one ignored value in series allowed in array destructuring.'}]
}
]
});