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.esm.js
CHANGED
|
@@ -22,13 +22,39 @@ var NodeType;
|
|
|
22
22
|
NodeType["UNKNOWN"] = "UNKNOWN";
|
|
23
23
|
NodeType["UNEXPECTED"] = "UNEXPECTED";
|
|
24
24
|
})(NodeType || (NodeType = {}));
|
|
25
|
-
class
|
|
26
|
-
constructor(
|
|
27
|
-
Object.defineProperty(this, "
|
|
25
|
+
class Traversal {
|
|
26
|
+
constructor() {
|
|
27
|
+
Object.defineProperty(this, "type", {
|
|
28
|
+
enumerable: true,
|
|
29
|
+
configurable: true,
|
|
30
|
+
writable: true,
|
|
31
|
+
value: null
|
|
32
|
+
});
|
|
33
|
+
Object.defineProperty(this, "loc", {
|
|
34
|
+
enumerable: true,
|
|
35
|
+
configurable: true,
|
|
36
|
+
writable: true,
|
|
37
|
+
value: null
|
|
38
|
+
});
|
|
39
|
+
Object.defineProperty(this, "body", {
|
|
28
40
|
enumerable: true,
|
|
29
41
|
configurable: true,
|
|
30
42
|
writable: true,
|
|
31
|
-
value:
|
|
43
|
+
value: void 0
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
traverse(cb) {
|
|
47
|
+
this.body?.forEach(cb);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
class RootNode extends Traversal {
|
|
51
|
+
constructor(body, loc) {
|
|
52
|
+
super();
|
|
53
|
+
Object.defineProperty(this, "body", {
|
|
54
|
+
enumerable: true,
|
|
55
|
+
configurable: true,
|
|
56
|
+
writable: true,
|
|
57
|
+
value: body
|
|
32
58
|
});
|
|
33
59
|
Object.defineProperty(this, "loc", {
|
|
34
60
|
enumerable: true,
|
|
@@ -44,8 +70,9 @@ class TemplateNode {
|
|
|
44
70
|
});
|
|
45
71
|
}
|
|
46
72
|
}
|
|
47
|
-
class OutputNode {
|
|
73
|
+
class OutputNode extends Traversal {
|
|
48
74
|
constructor(val, loc, strip, exp) {
|
|
75
|
+
super();
|
|
49
76
|
Object.defineProperty(this, "val", {
|
|
50
77
|
enumerable: true,
|
|
51
78
|
configurable: true,
|
|
@@ -78,8 +105,9 @@ class OutputNode {
|
|
|
78
105
|
});
|
|
79
106
|
}
|
|
80
107
|
}
|
|
81
|
-
class TextNode {
|
|
108
|
+
class TextNode extends Traversal {
|
|
82
109
|
constructor(val, loc, strip) {
|
|
110
|
+
super();
|
|
83
111
|
Object.defineProperty(this, "val", {
|
|
84
112
|
enumerable: true,
|
|
85
113
|
configurable: true,
|
|
@@ -106,8 +134,9 @@ class TextNode {
|
|
|
106
134
|
});
|
|
107
135
|
}
|
|
108
136
|
}
|
|
109
|
-
class IfNode {
|
|
137
|
+
class IfNode extends Traversal {
|
|
110
138
|
constructor(test, body, alternatives, loc, strip) {
|
|
139
|
+
super();
|
|
111
140
|
Object.defineProperty(this, "test", {
|
|
112
141
|
enumerable: true,
|
|
113
142
|
configurable: true,
|
|
@@ -145,9 +174,14 @@ class IfNode {
|
|
|
145
174
|
value: NodeType.IF
|
|
146
175
|
});
|
|
147
176
|
}
|
|
177
|
+
traverse(cb) {
|
|
178
|
+
this.body?.forEach(cb);
|
|
179
|
+
this.alternatives?.forEach((node) => node.body?.forEach(cb));
|
|
180
|
+
}
|
|
148
181
|
}
|
|
149
|
-
class ElseIfNode {
|
|
182
|
+
class ElseIfNode extends Traversal {
|
|
150
183
|
constructor(test, body, loc, strip) {
|
|
184
|
+
super();
|
|
151
185
|
Object.defineProperty(this, "test", {
|
|
152
186
|
enumerable: true,
|
|
153
187
|
configurable: true,
|
|
@@ -180,8 +214,9 @@ class ElseIfNode {
|
|
|
180
214
|
});
|
|
181
215
|
}
|
|
182
216
|
}
|
|
183
|
-
class ElseNode {
|
|
217
|
+
class ElseNode extends Traversal {
|
|
184
218
|
constructor(body, loc, strip) {
|
|
219
|
+
super();
|
|
185
220
|
Object.defineProperty(this, "body", {
|
|
186
221
|
enumerable: true,
|
|
187
222
|
configurable: true,
|
|
@@ -208,8 +243,9 @@ class ElseNode {
|
|
|
208
243
|
});
|
|
209
244
|
}
|
|
210
245
|
}
|
|
211
|
-
class ForNode {
|
|
246
|
+
class ForNode extends Traversal {
|
|
212
247
|
constructor(loop, body, loc, strip) {
|
|
248
|
+
super();
|
|
213
249
|
Object.defineProperty(this, "loop", {
|
|
214
250
|
enumerable: true,
|
|
215
251
|
configurable: true,
|
|
@@ -242,8 +278,9 @@ class ForNode {
|
|
|
242
278
|
});
|
|
243
279
|
}
|
|
244
280
|
}
|
|
245
|
-
class BreakNode {
|
|
281
|
+
class BreakNode extends Traversal {
|
|
246
282
|
constructor(val, loc, strip) {
|
|
283
|
+
super();
|
|
247
284
|
Object.defineProperty(this, "val", {
|
|
248
285
|
enumerable: true,
|
|
249
286
|
configurable: true,
|
|
@@ -270,8 +307,9 @@ class BreakNode {
|
|
|
270
307
|
});
|
|
271
308
|
}
|
|
272
309
|
}
|
|
273
|
-
class ContinueNode {
|
|
310
|
+
class ContinueNode extends Traversal {
|
|
274
311
|
constructor(val, loc, strip) {
|
|
312
|
+
super();
|
|
275
313
|
Object.defineProperty(this, "val", {
|
|
276
314
|
enumerable: true,
|
|
277
315
|
configurable: true,
|
|
@@ -298,8 +336,9 @@ class ContinueNode {
|
|
|
298
336
|
});
|
|
299
337
|
}
|
|
300
338
|
}
|
|
301
|
-
class UnknownDirectiveNode {
|
|
339
|
+
class UnknownDirectiveNode extends Traversal {
|
|
302
340
|
constructor(name, val, loc, strip) {
|
|
341
|
+
super();
|
|
303
342
|
Object.defineProperty(this, "name", {
|
|
304
343
|
enumerable: true,
|
|
305
344
|
configurable: true,
|
|
@@ -332,8 +371,9 @@ class UnknownDirectiveNode {
|
|
|
332
371
|
});
|
|
333
372
|
}
|
|
334
373
|
}
|
|
335
|
-
class UnexpectedDirectiveNode {
|
|
374
|
+
class UnexpectedDirectiveNode extends Traversal {
|
|
336
375
|
constructor(name, val, loc, strip) {
|
|
376
|
+
super();
|
|
337
377
|
Object.defineProperty(this, "name", {
|
|
338
378
|
enumerable: true,
|
|
339
379
|
configurable: true,
|
|
@@ -366,8 +406,9 @@ class UnexpectedDirectiveNode {
|
|
|
366
406
|
});
|
|
367
407
|
}
|
|
368
408
|
}
|
|
369
|
-
class CommentNode {
|
|
409
|
+
class CommentNode extends Traversal {
|
|
370
410
|
constructor(val, loc, strip) {
|
|
411
|
+
super();
|
|
371
412
|
Object.defineProperty(this, "val", {
|
|
372
413
|
enumerable: true,
|
|
373
414
|
configurable: true,
|
|
@@ -394,8 +435,9 @@ class CommentNode {
|
|
|
394
435
|
});
|
|
395
436
|
}
|
|
396
437
|
}
|
|
397
|
-
class IncludeNode {
|
|
438
|
+
class IncludeNode extends Traversal {
|
|
398
439
|
constructor(val, loc, strip) {
|
|
440
|
+
super();
|
|
399
441
|
Object.defineProperty(this, "val", {
|
|
400
442
|
enumerable: true,
|
|
401
443
|
configurable: true,
|
|
@@ -422,8 +464,9 @@ class IncludeNode {
|
|
|
422
464
|
});
|
|
423
465
|
}
|
|
424
466
|
}
|
|
425
|
-
class BlockNode {
|
|
467
|
+
class BlockNode extends Traversal {
|
|
426
468
|
constructor(val, body, loc, strip) {
|
|
469
|
+
super();
|
|
427
470
|
Object.defineProperty(this, "val", {
|
|
428
471
|
enumerable: true,
|
|
429
472
|
configurable: true,
|
|
@@ -456,8 +499,9 @@ class BlockNode {
|
|
|
456
499
|
});
|
|
457
500
|
}
|
|
458
501
|
}
|
|
459
|
-
class SuperNode {
|
|
502
|
+
class SuperNode extends Traversal {
|
|
460
503
|
constructor(val, loc, strip) {
|
|
504
|
+
super();
|
|
461
505
|
Object.defineProperty(this, "val", {
|
|
462
506
|
enumerable: true,
|
|
463
507
|
configurable: true,
|
|
@@ -484,8 +528,9 @@ class SuperNode {
|
|
|
484
528
|
});
|
|
485
529
|
}
|
|
486
530
|
}
|
|
487
|
-
class MacroNode {
|
|
531
|
+
class MacroNode extends Traversal {
|
|
488
532
|
constructor(val, body, loc, strip) {
|
|
533
|
+
super();
|
|
489
534
|
Object.defineProperty(this, "val", {
|
|
490
535
|
enumerable: true,
|
|
491
536
|
configurable: true,
|
|
@@ -518,8 +563,9 @@ class MacroNode {
|
|
|
518
563
|
});
|
|
519
564
|
}
|
|
520
565
|
}
|
|
521
|
-
class CallerNode {
|
|
566
|
+
class CallerNode extends Traversal {
|
|
522
567
|
constructor(val, loc, strip) {
|
|
568
|
+
super();
|
|
523
569
|
Object.defineProperty(this, "val", {
|
|
524
570
|
enumerable: true,
|
|
525
571
|
configurable: true,
|
|
@@ -546,8 +592,9 @@ class CallerNode {
|
|
|
546
592
|
});
|
|
547
593
|
}
|
|
548
594
|
}
|
|
549
|
-
class CallNode {
|
|
595
|
+
class CallNode extends Traversal {
|
|
550
596
|
constructor(val, body, loc, strip) {
|
|
597
|
+
super();
|
|
551
598
|
Object.defineProperty(this, "val", {
|
|
552
599
|
enumerable: true,
|
|
553
600
|
configurable: true,
|
|
@@ -580,8 +627,9 @@ class CallNode {
|
|
|
580
627
|
});
|
|
581
628
|
}
|
|
582
629
|
}
|
|
583
|
-
class SetNode {
|
|
630
|
+
class SetNode extends Traversal {
|
|
584
631
|
constructor(val, loc, strip) {
|
|
632
|
+
super();
|
|
585
633
|
Object.defineProperty(this, "val", {
|
|
586
634
|
enumerable: true,
|
|
587
635
|
configurable: true,
|
|
@@ -608,8 +656,9 @@ class SetNode {
|
|
|
608
656
|
});
|
|
609
657
|
}
|
|
610
658
|
}
|
|
611
|
-
class CaptureNode {
|
|
659
|
+
class CaptureNode extends Traversal {
|
|
612
660
|
constructor(val, body, loc, strip) {
|
|
661
|
+
super();
|
|
613
662
|
Object.defineProperty(this, "val", {
|
|
614
663
|
enumerable: true,
|
|
615
664
|
configurable: true,
|
|
@@ -643,7 +692,7 @@ class CaptureNode {
|
|
|
643
692
|
}
|
|
644
693
|
}
|
|
645
694
|
|
|
646
|
-
function highlightSource(message, source, { start: { line: l1, column: c1
|
|
695
|
+
function highlightSource(message, source, { start: { line: l1, column: c1 }, end: { line: l2, column: c2 } }) {
|
|
647
696
|
const output = [];
|
|
648
697
|
const caretLines = new Set();
|
|
649
698
|
const addCaretLine = (index) => {
|
|
@@ -718,7 +767,7 @@ function parseBlock(token, parser) {
|
|
|
718
767
|
const val = parser.parseExp(token.expression);
|
|
719
768
|
parser.advance();
|
|
720
769
|
const body = parser.parseUntil(['endblock']);
|
|
721
|
-
if (parser.match('endblock')) {
|
|
770
|
+
if (parser.match(['endblock'])) {
|
|
722
771
|
parser.advance();
|
|
723
772
|
}
|
|
724
773
|
else {
|
|
@@ -742,7 +791,7 @@ function parseCall(token, parser) {
|
|
|
742
791
|
const val = parser.parseExp(token.expression);
|
|
743
792
|
parser.advance();
|
|
744
793
|
const body = parser.parseUntil(['endcall']);
|
|
745
|
-
if (parser.match('endcall')) {
|
|
794
|
+
if (parser.match(['endcall'])) {
|
|
746
795
|
parser.advance();
|
|
747
796
|
}
|
|
748
797
|
else {
|
|
@@ -760,7 +809,7 @@ function parseCapture(token, parser) {
|
|
|
760
809
|
const val = parser.parseExp(token.expression);
|
|
761
810
|
parser.advance();
|
|
762
811
|
const body = parser.parseUntil(['endcapture']);
|
|
763
|
-
if (parser.match('endcapture')) {
|
|
812
|
+
if (parser.match(['endcapture'])) {
|
|
764
813
|
parser.advance();
|
|
765
814
|
}
|
|
766
815
|
else {
|
|
@@ -778,7 +827,7 @@ function parseFor(token, parser) {
|
|
|
778
827
|
const loop = parser.parseExp(token.expression);
|
|
779
828
|
parser.advance();
|
|
780
829
|
const body = parser.parseUntil(['endfor']);
|
|
781
|
-
if (parser.match('endfor')) {
|
|
830
|
+
if (parser.match(['endfor'])) {
|
|
782
831
|
parser.advance();
|
|
783
832
|
}
|
|
784
833
|
else {
|
|
@@ -808,13 +857,13 @@ function parseIf(token, parser) {
|
|
|
808
857
|
parser.advance();
|
|
809
858
|
const body = parser.parseUntil(['else', 'elseif', 'elsif', 'elif', 'endif']);
|
|
810
859
|
const alternatives = [];
|
|
811
|
-
while (parser.
|
|
860
|
+
while (parser.match(['else', 'elseif', 'elsif', 'elif'])) {
|
|
812
861
|
const branchToken = parser.peek();
|
|
813
862
|
alternatives.push(branchToken.name.toLowerCase() === 'else'
|
|
814
863
|
? parseElse(branchToken, parser)
|
|
815
864
|
: parseElseIf(branchToken, parser));
|
|
816
865
|
}
|
|
817
|
-
if (parser.match('endif')) {
|
|
866
|
+
if (parser.match(['endif'])) {
|
|
818
867
|
parser.advance();
|
|
819
868
|
}
|
|
820
869
|
else {
|
|
@@ -824,10 +873,9 @@ function parseIf(token, parser) {
|
|
|
824
873
|
}
|
|
825
874
|
function parseElseIf(token, parser) {
|
|
826
875
|
parser.requireExpression(token);
|
|
827
|
-
const test = parser.parseExp(token.expression);
|
|
828
876
|
parser.advance();
|
|
829
|
-
const body = parser.parseUntil(['else', 'elseif', 'elif', 'endif']);
|
|
830
|
-
return new ElseIfNode(
|
|
877
|
+
const body = parser.parseUntil(['else', 'elseif', 'elsif', 'elif', 'endif']);
|
|
878
|
+
return new ElseIfNode(parser.parseExp(token.expression), body, token.loc, token.strip);
|
|
831
879
|
}
|
|
832
880
|
function parseElse(token, parser) {
|
|
833
881
|
parser.requireNoExpression(token);
|
|
@@ -858,7 +906,7 @@ function parseMacro(token, parser) {
|
|
|
858
906
|
const name = parser.parseExp(token.expression);
|
|
859
907
|
parser.advance();
|
|
860
908
|
const body = parser.parseUntil(['endmacro']);
|
|
861
|
-
if (parser.match('endmacro')) {
|
|
909
|
+
if (parser.match(['endmacro'])) {
|
|
862
910
|
parser.advance();
|
|
863
911
|
}
|
|
864
912
|
else {
|
|
@@ -920,13 +968,13 @@ function add(value = 0, addend = 0) {
|
|
|
920
968
|
return value + addend;
|
|
921
969
|
}
|
|
922
970
|
function capitalize(value = '') {
|
|
923
|
-
return value.replace(/\b\w/g, match => match.toUpperCase());
|
|
971
|
+
return value.replace(/\b\w/g, (match) => match.toUpperCase());
|
|
924
972
|
}
|
|
925
973
|
function ceil(value = 0) {
|
|
926
974
|
return Math.ceil(value);
|
|
927
975
|
}
|
|
928
976
|
function compact(value = []) {
|
|
929
|
-
return value.filter(v => v != null);
|
|
977
|
+
return value.filter((v) => v != null);
|
|
930
978
|
}
|
|
931
979
|
function div(value = 0, divisor = 1) {
|
|
932
980
|
return value / divisor;
|
|
@@ -938,13 +986,13 @@ function even(value = 0) {
|
|
|
938
986
|
return +value % 2 === 0;
|
|
939
987
|
}
|
|
940
988
|
function fallback(value, defaultValue, anyFalsy = false) {
|
|
941
|
-
return anyFalsy ?
|
|
989
|
+
return anyFalsy ? value || defaultValue : (value ?? defaultValue);
|
|
942
990
|
}
|
|
943
991
|
function first(value = []) {
|
|
944
992
|
return [...value][0];
|
|
945
993
|
}
|
|
946
994
|
function get(value, ...path) {
|
|
947
|
-
return path.reduce((acc, cur) =>
|
|
995
|
+
return path.reduce((acc, cur) => acc && typeof acc === 'object' && cur in acc ? acc[cur] : undefined, value);
|
|
948
996
|
}
|
|
949
997
|
function groupby(value = [], key) {
|
|
950
998
|
return value.reduce((o, v) => {
|
|
@@ -974,13 +1022,13 @@ function lower(value = '') {
|
|
|
974
1022
|
return value.toLowerCase();
|
|
975
1023
|
}
|
|
976
1024
|
function map(value = [], key, defaultValue) {
|
|
977
|
-
return value.map(o => o[key] ?? defaultValue);
|
|
1025
|
+
return value.map((o) => o[key] ?? defaultValue);
|
|
978
1026
|
}
|
|
979
|
-
function max(value = 0, ...
|
|
980
|
-
return Math.max(value, ...
|
|
1027
|
+
function max(value = 0, ...rest) {
|
|
1028
|
+
return Math.max(value, ...rest);
|
|
981
1029
|
}
|
|
982
|
-
function min(value = 0, ...
|
|
983
|
-
return Math.min(value, ...
|
|
1030
|
+
function min(value = 0, ...rest) {
|
|
1031
|
+
return Math.min(value, ...rest);
|
|
984
1032
|
}
|
|
985
1033
|
function mul(value = 0, multiplier = 1) {
|
|
986
1034
|
return value * multiplier;
|
|
@@ -988,17 +1036,17 @@ function mul(value = 0, multiplier = 1) {
|
|
|
988
1036
|
function odd(value = 0) {
|
|
989
1037
|
return +value % 2 === 1;
|
|
990
1038
|
}
|
|
991
|
-
function omit(value = {}, ...
|
|
992
|
-
return Object.entries(value).reduce((o, [k, v]) => (
|
|
1039
|
+
function omit(value = {}, ...omitKeys) {
|
|
1040
|
+
return Object.entries(value).reduce((o, [k, v]) => (omitKeys.includes(k) ? o : { ...o, [k]: v }), {});
|
|
993
1041
|
}
|
|
994
|
-
function pick(value = {}, ...
|
|
995
|
-
return
|
|
1042
|
+
function pick(value = {}, ...pickKeys) {
|
|
1043
|
+
return pickKeys.reduce((o, k) => ({ ...o, [k]: value[k] }), {});
|
|
996
1044
|
}
|
|
997
1045
|
function repeat(value = '', count = 0) {
|
|
998
1046
|
return value.repeat(count);
|
|
999
1047
|
}
|
|
1000
|
-
function replace(value = '', search,
|
|
1001
|
-
return value.replace(new RegExp(search, 'g'),
|
|
1048
|
+
function replace(value = '', search, replacement) {
|
|
1049
|
+
return value.replace(new RegExp(search, 'g'), replacement);
|
|
1002
1050
|
}
|
|
1003
1051
|
function reverse(value = []) {
|
|
1004
1052
|
return Array.isArray(value) ? value.reverse() : [...value].reverse().join('');
|
|
@@ -1024,8 +1072,8 @@ function sum(value = []) {
|
|
|
1024
1072
|
function trim(value = '') {
|
|
1025
1073
|
return value.trim();
|
|
1026
1074
|
}
|
|
1027
|
-
function truncate(value = '',
|
|
1028
|
-
return value.slice(0,
|
|
1075
|
+
function truncate(value = '', maxLength = 0, truncateStr = '...') {
|
|
1076
|
+
return (value.slice(0, maxLength) + (value.length > maxLength ? truncateStr : ''));
|
|
1029
1077
|
}
|
|
1030
1078
|
function unique(value = '') {
|
|
1031
1079
|
return Array.isArray(value)
|
|
@@ -1045,7 +1093,7 @@ function values(value = {}) {
|
|
|
1045
1093
|
return Object.values(value);
|
|
1046
1094
|
}
|
|
1047
1095
|
|
|
1048
|
-
var
|
|
1096
|
+
var _filters = /*#__PURE__*/Object.freeze({
|
|
1049
1097
|
__proto__: null,
|
|
1050
1098
|
abs: abs,
|
|
1051
1099
|
add: add,
|
|
@@ -1091,25 +1139,15 @@ var filters = /*#__PURE__*/Object.freeze({
|
|
|
1091
1139
|
values: values
|
|
1092
1140
|
});
|
|
1093
1141
|
|
|
1094
|
-
async function compileBlock({ val: { value } }, compiler) {
|
|
1095
|
-
if
|
|
1096
|
-
return;
|
|
1097
|
-
}
|
|
1098
|
-
compiler.state.block = value;
|
|
1099
|
-
const blockNode = compiler.blocks.get(value).pop();
|
|
1100
|
-
await compiler.compileNodes(blockNode.body);
|
|
1101
|
-
compiler.blocks.delete(value);
|
|
1102
|
-
compiler.state.block = undefined;
|
|
1142
|
+
async function compileBlock({ val: { value }, loc }, compiler) {
|
|
1143
|
+
compiler.pushRaw(loc, `if(b["${value}"]&&!(b["${value}"].u++)){`, `s+=await b["${value}"].s?.pop()?.()??"";`, '}');
|
|
1103
1144
|
}
|
|
1104
1145
|
async function compileSuper({ loc }, compiler) {
|
|
1105
1146
|
if (!compiler.state.block) {
|
|
1106
1147
|
compiler.options.debug?.(new CompileError('"super" should be used inside a block', compiler.template, loc));
|
|
1107
1148
|
return;
|
|
1108
1149
|
}
|
|
1109
|
-
|
|
1110
|
-
if (block) {
|
|
1111
|
-
await compiler.compileNodes(block.body);
|
|
1112
|
-
}
|
|
1150
|
+
compiler.pushRaw(loc, `s+=await b["${compiler.state.block}"]?.s?.pop()?.()??"";`);
|
|
1113
1151
|
}
|
|
1114
1152
|
const compilers$b = {
|
|
1115
1153
|
[NodeType.BLOCK]: compileBlock,
|
|
@@ -1207,6 +1245,8 @@ class ExpCompiler {
|
|
|
1207
1245
|
return this.compileTernary(expression);
|
|
1208
1246
|
case 'SEQ':
|
|
1209
1247
|
return this.compileSeq(expression);
|
|
1248
|
+
default:
|
|
1249
|
+
return '';
|
|
1210
1250
|
}
|
|
1211
1251
|
}
|
|
1212
1252
|
compileNot({ argument }) {
|
|
@@ -1215,10 +1255,10 @@ class ExpCompiler {
|
|
|
1215
1255
|
compileId({ value, path, args }) {
|
|
1216
1256
|
let s = `${this.context}.${value}`;
|
|
1217
1257
|
if (path) {
|
|
1218
|
-
s += path.map(p => `.${p.value}`).join('');
|
|
1258
|
+
s += path.map((p) => `.${p.value}`).join('');
|
|
1219
1259
|
}
|
|
1220
1260
|
if (args) {
|
|
1221
|
-
s += `(${args.map(arg => this.compileExp(arg)).join(',')})`;
|
|
1261
|
+
s += `(${args.map((arg) => this.compileExp(arg)).join(',')})`;
|
|
1222
1262
|
}
|
|
1223
1263
|
return s;
|
|
1224
1264
|
}
|
|
@@ -1226,11 +1266,11 @@ class ExpCompiler {
|
|
|
1226
1266
|
return value === undefined ? 'undefined' : JSON.stringify(value);
|
|
1227
1267
|
}
|
|
1228
1268
|
compileSeq({ elements }) {
|
|
1229
|
-
return elements.map(element => this.compileExp(element)).toString();
|
|
1269
|
+
return elements.map((element) => this.compileExp(element)).toString();
|
|
1230
1270
|
}
|
|
1231
1271
|
compilePipe({ left, right }) {
|
|
1232
1272
|
const { value: name, args = [] } = right;
|
|
1233
|
-
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(',')}))`;
|
|
1273
|
+
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(',')}))`;
|
|
1234
1274
|
}
|
|
1235
1275
|
compileOf({ left, right }) {
|
|
1236
1276
|
// destructuring
|
|
@@ -1261,7 +1301,7 @@ class ExpCompiler {
|
|
|
1261
1301
|
async function compileCall({ val, body, loc }, compiler) {
|
|
1262
1302
|
compiler.pushRaw(loc, `await ${new ExpCompiler().compile(val, compiler.context)}(async()=>{`);
|
|
1263
1303
|
await compiler.compileNodes(body);
|
|
1264
|
-
compiler.pushRaw(null,
|
|
1304
|
+
compiler.pushRaw(null, '});');
|
|
1265
1305
|
}
|
|
1266
1306
|
const compilers$a = {
|
|
1267
1307
|
[NodeType.CALL]: compileCall,
|
|
@@ -1270,7 +1310,7 @@ const compilers$a = {
|
|
|
1270
1310
|
async function compileCapture({ val, body, loc }, compiler) {
|
|
1271
1311
|
compiler.pushRaw(loc, `${compiler.context}.${val.value}=await(async(s)=>{`);
|
|
1272
1312
|
await compiler.compileNodes(body);
|
|
1273
|
-
compiler.pushRaw(null,
|
|
1313
|
+
compiler.pushRaw(null, 'return s;})("");');
|
|
1274
1314
|
}
|
|
1275
1315
|
const compilers$9 = {
|
|
1276
1316
|
[NodeType.CAPTURE]: compileCapture,
|
|
@@ -1288,20 +1328,20 @@ const compilers$8 = {
|
|
|
1288
1328
|
async function compileFor({ loop, body }, compiler) {
|
|
1289
1329
|
const { context } = compiler;
|
|
1290
1330
|
if (loop.left.type === 'SEQ') {
|
|
1291
|
-
compiler.pushRaw(loop.loc, `for(${new ExpCompiler().compile(loop, context)}){`, `const ${compiler.in()}={`, `...${context},`, `${loop.left.elements.map(({ value }) => value).join(',')},`,
|
|
1331
|
+
compiler.pushRaw(loop.loc, `for(${new ExpCompiler().compile(loop, context)}){`, `const ${compiler.in()}={`, `...${context},`, `${loop.left.elements.map(({ value }) => value).join(',')},`, '};');
|
|
1292
1332
|
}
|
|
1293
1333
|
else {
|
|
1294
|
-
compiler.pushRaw(loop.loc, `for(${new ExpCompiler().compile(loop, context)}){`, `const ${compiler.in()}={`, `...${context},`, `${loop.left.value},`,
|
|
1334
|
+
compiler.pushRaw(loop.loc, `for(${new ExpCompiler().compile(loop, context)}){`, `const ${compiler.in()}={`, `...${context},`, `${loop.left.value},`, '};');
|
|
1295
1335
|
}
|
|
1296
1336
|
await compiler.compileNodes(body);
|
|
1297
1337
|
compiler.out();
|
|
1298
1338
|
compiler.pushRaw(null, '}');
|
|
1299
1339
|
}
|
|
1300
1340
|
function compileBreak({ loc }, compiler) {
|
|
1301
|
-
compiler.pushRaw(loc,
|
|
1341
|
+
compiler.pushRaw(loc, 'break;');
|
|
1302
1342
|
}
|
|
1303
1343
|
function compileContinue({ loc }, compiler) {
|
|
1304
|
-
compiler.pushRaw(loc,
|
|
1344
|
+
compiler.pushRaw(loc, 'continue;');
|
|
1305
1345
|
}
|
|
1306
1346
|
const compilers$7 = {
|
|
1307
1347
|
[NodeType.FOR]: compileFor,
|
|
@@ -1328,8 +1368,8 @@ const compilers$6 = {
|
|
|
1328
1368
|
[NodeType.IF]: compileIf,
|
|
1329
1369
|
};
|
|
1330
1370
|
|
|
1331
|
-
async function compileInclude({ val:
|
|
1332
|
-
compiler.pushRaw(loc, `
|
|
1371
|
+
async function compileInclude({ val: { value }, loc }, compiler) {
|
|
1372
|
+
compiler.pushRaw(loc, `s+=await p["${value}"]?.()??"";`);
|
|
1333
1373
|
}
|
|
1334
1374
|
const compilers$5 = {
|
|
1335
1375
|
[NodeType.INCLUDE]: compileInclude,
|
|
@@ -1339,15 +1379,19 @@ async function compileMacro({ val: { left, right }, loc, body }, compiler) {
|
|
|
1339
1379
|
const { context } = compiler;
|
|
1340
1380
|
const { elements } = right;
|
|
1341
1381
|
const expCompiler = new ExpCompiler();
|
|
1342
|
-
compiler.pushRaw(loc, `${expCompiler.compile(left)}=(${
|
|
1343
|
-
|
|
1344
|
-
|
|
1382
|
+
compiler.pushRaw(loc, `${expCompiler.compile(left)}=(${elements
|
|
1383
|
+
.map((el) => el.type === 'SET'
|
|
1384
|
+
? `${el.left.value}=${expCompiler.compile(el.right)}`
|
|
1385
|
+
: el.value)
|
|
1386
|
+
.join(',')})=>async(_c)=>{`, `const ${compiler.in()}={`, `...${context},`, elements
|
|
1387
|
+
.map((el) => el.type === 'SET' ? el.left.value : el.value)
|
|
1388
|
+
.join(','), '};');
|
|
1345
1389
|
await compiler.compileNodes(body);
|
|
1346
1390
|
compiler.out();
|
|
1347
1391
|
compiler.pushRaw(null, '};');
|
|
1348
1392
|
}
|
|
1349
1393
|
function compileCaller({ loc }, compiler) {
|
|
1350
|
-
compiler.pushRaw(loc,
|
|
1394
|
+
compiler.pushRaw(loc, 'await _c?.();');
|
|
1351
1395
|
}
|
|
1352
1396
|
const compilers$4 = {
|
|
1353
1397
|
[NodeType.MACRO]: compileMacro,
|
|
@@ -1363,7 +1407,6 @@ const compilers$3 = {
|
|
|
1363
1407
|
|
|
1364
1408
|
async function compileSet({ val, loc }, compiler) {
|
|
1365
1409
|
compiler.pushRaw(loc, new ExpCompiler().compile(val, compiler.context));
|
|
1366
|
-
compiler.pushRaw(null, `});`);
|
|
1367
1410
|
}
|
|
1368
1411
|
const compilers$2 = {
|
|
1369
1412
|
[NodeType.SET]: compileSet,
|
|
@@ -1416,8 +1459,9 @@ const compilerOptions = {
|
|
|
1416
1459
|
const renderOptions = {
|
|
1417
1460
|
...compilerOptions,
|
|
1418
1461
|
globals: {},
|
|
1419
|
-
filters:
|
|
1462
|
+
filters: _filters,
|
|
1420
1463
|
autoEscape: true,
|
|
1464
|
+
plugins: [],
|
|
1421
1465
|
};
|
|
1422
1466
|
|
|
1423
1467
|
/**
|
|
@@ -1470,10 +1514,10 @@ class SourceMap {
|
|
|
1470
1514
|
}
|
|
1471
1515
|
getSourceLoc({ line, column }) {
|
|
1472
1516
|
return this.mappings
|
|
1473
|
-
.filter(({ target: { start, end } }) => start.line <= line
|
|
1474
|
-
|
|
1475
|
-
|
|
1476
|
-
|
|
1517
|
+
.filter(({ target: { start, end } }) => start.line <= line &&
|
|
1518
|
+
end.line >= line &&
|
|
1519
|
+
(start.line < line || start.column <= column) &&
|
|
1520
|
+
(end.line > line || end.column >= column))
|
|
1477
1521
|
.map(({ source }) => source);
|
|
1478
1522
|
}
|
|
1479
1523
|
}
|
|
@@ -1489,6 +1533,7 @@ class OutScript extends SourceMap {
|
|
|
1489
1533
|
});
|
|
1490
1534
|
}
|
|
1491
1535
|
get script() {
|
|
1536
|
+
// eslint-disable-next-line no-new-func
|
|
1492
1537
|
return new Function(CONTEXT, ESCAPE, FILTERS, this.code);
|
|
1493
1538
|
}
|
|
1494
1539
|
start() {
|
|
@@ -1531,7 +1576,7 @@ class OutScript extends SourceMap {
|
|
|
1531
1576
|
|
|
1532
1577
|
class Context extends OutScript {
|
|
1533
1578
|
constructor() {
|
|
1534
|
-
super();
|
|
1579
|
+
super(...arguments);
|
|
1535
1580
|
Object.defineProperty(this, "context", {
|
|
1536
1581
|
enumerable: true,
|
|
1537
1582
|
configurable: true,
|
|
@@ -1561,7 +1606,7 @@ class Context extends OutScript {
|
|
|
1561
1606
|
return;
|
|
1562
1607
|
}
|
|
1563
1608
|
this.contexts.pop();
|
|
1564
|
-
this.context = this.contexts.at(-1)
|
|
1609
|
+
this.context = this.contexts.at(-1);
|
|
1565
1610
|
}
|
|
1566
1611
|
}
|
|
1567
1612
|
|
|
@@ -1597,30 +1642,30 @@ const expTokenPrecedences = {
|
|
|
1597
1642
|
};
|
|
1598
1643
|
|
|
1599
1644
|
const expTokenTypes = {
|
|
1600
|
-
|
|
1601
|
-
|
|
1602
|
-
|
|
1603
|
-
|
|
1604
|
-
|
|
1605
|
-
|
|
1606
|
-
|
|
1607
|
-
|
|
1608
|
-
|
|
1609
|
-
|
|
1610
|
-
|
|
1611
|
-
|
|
1612
|
-
|
|
1645
|
+
and: 'AND',
|
|
1646
|
+
or: 'OR',
|
|
1647
|
+
not: 'NOT',
|
|
1648
|
+
is: 'IS',
|
|
1649
|
+
eq: 'EQ',
|
|
1650
|
+
ne: 'NE',
|
|
1651
|
+
gt: 'GT',
|
|
1652
|
+
lt: 'LT',
|
|
1653
|
+
ge: 'GE',
|
|
1654
|
+
le: 'LE',
|
|
1655
|
+
in: 'IN',
|
|
1656
|
+
ni: 'NI',
|
|
1657
|
+
of: 'OF',
|
|
1613
1658
|
'+': 'ADD',
|
|
1614
1659
|
'-': 'SUB',
|
|
1615
1660
|
'*': 'MUL',
|
|
1616
1661
|
'/': 'DIV',
|
|
1617
1662
|
'%': 'MOD',
|
|
1618
|
-
|
|
1619
|
-
|
|
1620
|
-
|
|
1621
|
-
|
|
1622
|
-
|
|
1623
|
-
|
|
1663
|
+
if: 'IF',
|
|
1664
|
+
else: 'ELSE',
|
|
1665
|
+
true: 'LIT',
|
|
1666
|
+
false: 'LIT',
|
|
1667
|
+
null: 'LIT',
|
|
1668
|
+
undefined: 'LIT',
|
|
1624
1669
|
'|': 'PIPE',
|
|
1625
1670
|
'=': 'SET',
|
|
1626
1671
|
'(': 'LP',
|
|
@@ -1707,7 +1752,7 @@ class ExpTokenizer {
|
|
|
1707
1752
|
this.index++;
|
|
1708
1753
|
continue;
|
|
1709
1754
|
}
|
|
1710
|
-
if (char === '
|
|
1755
|
+
if (char === "'" || char === '"' || char === '`') {
|
|
1711
1756
|
this.readString(char);
|
|
1712
1757
|
continue;
|
|
1713
1758
|
}
|
|
@@ -1806,7 +1851,9 @@ class ExpTokenizer {
|
|
|
1806
1851
|
}
|
|
1807
1852
|
break;
|
|
1808
1853
|
}
|
|
1809
|
-
const type = value in expTokenTypes
|
|
1854
|
+
const type = value in expTokenTypes
|
|
1855
|
+
? expTokenTypes[value]
|
|
1856
|
+
: 'ID';
|
|
1810
1857
|
this.tokens.push({
|
|
1811
1858
|
type,
|
|
1812
1859
|
value: type === 'LIT'
|
|
@@ -1921,12 +1968,12 @@ class ExpParser {
|
|
|
1921
1968
|
}
|
|
1922
1969
|
if (token.type === 'LP') {
|
|
1923
1970
|
if (!this.peek()) {
|
|
1924
|
-
throw new CompileError(
|
|
1971
|
+
throw new CompileError('unexpected end of expression', this.template, token.loc);
|
|
1925
1972
|
}
|
|
1926
1973
|
const elements = this.parseSequence();
|
|
1927
1974
|
const rp = this.consume('RP');
|
|
1928
1975
|
if (!rp) {
|
|
1929
|
-
throw new CompileError(
|
|
1976
|
+
throw new CompileError('expected "RP" after "LP"', this.template, token.loc);
|
|
1930
1977
|
}
|
|
1931
1978
|
left = {
|
|
1932
1979
|
...token,
|
|
@@ -1941,12 +1988,12 @@ class ExpParser {
|
|
|
1941
1988
|
}
|
|
1942
1989
|
if (token.type === 'NOT') {
|
|
1943
1990
|
if (!this.peek()) {
|
|
1944
|
-
throw new CompileError(
|
|
1991
|
+
throw new CompileError('unexpected end of expression', this.template, token.loc);
|
|
1945
1992
|
}
|
|
1946
1993
|
left = {
|
|
1947
1994
|
...token,
|
|
1948
1995
|
type: 'NOT',
|
|
1949
|
-
argument: this.parseExp(t => isHigher(token, t) ? 'BACK' : undefined),
|
|
1996
|
+
argument: this.parseExp((t) => isHigher(token, t) ? 'BACK' : undefined),
|
|
1950
1997
|
};
|
|
1951
1998
|
continue;
|
|
1952
1999
|
}
|
|
@@ -1963,7 +2010,7 @@ class ExpParser {
|
|
|
1963
2010
|
if (lp) {
|
|
1964
2011
|
left.args = this.parseSequence();
|
|
1965
2012
|
if (!this.consume('RP')) {
|
|
1966
|
-
throw new CompileError(
|
|
2013
|
+
throw new CompileError('expected "RP" after "LP"', this.template, lp.loc);
|
|
1967
2014
|
}
|
|
1968
2015
|
continue;
|
|
1969
2016
|
}
|
|
@@ -1977,7 +2024,7 @@ class ExpParser {
|
|
|
1977
2024
|
continue;
|
|
1978
2025
|
}
|
|
1979
2026
|
if (token.type === 'IF') {
|
|
1980
|
-
const test = this.parseExp(t => t.type === 'ELSE' ? 'BACK' : isHigher(token, t) ? 'BACK' : undefined);
|
|
2027
|
+
const test = this.parseExp((t) => t.type === 'ELSE' ? 'BACK' : isHigher(token, t) ? 'BACK' : undefined);
|
|
1981
2028
|
if (!test) {
|
|
1982
2029
|
throw new CompileError('expected test expression', this.template, token.loc);
|
|
1983
2030
|
}
|
|
@@ -1988,7 +2035,7 @@ class ExpParser {
|
|
|
1988
2035
|
consequent: left,
|
|
1989
2036
|
};
|
|
1990
2037
|
if (this.consume('ELSE')) {
|
|
1991
|
-
const alternative = this.parseExp(t => isHigher(token, t) ? 'BACK' : undefined);
|
|
2038
|
+
const alternative = this.parseExp((t) => isHigher(token, t) ? 'BACK' : undefined);
|
|
1992
2039
|
if (!alternative) {
|
|
1993
2040
|
throw new CompileError('expected alternative expression', this.template, token.loc);
|
|
1994
2041
|
}
|
|
@@ -1996,28 +2043,28 @@ class ExpParser {
|
|
|
1996
2043
|
}
|
|
1997
2044
|
continue;
|
|
1998
2045
|
}
|
|
1999
|
-
if (token.type === 'AND'
|
|
2000
|
-
|
|
2001
|
-
|
|
2002
|
-
|
|
2003
|
-
|
|
2004
|
-
|
|
2005
|
-
|
|
2006
|
-
|
|
2007
|
-
|
|
2008
|
-
|
|
2009
|
-
|
|
2010
|
-
|
|
2011
|
-
|
|
2012
|
-
|
|
2013
|
-
|
|
2014
|
-
|
|
2015
|
-
|
|
2016
|
-
|
|
2046
|
+
if (token.type === 'AND' ||
|
|
2047
|
+
token.type === 'OR' ||
|
|
2048
|
+
token.type === 'IS' ||
|
|
2049
|
+
token.type === 'EQ' ||
|
|
2050
|
+
token.type === 'NE' ||
|
|
2051
|
+
token.type === 'GT' ||
|
|
2052
|
+
token.type === 'LT' ||
|
|
2053
|
+
token.type === 'GE' ||
|
|
2054
|
+
token.type === 'LE' ||
|
|
2055
|
+
token.type === 'IN' ||
|
|
2056
|
+
token.type === 'NI' ||
|
|
2057
|
+
token.type === 'OF' ||
|
|
2058
|
+
token.type === 'ADD' ||
|
|
2059
|
+
token.type === 'SUB' ||
|
|
2060
|
+
token.type === 'MUL' ||
|
|
2061
|
+
token.type === 'DIV' ||
|
|
2062
|
+
token.type === 'MOD' ||
|
|
2063
|
+
token.type === 'SET') {
|
|
2017
2064
|
if (!left) {
|
|
2018
2065
|
throw new CompileError(`no left operand for "${token.type}"`, this.template, token.loc);
|
|
2019
2066
|
}
|
|
2020
|
-
const right = this.parseExp(t => isHigher(token, t) ? 'BACK' : undefined);
|
|
2067
|
+
const right = this.parseExp((t) => isHigher(token, t) ? 'BACK' : undefined);
|
|
2021
2068
|
if (!right) {
|
|
2022
2069
|
throw new CompileError(`no right operand for "${token.type}"`, this.template, token.loc);
|
|
2023
2070
|
}
|
|
@@ -2034,7 +2081,7 @@ class ExpParser {
|
|
|
2034
2081
|
throw new CompileError(`no left operand for "${token.type}"`, this.template, token.loc);
|
|
2035
2082
|
}
|
|
2036
2083
|
if (!this.check('ID')) {
|
|
2037
|
-
throw new CompileError(
|
|
2084
|
+
throw new CompileError('expected "ID" after "PIPE"', this.template, token.loc);
|
|
2038
2085
|
}
|
|
2039
2086
|
left = {
|
|
2040
2087
|
...token,
|
|
@@ -2058,10 +2105,10 @@ class ExpParser {
|
|
|
2058
2105
|
}
|
|
2059
2106
|
const token = this.next();
|
|
2060
2107
|
if (!token) {
|
|
2061
|
-
throw new CompileError(
|
|
2108
|
+
throw new CompileError('expected "ID" after "DOT"', this.template, dot.loc);
|
|
2062
2109
|
}
|
|
2063
2110
|
if (token.type !== 'ID') {
|
|
2064
|
-
throw new CompileError(
|
|
2111
|
+
throw new CompileError('expected "ID" after "DOT"', this.template, dot.loc);
|
|
2065
2112
|
}
|
|
2066
2113
|
ids.push({
|
|
2067
2114
|
...token,
|
|
@@ -2074,7 +2121,7 @@ class ExpParser {
|
|
|
2074
2121
|
parseSequence() {
|
|
2075
2122
|
const elements = [];
|
|
2076
2123
|
while (!this.check('RP')) {
|
|
2077
|
-
const element = this.parseExp(t => t.type === 'COMMA' ? 'BREAK' : undefined);
|
|
2124
|
+
const element = this.parseExp((t) => t.type === 'COMMA' ? 'BREAK' : undefined);
|
|
2078
2125
|
if (element) {
|
|
2079
2126
|
elements.push(element);
|
|
2080
2127
|
}
|
|
@@ -2098,7 +2145,7 @@ class ExpParser {
|
|
|
2098
2145
|
right.args = args;
|
|
2099
2146
|
}
|
|
2100
2147
|
if (!this.consume('RP')) {
|
|
2101
|
-
throw new CompileError(
|
|
2148
|
+
throw new CompileError('expected "RP" after "LP"', this.template, lp.loc);
|
|
2102
2149
|
}
|
|
2103
2150
|
}
|
|
2104
2151
|
return right;
|
|
@@ -2192,23 +2239,11 @@ class Tokenizer {
|
|
|
2192
2239
|
...options,
|
|
2193
2240
|
};
|
|
2194
2241
|
const { commentOpen, commentClose, directiveOpen, directiveClose, outputOpen, outputClose, } = this.options;
|
|
2195
|
-
this.tokenPatterns =
|
|
2196
|
-
[
|
|
2197
|
-
|
|
2198
|
-
|
|
2199
|
-
|
|
2200
|
-
],
|
|
2201
|
-
[
|
|
2202
|
-
TokenType.DIRECTIVE,
|
|
2203
|
-
directiveOpen,
|
|
2204
|
-
directiveClose,
|
|
2205
|
-
],
|
|
2206
|
-
[
|
|
2207
|
-
TokenType.OUTPUT,
|
|
2208
|
-
outputOpen,
|
|
2209
|
-
outputClose,
|
|
2210
|
-
],
|
|
2211
|
-
].sort(([, open1], [, open2]) => open1.length > open2.length ? -1 : 1));
|
|
2242
|
+
this.tokenPatterns = [
|
|
2243
|
+
[TokenType.COMMENT, commentOpen, commentClose],
|
|
2244
|
+
[TokenType.DIRECTIVE, directiveOpen, directiveClose],
|
|
2245
|
+
[TokenType.OUTPUT, outputOpen, outputClose],
|
|
2246
|
+
].sort(([, open1], [, open2]) => open1.length > open2.length ? -1 : 1);
|
|
2212
2247
|
}
|
|
2213
2248
|
tokenize(template) {
|
|
2214
2249
|
this.template = template;
|
|
@@ -2277,7 +2312,9 @@ class Tokenizer {
|
|
|
2277
2312
|
: {
|
|
2278
2313
|
val: val.slice(i + 1),
|
|
2279
2314
|
loc: {
|
|
2280
|
-
start: updatePosition(val.slice(0, i + 1), updatePosition(`${openMarker}${stripBefore ? '-' : ''}`, {
|
|
2315
|
+
start: updatePosition(val.slice(0, i + 1), updatePosition(`${openMarker}${stripBefore ? '-' : ''}`, {
|
|
2316
|
+
...start,
|
|
2317
|
+
})),
|
|
2281
2318
|
end: {
|
|
2282
2319
|
line: this.line,
|
|
2283
2320
|
column: this.column - closeMarker.length - +stripAfter,
|
|
@@ -2292,41 +2329,32 @@ class Tokenizer {
|
|
|
2292
2329
|
}
|
|
2293
2330
|
consumeTextToken() {
|
|
2294
2331
|
const nextSpecialIndex = this.findNextSpecialIndex();
|
|
2295
|
-
|
|
2296
|
-
|
|
2297
|
-
|
|
2298
|
-
|
|
2299
|
-
|
|
2300
|
-
|
|
2301
|
-
|
|
2302
|
-
|
|
2303
|
-
|
|
2304
|
-
|
|
2305
|
-
|
|
2306
|
-
|
|
2307
|
-
|
|
2308
|
-
|
|
2309
|
-
|
|
2310
|
-
this.index = nextSpecialIndex;
|
|
2311
|
-
}
|
|
2312
|
-
else {
|
|
2313
|
-
updatePosition(this.template[this.index++], this);
|
|
2314
|
-
}
|
|
2332
|
+
const start = {
|
|
2333
|
+
line: this.line,
|
|
2334
|
+
column: this.column,
|
|
2335
|
+
};
|
|
2336
|
+
const val = this.template.slice(this.index, nextSpecialIndex);
|
|
2337
|
+
this.tokens.push({
|
|
2338
|
+
type: TokenType.TEXT,
|
|
2339
|
+
val: unescapeTag(val),
|
|
2340
|
+
loc: {
|
|
2341
|
+
start,
|
|
2342
|
+
end: updatePosition(val, this),
|
|
2343
|
+
},
|
|
2344
|
+
strip: {},
|
|
2345
|
+
});
|
|
2346
|
+
this.index = nextSpecialIndex;
|
|
2315
2347
|
}
|
|
2316
2348
|
lookahead(marker, offset = 0) {
|
|
2317
2349
|
return this.template.startsWith(marker, this.index + offset);
|
|
2318
2350
|
}
|
|
2319
2351
|
findNextSpecialIndex() {
|
|
2320
|
-
const { outputOpen, directiveOpen, commentOpen
|
|
2352
|
+
const { outputOpen, directiveOpen, commentOpen } = this.options;
|
|
2321
2353
|
const commentIndex = this.template.indexOf(commentOpen, this.index);
|
|
2322
2354
|
const directiveIndex = this.template.indexOf(directiveOpen, this.index);
|
|
2323
2355
|
const outputIndex = this.template.indexOf(outputOpen, this.index);
|
|
2324
|
-
const indexes = [
|
|
2325
|
-
|
|
2326
|
-
directiveIndex,
|
|
2327
|
-
outputIndex,
|
|
2328
|
-
]
|
|
2329
|
-
.filter(index => index !== -1)
|
|
2356
|
+
const indexes = [commentIndex, directiveIndex, outputIndex]
|
|
2357
|
+
.filter((index) => index !== -1)
|
|
2330
2358
|
.sort((a, b) => a - b);
|
|
2331
2359
|
return indexes.length ? indexes[0] : this.length;
|
|
2332
2360
|
}
|
|
@@ -2354,16 +2382,17 @@ class Parser extends Tokenizer {
|
|
|
2354
2382
|
const end = this.tokens.at(-1)?.loc.end ?? { line: 1, column: 1 };
|
|
2355
2383
|
this.expParser = new ExpParser(template);
|
|
2356
2384
|
this.cursor = 0;
|
|
2357
|
-
return new
|
|
2385
|
+
return new RootNode(this.parseUntil(), { start, end });
|
|
2358
2386
|
}
|
|
2359
2387
|
parseUntil(names) {
|
|
2360
2388
|
const nodes = [];
|
|
2361
2389
|
let prevToken = null;
|
|
2362
2390
|
while (true) {
|
|
2363
2391
|
const token = this.peek();
|
|
2364
|
-
if (!token
|
|
2365
|
-
|
|
2366
|
-
|
|
2392
|
+
if (!token ||
|
|
2393
|
+
(token.type === TokenType.DIRECTIVE &&
|
|
2394
|
+
names &&
|
|
2395
|
+
this.match(names, token))) {
|
|
2367
2396
|
break;
|
|
2368
2397
|
}
|
|
2369
2398
|
if (prevToken?.strip?.after && token.type === TokenType.TEXT) {
|
|
@@ -2403,8 +2432,8 @@ class Parser extends Tokenizer {
|
|
|
2403
2432
|
return new TextNode(val, loc, strip);
|
|
2404
2433
|
}
|
|
2405
2434
|
createDirective(token) {
|
|
2406
|
-
return this.options.parsers[token.name.toLowerCase()]?.(token, this)
|
|
2407
|
-
|
|
2435
|
+
return (this.options.parsers[token.name.toLowerCase()]?.(token, this) ??
|
|
2436
|
+
this.createUnknownDirective(token));
|
|
2408
2437
|
}
|
|
2409
2438
|
createUnexpectedDirective(token) {
|
|
2410
2439
|
this.options.debug?.(new CompileError(`Unexpected "${token.name}" directive`, this.template, token.loc));
|
|
@@ -2425,15 +2454,8 @@ class Parser extends Tokenizer {
|
|
|
2425
2454
|
advance() {
|
|
2426
2455
|
this.cursor++;
|
|
2427
2456
|
}
|
|
2428
|
-
match(
|
|
2429
|
-
return
|
|
2430
|
-
}
|
|
2431
|
-
isDirective(names) {
|
|
2432
|
-
const token = this.peek();
|
|
2433
|
-
if (token?.type !== TokenType.DIRECTIVE) {
|
|
2434
|
-
return false;
|
|
2435
|
-
}
|
|
2436
|
-
return names.includes(token.name.toLowerCase());
|
|
2457
|
+
match(names, token = this.peek()) {
|
|
2458
|
+
return names.includes(token?.name);
|
|
2437
2459
|
}
|
|
2438
2460
|
requireExpression({ expression, name, loc }) {
|
|
2439
2461
|
if (!expression) {
|
|
@@ -2462,31 +2484,31 @@ class Compiler extends Context {
|
|
|
2462
2484
|
writable: true,
|
|
2463
2485
|
value: void 0
|
|
2464
2486
|
});
|
|
2465
|
-
Object.defineProperty(this, "
|
|
2487
|
+
Object.defineProperty(this, "state", {
|
|
2466
2488
|
enumerable: true,
|
|
2467
2489
|
configurable: true,
|
|
2468
2490
|
writable: true,
|
|
2469
2491
|
value: void 0
|
|
2470
2492
|
});
|
|
2471
|
-
Object.defineProperty(this, "
|
|
2493
|
+
Object.defineProperty(this, "isRoot", {
|
|
2472
2494
|
enumerable: true,
|
|
2473
2495
|
configurable: true,
|
|
2474
2496
|
writable: true,
|
|
2475
2497
|
value: void 0
|
|
2476
2498
|
});
|
|
2477
|
-
Object.defineProperty(this, "
|
|
2499
|
+
Object.defineProperty(this, "rootNode", {
|
|
2478
2500
|
enumerable: true,
|
|
2479
2501
|
configurable: true,
|
|
2480
2502
|
writable: true,
|
|
2481
2503
|
value: void 0
|
|
2482
2504
|
});
|
|
2483
|
-
Object.defineProperty(this, "
|
|
2505
|
+
Object.defineProperty(this, "partials", {
|
|
2484
2506
|
enumerable: true,
|
|
2485
2507
|
configurable: true,
|
|
2486
2508
|
writable: true,
|
|
2487
2509
|
value: void 0
|
|
2488
2510
|
});
|
|
2489
|
-
Object.defineProperty(this, "
|
|
2511
|
+
Object.defineProperty(this, "blocks", {
|
|
2490
2512
|
enumerable: true,
|
|
2491
2513
|
configurable: true,
|
|
2492
2514
|
writable: true,
|
|
@@ -2497,105 +2519,91 @@ class Compiler extends Context {
|
|
|
2497
2519
|
...options,
|
|
2498
2520
|
};
|
|
2499
2521
|
}
|
|
2500
|
-
async compile(template, root = true, blocks = new Map()) {
|
|
2522
|
+
async compile(template, root = true, partials = new Map(), blocks = new Map()) {
|
|
2501
2523
|
this.template = template;
|
|
2502
|
-
this.
|
|
2503
|
-
this.partials =
|
|
2524
|
+
this.isRoot = root;
|
|
2525
|
+
this.partials = partials;
|
|
2504
2526
|
this.blocks = blocks;
|
|
2505
|
-
this.
|
|
2527
|
+
this.rootNode = new Parser(this.options).parse(template);
|
|
2506
2528
|
this.state = {};
|
|
2507
2529
|
this.start();
|
|
2508
|
-
|
|
2509
|
-
|
|
2510
|
-
if (this.partials.size
|
|
2511
|
-
this.pushRaw(null,
|
|
2530
|
+
await this.parseTemplates();
|
|
2531
|
+
if (this.isRoot) {
|
|
2532
|
+
if (this.partials.size) {
|
|
2533
|
+
this.pushRaw(null, 'const p={');
|
|
2512
2534
|
for (const [path, code] of this.partials) {
|
|
2513
|
-
this.pushRaw(null, `"${path}":async
|
|
2535
|
+
this.pushRaw(null, `"${path}":async()=>{`, code, '},');
|
|
2536
|
+
}
|
|
2537
|
+
this.pushRaw(null, '};');
|
|
2538
|
+
}
|
|
2539
|
+
if (this.blocks.size) {
|
|
2540
|
+
this.pushRaw(null, 'const b={');
|
|
2541
|
+
for (const [name, _blocks] of this.blocks) {
|
|
2542
|
+
this.pushRaw(null, `"${name}":{`, 'u:0,', 's:[');
|
|
2543
|
+
this.state.block = name;
|
|
2544
|
+
for (const block of _blocks) {
|
|
2545
|
+
this.pushRaw(null, 'async()=>{', 'let s="";');
|
|
2546
|
+
await this.compileNodes(block.body);
|
|
2547
|
+
this.pushRaw(null, 'return s;', '},');
|
|
2548
|
+
}
|
|
2549
|
+
this.state.block = undefined;
|
|
2550
|
+
this.pushRaw(null, '],', '},');
|
|
2514
2551
|
}
|
|
2515
|
-
this.pushRaw(null,
|
|
2552
|
+
this.pushRaw(null, '};');
|
|
2516
2553
|
}
|
|
2517
2554
|
}
|
|
2518
|
-
await this.compileNode(this.
|
|
2555
|
+
await this.compileNode(this.rootNode);
|
|
2519
2556
|
this.end();
|
|
2520
2557
|
return this;
|
|
2521
2558
|
}
|
|
2522
2559
|
async parseTemplates() {
|
|
2523
|
-
const
|
|
2524
|
-
const
|
|
2525
|
-
|
|
2526
|
-
|
|
2527
|
-
|
|
2528
|
-
|
|
2529
|
-
}
|
|
2530
|
-
const { includes, blocks } = this.collectIncludesAndBlocks();
|
|
2531
|
-
for (const [path, node] of includes) {
|
|
2532
|
-
if (!astMap.has(path)) {
|
|
2533
|
-
try {
|
|
2534
|
-
queue.push({
|
|
2535
|
-
path,
|
|
2536
|
-
node,
|
|
2537
|
-
template: await this.options.loader(path),
|
|
2538
|
-
});
|
|
2539
|
-
}
|
|
2540
|
-
catch {
|
|
2541
|
-
this.options.debug?.(new CompileError(`Failed to load template from "${path}"`, this.template, node.loc));
|
|
2542
|
-
return;
|
|
2543
|
-
}
|
|
2560
|
+
const { partials, blocks } = this.collectPartialsAndBlocks();
|
|
2561
|
+
for (const [path, node] of partials) {
|
|
2562
|
+
if (!this.partials.has(path)) {
|
|
2563
|
+
let template;
|
|
2564
|
+
try {
|
|
2565
|
+
template = await this.options.loader(path);
|
|
2544
2566
|
}
|
|
2545
|
-
|
|
2546
|
-
|
|
2547
|
-
|
|
2548
|
-
|
|
2549
|
-
|
|
2567
|
+
catch {
|
|
2568
|
+
this.options.debug?.(new CompileError(`Failed to load template from "${path}"`, this.template, node.loc));
|
|
2569
|
+
return;
|
|
2570
|
+
}
|
|
2571
|
+
if (template) {
|
|
2572
|
+
await this.compilePartial(path, node, template);
|
|
2550
2573
|
}
|
|
2551
|
-
this.blocks.get(value).push(block);
|
|
2552
2574
|
}
|
|
2553
2575
|
}
|
|
2554
|
-
|
|
2555
|
-
|
|
2556
|
-
if (!this.
|
|
2557
|
-
|
|
2576
|
+
for (const block of blocks) {
|
|
2577
|
+
const { val: { value }, } = block;
|
|
2578
|
+
if (!this.blocks.has(value)) {
|
|
2579
|
+
this.blocks.set(value, []);
|
|
2558
2580
|
}
|
|
2581
|
+
this.blocks.get(value).push(block);
|
|
2559
2582
|
}
|
|
2560
2583
|
}
|
|
2561
|
-
|
|
2562
|
-
const
|
|
2584
|
+
collectPartialsAndBlocks() {
|
|
2585
|
+
const partials = new Map();
|
|
2563
2586
|
const blocks = [];
|
|
2564
2587
|
const traverse = (node) => {
|
|
2565
2588
|
if (node.type === NodeType.INCLUDE) {
|
|
2566
|
-
if (!
|
|
2567
|
-
|
|
2589
|
+
if (!partials.has(node.val.value)) {
|
|
2590
|
+
partials.set(node.val.value, node);
|
|
2568
2591
|
}
|
|
2569
2592
|
}
|
|
2570
2593
|
if (node.type === NodeType.BLOCK) {
|
|
2571
2594
|
blocks.push(node);
|
|
2572
2595
|
}
|
|
2573
|
-
|
|
2574
|
-
node.children.forEach(traverse);
|
|
2575
|
-
}
|
|
2576
|
-
if (node.type === NodeType.IF) {
|
|
2577
|
-
const ifNode = node;
|
|
2578
|
-
ifNode.body.forEach(traverse);
|
|
2579
|
-
ifNode.alternatives.forEach((alt) => {
|
|
2580
|
-
alt.body.forEach(traverse);
|
|
2581
|
-
});
|
|
2582
|
-
}
|
|
2583
|
-
if (node.type === NodeType.FOR) {
|
|
2584
|
-
node.body.forEach(traverse);
|
|
2585
|
-
}
|
|
2586
|
-
if (node.type === NodeType.BLOCK) {
|
|
2587
|
-
node.body.forEach(traverse);
|
|
2588
|
-
}
|
|
2596
|
+
node.traverse(traverse);
|
|
2589
2597
|
};
|
|
2590
|
-
traverse(this.
|
|
2598
|
+
traverse(this.rootNode);
|
|
2591
2599
|
return {
|
|
2592
|
-
|
|
2600
|
+
partials,
|
|
2593
2601
|
blocks,
|
|
2594
2602
|
};
|
|
2595
2603
|
}
|
|
2596
2604
|
async compileNode(node) {
|
|
2597
2605
|
if (node.type === NodeType.TEMPLATE) {
|
|
2598
|
-
await this.compileNodes(node.
|
|
2606
|
+
await this.compileNodes(node.body);
|
|
2599
2607
|
return;
|
|
2600
2608
|
}
|
|
2601
2609
|
if (this.options.compilers[node.type]) {
|
|
@@ -2609,13 +2617,13 @@ class Compiler extends Context {
|
|
|
2609
2617
|
await this.compileNode(child);
|
|
2610
2618
|
}
|
|
2611
2619
|
}
|
|
2612
|
-
async compilePartial(path, node, template
|
|
2620
|
+
async compilePartial(path, node, template) {
|
|
2613
2621
|
try {
|
|
2614
|
-
const { code } = await new Compiler(this.options).compile(template, false, blocks);
|
|
2622
|
+
const { code } = await new Compiler(this.options).compile(template, false, this.partials, this.blocks);
|
|
2615
2623
|
this.partials.set(path, code);
|
|
2616
2624
|
}
|
|
2617
2625
|
catch {
|
|
2618
|
-
this.options.debug?.(new CompileError(
|
|
2626
|
+
this.options.debug?.(new CompileError('Failed to compile template', this.template, node.loc));
|
|
2619
2627
|
}
|
|
2620
2628
|
}
|
|
2621
2629
|
}
|
|
@@ -2645,7 +2653,7 @@ class RenderError extends CompileError {
|
|
|
2645
2653
|
}
|
|
2646
2654
|
|
|
2647
2655
|
class Renderer {
|
|
2648
|
-
constructor({ globals, filters, parsers, compilers, ...options } = {}) {
|
|
2656
|
+
constructor({ globals, filters, parsers, compilers, plugins, ...options } = {}) {
|
|
2649
2657
|
Object.defineProperty(this, "options", {
|
|
2650
2658
|
enumerable: true,
|
|
2651
2659
|
configurable: true,
|
|
@@ -2657,6 +2665,11 @@ class Renderer {
|
|
|
2657
2665
|
Object.assign(this.options.filters, filters);
|
|
2658
2666
|
Object.assign(this.options.parsers, parsers);
|
|
2659
2667
|
Object.assign(this.options.compilers, compilers);
|
|
2668
|
+
plugins?.forEach((plugin) => {
|
|
2669
|
+
Object.assign(this.options.filters, plugin.filters);
|
|
2670
|
+
Object.assign(this.options.parsers, plugin.parsers);
|
|
2671
|
+
Object.assign(this.options.compilers, plugin.compilers);
|
|
2672
|
+
});
|
|
2660
2673
|
}
|
|
2661
2674
|
async render(template, globals) {
|
|
2662
2675
|
const compiler = new Compiler(this.options);
|
|
@@ -2667,24 +2680,27 @@ class Renderer {
|
|
|
2667
2680
|
return `${v}`;
|
|
2668
2681
|
}
|
|
2669
2682
|
return this.options.autoEscape ? escape(v) : v;
|
|
2670
|
-
}, { ...this.options.filters, ...
|
|
2683
|
+
}, { ...this.options.filters, ..._filters });
|
|
2671
2684
|
}
|
|
2672
2685
|
catch (error) {
|
|
2673
|
-
|
|
2674
|
-
|
|
2675
|
-
|
|
2676
|
-
throw new RenderError(error.message, template, () => {
|
|
2677
|
-
const matched = error.stack.match(/<anonymous>:(\d+):(\d+)\)/);
|
|
2678
|
-
if (!matched) {
|
|
2679
|
-
return error.stack ?? '';
|
|
2686
|
+
this.options.debug((() => {
|
|
2687
|
+
if (error.name === 'CompileError') {
|
|
2688
|
+
return error;
|
|
2680
2689
|
}
|
|
2681
|
-
|
|
2682
|
-
|
|
2683
|
-
|
|
2690
|
+
return new RenderError(error.message, template, () => {
|
|
2691
|
+
const matched = error.stack.match(/<anonymous>:(\d+):(\d+)\)/);
|
|
2692
|
+
if (!matched) {
|
|
2693
|
+
return error.stack ?? '';
|
|
2694
|
+
}
|
|
2695
|
+
const [loc] = compiler.getSourceLoc({
|
|
2696
|
+
line: +matched[1] - 2,
|
|
2697
|
+
column: +matched[2],
|
|
2698
|
+
});
|
|
2699
|
+
return loc;
|
|
2684
2700
|
});
|
|
2685
|
-
|
|
2686
|
-
});
|
|
2701
|
+
})());
|
|
2687
2702
|
}
|
|
2703
|
+
return '';
|
|
2688
2704
|
}
|
|
2689
2705
|
async renderFile(filepath, globals) {
|
|
2690
2706
|
return this.render(await this.options.loader(filepath), globals);
|