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 | var assert = require('assert');
|
31 |
|
32 | module.exports = function() {};
|
33 |
|
34 | module.exports.prototype = {
|
35 |
|
36 | configure: function(options) {
|
37 | assert(
|
38 | options === true,
|
39 | this.getOptionName() + ' option requires a true value or should be removed'
|
40 | );
|
41 | },
|
42 |
|
43 | getOptionName: function() {
|
44 | return 'disallowArrayDestructuringReturn';
|
45 | },
|
46 |
|
47 | check: function(file, errors) {
|
48 | var addError = function(node) {
|
49 | errors.add(
|
50 | 'Array destructuring is not allowed for return, ' +
|
51 | 'use object destructuring instead',
|
52 | node
|
53 | );
|
54 | };
|
55 |
|
56 | var isViolationDetected = function(maybeArrayPattern, maybeCallExpression) {
|
57 |
|
58 | return maybeCallExpression && maybeCallExpression.type === 'CallExpression' &&
|
59 | maybeArrayPattern && maybeArrayPattern.type === 'ArrayPattern';
|
60 | };
|
61 |
|
62 | file.iterateNodesByType(['VariableDeclaration', 'AssignmentExpression'], function(node) {
|
63 |
|
64 | if (node.type === 'VariableDeclaration') {
|
65 | node.declarations.forEach(function(declaration) {
|
66 | if (!isViolationDetected(declaration.id, declaration.init)) {
|
67 | return;
|
68 | }
|
69 |
|
70 | addError(declaration.init);
|
71 | });
|
72 | }
|
73 |
|
74 | if (node.type === 'AssignmentExpression') {
|
75 | if (!isViolationDetected(node.left, node.right)) {
|
76 | return;
|
77 | }
|
78 |
|
79 | addError(node.right);
|
80 | }
|
81 | });
|
82 | }
|
83 | };
|