|
| 1 | +import type { RegExpVisitor } from "@eslint-community/regexpp/visitor" |
| 2 | +import type { RegExpContext } from "../utils" |
| 3 | +import { createRule, defineRegexpVisitor } from "../utils" |
| 4 | +import type { |
| 5 | + ClassStringDisjunction, |
| 6 | + StringAlternative, |
| 7 | +} from "@eslint-community/regexpp/ast" |
| 8 | +import { RESERVED_DOUBLE_PUNCTUATOR_CHARS } from "../utils/unicode-set" |
| 9 | + |
| 10 | +export default createRule("no-useless-string-literal", { |
| 11 | + meta: { |
| 12 | + docs: { |
| 13 | + description: |
| 14 | + "disallow string disjunction of single characters in `\\q{...}`", |
| 15 | + category: "Best Practices", |
| 16 | + recommended: true, |
| 17 | + }, |
| 18 | + schema: [], |
| 19 | + messages: { |
| 20 | + unexpected: "Unexpected string disjunction of single character.", |
| 21 | + }, |
| 22 | + type: "suggestion", |
| 23 | + fixable: "code", |
| 24 | + }, |
| 25 | + create(context) { |
| 26 | + function createVisitor( |
| 27 | + regexpContext: RegExpContext, |
| 28 | + ): RegExpVisitor.Handlers { |
| 29 | + const { node, getRegexpLocation, fixReplaceNode, pattern } = |
| 30 | + regexpContext |
| 31 | + return { |
| 32 | + onStringAlternativeEnter(saNode) { |
| 33 | + if (saNode.elements.length === 1) { |
| 34 | + const csdNode = saNode.parent |
| 35 | + context.report({ |
| 36 | + node, |
| 37 | + loc: getRegexpLocation(saNode), |
| 38 | + messageId: "unexpected", |
| 39 | + fix: fixReplaceNode(csdNode, () => { |
| 40 | + const alternativesText = csdNode.alternatives |
| 41 | + .filter((alt) => alt !== saNode) |
| 42 | + .map((alt) => alt.raw) |
| 43 | + .join("|") |
| 44 | + if (!alternativesText.length) { |
| 45 | + const escape = |
| 46 | + isNeedEscapedInCharacterClass( |
| 47 | + csdNode, |
| 48 | + saNode, |
| 49 | + ) |
| 50 | + ? "\\" |
| 51 | + : "" |
| 52 | + return `${escape}${saNode.raw}` |
| 53 | + } |
| 54 | + if ( |
| 55 | + csdNode.parent.type === |
| 56 | + "ClassIntersection" || |
| 57 | + csdNode.parent.type === "ClassSubtraction" |
| 58 | + ) { |
| 59 | + const escape = |
| 60 | + saNode.raw === "^" ? "\\" : "" |
| 61 | + return String.raw`[${escape}${saNode.raw}\q{${alternativesText}}]` |
| 62 | + } |
| 63 | + const escape = isNeedEscapedInCharacterClass( |
| 64 | + csdNode, |
| 65 | + saNode, |
| 66 | + ) |
| 67 | + ? "\\" |
| 68 | + : "" |
| 69 | + return String.raw`${escape}${saNode.raw}\q{${alternativesText}}` |
| 70 | + }), |
| 71 | + }) |
| 72 | + } |
| 73 | + }, |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * Checks whether an escape is required if the given character when placed before a \q{...} |
| 78 | + */ |
| 79 | + function isNeedEscapedInCharacterClass( |
| 80 | + disjunction: ClassStringDisjunction, |
| 81 | + character: StringAlternative, |
| 82 | + ) { |
| 83 | + const char = character.raw |
| 84 | + // Avoid [A&&\q{&}] => [A&&&] |
| 85 | + if ( |
| 86 | + RESERVED_DOUBLE_PUNCTUATOR_CHARS.has(char) && |
| 87 | + // The previous character is the same |
| 88 | + pattern[disjunction.start - 1] === char |
| 89 | + ) { |
| 90 | + return true |
| 91 | + } |
| 92 | + |
| 93 | + // Avoid [\q{^|ab}] => [^\q{ab}] |
| 94 | + return ( |
| 95 | + char === "^" && |
| 96 | + disjunction.parent.type === "CharacterClass" && |
| 97 | + disjunction.parent.start === disjunction.start - 1 |
| 98 | + ) |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + return defineRegexpVisitor(context, { |
| 103 | + createVisitor, |
| 104 | + }) |
| 105 | + }, |
| 106 | +}) |
0 commit comments