feat(generic-spacing): remove space before generic
This commit is contained in:
parent
254f8194df
commit
e111029e0a
@ -4,17 +4,21 @@ import rule, { RULE_NAME } from './generic-spacing'
|
|||||||
|
|
||||||
const valids = [
|
const valids = [
|
||||||
'type Foo<T = true> = T',
|
'type Foo<T = true> = T',
|
||||||
|
'type Foo<T extends true = true> = T',
|
||||||
`
|
`
|
||||||
type Foo<
|
type Foo<
|
||||||
T = true,
|
T = true,
|
||||||
K = false
|
K = false
|
||||||
> = T`,
|
> = T`,
|
||||||
'type Foo<T extends true = true> = T',
|
`function foo<
|
||||||
|
T
|
||||||
|
>() {}`,
|
||||||
]
|
]
|
||||||
const invalids = [
|
const invalids = [
|
||||||
['type Foo<T=true> = T', 'type Foo<T = true> = T'],
|
['type Foo<T=true> = T', 'type Foo<T = true> = T'],
|
||||||
['type Foo<T,K> = T', 'type Foo<T, K> = 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],
|
['type Foo<T=false,K=1|2> = T', 'type Foo<T = false, K = 1|2> = T', 3],
|
||||||
|
['function foo <T>() {}', 'function foo<T>() {}'],
|
||||||
] as const
|
] as const
|
||||||
|
|
||||||
it('runs', () => {
|
it('runs', () => {
|
||||||
|
@ -23,6 +23,20 @@ export default createEslintRule<Options, MessageIds>({
|
|||||||
const sourceCode = context.getSourceCode()
|
const sourceCode = context.getSourceCode()
|
||||||
return {
|
return {
|
||||||
TSTypeParameterDeclaration: (node) => {
|
TSTypeParameterDeclaration: (node) => {
|
||||||
|
const pre = sourceCode.text.slice(0, node.range[0])
|
||||||
|
const preSpace = pre.match(/(\s+)$/)?.[0]
|
||||||
|
// strip space before <T>
|
||||||
|
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 <T,K>
|
||||||
const params = node.params
|
const params = node.params
|
||||||
for (let i = 1; i < params.length; i++) {
|
for (let i = 1; i < params.length; i++) {
|
||||||
const prev = params[i - 1]
|
const prev = params[i - 1]
|
||||||
@ -45,6 +59,7 @@ export default createEslintRule<Options, MessageIds>({
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
// add space around = in type Foo<T = true>
|
||||||
TSTypeParameter: (node) => {
|
TSTypeParameter: (node) => {
|
||||||
if (!node.default)
|
if (!node.default)
|
||||||
return
|
return
|
||||||
|
Loading…
Reference in New Issue
Block a user