Skip to content

Commit ced6591

Browse files
authored
fix(eslint-plugin): [space-before-function-paren] incorrect handling of abstract methods (typescript-eslint#2275)
Fixes typescript-eslint#2274
1 parent 2d80c51 commit ced6591

File tree

2 files changed

+24
-7
lines changed

2 files changed

+24
-7
lines changed

packages/eslint-plugin/src/rules/space-before-function-paren.ts

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,10 @@ export default util.createRule<Options, MessageIds>({
7676
| TSESTree.ArrowFunctionExpression
7777
| TSESTree.FunctionDeclaration
7878
| TSESTree.FunctionExpression
79-
| TSESTree.TSAbstractMethodDefinition,
79+
| TSESTree.TSEmptyBodyFunctionExpression
80+
| TSESTree.TSDeclareFunction,
8081
): boolean {
81-
if ('id' in node && node.id != null) {
82+
if (node.id != null) {
8283
return true;
8384
}
8485

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

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

@@ -133,7 +135,8 @@ export default util.createRule<Options, MessageIds>({
133135
| TSESTree.ArrowFunctionExpression
134136
| TSESTree.FunctionDeclaration
135137
| TSESTree.FunctionExpression
136-
| TSESTree.TSAbstractMethodDefinition,
138+
| TSESTree.TSEmptyBodyFunctionExpression
139+
| TSESTree.TSDeclareFunction,
137140
): void {
138141
const functionConfig = getConfigForFunction(node);
139142

@@ -165,7 +168,7 @@ export default util.createRule<Options, MessageIds>({
165168
} else if (
166169
!hasSpacing &&
167170
functionConfig === 'always' &&
168-
(!node.typeParameters || ('id' in node && node != null))
171+
(!node.typeParameters || node.id)
169172
) {
170173
context.report({
171174
node,
@@ -180,7 +183,8 @@ export default util.createRule<Options, MessageIds>({
180183
ArrowFunctionExpression: checkFunction,
181184
FunctionDeclaration: checkFunction,
182185
FunctionExpression: checkFunction,
183-
TSAbstractMethodDefinition: checkFunction,
186+
TSEmptyBodyFunctionExpression: checkFunction,
187+
TSDeclareFunction: checkFunction,
184188
};
185189
},
186190
});

packages/eslint-plugin/tests/rules/space-before-function-paren.test.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,19 @@ ruleTester.run('space-before-function-paren', rule, {
153153
code: 'abstract class Foo { constructor() {} abstract method() }',
154154
options: ['never'],
155155
},
156+
{
157+
code: 'abstract class Foo { constructor() {} abstract method() }',
158+
options: [{ anonymous: 'always', named: 'never' }],
159+
},
160+
'function foo ();',
161+
{
162+
code: 'function foo();',
163+
options: ['never'],
164+
},
165+
{
166+
code: 'function foo();',
167+
options: [{ anonymous: 'always', named: 'never' }],
168+
},
156169
],
157170

158171
invalid: [

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy