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

Commit

Permalink
New rule: disallowArrayDestructuringReturn
Browse files Browse the repository at this point in the history
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
  • Loading branch information
fxmaxvl authored and markelog committed Mar 1, 2016
1 parent d760044 commit 8500623
Show file tree
Hide file tree
Showing 4 changed files with 128 additions and 1 deletion.
3 changes: 2 additions & 1 deletion grouping.json
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,8 @@
"requireEnhancedObjectLiterals",
"requireArrayDestructuring",
"disallowVar",
"requireSpaceBeforeDestructuredValues"
"requireSpaceBeforeDestructuredValues",
"disallowArrayDestructuringReturn"
],
"Everything else": [
"requireParenthesesAroundIIFE",
Expand Down
1 change: 1 addition & 0 deletions lib/config/configuration.js
Original file line number Diff line number Diff line change
Expand Up @@ -959,6 +959,7 @@ Configuration.prototype.registerDefaultRules = function() {
this.registerRule(require('../rules/disallow-var'));
this.registerRule(require('../rules/require-imports-alphabetized'));
this.registerRule(require('../rules/require-space-before-destructured-values'));
this.registerRule(require('../rules/disallow-array-destructuring-return'));
/* ES6 only (end) */

this.registerRule(require('../rules/require-curly-braces'));
Expand Down
83 changes: 83 additions & 0 deletions lib/rules/disallow-array-destructuring-return.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/**
* Requires object destructuring for multiple return values,
* not array destructuring.
*
* Type: `Boolean`
*
* Value: `true`
*
* Version: `ES6`
*
* #### Example
*
* ```js
* "disallowArrayDestructuringReturn": true
* ```
*
* ##### Valid
*
* ```js
* const { left, right } = processInput(input);
* ```
*
* ##### Invalid
*
* ```js
* const [ left, __, top ] = processInput(input);
* ```
*/

var assert = require('assert');

module.exports = function() {};

module.exports.prototype = {

configure: function(options) {
assert(
options === true,
this.getOptionName() + ' option requires a true value or should be removed'
);
},

getOptionName: function() {
return 'disallowArrayDestructuringReturn';
},

check: function(file, errors) {
var addError = function(node) {
errors.add(
'Array destructuring is not allowed for return, ' +
'use object destructuring instead',
node
);
};

var isViolationDetected = function(maybeArrayPattern, maybeCallExpression) {

return maybeCallExpression && maybeCallExpression.type === 'CallExpression' &&
maybeArrayPattern && maybeArrayPattern.type === 'ArrayPattern';
};

file.iterateNodesByType(['VariableDeclaration', 'AssignmentExpression'], function(node) {

if (node.type === 'VariableDeclaration') {
node.declarations.forEach(function(declaration) {
if (!isViolationDetected(declaration.id, declaration.init)) {
return;
}

addError(declaration.init);
});
}

if (node.type === 'AssignmentExpression') {
if (!isViolationDetected(node.left, node.right)) {
return;
}

addError(node.right);
}
});
}
};
42 changes: 42 additions & 0 deletions test/specs/rules/disallow-array-destructuring-return.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
var expect = require('chai').expect;
var Checker = require('../../../lib/checker');

describe('rules/disallow-array-destructuring-return', function() {
var checker;

describe('when { disallowArrayDestructuringReturn: true }', function() {
beforeEach(function() {
checker = new Checker();
checker.registerDefaultRules();
checker.configure({disallowArrayDestructuringReturn: true});
});

it('should report on array destructuring with function call', function() {
expect(
checker.checkString('const [ a, b ] = func();')
).to.have.one.validation.error.from('disallowArrayDestructuringReturn');
});

it('should report on array destructuring with self invoking functions', function() {
expect(
checker.checkString('const [ a, b ] = (() => [1, 2])();')
).to.have.one.validation.error.from('disallowArrayDestructuringReturn');
});

it('should report on array destructuring in assignment expressions', function() {
expect(
checker.checkString('([ a, b ] = func());')
).to.have.one.validation.error.from('disallowArrayDestructuringReturn');
});

it('should not report on object destructuring', function() {
expect(checker.checkString('const { a, b } = func();')).
to.not.have.errors();
});

it('should not report on object destructuring in assignment expression', function() {
expect(checker.checkString('({ a, b } = func());')).
to.not.have.errors();
});
});
});

0 comments on commit 8500623

Please sign in to comment.