-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathcontrol-character-escape.ts
110 lines (101 loc) · 3.26 KB
/
control-character-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
105
106
107
108
109
110
import type { RegExpVisitor } from "@eslint-community/regexpp/visitor"
import type { RegExpContext } from "../utils"
import {
CP_VT,
CP_CR,
CP_FF,
CP_LF,
CP_TAB,
createRule,
defineRegexpVisitor,
} from "../utils"
import type { PatternRange } from "../utils/ast-utils/pattern-source"
import { isRegexpLiteral } from "../utils/ast-utils/utils"
import { mentionChar } from "../utils/mention"
const CONTROL_CHARS = new Map<number, string>([
[0, "\\0"],
[CP_TAB, "\\t"],
[CP_LF, "\\n"],
[CP_VT, "\\v"],
[CP_FF, "\\f"],
[CP_CR, "\\r"],
])
/**
* Returns whether the regex is represented by a RegExp literal in source code
* at the given range.
*/
function isRegExpLiteralAt(
{ node, patternSource }: RegExpContext,
at: PatternRange,
): boolean {
if (isRegexpLiteral(node)) {
return true
}
const replaceRange = patternSource.getReplaceRange(at)
if (replaceRange && replaceRange.type === "RegExp") {
return true
}
return false
}
export default createRule("control-character-escape", {
meta: {
docs: {
description: "enforce consistent escaping of control characters",
category: "Best Practices",
recommended: true,
},
fixable: "code",
schema: [],
messages: {
unexpected:
"Unexpected control character escape {{actual}}. Use '{{expected}}' instead.",
},
type: "suggestion", // "problem",
},
create(context) {
function createVisitor(
regexpContext: RegExpContext,
): RegExpVisitor.Handlers {
const { node, getRegexpLocation, fixReplaceNode } = regexpContext
return {
onCharacterEnter(cNode) {
if (cNode.parent.type === "CharacterClassRange") {
// ignore ranges
return
}
const expectedRaw = CONTROL_CHARS.get(cNode.value)
if (expectedRaw === undefined) {
// not a known control character
return
}
if (cNode.raw === expectedRaw) {
// all good
return
}
if (
!isRegExpLiteralAt(regexpContext, cNode) &&
cNode.raw === String.fromCodePoint(cNode.value)
) {
// we allow the direct usage of control characters in
// string if it's not in regexp literal
// e.g. `RegExp("[\t\n]")` is ok
return
}
context.report({
node,
loc: getRegexpLocation(cNode),
messageId: "unexpected",
data: {
actual: mentionChar(cNode),
expected: expectedRaw,
},
fix: fixReplaceNode(cNode, expectedRaw),
})
},
}
}
return defineRegexpVisitor(context, {
createVisitor,
})
},
})