1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 | var assert = require('assert');
|
34 |
|
35 | module.exports = function() {};
|
36 |
|
37 | module.exports.prototype = {
|
38 | configure: function(option) {
|
39 | var isTrue = option === true;
|
40 |
|
41 | assert(
|
42 | isTrue || (typeof option === 'object' && Array.isArray(option.allExcept)),
|
43 | this.getOptionName() + ' requires the value `true` ' +
|
44 | 'or an object with an `allExcept` array property'
|
45 | );
|
46 |
|
47 | this._propertyExceptions = !isTrue && option.allExcept || [];
|
48 | },
|
49 |
|
50 | getOptionName: function() {
|
51 | return 'requireObjectDestructuring';
|
52 | },
|
53 |
|
54 | check: function(file, errors) {
|
55 | var propertyExceptions = this._propertyExceptions;
|
56 |
|
57 | file.iterateNodesByType('VariableDeclaration', function(node) {
|
58 |
|
59 | node.declarations.forEach(function(declaration) {
|
60 | var declarationId = declaration.id || {};
|
61 | var declarationInit = declaration.init || {};
|
62 |
|
63 | if (declarationId.type !== 'Identifier' || declarationInit.type !== 'MemberExpression') {
|
64 | return;
|
65 | }
|
66 |
|
67 | var propertyName = declarationInit.property && declarationInit.property.name;
|
68 |
|
69 | if (declarationId.name === propertyName &&
|
70 | propertyExceptions.indexOf(propertyName) < 0) {
|
71 |
|
72 | errors.add('Property assignments should use destructuring', node);
|
73 | }
|
74 | });
|
75 | });
|
76 | }
|
77 | };
|