|
| 1 | +import type { Expression } from "estree" |
| 2 | +import type { RegExpVisitor } from "regexpp/visitor" |
| 3 | +import type { Character, CharacterSet, Quantifier } from "regexpp/ast" |
| 4 | +import { |
| 5 | + createRule, |
| 6 | + defineRegexpVisitor, |
| 7 | + getRegexpRange, |
| 8 | + isDigit, |
| 9 | + isLetter, |
| 10 | +} from "../utils" |
| 11 | + |
| 12 | +class CharBuffer { |
| 13 | + public target: CharacterSet | Character |
| 14 | + |
| 15 | + public min: number |
| 16 | + |
| 17 | + public max: number |
| 18 | + |
| 19 | + public elements: (CharacterSet | Character | Quantifier)[] |
| 20 | + |
| 21 | + public equalChar: (element: CharacterSet | Character) => boolean |
| 22 | + |
| 23 | + public constructor( |
| 24 | + element: CharacterSet | Character | Quantifier, |
| 25 | + target: CharacterSet | Character, |
| 26 | + ) { |
| 27 | + this.target = target |
| 28 | + this.elements = [element] |
| 29 | + |
| 30 | + if (element.type === "Quantifier") { |
| 31 | + this.min = element.min |
| 32 | + this.max = element.max |
| 33 | + } else { |
| 34 | + this.min = 1 |
| 35 | + this.max = 1 |
| 36 | + } |
| 37 | + if (target.type === "CharacterSet") { |
| 38 | + if (target.kind === "any") { |
| 39 | + this.equalChar = (e) => |
| 40 | + e.type === "CharacterSet" && e.kind === "any" |
| 41 | + } else if (target.kind === "property") { |
| 42 | + this.equalChar = (e) => |
| 43 | + e.type === "CharacterSet" && |
| 44 | + e.kind === "property" && |
| 45 | + e.key === target.key && |
| 46 | + e.value === target.value && |
| 47 | + e.negate === target.negate |
| 48 | + } else { |
| 49 | + // Escape |
| 50 | + this.equalChar = (e) => |
| 51 | + e.type === "CharacterSet" && |
| 52 | + e.kind === target.kind && |
| 53 | + e.negate === target.negate |
| 54 | + } |
| 55 | + } else { |
| 56 | + this.equalChar = (e) => |
| 57 | + e.type === "Character" && e.value === target.value |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + public addElement(element: CharacterSet | Character | Quantifier) { |
| 62 | + this.elements.push(element) |
| 63 | + if (element.type === "Quantifier") { |
| 64 | + this.min += element.min |
| 65 | + this.max += element.max |
| 66 | + } else { |
| 67 | + this.min += 1 |
| 68 | + this.max += 1 |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + public isValid(): boolean { |
| 73 | + if (this.elements.length < 2) { |
| 74 | + return true |
| 75 | + } |
| 76 | + let charKind: "digit" | "letter" | null = null |
| 77 | + for (const element of this.elements) { |
| 78 | + if (element.type === "Character") { |
| 79 | + if (charKind == null) { |
| 80 | + if (isDigit(element.value)) { |
| 81 | + charKind = "digit" |
| 82 | + } else if (isLetter(element.value)) { |
| 83 | + charKind = "letter" |
| 84 | + } else { |
| 85 | + return false |
| 86 | + } |
| 87 | + } |
| 88 | + } else { |
| 89 | + return false |
| 90 | + } |
| 91 | + } |
| 92 | + if ( |
| 93 | + // It is valid when the same numbers are consecutive. |
| 94 | + charKind === "digit" || |
| 95 | + // It is valid when the same letter character continues twice. |
| 96 | + (charKind === "letter" && this.elements.length <= 2) |
| 97 | + ) { |
| 98 | + return true |
| 99 | + } |
| 100 | + return false |
| 101 | + } |
| 102 | + |
| 103 | + public getQuantifier(): string { |
| 104 | + if (this.min === 0 && this.max === Number.POSITIVE_INFINITY) { |
| 105 | + return "*" |
| 106 | + } else if (this.min === 1 && this.max === Number.POSITIVE_INFINITY) { |
| 107 | + return "+" |
| 108 | + } else if (this.min === 0 && this.max === 1) { |
| 109 | + return "?" |
| 110 | + } else if (this.min === this.max) { |
| 111 | + return `{${this.min}}` |
| 112 | + } else if (this.max === Number.POSITIVE_INFINITY) { |
| 113 | + return `{${this.min},}` |
| 114 | + } |
| 115 | + return `{${this.min},${this.max}}` |
| 116 | + } |
| 117 | +} |
| 118 | + |
| 119 | +export default createRule("prefer-quantifier", { |
| 120 | + meta: { |
| 121 | + docs: { |
| 122 | + description: "enforce using quantifier", |
| 123 | + // TODO In the major version, it will be changed to "recommended". |
| 124 | + recommended: false, |
| 125 | + }, |
| 126 | + fixable: "code", |
| 127 | + schema: [], |
| 128 | + messages: { |
| 129 | + unexpected: |
| 130 | + 'Unexpected consecutive same {{type}}. Use "{{quantifier}}" instead.', |
| 131 | + }, |
| 132 | + type: "suggestion", // "problem", |
| 133 | + }, |
| 134 | + create(context) { |
| 135 | + const sourceCode = context.getSourceCode() |
| 136 | + |
| 137 | + /** |
| 138 | + * Create visitor |
| 139 | + * @param node |
| 140 | + */ |
| 141 | + function createVisitor(node: Expression): RegExpVisitor.Handlers { |
| 142 | + return { |
| 143 | + onAlternativeEnter(aNode) { |
| 144 | + let charBuffer: CharBuffer | null = null |
| 145 | + for (const element of aNode.elements) { |
| 146 | + let target: CharacterSet | Character |
| 147 | + if ( |
| 148 | + element.type === "CharacterSet" || |
| 149 | + element.type === "Character" |
| 150 | + ) { |
| 151 | + target = element |
| 152 | + } else if (element.type === "Quantifier") { |
| 153 | + if ( |
| 154 | + element.element.type === "CharacterSet" || |
| 155 | + element.element.type === "Character" |
| 156 | + ) { |
| 157 | + target = element.element |
| 158 | + } else { |
| 159 | + if (charBuffer) { |
| 160 | + validateBuffer(charBuffer) |
| 161 | + charBuffer = null |
| 162 | + } |
| 163 | + continue |
| 164 | + } |
| 165 | + } else { |
| 166 | + if (charBuffer) { |
| 167 | + validateBuffer(charBuffer) |
| 168 | + charBuffer = null |
| 169 | + } |
| 170 | + continue |
| 171 | + } |
| 172 | + if (charBuffer) { |
| 173 | + if (charBuffer.equalChar(target)) { |
| 174 | + charBuffer.addElement(element) |
| 175 | + continue |
| 176 | + } |
| 177 | + validateBuffer(charBuffer) |
| 178 | + } |
| 179 | + charBuffer = new CharBuffer(element, target) |
| 180 | + } |
| 181 | + if (charBuffer) { |
| 182 | + validateBuffer(charBuffer) |
| 183 | + charBuffer = null |
| 184 | + } |
| 185 | + |
| 186 | + /** |
| 187 | + * Validate |
| 188 | + */ |
| 189 | + function validateBuffer(buffer: CharBuffer) { |
| 190 | + if (buffer.isValid()) { |
| 191 | + return |
| 192 | + } |
| 193 | + const firstRange = getRegexpRange( |
| 194 | + sourceCode, |
| 195 | + node, |
| 196 | + buffer.elements[0], |
| 197 | + ) |
| 198 | + const lastRange = getRegexpRange( |
| 199 | + sourceCode, |
| 200 | + node, |
| 201 | + buffer.elements[buffer.elements.length - 1], |
| 202 | + ) |
| 203 | + let range: [number, number] | null = null |
| 204 | + if (firstRange && lastRange) { |
| 205 | + range = [firstRange[0], lastRange[1]] |
| 206 | + } |
| 207 | + context.report({ |
| 208 | + node, |
| 209 | + loc: range |
| 210 | + ? { |
| 211 | + start: sourceCode.getLocFromIndex( |
| 212 | + range[0], |
| 213 | + ), |
| 214 | + end: sourceCode.getLocFromIndex(range[1]), |
| 215 | + } |
| 216 | + : undefined, |
| 217 | + messageId: "unexpected", |
| 218 | + data: { |
| 219 | + type: |
| 220 | + buffer.target.type === "Character" |
| 221 | + ? "characters" |
| 222 | + : "character sets", |
| 223 | + quantifier: buffer.getQuantifier(), |
| 224 | + }, |
| 225 | + fix(fixer) { |
| 226 | + if (range == null) { |
| 227 | + return null |
| 228 | + } |
| 229 | + return fixer.replaceTextRange( |
| 230 | + range, |
| 231 | + buffer.target.raw + buffer.getQuantifier(), |
| 232 | + ) |
| 233 | + }, |
| 234 | + }) |
| 235 | + } |
| 236 | + }, |
| 237 | + } |
| 238 | + } |
| 239 | + |
| 240 | + return defineRegexpVisitor(context, { |
| 241 | + createVisitor, |
| 242 | + }) |
| 243 | + }, |
| 244 | +}) |
0 commit comments