Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(eslint-plugin): add space-before-function-paren [extension] (#924)
- Loading branch information
1 parent
ca41dcf
commit d8b07a7
Showing
7 changed files
with
805 additions
and
0 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
42 changes: 42 additions & 0 deletions
42
packages/eslint-plugin/docs/rules/space-before-function-paren.md
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 @@ | ||
# Require or disallow a space before function parenthesis (space-before-function-paren) | ||
|
||
When formatting a function, whitespace is allowed between the function name or `function` keyword and the opening paren. Named functions also require a space between the `function` keyword and the function name, but anonymous functions require no whitespace. For example: | ||
|
||
<!-- prettier-ignore --> | ||
```ts | ||
function withoutSpace (x) { | ||
// ... | ||
} | ||
|
||
function withSpace (x) { | ||
// ... | ||
} | ||
|
||
var anonymousWithoutSpace = function () {}; | ||
|
||
var anonymousWithSpace = function () {}; | ||
``` | ||
|
||
Style guides may require a space after the `function` keyword for anonymous functions, while others specify no whitespace. Similarly, the space after a function name may or may not be required. | ||
|
||
## Rule Details | ||
|
||
This rule extends the base [eslint/func-call-spacing](https://eslint.org/docs/rules/space-before-function-paren) rule. | ||
It supports all options and features of the base rule. | ||
This version adds support for generic type parameters on function calls. | ||
|
||
## How to use | ||
|
||
```cjson | ||
{ | ||
// note you must disable the base rule as it can report incorrect errors | ||
"space-before-function-paren": "off", | ||
"@typescript-eslint/space-before-function-paren": ["error"] | ||
} | ||
``` | ||
|
||
## Options | ||
|
||
See [eslint/space-before-function-paren options](https://eslint.org/docs/rules/space-before-function-paren#options). | ||
|
||
<sup>Taken with ❤️ [from ESLint core](https://github.com/eslint/eslint/blob/master/docs/rules/space-before-function-paren.md)</sup> |
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
180 changes: 180 additions & 0 deletions
180
packages/eslint-plugin/src/rules/space-before-function-paren.ts
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,180 @@ | ||
import { | ||
TSESTree, | ||
AST_NODE_TYPES, | ||
} from '@typescript-eslint/experimental-utils'; | ||
import { isOpeningParenToken } from 'eslint-utils'; | ||
import * as util from '../util'; | ||
|
||
type Option = 'never' | 'always'; | ||
type FuncOption = Option | 'ignore'; | ||
|
||
export type Options = [ | ||
|
||
| Option | ||
| Partial<{ | ||
anonymous: FuncOption; | ||
named: FuncOption; | ||
asyncArrow: FuncOption; | ||
}>, | ||
]; | ||
export type MessageIds = 'unexpected' | 'missing'; | ||
|
||
export default util.createRule<Options, MessageIds>({ | ||
name: 'space-before-function-paren', | ||
meta: { | ||
type: 'layout', | ||
docs: { | ||
description: | ||
'enforce consistent spacing before `function` definition opening parenthesis', | ||
category: 'Stylistic Issues', | ||
recommended: false, | ||
}, | ||
fixable: 'whitespace', | ||
schema: [ | ||
{ | ||
oneOf: [ | ||
{ | ||
enum: ['always', 'never'], | ||
}, | ||
{ | ||
type: 'object', | ||
properties: { | ||
anonymous: { | ||
enum: ['always', 'never', 'ignore'], | ||
}, | ||
named: { | ||
enum: ['always', 'never', 'ignore'], | ||
}, | ||
asyncArrow: { | ||
enum: ['always', 'never', 'ignore'], | ||
}, | ||
}, | ||
additionalProperties: false, | ||
}, | ||
], | ||
}, | ||
], | ||
messages: { | ||
unexpected: 'Unexpected space before function parentheses.', | ||
missing: 'Missing space before function parentheses.', | ||
}, | ||
}, | ||
defaultOptions: ['always'], | ||
|
||
create(context) { | ||
const sourceCode = context.getSourceCode(); | ||
const baseConfig = | ||
typeof context.options[0] === 'string' ? context.options[0] : 'always'; | ||
const overrideConfig = | ||
typeof context.options[0] === 'object' ? context.options[0] : {}; | ||
|
||
/** | ||
* Determines whether a function has a name. | ||
* @param {ASTNode} node The function node. | ||
* @returns {boolean} Whether the function has a name. | ||
*/ | ||
function isNamedFunction( | ||
node: | ||
| TSESTree.ArrowFunctionExpression | ||
| TSESTree.FunctionDeclaration | ||
| TSESTree.FunctionExpression, | ||
): boolean { | ||
if (node.id) { | ||
return true; | ||
} | ||
|
||
const parent = node.parent!; | ||
|
||
return ( | ||
parent.type === 'MethodDefinition' || | ||
(parent.type === 'Property' && | ||
(parent.kind === 'get' || parent.kind === 'set' || parent.method)) | ||
); | ||
} | ||
|
||
/** | ||
* Gets the config for a given function | ||
* @param {ASTNode} node The function node | ||
* @returns {string} "always", "never", or "ignore" | ||
*/ | ||
function getConfigForFunction( | ||
node: | ||
| TSESTree.ArrowFunctionExpression | ||
| TSESTree.FunctionDeclaration | ||
| TSESTree.FunctionExpression, | ||
): FuncOption { | ||
if (node.type === AST_NODE_TYPES.ArrowFunctionExpression) { | ||
// Always ignore non-async functions and arrow functions without parens, e.g. async foo => bar | ||
if ( | ||
node.async && | ||
isOpeningParenToken(sourceCode.getFirstToken(node, { skip: 1 })!) | ||
) { | ||
return overrideConfig.asyncArrow || baseConfig; | ||
} | ||
} else if (isNamedFunction(node)) { | ||
return overrideConfig.named || baseConfig; | ||
|
||
// `generator-star-spacing` should warn anonymous generators. E.g. `function* () {}` | ||
} else if (!node.generator) { | ||
return overrideConfig.anonymous || baseConfig; | ||
} | ||
|
||
return 'ignore'; | ||
} | ||
|
||
/** | ||
* Checks the parens of a function node | ||
* @param {ASTNode} node A function node | ||
* @returns {void} | ||
*/ | ||
function checkFunction( | ||
node: | ||
| TSESTree.ArrowFunctionExpression | ||
| TSESTree.FunctionDeclaration | ||
| TSESTree.FunctionExpression, | ||
): void { | ||
const functionConfig = getConfigForFunction(node); | ||
|
||
if (functionConfig === 'ignore') { | ||
return; | ||
} | ||
|
||
let leftToken: TSESTree.Token, rightToken: TSESTree.Token; | ||
if (node.typeParameters) { | ||
leftToken = sourceCode.getLastToken(node.typeParameters)!; | ||
rightToken = sourceCode.getTokenAfter(leftToken)!; | ||
} else { | ||
rightToken = sourceCode.getFirstToken(node, isOpeningParenToken)!; | ||
leftToken = sourceCode.getTokenBefore(rightToken)!; | ||
} | ||
const hasSpacing = sourceCode.isSpaceBetweenTokens(leftToken, rightToken); | ||
|
||
if (hasSpacing && functionConfig === 'never') { | ||
context.report({ | ||
node, | ||
loc: leftToken.loc.end, | ||
messageId: 'unexpected', | ||
fix: fixer => | ||
fixer.removeRange([leftToken.range[1], rightToken.range[0]]), | ||
}); | ||
} else if ( | ||
!hasSpacing && | ||
functionConfig === 'always' && | ||
(!node.typeParameters || node.id) | ||
) { | ||
context.report({ | ||
node, | ||
loc: leftToken.loc.end, | ||
messageId: 'missing', | ||
fix: fixer => fixer.insertTextAfter(leftToken, ' '), | ||
}); | ||
} | ||
} | ||
|
||
return { | ||
ArrowFunctionExpression: checkFunction, | ||
FunctionDeclaration: checkFunction, | ||
FunctionExpression: checkFunction, | ||
}; | ||
}, | ||
}); |
Oops, something went wrong.