From 88a3edfce8349f871b7b660d2b76508b67c94eda Mon Sep 17 00:00:00 2001 From: Adriel Codeco Date: Mon, 6 Jul 2020 13:59:27 -0300 Subject: [PATCH] fix(eslint-plugin): [space-before-function-paren] handle abstract functions (#2199) Co-authored-by: Brad Zacher --- .../src/rules/space-before-function-paren.ts | 17 +++++++++++------ .../rules/space-before-function-paren.test.ts | 5 +++++ 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/packages/eslint-plugin/src/rules/space-before-function-paren.ts b/packages/eslint-plugin/src/rules/space-before-function-paren.ts index 41a817938da..f06327e2dd5 100644 --- a/packages/eslint-plugin/src/rules/space-before-function-paren.ts +++ b/packages/eslint-plugin/src/rules/space-before-function-paren.ts @@ -75,9 +75,10 @@ export default util.createRule({ node: | TSESTree.ArrowFunctionExpression | TSESTree.FunctionDeclaration - | TSESTree.FunctionExpression, + | TSESTree.FunctionExpression + | TSESTree.TSAbstractMethodDefinition, ): boolean { - if (node.id) { + if ('id' in node && node.id != null) { return true; } @@ -85,6 +86,7 @@ export default util.createRule({ 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)) ); @@ -99,7 +101,8 @@ export default util.createRule({ 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 @@ -113,7 +116,7 @@ export default util.createRule({ 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; } @@ -129,7 +132,8 @@ export default util.createRule({ node: | TSESTree.ArrowFunctionExpression | TSESTree.FunctionDeclaration - | TSESTree.FunctionExpression, + | TSESTree.FunctionExpression + | TSESTree.TSAbstractMethodDefinition, ): void { const functionConfig = getConfigForFunction(node); @@ -161,7 +165,7 @@ export default util.createRule({ } else if ( !hasSpacing && functionConfig === 'always' && - (!node.typeParameters || node.id) + (!node.typeParameters || ('id' in node && node != null)) ) { context.report({ node, @@ -176,6 +180,7 @@ export default util.createRule({ ArrowFunctionExpression: checkFunction, FunctionDeclaration: checkFunction, FunctionExpression: checkFunction, + TSAbstractMethodDefinition: checkFunction, }; }, }); diff --git a/packages/eslint-plugin/tests/rules/space-before-function-paren.test.ts b/packages/eslint-plugin/tests/rules/space-before-function-paren.test.ts index 944ea672f1f..5de3b0e6378 100644 --- a/packages/eslint-plugin/tests/rules/space-before-function-paren.test.ts +++ b/packages/eslint-plugin/tests/rules/space-before-function-paren.test.ts @@ -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: [