@localness/contract 0.4.2 → 0.4.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +11 -0
- package/dist/index.d.mts +17 -1
- package/dist/index.mjs +14 -1
- package/dist/locale-file.d.mts +61 -0
- package/dist/locale-file.mjs +1389 -0
- package/package.json +6 -1
|
@@ -0,0 +1,1389 @@
|
|
|
1
|
+
// ../../node_modules/.pnpm/jsonc-parser@3.3.1/node_modules/jsonc-parser/lib/esm/impl/scanner.js
|
|
2
|
+
/**
|
|
3
|
+
* Creates a JSON scanner on the given text.
|
|
4
|
+
* If ignoreTrivia is set, whitespaces or comments are ignored.
|
|
5
|
+
*/
|
|
6
|
+
function createScanner(text, ignoreTrivia = false) {
|
|
7
|
+
const len = text.length;
|
|
8
|
+
let pos = 0, value = "", tokenOffset = 0, token = 16, lineNumber = 0, lineStartOffset = 0, tokenLineStartOffset = 0, prevTokenLineStartOffset = 0, scanError = 0;
|
|
9
|
+
function scanHexDigits(count, exact) {
|
|
10
|
+
let digits = 0;
|
|
11
|
+
let value = 0;
|
|
12
|
+
while (digits < count || !exact) {
|
|
13
|
+
let ch = text.charCodeAt(pos);
|
|
14
|
+
if (ch >= 48 && ch <= 57) value = value * 16 + ch - 48;
|
|
15
|
+
else if (ch >= 65 && ch <= 70) value = value * 16 + ch - 65 + 10;
|
|
16
|
+
else if (ch >= 97 && ch <= 102) value = value * 16 + ch - 97 + 10;
|
|
17
|
+
else break;
|
|
18
|
+
pos++;
|
|
19
|
+
digits++;
|
|
20
|
+
}
|
|
21
|
+
if (digits < count) value = -1;
|
|
22
|
+
return value;
|
|
23
|
+
}
|
|
24
|
+
function setPosition(newPosition) {
|
|
25
|
+
pos = newPosition;
|
|
26
|
+
value = "";
|
|
27
|
+
tokenOffset = 0;
|
|
28
|
+
token = 16;
|
|
29
|
+
scanError = 0;
|
|
30
|
+
}
|
|
31
|
+
function scanNumber() {
|
|
32
|
+
let start = pos;
|
|
33
|
+
if (text.charCodeAt(pos) === 48) pos++;
|
|
34
|
+
else {
|
|
35
|
+
pos++;
|
|
36
|
+
while (pos < text.length && isDigit(text.charCodeAt(pos))) pos++;
|
|
37
|
+
}
|
|
38
|
+
if (pos < text.length && text.charCodeAt(pos) === 46) {
|
|
39
|
+
pos++;
|
|
40
|
+
if (pos < text.length && isDigit(text.charCodeAt(pos))) {
|
|
41
|
+
pos++;
|
|
42
|
+
while (pos < text.length && isDigit(text.charCodeAt(pos))) pos++;
|
|
43
|
+
} else {
|
|
44
|
+
scanError = 3;
|
|
45
|
+
return text.substring(start, pos);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
let end = pos;
|
|
49
|
+
if (pos < text.length && (text.charCodeAt(pos) === 69 || text.charCodeAt(pos) === 101)) {
|
|
50
|
+
pos++;
|
|
51
|
+
if (pos < text.length && text.charCodeAt(pos) === 43 || text.charCodeAt(pos) === 45) pos++;
|
|
52
|
+
if (pos < text.length && isDigit(text.charCodeAt(pos))) {
|
|
53
|
+
pos++;
|
|
54
|
+
while (pos < text.length && isDigit(text.charCodeAt(pos))) pos++;
|
|
55
|
+
end = pos;
|
|
56
|
+
} else scanError = 3;
|
|
57
|
+
}
|
|
58
|
+
return text.substring(start, end);
|
|
59
|
+
}
|
|
60
|
+
function scanString() {
|
|
61
|
+
let result = "", start = pos;
|
|
62
|
+
while (true) {
|
|
63
|
+
if (pos >= len) {
|
|
64
|
+
result += text.substring(start, pos);
|
|
65
|
+
scanError = 2;
|
|
66
|
+
break;
|
|
67
|
+
}
|
|
68
|
+
const ch = text.charCodeAt(pos);
|
|
69
|
+
if (ch === 34) {
|
|
70
|
+
result += text.substring(start, pos);
|
|
71
|
+
pos++;
|
|
72
|
+
break;
|
|
73
|
+
}
|
|
74
|
+
if (ch === 92) {
|
|
75
|
+
result += text.substring(start, pos);
|
|
76
|
+
pos++;
|
|
77
|
+
if (pos >= len) {
|
|
78
|
+
scanError = 2;
|
|
79
|
+
break;
|
|
80
|
+
}
|
|
81
|
+
switch (text.charCodeAt(pos++)) {
|
|
82
|
+
case 34:
|
|
83
|
+
result += "\"";
|
|
84
|
+
break;
|
|
85
|
+
case 92:
|
|
86
|
+
result += "\\";
|
|
87
|
+
break;
|
|
88
|
+
case 47:
|
|
89
|
+
result += "/";
|
|
90
|
+
break;
|
|
91
|
+
case 98:
|
|
92
|
+
result += "\b";
|
|
93
|
+
break;
|
|
94
|
+
case 102:
|
|
95
|
+
result += "\f";
|
|
96
|
+
break;
|
|
97
|
+
case 110:
|
|
98
|
+
result += "\n";
|
|
99
|
+
break;
|
|
100
|
+
case 114:
|
|
101
|
+
result += "\r";
|
|
102
|
+
break;
|
|
103
|
+
case 116:
|
|
104
|
+
result += " ";
|
|
105
|
+
break;
|
|
106
|
+
case 117:
|
|
107
|
+
const ch3 = scanHexDigits(4, true);
|
|
108
|
+
if (ch3 >= 0) result += String.fromCharCode(ch3);
|
|
109
|
+
else scanError = 4;
|
|
110
|
+
break;
|
|
111
|
+
default: scanError = 5;
|
|
112
|
+
}
|
|
113
|
+
start = pos;
|
|
114
|
+
continue;
|
|
115
|
+
}
|
|
116
|
+
if (ch >= 0 && ch <= 31) {
|
|
117
|
+
if (isLineBreak(ch)) {
|
|
118
|
+
result += text.substring(start, pos);
|
|
119
|
+
scanError = 2;
|
|
120
|
+
break;
|
|
121
|
+
} else scanError = 6;
|
|
122
|
+
}
|
|
123
|
+
pos++;
|
|
124
|
+
}
|
|
125
|
+
return result;
|
|
126
|
+
}
|
|
127
|
+
function scanNext() {
|
|
128
|
+
value = "";
|
|
129
|
+
scanError = 0;
|
|
130
|
+
tokenOffset = pos;
|
|
131
|
+
lineStartOffset = lineNumber;
|
|
132
|
+
prevTokenLineStartOffset = tokenLineStartOffset;
|
|
133
|
+
if (pos >= len) {
|
|
134
|
+
tokenOffset = len;
|
|
135
|
+
return token = 17;
|
|
136
|
+
}
|
|
137
|
+
let code = text.charCodeAt(pos);
|
|
138
|
+
if (isWhiteSpace(code)) {
|
|
139
|
+
do {
|
|
140
|
+
pos++;
|
|
141
|
+
value += String.fromCharCode(code);
|
|
142
|
+
code = text.charCodeAt(pos);
|
|
143
|
+
} while (isWhiteSpace(code));
|
|
144
|
+
return token = 15;
|
|
145
|
+
}
|
|
146
|
+
if (isLineBreak(code)) {
|
|
147
|
+
pos++;
|
|
148
|
+
value += String.fromCharCode(code);
|
|
149
|
+
if (code === 13 && text.charCodeAt(pos) === 10) {
|
|
150
|
+
pos++;
|
|
151
|
+
value += "\n";
|
|
152
|
+
}
|
|
153
|
+
lineNumber++;
|
|
154
|
+
tokenLineStartOffset = pos;
|
|
155
|
+
return token = 14;
|
|
156
|
+
}
|
|
157
|
+
switch (code) {
|
|
158
|
+
case 123:
|
|
159
|
+
pos++;
|
|
160
|
+
return token = 1;
|
|
161
|
+
case 125:
|
|
162
|
+
pos++;
|
|
163
|
+
return token = 2;
|
|
164
|
+
case 91:
|
|
165
|
+
pos++;
|
|
166
|
+
return token = 3;
|
|
167
|
+
case 93:
|
|
168
|
+
pos++;
|
|
169
|
+
return token = 4;
|
|
170
|
+
case 58:
|
|
171
|
+
pos++;
|
|
172
|
+
return token = 6;
|
|
173
|
+
case 44:
|
|
174
|
+
pos++;
|
|
175
|
+
return token = 5;
|
|
176
|
+
case 34:
|
|
177
|
+
pos++;
|
|
178
|
+
value = scanString();
|
|
179
|
+
return token = 10;
|
|
180
|
+
case 47:
|
|
181
|
+
const start = pos - 1;
|
|
182
|
+
if (text.charCodeAt(pos + 1) === 47) {
|
|
183
|
+
pos += 2;
|
|
184
|
+
while (pos < len) {
|
|
185
|
+
if (isLineBreak(text.charCodeAt(pos))) break;
|
|
186
|
+
pos++;
|
|
187
|
+
}
|
|
188
|
+
value = text.substring(start, pos);
|
|
189
|
+
return token = 12;
|
|
190
|
+
}
|
|
191
|
+
if (text.charCodeAt(pos + 1) === 42) {
|
|
192
|
+
pos += 2;
|
|
193
|
+
const safeLength = len - 1;
|
|
194
|
+
let commentClosed = false;
|
|
195
|
+
while (pos < safeLength) {
|
|
196
|
+
const ch = text.charCodeAt(pos);
|
|
197
|
+
if (ch === 42 && text.charCodeAt(pos + 1) === 47) {
|
|
198
|
+
pos += 2;
|
|
199
|
+
commentClosed = true;
|
|
200
|
+
break;
|
|
201
|
+
}
|
|
202
|
+
pos++;
|
|
203
|
+
if (isLineBreak(ch)) {
|
|
204
|
+
if (ch === 13 && text.charCodeAt(pos) === 10) pos++;
|
|
205
|
+
lineNumber++;
|
|
206
|
+
tokenLineStartOffset = pos;
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
if (!commentClosed) {
|
|
210
|
+
pos++;
|
|
211
|
+
scanError = 1;
|
|
212
|
+
}
|
|
213
|
+
value = text.substring(start, pos);
|
|
214
|
+
return token = 13;
|
|
215
|
+
}
|
|
216
|
+
value += String.fromCharCode(code);
|
|
217
|
+
pos++;
|
|
218
|
+
return token = 16;
|
|
219
|
+
case 45:
|
|
220
|
+
value += String.fromCharCode(code);
|
|
221
|
+
pos++;
|
|
222
|
+
if (pos === len || !isDigit(text.charCodeAt(pos))) return token = 16;
|
|
223
|
+
case 48:
|
|
224
|
+
case 49:
|
|
225
|
+
case 50:
|
|
226
|
+
case 51:
|
|
227
|
+
case 52:
|
|
228
|
+
case 53:
|
|
229
|
+
case 54:
|
|
230
|
+
case 55:
|
|
231
|
+
case 56:
|
|
232
|
+
case 57:
|
|
233
|
+
value += scanNumber();
|
|
234
|
+
return token = 11;
|
|
235
|
+
default:
|
|
236
|
+
while (pos < len && isUnknownContentCharacter(code)) {
|
|
237
|
+
pos++;
|
|
238
|
+
code = text.charCodeAt(pos);
|
|
239
|
+
}
|
|
240
|
+
if (tokenOffset !== pos) {
|
|
241
|
+
value = text.substring(tokenOffset, pos);
|
|
242
|
+
switch (value) {
|
|
243
|
+
case "true": return token = 8;
|
|
244
|
+
case "false": return token = 9;
|
|
245
|
+
case "null": return token = 7;
|
|
246
|
+
}
|
|
247
|
+
return token = 16;
|
|
248
|
+
}
|
|
249
|
+
value += String.fromCharCode(code);
|
|
250
|
+
pos++;
|
|
251
|
+
return token = 16;
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
function isUnknownContentCharacter(code) {
|
|
255
|
+
if (isWhiteSpace(code) || isLineBreak(code)) return false;
|
|
256
|
+
switch (code) {
|
|
257
|
+
case 125:
|
|
258
|
+
case 93:
|
|
259
|
+
case 123:
|
|
260
|
+
case 91:
|
|
261
|
+
case 34:
|
|
262
|
+
case 58:
|
|
263
|
+
case 44:
|
|
264
|
+
case 47: return false;
|
|
265
|
+
}
|
|
266
|
+
return true;
|
|
267
|
+
}
|
|
268
|
+
function scanNextNonTrivia() {
|
|
269
|
+
let result;
|
|
270
|
+
do
|
|
271
|
+
result = scanNext();
|
|
272
|
+
while (result >= 12 && result <= 15);
|
|
273
|
+
return result;
|
|
274
|
+
}
|
|
275
|
+
return {
|
|
276
|
+
setPosition,
|
|
277
|
+
getPosition: () => pos,
|
|
278
|
+
scan: ignoreTrivia ? scanNextNonTrivia : scanNext,
|
|
279
|
+
getToken: () => token,
|
|
280
|
+
getTokenValue: () => value,
|
|
281
|
+
getTokenOffset: () => tokenOffset,
|
|
282
|
+
getTokenLength: () => pos - tokenOffset,
|
|
283
|
+
getTokenStartLine: () => lineStartOffset,
|
|
284
|
+
getTokenStartCharacter: () => tokenOffset - prevTokenLineStartOffset,
|
|
285
|
+
getTokenError: () => scanError
|
|
286
|
+
};
|
|
287
|
+
}
|
|
288
|
+
function isWhiteSpace(ch) {
|
|
289
|
+
return ch === 32 || ch === 9;
|
|
290
|
+
}
|
|
291
|
+
function isLineBreak(ch) {
|
|
292
|
+
return ch === 10 || ch === 13;
|
|
293
|
+
}
|
|
294
|
+
function isDigit(ch) {
|
|
295
|
+
return ch >= 48 && ch <= 57;
|
|
296
|
+
}
|
|
297
|
+
var CharacterCodes;
|
|
298
|
+
(function(CharacterCodes) {
|
|
299
|
+
CharacterCodes[CharacterCodes["lineFeed"] = 10] = "lineFeed";
|
|
300
|
+
CharacterCodes[CharacterCodes["carriageReturn"] = 13] = "carriageReturn";
|
|
301
|
+
CharacterCodes[CharacterCodes["space"] = 32] = "space";
|
|
302
|
+
CharacterCodes[CharacterCodes["_0"] = 48] = "_0";
|
|
303
|
+
CharacterCodes[CharacterCodes["_1"] = 49] = "_1";
|
|
304
|
+
CharacterCodes[CharacterCodes["_2"] = 50] = "_2";
|
|
305
|
+
CharacterCodes[CharacterCodes["_3"] = 51] = "_3";
|
|
306
|
+
CharacterCodes[CharacterCodes["_4"] = 52] = "_4";
|
|
307
|
+
CharacterCodes[CharacterCodes["_5"] = 53] = "_5";
|
|
308
|
+
CharacterCodes[CharacterCodes["_6"] = 54] = "_6";
|
|
309
|
+
CharacterCodes[CharacterCodes["_7"] = 55] = "_7";
|
|
310
|
+
CharacterCodes[CharacterCodes["_8"] = 56] = "_8";
|
|
311
|
+
CharacterCodes[CharacterCodes["_9"] = 57] = "_9";
|
|
312
|
+
CharacterCodes[CharacterCodes["a"] = 97] = "a";
|
|
313
|
+
CharacterCodes[CharacterCodes["b"] = 98] = "b";
|
|
314
|
+
CharacterCodes[CharacterCodes["c"] = 99] = "c";
|
|
315
|
+
CharacterCodes[CharacterCodes["d"] = 100] = "d";
|
|
316
|
+
CharacterCodes[CharacterCodes["e"] = 101] = "e";
|
|
317
|
+
CharacterCodes[CharacterCodes["f"] = 102] = "f";
|
|
318
|
+
CharacterCodes[CharacterCodes["g"] = 103] = "g";
|
|
319
|
+
CharacterCodes[CharacterCodes["h"] = 104] = "h";
|
|
320
|
+
CharacterCodes[CharacterCodes["i"] = 105] = "i";
|
|
321
|
+
CharacterCodes[CharacterCodes["j"] = 106] = "j";
|
|
322
|
+
CharacterCodes[CharacterCodes["k"] = 107] = "k";
|
|
323
|
+
CharacterCodes[CharacterCodes["l"] = 108] = "l";
|
|
324
|
+
CharacterCodes[CharacterCodes["m"] = 109] = "m";
|
|
325
|
+
CharacterCodes[CharacterCodes["n"] = 110] = "n";
|
|
326
|
+
CharacterCodes[CharacterCodes["o"] = 111] = "o";
|
|
327
|
+
CharacterCodes[CharacterCodes["p"] = 112] = "p";
|
|
328
|
+
CharacterCodes[CharacterCodes["q"] = 113] = "q";
|
|
329
|
+
CharacterCodes[CharacterCodes["r"] = 114] = "r";
|
|
330
|
+
CharacterCodes[CharacterCodes["s"] = 115] = "s";
|
|
331
|
+
CharacterCodes[CharacterCodes["t"] = 116] = "t";
|
|
332
|
+
CharacterCodes[CharacterCodes["u"] = 117] = "u";
|
|
333
|
+
CharacterCodes[CharacterCodes["v"] = 118] = "v";
|
|
334
|
+
CharacterCodes[CharacterCodes["w"] = 119] = "w";
|
|
335
|
+
CharacterCodes[CharacterCodes["x"] = 120] = "x";
|
|
336
|
+
CharacterCodes[CharacterCodes["y"] = 121] = "y";
|
|
337
|
+
CharacterCodes[CharacterCodes["z"] = 122] = "z";
|
|
338
|
+
CharacterCodes[CharacterCodes["A"] = 65] = "A";
|
|
339
|
+
CharacterCodes[CharacterCodes["B"] = 66] = "B";
|
|
340
|
+
CharacterCodes[CharacterCodes["C"] = 67] = "C";
|
|
341
|
+
CharacterCodes[CharacterCodes["D"] = 68] = "D";
|
|
342
|
+
CharacterCodes[CharacterCodes["E"] = 69] = "E";
|
|
343
|
+
CharacterCodes[CharacterCodes["F"] = 70] = "F";
|
|
344
|
+
CharacterCodes[CharacterCodes["G"] = 71] = "G";
|
|
345
|
+
CharacterCodes[CharacterCodes["H"] = 72] = "H";
|
|
346
|
+
CharacterCodes[CharacterCodes["I"] = 73] = "I";
|
|
347
|
+
CharacterCodes[CharacterCodes["J"] = 74] = "J";
|
|
348
|
+
CharacterCodes[CharacterCodes["K"] = 75] = "K";
|
|
349
|
+
CharacterCodes[CharacterCodes["L"] = 76] = "L";
|
|
350
|
+
CharacterCodes[CharacterCodes["M"] = 77] = "M";
|
|
351
|
+
CharacterCodes[CharacterCodes["N"] = 78] = "N";
|
|
352
|
+
CharacterCodes[CharacterCodes["O"] = 79] = "O";
|
|
353
|
+
CharacterCodes[CharacterCodes["P"] = 80] = "P";
|
|
354
|
+
CharacterCodes[CharacterCodes["Q"] = 81] = "Q";
|
|
355
|
+
CharacterCodes[CharacterCodes["R"] = 82] = "R";
|
|
356
|
+
CharacterCodes[CharacterCodes["S"] = 83] = "S";
|
|
357
|
+
CharacterCodes[CharacterCodes["T"] = 84] = "T";
|
|
358
|
+
CharacterCodes[CharacterCodes["U"] = 85] = "U";
|
|
359
|
+
CharacterCodes[CharacterCodes["V"] = 86] = "V";
|
|
360
|
+
CharacterCodes[CharacterCodes["W"] = 87] = "W";
|
|
361
|
+
CharacterCodes[CharacterCodes["X"] = 88] = "X";
|
|
362
|
+
CharacterCodes[CharacterCodes["Y"] = 89] = "Y";
|
|
363
|
+
CharacterCodes[CharacterCodes["Z"] = 90] = "Z";
|
|
364
|
+
CharacterCodes[CharacterCodes["asterisk"] = 42] = "asterisk";
|
|
365
|
+
CharacterCodes[CharacterCodes["backslash"] = 92] = "backslash";
|
|
366
|
+
CharacterCodes[CharacterCodes["closeBrace"] = 125] = "closeBrace";
|
|
367
|
+
CharacterCodes[CharacterCodes["closeBracket"] = 93] = "closeBracket";
|
|
368
|
+
CharacterCodes[CharacterCodes["colon"] = 58] = "colon";
|
|
369
|
+
CharacterCodes[CharacterCodes["comma"] = 44] = "comma";
|
|
370
|
+
CharacterCodes[CharacterCodes["dot"] = 46] = "dot";
|
|
371
|
+
CharacterCodes[CharacterCodes["doubleQuote"] = 34] = "doubleQuote";
|
|
372
|
+
CharacterCodes[CharacterCodes["minus"] = 45] = "minus";
|
|
373
|
+
CharacterCodes[CharacterCodes["openBrace"] = 123] = "openBrace";
|
|
374
|
+
CharacterCodes[CharacterCodes["openBracket"] = 91] = "openBracket";
|
|
375
|
+
CharacterCodes[CharacterCodes["plus"] = 43] = "plus";
|
|
376
|
+
CharacterCodes[CharacterCodes["slash"] = 47] = "slash";
|
|
377
|
+
CharacterCodes[CharacterCodes["formFeed"] = 12] = "formFeed";
|
|
378
|
+
CharacterCodes[CharacterCodes["tab"] = 9] = "tab";
|
|
379
|
+
})(CharacterCodes || (CharacterCodes = {}));
|
|
380
|
+
// ../../node_modules/.pnpm/jsonc-parser@3.3.1/node_modules/jsonc-parser/lib/esm/impl/string-intern.js
|
|
381
|
+
const cachedSpaces = new Array(20).fill(0).map((_, index) => {
|
|
382
|
+
return " ".repeat(index);
|
|
383
|
+
});
|
|
384
|
+
const maxCachedValues = 200;
|
|
385
|
+
const cachedBreakLinesWithSpaces = {
|
|
386
|
+
" ": {
|
|
387
|
+
"\n": new Array(maxCachedValues).fill(0).map((_, index) => {
|
|
388
|
+
return "\n" + " ".repeat(index);
|
|
389
|
+
}),
|
|
390
|
+
"\r": new Array(maxCachedValues).fill(0).map((_, index) => {
|
|
391
|
+
return "\r" + " ".repeat(index);
|
|
392
|
+
}),
|
|
393
|
+
"\r\n": new Array(maxCachedValues).fill(0).map((_, index) => {
|
|
394
|
+
return "\r\n" + " ".repeat(index);
|
|
395
|
+
})
|
|
396
|
+
},
|
|
397
|
+
" ": {
|
|
398
|
+
"\n": new Array(maxCachedValues).fill(0).map((_, index) => {
|
|
399
|
+
return "\n" + " ".repeat(index);
|
|
400
|
+
}),
|
|
401
|
+
"\r": new Array(maxCachedValues).fill(0).map((_, index) => {
|
|
402
|
+
return "\r" + " ".repeat(index);
|
|
403
|
+
}),
|
|
404
|
+
"\r\n": new Array(maxCachedValues).fill(0).map((_, index) => {
|
|
405
|
+
return "\r\n" + " ".repeat(index);
|
|
406
|
+
})
|
|
407
|
+
}
|
|
408
|
+
};
|
|
409
|
+
const supportedEols = [
|
|
410
|
+
"\n",
|
|
411
|
+
"\r",
|
|
412
|
+
"\r\n"
|
|
413
|
+
];
|
|
414
|
+
// ../../node_modules/.pnpm/jsonc-parser@3.3.1/node_modules/jsonc-parser/lib/esm/impl/format.js
|
|
415
|
+
function format(documentText, range, options) {
|
|
416
|
+
let initialIndentLevel;
|
|
417
|
+
let formatText;
|
|
418
|
+
let formatTextStart;
|
|
419
|
+
let rangeStart;
|
|
420
|
+
let rangeEnd;
|
|
421
|
+
if (range) {
|
|
422
|
+
rangeStart = range.offset;
|
|
423
|
+
rangeEnd = rangeStart + range.length;
|
|
424
|
+
formatTextStart = rangeStart;
|
|
425
|
+
while (formatTextStart > 0 && !isEOL(documentText, formatTextStart - 1)) formatTextStart--;
|
|
426
|
+
let endOffset = rangeEnd;
|
|
427
|
+
while (endOffset < documentText.length && !isEOL(documentText, endOffset)) endOffset++;
|
|
428
|
+
formatText = documentText.substring(formatTextStart, endOffset);
|
|
429
|
+
initialIndentLevel = computeIndentLevel(formatText, options);
|
|
430
|
+
} else {
|
|
431
|
+
formatText = documentText;
|
|
432
|
+
initialIndentLevel = 0;
|
|
433
|
+
formatTextStart = 0;
|
|
434
|
+
rangeStart = 0;
|
|
435
|
+
rangeEnd = documentText.length;
|
|
436
|
+
}
|
|
437
|
+
const eol = getEOL(options, documentText);
|
|
438
|
+
const eolFastPathSupported = supportedEols.includes(eol);
|
|
439
|
+
let numberLineBreaks = 0;
|
|
440
|
+
let indentLevel = 0;
|
|
441
|
+
let indentValue;
|
|
442
|
+
if (options.insertSpaces) indentValue = cachedSpaces[options.tabSize || 4] ?? repeat(cachedSpaces[1], options.tabSize || 4);
|
|
443
|
+
else indentValue = " ";
|
|
444
|
+
const indentType = indentValue === " " ? " " : " ";
|
|
445
|
+
let scanner = createScanner(formatText, false);
|
|
446
|
+
let hasError = false;
|
|
447
|
+
function newLinesAndIndent() {
|
|
448
|
+
if (numberLineBreaks > 1) return repeat(eol, numberLineBreaks) + repeat(indentValue, initialIndentLevel + indentLevel);
|
|
449
|
+
const amountOfSpaces = indentValue.length * (initialIndentLevel + indentLevel);
|
|
450
|
+
if (!eolFastPathSupported || amountOfSpaces > cachedBreakLinesWithSpaces[indentType][eol].length) return eol + repeat(indentValue, initialIndentLevel + indentLevel);
|
|
451
|
+
if (amountOfSpaces <= 0) return eol;
|
|
452
|
+
return cachedBreakLinesWithSpaces[indentType][eol][amountOfSpaces];
|
|
453
|
+
}
|
|
454
|
+
function scanNext() {
|
|
455
|
+
let token = scanner.scan();
|
|
456
|
+
numberLineBreaks = 0;
|
|
457
|
+
while (token === 15 || token === 14) {
|
|
458
|
+
if (token === 14 && options.keepLines) numberLineBreaks += 1;
|
|
459
|
+
else if (token === 14) numberLineBreaks = 1;
|
|
460
|
+
token = scanner.scan();
|
|
461
|
+
}
|
|
462
|
+
hasError = token === 16 || scanner.getTokenError() !== 0;
|
|
463
|
+
return token;
|
|
464
|
+
}
|
|
465
|
+
const editOperations = [];
|
|
466
|
+
function addEdit(text, startOffset, endOffset) {
|
|
467
|
+
if (!hasError && (!range || startOffset < rangeEnd && endOffset > rangeStart) && documentText.substring(startOffset, endOffset) !== text) editOperations.push({
|
|
468
|
+
offset: startOffset,
|
|
469
|
+
length: endOffset - startOffset,
|
|
470
|
+
content: text
|
|
471
|
+
});
|
|
472
|
+
}
|
|
473
|
+
let firstToken = scanNext();
|
|
474
|
+
if (options.keepLines && numberLineBreaks > 0) addEdit(repeat(eol, numberLineBreaks), 0, 0);
|
|
475
|
+
if (firstToken !== 17) {
|
|
476
|
+
let firstTokenStart = scanner.getTokenOffset() + formatTextStart;
|
|
477
|
+
addEdit(indentValue.length * initialIndentLevel < 20 && options.insertSpaces ? cachedSpaces[indentValue.length * initialIndentLevel] : repeat(indentValue, initialIndentLevel), formatTextStart, firstTokenStart);
|
|
478
|
+
}
|
|
479
|
+
while (firstToken !== 17) {
|
|
480
|
+
let firstTokenEnd = scanner.getTokenOffset() + scanner.getTokenLength() + formatTextStart;
|
|
481
|
+
let secondToken = scanNext();
|
|
482
|
+
let replaceContent = "";
|
|
483
|
+
let needsLineBreak = false;
|
|
484
|
+
while (numberLineBreaks === 0 && (secondToken === 12 || secondToken === 13)) {
|
|
485
|
+
let commentTokenStart = scanner.getTokenOffset() + formatTextStart;
|
|
486
|
+
addEdit(cachedSpaces[1], firstTokenEnd, commentTokenStart);
|
|
487
|
+
firstTokenEnd = scanner.getTokenOffset() + scanner.getTokenLength() + formatTextStart;
|
|
488
|
+
needsLineBreak = secondToken === 12;
|
|
489
|
+
replaceContent = needsLineBreak ? newLinesAndIndent() : "";
|
|
490
|
+
secondToken = scanNext();
|
|
491
|
+
}
|
|
492
|
+
if (secondToken === 2) {
|
|
493
|
+
if (firstToken !== 1) indentLevel--;
|
|
494
|
+
if (options.keepLines && numberLineBreaks > 0 || !options.keepLines && firstToken !== 1) replaceContent = newLinesAndIndent();
|
|
495
|
+
else if (options.keepLines) replaceContent = cachedSpaces[1];
|
|
496
|
+
} else if (secondToken === 4) {
|
|
497
|
+
if (firstToken !== 3) indentLevel--;
|
|
498
|
+
if (options.keepLines && numberLineBreaks > 0 || !options.keepLines && firstToken !== 3) replaceContent = newLinesAndIndent();
|
|
499
|
+
else if (options.keepLines) replaceContent = cachedSpaces[1];
|
|
500
|
+
} else {
|
|
501
|
+
switch (firstToken) {
|
|
502
|
+
case 3:
|
|
503
|
+
case 1:
|
|
504
|
+
indentLevel++;
|
|
505
|
+
if (options.keepLines && numberLineBreaks > 0 || !options.keepLines) replaceContent = newLinesAndIndent();
|
|
506
|
+
else replaceContent = cachedSpaces[1];
|
|
507
|
+
break;
|
|
508
|
+
case 5:
|
|
509
|
+
if (options.keepLines && numberLineBreaks > 0 || !options.keepLines) replaceContent = newLinesAndIndent();
|
|
510
|
+
else replaceContent = cachedSpaces[1];
|
|
511
|
+
break;
|
|
512
|
+
case 12:
|
|
513
|
+
replaceContent = newLinesAndIndent();
|
|
514
|
+
break;
|
|
515
|
+
case 13:
|
|
516
|
+
if (numberLineBreaks > 0) replaceContent = newLinesAndIndent();
|
|
517
|
+
else if (!needsLineBreak) replaceContent = cachedSpaces[1];
|
|
518
|
+
break;
|
|
519
|
+
case 6:
|
|
520
|
+
if (options.keepLines && numberLineBreaks > 0) replaceContent = newLinesAndIndent();
|
|
521
|
+
else if (!needsLineBreak) replaceContent = cachedSpaces[1];
|
|
522
|
+
break;
|
|
523
|
+
case 10:
|
|
524
|
+
if (options.keepLines && numberLineBreaks > 0) replaceContent = newLinesAndIndent();
|
|
525
|
+
else if (secondToken === 6 && !needsLineBreak) replaceContent = "";
|
|
526
|
+
break;
|
|
527
|
+
case 7:
|
|
528
|
+
case 8:
|
|
529
|
+
case 9:
|
|
530
|
+
case 11:
|
|
531
|
+
case 2:
|
|
532
|
+
case 4:
|
|
533
|
+
if (options.keepLines && numberLineBreaks > 0) replaceContent = newLinesAndIndent();
|
|
534
|
+
else if ((secondToken === 12 || secondToken === 13) && !needsLineBreak) replaceContent = cachedSpaces[1];
|
|
535
|
+
else if (secondToken !== 5 && secondToken !== 17) hasError = true;
|
|
536
|
+
break;
|
|
537
|
+
case 16: hasError = true;
|
|
538
|
+
}
|
|
539
|
+
if (numberLineBreaks > 0 && (secondToken === 12 || secondToken === 13)) replaceContent = newLinesAndIndent();
|
|
540
|
+
}
|
|
541
|
+
if (secondToken === 17) {
|
|
542
|
+
if (options.keepLines && numberLineBreaks > 0) replaceContent = newLinesAndIndent();
|
|
543
|
+
else replaceContent = options.insertFinalNewline ? eol : "";
|
|
544
|
+
}
|
|
545
|
+
const secondTokenStart = scanner.getTokenOffset() + formatTextStart;
|
|
546
|
+
addEdit(replaceContent, firstTokenEnd, secondTokenStart);
|
|
547
|
+
firstToken = secondToken;
|
|
548
|
+
}
|
|
549
|
+
return editOperations;
|
|
550
|
+
}
|
|
551
|
+
function repeat(s, count) {
|
|
552
|
+
let result = "";
|
|
553
|
+
for (let i = 0; i < count; i++) result += s;
|
|
554
|
+
return result;
|
|
555
|
+
}
|
|
556
|
+
function computeIndentLevel(content, options) {
|
|
557
|
+
let i = 0;
|
|
558
|
+
let nChars = 0;
|
|
559
|
+
const tabSize = options.tabSize || 4;
|
|
560
|
+
while (i < content.length) {
|
|
561
|
+
let ch = content.charAt(i);
|
|
562
|
+
if (ch === cachedSpaces[1]) nChars++;
|
|
563
|
+
else if (ch === " ") nChars += tabSize;
|
|
564
|
+
else break;
|
|
565
|
+
i++;
|
|
566
|
+
}
|
|
567
|
+
return Math.floor(nChars / tabSize);
|
|
568
|
+
}
|
|
569
|
+
function getEOL(options, text) {
|
|
570
|
+
for (let i = 0; i < text.length; i++) {
|
|
571
|
+
const ch = text.charAt(i);
|
|
572
|
+
if (ch === "\r") {
|
|
573
|
+
if (i + 1 < text.length && text.charAt(i + 1) === "\n") return "\r\n";
|
|
574
|
+
return "\r";
|
|
575
|
+
} else if (ch === "\n") return "\n";
|
|
576
|
+
}
|
|
577
|
+
return options && options.eol || "\n";
|
|
578
|
+
}
|
|
579
|
+
function isEOL(text, offset) {
|
|
580
|
+
return "\r\n".indexOf(text.charAt(offset)) !== -1;
|
|
581
|
+
}
|
|
582
|
+
// ../../node_modules/.pnpm/jsonc-parser@3.3.1/node_modules/jsonc-parser/lib/esm/impl/parser.js
|
|
583
|
+
var ParseOptions;
|
|
584
|
+
(function(ParseOptions) {
|
|
585
|
+
ParseOptions.DEFAULT = { allowTrailingComma: false };
|
|
586
|
+
})(ParseOptions || (ParseOptions = {}));
|
|
587
|
+
/**
|
|
588
|
+
* Parses the given text and returns a tree representation the JSON content. On invalid input, the parser tries to be as fault tolerant as possible, but still return a result.
|
|
589
|
+
*/
|
|
590
|
+
function parseTree$1(text, errors = [], options = ParseOptions.DEFAULT) {
|
|
591
|
+
let currentParent = {
|
|
592
|
+
type: "array",
|
|
593
|
+
offset: -1,
|
|
594
|
+
length: -1,
|
|
595
|
+
children: [],
|
|
596
|
+
parent: void 0
|
|
597
|
+
};
|
|
598
|
+
function ensurePropertyComplete(endOffset) {
|
|
599
|
+
if (currentParent.type === "property") {
|
|
600
|
+
currentParent.length = endOffset - currentParent.offset;
|
|
601
|
+
currentParent = currentParent.parent;
|
|
602
|
+
}
|
|
603
|
+
}
|
|
604
|
+
function onValue(valueNode) {
|
|
605
|
+
currentParent.children.push(valueNode);
|
|
606
|
+
return valueNode;
|
|
607
|
+
}
|
|
608
|
+
visit(text, {
|
|
609
|
+
onObjectBegin: (offset) => {
|
|
610
|
+
currentParent = onValue({
|
|
611
|
+
type: "object",
|
|
612
|
+
offset,
|
|
613
|
+
length: -1,
|
|
614
|
+
parent: currentParent,
|
|
615
|
+
children: []
|
|
616
|
+
});
|
|
617
|
+
},
|
|
618
|
+
onObjectProperty: (name, offset, length) => {
|
|
619
|
+
currentParent = onValue({
|
|
620
|
+
type: "property",
|
|
621
|
+
offset,
|
|
622
|
+
length: -1,
|
|
623
|
+
parent: currentParent,
|
|
624
|
+
children: []
|
|
625
|
+
});
|
|
626
|
+
currentParent.children.push({
|
|
627
|
+
type: "string",
|
|
628
|
+
value: name,
|
|
629
|
+
offset,
|
|
630
|
+
length,
|
|
631
|
+
parent: currentParent
|
|
632
|
+
});
|
|
633
|
+
},
|
|
634
|
+
onObjectEnd: (offset, length) => {
|
|
635
|
+
ensurePropertyComplete(offset + length);
|
|
636
|
+
currentParent.length = offset + length - currentParent.offset;
|
|
637
|
+
currentParent = currentParent.parent;
|
|
638
|
+
ensurePropertyComplete(offset + length);
|
|
639
|
+
},
|
|
640
|
+
onArrayBegin: (offset, length) => {
|
|
641
|
+
currentParent = onValue({
|
|
642
|
+
type: "array",
|
|
643
|
+
offset,
|
|
644
|
+
length: -1,
|
|
645
|
+
parent: currentParent,
|
|
646
|
+
children: []
|
|
647
|
+
});
|
|
648
|
+
},
|
|
649
|
+
onArrayEnd: (offset, length) => {
|
|
650
|
+
currentParent.length = offset + length - currentParent.offset;
|
|
651
|
+
currentParent = currentParent.parent;
|
|
652
|
+
ensurePropertyComplete(offset + length);
|
|
653
|
+
},
|
|
654
|
+
onLiteralValue: (value, offset, length) => {
|
|
655
|
+
onValue({
|
|
656
|
+
type: getNodeType(value),
|
|
657
|
+
offset,
|
|
658
|
+
length,
|
|
659
|
+
parent: currentParent,
|
|
660
|
+
value
|
|
661
|
+
});
|
|
662
|
+
ensurePropertyComplete(offset + length);
|
|
663
|
+
},
|
|
664
|
+
onSeparator: (sep, offset, length) => {
|
|
665
|
+
if (currentParent.type === "property") {
|
|
666
|
+
if (sep === ":") currentParent.colonOffset = offset;
|
|
667
|
+
else if (sep === ",") ensurePropertyComplete(offset);
|
|
668
|
+
}
|
|
669
|
+
},
|
|
670
|
+
onError: (error, offset, length) => {
|
|
671
|
+
errors.push({
|
|
672
|
+
error,
|
|
673
|
+
offset,
|
|
674
|
+
length
|
|
675
|
+
});
|
|
676
|
+
}
|
|
677
|
+
}, options);
|
|
678
|
+
const result = currentParent.children[0];
|
|
679
|
+
if (result) delete result.parent;
|
|
680
|
+
return result;
|
|
681
|
+
}
|
|
682
|
+
/**
|
|
683
|
+
* Finds the node at the given path in a JSON DOM.
|
|
684
|
+
*/
|
|
685
|
+
function findNodeAtLocation$1(root, path) {
|
|
686
|
+
if (!root) return;
|
|
687
|
+
let node = root;
|
|
688
|
+
for (let segment of path) if (typeof segment === "string") {
|
|
689
|
+
if (node.type !== "object" || !Array.isArray(node.children)) return;
|
|
690
|
+
let found = false;
|
|
691
|
+
for (const propertyNode of node.children) if (Array.isArray(propertyNode.children) && propertyNode.children[0].value === segment && propertyNode.children.length === 2) {
|
|
692
|
+
node = propertyNode.children[1];
|
|
693
|
+
found = true;
|
|
694
|
+
break;
|
|
695
|
+
}
|
|
696
|
+
if (!found) return;
|
|
697
|
+
} else {
|
|
698
|
+
const index = segment;
|
|
699
|
+
if (node.type !== "array" || index < 0 || !Array.isArray(node.children) || index >= node.children.length) return;
|
|
700
|
+
node = node.children[index];
|
|
701
|
+
}
|
|
702
|
+
return node;
|
|
703
|
+
}
|
|
704
|
+
/**
|
|
705
|
+
* Parses the given text and invokes the visitor functions for each object, array and literal reached.
|
|
706
|
+
*/
|
|
707
|
+
function visit(text, visitor, options = ParseOptions.DEFAULT) {
|
|
708
|
+
const _scanner = createScanner(text, false);
|
|
709
|
+
const _jsonPath = [];
|
|
710
|
+
let suppressedCallbacks = 0;
|
|
711
|
+
function toNoArgVisit(visitFunction) {
|
|
712
|
+
return visitFunction ? () => suppressedCallbacks === 0 && visitFunction(_scanner.getTokenOffset(), _scanner.getTokenLength(), _scanner.getTokenStartLine(), _scanner.getTokenStartCharacter()) : () => true;
|
|
713
|
+
}
|
|
714
|
+
function toOneArgVisit(visitFunction) {
|
|
715
|
+
return visitFunction ? (arg) => suppressedCallbacks === 0 && visitFunction(arg, _scanner.getTokenOffset(), _scanner.getTokenLength(), _scanner.getTokenStartLine(), _scanner.getTokenStartCharacter()) : () => true;
|
|
716
|
+
}
|
|
717
|
+
function toOneArgVisitWithPath(visitFunction) {
|
|
718
|
+
return visitFunction ? (arg) => suppressedCallbacks === 0 && visitFunction(arg, _scanner.getTokenOffset(), _scanner.getTokenLength(), _scanner.getTokenStartLine(), _scanner.getTokenStartCharacter(), () => _jsonPath.slice()) : () => true;
|
|
719
|
+
}
|
|
720
|
+
function toBeginVisit(visitFunction) {
|
|
721
|
+
return visitFunction ? () => {
|
|
722
|
+
if (suppressedCallbacks > 0) suppressedCallbacks++;
|
|
723
|
+
else if (visitFunction(_scanner.getTokenOffset(), _scanner.getTokenLength(), _scanner.getTokenStartLine(), _scanner.getTokenStartCharacter(), () => _jsonPath.slice()) === false) suppressedCallbacks = 1;
|
|
724
|
+
} : () => true;
|
|
725
|
+
}
|
|
726
|
+
function toEndVisit(visitFunction) {
|
|
727
|
+
return visitFunction ? () => {
|
|
728
|
+
if (suppressedCallbacks > 0) suppressedCallbacks--;
|
|
729
|
+
if (suppressedCallbacks === 0) visitFunction(_scanner.getTokenOffset(), _scanner.getTokenLength(), _scanner.getTokenStartLine(), _scanner.getTokenStartCharacter());
|
|
730
|
+
} : () => true;
|
|
731
|
+
}
|
|
732
|
+
const onObjectBegin = toBeginVisit(visitor.onObjectBegin), onObjectProperty = toOneArgVisitWithPath(visitor.onObjectProperty), onObjectEnd = toEndVisit(visitor.onObjectEnd), onArrayBegin = toBeginVisit(visitor.onArrayBegin), onArrayEnd = toEndVisit(visitor.onArrayEnd), onLiteralValue = toOneArgVisitWithPath(visitor.onLiteralValue), onSeparator = toOneArgVisit(visitor.onSeparator), onComment = toNoArgVisit(visitor.onComment), onError = toOneArgVisit(visitor.onError);
|
|
733
|
+
const disallowComments = options && options.disallowComments;
|
|
734
|
+
const allowTrailingComma = options && options.allowTrailingComma;
|
|
735
|
+
function scanNext() {
|
|
736
|
+
while (true) {
|
|
737
|
+
const token = _scanner.scan();
|
|
738
|
+
switch (_scanner.getTokenError()) {
|
|
739
|
+
case 4:
|
|
740
|
+
handleError(14);
|
|
741
|
+
break;
|
|
742
|
+
case 5:
|
|
743
|
+
handleError(15);
|
|
744
|
+
break;
|
|
745
|
+
case 3:
|
|
746
|
+
handleError(13);
|
|
747
|
+
break;
|
|
748
|
+
case 1:
|
|
749
|
+
if (!disallowComments) handleError(11);
|
|
750
|
+
break;
|
|
751
|
+
case 2:
|
|
752
|
+
handleError(12);
|
|
753
|
+
break;
|
|
754
|
+
case 6: handleError(16);
|
|
755
|
+
}
|
|
756
|
+
switch (token) {
|
|
757
|
+
case 12:
|
|
758
|
+
case 13:
|
|
759
|
+
if (disallowComments) handleError(10);
|
|
760
|
+
else onComment();
|
|
761
|
+
break;
|
|
762
|
+
case 16:
|
|
763
|
+
handleError(1);
|
|
764
|
+
break;
|
|
765
|
+
case 15:
|
|
766
|
+
case 14: break;
|
|
767
|
+
default: return token;
|
|
768
|
+
}
|
|
769
|
+
}
|
|
770
|
+
}
|
|
771
|
+
function handleError(error, skipUntilAfter = [], skipUntil = []) {
|
|
772
|
+
onError(error);
|
|
773
|
+
if (skipUntilAfter.length + skipUntil.length > 0) {
|
|
774
|
+
let token = _scanner.getToken();
|
|
775
|
+
while (token !== 17) {
|
|
776
|
+
if (skipUntilAfter.indexOf(token) !== -1) {
|
|
777
|
+
scanNext();
|
|
778
|
+
break;
|
|
779
|
+
} else if (skipUntil.indexOf(token) !== -1) break;
|
|
780
|
+
token = scanNext();
|
|
781
|
+
}
|
|
782
|
+
}
|
|
783
|
+
}
|
|
784
|
+
function parseString(isValue) {
|
|
785
|
+
const value = _scanner.getTokenValue();
|
|
786
|
+
if (isValue) onLiteralValue(value);
|
|
787
|
+
else {
|
|
788
|
+
onObjectProperty(value);
|
|
789
|
+
_jsonPath.push(value);
|
|
790
|
+
}
|
|
791
|
+
scanNext();
|
|
792
|
+
return true;
|
|
793
|
+
}
|
|
794
|
+
function parseLiteral() {
|
|
795
|
+
switch (_scanner.getToken()) {
|
|
796
|
+
case 11:
|
|
797
|
+
const tokenValue = _scanner.getTokenValue();
|
|
798
|
+
let value = Number(tokenValue);
|
|
799
|
+
if (isNaN(value)) {
|
|
800
|
+
handleError(2);
|
|
801
|
+
value = 0;
|
|
802
|
+
}
|
|
803
|
+
onLiteralValue(value);
|
|
804
|
+
break;
|
|
805
|
+
case 7:
|
|
806
|
+
onLiteralValue(null);
|
|
807
|
+
break;
|
|
808
|
+
case 8:
|
|
809
|
+
onLiteralValue(true);
|
|
810
|
+
break;
|
|
811
|
+
case 9:
|
|
812
|
+
onLiteralValue(false);
|
|
813
|
+
break;
|
|
814
|
+
default: return false;
|
|
815
|
+
}
|
|
816
|
+
scanNext();
|
|
817
|
+
return true;
|
|
818
|
+
}
|
|
819
|
+
function parseProperty() {
|
|
820
|
+
if (_scanner.getToken() !== 10) {
|
|
821
|
+
handleError(3, [], [2, 5]);
|
|
822
|
+
return false;
|
|
823
|
+
}
|
|
824
|
+
parseString(false);
|
|
825
|
+
if (_scanner.getToken() === 6) {
|
|
826
|
+
onSeparator(":");
|
|
827
|
+
scanNext();
|
|
828
|
+
if (!parseValue()) handleError(4, [], [2, 5]);
|
|
829
|
+
} else handleError(5, [], [2, 5]);
|
|
830
|
+
_jsonPath.pop();
|
|
831
|
+
return true;
|
|
832
|
+
}
|
|
833
|
+
function parseObject() {
|
|
834
|
+
onObjectBegin();
|
|
835
|
+
scanNext();
|
|
836
|
+
let needsComma = false;
|
|
837
|
+
while (_scanner.getToken() !== 2 && _scanner.getToken() !== 17) {
|
|
838
|
+
if (_scanner.getToken() === 5) {
|
|
839
|
+
if (!needsComma) handleError(4, [], []);
|
|
840
|
+
onSeparator(",");
|
|
841
|
+
scanNext();
|
|
842
|
+
if (_scanner.getToken() === 2 && allowTrailingComma) break;
|
|
843
|
+
} else if (needsComma) handleError(6, [], []);
|
|
844
|
+
if (!parseProperty()) handleError(4, [], [2, 5]);
|
|
845
|
+
needsComma = true;
|
|
846
|
+
}
|
|
847
|
+
onObjectEnd();
|
|
848
|
+
if (_scanner.getToken() !== 2) handleError(7, [2], []);
|
|
849
|
+
else scanNext();
|
|
850
|
+
return true;
|
|
851
|
+
}
|
|
852
|
+
function parseArray() {
|
|
853
|
+
onArrayBegin();
|
|
854
|
+
scanNext();
|
|
855
|
+
let isFirstElement = true;
|
|
856
|
+
let needsComma = false;
|
|
857
|
+
while (_scanner.getToken() !== 4 && _scanner.getToken() !== 17) {
|
|
858
|
+
if (_scanner.getToken() === 5) {
|
|
859
|
+
if (!needsComma) handleError(4, [], []);
|
|
860
|
+
onSeparator(",");
|
|
861
|
+
scanNext();
|
|
862
|
+
if (_scanner.getToken() === 4 && allowTrailingComma) break;
|
|
863
|
+
} else if (needsComma) handleError(6, [], []);
|
|
864
|
+
if (isFirstElement) {
|
|
865
|
+
_jsonPath.push(0);
|
|
866
|
+
isFirstElement = false;
|
|
867
|
+
} else _jsonPath[_jsonPath.length - 1]++;
|
|
868
|
+
if (!parseValue()) handleError(4, [], [4, 5]);
|
|
869
|
+
needsComma = true;
|
|
870
|
+
}
|
|
871
|
+
onArrayEnd();
|
|
872
|
+
if (!isFirstElement) _jsonPath.pop();
|
|
873
|
+
if (_scanner.getToken() !== 4) handleError(8, [4], []);
|
|
874
|
+
else scanNext();
|
|
875
|
+
return true;
|
|
876
|
+
}
|
|
877
|
+
function parseValue() {
|
|
878
|
+
switch (_scanner.getToken()) {
|
|
879
|
+
case 3: return parseArray();
|
|
880
|
+
case 1: return parseObject();
|
|
881
|
+
case 10: return parseString(true);
|
|
882
|
+
default: return parseLiteral();
|
|
883
|
+
}
|
|
884
|
+
}
|
|
885
|
+
scanNext();
|
|
886
|
+
if (_scanner.getToken() === 17) {
|
|
887
|
+
if (options.allowEmptyContent) return true;
|
|
888
|
+
handleError(4, [], []);
|
|
889
|
+
return false;
|
|
890
|
+
}
|
|
891
|
+
if (!parseValue()) {
|
|
892
|
+
handleError(4, [], []);
|
|
893
|
+
return false;
|
|
894
|
+
}
|
|
895
|
+
if (_scanner.getToken() !== 17) handleError(9, [], []);
|
|
896
|
+
return true;
|
|
897
|
+
}
|
|
898
|
+
function getNodeType(value) {
|
|
899
|
+
switch (typeof value) {
|
|
900
|
+
case "boolean": return "boolean";
|
|
901
|
+
case "number": return "number";
|
|
902
|
+
case "string": return "string";
|
|
903
|
+
case "object":
|
|
904
|
+
if (!value) return "null";
|
|
905
|
+
else if (Array.isArray(value)) return "array";
|
|
906
|
+
return "object";
|
|
907
|
+
default: return "null";
|
|
908
|
+
}
|
|
909
|
+
}
|
|
910
|
+
// ../../node_modules/.pnpm/jsonc-parser@3.3.1/node_modules/jsonc-parser/lib/esm/impl/edit.js
|
|
911
|
+
function setProperty(text, originalPath, value, options) {
|
|
912
|
+
const path = originalPath.slice();
|
|
913
|
+
const root = parseTree$1(text, []);
|
|
914
|
+
let parent = void 0;
|
|
915
|
+
let lastSegment = void 0;
|
|
916
|
+
while (path.length > 0) {
|
|
917
|
+
lastSegment = path.pop();
|
|
918
|
+
parent = findNodeAtLocation$1(root, path);
|
|
919
|
+
if (parent === void 0 && value !== void 0) {
|
|
920
|
+
if (typeof lastSegment === "string") value = { [lastSegment]: value };
|
|
921
|
+
else value = [value];
|
|
922
|
+
} else break;
|
|
923
|
+
}
|
|
924
|
+
if (!parent) {
|
|
925
|
+
if (value === void 0) throw new Error("Can not delete in empty document");
|
|
926
|
+
return withFormatting(text, {
|
|
927
|
+
offset: root ? root.offset : 0,
|
|
928
|
+
length: root ? root.length : 0,
|
|
929
|
+
content: JSON.stringify(value)
|
|
930
|
+
}, options);
|
|
931
|
+
} else if (parent.type === "object" && typeof lastSegment === "string" && Array.isArray(parent.children)) {
|
|
932
|
+
const existing = findNodeAtLocation$1(parent, [lastSegment]);
|
|
933
|
+
if (existing !== void 0) {
|
|
934
|
+
if (value === void 0) {
|
|
935
|
+
if (!existing.parent) throw new Error("Malformed AST");
|
|
936
|
+
const propertyIndex = parent.children.indexOf(existing.parent);
|
|
937
|
+
let removeBegin;
|
|
938
|
+
let removeEnd = existing.parent.offset + existing.parent.length;
|
|
939
|
+
if (propertyIndex > 0) {
|
|
940
|
+
let previous = parent.children[propertyIndex - 1];
|
|
941
|
+
removeBegin = previous.offset + previous.length;
|
|
942
|
+
} else {
|
|
943
|
+
removeBegin = parent.offset + 1;
|
|
944
|
+
if (parent.children.length > 1) removeEnd = parent.children[1].offset;
|
|
945
|
+
}
|
|
946
|
+
return withFormatting(text, {
|
|
947
|
+
offset: removeBegin,
|
|
948
|
+
length: removeEnd - removeBegin,
|
|
949
|
+
content: ""
|
|
950
|
+
}, options);
|
|
951
|
+
} else return withFormatting(text, {
|
|
952
|
+
offset: existing.offset,
|
|
953
|
+
length: existing.length,
|
|
954
|
+
content: JSON.stringify(value)
|
|
955
|
+
}, options);
|
|
956
|
+
} else {
|
|
957
|
+
if (value === void 0) return [];
|
|
958
|
+
const newProperty = `${JSON.stringify(lastSegment)}: ${JSON.stringify(value)}`;
|
|
959
|
+
const index = options.getInsertionIndex ? options.getInsertionIndex(parent.children.map((p) => p.children[0].value)) : parent.children.length;
|
|
960
|
+
let edit;
|
|
961
|
+
if (index > 0) {
|
|
962
|
+
let previous = parent.children[index - 1];
|
|
963
|
+
edit = {
|
|
964
|
+
offset: previous.offset + previous.length,
|
|
965
|
+
length: 0,
|
|
966
|
+
content: "," + newProperty
|
|
967
|
+
};
|
|
968
|
+
} else if (parent.children.length === 0) edit = {
|
|
969
|
+
offset: parent.offset + 1,
|
|
970
|
+
length: 0,
|
|
971
|
+
content: newProperty
|
|
972
|
+
};
|
|
973
|
+
else edit = {
|
|
974
|
+
offset: parent.offset + 1,
|
|
975
|
+
length: 0,
|
|
976
|
+
content: newProperty + ","
|
|
977
|
+
};
|
|
978
|
+
return withFormatting(text, edit, options);
|
|
979
|
+
}
|
|
980
|
+
} else if (parent.type === "array" && typeof lastSegment === "number" && Array.isArray(parent.children)) {
|
|
981
|
+
const insertIndex = lastSegment;
|
|
982
|
+
if (insertIndex === -1) {
|
|
983
|
+
const newProperty = `${JSON.stringify(value)}`;
|
|
984
|
+
let edit;
|
|
985
|
+
if (parent.children.length === 0) edit = {
|
|
986
|
+
offset: parent.offset + 1,
|
|
987
|
+
length: 0,
|
|
988
|
+
content: newProperty
|
|
989
|
+
};
|
|
990
|
+
else {
|
|
991
|
+
const previous = parent.children[parent.children.length - 1];
|
|
992
|
+
edit = {
|
|
993
|
+
offset: previous.offset + previous.length,
|
|
994
|
+
length: 0,
|
|
995
|
+
content: "," + newProperty
|
|
996
|
+
};
|
|
997
|
+
}
|
|
998
|
+
return withFormatting(text, edit, options);
|
|
999
|
+
} else if (value === void 0 && parent.children.length >= 0) {
|
|
1000
|
+
const removalIndex = lastSegment;
|
|
1001
|
+
const toRemove = parent.children[removalIndex];
|
|
1002
|
+
let edit;
|
|
1003
|
+
if (parent.children.length === 1) edit = {
|
|
1004
|
+
offset: parent.offset + 1,
|
|
1005
|
+
length: parent.length - 2,
|
|
1006
|
+
content: ""
|
|
1007
|
+
};
|
|
1008
|
+
else if (parent.children.length - 1 === removalIndex) {
|
|
1009
|
+
let previous = parent.children[removalIndex - 1];
|
|
1010
|
+
let offset = previous.offset + previous.length;
|
|
1011
|
+
edit = {
|
|
1012
|
+
offset,
|
|
1013
|
+
length: parent.offset + parent.length - 2 - offset,
|
|
1014
|
+
content: ""
|
|
1015
|
+
};
|
|
1016
|
+
} else edit = {
|
|
1017
|
+
offset: toRemove.offset,
|
|
1018
|
+
length: parent.children[removalIndex + 1].offset - toRemove.offset,
|
|
1019
|
+
content: ""
|
|
1020
|
+
};
|
|
1021
|
+
return withFormatting(text, edit, options);
|
|
1022
|
+
} else if (value !== void 0) {
|
|
1023
|
+
let edit;
|
|
1024
|
+
const newProperty = `${JSON.stringify(value)}`;
|
|
1025
|
+
if (!options.isArrayInsertion && parent.children.length > lastSegment) {
|
|
1026
|
+
const toModify = parent.children[lastSegment];
|
|
1027
|
+
edit = {
|
|
1028
|
+
offset: toModify.offset,
|
|
1029
|
+
length: toModify.length,
|
|
1030
|
+
content: newProperty
|
|
1031
|
+
};
|
|
1032
|
+
} else if (parent.children.length === 0 || lastSegment === 0) edit = {
|
|
1033
|
+
offset: parent.offset + 1,
|
|
1034
|
+
length: 0,
|
|
1035
|
+
content: parent.children.length === 0 ? newProperty : newProperty + ","
|
|
1036
|
+
};
|
|
1037
|
+
else {
|
|
1038
|
+
const index = lastSegment > parent.children.length ? parent.children.length : lastSegment;
|
|
1039
|
+
const previous = parent.children[index - 1];
|
|
1040
|
+
edit = {
|
|
1041
|
+
offset: previous.offset + previous.length,
|
|
1042
|
+
length: 0,
|
|
1043
|
+
content: "," + newProperty
|
|
1044
|
+
};
|
|
1045
|
+
}
|
|
1046
|
+
return withFormatting(text, edit, options);
|
|
1047
|
+
} else throw new Error(`Can not ${value === void 0 ? "remove" : options.isArrayInsertion ? "insert" : "modify"} Array index ${insertIndex} as length is not sufficient`);
|
|
1048
|
+
} else throw new Error(`Can not add ${typeof lastSegment !== "number" ? "index" : "property"} to parent of type ${parent.type}`);
|
|
1049
|
+
}
|
|
1050
|
+
function withFormatting(text, edit, options) {
|
|
1051
|
+
if (!options.formattingOptions) return [edit];
|
|
1052
|
+
let newText = applyEdit(text, edit);
|
|
1053
|
+
let begin = edit.offset;
|
|
1054
|
+
let end = edit.offset + edit.content.length;
|
|
1055
|
+
if (edit.length === 0 || edit.content.length === 0) {
|
|
1056
|
+
while (begin > 0 && !isEOL(newText, begin - 1)) begin--;
|
|
1057
|
+
while (end < newText.length && !isEOL(newText, end)) end++;
|
|
1058
|
+
}
|
|
1059
|
+
const edits = format(newText, {
|
|
1060
|
+
offset: begin,
|
|
1061
|
+
length: end - begin
|
|
1062
|
+
}, {
|
|
1063
|
+
...options.formattingOptions,
|
|
1064
|
+
keepLines: false
|
|
1065
|
+
});
|
|
1066
|
+
for (let i = edits.length - 1; i >= 0; i--) {
|
|
1067
|
+
const edit = edits[i];
|
|
1068
|
+
newText = applyEdit(newText, edit);
|
|
1069
|
+
begin = Math.min(begin, edit.offset);
|
|
1070
|
+
end = Math.max(end, edit.offset + edit.length);
|
|
1071
|
+
end += edit.content.length - edit.length;
|
|
1072
|
+
}
|
|
1073
|
+
const editLength = text.length - (newText.length - end) - begin;
|
|
1074
|
+
return [{
|
|
1075
|
+
offset: begin,
|
|
1076
|
+
length: editLength,
|
|
1077
|
+
content: newText.substring(begin, end)
|
|
1078
|
+
}];
|
|
1079
|
+
}
|
|
1080
|
+
function applyEdit(text, edit) {
|
|
1081
|
+
return text.substring(0, edit.offset) + edit.content + text.substring(edit.offset + edit.length);
|
|
1082
|
+
}
|
|
1083
|
+
// ../../node_modules/.pnpm/jsonc-parser@3.3.1/node_modules/jsonc-parser/lib/esm/main.js
|
|
1084
|
+
var ScanError;
|
|
1085
|
+
(function(ScanError) {
|
|
1086
|
+
ScanError[ScanError["None"] = 0] = "None";
|
|
1087
|
+
ScanError[ScanError["UnexpectedEndOfComment"] = 1] = "UnexpectedEndOfComment";
|
|
1088
|
+
ScanError[ScanError["UnexpectedEndOfString"] = 2] = "UnexpectedEndOfString";
|
|
1089
|
+
ScanError[ScanError["UnexpectedEndOfNumber"] = 3] = "UnexpectedEndOfNumber";
|
|
1090
|
+
ScanError[ScanError["InvalidUnicode"] = 4] = "InvalidUnicode";
|
|
1091
|
+
ScanError[ScanError["InvalidEscapeCharacter"] = 5] = "InvalidEscapeCharacter";
|
|
1092
|
+
ScanError[ScanError["InvalidCharacter"] = 6] = "InvalidCharacter";
|
|
1093
|
+
})(ScanError || (ScanError = {}));
|
|
1094
|
+
var SyntaxKind;
|
|
1095
|
+
(function(SyntaxKind) {
|
|
1096
|
+
SyntaxKind[SyntaxKind["OpenBraceToken"] = 1] = "OpenBraceToken";
|
|
1097
|
+
SyntaxKind[SyntaxKind["CloseBraceToken"] = 2] = "CloseBraceToken";
|
|
1098
|
+
SyntaxKind[SyntaxKind["OpenBracketToken"] = 3] = "OpenBracketToken";
|
|
1099
|
+
SyntaxKind[SyntaxKind["CloseBracketToken"] = 4] = "CloseBracketToken";
|
|
1100
|
+
SyntaxKind[SyntaxKind["CommaToken"] = 5] = "CommaToken";
|
|
1101
|
+
SyntaxKind[SyntaxKind["ColonToken"] = 6] = "ColonToken";
|
|
1102
|
+
SyntaxKind[SyntaxKind["NullKeyword"] = 7] = "NullKeyword";
|
|
1103
|
+
SyntaxKind[SyntaxKind["TrueKeyword"] = 8] = "TrueKeyword";
|
|
1104
|
+
SyntaxKind[SyntaxKind["FalseKeyword"] = 9] = "FalseKeyword";
|
|
1105
|
+
SyntaxKind[SyntaxKind["StringLiteral"] = 10] = "StringLiteral";
|
|
1106
|
+
SyntaxKind[SyntaxKind["NumericLiteral"] = 11] = "NumericLiteral";
|
|
1107
|
+
SyntaxKind[SyntaxKind["LineCommentTrivia"] = 12] = "LineCommentTrivia";
|
|
1108
|
+
SyntaxKind[SyntaxKind["BlockCommentTrivia"] = 13] = "BlockCommentTrivia";
|
|
1109
|
+
SyntaxKind[SyntaxKind["LineBreakTrivia"] = 14] = "LineBreakTrivia";
|
|
1110
|
+
SyntaxKind[SyntaxKind["Trivia"] = 15] = "Trivia";
|
|
1111
|
+
SyntaxKind[SyntaxKind["Unknown"] = 16] = "Unknown";
|
|
1112
|
+
SyntaxKind[SyntaxKind["EOF"] = 17] = "EOF";
|
|
1113
|
+
})(SyntaxKind || (SyntaxKind = {}));
|
|
1114
|
+
/**
|
|
1115
|
+
* Parses the given text and returns a tree representation the JSON content. On invalid input, the parser tries to be as fault tolerant as possible, but still return a result.
|
|
1116
|
+
*/
|
|
1117
|
+
const parseTree = parseTree$1;
|
|
1118
|
+
/**
|
|
1119
|
+
* Finds the node at the given path in a JSON DOM.
|
|
1120
|
+
*/
|
|
1121
|
+
const findNodeAtLocation = findNodeAtLocation$1;
|
|
1122
|
+
var ParseErrorCode;
|
|
1123
|
+
(function(ParseErrorCode) {
|
|
1124
|
+
ParseErrorCode[ParseErrorCode["InvalidSymbol"] = 1] = "InvalidSymbol";
|
|
1125
|
+
ParseErrorCode[ParseErrorCode["InvalidNumberFormat"] = 2] = "InvalidNumberFormat";
|
|
1126
|
+
ParseErrorCode[ParseErrorCode["PropertyNameExpected"] = 3] = "PropertyNameExpected";
|
|
1127
|
+
ParseErrorCode[ParseErrorCode["ValueExpected"] = 4] = "ValueExpected";
|
|
1128
|
+
ParseErrorCode[ParseErrorCode["ColonExpected"] = 5] = "ColonExpected";
|
|
1129
|
+
ParseErrorCode[ParseErrorCode["CommaExpected"] = 6] = "CommaExpected";
|
|
1130
|
+
ParseErrorCode[ParseErrorCode["CloseBraceExpected"] = 7] = "CloseBraceExpected";
|
|
1131
|
+
ParseErrorCode[ParseErrorCode["CloseBracketExpected"] = 8] = "CloseBracketExpected";
|
|
1132
|
+
ParseErrorCode[ParseErrorCode["EndOfFileExpected"] = 9] = "EndOfFileExpected";
|
|
1133
|
+
ParseErrorCode[ParseErrorCode["InvalidCommentToken"] = 10] = "InvalidCommentToken";
|
|
1134
|
+
ParseErrorCode[ParseErrorCode["UnexpectedEndOfComment"] = 11] = "UnexpectedEndOfComment";
|
|
1135
|
+
ParseErrorCode[ParseErrorCode["UnexpectedEndOfString"] = 12] = "UnexpectedEndOfString";
|
|
1136
|
+
ParseErrorCode[ParseErrorCode["UnexpectedEndOfNumber"] = 13] = "UnexpectedEndOfNumber";
|
|
1137
|
+
ParseErrorCode[ParseErrorCode["InvalidUnicode"] = 14] = "InvalidUnicode";
|
|
1138
|
+
ParseErrorCode[ParseErrorCode["InvalidEscapeCharacter"] = 15] = "InvalidEscapeCharacter";
|
|
1139
|
+
ParseErrorCode[ParseErrorCode["InvalidCharacter"] = 16] = "InvalidCharacter";
|
|
1140
|
+
})(ParseErrorCode || (ParseErrorCode = {}));
|
|
1141
|
+
function printParseErrorCode(code) {
|
|
1142
|
+
switch (code) {
|
|
1143
|
+
case 1: return "InvalidSymbol";
|
|
1144
|
+
case 2: return "InvalidNumberFormat";
|
|
1145
|
+
case 3: return "PropertyNameExpected";
|
|
1146
|
+
case 4: return "ValueExpected";
|
|
1147
|
+
case 5: return "ColonExpected";
|
|
1148
|
+
case 6: return "CommaExpected";
|
|
1149
|
+
case 7: return "CloseBraceExpected";
|
|
1150
|
+
case 8: return "CloseBracketExpected";
|
|
1151
|
+
case 9: return "EndOfFileExpected";
|
|
1152
|
+
case 10: return "InvalidCommentToken";
|
|
1153
|
+
case 11: return "UnexpectedEndOfComment";
|
|
1154
|
+
case 12: return "UnexpectedEndOfString";
|
|
1155
|
+
case 13: return "UnexpectedEndOfNumber";
|
|
1156
|
+
case 14: return "InvalidUnicode";
|
|
1157
|
+
case 15: return "InvalidEscapeCharacter";
|
|
1158
|
+
case 16: return "InvalidCharacter";
|
|
1159
|
+
}
|
|
1160
|
+
return "<unknown ParseErrorCode>";
|
|
1161
|
+
}
|
|
1162
|
+
/**
|
|
1163
|
+
* Computes the edit operations needed to modify a value in the JSON document.
|
|
1164
|
+
*
|
|
1165
|
+
* @param documentText The input text
|
|
1166
|
+
* @param path The path of the value to change. The path represents either to the document root, a property or an array item.
|
|
1167
|
+
* If the path points to an non-existing property or item, it will be created.
|
|
1168
|
+
* @param value The new value for the specified property or item. If the value is undefined,
|
|
1169
|
+
* the property or item will be removed.
|
|
1170
|
+
* @param options Options
|
|
1171
|
+
* @returns The edit operations describing the changes to the original document, following the format described in {@linkcode EditResult}.
|
|
1172
|
+
* To apply the edit operations to the input, use {@linkcode applyEdits}.
|
|
1173
|
+
*/
|
|
1174
|
+
function modify(text, path, value, options) {
|
|
1175
|
+
return setProperty(text, path, value, options);
|
|
1176
|
+
}
|
|
1177
|
+
/**
|
|
1178
|
+
* Applies edits to an input string.
|
|
1179
|
+
* @param text The input text
|
|
1180
|
+
* @param edits Edit operations following the format described in {@linkcode EditResult}.
|
|
1181
|
+
* @returns The text with the applied edits.
|
|
1182
|
+
* @throws An error if the edit operations are not well-formed as described in {@linkcode EditResult}.
|
|
1183
|
+
*/
|
|
1184
|
+
function applyEdits(text, edits) {
|
|
1185
|
+
let sortedEdits = edits.slice(0).sort((a, b) => {
|
|
1186
|
+
const diff = a.offset - b.offset;
|
|
1187
|
+
if (diff === 0) return a.length - b.length;
|
|
1188
|
+
return diff;
|
|
1189
|
+
});
|
|
1190
|
+
let lastModifiedOffset = text.length;
|
|
1191
|
+
for (let i = sortedEdits.length - 1; i >= 0; i--) {
|
|
1192
|
+
let e = sortedEdits[i];
|
|
1193
|
+
if (e.offset + e.length <= lastModifiedOffset) text = applyEdit(text, e);
|
|
1194
|
+
else throw new Error("Overlapping edit");
|
|
1195
|
+
lastModifiedOffset = e.offset;
|
|
1196
|
+
}
|
|
1197
|
+
return text;
|
|
1198
|
+
}
|
|
1199
|
+
// ../../shared/contract/locale-file.ts
|
|
1200
|
+
/**
|
|
1201
|
+
* One JSON file per locale, as git-sync's `locale-files` layout and the
|
|
1202
|
+
* `localness` CLI read and write it. Both go through this module, so a file
|
|
1203
|
+
* written by either one is byte-for-byte what the other would write.
|
|
1204
|
+
*
|
|
1205
|
+
* Package subpath `@localness/contract/locale-file`.
|
|
1206
|
+
*/
|
|
1207
|
+
const LOCALE_PLACEHOLDER = "{locale}";
|
|
1208
|
+
const DEFAULT_LOCALE_FILE_PATTERN = `${LOCALE_PLACEHOLDER}.json`;
|
|
1209
|
+
/** Nested JSON joins its path with this. */
|
|
1210
|
+
const LOCALE_KEY_SEPARATOR = ".";
|
|
1211
|
+
/** Key layout a write uses for a new key. Reading accepts both. */
|
|
1212
|
+
const LocaleFileFormat = {
|
|
1213
|
+
FLAT_JSON: "flat-json",
|
|
1214
|
+
NESTED_JSON: "nested-json"
|
|
1215
|
+
};
|
|
1216
|
+
/**
|
|
1217
|
+
* The text to store, or null for "no translation". Only line endings and the
|
|
1218
|
+
* ends are touched: inner whitespace is copy.
|
|
1219
|
+
*/
|
|
1220
|
+
function normalizeLocaleText(value) {
|
|
1221
|
+
let text;
|
|
1222
|
+
if (typeof value === "string") text = value;
|
|
1223
|
+
else if (typeof value === "number" && Number.isFinite(value)) text = String(value);
|
|
1224
|
+
else if (typeof value === "boolean") text = String(value);
|
|
1225
|
+
else return null;
|
|
1226
|
+
const normalized = text.replace(/\r\n?/g, "\n").trim();
|
|
1227
|
+
return normalized === "" ? null : normalized;
|
|
1228
|
+
}
|
|
1229
|
+
/** No prototype, so a key named `__proto__` is stored like any other. */
|
|
1230
|
+
function emptyEntries() {
|
|
1231
|
+
return Object.create(null);
|
|
1232
|
+
}
|
|
1233
|
+
const SEGMENT = /^[A-Za-z0-9][A-Za-z0-9._-]*$/;
|
|
1234
|
+
function validPath(path) {
|
|
1235
|
+
return path.split("/").every((segment) => SEGMENT.test(segment));
|
|
1236
|
+
}
|
|
1237
|
+
/** Error text for an unusable directory / pattern, or null. */
|
|
1238
|
+
function localeFilesScopeError(dir, pattern) {
|
|
1239
|
+
if (!validPath(dir)) return "Directory must be a relative path like src/locales";
|
|
1240
|
+
if (pattern.split("{locale}").length !== 2) return `File pattern must contain ${LOCALE_PLACEHOLDER} exactly once`;
|
|
1241
|
+
if (!pattern.toLowerCase().endsWith(".json")) return "File pattern must end with .json";
|
|
1242
|
+
if (!validPath(pattern.replace("{locale}", "en"))) return "File pattern must be a relative path like {locale}.json";
|
|
1243
|
+
return null;
|
|
1244
|
+
}
|
|
1245
|
+
/**
|
|
1246
|
+
* `zh_cn` → `zh-CN`, `en` → `en`: the BCP 47 shape app repos name files by.
|
|
1247
|
+
* An explicit `localeMap` entry wins.
|
|
1248
|
+
*/
|
|
1249
|
+
function localeFileName(locale, localeMap) {
|
|
1250
|
+
const mapped = localeMap?.[locale];
|
|
1251
|
+
if (mapped) return mapped;
|
|
1252
|
+
const [language, ...rest] = locale.split("_");
|
|
1253
|
+
return [language, ...rest.map((part) => part.toUpperCase())].join("-");
|
|
1254
|
+
}
|
|
1255
|
+
/** `/`-separated path of a locale's file. `dir` must pass `localeFilesScopeError`. */
|
|
1256
|
+
function localeFilePath(dir, pattern, locale, localeMap) {
|
|
1257
|
+
return `${dir}/${pattern.replace(LOCALE_PLACEHOLDER, localeFileName(locale, localeMap))}`;
|
|
1258
|
+
}
|
|
1259
|
+
function readLocaleFile(source) {
|
|
1260
|
+
const errors = [];
|
|
1261
|
+
const root = parseTree(source, errors, { allowTrailingComma: true });
|
|
1262
|
+
const first = errors[0];
|
|
1263
|
+
if (first) throw new Error(`Invalid JSON (${printParseErrorCode(first.error)})`);
|
|
1264
|
+
if (root?.type !== "object") throw new Error("The JSON file must contain an object at the top level");
|
|
1265
|
+
const entries = emptyEntries();
|
|
1266
|
+
const paths = /* @__PURE__ */ new Map();
|
|
1267
|
+
const blocked = [];
|
|
1268
|
+
const walk = (node, path) => {
|
|
1269
|
+
for (const property of node.children ?? []) {
|
|
1270
|
+
const [nameNode, valueNode] = property.children ?? [];
|
|
1271
|
+
if (!nameNode || !valueNode) continue;
|
|
1272
|
+
const segments = [...path, String(nameNode.value)];
|
|
1273
|
+
switch (valueNode.type) {
|
|
1274
|
+
case "object":
|
|
1275
|
+
walk(valueNode, segments);
|
|
1276
|
+
break;
|
|
1277
|
+
case "string":
|
|
1278
|
+
case "number":
|
|
1279
|
+
case "boolean": {
|
|
1280
|
+
const key = segments.join(".");
|
|
1281
|
+
paths.set(key, segments);
|
|
1282
|
+
const text = normalizeLocaleText(valueNode.value);
|
|
1283
|
+
if (text !== null) entries[key] = text;
|
|
1284
|
+
break;
|
|
1285
|
+
}
|
|
1286
|
+
case "array":
|
|
1287
|
+
case "null":
|
|
1288
|
+
case "property":
|
|
1289
|
+
blocked.push(segments);
|
|
1290
|
+
break;
|
|
1291
|
+
default: {
|
|
1292
|
+
const unreachable = valueNode.type;
|
|
1293
|
+
throw new Error(`Unknown JSON node type: ${unreachable}`);
|
|
1294
|
+
}
|
|
1295
|
+
}
|
|
1296
|
+
}
|
|
1297
|
+
};
|
|
1298
|
+
walk(root, []);
|
|
1299
|
+
return {
|
|
1300
|
+
entries,
|
|
1301
|
+
paths,
|
|
1302
|
+
blocked
|
|
1303
|
+
};
|
|
1304
|
+
}
|
|
1305
|
+
function isPrefix(a, b) {
|
|
1306
|
+
return a.length <= b.length && a.every((segment, i) => segment === b[i]);
|
|
1307
|
+
}
|
|
1308
|
+
function newKeyPath(key, format) {
|
|
1309
|
+
switch (format) {
|
|
1310
|
+
case LocaleFileFormat.FLAT_JSON: return [key];
|
|
1311
|
+
case LocaleFileFormat.NESTED_JSON: return key.split(".");
|
|
1312
|
+
default: return format;
|
|
1313
|
+
}
|
|
1314
|
+
}
|
|
1315
|
+
/**
|
|
1316
|
+
* Keys a write cannot place. A key already in the file is always writable, in
|
|
1317
|
+
* place. A new nested key is not when its path runs into an existing value
|
|
1318
|
+
* (`a` is text, so `a.b` has nowhere to go), would replace an object, has an
|
|
1319
|
+
* empty segment, or collides that way with another new key — then neither of
|
|
1320
|
+
* the two is written, since there is no right one to pick.
|
|
1321
|
+
*/
|
|
1322
|
+
function unwritableKeys(file, keys, format) {
|
|
1323
|
+
const out = /* @__PURE__ */ new Set();
|
|
1324
|
+
const occupied = [...file.paths.values(), ...file.blocked];
|
|
1325
|
+
const fresh = keys.filter((key) => !file.paths.has(key)).map((key) => ({
|
|
1326
|
+
key,
|
|
1327
|
+
path: newKeyPath(key, format)
|
|
1328
|
+
}));
|
|
1329
|
+
for (const item of fresh) {
|
|
1330
|
+
if (item.path.some((segment) => segment === "")) {
|
|
1331
|
+
out.add(item.key);
|
|
1332
|
+
continue;
|
|
1333
|
+
}
|
|
1334
|
+
const hitsFile = occupied.some((path) => isPrefix(path, item.path) || isPrefix(item.path, path));
|
|
1335
|
+
const hitsFresh = fresh.some((other) => other.key !== item.key && (isPrefix(other.path, item.path) || isPrefix(item.path, other.path)));
|
|
1336
|
+
if (hitsFile || hitsFresh) out.add(item.key);
|
|
1337
|
+
}
|
|
1338
|
+
return out;
|
|
1339
|
+
}
|
|
1340
|
+
function detectIndent(source) {
|
|
1341
|
+
const indent = source.match(/^[ \t]+(?=\S)/m)?.[0];
|
|
1342
|
+
if (!indent) return {
|
|
1343
|
+
insertSpaces: true,
|
|
1344
|
+
tabSize: 2
|
|
1345
|
+
};
|
|
1346
|
+
if (indent.startsWith(" ")) return {
|
|
1347
|
+
insertSpaces: false,
|
|
1348
|
+
tabSize: 1
|
|
1349
|
+
};
|
|
1350
|
+
return {
|
|
1351
|
+
insertSpaces: true,
|
|
1352
|
+
tabSize: indent.length
|
|
1353
|
+
};
|
|
1354
|
+
}
|
|
1355
|
+
/**
|
|
1356
|
+
* Sets each entry in place and leaves everything else byte-for-byte: key
|
|
1357
|
+
* order, indentation, comments and line endings. A new key goes to the end of
|
|
1358
|
+
* its parent object. A `null` value removes the key. `source` null creates the
|
|
1359
|
+
* file. Callers must drop `unwritableKeys` first.
|
|
1360
|
+
*/
|
|
1361
|
+
function writeLocaleFile(source, entries, format) {
|
|
1362
|
+
let text = source ?? "{}\n";
|
|
1363
|
+
const file = readLocaleFile(text);
|
|
1364
|
+
const eol = text.includes("\r\n") ? "\r\n" : "\n";
|
|
1365
|
+
const formattingOptions = {
|
|
1366
|
+
...detectIndent(text),
|
|
1367
|
+
eol
|
|
1368
|
+
};
|
|
1369
|
+
const set = (path, value) => {
|
|
1370
|
+
text = applyEdits(text, modify(text, path, value, { formattingOptions }));
|
|
1371
|
+
};
|
|
1372
|
+
for (const [key, value] of Object.entries(entries)) {
|
|
1373
|
+
const known = file.paths.get(key);
|
|
1374
|
+
if (value !== null) {
|
|
1375
|
+
set(known ?? newKeyPath(key, format), value);
|
|
1376
|
+
continue;
|
|
1377
|
+
}
|
|
1378
|
+
if (!known) continue;
|
|
1379
|
+
set(known, void 0);
|
|
1380
|
+
for (let depth = known.length - 1; depth > 0; depth--) {
|
|
1381
|
+
const parent = known.slice(0, depth);
|
|
1382
|
+
const node = findNodeAtLocation(parseTree(text), parent);
|
|
1383
|
+
if (node?.type !== "object" || node.children?.length) break;
|
|
1384
|
+
set(parent, void 0);
|
|
1385
|
+
}
|
|
1386
|
+
}
|
|
1387
|
+
return text.endsWith("\n") ? text : `${text}${eol}`;
|
|
1388
|
+
}
|
|
1389
|
+
export { DEFAULT_LOCALE_FILE_PATTERN, LOCALE_KEY_SEPARATOR, LOCALE_PLACEHOLDER, LocaleFileFormat, emptyEntries, localeFileName, localeFilePath, localeFilesScopeError, normalizeLocaleText, readLocaleFile, unwritableKeys, writeLocaleFile };
|