Compare commits
8 Commits
Author | SHA1 | Date | |
---|---|---|---|
|
f8014b745e | ||
|
95e28e4a4a | ||
|
54b234f4ef | ||
|
ff6a1fad6a | ||
|
e13613fd5f | ||
|
e111029e0a | ||
|
254f8194df | ||
|
4119f52750 |
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@antfu/eslint-config-monorepo",
|
"name": "@antfu/eslint-config-monorepo",
|
||||||
"version": "0.29.0",
|
"version": "0.29.4",
|
||||||
"private": true,
|
"private": true,
|
||||||
"packageManager": "pnpm@7.1.0",
|
"packageManager": "pnpm@7.1.0",
|
||||||
"author": "Anthony Fu <anthonyfu117@hotmail.com> (https://github.com/antfu/)",
|
"author": "Anthony Fu <anthonyfu117@hotmail.com> (https://github.com/antfu/)",
|
||||||
|
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@antfu/eslint-config",
|
"name": "@antfu/eslint-config",
|
||||||
"version": "0.29.0",
|
"version": "0.29.4",
|
||||||
"description": "Anthony's ESLint config",
|
"description": "Anthony's ESLint config",
|
||||||
"author": "Anthony Fu <anthonyfu117@hotmail.com> (https://github.com/antfu/)",
|
"author": "Anthony Fu <anthonyfu117@hotmail.com> (https://github.com/antfu/)",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
|
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@antfu/eslint-config-basic",
|
"name": "@antfu/eslint-config-basic",
|
||||||
"version": "0.29.0",
|
"version": "0.29.4",
|
||||||
"description": "",
|
"description": "",
|
||||||
"author": "Anthony Fu <anthonyfu117@hotmail.com> (https://github.com/antfu/)",
|
"author": "Anthony Fu <anthonyfu117@hotmail.com> (https://github.com/antfu/)",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
|
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "eslint-plugin-antfu",
|
"name": "eslint-plugin-antfu",
|
||||||
"version": "0.29.0",
|
"version": "0.29.4",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"homepage": "https://github.com/antfu/eslint-config",
|
"homepage": "https://github.com/antfu/eslint-config",
|
||||||
"main": "./dist/index.cjs",
|
"main": "./dist/index.cjs",
|
||||||
|
@@ -4,16 +4,36 @@ 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`,
|
||||||
|
`function foo<
|
||||||
|
T
|
||||||
|
>() {}`,
|
||||||
|
'const foo = <T>(name: T) => name',
|
||||||
|
`interface Log {
|
||||||
|
foo<T>(name: T): void
|
||||||
|
}`,
|
||||||
|
`interface Log {
|
||||||
|
<T>(name: T): void
|
||||||
|
}`,
|
||||||
|
`interface Foo {
|
||||||
|
foo?: <T>(name: T) => void
|
||||||
|
}`,
|
||||||
]
|
]
|
||||||
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>() {}'],
|
||||||
|
[`interface Log {
|
||||||
|
foo <T>(name: T): void
|
||||||
|
}`, `interface Log {
|
||||||
|
foo<T>(name: T): void
|
||||||
|
}`],
|
||||||
] as const
|
] as const
|
||||||
|
|
||||||
it('runs', () => {
|
it('runs', () => {
|
||||||
|
@@ -23,6 +23,22 @@ export default createEslintRule<Options, MessageIds>({
|
|||||||
const sourceCode = context.getSourceCode()
|
const sourceCode = context.getSourceCode()
|
||||||
return {
|
return {
|
||||||
TSTypeParameterDeclaration: (node) => {
|
TSTypeParameterDeclaration: (node) => {
|
||||||
|
if (!['TSCallSignatureDeclaration', 'ArrowFunctionExpression', 'TSFunctionType'].includes(node.parent.type)) {
|
||||||
|
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,10 +61,12 @@ 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
|
||||||
const from = node.name.range[1]
|
const endNode = node.constraint || node.name
|
||||||
|
const from = endNode.range[1]
|
||||||
const to = node.default.range[0]
|
const to = node.default.range[0]
|
||||||
if (sourceCode.text.slice(from, to) !== ' = ') {
|
if (sourceCode.text.slice(from, to) !== ' = ') {
|
||||||
context.report({
|
context.report({
|
||||||
@@ -56,7 +74,7 @@ export default createEslintRule<Options, MessageIds>({
|
|||||||
yield fixer.replaceTextRange([from, to], ' = ')
|
yield fixer.replaceTextRange([from, to], ' = ')
|
||||||
},
|
},
|
||||||
loc: {
|
loc: {
|
||||||
start: node.name.loc.end,
|
start: endNode.loc.end,
|
||||||
end: node.default.loc.start,
|
end: node.default.loc.start,
|
||||||
},
|
},
|
||||||
messageId: 'genericSpacingMismatch',
|
messageId: 'genericSpacingMismatch',
|
||||||
|
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@antfu/eslint-config-react",
|
"name": "@antfu/eslint-config-react",
|
||||||
"version": "0.29.0",
|
"version": "0.29.4",
|
||||||
"description": "",
|
"description": "",
|
||||||
"author": "Anthony Fu <anthonyfu117@hotmail.com> (https://github.com/antfu/)",
|
"author": "Anthony Fu <anthonyfu117@hotmail.com> (https://github.com/antfu/)",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
|
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@antfu/eslint-config-ts",
|
"name": "@antfu/eslint-config-ts",
|
||||||
"version": "0.29.0",
|
"version": "0.29.4",
|
||||||
"description": "",
|
"description": "",
|
||||||
"author": "Anthony Fu <anthonyfu117@hotmail.com> (https://github.com/antfu/)",
|
"author": "Anthony Fu <anthonyfu117@hotmail.com> (https://github.com/antfu/)",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
|
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@antfu/eslint-config-vue",
|
"name": "@antfu/eslint-config-vue",
|
||||||
"version": "0.29.0",
|
"version": "0.29.4",
|
||||||
"description": "",
|
"description": "",
|
||||||
"author": "Anthony Fu <anthonyfu117@hotmail.com> (https://github.com/antfu/)",
|
"author": "Anthony Fu <anthonyfu117@hotmail.com> (https://github.com/antfu/)",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
|
Reference in New Issue
Block a user