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