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.indexOf('Literal') > -1 && /^\d+$/.test(property.value)) {
|
66 | errors.add('Use array destructuring', property);
|
67 | }
|
68 | });
|
69 | });
|
70 | }
|
71 | };
|