Skip to content

Commit

Permalink
fix(plugin): named-tuple-spacing handling, close #232
Browse files Browse the repository at this point in the history
  • Loading branch information
antfu committed Aug 7, 2023
1 parent 59308e8 commit 3a2c549
Showing 1 changed file with 15 additions and 11 deletions.
26 changes: 15 additions & 11 deletions packages/eslint-plugin-antfu/src/rules/named-tuple-spacing.ts
Expand Up @@ -4,13 +4,15 @@ export const RULE_NAME = 'named-tuple-spacing'
export type MessageIds = 'expectedSpaceAfter' | 'unexpectedSpaceBetween' | 'unexpectedSpaceBefore'
export type Options = []

const RE = /^([\w_$]+)(\s*)(\?\s*)?:(\s*)(.*)$/

export default createEslintRule<Options, MessageIds>({
name: RULE_NAME,
meta: {
type: 'suggestion',
docs: {
description: 'Expect space before type declaration in named tuple',
recommended: 'error',
recommended: 'stylistic',
},
fixable: 'code',
schema: [],
Expand All @@ -24,16 +26,18 @@ export default createEslintRule<Options, MessageIds>({
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+)(\s*)(\?\s*)?:(\s*)(\w+)/
const match = code.match(RE)
if (!match)
return

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]
const spaceBeforeColon = match[2]
const optionalMark = match[3]
const spacesAfterColon = match[4]
const elementType = match[5]

function getReplaceValue() {
let ret = labelName
Expand All @@ -49,7 +53,7 @@ export default createEslintRule<Options, MessageIds>({
node,
messageId: 'unexpectedSpaceBetween',
*fix(fixer) {
yield fixer.replaceTextRange(node.range, code.replace(reg, getReplaceValue()))
yield fixer.replaceTextRange(node.range, code.replace(RE, getReplaceValue()))
},
})
}
Expand All @@ -59,17 +63,17 @@ export default createEslintRule<Options, MessageIds>({
node,
messageId: 'unexpectedSpaceBefore',
*fix(fixer) {
yield fixer.replaceTextRange(node.range, code.replace(reg, getReplaceValue()))
yield fixer.replaceTextRange(node.range, code.replace(RE, getReplaceValue()))
},
})
}

if (spacesAfterColon.length !== 1) {
if (spacesAfterColon != null && spacesAfterColon.length !== 1) {
context.report({
node,
messageId: 'expectedSpaceAfter',
*fix(fixer) {
yield fixer.replaceTextRange(node.range, code.replace(reg, getReplaceValue()))
yield fixer.replaceTextRange(node.range, code.replace(RE, getReplaceValue()))
},
})
}
Expand Down

0 comments on commit 3a2c549

Please sign in to comment.