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 01b7735..ec82cd2 100644 --- a/packages/eslint-plugin-antfu/src/rules/generic-spacing.test.ts +++ b/packages/eslint-plugin-antfu/src/rules/generic-spacing.test.ts @@ -4,17 +4,21 @@ import rule, { RULE_NAME } from './generic-spacing' const valids = [ 'type Foo = T', + 'type Foo = T', ` type Foo< T = true, K = false > = T`, - 'type Foo = T', + `function foo< + T +>() {}`, ] 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() {}'], ] 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 0345c3a..15a0658 100644 --- a/packages/eslint-plugin-antfu/src/rules/generic-spacing.ts +++ b/packages/eslint-plugin-antfu/src/rules/generic-spacing.ts @@ -23,6 +23,20 @@ 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]], '') + }, + }) + } + + // add space between const params = node.params for (let i = 1; i < params.length; i++) { const prev = params[i - 1] @@ -45,6 +59,7 @@ export default createEslintRule({ } } }, + // add space around = in type Foo TSTypeParameter: (node) => { if (!node.default) return