Skip to content
This repository was archived by the owner on Mar 23, 2024. It is now read-only.

Commit ecec961

Browse files
seanpdoylemarkelog
authored andcommitted
New rule: requireArrayDestructuring
When `{ requireArrayDestructuring: true }`, assert that variable declarations use array destructuring when assigning via indexing the array with a literal Closes gh-1984
1 parent b8969d4 commit ecec961

3 files changed

Lines changed: 137 additions & 0 deletions

File tree

lib/config/configuration.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -950,6 +950,7 @@ Configuration.prototype.registerDefaultRules = function() {
950950
this.registerRule(require('../rules/require-spaces-in-generator'));
951951
this.registerRule(require('../rules/require-object-destructuring'));
952952
this.registerRule(require('../rules/require-enhanced-object-literals'));
953+
this.registerRule(require('../rules/require-array-destructuring'));
953954
/* ES6 only (end) */
954955

955956
this.registerRule(require('../rules/require-curly-braces'));
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/**
2+
* Requires that variable assignment from array values are * destructured.
3+
*
4+
* Type: `Boolean`
5+
*
6+
* Values: `true`
7+
*
8+
* Version: `ES6`
9+
*
10+
* #### Example
11+
*
12+
* ```js
13+
* "requireArrayDestructuring": true
14+
* ```
15+
*
16+
* ##### Valid
17+
*
18+
* ```js
19+
* var colors = ['red', 'green', 'blue'];
20+
* var [ red ] = colors;
21+
*
22+
* var attributes = {
23+
* colors: ['red', 'green', 'blue'];
24+
* };
25+
*
26+
* var [ red ] = attributes.colors;
27+
* ```
28+
*
29+
* ##### Invalid
30+
*
31+
* ```js
32+
* var colors = ['red', 'green', 'blue'];
33+
* var red = colors[0];
34+
*
35+
* var attributes = {
36+
* colors: ['red', 'green', 'blue'];
37+
* };
38+
*
39+
* var red = attributes.colors[0];
40+
* ```
41+
*/
42+
43+
var assert = require('assert');
44+
45+
module.exports = function() {};
46+
47+
module.exports.prototype = {
48+
configure: function(option) {
49+
assert(option === true, this.getOptionName() + ' requires a true value');
50+
},
51+
52+
getOptionName: function() {
53+
return 'requireArrayDestructuring';
54+
},
55+
56+
check: function(file, errors) {
57+
file.iterateNodesByType('VariableDeclaration', function(node) {
58+
59+
node.declarations.forEach(function(declaration) {
60+
if (!declaration.init || declaration.init.type !== 'MemberExpression') {
61+
return;
62+
}
63+
64+
var property = declaration.init.property || {};
65+
if (property.type === 'Literal' && /^\d+$/.test(property.value)) {
66+
errors.add('Use array destructuring', property.loc.start);
67+
}
68+
});
69+
});
70+
}
71+
};
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
var Checker = require('../../../lib/checker');
2+
var expect = require('chai').expect;
3+
4+
describe('rules/require-array-destructuring', function() {
5+
var nestedWithDestructuring = [
6+
'var attributes = {',
7+
' colors: ["red", "green", "blue"]',
8+
'};',
9+
'var first = attributes.colors[0];'
10+
].join('\n');
11+
12+
var withDestructuring = [
13+
'var colors = ["red", "green", "blue"];',
14+
'var [ red ] = colors;'
15+
].join('\n');
16+
17+
var withIndexing = [
18+
'var colors = ["red", "green", "blue"];',
19+
'var red = colors[0];'
20+
].join('\n');
21+
22+
describe('when { requireArrayDestructuring: true }', function() {
23+
it('allows array destructuring', function() {
24+
var checker = buildChecker({ requireArrayDestructuring: true });
25+
26+
expect(checker.checkString(withDestructuring)).to.have.no.errors();
27+
});
28+
29+
it('disallows variable assignment via array indexing with a literal', function() {
30+
var checker = buildChecker({ requireArrayDestructuring: true });
31+
32+
expect(checker.checkString(withIndexing)).
33+
to.have.one.validation.error.from('requireArrayDestructuring');
34+
});
35+
36+
it('requires nested array destructuring', function() {
37+
var checker = buildChecker({ requireArrayDestructuring: true });
38+
39+
expect(checker.checkString(nestedWithDestructuring))
40+
.to.have.one.validation.error.from('requireArrayDestructuring');
41+
});
42+
});
43+
44+
describe('when { requireArrayDestructuring: false }', function() {
45+
it('allows array destructuring', function() {
46+
var checker = buildChecker({ requireArrayDestructuring: false });
47+
48+
expect(checker.checkString(withDestructuring)).to.have.no.errors();
49+
});
50+
51+
it('allows variable assignment via array indexing with a literal', function() {
52+
var checker = buildChecker({ requireArrayDestructuring: false });
53+
54+
expect(checker.checkString(withDestructuring)).to.have.no.errors();
55+
});
56+
});
57+
58+
function buildChecker(rules) {
59+
var checker = new Checker();
60+
checker.registerDefaultRules();
61+
checker.configure(rules);
62+
63+
return checker;
64+
}
65+
});

0 commit comments

Comments
 (0)