From ff6a1fad6a4f87c0f726e59c3c35f00805cde331 Mon Sep 17 00:00:00 2001 From: Anthony Fu Date: Wed, 2 Nov 2022 17:21:24 +0800 Subject: [PATCH] fix(generic-spacing): improve cases --- .../src/rules/generic-spacing.test.ts | 12 ++++++++++ .../src/rules/generic-spacing.ts | 24 ++++++++++--------- 2 files changed, 25 insertions(+), 11 deletions(-) diff --git a/packages/eslint-plugin-antfu/src/rules/generic-spacing.test.ts b/packages/eslint-plugin-antfu/src/rules/generic-spacing.test.ts index ec82cd2b0f..dc98507fa6 100644 --- a/packages/eslint-plugin-antfu/src/rules/generic-spacing.test.ts +++ b/packages/eslint-plugin-antfu/src/rules/generic-spacing.test.ts @@ -13,12 +13,24 @@ type Foo< `function foo< T >() {}`, + 'const foo = (name: T) => name', + `interface Log { + foo(name: T): void + }`, + `interface Log { + (name: T): void +}`, ] const invalids = [ ['type Foo = T', 'type Foo = T'], ['type Foo = T', 'type Foo = T'], ['type Foo = T', 'type Foo = T', 3], ['function foo () {}', 'function foo() {}'], + [`interface Log { + foo (name: T): void +}`, `interface Log { + foo(name: T): void +}`], ] as const it('runs', () => { diff --git a/packages/eslint-plugin-antfu/src/rules/generic-spacing.ts b/packages/eslint-plugin-antfu/src/rules/generic-spacing.ts index 15a06589e0..280d3e7181 100644 --- a/packages/eslint-plugin-antfu/src/rules/generic-spacing.ts +++ b/packages/eslint-plugin-antfu/src/rules/generic-spacing.ts @@ -23,17 +23,19 @@ export default createEslintRule({ const sourceCode = context.getSourceCode() return { TSTypeParameterDeclaration: (node) => { - const pre = sourceCode.text.slice(0, node.range[0]) - const preSpace = pre.match(/(\s+)$/)?.[0] - // strip space before - if (preSpace && preSpace.length) { - context.report({ - node, - messageId: 'genericSpacingMismatch', - *fix(fixer) { - yield fixer.replaceTextRange([node.range[0] - preSpace.length, node.range[0]], '') - }, - }) + if (!['TSCallSignatureDeclaration', 'ArrowFunctionExpression'].includes(node.parent.type)) { + const pre = sourceCode.text.slice(0, node.range[0]) + const preSpace = pre.match(/(\s+)$/)?.[0] + // strip space before + if (preSpace && preSpace.length) { + context.report({ + node, + messageId: 'genericSpacingMismatch', + *fix(fixer) { + yield fixer.replaceTextRange([node.range[0] - preSpace.length, node.range[0]], '') + }, + }) + } } // add space between