Skip to content

Commit 88a3edf

Browse files
fix(eslint-plugin): [space-before-function-paren] handle abstract functions (typescript-eslint#2199)
Co-authored-by: Brad Zacher <brad.zacher@gmail.com>
1 parent fe2b2ec commit 88a3edf

File tree

2 files changed

+16
-6
lines changed

2 files changed

+16
-6
lines changed

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

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -75,16 +75,18 @@ export default util.createRule<Options, MessageIds>({
7575
node:
7676
| TSESTree.ArrowFunctionExpression
7777
| TSESTree.FunctionDeclaration
78-
| TSESTree.FunctionExpression,
78+
| TSESTree.FunctionExpression
79+
| TSESTree.TSAbstractMethodDefinition,
7980
): boolean {
80-
if (node.id) {
81+
if ('id' in node && node.id != null) {
8182
return true;
8283
}
8384

8485
const parent = node.parent!;
8586

8687
return (
8788
parent.type === AST_NODE_TYPES.MethodDefinition ||
89+
parent.type === AST_NODE_TYPES.TSAbstractMethodDefinition ||
8890
(parent.type === AST_NODE_TYPES.Property &&
8991
(parent.kind === 'get' || parent.kind === 'set' || parent.method))
9092
);
@@ -99,7 +101,8 @@ export default util.createRule<Options, MessageIds>({
99101
node:
100102
| TSESTree.ArrowFunctionExpression
101103
| TSESTree.FunctionDeclaration
102-
| TSESTree.FunctionExpression,
104+
| TSESTree.FunctionExpression
105+
| TSESTree.TSAbstractMethodDefinition,
103106
): FuncOption {
104107
if (node.type === AST_NODE_TYPES.ArrowFunctionExpression) {
105108
// Always ignore non-async functions and arrow functions without parens, e.g. async foo => bar
@@ -113,7 +116,7 @@ export default util.createRule<Options, MessageIds>({
113116
return overrideConfig.named ?? baseConfig;
114117

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

@@ -129,7 +132,8 @@ export default util.createRule<Options, MessageIds>({
129132
node:
130133
| TSESTree.ArrowFunctionExpression
131134
| TSESTree.FunctionDeclaration
132-
| TSESTree.FunctionExpression,
135+
| TSESTree.FunctionExpression
136+
| TSESTree.TSAbstractMethodDefinition,
133137
): void {
134138
const functionConfig = getConfigForFunction(node);
135139

@@ -161,7 +165,7 @@ export default util.createRule<Options, MessageIds>({
161165
} else if (
162166
!hasSpacing &&
163167
functionConfig === 'always' &&
164-
(!node.typeParameters || node.id)
168+
(!node.typeParameters || ('id' in node && node != null))
165169
) {
166170
context.report({
167171
node,
@@ -176,6 +180,7 @@ export default util.createRule<Options, MessageIds>({
176180
ArrowFunctionExpression: checkFunction,
177181
FunctionDeclaration: checkFunction,
178182
FunctionExpression: checkFunction,
183+
TSAbstractMethodDefinition: checkFunction,
179184
};
180185
},
181186
});

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,11 @@ ruleTester.run('space-before-function-paren', rule, {
148148
options: ['never'],
149149
parserOptions: { ecmaVersion: 8 },
150150
},
151+
'abstract class Foo { constructor () {} abstract method () }',
152+
{
153+
code: 'abstract class Foo { constructor() {} abstract method() }',
154+
options: ['never'],
155+
},
151156
],
152157

153158
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