This repository has been archived by the owner. It is now read-only.
Permalink
Browse files

Fix: `disallowParenthesesAroundArrowParam` - account for non-identifi…

…ers (RestElement, ArrayPattern) correctly

Fixes #1831
Closes gh-1832
  • Loading branch information...
hzoo committed Oct 2, 2015
1 parent 94f175e commit bcfaa5192b09391bdec31adecab14d3861817c8a
@@ -58,21 +58,22 @@ module.exports.prototype = {
if (node.params.length !== 1) {
return;
}
var firstParam = node.params[0];
// Old Esprima
var hasDefaultParameter = node.defaults && node.defaults.length === 1;
// ESTree
var hasDefaultParameterESTree = node.params[0].type === 'AssignmentPattern';
var hasDestructuring = node.params[0].type === 'ObjectPattern';
var hasDefaultParameterESTree = firstParam.type === 'AssignmentPattern';
var hasDestructuring = firstParam.type === 'ObjectPattern' || firstParam.type === 'ArrayPattern';
var hasRestElement = firstParam.type === 'RestElement';
if (hasDefaultParameter ||
hasDefaultParameterESTree ||
hasDefaultParameter ||
hasDestructuring) {
hasDestructuring ||
hasRestElement) {
return;
}
var firstParam = node.params[0];
if (isWrapped(firstParam)) {
errors.add(
'Illegal wrap of arrow function expressions in parentheses',
@@ -48,9 +48,6 @@ module.exports.prototype = {
check: function(file, errors) {
function isWrapped(node) {
var openParensToken = file.getPrevToken(file.getFirstNodeToken(node));
if (openParensToken.value === '...') {
openParensToken = file.getPrevToken(openParensToken);
}
var closingParensToken = file.getNextToken(file.getLastNodeToken(node));
var closingTokenValue = closingParensToken ? closingParensToken.value : '';
@@ -41,4 +41,12 @@ describe('rules/disallow-parentheses-around-arrow-param', function() {
it('should not error with no params #1747', function() {
assert(checker.checkString('() => {};').isEmpty());
});
it('should not report an arrow function expression with a single rest param #1616, #1831', function() {
assert(checker.checkString('[1, 2].map((...x) => x);').isEmpty());
});
it('should not report an arrow function expression with a single array param #1831', function() {
assert(checker.checkString('[1, 2].map(([x]) => x);').isEmpty());
});
});

0 comments on commit bcfaa51

Please sign in to comment.