Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
117 additions
and
71 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
packages/eslint-plugin-antfu/src/rules/generic-spacing.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { RuleTester } from '@typescript-eslint/utils/dist/ts-eslint' | ||
import { it } from 'vitest' | ||
import rule, { RULE_NAME } from './generic-spacing' | ||
|
||
const valids = [ | ||
'type Foo<T = true> = T', | ||
` | ||
type Foo< | ||
T = true, | ||
K = false | ||
> = T`, | ||
] | ||
const invalids = [ | ||
['type Foo<T=true> = T', 'type Foo<T = true> = T'], | ||
['type Foo<T,K> = T', 'type Foo<T, K> = T'], | ||
['type Foo<T=false,K=1|2> = T', 'type Foo<T = false, K = 1|2> = T', 3], | ||
] as const | ||
|
||
it('runs', () => { | ||
const ruleTester: RuleTester = new RuleTester({ | ||
parser: require.resolve('@typescript-eslint/parser'), | ||
}) | ||
|
||
ruleTester.run(RULE_NAME, rule, { | ||
valid: valids, | ||
invalid: invalids.map(i => ({ | ||
code: i[0], | ||
output: i[1].trim(), | ||
errors: Array.from({ length: i[2] || 1 }, () => ({ messageId: 'genericSpacingMismatch' })), | ||
})), | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import { createEslintRule } from '../utils' | ||
|
||
export const RULE_NAME = 'generic-spacing' | ||
export type MessageIds = 'genericSpacingMismatch' | ||
export type Options = [] | ||
|
||
export default createEslintRule<Options, MessageIds>({ | ||
name: RULE_NAME, | ||
meta: { | ||
type: 'suggestion', | ||
docs: { | ||
description: 'Spaces around generic type parameters', | ||
recommended: 'error', | ||
}, | ||
fixable: 'code', | ||
schema: [], | ||
messages: { | ||
genericSpacingMismatch: 'Generic spaces mismatch', | ||
}, | ||
}, | ||
defaultOptions: [], | ||
create: (context) => { | ||
const sourceCode = context.getSourceCode() | ||
return { | ||
TSTypeParameterDeclaration: (node) => { | ||
const params = node.params | ||
for (let i = 1; i < params.length; i++) { | ||
const prev = params[i - 1] | ||
const current = params[i] | ||
const from = prev.range[1] | ||
const to = current.range[0] | ||
const span = sourceCode.text.slice(from, to) | ||
if (span !== ', ' && !span.match(/,\n/)) { | ||
context.report({ | ||
*fix(fixer) { | ||
yield fixer.replaceTextRange([from, to], ', ') | ||
}, | ||
loc: { | ||
start: prev.loc.end, | ||
end: current.loc.start, | ||
}, | ||
messageId: 'genericSpacingMismatch', | ||
node, | ||
}) | ||
} | ||
} | ||
}, | ||
TSTypeParameter: (node) => { | ||
if (!node.default) | ||
return | ||
const from = node.name.range[1] | ||
const to = node.default.range[0] | ||
if (sourceCode.text.slice(from, to) !== ' = ') { | ||
context.report({ | ||
*fix(fixer) { | ||
yield fixer.replaceTextRange([from, to], ' = ') | ||
}, | ||
loc: { | ||
start: node.name.loc.end, | ||
end: node.default.loc.start, | ||
}, | ||
messageId: 'genericSpacingMismatch', | ||
node, | ||
}) | ||
} | ||
}, | ||
} | ||
}, | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.