Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(eslint-plugin): [space-before-function-paren] handle abstract functions #2199

Merged
merged 7 commits into from Jul 6, 2020
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