This repository has been archived by the owner on Mar 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 510
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
- Loading branch information
Showing
4 changed files
with
128 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
}); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); | ||
}); | ||
}); |