From 4156b0ad5c9d7d522c825ffafde52ed9519c9029 Mon Sep 17 00:00:00 2001 From: Brad Zacher Date: Mon, 6 Jul 2020 10:32:27 -0700 Subject: [PATCH] fix(eslint-plugin): [space-before-function-paren] incorrect handling of abstract functions Fixes #2274 --- .../src/rules/space-before-function-paren.ts | 18 +++++++++++------- .../rules/space-before-function-paren.test.ts | 13 +++++++++++++ 2 files changed, 24 insertions(+), 7 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 f06327e2dd59..e2ccfa5182db 100644 --- a/packages/eslint-plugin/src/rules/space-before-function-paren.ts +++ b/packages/eslint-plugin/src/rules/space-before-function-paren.ts @@ -76,9 +76,10 @@ export default util.createRule({ | 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; } @@ -102,7 +103,8 @@ export default util.createRule({ | 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 @@ -116,7 +118,7 @@ export default util.createRule({ 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; } @@ -133,7 +135,8 @@ export default util.createRule({ | TSESTree.ArrowFunctionExpression | TSESTree.FunctionDeclaration | TSESTree.FunctionExpression - | TSESTree.TSAbstractMethodDefinition, + | TSESTree.TSEmptyBodyFunctionExpression + | TSESTree.TSDeclareFunction, ): void { const functionConfig = getConfigForFunction(node); @@ -165,7 +168,7 @@ export default util.createRule({ } else if ( !hasSpacing && functionConfig === 'always' && - (!node.typeParameters || ('id' in node && node != null)) + (!node.typeParameters || node.id) ) { context.report({ node, @@ -180,7 +183,8 @@ export default util.createRule({ ArrowFunctionExpression: checkFunction, FunctionDeclaration: checkFunction, FunctionExpression: checkFunction, - TSAbstractMethodDefinition: checkFunction, + TSEmptyBodyFunctionExpression: checkFunction, + TSDeclareFunction: 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 5de3b0e6378d..13c68b88bc79 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 @@ -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: [ 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