UNPKG

2.01 kBJavaScriptView Raw
1/**
2 * Requires object destructuring for multiple return values,
3 * not array destructuring.
4 *
5 * Type: `Boolean`
6 *
7 * Value: `true`
8 *
9 * Version: `ES6`
10 *
11 * #### Example
12 *
13 * ```js
14 * "disallowArrayDestructuringReturn": true
15 * ```
16 *
17 * ##### Valid
18 *
19 * ```js
20 * const { left, right } = processInput(input);
21 * ```
22 *
23 * ##### Invalid
24 *
25 * ```js
26 * const [ left, __, top ] = processInput(input);
27 * ```
28 */
29
30var assert = require('assert');
31
32module.exports = function() {};
33
34module.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};