Skip to content

Commit

Permalink
fix: handle optional case
Browse files Browse the repository at this point in the history
  • Loading branch information
xuzuodong committed Jul 28, 2023
1 parent 0b9a4aa commit 97d3368
Show file tree
Hide file tree
Showing 4 changed files with 164 additions and 40 deletions.
2 changes: 1 addition & 1 deletion packages/eslint-plugin-antfu/package.json
Expand Up @@ -16,7 +16,7 @@
"prepublishOnly": "nr build"
},
"dependencies": {
"@typescript-eslint/utils": "^6.0.0"
"@typescript-eslint/utils": "^5.61.0"
},
"devDependencies": {
"@types/node": "^20.4.2",
Expand Down
97 changes: 73 additions & 24 deletions packages/eslint-plugin-antfu/src/rules/named-tuple-spacing.test.ts
@@ -1,46 +1,95 @@
import { RuleTester } from '@typescript-eslint/utils/dist/ts-eslint'
import { it } from 'vitest'
import type { MessageIds } from './named-tuple-spacing'
import rule, { RULE_NAME } from './named-tuple-spacing'

const valids = [
'type T = [i: number]',
'type T = [i: number]', // passes since it will be handled by eslint's no-multi-spaces
'type T = [i?: number]',
'type T = [i: number, j: number]',
`const emit = defineEmits<{
change: [id: number]
update: [value: string]
}>()`,
]

const invalids = [
['type T = [i:number]', 'type T = [i: number]'],
['type T = [i : number]', 'type T = [i: number]', 1, 'unexpectedSpaceBefore'],
['type T = [i:number, j:number]', 'type T = [i: number, j: number]', 2],
[
`const emit = defineEmits<{
change: [id:number]
update: [value:string]
}>()`,
`const emit = defineEmits<{
change: [id: number]
update: [value: string]
}>()`,
2,
],
] as [error: string, correct: string, errorNumber: number | undefined, errorMsg: MessageIds | undefined][]

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: i[3] || 'expectedSpaceAfter' })),
})),
invalid: [
{
code: 'type T = [i:number]',
output: 'type T = [i: number]',
errors: [{ messageId: 'expectedSpaceAfter' }],
},
{
code: 'type T = [i: number]',
output: 'type T = [i: number]',
errors: [{ messageId: 'expectedSpaceAfter' }],
},
{
code: 'type T = [i?:number]',
output: 'type T = [i?: number]',
errors: [{ messageId: 'expectedSpaceAfter' }],
},
{
code: 'type T = [i? :number]',
output: 'type T = [i?: number]',
errors: [{ messageId: 'unexpectedSpaceBetween' }, { messageId: 'expectedSpaceAfter' }],
},
{
code: 'type T = [i : number]',
output: 'type T = [i: number]',
errors: [{ messageId: 'unexpectedSpaceBefore' }],
},
{
code: 'type T = [i : number]',
output: 'type T = [i: number]',
errors: [{ messageId: 'unexpectedSpaceBefore' }],
},
{
code: 'type T = [i ? : number]',
output: 'type T = [i?: number]',
errors: [{ messageId: 'unexpectedSpaceBetween' }, { messageId: 'unexpectedSpaceBefore' }],
},
{
code: 'type T = [i:number, j:number]',
output: 'type T = [i: number, j: number]',
errors: [{ messageId: 'expectedSpaceAfter' }, { messageId: 'expectedSpaceAfter' }],
},
{
code: `
const emit = defineEmits<{
change: [id:number]
update: [value:string]
}>()
`,
output: `
const emit = defineEmits<{
change: [id: number]
update: [value: string]
}>()
`,
errors: [{ messageId: 'expectedSpaceAfter' }, { messageId: 'expectedSpaceAfter' }],
},
{
code: `
const emit = defineEmits<{
change: [id? :number]
update: [value:string]
}>()
`,
output: `
const emit = defineEmits<{
change: [id?: number]
update: [value: string]
}>()
`,
errors: [{ messageId: 'unexpectedSpaceBetween' }, { messageId: 'expectedSpaceAfter' }, { messageId: 'expectedSpaceAfter' }],
},
],
})
})
44 changes: 34 additions & 10 deletions packages/eslint-plugin-antfu/src/rules/named-tuple-spacing.ts
@@ -1,7 +1,7 @@
import { createEslintRule } from '../utils'

export const RULE_NAME = 'named-tuple-spacing'
export type MessageIds = 'expectedSpaceAfter' | 'unexpectedSpaceBefore'
export type MessageIds = 'expectedSpaceAfter' | 'unexpectedSpaceBetween' | 'unexpectedSpaceBefore'
export type Options = []

export default createEslintRule<Options, MessageIds>({
Expand All @@ -16,36 +16,60 @@ export default createEslintRule<Options, MessageIds>({
schema: [],
messages: {
expectedSpaceAfter: 'Expected a space after the \':\'.',
unexpectedSpaceBefore: 'Unexpected space(s) before the \':\'.',
unexpectedSpaceBetween: 'Unexpected space between \'?\' and the \':\'.',
unexpectedSpaceBefore: 'Unexpected space before the \':\'.',
},
},
defaultOptions: [],
create: (context) => {
const sourceCode = context.getSourceCode()
return {
TSNamedTupleMember: (node) => {
TSNamedTupleMember: (node: any) => {
const code = sourceCode.text.slice(node.range[0], node.range[1])

const reg = /(\w+)( *):( *)(\w+)/
const spacesBeforeColon = code.match(reg)?.[2]
const spacesAfterColon = code.match(reg)?.[3]
const reg = /(\w+)(\s*)(\?\s*)?:(\s*)(\w+)/

if (spacesBeforeColon?.length) {
const labelName = node.label.name
const spaceBeforeColon = code.match(reg)?.[2]
const optionalMark = code.match(reg)?.[3]
const spacesAfterColon = code.match(reg)?.[4]
const elementType = code.match(reg)?.[5]

function getReplaceValue() {
let ret = labelName
if (node.optional)
ret += '?'
ret += ': '
ret += elementType
return ret
}

if (optionalMark?.length > 1) {
context.report({
node,
messageId: 'unexpectedSpaceBetween',
*fix(fixer) {
yield fixer.replaceTextRange(node.range, code.replace(reg, getReplaceValue()))
},
})
}

if (spaceBeforeColon?.length) {
context.report({
node,
messageId: 'unexpectedSpaceBefore',
*fix(fixer) {
yield fixer.replaceTextRange(node.range, code.replace(reg, '$1: $4'))
yield fixer.replaceTextRange(node.range, code.replace(reg, getReplaceValue()))
},
})
}

if (!spacesAfterColon) {
if (spacesAfterColon.length !== 1) {
context.report({
node,
messageId: 'expectedSpaceAfter',
*fix(fixer) {
yield fixer.replaceTextRange(node.range, code.replace(reg, '$1: $4'))
yield fixer.replaceTextRange(node.range, code.replace(reg, getReplaceValue()))
},
})
}
Expand Down
61 changes: 56 additions & 5 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 97d3368

Please sign in to comment.