-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathno-invisible-character.ts
99 lines (94 loc) · 3.36 KB
/
no-invisible-character.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
import type { RegExpVisitor } from "@eslint-community/regexpp/visitor"
import type { AST } from "eslint"
import type { RegExpContextForLiteral, RegExpContextForSource } from "../utils"
import { createRule, defineRegexpVisitor, isInvisible } from "../utils"
import { toCharSetSource } from "../utils/refa"
export default createRule("no-invisible-character", {
meta: {
docs: {
description: "disallow invisible raw character",
category: "Best Practices",
recommended: true,
},
fixable: "code",
schema: [],
messages: {
unexpected:
"Unexpected invisible character. Use '{{instead}}' instead.",
},
type: "suggestion", // "problem",
},
create(context) {
const sourceCode = context.sourceCode
function createLiteralVisitor({
node,
flags,
getRegexpLocation,
fixReplaceNode,
}: RegExpContextForLiteral): RegExpVisitor.Handlers {
return {
onCharacterEnter(cNode) {
if (cNode.raw === " ") {
return
}
if (cNode.raw.length === 1 && isInvisible(cNode.value)) {
const instead = toCharSetSource(cNode.value, flags)
context.report({
node,
loc: getRegexpLocation(cNode),
messageId: "unexpected",
data: {
instead,
},
fix: fixReplaceNode(cNode, instead),
})
}
},
}
}
/**
* Verify a given string literal.
*/
function verifyString({ node, flags }: RegExpContextForSource): void {
const text = sourceCode.getText(node)
let index = 0
for (const c of text) {
if (c === " ") {
continue
}
const cp = c.codePointAt(0)!
if (isInvisible(cp)) {
const instead = toCharSetSource(cp, flags)
const range: AST.Range = [
node.range![0] + index,
node.range![0] + index + c.length,
]
context.report({
node,
loc: {
start: sourceCode.getLocFromIndex(range[0]),
end: sourceCode.getLocFromIndex(range[1]),
},
messageId: "unexpected",
data: {
instead,
},
fix(fixer) {
return fixer.replaceTextRange(range, instead)
},
})
}
index += c.length
}
}
return defineRegexpVisitor(context, {
createLiteralVisitor,
createSourceVisitor(regexpContext) {
if (regexpContext.node.type === "Literal") {
verifyString(regexpContext)
}
return {} // no visit
},
})
},
})