-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathhexadecimal-escape.ts
104 lines (95 loc) · 3.2 KB
/
hexadecimal-escape.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
import type { Character } from "@eslint-community/regexpp/ast"
import type { RegExpVisitor } from "@eslint-community/regexpp/visitor"
import type { RegExpContext } from "../utils"
import { defineRegexpVisitor, createRule } from "../utils"
import {
getEscapeSequenceKind,
EscapeSequenceKind,
} from "../utils/regex-syntax"
export default createRule("hexadecimal-escape", {
meta: {
docs: {
description: "enforce consistent usage of hexadecimal escape",
category: "Stylistic Issues",
recommended: false,
},
fixable: "code",
schema: [
{
enum: ["always", "never"], // default always
},
],
messages: {
expectedHexEscape:
"Expected hexadecimal escape ('{{hexEscape}}'), but {{unexpectedKind}} escape ('{{rejectEscape}}') is used.",
unexpectedHexEscape:
"Unexpected hexadecimal escape ('{{hexEscape}}').",
},
type: "suggestion", // "problem",
},
create(context) {
const always = context.options[0] !== "never"
function verifyForAlways(
{ node, getRegexpLocation, fixReplaceNode }: RegExpContext,
kind: EscapeSequenceKind,
cNode: Character,
) {
if (
kind !== EscapeSequenceKind.unicode &&
kind !== EscapeSequenceKind.unicodeCodePoint
) {
return
}
const hexEscape = `\\x${cNode.value.toString(16).padStart(2, "0")}`
context.report({
node,
loc: getRegexpLocation(cNode),
messageId: "expectedHexEscape",
data: {
hexEscape,
unexpectedKind: kind,
rejectEscape: cNode.raw,
},
fix: fixReplaceNode(cNode, hexEscape),
})
}
function verifyForNever(
{ node, getRegexpLocation, fixReplaceNode }: RegExpContext,
kind: EscapeSequenceKind,
cNode: Character,
) {
if (kind !== EscapeSequenceKind.hexadecimal) {
return
}
context.report({
node,
loc: getRegexpLocation(cNode),
messageId: "unexpectedHexEscape",
data: {
hexEscape: cNode.raw,
},
fix: fixReplaceNode(cNode, () => `\\u00${cNode.raw.slice(2)}`),
})
}
const verify = always ? verifyForAlways : verifyForNever
function createVisitor(
regexpContext: RegExpContext,
): RegExpVisitor.Handlers {
return {
onCharacterEnter(cNode) {
if (cNode.value > 0xff) {
return
}
const kind = getEscapeSequenceKind(cNode.raw)
if (!kind) {
return
}
verify(regexpContext, kind, cNode)
},
}
}
return defineRegexpVisitor(context, {
createVisitor,
})
},
})