Skip to content

Commit

Permalink
fix(eslint-plugin): [space-before-function-paren] handle abstract fun…
Browse files Browse the repository at this point in the history
…ctions (#2199)

Co-authored-by: Brad Zacher <brad.zacher@gmail.com>
  • Loading branch information
adrielcodeco and bradzacher committed Jul 6, 2020
1 parent fe2b2ec commit 88a3edf
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
17 changes: 11 additions & 6 deletions packages/eslint-plugin/src/rules/space-before-function-paren.ts
Expand Up @@ -75,16 +75,18 @@ export default util.createRule<Options, MessageIds>({
node:
| TSESTree.ArrowFunctionExpression
| TSESTree.FunctionDeclaration
| TSESTree.FunctionExpression,
| TSESTree.FunctionExpression
| TSESTree.TSAbstractMethodDefinition,
): boolean {
if (node.id) {
if ('id' in node && node.id != null) {
return true;
}

const parent = node.parent!;

return (
parent.type === AST_NODE_TYPES.MethodDefinition ||
parent.type === AST_NODE_TYPES.TSAbstractMethodDefinition ||
(parent.type === AST_NODE_TYPES.Property &&
(parent.kind === 'get' || parent.kind === 'set' || parent.method))
);
Expand All @@ -99,7 +101,8 @@ export default util.createRule<Options, MessageIds>({
node:
| TSESTree.ArrowFunctionExpression
| TSESTree.FunctionDeclaration
| TSESTree.FunctionExpression,
| TSESTree.FunctionExpression
| TSESTree.TSAbstractMethodDefinition,
): 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 @@ -113,7 +116,7 @@ export default util.createRule<Options, MessageIds>({
return overrideConfig.named ?? baseConfig;

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

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

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

invalid: [
Expand Down

0 comments on commit 88a3edf

Please sign in to comment.