|
| 1 | +import type { Text } from 'mdast' |
| 2 | +import type { ValueOf } from '@/types' |
| 3 | +import type { TokenContext } from '@/utils/ast' |
| 4 | +import { createRule } from '@/utils' |
| 5 | +import { getNodeContextByParent } from '@/utils/ast' |
| 6 | +import { SPACE_MESSAGE_IDS as MESSAGE_IDS } from '@/utils/space' |
| 7 | +import { buildTextNodeAst, isNumberType, TEXT_TYPE } from '@/utils/text-tokenizer' |
| 8 | + |
| 9 | +export const RULE_NAME = 'space-around-number' |
| 10 | + |
| 11 | +type MessageIds = ValueOf<typeof MESSAGE_IDS> |
| 12 | +type Options = [] |
| 13 | + |
| 14 | +export default createRule<Options, MessageIds>({ |
| 15 | + name: RULE_NAME, |
| 16 | + meta: { |
| 17 | + type: 'layout', |
| 18 | + docs: { |
| 19 | + description: 'Enforce a single space between CJK characters and numbers.', |
| 20 | + }, |
| 21 | + messages: { |
| 22 | + missingSpaceBefore: 'Add a space before the number.', |
| 23 | + missingSpaceAfter: 'Add a space after the number.', |
| 24 | + missingSpacesAround: 'Add spaces before and after the number.', |
| 25 | + unexpectedSpaceBefore: 'Remove the unexpected space before the number.', |
| 26 | + unexpectedSpaceAfter: 'Remove the unexpected space after the number.', |
| 27 | + unexpectedSpaceAround: 'Remove the unexpected spaces around the number.', |
| 28 | + }, |
| 29 | + fixable: 'code', |
| 30 | + schema: [], |
| 31 | + }, |
| 32 | + defaultOptions: [], |
| 33 | + create(context) { |
| 34 | + return { |
| 35 | + text(node: Text) { |
| 36 | + const { fixed, missingBefore, missingAfter, unexpectedBefore, unexpectedAfter } = fixBoundarySpace(node) |
| 37 | + |
| 38 | + if (fixed === node.value) |
| 39 | + return |
| 40 | + |
| 41 | + context.report({ |
| 42 | + node, |
| 43 | + messageId: getMessageId({ missingBefore, missingAfter, unexpectedBefore, unexpectedAfter }), |
| 44 | + fix(fixer) { |
| 45 | + return fixer.replaceText(node, fixed) |
| 46 | + }, |
| 47 | + }) |
| 48 | + }, |
| 49 | + } |
| 50 | + }, |
| 51 | +}) |
| 52 | + |
| 53 | +interface FixBoundarySpaceResult { |
| 54 | + fixed: string |
| 55 | + missingBefore: boolean |
| 56 | + missingAfter: boolean |
| 57 | + unexpectedBefore: boolean |
| 58 | + unexpectedAfter: boolean |
| 59 | +} |
| 60 | + |
| 61 | +function processSpaceToken(ctx: TokenContext, result: FixBoundarySpaceResult): void { |
| 62 | + const { prev, current, next } = ctx |
| 63 | + /* v8 ignore if -- @preserve */ |
| 64 | + if (!current) |
| 65 | + return |
| 66 | + |
| 67 | + const CJK2Number = prev?.type === TEXT_TYPE.cjk && isNumberType(next?.type) |
| 68 | + const number2CJK = isNumberType(prev?.type) && next?.type === TEXT_TYPE.cjk |
| 69 | + const numbers = isNumberType(prev?.type) && isNumberType(next?.type) |
| 70 | + const hasUnexpectedSpaces = current.value.length !== 1 |
| 71 | + |
| 72 | + if (hasUnexpectedSpaces) { |
| 73 | + if (CJK2Number || numbers) |
| 74 | + result.unexpectedBefore = true |
| 75 | + |
| 76 | + if (number2CJK || numbers) |
| 77 | + result.unexpectedAfter = true |
| 78 | + } |
| 79 | + |
| 80 | + if (CJK2Number || number2CJK || hasUnexpectedSpaces) { |
| 81 | + result.fixed += ' ' |
| 82 | + } |
| 83 | + else { |
| 84 | + result.fixed += current.value |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +function processNumberToken(ctx: TokenContext, result: FixBoundarySpaceResult): void { |
| 89 | + const { prev, current, next } = ctx |
| 90 | + /* v8 ignore if -- @preserve */ |
| 91 | + if (!current) |
| 92 | + return |
| 93 | + |
| 94 | + if (prev?.type === TEXT_TYPE.cjk) { |
| 95 | + result.fixed += ' ' |
| 96 | + result.missingBefore = true |
| 97 | + } |
| 98 | + |
| 99 | + result.fixed += current.value |
| 100 | + |
| 101 | + if (next?.type === TEXT_TYPE.cjk) { |
| 102 | + result.fixed += ' ' |
| 103 | + result.missingAfter = true |
| 104 | + } |
| 105 | +} |
| 106 | + |
| 107 | +function getMessageId(boundary: { |
| 108 | + missingBefore: boolean |
| 109 | + missingAfter: boolean |
| 110 | + unexpectedBefore: boolean |
| 111 | + unexpectedAfter: boolean |
| 112 | +}): MessageIds { |
| 113 | + if (boundary.missingBefore && boundary.missingAfter) |
| 114 | + return MESSAGE_IDS.missingSpacesAround |
| 115 | + |
| 116 | + if (boundary.unexpectedBefore && boundary.unexpectedAfter) |
| 117 | + return MESSAGE_IDS.unexpectedSpaceAround |
| 118 | + |
| 119 | + if (boundary.missingBefore) |
| 120 | + return MESSAGE_IDS.missingSpaceBefore |
| 121 | + |
| 122 | + if (boundary.missingAfter) |
| 123 | + return MESSAGE_IDS.missingSpaceAfter |
| 124 | + |
| 125 | + if (boundary.unexpectedBefore) |
| 126 | + return MESSAGE_IDS.unexpectedSpaceBefore |
| 127 | + |
| 128 | + return MESSAGE_IDS.unexpectedSpaceAfter |
| 129 | +} |
| 130 | + |
| 131 | +function fixBoundarySpace(node: Text): FixBoundarySpaceResult { |
| 132 | + const { children } = buildTextNodeAst(node) |
| 133 | + const result: FixBoundarySpaceResult = { |
| 134 | + fixed: '', |
| 135 | + missingBefore: false, |
| 136 | + missingAfter: false, |
| 137 | + unexpectedBefore: false, |
| 138 | + unexpectedAfter: false, |
| 139 | + } |
| 140 | + |
| 141 | + for (let i = 0; i < children.length; i += 1) { |
| 142 | + const ctx = getNodeContextByParent(children, i) |
| 143 | + /* v8 ignore if -- @preserve */ |
| 144 | + if (!ctx.current) |
| 145 | + continue |
| 146 | + |
| 147 | + if (ctx.current.type === TEXT_TYPE.space) |
| 148 | + processSpaceToken(ctx, result) |
| 149 | + else if (isNumberType(ctx.current.type)) |
| 150 | + processNumberToken(ctx, result) |
| 151 | + else |
| 152 | + result.fixed += ctx.current.value |
| 153 | + } |
| 154 | + |
| 155 | + return result |
| 156 | +} |
0 commit comments