Skip to content

Commit

Permalink
fix(eslint-plugin): [space-before-function-paren] incorrect handling …
Browse files Browse the repository at this point in the history
…of abstract methods (#2275)

Fixes #2274
  • Loading branch information
bradzacher authored Jul 6, 2020
1 parent 2d80c51 commit ced6591
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 7 deletions.
18 changes: 11 additions & 7 deletions packages/eslint-plugin/src/rules/space-before-function-paren.ts
Original file line number Diff line number Diff line change
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,
};
},
});
Original file line number Diff line number Diff line change
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

0 comments on commit ced6591

Please sign in to comment.