-
-
Notifications
You must be signed in to change notification settings - Fork 153
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
1 parent
9b37c35
commit 94a9e8b
Showing
8 changed files
with
531 additions
and
2 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
### `arrow-parens` | ||
|
||
_The `--fix` option on the command line automatically fixes problems reported by this rule._ | ||
|
||
Enforces the consistent use of parentheses in arrow functions. | ||
|
||
This rule has a string option and an object one. | ||
|
||
String options are: | ||
|
||
- `"always"` (default) requires parens around arguments in all cases. | ||
- `"as-needed"` enforces no braces where they can be omitted. | ||
|
||
Object properties for variants of the `"as-needed"` option: | ||
|
||
- `"requireForBlockBody": true` modifies the as-needed rule in order to require parens if the function body is in an instructions block (surrounded by braces). | ||
|
||
<!-- assertions arrowParens --> |
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,169 @@ | ||
const getLocation = (node) => { | ||
return { | ||
end: node.params[node.params.length - 1].loc.end, | ||
start: node.params[0].loc.start | ||
}; | ||
}; | ||
|
||
const isOpeningParenToken = (token) => { | ||
return token.value === '(' && token.type === 'Punctuator'; | ||
}; | ||
|
||
const isClosingParenToken = (token) => { | ||
return token.value === ')' && token.type === 'Punctuator'; | ||
}; | ||
|
||
export default { | ||
create (context) { | ||
const asNeeded = context.options[0] === 'as-needed'; | ||
const requireForBlockBody = asNeeded && context.options[1] && context.options[1].requireForBlockBody === true; | ||
|
||
const sourceCode = context.getSourceCode(); | ||
|
||
// Determines whether a arrow function argument end with `)` | ||
// eslint-disable-next-line complexity | ||
const parens = (node) => { | ||
const isAsync = node.async; | ||
const firstTokenOfParam = sourceCode.getFirstToken(node, isAsync ? 1 : 0); | ||
|
||
// Remove the parenthesis around a parameter | ||
const fixParamsWithParenthesis = (fixer) => { | ||
const paramToken = sourceCode.getTokenAfter(firstTokenOfParam); | ||
|
||
/* | ||
* ES8 allows Trailing commas in function parameter lists and calls | ||
* https://github.com/eslint/eslint/issues/8834 | ||
*/ | ||
const closingParenToken = sourceCode.getTokenAfter(paramToken, isClosingParenToken); | ||
const asyncToken = isAsync ? sourceCode.getTokenBefore(firstTokenOfParam) : null; | ||
const shouldAddSpaceForAsync = asyncToken && asyncToken.range[1] === firstTokenOfParam.range[0]; | ||
|
||
return fixer.replaceTextRange([ | ||
firstTokenOfParam.range[0], | ||
closingParenToken.range[1] | ||
], `${shouldAddSpaceForAsync ? ' ' : ''}${paramToken.value}`); | ||
}; | ||
|
||
// Type parameters without an opening paren is always a parse error, and | ||
// can therefore be safely ignored. | ||
if (node.typeParameters) { | ||
return; | ||
} | ||
|
||
// "as-needed", { "requireForBlockBody": true }: x => x | ||
if ( | ||
requireForBlockBody && | ||
node.params.length === 1 && | ||
node.params[0].type === 'Identifier' && | ||
!node.params[0].typeAnnotation && | ||
node.body.type !== 'BlockStatement' && | ||
!node.returnType | ||
) { | ||
if (isOpeningParenToken(firstTokenOfParam)) { | ||
context.report({ | ||
fix: fixParamsWithParenthesis, | ||
loc: getLocation(node), | ||
messageId: 'unexpectedParensInline', | ||
node | ||
}); | ||
} | ||
|
||
return; | ||
} | ||
|
||
if ( | ||
requireForBlockBody && | ||
node.body.type === 'BlockStatement' | ||
) { | ||
if (!isOpeningParenToken(firstTokenOfParam)) { | ||
context.report({ | ||
fix (fixer) { | ||
return fixer.replaceText(firstTokenOfParam, `(${firstTokenOfParam.value})`); | ||
}, | ||
loc: getLocation(node), | ||
messageId: 'expectedParensBlock', | ||
node | ||
}); | ||
} | ||
|
||
return; | ||
} | ||
|
||
// "as-needed": x => x | ||
if (asNeeded && | ||
node.params.length === 1 && | ||
node.params[0].type === 'Identifier' && | ||
!node.params[0].typeAnnotation && | ||
!node.returnType | ||
) { | ||
if (isOpeningParenToken(firstTokenOfParam)) { | ||
context.report({ | ||
fix: fixParamsWithParenthesis, | ||
loc: getLocation(node), | ||
messageId: 'unexpectedParens', | ||
node | ||
}); | ||
} | ||
|
||
return; | ||
} | ||
|
||
if (firstTokenOfParam.type === 'Identifier') { | ||
const after = sourceCode.getTokenAfter(firstTokenOfParam); | ||
|
||
// (x) => x | ||
if (after.value !== ')') { | ||
context.report({ | ||
fix (fixer) { | ||
return fixer.replaceText(firstTokenOfParam, `(${firstTokenOfParam.value})`); | ||
}, | ||
loc: getLocation(node), | ||
messageId: 'expectedParens', | ||
node | ||
}); | ||
} | ||
} | ||
}; | ||
|
||
return { | ||
ArrowFunctionExpression: parens | ||
}; | ||
}, | ||
|
||
meta: { | ||
docs: { | ||
category: 'ECMAScript 6', | ||
description: 'require parentheses around arrow function arguments', | ||
recommended: false, | ||
url: 'https://eslint.org/docs/rules/arrow-parens' | ||
}, | ||
|
||
fixable: 'code', | ||
|
||
messages: { | ||
expectedParens: 'Expected parentheses around arrow function argument.', | ||
expectedParensBlock: 'Expected parentheses around arrow function argument having a body with curly braces.', | ||
|
||
unexpectedParens: 'Unexpected parentheses around single function argument.', | ||
unexpectedParensInline: 'Unexpected parentheses around single function argument having a body with no curly braces.' | ||
}, | ||
|
||
type: 'layout' | ||
}, | ||
|
||
schema: [ | ||
{ | ||
enum: ['always', 'as-needed'] | ||
}, | ||
{ | ||
additionalProperties: false, | ||
properties: { | ||
requireForBlockBody: { | ||
default: false, | ||
type: 'boolean' | ||
} | ||
}, | ||
type: 'object' | ||
} | ||
] | ||
}; |
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
Oops, something went wrong.