|
| 1 | +import type { RegExpVisitor } from "@eslint-community/regexpp/visitor" |
| 2 | +import type { RegExpContext } from "../utils" |
| 3 | +import { createRule, defineRegexpVisitor } from "../utils" |
| 4 | +import { RegExpParser, visitRegExpAST } from "@eslint-community/regexpp" |
| 5 | +import { toUnicodeSet } from "regexp-ast-analysis" |
| 6 | + |
| 7 | +const CLASS_SET_RESERVED_DOUBLE_PUNCTUATORS = [ |
| 8 | + "&&", |
| 9 | + "!!", |
| 10 | + "##", |
| 11 | + "$$", |
| 12 | + "%%", |
| 13 | + "**", |
| 14 | + "++", |
| 15 | + ",,", |
| 16 | + "..", |
| 17 | + "::", |
| 18 | + ";;", |
| 19 | + "<<", |
| 20 | + "==", |
| 21 | + ">>", |
| 22 | + "??", |
| 23 | + "@@", |
| 24 | + "^^", |
| 25 | + "``", |
| 26 | + "~~", |
| 27 | + "--", |
| 28 | +] |
| 29 | + |
| 30 | +/** |
| 31 | + * Returns whether the regex would keep its behavior if the v flag were to be |
| 32 | + * added. |
| 33 | + */ |
| 34 | +function isCompatible(regexpContext: RegExpContext): boolean { |
| 35 | + const INCOMPATIBLE = {} |
| 36 | + |
| 37 | + const { flags, patternAst, pattern } = regexpContext |
| 38 | + |
| 39 | + try { |
| 40 | + const flagsWithV = { ...flags, unicodeSets: true, unicode: false } |
| 41 | + visitRegExpAST(patternAst, { |
| 42 | + onCharacterClassEnter(node) { |
| 43 | + const us = toUnicodeSet(node, flags) |
| 44 | + const vus = toUnicodeSet( |
| 45 | + { ...node, unicodeSets: true }, |
| 46 | + flagsWithV, |
| 47 | + ) |
| 48 | + if (!us.equals(vus)) { |
| 49 | + throw INCOMPATIBLE |
| 50 | + } |
| 51 | + if ( |
| 52 | + CLASS_SET_RESERVED_DOUBLE_PUNCTUATORS.some((punctuator) => |
| 53 | + node.raw.includes(punctuator), |
| 54 | + ) |
| 55 | + ) { |
| 56 | + throw INCOMPATIBLE |
| 57 | + } |
| 58 | + }, |
| 59 | + }) |
| 60 | + } catch (error) { |
| 61 | + if (error === INCOMPATIBLE) { |
| 62 | + return false |
| 63 | + } |
| 64 | + // just rethrow |
| 65 | + throw error |
| 66 | + } |
| 67 | + |
| 68 | + try { |
| 69 | + // The `v` flag has more strict escape characters. |
| 70 | + // To check whether it can be converted to a pattern with the `v` flag, |
| 71 | + // parse the pattern with the `v` flag and check for errors. |
| 72 | + new RegExpParser().parsePattern(pattern, undefined, undefined, { |
| 73 | + unicodeSets: true, |
| 74 | + }) |
| 75 | + } catch (_error) { |
| 76 | + return false |
| 77 | + } |
| 78 | + |
| 79 | + return true |
| 80 | +} |
| 81 | + |
| 82 | +export default createRule("require-unicode-sets-regexp", { |
| 83 | + meta: { |
| 84 | + docs: { |
| 85 | + description: "enforce the use of the `v` flag", |
| 86 | + category: "Best Practices", |
| 87 | + recommended: false, |
| 88 | + }, |
| 89 | + schema: [], |
| 90 | + fixable: "code", |
| 91 | + messages: { |
| 92 | + require: "Use the 'v' flag.", |
| 93 | + }, |
| 94 | + type: "suggestion", |
| 95 | + }, |
| 96 | + create(context) { |
| 97 | + /** |
| 98 | + * Create visitor |
| 99 | + */ |
| 100 | + function createVisitor( |
| 101 | + regexpContext: RegExpContext, |
| 102 | + ): RegExpVisitor.Handlers { |
| 103 | + const { |
| 104 | + node, |
| 105 | + flags, |
| 106 | + flagsString, |
| 107 | + getFlagsLocation, |
| 108 | + fixReplaceFlags, |
| 109 | + } = regexpContext |
| 110 | + |
| 111 | + if (flagsString === null) { |
| 112 | + // This means that there are flags (probably) but we were |
| 113 | + // unable to evaluate them. |
| 114 | + return {} |
| 115 | + } |
| 116 | + |
| 117 | + if (!flags.unicodeSets) { |
| 118 | + context.report({ |
| 119 | + node, |
| 120 | + loc: getFlagsLocation(), |
| 121 | + messageId: "require", |
| 122 | + fix: fixReplaceFlags(() => { |
| 123 | + if ( |
| 124 | + // Only patterns with the u flag are auto-fixed. |
| 125 | + // When migrating from legacy, first add the `u` flag with the `require-unicode-regexp` rule. |
| 126 | + !flags.unicode || |
| 127 | + !isCompatible(regexpContext) |
| 128 | + ) { |
| 129 | + return null |
| 130 | + } |
| 131 | + return `${flagsString.replace(/u/gu, "")}v` |
| 132 | + }), |
| 133 | + }) |
| 134 | + } |
| 135 | + |
| 136 | + return {} |
| 137 | + } |
| 138 | + |
| 139 | + return defineRegexpVisitor(context, { |
| 140 | + createVisitor, |
| 141 | + }) |
| 142 | + }, |
| 143 | +}) |
0 commit comments