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

Commit 8500623

Browse files
fxmaxvlmarkelog
authored andcommitted
New rule: disallowArrayDestructuringReturn
Requires object destructuring for multiple return values, not array destructuring Valid: const { left, right } = processInput(input); Invalid: const [ left, __, top ] = processInput(input); Fixes #2073 Ref gh-2149
1 parent d760044 commit 8500623

File tree

4 files changed

+128
-1
lines changed

4 files changed

+128
-1
lines changed

grouping.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,8 @@
163163
"requireEnhancedObjectLiterals",
164164
"requireArrayDestructuring",
165165
"disallowVar",
166-
"requireSpaceBeforeDestructuredValues"
166+
"requireSpaceBeforeDestructuredValues",
167+
"disallowArrayDestructuringReturn"
167168
],
168169
"Everything else": [
169170
"requireParenthesesAroundIIFE",

lib/config/configuration.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -959,6 +959,7 @@ Configuration.prototype.registerDefaultRules = function() {
959959
this.registerRule(require('../rules/disallow-var'));
960960
this.registerRule(require('../rules/require-imports-alphabetized'));
961961
this.registerRule(require('../rules/require-space-before-destructured-values'));
962+
this.registerRule(require('../rules/disallow-array-destructuring-return'));
962963
/* ES6 only (end) */
963964

964965
this.registerRule(require('../rules/require-curly-braces'));
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
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+
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+
};
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
var expect = require('chai').expect;
2+
var Checker = require('../../../lib/checker');
3+
4+
describe('rules/disallow-array-destructuring-return', function() {
5+
var checker;
6+
7+
describe('when { disallowArrayDestructuringReturn: true }', function() {
8+
beforeEach(function() {
9+
checker = new Checker();
10+
checker.registerDefaultRules();
11+
checker.configure({disallowArrayDestructuringReturn: true});
12+
});
13+
14+
it('should report on array destructuring with function call', function() {
15+
expect(
16+
checker.checkString('const [ a, b ] = func();')
17+
).to.have.one.validation.error.from('disallowArrayDestructuringReturn');
18+
});
19+
20+
it('should report on array destructuring with self invoking functions', function() {
21+
expect(
22+
checker.checkString('const [ a, b ] = (() => [1, 2])();')
23+
).to.have.one.validation.error.from('disallowArrayDestructuringReturn');
24+
});
25+
26+
it('should report on array destructuring in assignment expressions', function() {
27+
expect(
28+
checker.checkString('([ a, b ] = func());')
29+
).to.have.one.validation.error.from('disallowArrayDestructuringReturn');
30+
});
31+
32+
it('should not report on object destructuring', function() {
33+
expect(checker.checkString('const { a, b } = func();')).
34+
to.not.have.errors();
35+
});
36+
37+
it('should not report on object destructuring in assignment expression', function() {
38+
expect(checker.checkString('({ a, b } = func());')).
39+
to.not.have.errors();
40+
});
41+
});
42+
});

0 commit comments

Comments
 (0)