-
-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathutils.ts
305 lines (292 loc) · 7.9 KB
/
utils.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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
import type { RuleContext } from '../../types'
import { Range, intersects } from 'semver'
import type { AST as JSONAST } from 'jsonc-eslint-parser'
import type { AST as YAMLAST } from 'yaml-eslint-parser'
export const NodeTypes = {
Resource: 0,
Plural: 1,
Message: 2,
Text: 3,
Named: 4,
List: 5,
Linked: 6,
LinkedKey: 7,
LinkedModifier: 8,
Literal: 9
} as const
export type MessageSyntaxVersions = {
v8: boolean
v9: boolean
v10: boolean
isNotSet: boolean
reportIfMissingSetting: () => boolean
}
/** @type {Set<RuleContext>} */
const puttedSettingsError = new WeakSet<RuleContext>()
export function getMessageSyntaxVersions(
context: RuleContext
): MessageSyntaxVersions {
const { settings } = context
const messageSyntaxVersion =
settings['vue-i18n'] && settings['vue-i18n'].messageSyntaxVersion
if (!messageSyntaxVersion) {
return {
v8: true,
v9: true,
v10: true,
isNotSet: true,
reportIfMissingSetting: () => {
if (!puttedSettingsError.has(context)) {
const ruleName = context.id
context.report({
loc: { line: 1, column: 0 },
message: `If you want to use '${ruleName}' rule, you need to set 'messageSyntaxVersion' at 'settings'. See the 'eslint-plugin-vue-i18n' documentation`
})
puttedSettingsError.add(context)
}
return true
}
}
}
const range = new Range(messageSyntaxVersion)
return {
v8: intersects(range, '^8.0.0 || <=8.0.0'),
v9: intersects(range, '^9.0.0-0'),
v10: intersects(range, '>=10.0.0-0'),
isNotSet: false,
reportIfMissingSetting: () => false
}
}
export function getReportIndex(
node: JSONAST.JSONNode | YAMLAST.YAMLNode,
stringOffset: number
): number | null {
if (node.type === 'JSONLiteral' || node.type === 'JSONTemplateLiteral') {
const stringCode =
node.type === 'JSONLiteral'
? node.raw.slice(1, -1)
: node.quasis[0].value.raw
return (
node.range[0] +
1 /* quote*/ +
getJSONStringOffset(stringCode, stringOffset)
)
}
if (node.type === 'YAMLScalar') {
if (node.style === 'single-quoted' || node.style === 'double-quoted') {
const stringCode = node.raw.slice(1, -1)
return (
node.range[0] +
1 /* quote*/ +
(node.style === 'single-quoted'
? getYAMLSingleQuotedStringOffset(stringCode, stringOffset)
: getYAMLDoubleQuotedStringOffset(stringCode, stringOffset))
)
}
if (node.style === 'plain') {
const stringCode = node.raw
return node.range[0] + getYAMLPlainStringOffset(stringCode, stringOffset)
}
}
// Unknown location
return null
}
export function getJSONStringOffset(
code: string,
stringOffset: number
): number {
let offset = stringOffset
let codeIndex = 0
let char
while ((char = code[codeIndex++]) && offset > 0) {
if (char === '\\') {
char = code[codeIndex++]
if (char === 'x') {
// Hexadecimal escape sequences
codeIndex += 2
} else if (char === 'u') {
char = code[codeIndex++]
if (char === '{') {
// Unicode code point escapes
let pointStr = ''
while ((char = code[codeIndex++]) && char !== '}') {
pointStr += char
}
let point = parseInt(pointStr, 16)
while (point > 0xffff) {
offset--
point >>= 16
}
} else {
// Unicode escape sequences
codeIndex += 3
}
} else if (char >= '0' && char <= '9') {
let octalStr = code.substr(codeIndex - 1, 3).match(/^[0-7]+/u)![0]
const octal = parseInt(octalStr, 8)
if (octal > 255) {
octalStr = octalStr.slice(0, -1)
}
if (octalStr.length >= 2) {
codeIndex += octalStr.length - 1
}
}
}
offset--
}
return Math.min(codeIndex - 1, code.length)
}
export function getYAMLSingleQuotedStringOffset(
code: string,
stringOffset: number
): number {
let offset = stringOffset
let codeIndex = 0
let char
let lineFolding = 0
while ((char = code[codeIndex++]) && offset > 0) {
if (char === "'" && code[codeIndex] === "'") {
codeIndex++
lineFolding = 0
} else if (isSpaceOrEOL(char)) {
let nextCodeIndex = processMaybeLineFolding(code, codeIndex - 1)
if (nextCodeIndex != null) {
if (lineFolding === 0 && isEOL(code[nextCodeIndex])) {
nextCodeIndex = processMaybeLineFolding(code, nextCodeIndex)!
}
codeIndex = nextCodeIndex
lineFolding++
} else {
lineFolding = 0
}
} else {
lineFolding = 0
}
offset--
}
return Math.min(codeIndex - 1, code.length)
}
export function getYAMLDoubleQuotedStringOffset(
code: string,
stringOffset: number
): number {
let offset = stringOffset
let codeIndex = 0
let char
let lineFolding = 0
while ((char = code[codeIndex++]) && offset > 0) {
if (char === '\\') {
char = code[codeIndex++]
if (char === 'x') {
// Escaped 8-bit Unicode character.
codeIndex += 2
} else if (char === 'u') {
// Escaped 16-bit Unicode character.
codeIndex += 4
} else if (char === 'U') {
// Escaped 32-bit Unicode character.
if (code.substr(codeIndex, 4) !== '0000') {
offset--
}
codeIndex += 8
} else if (isEOL(char)) {
const nextCodeIndex = processMaybeLineFolding(code, codeIndex - 1)
if (nextCodeIndex != null) {
// escape line break
codeIndex = nextCodeIndex + 1
if (code[nextCodeIndex] === '\\') {
// If the next char is escape, it back.
codeIndex--
lineFolding = 0
// Do the next process without moving the offset.
continue
}
}
}
lineFolding = 0
} else if (isSpaceOrEOL(char)) {
let nextCodeIndex = processMaybeLineFolding(code, codeIndex - 1)
if (nextCodeIndex != null) {
if (lineFolding === 0 && isEOL(code[nextCodeIndex])) {
nextCodeIndex = processMaybeLineFolding(code, nextCodeIndex)!
}
codeIndex = nextCodeIndex
lineFolding++
} else {
lineFolding = 0
}
} else {
lineFolding = 0
}
offset--
}
return Math.min(codeIndex - 1, code.length)
}
export function getYAMLPlainStringOffset(
code: string,
stringOffset: number
): number {
let offset = stringOffset
let codeIndex = 0
let char
let lineFolding = 0
while ((char = code[codeIndex++]) && offset > 0) {
if (isSpaceOrEOL(char)) {
let nextCodeIndex = processMaybeLineFolding(code, codeIndex - 1)
if (nextCodeIndex != null) {
if (lineFolding === 0 && isEOL(code[nextCodeIndex])) {
nextCodeIndex = processMaybeLineFolding(code, nextCodeIndex)!
}
codeIndex = nextCodeIndex
lineFolding++
} else {
lineFolding = 0
}
} else {
lineFolding = 0
}
offset--
}
return Math.min(codeIndex - 1, code.length)
}
function isSpace(char: string) {
return char === ' ' || char === '\t'
}
function isSpaceOrEOL(char: string) {
return isSpace(char) || isEOL(char)
}
function isEOL(char: string) {
return char === '\n' || char === '\r'
}
function processMaybeLineFolding(code: string, spaceIndex: number) {
let codeIndex = spaceIndex
let char
let hasNewline = false
while ((char = code[codeIndex++])) {
if (isEOL(char)) {
if (hasNewline) {
return codeIndex - 1
}
if (char === '\r' && code[codeIndex] === '\n') {
codeIndex++
}
hasNewline = true
} else if (!isSpace(char)) {
if (hasNewline) {
// to space
return codeIndex - 1
} else {
// no line folding
return null
}
}
// space
}
if (hasNewline) {
// to space
return codeIndex - 1
} else {
// no line folding
return null
}
}