fix(plugin): named-tuple-spacing
handling, close #232
This commit is contained in:
parent
59308e8e36
commit
3a2c549110
@ -4,13 +4,15 @@ export const RULE_NAME = 'named-tuple-spacing'
|
|||||||
export type MessageIds = 'expectedSpaceAfter' | 'unexpectedSpaceBetween' | 'unexpectedSpaceBefore'
|
export type MessageIds = 'expectedSpaceAfter' | 'unexpectedSpaceBetween' | 'unexpectedSpaceBefore'
|
||||||
export type Options = []
|
export type Options = []
|
||||||
|
|
||||||
|
const RE = /^([\w_$]+)(\s*)(\?\s*)?:(\s*)(.*)$/
|
||||||
|
|
||||||
export default createEslintRule<Options, MessageIds>({
|
export default createEslintRule<Options, MessageIds>({
|
||||||
name: RULE_NAME,
|
name: RULE_NAME,
|
||||||
meta: {
|
meta: {
|
||||||
type: 'suggestion',
|
type: 'suggestion',
|
||||||
docs: {
|
docs: {
|
||||||
description: 'Expect space before type declaration in named tuple',
|
description: 'Expect space before type declaration in named tuple',
|
||||||
recommended: 'error',
|
recommended: 'stylistic',
|
||||||
},
|
},
|
||||||
fixable: 'code',
|
fixable: 'code',
|
||||||
schema: [],
|
schema: [],
|
||||||
@ -24,16 +26,18 @@ export default createEslintRule<Options, MessageIds>({
|
|||||||
create: (context) => {
|
create: (context) => {
|
||||||
const sourceCode = context.getSourceCode()
|
const sourceCode = context.getSourceCode()
|
||||||
return {
|
return {
|
||||||
TSNamedTupleMember: (node) => {
|
TSNamedTupleMember: (node: any) => {
|
||||||
const code = sourceCode.text.slice(node.range[0], node.range[1])
|
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 labelName = node.label.name
|
||||||
const spaceBeforeColon = code.match(reg)?.[2]
|
const spaceBeforeColon = match[2]
|
||||||
const optionalMark = code.match(reg)?.[3]
|
const optionalMark = match[3]
|
||||||
const spacesAfterColon = code.match(reg)?.[4]
|
const spacesAfterColon = match[4]
|
||||||
const elementType = code.match(reg)?.[5]
|
const elementType = match[5]
|
||||||
|
|
||||||
function getReplaceValue() {
|
function getReplaceValue() {
|
||||||
let ret = labelName
|
let ret = labelName
|
||||||
@ -49,7 +53,7 @@ export default createEslintRule<Options, MessageIds>({
|
|||||||
node,
|
node,
|
||||||
messageId: 'unexpectedSpaceBetween',
|
messageId: 'unexpectedSpaceBetween',
|
||||||
*fix(fixer) {
|
*fix(fixer) {
|
||||||
yield fixer.replaceTextRange(node.range, code.replace(reg, getReplaceValue()))
|
yield fixer.replaceTextRange(node.range, code.replace(RE, getReplaceValue()))
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@ -59,17 +63,17 @@ export default createEslintRule<Options, MessageIds>({
|
|||||||
node,
|
node,
|
||||||
messageId: 'unexpectedSpaceBefore',
|
messageId: 'unexpectedSpaceBefore',
|
||||||
*fix(fixer) {
|
*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({
|
context.report({
|
||||||
node,
|
node,
|
||||||
messageId: 'expectedSpaceAfter',
|
messageId: 'expectedSpaceAfter',
|
||||||
*fix(fixer) {
|
*fix(fixer) {
|
||||||
yield fixer.replaceTextRange(node.range, code.replace(reg, getReplaceValue()))
|
yield fixer.replaceTextRange(node.range, code.replace(RE, getReplaceValue()))
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user