janja 2.0.0 → 2.1.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.
- package/dist/index.cjs.js +304 -288
- package/dist/index.d.ts +43 -34
- package/dist/index.esm.js +304 -288
- package/dist/index.js +304 -288
- package/package.json +4 -3
package/dist/index.js
CHANGED
|
@@ -25,13 +25,39 @@
|
|
|
25
25
|
NodeType["UNKNOWN"] = "UNKNOWN";
|
|
26
26
|
NodeType["UNEXPECTED"] = "UNEXPECTED";
|
|
27
27
|
})(NodeType || (NodeType = {}));
|
|
28
|
-
class
|
|
29
|
-
constructor(
|
|
30
|
-
Object.defineProperty(this, "
|
|
28
|
+
class Traversal {
|
|
29
|
+
constructor() {
|
|
30
|
+
Object.defineProperty(this, "type", {
|
|
31
|
+
enumerable: true,
|
|
32
|
+
configurable: true,
|
|
33
|
+
writable: true,
|
|
34
|
+
value: null
|
|
35
|
+
});
|
|
36
|
+
Object.defineProperty(this, "loc", {
|
|
37
|
+
enumerable: true,
|
|
38
|
+
configurable: true,
|
|
39
|
+
writable: true,
|
|
40
|
+
value: null
|
|
41
|
+
});
|
|
42
|
+
Object.defineProperty(this, "body", {
|
|
31
43
|
enumerable: true,
|
|
32
44
|
configurable: true,
|
|
33
45
|
writable: true,
|
|
34
|
-
value:
|
|
46
|
+
value: void 0
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
traverse(cb) {
|
|
50
|
+
this.body?.forEach(cb);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
class RootNode extends Traversal {
|
|
54
|
+
constructor(body, loc) {
|
|
55
|
+
super();
|
|
56
|
+
Object.defineProperty(this, "body", {
|
|
57
|
+
enumerable: true,
|
|
58
|
+
configurable: true,
|
|
59
|
+
writable: true,
|
|
60
|
+
value: body
|
|
35
61
|
});
|
|
36
62
|
Object.defineProperty(this, "loc", {
|
|
37
63
|
enumerable: true,
|
|
@@ -47,8 +73,9 @@
|
|
|
47
73
|
});
|
|
48
74
|
}
|
|
49
75
|
}
|
|
50
|
-
class OutputNode {
|
|
76
|
+
class OutputNode extends Traversal {
|
|
51
77
|
constructor(val, loc, strip, exp) {
|
|
78
|
+
super();
|
|
52
79
|
Object.defineProperty(this, "val", {
|
|
53
80
|
enumerable: true,
|
|
54
81
|
configurable: true,
|
|
@@ -81,8 +108,9 @@
|
|
|
81
108
|
});
|
|
82
109
|
}
|
|
83
110
|
}
|
|
84
|
-
class TextNode {
|
|
111
|
+
class TextNode extends Traversal {
|
|
85
112
|
constructor(val, loc, strip) {
|
|
113
|
+
super();
|
|
86
114
|
Object.defineProperty(this, "val", {
|
|
87
115
|
enumerable: true,
|
|
88
116
|
configurable: true,
|
|
@@ -109,8 +137,9 @@
|
|
|
109
137
|
});
|
|
110
138
|
}
|
|
111
139
|
}
|
|
112
|
-
class IfNode {
|
|
140
|
+
class IfNode extends Traversal {
|
|
113
141
|
constructor(test, body, alternatives, loc, strip) {
|
|
142
|
+
super();
|
|
114
143
|
Object.defineProperty(this, "test", {
|
|
115
144
|
enumerable: true,
|
|
116
145
|
configurable: true,
|
|
@@ -148,9 +177,14 @@
|
|
|
148
177
|
value: NodeType.IF
|
|
149
178
|
});
|
|
150
179
|
}
|
|
180
|
+
traverse(cb) {
|
|
181
|
+
this.body?.forEach(cb);
|
|
182
|
+
this.alternatives?.forEach((node) => node.body?.forEach(cb));
|
|
183
|
+
}
|
|
151
184
|
}
|
|
152
|
-
class ElseIfNode {
|
|
185
|
+
class ElseIfNode extends Traversal {
|
|
153
186
|
constructor(test, body, loc, strip) {
|
|
187
|
+
super();
|
|
154
188
|
Object.defineProperty(this, "test", {
|
|
155
189
|
enumerable: true,
|
|
156
190
|
configurable: true,
|
|
@@ -183,8 +217,9 @@
|
|
|
183
217
|
});
|
|
184
218
|
}
|
|
185
219
|
}
|
|
186
|
-
class ElseNode {
|
|
220
|
+
class ElseNode extends Traversal {
|
|
187
221
|
constructor(body, loc, strip) {
|
|
222
|
+
super();
|
|
188
223
|
Object.defineProperty(this, "body", {
|
|
189
224
|
enumerable: true,
|
|
190
225
|
configurable: true,
|
|
@@ -211,8 +246,9 @@
|
|
|
211
246
|
});
|
|
212
247
|
}
|
|
213
248
|
}
|
|
214
|
-
class ForNode {
|
|
249
|
+
class ForNode extends Traversal {
|
|
215
250
|
constructor(loop, body, loc, strip) {
|
|
251
|
+
super();
|
|
216
252
|
Object.defineProperty(this, "loop", {
|
|
217
253
|
enumerable: true,
|
|
218
254
|
configurable: true,
|
|
@@ -245,8 +281,9 @@
|
|
|
245
281
|
});
|
|
246
282
|
}
|
|
247
283
|
}
|
|
248
|
-
class BreakNode {
|
|
284
|
+
class BreakNode extends Traversal {
|
|
249
285
|
constructor(val, loc, strip) {
|
|
286
|
+
super();
|
|
250
287
|
Object.defineProperty(this, "val", {
|
|
251
288
|
enumerable: true,
|
|
252
289
|
configurable: true,
|
|
@@ -273,8 +310,9 @@
|
|
|
273
310
|
});
|
|
274
311
|
}
|
|
275
312
|
}
|
|
276
|
-
class ContinueNode {
|
|
313
|
+
class ContinueNode extends Traversal {
|
|
277
314
|
constructor(val, loc, strip) {
|
|
315
|
+
super();
|
|
278
316
|
Object.defineProperty(this, "val", {
|
|
279
317
|
enumerable: true,
|
|
280
318
|
configurable: true,
|
|
@@ -301,8 +339,9 @@
|
|
|
301
339
|
});
|
|
302
340
|
}
|
|
303
341
|
}
|
|
304
|
-
class UnknownDirectiveNode {
|
|
342
|
+
class UnknownDirectiveNode extends Traversal {
|
|
305
343
|
constructor(name, val, loc, strip) {
|
|
344
|
+
super();
|
|
306
345
|
Object.defineProperty(this, "name", {
|
|
307
346
|
enumerable: true,
|
|
308
347
|
configurable: true,
|
|
@@ -335,8 +374,9 @@
|
|
|
335
374
|
});
|
|
336
375
|
}
|
|
337
376
|
}
|
|
338
|
-
class UnexpectedDirectiveNode {
|
|
377
|
+
class UnexpectedDirectiveNode extends Traversal {
|
|
339
378
|
constructor(name, val, loc, strip) {
|
|
379
|
+
super();
|
|
340
380
|
Object.defineProperty(this, "name", {
|
|
341
381
|
enumerable: true,
|
|
342
382
|
configurable: true,
|
|
@@ -369,8 +409,9 @@
|
|
|
369
409
|
});
|
|
370
410
|
}
|
|
371
411
|
}
|
|
372
|
-
class CommentNode {
|
|
412
|
+
class CommentNode extends Traversal {
|
|
373
413
|
constructor(val, loc, strip) {
|
|
414
|
+
super();
|
|
374
415
|
Object.defineProperty(this, "val", {
|
|
375
416
|
enumerable: true,
|
|
376
417
|
configurable: true,
|
|
@@ -397,8 +438,9 @@
|
|
|
397
438
|
});
|
|
398
439
|
}
|
|
399
440
|
}
|
|
400
|
-
class IncludeNode {
|
|
441
|
+
class IncludeNode extends Traversal {
|
|
401
442
|
constructor(val, loc, strip) {
|
|
443
|
+
super();
|
|
402
444
|
Object.defineProperty(this, "val", {
|
|
403
445
|
enumerable: true,
|
|
404
446
|
configurable: true,
|
|
@@ -425,8 +467,9 @@
|
|
|
425
467
|
});
|
|
426
468
|
}
|
|
427
469
|
}
|
|
428
|
-
class BlockNode {
|
|
470
|
+
class BlockNode extends Traversal {
|
|
429
471
|
constructor(val, body, loc, strip) {
|
|
472
|
+
super();
|
|
430
473
|
Object.defineProperty(this, "val", {
|
|
431
474
|
enumerable: true,
|
|
432
475
|
configurable: true,
|
|
@@ -459,8 +502,9 @@
|
|
|
459
502
|
});
|
|
460
503
|
}
|
|
461
504
|
}
|
|
462
|
-
class SuperNode {
|
|
505
|
+
class SuperNode extends Traversal {
|
|
463
506
|
constructor(val, loc, strip) {
|
|
507
|
+
super();
|
|
464
508
|
Object.defineProperty(this, "val", {
|
|
465
509
|
enumerable: true,
|
|
466
510
|
configurable: true,
|
|
@@ -487,8 +531,9 @@
|
|
|
487
531
|
});
|
|
488
532
|
}
|
|
489
533
|
}
|
|
490
|
-
class MacroNode {
|
|
534
|
+
class MacroNode extends Traversal {
|
|
491
535
|
constructor(val, body, loc, strip) {
|
|
536
|
+
super();
|
|
492
537
|
Object.defineProperty(this, "val", {
|
|
493
538
|
enumerable: true,
|
|
494
539
|
configurable: true,
|
|
@@ -521,8 +566,9 @@
|
|
|
521
566
|
});
|
|
522
567
|
}
|
|
523
568
|
}
|
|
524
|
-
class CallerNode {
|
|
569
|
+
class CallerNode extends Traversal {
|
|
525
570
|
constructor(val, loc, strip) {
|
|
571
|
+
super();
|
|
526
572
|
Object.defineProperty(this, "val", {
|
|
527
573
|
enumerable: true,
|
|
528
574
|
configurable: true,
|
|
@@ -549,8 +595,9 @@
|
|
|
549
595
|
});
|
|
550
596
|
}
|
|
551
597
|
}
|
|
552
|
-
class CallNode {
|
|
598
|
+
class CallNode extends Traversal {
|
|
553
599
|
constructor(val, body, loc, strip) {
|
|
600
|
+
super();
|
|
554
601
|
Object.defineProperty(this, "val", {
|
|
555
602
|
enumerable: true,
|
|
556
603
|
configurable: true,
|
|
@@ -583,8 +630,9 @@
|
|
|
583
630
|
});
|
|
584
631
|
}
|
|
585
632
|
}
|
|
586
|
-
class SetNode {
|
|
633
|
+
class SetNode extends Traversal {
|
|
587
634
|
constructor(val, loc, strip) {
|
|
635
|
+
super();
|
|
588
636
|
Object.defineProperty(this, "val", {
|
|
589
637
|
enumerable: true,
|
|
590
638
|
configurable: true,
|
|
@@ -611,8 +659,9 @@
|
|
|
611
659
|
});
|
|
612
660
|
}
|
|
613
661
|
}
|
|
614
|
-
class CaptureNode {
|
|
662
|
+
class CaptureNode extends Traversal {
|
|
615
663
|
constructor(val, body, loc, strip) {
|
|
664
|
+
super();
|
|
616
665
|
Object.defineProperty(this, "val", {
|
|
617
666
|
enumerable: true,
|
|
618
667
|
configurable: true,
|
|
@@ -646,7 +695,7 @@
|
|
|
646
695
|
}
|
|
647
696
|
}
|
|
648
697
|
|
|
649
|
-
function highlightSource(message, source, { start: { line: l1, column: c1
|
|
698
|
+
function highlightSource(message, source, { start: { line: l1, column: c1 }, end: { line: l2, column: c2 } }) {
|
|
650
699
|
const output = [];
|
|
651
700
|
const caretLines = new Set();
|
|
652
701
|
const addCaretLine = (index) => {
|
|
@@ -721,7 +770,7 @@ ${output
|
|
|
721
770
|
const val = parser.parseExp(token.expression);
|
|
722
771
|
parser.advance();
|
|
723
772
|
const body = parser.parseUntil(['endblock']);
|
|
724
|
-
if (parser.match('endblock')) {
|
|
773
|
+
if (parser.match(['endblock'])) {
|
|
725
774
|
parser.advance();
|
|
726
775
|
}
|
|
727
776
|
else {
|
|
@@ -745,7 +794,7 @@ ${output
|
|
|
745
794
|
const val = parser.parseExp(token.expression);
|
|
746
795
|
parser.advance();
|
|
747
796
|
const body = parser.parseUntil(['endcall']);
|
|
748
|
-
if (parser.match('endcall')) {
|
|
797
|
+
if (parser.match(['endcall'])) {
|
|
749
798
|
parser.advance();
|
|
750
799
|
}
|
|
751
800
|
else {
|
|
@@ -763,7 +812,7 @@ ${output
|
|
|
763
812
|
const val = parser.parseExp(token.expression);
|
|
764
813
|
parser.advance();
|
|
765
814
|
const body = parser.parseUntil(['endcapture']);
|
|
766
|
-
if (parser.match('endcapture')) {
|
|
815
|
+
if (parser.match(['endcapture'])) {
|
|
767
816
|
parser.advance();
|
|
768
817
|
}
|
|
769
818
|
else {
|
|
@@ -781,7 +830,7 @@ ${output
|
|
|
781
830
|
const loop = parser.parseExp(token.expression);
|
|
782
831
|
parser.advance();
|
|
783
832
|
const body = parser.parseUntil(['endfor']);
|
|
784
|
-
if (parser.match('endfor')) {
|
|
833
|
+
if (parser.match(['endfor'])) {
|
|
785
834
|
parser.advance();
|
|
786
835
|
}
|
|
787
836
|
else {
|
|
@@ -811,13 +860,13 @@ ${output
|
|
|
811
860
|
parser.advance();
|
|
812
861
|
const body = parser.parseUntil(['else', 'elseif', 'elsif', 'elif', 'endif']);
|
|
813
862
|
const alternatives = [];
|
|
814
|
-
while (parser.
|
|
863
|
+
while (parser.match(['else', 'elseif', 'elsif', 'elif'])) {
|
|
815
864
|
const branchToken = parser.peek();
|
|
816
865
|
alternatives.push(branchToken.name.toLowerCase() === 'else'
|
|
817
866
|
? parseElse(branchToken, parser)
|
|
818
867
|
: parseElseIf(branchToken, parser));
|
|
819
868
|
}
|
|
820
|
-
if (parser.match('endif')) {
|
|
869
|
+
if (parser.match(['endif'])) {
|
|
821
870
|
parser.advance();
|
|
822
871
|
}
|
|
823
872
|
else {
|
|
@@ -827,10 +876,9 @@ ${output
|
|
|
827
876
|
}
|
|
828
877
|
function parseElseIf(token, parser) {
|
|
829
878
|
parser.requireExpression(token);
|
|
830
|
-
const test = parser.parseExp(token.expression);
|
|
831
879
|
parser.advance();
|
|
832
|
-
const body = parser.parseUntil(['else', 'elseif', 'elif', 'endif']);
|
|
833
|
-
return new ElseIfNode(
|
|
880
|
+
const body = parser.parseUntil(['else', 'elseif', 'elsif', 'elif', 'endif']);
|
|
881
|
+
return new ElseIfNode(parser.parseExp(token.expression), body, token.loc, token.strip);
|
|
834
882
|
}
|
|
835
883
|
function parseElse(token, parser) {
|
|
836
884
|
parser.requireNoExpression(token);
|
|
@@ -861,7 +909,7 @@ ${output
|
|
|
861
909
|
const name = parser.parseExp(token.expression);
|
|
862
910
|
parser.advance();
|
|
863
911
|
const body = parser.parseUntil(['endmacro']);
|
|
864
|
-
if (parser.match('endmacro')) {
|
|
912
|
+
if (parser.match(['endmacro'])) {
|
|
865
913
|
parser.advance();
|
|
866
914
|
}
|
|
867
915
|
else {
|
|
@@ -923,13 +971,13 @@ ${output
|
|
|
923
971
|
return value + addend;
|
|
924
972
|
}
|
|
925
973
|
function capitalize(value = '') {
|
|
926
|
-
return value.replace(/\b\w/g, match => match.toUpperCase());
|
|
974
|
+
return value.replace(/\b\w/g, (match) => match.toUpperCase());
|
|
927
975
|
}
|
|
928
976
|
function ceil(value = 0) {
|
|
929
977
|
return Math.ceil(value);
|
|
930
978
|
}
|
|
931
979
|
function compact(value = []) {
|
|
932
|
-
return value.filter(v => v != null);
|
|
980
|
+
return value.filter((v) => v != null);
|
|
933
981
|
}
|
|
934
982
|
function div(value = 0, divisor = 1) {
|
|
935
983
|
return value / divisor;
|
|
@@ -941,13 +989,13 @@ ${output
|
|
|
941
989
|
return +value % 2 === 0;
|
|
942
990
|
}
|
|
943
991
|
function fallback(value, defaultValue, anyFalsy = false) {
|
|
944
|
-
return anyFalsy ?
|
|
992
|
+
return anyFalsy ? value || defaultValue : (value ?? defaultValue);
|
|
945
993
|
}
|
|
946
994
|
function first(value = []) {
|
|
947
995
|
return [...value][0];
|
|
948
996
|
}
|
|
949
997
|
function get(value, ...path) {
|
|
950
|
-
return path.reduce((acc, cur) =>
|
|
998
|
+
return path.reduce((acc, cur) => acc && typeof acc === 'object' && cur in acc ? acc[cur] : undefined, value);
|
|
951
999
|
}
|
|
952
1000
|
function groupby(value = [], key) {
|
|
953
1001
|
return value.reduce((o, v) => {
|
|
@@ -977,13 +1025,13 @@ ${output
|
|
|
977
1025
|
return value.toLowerCase();
|
|
978
1026
|
}
|
|
979
1027
|
function map(value = [], key, defaultValue) {
|
|
980
|
-
return value.map(o => o[key] ?? defaultValue);
|
|
1028
|
+
return value.map((o) => o[key] ?? defaultValue);
|
|
981
1029
|
}
|
|
982
|
-
function max(value = 0, ...
|
|
983
|
-
return Math.max(value, ...
|
|
1030
|
+
function max(value = 0, ...rest) {
|
|
1031
|
+
return Math.max(value, ...rest);
|
|
984
1032
|
}
|
|
985
|
-
function min(value = 0, ...
|
|
986
|
-
return Math.min(value, ...
|
|
1033
|
+
function min(value = 0, ...rest) {
|
|
1034
|
+
return Math.min(value, ...rest);
|
|
987
1035
|
}
|
|
988
1036
|
function mul(value = 0, multiplier = 1) {
|
|
989
1037
|
return value * multiplier;
|
|
@@ -991,17 +1039,17 @@ ${output
|
|
|
991
1039
|
function odd(value = 0) {
|
|
992
1040
|
return +value % 2 === 1;
|
|
993
1041
|
}
|
|
994
|
-
function omit(value = {}, ...
|
|
995
|
-
return Object.entries(value).reduce((o, [k, v]) => (
|
|
1042
|
+
function omit(value = {}, ...omitKeys) {
|
|
1043
|
+
return Object.entries(value).reduce((o, [k, v]) => (omitKeys.includes(k) ? o : { ...o, [k]: v }), {});
|
|
996
1044
|
}
|
|
997
|
-
function pick(value = {}, ...
|
|
998
|
-
return
|
|
1045
|
+
function pick(value = {}, ...pickKeys) {
|
|
1046
|
+
return pickKeys.reduce((o, k) => ({ ...o, [k]: value[k] }), {});
|
|
999
1047
|
}
|
|
1000
1048
|
function repeat(value = '', count = 0) {
|
|
1001
1049
|
return value.repeat(count);
|
|
1002
1050
|
}
|
|
1003
|
-
function replace(value = '', search,
|
|
1004
|
-
return value.replace(new RegExp(search, 'g'),
|
|
1051
|
+
function replace(value = '', search, replacement) {
|
|
1052
|
+
return value.replace(new RegExp(search, 'g'), replacement);
|
|
1005
1053
|
}
|
|
1006
1054
|
function reverse(value = []) {
|
|
1007
1055
|
return Array.isArray(value) ? value.reverse() : [...value].reverse().join('');
|
|
@@ -1027,8 +1075,8 @@ ${output
|
|
|
1027
1075
|
function trim(value = '') {
|
|
1028
1076
|
return value.trim();
|
|
1029
1077
|
}
|
|
1030
|
-
function truncate(value = '',
|
|
1031
|
-
return value.slice(0,
|
|
1078
|
+
function truncate(value = '', maxLength = 0, truncateStr = '...') {
|
|
1079
|
+
return (value.slice(0, maxLength) + (value.length > maxLength ? truncateStr : ''));
|
|
1032
1080
|
}
|
|
1033
1081
|
function unique(value = '') {
|
|
1034
1082
|
return Array.isArray(value)
|
|
@@ -1048,7 +1096,7 @@ ${output
|
|
|
1048
1096
|
return Object.values(value);
|
|
1049
1097
|
}
|
|
1050
1098
|
|
|
1051
|
-
var
|
|
1099
|
+
var _filters = /*#__PURE__*/Object.freeze({
|
|
1052
1100
|
__proto__: null,
|
|
1053
1101
|
abs: abs,
|
|
1054
1102
|
add: add,
|
|
@@ -1094,25 +1142,15 @@ ${output
|
|
|
1094
1142
|
values: values
|
|
1095
1143
|
});
|
|
1096
1144
|
|
|
1097
|
-
async function compileBlock({ val: { value } }, compiler) {
|
|
1098
|
-
if
|
|
1099
|
-
return;
|
|
1100
|
-
}
|
|
1101
|
-
compiler.state.block = value;
|
|
1102
|
-
const blockNode = compiler.blocks.get(value).pop();
|
|
1103
|
-
await compiler.compileNodes(blockNode.body);
|
|
1104
|
-
compiler.blocks.delete(value);
|
|
1105
|
-
compiler.state.block = undefined;
|
|
1145
|
+
async function compileBlock({ val: { value }, loc }, compiler) {
|
|
1146
|
+
compiler.pushRaw(loc, `if(b["${value}"]&&!(b["${value}"].u++)){`, `s+=await b["${value}"].s?.pop()?.()??"";`, '}');
|
|
1106
1147
|
}
|
|
1107
1148
|
async function compileSuper({ loc }, compiler) {
|
|
1108
1149
|
if (!compiler.state.block) {
|
|
1109
1150
|
compiler.options.debug?.(new CompileError('"super" should be used inside a block', compiler.template, loc));
|
|
1110
1151
|
return;
|
|
1111
1152
|
}
|
|
1112
|
-
|
|
1113
|
-
if (block) {
|
|
1114
|
-
await compiler.compileNodes(block.body);
|
|
1115
|
-
}
|
|
1153
|
+
compiler.pushRaw(loc, `s+=await b["${compiler.state.block}"]?.s?.pop()?.()??"";`);
|
|
1116
1154
|
}
|
|
1117
1155
|
const compilers$b = {
|
|
1118
1156
|
[NodeType.BLOCK]: compileBlock,
|
|
@@ -1210,6 +1248,8 @@ ${output
|
|
|
1210
1248
|
return this.compileTernary(expression);
|
|
1211
1249
|
case 'SEQ':
|
|
1212
1250
|
return this.compileSeq(expression);
|
|
1251
|
+
default:
|
|
1252
|
+
return '';
|
|
1213
1253
|
}
|
|
1214
1254
|
}
|
|
1215
1255
|
compileNot({ argument }) {
|
|
@@ -1218,10 +1258,10 @@ ${output
|
|
|
1218
1258
|
compileId({ value, path, args }) {
|
|
1219
1259
|
let s = `${this.context}.${value}`;
|
|
1220
1260
|
if (path) {
|
|
1221
|
-
s += path.map(p => `.${p.value}`).join('');
|
|
1261
|
+
s += path.map((p) => `.${p.value}`).join('');
|
|
1222
1262
|
}
|
|
1223
1263
|
if (args) {
|
|
1224
|
-
s += `(${args.map(arg => this.compileExp(arg)).join(',')})`;
|
|
1264
|
+
s += `(${args.map((arg) => this.compileExp(arg)).join(',')})`;
|
|
1225
1265
|
}
|
|
1226
1266
|
return s;
|
|
1227
1267
|
}
|
|
@@ -1229,11 +1269,11 @@ ${output
|
|
|
1229
1269
|
return value === undefined ? 'undefined' : JSON.stringify(value);
|
|
1230
1270
|
}
|
|
1231
1271
|
compileSeq({ elements }) {
|
|
1232
|
-
return elements.map(element => this.compileExp(element)).toString();
|
|
1272
|
+
return elements.map((element) => this.compileExp(element)).toString();
|
|
1233
1273
|
}
|
|
1234
1274
|
compilePipe({ left, right }) {
|
|
1235
1275
|
const { value: name, args = [] } = right;
|
|
1236
|
-
return `(await ${this.filters}.${name}.call(${[this.context, this.compileExp(left), ...args.map(arg => arg.type === 'SET' ? `${this.context}.${arg.left.value}=${this.compileExp(arg.right)}` : this.compileExp(arg))].join(',')}))`;
|
|
1276
|
+
return `(await ${this.filters}.${name}.call(${[this.context, this.compileExp(left), ...args.map((arg) => (arg.type === 'SET' ? `${this.context}.${arg.left.value}=${this.compileExp(arg.right)}` : this.compileExp(arg)))].join(',')}))`;
|
|
1237
1277
|
}
|
|
1238
1278
|
compileOf({ left, right }) {
|
|
1239
1279
|
// destructuring
|
|
@@ -1264,7 +1304,7 @@ ${output
|
|
|
1264
1304
|
async function compileCall({ val, body, loc }, compiler) {
|
|
1265
1305
|
compiler.pushRaw(loc, `await ${new ExpCompiler().compile(val, compiler.context)}(async()=>{`);
|
|
1266
1306
|
await compiler.compileNodes(body);
|
|
1267
|
-
compiler.pushRaw(null,
|
|
1307
|
+
compiler.pushRaw(null, '});');
|
|
1268
1308
|
}
|
|
1269
1309
|
const compilers$a = {
|
|
1270
1310
|
[NodeType.CALL]: compileCall,
|
|
@@ -1273,7 +1313,7 @@ ${output
|
|
|
1273
1313
|
async function compileCapture({ val, body, loc }, compiler) {
|
|
1274
1314
|
compiler.pushRaw(loc, `${compiler.context}.${val.value}=await(async(s)=>{`);
|
|
1275
1315
|
await compiler.compileNodes(body);
|
|
1276
|
-
compiler.pushRaw(null,
|
|
1316
|
+
compiler.pushRaw(null, 'return s;})("");');
|
|
1277
1317
|
}
|
|
1278
1318
|
const compilers$9 = {
|
|
1279
1319
|
[NodeType.CAPTURE]: compileCapture,
|
|
@@ -1291,20 +1331,20 @@ ${output
|
|
|
1291
1331
|
async function compileFor({ loop, body }, compiler) {
|
|
1292
1332
|
const { context } = compiler;
|
|
1293
1333
|
if (loop.left.type === 'SEQ') {
|
|
1294
|
-
compiler.pushRaw(loop.loc, `for(${new ExpCompiler().compile(loop, context)}){`, `const ${compiler.in()}={`, `...${context},`, `${loop.left.elements.map(({ value }) => value).join(',')},`,
|
|
1334
|
+
compiler.pushRaw(loop.loc, `for(${new ExpCompiler().compile(loop, context)}){`, `const ${compiler.in()}={`, `...${context},`, `${loop.left.elements.map(({ value }) => value).join(',')},`, '};');
|
|
1295
1335
|
}
|
|
1296
1336
|
else {
|
|
1297
|
-
compiler.pushRaw(loop.loc, `for(${new ExpCompiler().compile(loop, context)}){`, `const ${compiler.in()}={`, `...${context},`, `${loop.left.value},`,
|
|
1337
|
+
compiler.pushRaw(loop.loc, `for(${new ExpCompiler().compile(loop, context)}){`, `const ${compiler.in()}={`, `...${context},`, `${loop.left.value},`, '};');
|
|
1298
1338
|
}
|
|
1299
1339
|
await compiler.compileNodes(body);
|
|
1300
1340
|
compiler.out();
|
|
1301
1341
|
compiler.pushRaw(null, '}');
|
|
1302
1342
|
}
|
|
1303
1343
|
function compileBreak({ loc }, compiler) {
|
|
1304
|
-
compiler.pushRaw(loc,
|
|
1344
|
+
compiler.pushRaw(loc, 'break;');
|
|
1305
1345
|
}
|
|
1306
1346
|
function compileContinue({ loc }, compiler) {
|
|
1307
|
-
compiler.pushRaw(loc,
|
|
1347
|
+
compiler.pushRaw(loc, 'continue;');
|
|
1308
1348
|
}
|
|
1309
1349
|
const compilers$7 = {
|
|
1310
1350
|
[NodeType.FOR]: compileFor,
|
|
@@ -1331,8 +1371,8 @@ ${output
|
|
|
1331
1371
|
[NodeType.IF]: compileIf,
|
|
1332
1372
|
};
|
|
1333
1373
|
|
|
1334
|
-
async function compileInclude({ val:
|
|
1335
|
-
compiler.pushRaw(loc, `
|
|
1374
|
+
async function compileInclude({ val: { value }, loc }, compiler) {
|
|
1375
|
+
compiler.pushRaw(loc, `s+=await p["${value}"]?.()??"";`);
|
|
1336
1376
|
}
|
|
1337
1377
|
const compilers$5 = {
|
|
1338
1378
|
[NodeType.INCLUDE]: compileInclude,
|
|
@@ -1342,15 +1382,19 @@ ${output
|
|
|
1342
1382
|
const { context } = compiler;
|
|
1343
1383
|
const { elements } = right;
|
|
1344
1384
|
const expCompiler = new ExpCompiler();
|
|
1345
|
-
compiler.pushRaw(loc, `${expCompiler.compile(left)}=(${
|
|
1346
|
-
|
|
1347
|
-
|
|
1385
|
+
compiler.pushRaw(loc, `${expCompiler.compile(left)}=(${elements
|
|
1386
|
+
.map((el) => el.type === 'SET'
|
|
1387
|
+
? `${el.left.value}=${expCompiler.compile(el.right)}`
|
|
1388
|
+
: el.value)
|
|
1389
|
+
.join(',')})=>async(_c)=>{`, `const ${compiler.in()}={`, `...${context},`, elements
|
|
1390
|
+
.map((el) => el.type === 'SET' ? el.left.value : el.value)
|
|
1391
|
+
.join(','), '};');
|
|
1348
1392
|
await compiler.compileNodes(body);
|
|
1349
1393
|
compiler.out();
|
|
1350
1394
|
compiler.pushRaw(null, '};');
|
|
1351
1395
|
}
|
|
1352
1396
|
function compileCaller({ loc }, compiler) {
|
|
1353
|
-
compiler.pushRaw(loc,
|
|
1397
|
+
compiler.pushRaw(loc, 'await _c?.();');
|
|
1354
1398
|
}
|
|
1355
1399
|
const compilers$4 = {
|
|
1356
1400
|
[NodeType.MACRO]: compileMacro,
|
|
@@ -1366,7 +1410,6 @@ ${output
|
|
|
1366
1410
|
|
|
1367
1411
|
async function compileSet({ val, loc }, compiler) {
|
|
1368
1412
|
compiler.pushRaw(loc, new ExpCompiler().compile(val, compiler.context));
|
|
1369
|
-
compiler.pushRaw(null, `});`);
|
|
1370
1413
|
}
|
|
1371
1414
|
const compilers$2 = {
|
|
1372
1415
|
[NodeType.SET]: compileSet,
|
|
@@ -1419,8 +1462,9 @@ ${output
|
|
|
1419
1462
|
const renderOptions = {
|
|
1420
1463
|
...compilerOptions,
|
|
1421
1464
|
globals: {},
|
|
1422
|
-
filters:
|
|
1465
|
+
filters: _filters,
|
|
1423
1466
|
autoEscape: true,
|
|
1467
|
+
plugins: [],
|
|
1424
1468
|
};
|
|
1425
1469
|
|
|
1426
1470
|
/**
|
|
@@ -1473,10 +1517,10 @@ ${output
|
|
|
1473
1517
|
}
|
|
1474
1518
|
getSourceLoc({ line, column }) {
|
|
1475
1519
|
return this.mappings
|
|
1476
|
-
.filter(({ target: { start, end } }) => start.line <= line
|
|
1477
|
-
|
|
1478
|
-
|
|
1479
|
-
|
|
1520
|
+
.filter(({ target: { start, end } }) => start.line <= line &&
|
|
1521
|
+
end.line >= line &&
|
|
1522
|
+
(start.line < line || start.column <= column) &&
|
|
1523
|
+
(end.line > line || end.column >= column))
|
|
1480
1524
|
.map(({ source }) => source);
|
|
1481
1525
|
}
|
|
1482
1526
|
}
|
|
@@ -1492,6 +1536,7 @@ ${output
|
|
|
1492
1536
|
});
|
|
1493
1537
|
}
|
|
1494
1538
|
get script() {
|
|
1539
|
+
// eslint-disable-next-line no-new-func
|
|
1495
1540
|
return new Function(CONTEXT, ESCAPE, FILTERS, this.code);
|
|
1496
1541
|
}
|
|
1497
1542
|
start() {
|
|
@@ -1534,7 +1579,7 @@ ${output
|
|
|
1534
1579
|
|
|
1535
1580
|
class Context extends OutScript {
|
|
1536
1581
|
constructor() {
|
|
1537
|
-
super();
|
|
1582
|
+
super(...arguments);
|
|
1538
1583
|
Object.defineProperty(this, "context", {
|
|
1539
1584
|
enumerable: true,
|
|
1540
1585
|
configurable: true,
|
|
@@ -1564,7 +1609,7 @@ ${output
|
|
|
1564
1609
|
return;
|
|
1565
1610
|
}
|
|
1566
1611
|
this.contexts.pop();
|
|
1567
|
-
this.context = this.contexts.at(-1)
|
|
1612
|
+
this.context = this.contexts.at(-1);
|
|
1568
1613
|
}
|
|
1569
1614
|
}
|
|
1570
1615
|
|
|
@@ -1600,30 +1645,30 @@ ${output
|
|
|
1600
1645
|
};
|
|
1601
1646
|
|
|
1602
1647
|
const expTokenTypes = {
|
|
1603
|
-
|
|
1604
|
-
|
|
1605
|
-
|
|
1606
|
-
|
|
1607
|
-
|
|
1608
|
-
|
|
1609
|
-
|
|
1610
|
-
|
|
1611
|
-
|
|
1612
|
-
|
|
1613
|
-
|
|
1614
|
-
|
|
1615
|
-
|
|
1648
|
+
and: 'AND',
|
|
1649
|
+
or: 'OR',
|
|
1650
|
+
not: 'NOT',
|
|
1651
|
+
is: 'IS',
|
|
1652
|
+
eq: 'EQ',
|
|
1653
|
+
ne: 'NE',
|
|
1654
|
+
gt: 'GT',
|
|
1655
|
+
lt: 'LT',
|
|
1656
|
+
ge: 'GE',
|
|
1657
|
+
le: 'LE',
|
|
1658
|
+
in: 'IN',
|
|
1659
|
+
ni: 'NI',
|
|
1660
|
+
of: 'OF',
|
|
1616
1661
|
'+': 'ADD',
|
|
1617
1662
|
'-': 'SUB',
|
|
1618
1663
|
'*': 'MUL',
|
|
1619
1664
|
'/': 'DIV',
|
|
1620
1665
|
'%': 'MOD',
|
|
1621
|
-
|
|
1622
|
-
|
|
1623
|
-
|
|
1624
|
-
|
|
1625
|
-
|
|
1626
|
-
|
|
1666
|
+
if: 'IF',
|
|
1667
|
+
else: 'ELSE',
|
|
1668
|
+
true: 'LIT',
|
|
1669
|
+
false: 'LIT',
|
|
1670
|
+
null: 'LIT',
|
|
1671
|
+
undefined: 'LIT',
|
|
1627
1672
|
'|': 'PIPE',
|
|
1628
1673
|
'=': 'SET',
|
|
1629
1674
|
'(': 'LP',
|
|
@@ -1710,7 +1755,7 @@ ${output
|
|
|
1710
1755
|
this.index++;
|
|
1711
1756
|
continue;
|
|
1712
1757
|
}
|
|
1713
|
-
if (char === '
|
|
1758
|
+
if (char === "'" || char === '"' || char === '`') {
|
|
1714
1759
|
this.readString(char);
|
|
1715
1760
|
continue;
|
|
1716
1761
|
}
|
|
@@ -1809,7 +1854,9 @@ ${output
|
|
|
1809
1854
|
}
|
|
1810
1855
|
break;
|
|
1811
1856
|
}
|
|
1812
|
-
const type = value in expTokenTypes
|
|
1857
|
+
const type = value in expTokenTypes
|
|
1858
|
+
? expTokenTypes[value]
|
|
1859
|
+
: 'ID';
|
|
1813
1860
|
this.tokens.push({
|
|
1814
1861
|
type,
|
|
1815
1862
|
value: type === 'LIT'
|
|
@@ -1924,12 +1971,12 @@ ${output
|
|
|
1924
1971
|
}
|
|
1925
1972
|
if (token.type === 'LP') {
|
|
1926
1973
|
if (!this.peek()) {
|
|
1927
|
-
throw new CompileError(
|
|
1974
|
+
throw new CompileError('unexpected end of expression', this.template, token.loc);
|
|
1928
1975
|
}
|
|
1929
1976
|
const elements = this.parseSequence();
|
|
1930
1977
|
const rp = this.consume('RP');
|
|
1931
1978
|
if (!rp) {
|
|
1932
|
-
throw new CompileError(
|
|
1979
|
+
throw new CompileError('expected "RP" after "LP"', this.template, token.loc);
|
|
1933
1980
|
}
|
|
1934
1981
|
left = {
|
|
1935
1982
|
...token,
|
|
@@ -1944,12 +1991,12 @@ ${output
|
|
|
1944
1991
|
}
|
|
1945
1992
|
if (token.type === 'NOT') {
|
|
1946
1993
|
if (!this.peek()) {
|
|
1947
|
-
throw new CompileError(
|
|
1994
|
+
throw new CompileError('unexpected end of expression', this.template, token.loc);
|
|
1948
1995
|
}
|
|
1949
1996
|
left = {
|
|
1950
1997
|
...token,
|
|
1951
1998
|
type: 'NOT',
|
|
1952
|
-
argument: this.parseExp(t => isHigher(token, t) ? 'BACK' : undefined),
|
|
1999
|
+
argument: this.parseExp((t) => isHigher(token, t) ? 'BACK' : undefined),
|
|
1953
2000
|
};
|
|
1954
2001
|
continue;
|
|
1955
2002
|
}
|
|
@@ -1966,7 +2013,7 @@ ${output
|
|
|
1966
2013
|
if (lp) {
|
|
1967
2014
|
left.args = this.parseSequence();
|
|
1968
2015
|
if (!this.consume('RP')) {
|
|
1969
|
-
throw new CompileError(
|
|
2016
|
+
throw new CompileError('expected "RP" after "LP"', this.template, lp.loc);
|
|
1970
2017
|
}
|
|
1971
2018
|
continue;
|
|
1972
2019
|
}
|
|
@@ -1980,7 +2027,7 @@ ${output
|
|
|
1980
2027
|
continue;
|
|
1981
2028
|
}
|
|
1982
2029
|
if (token.type === 'IF') {
|
|
1983
|
-
const test = this.parseExp(t => t.type === 'ELSE' ? 'BACK' : isHigher(token, t) ? 'BACK' : undefined);
|
|
2030
|
+
const test = this.parseExp((t) => t.type === 'ELSE' ? 'BACK' : isHigher(token, t) ? 'BACK' : undefined);
|
|
1984
2031
|
if (!test) {
|
|
1985
2032
|
throw new CompileError('expected test expression', this.template, token.loc);
|
|
1986
2033
|
}
|
|
@@ -1991,7 +2038,7 @@ ${output
|
|
|
1991
2038
|
consequent: left,
|
|
1992
2039
|
};
|
|
1993
2040
|
if (this.consume('ELSE')) {
|
|
1994
|
-
const alternative = this.parseExp(t => isHigher(token, t) ? 'BACK' : undefined);
|
|
2041
|
+
const alternative = this.parseExp((t) => isHigher(token, t) ? 'BACK' : undefined);
|
|
1995
2042
|
if (!alternative) {
|
|
1996
2043
|
throw new CompileError('expected alternative expression', this.template, token.loc);
|
|
1997
2044
|
}
|
|
@@ -1999,28 +2046,28 @@ ${output
|
|
|
1999
2046
|
}
|
|
2000
2047
|
continue;
|
|
2001
2048
|
}
|
|
2002
|
-
if (token.type === 'AND'
|
|
2003
|
-
|
|
2004
|
-
|
|
2005
|
-
|
|
2006
|
-
|
|
2007
|
-
|
|
2008
|
-
|
|
2009
|
-
|
|
2010
|
-
|
|
2011
|
-
|
|
2012
|
-
|
|
2013
|
-
|
|
2014
|
-
|
|
2015
|
-
|
|
2016
|
-
|
|
2017
|
-
|
|
2018
|
-
|
|
2019
|
-
|
|
2049
|
+
if (token.type === 'AND' ||
|
|
2050
|
+
token.type === 'OR' ||
|
|
2051
|
+
token.type === 'IS' ||
|
|
2052
|
+
token.type === 'EQ' ||
|
|
2053
|
+
token.type === 'NE' ||
|
|
2054
|
+
token.type === 'GT' ||
|
|
2055
|
+
token.type === 'LT' ||
|
|
2056
|
+
token.type === 'GE' ||
|
|
2057
|
+
token.type === 'LE' ||
|
|
2058
|
+
token.type === 'IN' ||
|
|
2059
|
+
token.type === 'NI' ||
|
|
2060
|
+
token.type === 'OF' ||
|
|
2061
|
+
token.type === 'ADD' ||
|
|
2062
|
+
token.type === 'SUB' ||
|
|
2063
|
+
token.type === 'MUL' ||
|
|
2064
|
+
token.type === 'DIV' ||
|
|
2065
|
+
token.type === 'MOD' ||
|
|
2066
|
+
token.type === 'SET') {
|
|
2020
2067
|
if (!left) {
|
|
2021
2068
|
throw new CompileError(`no left operand for "${token.type}"`, this.template, token.loc);
|
|
2022
2069
|
}
|
|
2023
|
-
const right = this.parseExp(t => isHigher(token, t) ? 'BACK' : undefined);
|
|
2070
|
+
const right = this.parseExp((t) => isHigher(token, t) ? 'BACK' : undefined);
|
|
2024
2071
|
if (!right) {
|
|
2025
2072
|
throw new CompileError(`no right operand for "${token.type}"`, this.template, token.loc);
|
|
2026
2073
|
}
|
|
@@ -2037,7 +2084,7 @@ ${output
|
|
|
2037
2084
|
throw new CompileError(`no left operand for "${token.type}"`, this.template, token.loc);
|
|
2038
2085
|
}
|
|
2039
2086
|
if (!this.check('ID')) {
|
|
2040
|
-
throw new CompileError(
|
|
2087
|
+
throw new CompileError('expected "ID" after "PIPE"', this.template, token.loc);
|
|
2041
2088
|
}
|
|
2042
2089
|
left = {
|
|
2043
2090
|
...token,
|
|
@@ -2061,10 +2108,10 @@ ${output
|
|
|
2061
2108
|
}
|
|
2062
2109
|
const token = this.next();
|
|
2063
2110
|
if (!token) {
|
|
2064
|
-
throw new CompileError(
|
|
2111
|
+
throw new CompileError('expected "ID" after "DOT"', this.template, dot.loc);
|
|
2065
2112
|
}
|
|
2066
2113
|
if (token.type !== 'ID') {
|
|
2067
|
-
throw new CompileError(
|
|
2114
|
+
throw new CompileError('expected "ID" after "DOT"', this.template, dot.loc);
|
|
2068
2115
|
}
|
|
2069
2116
|
ids.push({
|
|
2070
2117
|
...token,
|
|
@@ -2077,7 +2124,7 @@ ${output
|
|
|
2077
2124
|
parseSequence() {
|
|
2078
2125
|
const elements = [];
|
|
2079
2126
|
while (!this.check('RP')) {
|
|
2080
|
-
const element = this.parseExp(t => t.type === 'COMMA' ? 'BREAK' : undefined);
|
|
2127
|
+
const element = this.parseExp((t) => t.type === 'COMMA' ? 'BREAK' : undefined);
|
|
2081
2128
|
if (element) {
|
|
2082
2129
|
elements.push(element);
|
|
2083
2130
|
}
|
|
@@ -2101,7 +2148,7 @@ ${output
|
|
|
2101
2148
|
right.args = args;
|
|
2102
2149
|
}
|
|
2103
2150
|
if (!this.consume('RP')) {
|
|
2104
|
-
throw new CompileError(
|
|
2151
|
+
throw new CompileError('expected "RP" after "LP"', this.template, lp.loc);
|
|
2105
2152
|
}
|
|
2106
2153
|
}
|
|
2107
2154
|
return right;
|
|
@@ -2195,23 +2242,11 @@ ${output
|
|
|
2195
2242
|
...options,
|
|
2196
2243
|
};
|
|
2197
2244
|
const { commentOpen, commentClose, directiveOpen, directiveClose, outputOpen, outputClose, } = this.options;
|
|
2198
|
-
this.tokenPatterns =
|
|
2199
|
-
[
|
|
2200
|
-
|
|
2201
|
-
|
|
2202
|
-
|
|
2203
|
-
],
|
|
2204
|
-
[
|
|
2205
|
-
TokenType.DIRECTIVE,
|
|
2206
|
-
directiveOpen,
|
|
2207
|
-
directiveClose,
|
|
2208
|
-
],
|
|
2209
|
-
[
|
|
2210
|
-
TokenType.OUTPUT,
|
|
2211
|
-
outputOpen,
|
|
2212
|
-
outputClose,
|
|
2213
|
-
],
|
|
2214
|
-
].sort(([, open1], [, open2]) => open1.length > open2.length ? -1 : 1));
|
|
2245
|
+
this.tokenPatterns = [
|
|
2246
|
+
[TokenType.COMMENT, commentOpen, commentClose],
|
|
2247
|
+
[TokenType.DIRECTIVE, directiveOpen, directiveClose],
|
|
2248
|
+
[TokenType.OUTPUT, outputOpen, outputClose],
|
|
2249
|
+
].sort(([, open1], [, open2]) => open1.length > open2.length ? -1 : 1);
|
|
2215
2250
|
}
|
|
2216
2251
|
tokenize(template) {
|
|
2217
2252
|
this.template = template;
|
|
@@ -2280,7 +2315,9 @@ ${output
|
|
|
2280
2315
|
: {
|
|
2281
2316
|
val: val.slice(i + 1),
|
|
2282
2317
|
loc: {
|
|
2283
|
-
start: updatePosition(val.slice(0, i + 1), updatePosition(`${openMarker}${stripBefore ? '-' : ''}`, {
|
|
2318
|
+
start: updatePosition(val.slice(0, i + 1), updatePosition(`${openMarker}${stripBefore ? '-' : ''}`, {
|
|
2319
|
+
...start,
|
|
2320
|
+
})),
|
|
2284
2321
|
end: {
|
|
2285
2322
|
line: this.line,
|
|
2286
2323
|
column: this.column - closeMarker.length - +stripAfter,
|
|
@@ -2295,41 +2332,32 @@ ${output
|
|
|
2295
2332
|
}
|
|
2296
2333
|
consumeTextToken() {
|
|
2297
2334
|
const nextSpecialIndex = this.findNextSpecialIndex();
|
|
2298
|
-
|
|
2299
|
-
|
|
2300
|
-
|
|
2301
|
-
|
|
2302
|
-
|
|
2303
|
-
|
|
2304
|
-
|
|
2305
|
-
|
|
2306
|
-
|
|
2307
|
-
|
|
2308
|
-
|
|
2309
|
-
|
|
2310
|
-
|
|
2311
|
-
|
|
2312
|
-
|
|
2313
|
-
this.index = nextSpecialIndex;
|
|
2314
|
-
}
|
|
2315
|
-
else {
|
|
2316
|
-
updatePosition(this.template[this.index++], this);
|
|
2317
|
-
}
|
|
2335
|
+
const start = {
|
|
2336
|
+
line: this.line,
|
|
2337
|
+
column: this.column,
|
|
2338
|
+
};
|
|
2339
|
+
const val = this.template.slice(this.index, nextSpecialIndex);
|
|
2340
|
+
this.tokens.push({
|
|
2341
|
+
type: TokenType.TEXT,
|
|
2342
|
+
val: unescapeTag(val),
|
|
2343
|
+
loc: {
|
|
2344
|
+
start,
|
|
2345
|
+
end: updatePosition(val, this),
|
|
2346
|
+
},
|
|
2347
|
+
strip: {},
|
|
2348
|
+
});
|
|
2349
|
+
this.index = nextSpecialIndex;
|
|
2318
2350
|
}
|
|
2319
2351
|
lookahead(marker, offset = 0) {
|
|
2320
2352
|
return this.template.startsWith(marker, this.index + offset);
|
|
2321
2353
|
}
|
|
2322
2354
|
findNextSpecialIndex() {
|
|
2323
|
-
const { outputOpen, directiveOpen, commentOpen
|
|
2355
|
+
const { outputOpen, directiveOpen, commentOpen } = this.options;
|
|
2324
2356
|
const commentIndex = this.template.indexOf(commentOpen, this.index);
|
|
2325
2357
|
const directiveIndex = this.template.indexOf(directiveOpen, this.index);
|
|
2326
2358
|
const outputIndex = this.template.indexOf(outputOpen, this.index);
|
|
2327
|
-
const indexes = [
|
|
2328
|
-
|
|
2329
|
-
directiveIndex,
|
|
2330
|
-
outputIndex,
|
|
2331
|
-
]
|
|
2332
|
-
.filter(index => index !== -1)
|
|
2359
|
+
const indexes = [commentIndex, directiveIndex, outputIndex]
|
|
2360
|
+
.filter((index) => index !== -1)
|
|
2333
2361
|
.sort((a, b) => a - b);
|
|
2334
2362
|
return indexes.length ? indexes[0] : this.length;
|
|
2335
2363
|
}
|
|
@@ -2357,16 +2385,17 @@ ${output
|
|
|
2357
2385
|
const end = this.tokens.at(-1)?.loc.end ?? { line: 1, column: 1 };
|
|
2358
2386
|
this.expParser = new ExpParser(template);
|
|
2359
2387
|
this.cursor = 0;
|
|
2360
|
-
return new
|
|
2388
|
+
return new RootNode(this.parseUntil(), { start, end });
|
|
2361
2389
|
}
|
|
2362
2390
|
parseUntil(names) {
|
|
2363
2391
|
const nodes = [];
|
|
2364
2392
|
let prevToken = null;
|
|
2365
2393
|
while (true) {
|
|
2366
2394
|
const token = this.peek();
|
|
2367
|
-
if (!token
|
|
2368
|
-
|
|
2369
|
-
|
|
2395
|
+
if (!token ||
|
|
2396
|
+
(token.type === TokenType.DIRECTIVE &&
|
|
2397
|
+
names &&
|
|
2398
|
+
this.match(names, token))) {
|
|
2370
2399
|
break;
|
|
2371
2400
|
}
|
|
2372
2401
|
if (prevToken?.strip?.after && token.type === TokenType.TEXT) {
|
|
@@ -2406,8 +2435,8 @@ ${output
|
|
|
2406
2435
|
return new TextNode(val, loc, strip);
|
|
2407
2436
|
}
|
|
2408
2437
|
createDirective(token) {
|
|
2409
|
-
return this.options.parsers[token.name.toLowerCase()]?.(token, this)
|
|
2410
|
-
|
|
2438
|
+
return (this.options.parsers[token.name.toLowerCase()]?.(token, this) ??
|
|
2439
|
+
this.createUnknownDirective(token));
|
|
2411
2440
|
}
|
|
2412
2441
|
createUnexpectedDirective(token) {
|
|
2413
2442
|
this.options.debug?.(new CompileError(`Unexpected "${token.name}" directive`, this.template, token.loc));
|
|
@@ -2428,15 +2457,8 @@ ${output
|
|
|
2428
2457
|
advance() {
|
|
2429
2458
|
this.cursor++;
|
|
2430
2459
|
}
|
|
2431
|
-
match(
|
|
2432
|
-
return
|
|
2433
|
-
}
|
|
2434
|
-
isDirective(names) {
|
|
2435
|
-
const token = this.peek();
|
|
2436
|
-
if (token?.type !== TokenType.DIRECTIVE) {
|
|
2437
|
-
return false;
|
|
2438
|
-
}
|
|
2439
|
-
return names.includes(token.name.toLowerCase());
|
|
2460
|
+
match(names, token = this.peek()) {
|
|
2461
|
+
return names.includes(token?.name);
|
|
2440
2462
|
}
|
|
2441
2463
|
requireExpression({ expression, name, loc }) {
|
|
2442
2464
|
if (!expression) {
|
|
@@ -2465,31 +2487,31 @@ ${output
|
|
|
2465
2487
|
writable: true,
|
|
2466
2488
|
value: void 0
|
|
2467
2489
|
});
|
|
2468
|
-
Object.defineProperty(this, "
|
|
2490
|
+
Object.defineProperty(this, "state", {
|
|
2469
2491
|
enumerable: true,
|
|
2470
2492
|
configurable: true,
|
|
2471
2493
|
writable: true,
|
|
2472
2494
|
value: void 0
|
|
2473
2495
|
});
|
|
2474
|
-
Object.defineProperty(this, "
|
|
2496
|
+
Object.defineProperty(this, "isRoot", {
|
|
2475
2497
|
enumerable: true,
|
|
2476
2498
|
configurable: true,
|
|
2477
2499
|
writable: true,
|
|
2478
2500
|
value: void 0
|
|
2479
2501
|
});
|
|
2480
|
-
Object.defineProperty(this, "
|
|
2502
|
+
Object.defineProperty(this, "rootNode", {
|
|
2481
2503
|
enumerable: true,
|
|
2482
2504
|
configurable: true,
|
|
2483
2505
|
writable: true,
|
|
2484
2506
|
value: void 0
|
|
2485
2507
|
});
|
|
2486
|
-
Object.defineProperty(this, "
|
|
2508
|
+
Object.defineProperty(this, "partials", {
|
|
2487
2509
|
enumerable: true,
|
|
2488
2510
|
configurable: true,
|
|
2489
2511
|
writable: true,
|
|
2490
2512
|
value: void 0
|
|
2491
2513
|
});
|
|
2492
|
-
Object.defineProperty(this, "
|
|
2514
|
+
Object.defineProperty(this, "blocks", {
|
|
2493
2515
|
enumerable: true,
|
|
2494
2516
|
configurable: true,
|
|
2495
2517
|
writable: true,
|
|
@@ -2500,105 +2522,91 @@ ${output
|
|
|
2500
2522
|
...options,
|
|
2501
2523
|
};
|
|
2502
2524
|
}
|
|
2503
|
-
async compile(template, root = true, blocks = new Map()) {
|
|
2525
|
+
async compile(template, root = true, partials = new Map(), blocks = new Map()) {
|
|
2504
2526
|
this.template = template;
|
|
2505
|
-
this.
|
|
2506
|
-
this.partials =
|
|
2527
|
+
this.isRoot = root;
|
|
2528
|
+
this.partials = partials;
|
|
2507
2529
|
this.blocks = blocks;
|
|
2508
|
-
this.
|
|
2530
|
+
this.rootNode = new Parser(this.options).parse(template);
|
|
2509
2531
|
this.state = {};
|
|
2510
2532
|
this.start();
|
|
2511
|
-
|
|
2512
|
-
|
|
2513
|
-
if (this.partials.size
|
|
2514
|
-
this.pushRaw(null,
|
|
2533
|
+
await this.parseTemplates();
|
|
2534
|
+
if (this.isRoot) {
|
|
2535
|
+
if (this.partials.size) {
|
|
2536
|
+
this.pushRaw(null, 'const p={');
|
|
2515
2537
|
for (const [path, code] of this.partials) {
|
|
2516
|
-
this.pushRaw(null, `"${path}":async
|
|
2538
|
+
this.pushRaw(null, `"${path}":async()=>{`, code, '},');
|
|
2539
|
+
}
|
|
2540
|
+
this.pushRaw(null, '};');
|
|
2541
|
+
}
|
|
2542
|
+
if (this.blocks.size) {
|
|
2543
|
+
this.pushRaw(null, 'const b={');
|
|
2544
|
+
for (const [name, _blocks] of this.blocks) {
|
|
2545
|
+
this.pushRaw(null, `"${name}":{`, 'u:0,', 's:[');
|
|
2546
|
+
this.state.block = name;
|
|
2547
|
+
for (const block of _blocks) {
|
|
2548
|
+
this.pushRaw(null, 'async()=>{', 'let s="";');
|
|
2549
|
+
await this.compileNodes(block.body);
|
|
2550
|
+
this.pushRaw(null, 'return s;', '},');
|
|
2551
|
+
}
|
|
2552
|
+
this.state.block = undefined;
|
|
2553
|
+
this.pushRaw(null, '],', '},');
|
|
2517
2554
|
}
|
|
2518
|
-
this.pushRaw(null,
|
|
2555
|
+
this.pushRaw(null, '};');
|
|
2519
2556
|
}
|
|
2520
2557
|
}
|
|
2521
|
-
await this.compileNode(this.
|
|
2558
|
+
await this.compileNode(this.rootNode);
|
|
2522
2559
|
this.end();
|
|
2523
2560
|
return this;
|
|
2524
2561
|
}
|
|
2525
2562
|
async parseTemplates() {
|
|
2526
|
-
const
|
|
2527
|
-
const
|
|
2528
|
-
|
|
2529
|
-
|
|
2530
|
-
|
|
2531
|
-
|
|
2532
|
-
}
|
|
2533
|
-
const { includes, blocks } = this.collectIncludesAndBlocks();
|
|
2534
|
-
for (const [path, node] of includes) {
|
|
2535
|
-
if (!astMap.has(path)) {
|
|
2536
|
-
try {
|
|
2537
|
-
queue.push({
|
|
2538
|
-
path,
|
|
2539
|
-
node,
|
|
2540
|
-
template: await this.options.loader(path),
|
|
2541
|
-
});
|
|
2542
|
-
}
|
|
2543
|
-
catch {
|
|
2544
|
-
this.options.debug?.(new CompileError(`Failed to load template from "${path}"`, this.template, node.loc));
|
|
2545
|
-
return;
|
|
2546
|
-
}
|
|
2563
|
+
const { partials, blocks } = this.collectPartialsAndBlocks();
|
|
2564
|
+
for (const [path, node] of partials) {
|
|
2565
|
+
if (!this.partials.has(path)) {
|
|
2566
|
+
let template;
|
|
2567
|
+
try {
|
|
2568
|
+
template = await this.options.loader(path);
|
|
2547
2569
|
}
|
|
2548
|
-
|
|
2549
|
-
|
|
2550
|
-
|
|
2551
|
-
|
|
2552
|
-
|
|
2570
|
+
catch {
|
|
2571
|
+
this.options.debug?.(new CompileError(`Failed to load template from "${path}"`, this.template, node.loc));
|
|
2572
|
+
return;
|
|
2573
|
+
}
|
|
2574
|
+
if (template) {
|
|
2575
|
+
await this.compilePartial(path, node, template);
|
|
2553
2576
|
}
|
|
2554
|
-
this.blocks.get(value).push(block);
|
|
2555
2577
|
}
|
|
2556
2578
|
}
|
|
2557
|
-
|
|
2558
|
-
|
|
2559
|
-
if (!this.
|
|
2560
|
-
|
|
2579
|
+
for (const block of blocks) {
|
|
2580
|
+
const { val: { value }, } = block;
|
|
2581
|
+
if (!this.blocks.has(value)) {
|
|
2582
|
+
this.blocks.set(value, []);
|
|
2561
2583
|
}
|
|
2584
|
+
this.blocks.get(value).push(block);
|
|
2562
2585
|
}
|
|
2563
2586
|
}
|
|
2564
|
-
|
|
2565
|
-
const
|
|
2587
|
+
collectPartialsAndBlocks() {
|
|
2588
|
+
const partials = new Map();
|
|
2566
2589
|
const blocks = [];
|
|
2567
2590
|
const traverse = (node) => {
|
|
2568
2591
|
if (node.type === NodeType.INCLUDE) {
|
|
2569
|
-
if (!
|
|
2570
|
-
|
|
2592
|
+
if (!partials.has(node.val.value)) {
|
|
2593
|
+
partials.set(node.val.value, node);
|
|
2571
2594
|
}
|
|
2572
2595
|
}
|
|
2573
2596
|
if (node.type === NodeType.BLOCK) {
|
|
2574
2597
|
blocks.push(node);
|
|
2575
2598
|
}
|
|
2576
|
-
|
|
2577
|
-
node.children.forEach(traverse);
|
|
2578
|
-
}
|
|
2579
|
-
if (node.type === NodeType.IF) {
|
|
2580
|
-
const ifNode = node;
|
|
2581
|
-
ifNode.body.forEach(traverse);
|
|
2582
|
-
ifNode.alternatives.forEach((alt) => {
|
|
2583
|
-
alt.body.forEach(traverse);
|
|
2584
|
-
});
|
|
2585
|
-
}
|
|
2586
|
-
if (node.type === NodeType.FOR) {
|
|
2587
|
-
node.body.forEach(traverse);
|
|
2588
|
-
}
|
|
2589
|
-
if (node.type === NodeType.BLOCK) {
|
|
2590
|
-
node.body.forEach(traverse);
|
|
2591
|
-
}
|
|
2599
|
+
node.traverse(traverse);
|
|
2592
2600
|
};
|
|
2593
|
-
traverse(this.
|
|
2601
|
+
traverse(this.rootNode);
|
|
2594
2602
|
return {
|
|
2595
|
-
|
|
2603
|
+
partials,
|
|
2596
2604
|
blocks,
|
|
2597
2605
|
};
|
|
2598
2606
|
}
|
|
2599
2607
|
async compileNode(node) {
|
|
2600
2608
|
if (node.type === NodeType.TEMPLATE) {
|
|
2601
|
-
await this.compileNodes(node.
|
|
2609
|
+
await this.compileNodes(node.body);
|
|
2602
2610
|
return;
|
|
2603
2611
|
}
|
|
2604
2612
|
if (this.options.compilers[node.type]) {
|
|
@@ -2612,13 +2620,13 @@ ${output
|
|
|
2612
2620
|
await this.compileNode(child);
|
|
2613
2621
|
}
|
|
2614
2622
|
}
|
|
2615
|
-
async compilePartial(path, node, template
|
|
2623
|
+
async compilePartial(path, node, template) {
|
|
2616
2624
|
try {
|
|
2617
|
-
const { code } = await new Compiler(this.options).compile(template, false, blocks);
|
|
2625
|
+
const { code } = await new Compiler(this.options).compile(template, false, this.partials, this.blocks);
|
|
2618
2626
|
this.partials.set(path, code);
|
|
2619
2627
|
}
|
|
2620
2628
|
catch {
|
|
2621
|
-
this.options.debug?.(new CompileError(
|
|
2629
|
+
this.options.debug?.(new CompileError('Failed to compile template', this.template, node.loc));
|
|
2622
2630
|
}
|
|
2623
2631
|
}
|
|
2624
2632
|
}
|
|
@@ -2648,7 +2656,7 @@ ${output
|
|
|
2648
2656
|
}
|
|
2649
2657
|
|
|
2650
2658
|
class Renderer {
|
|
2651
|
-
constructor({ globals, filters, parsers, compilers, ...options } = {}) {
|
|
2659
|
+
constructor({ globals, filters, parsers, compilers, plugins, ...options } = {}) {
|
|
2652
2660
|
Object.defineProperty(this, "options", {
|
|
2653
2661
|
enumerable: true,
|
|
2654
2662
|
configurable: true,
|
|
@@ -2660,6 +2668,11 @@ ${output
|
|
|
2660
2668
|
Object.assign(this.options.filters, filters);
|
|
2661
2669
|
Object.assign(this.options.parsers, parsers);
|
|
2662
2670
|
Object.assign(this.options.compilers, compilers);
|
|
2671
|
+
plugins?.forEach((plugin) => {
|
|
2672
|
+
Object.assign(this.options.filters, plugin.filters);
|
|
2673
|
+
Object.assign(this.options.parsers, plugin.parsers);
|
|
2674
|
+
Object.assign(this.options.compilers, plugin.compilers);
|
|
2675
|
+
});
|
|
2663
2676
|
}
|
|
2664
2677
|
async render(template, globals) {
|
|
2665
2678
|
const compiler = new Compiler(this.options);
|
|
@@ -2670,24 +2683,27 @@ ${output
|
|
|
2670
2683
|
return `${v}`;
|
|
2671
2684
|
}
|
|
2672
2685
|
return this.options.autoEscape ? escape(v) : v;
|
|
2673
|
-
}, { ...this.options.filters, ...
|
|
2686
|
+
}, { ...this.options.filters, ..._filters });
|
|
2674
2687
|
}
|
|
2675
2688
|
catch (error) {
|
|
2676
|
-
|
|
2677
|
-
|
|
2678
|
-
|
|
2679
|
-
throw new RenderError(error.message, template, () => {
|
|
2680
|
-
const matched = error.stack.match(/<anonymous>:(\d+):(\d+)\)/);
|
|
2681
|
-
if (!matched) {
|
|
2682
|
-
return error.stack ?? '';
|
|
2689
|
+
this.options.debug((() => {
|
|
2690
|
+
if (error.name === 'CompileError') {
|
|
2691
|
+
return error;
|
|
2683
2692
|
}
|
|
2684
|
-
|
|
2685
|
-
|
|
2686
|
-
|
|
2693
|
+
return new RenderError(error.message, template, () => {
|
|
2694
|
+
const matched = error.stack.match(/<anonymous>:(\d+):(\d+)\)/);
|
|
2695
|
+
if (!matched) {
|
|
2696
|
+
return error.stack ?? '';
|
|
2697
|
+
}
|
|
2698
|
+
const [loc] = compiler.getSourceLoc({
|
|
2699
|
+
line: +matched[1] - 2,
|
|
2700
|
+
column: +matched[2],
|
|
2701
|
+
});
|
|
2702
|
+
return loc;
|
|
2687
2703
|
});
|
|
2688
|
-
|
|
2689
|
-
});
|
|
2704
|
+
})());
|
|
2690
2705
|
}
|
|
2706
|
+
return '';
|
|
2691
2707
|
}
|
|
2692
2708
|
async renderFile(filepath, globals) {
|
|
2693
2709
|
return this.render(await this.options.loader(filepath), globals);
|