@angular-devkit/core 14.0.0-next.9 → 14.0.0-rc.0

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.
@@ -1,743 +0,0 @@
1
- "use strict";
2
- /**
3
- * @license
4
- * Copyright Google LLC All Rights Reserved.
5
- *
6
- * Use of this source code is governed by an MIT-style license that can be
7
- * found in the LICENSE file at https://angular.io/license
8
- */
9
- Object.defineProperty(exports, "__esModule", { value: true });
10
- exports.parseJsonAst = exports.JsonParseMode = exports.UnexpectedEndOfInputException = exports.InvalidJsonCharacterException = exports.JsonException = void 0;
11
- /* eslint-disable no-constant-condition */
12
- const exception_1 = require("../exception");
13
- class JsonException extends exception_1.BaseException {
14
- }
15
- exports.JsonException = JsonException;
16
- /**
17
- * A character was invalid in this context.
18
- * @deprecated
19
- * @private
20
- */
21
- class InvalidJsonCharacterException extends JsonException {
22
- constructor(context) {
23
- const pos = context.previous;
24
- const invalidChar = JSON.stringify(_peek(context));
25
- super(`Invalid JSON character: ${invalidChar} at ${pos.line}:${pos.character}.`);
26
- this.invalidChar = invalidChar;
27
- this.line = pos.line;
28
- this.offset = pos.offset;
29
- this.character = pos.character;
30
- }
31
- }
32
- exports.InvalidJsonCharacterException = InvalidJsonCharacterException;
33
- /**
34
- * More input was expected, but we reached the end of the stream.
35
- * @deprecated
36
- * @private
37
- */
38
- class UnexpectedEndOfInputException extends JsonException {
39
- constructor(_context) {
40
- super(`Unexpected end of file.`);
41
- }
42
- }
43
- exports.UnexpectedEndOfInputException = UnexpectedEndOfInputException;
44
- /**
45
- * Peek and return the next character from the context.
46
- * @private
47
- */
48
- function _peek(context) {
49
- return context.original[context.position.offset];
50
- }
51
- /**
52
- * Move the context to the next character, including incrementing the line if necessary.
53
- * @private
54
- */
55
- function _next(context) {
56
- context.previous = context.position;
57
- let { offset, line, character } = context.position;
58
- const char = context.original[offset];
59
- offset++;
60
- if (char == '\n') {
61
- line++;
62
- character = 0;
63
- }
64
- else {
65
- character++;
66
- }
67
- context.position = { offset, line, character };
68
- }
69
- function _token(context, valid) {
70
- const char = _peek(context);
71
- if (valid) {
72
- if (!char) {
73
- throw new UnexpectedEndOfInputException(context);
74
- }
75
- if (valid.indexOf(char) == -1) {
76
- throw new InvalidJsonCharacterException(context);
77
- }
78
- }
79
- // Move the position of the context to the next character.
80
- _next(context);
81
- return char;
82
- }
83
- /**
84
- * Read the exponent part of a number. The exponent part is looser for JSON than the number
85
- * part. `str` is the string of the number itself found so far, and start the position
86
- * where the full number started. Returns the node found.
87
- * @private
88
- */
89
- function _readExpNumber(context, start, str, comments) {
90
- let char;
91
- let signed = false;
92
- while (true) {
93
- char = _token(context);
94
- if (char == '+' || char == '-') {
95
- if (signed) {
96
- break;
97
- }
98
- signed = true;
99
- str += char;
100
- }
101
- else if (char == '0' ||
102
- char == '1' ||
103
- char == '2' ||
104
- char == '3' ||
105
- char == '4' ||
106
- char == '5' ||
107
- char == '6' ||
108
- char == '7' ||
109
- char == '8' ||
110
- char == '9') {
111
- signed = true;
112
- str += char;
113
- }
114
- else {
115
- break;
116
- }
117
- }
118
- // We're done reading this number.
119
- context.position = context.previous;
120
- return {
121
- kind: 'number',
122
- start,
123
- end: context.position,
124
- text: context.original.substring(start.offset, context.position.offset),
125
- value: Number.parseFloat(str),
126
- comments: comments,
127
- };
128
- }
129
- /**
130
- * Read the hexa part of a 0xBADCAFE hexadecimal number.
131
- * @private
132
- */
133
- function _readHexaNumber(context, isNegative, start, comments) {
134
- // Read an hexadecimal number, until it's not hexadecimal.
135
- let hexa = '';
136
- const valid = '0123456789abcdefABCDEF';
137
- for (let ch = _peek(context); ch && valid.includes(ch); ch = _peek(context)) {
138
- // Add it to the hexa string.
139
- hexa += ch;
140
- // Move the position of the context to the next character.
141
- _next(context);
142
- }
143
- const value = Number.parseInt(hexa, 16);
144
- // We're done reading this number.
145
- return {
146
- kind: 'number',
147
- start,
148
- end: context.position,
149
- text: context.original.substring(start.offset, context.position.offset),
150
- value: isNegative ? -value : value,
151
- comments,
152
- };
153
- }
154
- /**
155
- * Read a number from the context.
156
- * @private
157
- */
158
- function _readNumber(context, comments = _readBlanks(context)) {
159
- let str = '';
160
- let dotted = false;
161
- const start = context.position;
162
- // read until `e` or end of line.
163
- while (true) {
164
- const char = _token(context);
165
- // Read tokens, one by one.
166
- if (char == '-') {
167
- if (str != '') {
168
- throw new InvalidJsonCharacterException(context);
169
- }
170
- }
171
- else if (char == 'I' &&
172
- (str == '-' || str == '' || str == '+') &&
173
- (context.mode & JsonParseMode.NumberConstantsAllowed) != 0) {
174
- // Infinity?
175
- // _token(context, 'I'); Already read.
176
- _token(context, 'n');
177
- _token(context, 'f');
178
- _token(context, 'i');
179
- _token(context, 'n');
180
- _token(context, 'i');
181
- _token(context, 't');
182
- _token(context, 'y');
183
- str += 'Infinity';
184
- break;
185
- }
186
- else if (char == '0') {
187
- if (str == '0' || str == '-0') {
188
- throw new InvalidJsonCharacterException(context);
189
- }
190
- }
191
- else if (char == '1' ||
192
- char == '2' ||
193
- char == '3' ||
194
- char == '4' ||
195
- char == '5' ||
196
- char == '6' ||
197
- char == '7' ||
198
- char == '8' ||
199
- char == '9') {
200
- if (str == '0' || str == '-0') {
201
- throw new InvalidJsonCharacterException(context);
202
- }
203
- }
204
- else if (char == '+' && str == '') {
205
- // Pass over.
206
- }
207
- else if (char == '.') {
208
- if (dotted) {
209
- throw new InvalidJsonCharacterException(context);
210
- }
211
- dotted = true;
212
- }
213
- else if (char == 'e' || char == 'E') {
214
- return _readExpNumber(context, start, str + char, comments);
215
- }
216
- else if (char == 'x' &&
217
- (str == '0' || str == '-0') &&
218
- (context.mode & JsonParseMode.HexadecimalNumberAllowed) != 0) {
219
- return _readHexaNumber(context, str == '-0', start, comments);
220
- }
221
- else {
222
- // We read one too many characters, so rollback the last character.
223
- context.position = context.previous;
224
- break;
225
- }
226
- str += char;
227
- }
228
- // We're done reading this number.
229
- if (str.endsWith('.') && (context.mode & JsonParseMode.HexadecimalNumberAllowed) == 0) {
230
- throw new InvalidJsonCharacterException(context);
231
- }
232
- return {
233
- kind: 'number',
234
- start,
235
- end: context.position,
236
- text: context.original.substring(start.offset, context.position.offset),
237
- value: Number.parseFloat(str),
238
- comments,
239
- };
240
- }
241
- /**
242
- * Read a string from the context. Takes the comments of the string or read the blanks before the
243
- * string.
244
- * @private
245
- */
246
- function _readString(context, comments = _readBlanks(context)) {
247
- const start = context.position;
248
- // Consume the first string delimiter.
249
- const delim = _token(context);
250
- if ((context.mode & JsonParseMode.SingleQuotesAllowed) == 0) {
251
- if (delim == "'") {
252
- throw new InvalidJsonCharacterException(context);
253
- }
254
- }
255
- let str = '';
256
- while (true) {
257
- let char = _token(context);
258
- if (char == delim) {
259
- return {
260
- kind: 'string',
261
- start,
262
- end: context.position,
263
- text: context.original.substring(start.offset, context.position.offset),
264
- value: str,
265
- comments: comments,
266
- };
267
- }
268
- else if (char == '\\') {
269
- char = _token(context);
270
- switch (char) {
271
- case '\\':
272
- case '/':
273
- case '"':
274
- case delim:
275
- str += char;
276
- break;
277
- case 'b':
278
- str += '\b';
279
- break;
280
- case 'f':
281
- str += '\f';
282
- break;
283
- case 'n':
284
- str += '\n';
285
- break;
286
- case 'r':
287
- str += '\r';
288
- break;
289
- case 't':
290
- str += '\t';
291
- break;
292
- case 'u':
293
- const [c0] = _token(context, '0123456789abcdefABCDEF');
294
- const [c1] = _token(context, '0123456789abcdefABCDEF');
295
- const [c2] = _token(context, '0123456789abcdefABCDEF');
296
- const [c3] = _token(context, '0123456789abcdefABCDEF');
297
- str += String.fromCharCode(parseInt(c0 + c1 + c2 + c3, 16));
298
- break;
299
- case undefined:
300
- throw new UnexpectedEndOfInputException(context);
301
- case '\n':
302
- // Only valid when multiline strings are allowed.
303
- if ((context.mode & JsonParseMode.MultiLineStringAllowed) == 0) {
304
- throw new InvalidJsonCharacterException(context);
305
- }
306
- str += char;
307
- break;
308
- default:
309
- throw new InvalidJsonCharacterException(context);
310
- }
311
- }
312
- else if (char === undefined) {
313
- throw new UnexpectedEndOfInputException(context);
314
- }
315
- else if (char == '\b' || char == '\f' || char == '\n' || char == '\r' || char == '\t') {
316
- throw new InvalidJsonCharacterException(context);
317
- }
318
- else {
319
- str += char;
320
- }
321
- }
322
- }
323
- /**
324
- * Read the constant `true` from the context.
325
- * @private
326
- */
327
- function _readTrue(context, comments = _readBlanks(context)) {
328
- const start = context.position;
329
- _token(context, 't');
330
- _token(context, 'r');
331
- _token(context, 'u');
332
- _token(context, 'e');
333
- const end = context.position;
334
- return {
335
- kind: 'true',
336
- start,
337
- end,
338
- text: context.original.substring(start.offset, end.offset),
339
- value: true,
340
- comments,
341
- };
342
- }
343
- /**
344
- * Read the constant `false` from the context.
345
- * @private
346
- */
347
- function _readFalse(context, comments = _readBlanks(context)) {
348
- const start = context.position;
349
- _token(context, 'f');
350
- _token(context, 'a');
351
- _token(context, 'l');
352
- _token(context, 's');
353
- _token(context, 'e');
354
- const end = context.position;
355
- return {
356
- kind: 'false',
357
- start,
358
- end,
359
- text: context.original.substring(start.offset, end.offset),
360
- value: false,
361
- comments,
362
- };
363
- }
364
- /**
365
- * Read the constant `null` from the context.
366
- * @private
367
- */
368
- function _readNull(context, comments = _readBlanks(context)) {
369
- const start = context.position;
370
- _token(context, 'n');
371
- _token(context, 'u');
372
- _token(context, 'l');
373
- _token(context, 'l');
374
- const end = context.position;
375
- return {
376
- kind: 'null',
377
- start,
378
- end,
379
- text: context.original.substring(start.offset, end.offset),
380
- value: null,
381
- comments: comments,
382
- };
383
- }
384
- /**
385
- * Read the constant `NaN` from the context.
386
- * @private
387
- */
388
- function _readNaN(context, comments = _readBlanks(context)) {
389
- const start = context.position;
390
- _token(context, 'N');
391
- _token(context, 'a');
392
- _token(context, 'N');
393
- const end = context.position;
394
- return {
395
- kind: 'number',
396
- start,
397
- end,
398
- text: context.original.substring(start.offset, end.offset),
399
- value: NaN,
400
- comments: comments,
401
- };
402
- }
403
- /**
404
- * Read an array of JSON values from the context.
405
- * @private
406
- */
407
- function _readArray(context, comments = _readBlanks(context)) {
408
- const start = context.position;
409
- // Consume the first delimiter.
410
- _token(context, '[');
411
- const value = [];
412
- const elements = [];
413
- _readBlanks(context);
414
- if (_peek(context) != ']') {
415
- const node = _readValue(context);
416
- elements.push(node);
417
- value.push(node.value);
418
- }
419
- while (_peek(context) != ']') {
420
- _token(context, ',');
421
- const valueComments = _readBlanks(context);
422
- if ((context.mode & JsonParseMode.TrailingCommasAllowed) !== 0 && _peek(context) === ']') {
423
- break;
424
- }
425
- const node = _readValue(context, valueComments);
426
- elements.push(node);
427
- value.push(node.value);
428
- }
429
- _token(context, ']');
430
- return {
431
- kind: 'array',
432
- start,
433
- end: context.position,
434
- text: context.original.substring(start.offset, context.position.offset),
435
- value,
436
- elements,
437
- comments,
438
- };
439
- }
440
- /**
441
- * Read an identifier from the context. An identifier is a valid JavaScript identifier, and this
442
- * function is only used in Loose mode.
443
- * @private
444
- */
445
- function _readIdentifier(context, comments = _readBlanks(context)) {
446
- const start = context.position;
447
- let char = _peek(context);
448
- if (char && '0123456789'.indexOf(char) != -1) {
449
- const identifierNode = _readNumber(context);
450
- return {
451
- kind: 'identifier',
452
- start,
453
- end: identifierNode.end,
454
- text: identifierNode.text,
455
- value: identifierNode.value.toString(),
456
- };
457
- }
458
- const identValidFirstChar = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMOPQRSTUVWXYZ';
459
- const identValidChar = '_$abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMOPQRSTUVWXYZ0123456789';
460
- let first = true;
461
- let value = '';
462
- while (true) {
463
- char = _token(context);
464
- if (char == undefined ||
465
- (first ? identValidFirstChar.indexOf(char) : identValidChar.indexOf(char)) == -1) {
466
- context.position = context.previous;
467
- return {
468
- kind: 'identifier',
469
- start,
470
- end: context.position,
471
- text: context.original.slice(start.offset, start.offset + context.position.offset),
472
- value,
473
- comments,
474
- };
475
- }
476
- value += char;
477
- first = false;
478
- }
479
- }
480
- /**
481
- * Read a property from the context. A property is a string or (in Loose mode only) a number or
482
- * an identifier, followed by a colon `:`.
483
- * @private
484
- */
485
- function _readProperty(context, comments = _readBlanks(context)) {
486
- const start = context.position;
487
- let key;
488
- if ((context.mode & JsonParseMode.IdentifierKeyNamesAllowed) != 0) {
489
- const top = _peek(context);
490
- if (top == '"' || top == "'") {
491
- key = _readString(context);
492
- }
493
- else {
494
- key = _readIdentifier(context);
495
- }
496
- }
497
- else {
498
- key = _readString(context);
499
- }
500
- _readBlanks(context);
501
- _token(context, ':');
502
- const value = _readValue(context);
503
- const end = context.position;
504
- return {
505
- kind: 'keyvalue',
506
- key,
507
- value,
508
- start,
509
- end,
510
- text: context.original.substring(start.offset, end.offset),
511
- comments,
512
- };
513
- }
514
- /**
515
- * Read an object of properties -> JSON values from the context.
516
- * @private
517
- */
518
- function _readObject(context, comments = _readBlanks(context)) {
519
- const start = context.position;
520
- // Consume the first delimiter.
521
- _token(context, '{');
522
- const value = {};
523
- const properties = [];
524
- _readBlanks(context);
525
- if (_peek(context) != '}') {
526
- const property = _readProperty(context);
527
- value[property.key.value] = property.value.value;
528
- properties.push(property);
529
- while (_peek(context) != '}') {
530
- _token(context, ',');
531
- const propertyComments = _readBlanks(context);
532
- if ((context.mode & JsonParseMode.TrailingCommasAllowed) !== 0 && _peek(context) === '}') {
533
- break;
534
- }
535
- const property = _readProperty(context, propertyComments);
536
- value[property.key.value] = property.value.value;
537
- properties.push(property);
538
- }
539
- }
540
- _token(context, '}');
541
- return {
542
- kind: 'object',
543
- properties,
544
- start,
545
- end: context.position,
546
- value,
547
- text: context.original.substring(start.offset, context.position.offset),
548
- comments,
549
- };
550
- }
551
- /**
552
- * Remove any blank character or comments (in Loose mode) from the context, returning an array
553
- * of comments if any are found.
554
- * @private
555
- */
556
- function _readBlanks(context) {
557
- if ((context.mode & JsonParseMode.CommentsAllowed) != 0) {
558
- const comments = [];
559
- while (true) {
560
- const char = context.original[context.position.offset];
561
- if (char == '/' && context.original[context.position.offset + 1] == '*') {
562
- const start = context.position;
563
- // Multi line comment.
564
- _next(context);
565
- _next(context);
566
- while (context.original[context.position.offset] != '*' ||
567
- context.original[context.position.offset + 1] != '/') {
568
- _next(context);
569
- if (context.position.offset >= context.original.length) {
570
- throw new UnexpectedEndOfInputException(context);
571
- }
572
- }
573
- // Remove "*/".
574
- _next(context);
575
- _next(context);
576
- comments.push({
577
- kind: 'multicomment',
578
- start,
579
- end: context.position,
580
- text: context.original.substring(start.offset, context.position.offset),
581
- content: context.original.substring(start.offset + 2, context.position.offset - 2),
582
- });
583
- }
584
- else if (char == '/' && context.original[context.position.offset + 1] == '/') {
585
- const start = context.position;
586
- // Multi line comment.
587
- _next(context);
588
- _next(context);
589
- while (context.original[context.position.offset] != '\n') {
590
- _next(context);
591
- if (context.position.offset >= context.original.length) {
592
- break;
593
- }
594
- }
595
- // Remove "\n".
596
- if (context.position.offset < context.original.length) {
597
- _next(context);
598
- }
599
- comments.push({
600
- kind: 'comment',
601
- start,
602
- end: context.position,
603
- text: context.original.substring(start.offset, context.position.offset),
604
- content: context.original.substring(start.offset + 2, context.position.offset - 1),
605
- });
606
- }
607
- else if (char == ' ' || char == '\t' || char == '\n' || char == '\r' || char == '\f') {
608
- _next(context);
609
- }
610
- else {
611
- break;
612
- }
613
- }
614
- return comments;
615
- }
616
- else {
617
- let char = context.original[context.position.offset];
618
- while (char == ' ' || char == '\t' || char == '\n' || char == '\r' || char == '\f') {
619
- _next(context);
620
- char = context.original[context.position.offset];
621
- }
622
- return [];
623
- }
624
- }
625
- /**
626
- * Read a JSON value from the context, which can be any form of JSON value.
627
- * @private
628
- */
629
- function _readValue(context, comments = _readBlanks(context)) {
630
- let result;
631
- // Clean up before.
632
- const char = _peek(context);
633
- switch (char) {
634
- case undefined:
635
- throw new UnexpectedEndOfInputException(context);
636
- case '-':
637
- case '0':
638
- case '1':
639
- case '2':
640
- case '3':
641
- case '4':
642
- case '5':
643
- case '6':
644
- case '7':
645
- case '8':
646
- case '9':
647
- result = _readNumber(context, comments);
648
- break;
649
- case '.':
650
- case '+':
651
- if ((context.mode & JsonParseMode.LaxNumberParsingAllowed) == 0) {
652
- throw new InvalidJsonCharacterException(context);
653
- }
654
- result = _readNumber(context, comments);
655
- break;
656
- case "'":
657
- case '"':
658
- result = _readString(context, comments);
659
- break;
660
- case 'I':
661
- if ((context.mode & JsonParseMode.NumberConstantsAllowed) == 0) {
662
- throw new InvalidJsonCharacterException(context);
663
- }
664
- result = _readNumber(context, comments);
665
- break;
666
- case 'N':
667
- if ((context.mode & JsonParseMode.NumberConstantsAllowed) == 0) {
668
- throw new InvalidJsonCharacterException(context);
669
- }
670
- result = _readNaN(context, comments);
671
- break;
672
- case 't':
673
- result = _readTrue(context, comments);
674
- break;
675
- case 'f':
676
- result = _readFalse(context, comments);
677
- break;
678
- case 'n':
679
- result = _readNull(context, comments);
680
- break;
681
- case '[':
682
- result = _readArray(context, comments);
683
- break;
684
- case '{':
685
- result = _readObject(context, comments);
686
- break;
687
- default:
688
- throw new InvalidJsonCharacterException(context);
689
- }
690
- // Clean up after.
691
- _readBlanks(context);
692
- return result;
693
- }
694
- /**
695
- * The Parse mode used for parsing the JSON string.
696
- */
697
- var JsonParseMode;
698
- (function (JsonParseMode) {
699
- JsonParseMode[JsonParseMode["Strict"] = 0] = "Strict";
700
- JsonParseMode[JsonParseMode["CommentsAllowed"] = 1] = "CommentsAllowed";
701
- JsonParseMode[JsonParseMode["SingleQuotesAllowed"] = 2] = "SingleQuotesAllowed";
702
- JsonParseMode[JsonParseMode["IdentifierKeyNamesAllowed"] = 4] = "IdentifierKeyNamesAllowed";
703
- JsonParseMode[JsonParseMode["TrailingCommasAllowed"] = 8] = "TrailingCommasAllowed";
704
- JsonParseMode[JsonParseMode["HexadecimalNumberAllowed"] = 16] = "HexadecimalNumberAllowed";
705
- JsonParseMode[JsonParseMode["MultiLineStringAllowed"] = 32] = "MultiLineStringAllowed";
706
- JsonParseMode[JsonParseMode["LaxNumberParsingAllowed"] = 64] = "LaxNumberParsingAllowed";
707
- JsonParseMode[JsonParseMode["NumberConstantsAllowed"] = 128] = "NumberConstantsAllowed";
708
- JsonParseMode[JsonParseMode["Default"] = 0] = "Default";
709
- JsonParseMode[JsonParseMode["Loose"] = 255] = "Loose";
710
- JsonParseMode[JsonParseMode["Json"] = 0] = "Json";
711
- JsonParseMode[JsonParseMode["Json5"] = 255] = "Json5";
712
- })(JsonParseMode = exports.JsonParseMode || (exports.JsonParseMode = {}));
713
- /**
714
- * Parse the JSON string and return its AST. The AST may be losing data (end comments are
715
- * discarded for example, and space characters are not represented in the AST), but all values
716
- * will have a single node in the AST (a 1-to-1 mapping).
717
- *
718
- * @deprecated Deprecated since version 11. Use 3rd party JSON parsers such as `jsonc-parser` instead.
719
- * @param input The string to use.
720
- * @param mode The mode to parse the input with. {@see JsonParseMode}.
721
- * @returns {JsonAstNode} The root node of the value of the AST.
722
- */
723
- function parseJsonAst(input, mode = JsonParseMode.Default) {
724
- if (mode == JsonParseMode.Default) {
725
- mode = JsonParseMode.Strict;
726
- }
727
- const context = {
728
- position: { offset: 0, line: 0, character: 0 },
729
- previous: { offset: 0, line: 0, character: 0 },
730
- original: input,
731
- comments: undefined,
732
- mode,
733
- };
734
- const ast = _readValue(context);
735
- if (context.position.offset < input.length) {
736
- const rest = input.slice(context.position.offset);
737
- const i = rest.length > 20 ? rest.slice(0, 20) + '...' : rest;
738
- throw new Error(`Expected end of file, got "${i}" at ` +
739
- `${context.position.line}:${context.position.character}.`);
740
- }
741
- return ast;
742
- }
743
- exports.parseJsonAst = parseJsonAst;