/
unicode-property.ts
253 lines (235 loc) · 9.87 KB
/
unicode-property.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
import type { UnicodePropertyCharacterSet } from "@eslint-community/regexpp/ast"
import type { RegExpVisitor } from "@eslint-community/regexpp/visitor"
import type { RegExpContext } from "../utils"
import { createRule, defineRegexpVisitor } from "../utils"
import {
UNICODE_BINARY_PROPERTY_ALIAS,
UNICODE_CATEGORY_ALIAS,
UNICODE_GENERAL_CATEGORY_ALIAS,
UNICODE_SCRIPT_ALIAS,
} from "../utils/unicode-alias"
function isGeneralCategory(key: string): boolean {
return UNICODE_CATEGORY_ALIAS.toShort(key) === "gc"
}
export default createRule("unicode-property", {
meta: {
docs: {
description: "enforce consistent naming of unicode properties",
category: "Stylistic Issues",
recommended: false,
},
schema: [
{
type: "object",
properties: {
generalCategory: {
enum: ["always", "never", "ignore"],
},
key: {
enum: ["short", "long", "ignore"],
},
property: {
anyOf: [
{
enum: ["short", "long", "ignore"],
},
{
type: "object",
properties: {
binary: {
enum: ["short", "long", "ignore"],
},
generalCategory: {
enum: ["short", "long", "ignore"],
},
script: {
enum: ["short", "long", "ignore"],
},
},
additionalProperties: false,
},
],
},
},
additionalProperties: false,
},
],
messages: {
unnecessaryGc: "Unnecessary '{{ gc }}=' in Unicode property.",
missingGc: "Missing '{{ gc }}=' in Unicode property.",
expectedKey: "Excepted {{ len }} key. Use '{{ key }}' instead.",
expectedProperty:
"Excepted {{ len }} {{ type }} property. Use '{{ prop }}' instead.",
},
type: "suggestion",
fixable: "code",
},
create(context) {
type Length = "short" | "long" | "ignore"
interface Options {
generalCategory?: "always" | "never" | "ignore"
key?: Length
property?:
| Length
| { binary?: Length; generalCategory?: Length; script?: Length }
}
const {
generalCategory = "never",
key: keyFormat = "ignore",
property = {
binary: "ignore",
generalCategory: "ignore",
script: "long",
},
} = (context.options[0] || {}) as Options
let defaultPropertyFormat: Length = "long"
if (typeof property === "string") {
defaultPropertyFormat = property
}
const {
binary: binaryFormat = defaultPropertyFormat,
generalCategory: generalCategoryFormat = defaultPropertyFormat,
script: scriptFormat = defaultPropertyFormat,
} = typeof property === "string" ? {} : property
function createVisitor(
regexpContext: RegExpContext,
): RegExpVisitor.Handlers {
const { node, getRegexpLocation, fixReplaceNode } = regexpContext
function onUnicodeProperty(cs: UnicodePropertyCharacterSet): void {
// Whether the unicode property is in the form of \p{key} or \p{key=value}.
// We need to check this separately because regexpp automatically parses
// \p{L} as \p{General_Category=L} for all General_Category values.
const keyValueSyntax = cs.raw.includes("=")
function fixReplace(inner: string) {
return fixReplaceNode(cs, `${cs.raw.slice(0, 2)}{${inner}}`)
}
function getKeyLocation() {
const offset = "\\p{".length
if (keyValueSyntax) {
return getRegexpLocation({
start: cs.start + offset,
end: cs.start + offset + cs.key.length,
})
}
return getRegexpLocation({
start: cs.start + offset,
end: cs.end - 1,
})
}
function getValueLocation() {
return getRegexpLocation({
start: cs.end - 1 - (cs.value || cs.key).length,
end: cs.end - 1,
})
}
const { key, value } = cs
if (value === null) {
// format: \p{key}
if (binaryFormat !== "ignore") {
const expected =
binaryFormat === "short"
? UNICODE_BINARY_PROPERTY_ALIAS.toShort(key)
: UNICODE_BINARY_PROPERTY_ALIAS.toLong(key)
if (key !== expected) {
context.report({
node,
loc: getKeyLocation(),
messageId: "expectedProperty",
data: {
len: binaryFormat,
type: "binary",
prop: expected,
},
fix: fixReplace(expected),
})
}
}
} else {
// format: \p{key=value}
const isGC = isGeneralCategory(key)
let handledKey = false
if (isGC) {
if (keyValueSyntax && generalCategory === "never") {
context.report({
node,
loc: getKeyLocation(),
messageId: "unnecessaryGc",
data: { gc: key },
fix: fixReplace(value),
})
handledKey = true
}
if (!keyValueSyntax && generalCategory === "always") {
const missing =
keyFormat === "long" ? "General_Category" : "gc"
context.report({
node,
loc: getRegexpLocation(cs),
messageId: "missingGc",
data: { gc: missing },
fix: fixReplace(`${missing}=${value}`),
})
handledKey = true
}
}
if (
!handledKey &&
keyValueSyntax &&
keyFormat !== "ignore"
) {
const expected =
keyFormat === "short"
? UNICODE_CATEGORY_ALIAS.toShort(key)
: UNICODE_CATEGORY_ALIAS.toLong(key)
if (key !== expected) {
context.report({
node,
loc: getKeyLocation(),
messageId: "expectedKey",
data: { len: keyFormat, key: expected },
fix: fixReplace(`${expected}=${value}`),
})
}
}
const valueFormat = isGC
? generalCategoryFormat
: scriptFormat
if (valueFormat !== "ignore") {
const aliasMap = isGC
? UNICODE_GENERAL_CATEGORY_ALIAS
: UNICODE_SCRIPT_ALIAS
const expected =
valueFormat === "short"
? aliasMap.toShort(value)
: aliasMap.toLong(value)
if (value !== expected) {
const prefix = keyValueSyntax ? `${key}=` : ""
const type = isGC ? "General_Category" : "Script"
context.report({
node,
loc: getValueLocation(),
messageId: "expectedProperty",
data: {
len: valueFormat,
type,
prop: expected,
},
fix: fixReplace(`${prefix}${expected}`),
})
}
}
}
}
return {
onCharacterSetEnter(cs) {
if (cs.kind === "property") {
onUnicodeProperty(cs)
}
},
}
}
return defineRegexpVisitor(context, {
createVisitor,
})
},
})