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