Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(eslint-plugin): [space-before-function-paren] incorrect handling of abstract methods #2275

Merged
merged 1 commit into from Jul 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 11 additions & 7 deletions packages/eslint-plugin/src/rules/space-before-function-paren.ts
Expand Up @@ -76,9 +76,10 @@ export default util.createRule<Options, MessageIds>({
| TSESTree.ArrowFunctionExpression
| TSESTree.FunctionDeclaration
| TSESTree.FunctionExpression
| TSESTree.TSAbstractMethodDefinition,
| TSESTree.TSEmptyBodyFunctionExpression
| TSESTree.TSDeclareFunction,
): boolean {
if ('id' in node && node.id != null) {
if (node.id != null) {
return true;
}

Expand All @@ -102,7 +103,8 @@ export default util.createRule<Options, MessageIds>({
| TSESTree.ArrowFunctionExpression
| TSESTree.FunctionDeclaration
| TSESTree.FunctionExpression
| TSESTree.TSAbstractMethodDefinition,
| TSESTree.TSEmptyBodyFunctionExpression
| TSESTree.TSDeclareFunction,
): FuncOption {
if (node.type === AST_NODE_TYPES.ArrowFunctionExpression) {
// Always ignore non-async functions and arrow functions without parens, e.g. async foo => bar
Expand All @@ -116,7 +118,7 @@ export default util.createRule<Options, MessageIds>({
return overrideConfig.named ?? baseConfig;

// `generator-star-spacing` should warn anonymous generators. E.g. `function* () {}`
} else if (!('generator' in node) || node.generator === false) {
} else if (!node.generator) {
return overrideConfig.anonymous ?? baseConfig;
}

Expand All @@ -133,7 +135,8 @@ export default util.createRule<Options, MessageIds>({
| TSESTree.ArrowFunctionExpression
| TSESTree.FunctionDeclaration
| TSESTree.FunctionExpression
| TSESTree.TSAbstractMethodDefinition,
| TSESTree.TSEmptyBodyFunctionExpression
| TSESTree.TSDeclareFunction,
): void {
const functionConfig = getConfigForFunction(node);

Expand Down Expand Up @@ -165,7 +168,7 @@ export default util.createRule<Options, MessageIds>({
} else if (
!hasSpacing &&
functionConfig === 'always' &&
(!node.typeParameters || ('id' in node && node != null))
(!node.typeParameters || node.id)
) {
context.report({
node,
Expand All @@ -180,7 +183,8 @@ export default util.createRule<Options, MessageIds>({
ArrowFunctionExpression: checkFunction,
FunctionDeclaration: checkFunction,
FunctionExpression: checkFunction,
TSAbstractMethodDefinition: checkFunction,
TSEmptyBodyFunctionExpression: checkFunction,
TSDeclareFunction: checkFunction,
};
},
});
Expand Up @@ -153,6 +153,19 @@ ruleTester.run('space-before-function-paren', rule, {
code: 'abstract class Foo { constructor() {} abstract method() }',
options: ['never'],
},
{
code: 'abstract class Foo { constructor() {} abstract method() }',
options: [{ anonymous: 'always', named: 'never' }],
},
'function foo ();',
{
code: 'function foo();',
options: ['never'],
},
{
code: 'function foo();',
options: [{ anonymous: 'always', named: 'never' }],
},
],

invalid: [
Expand Down