janja 2.0.0 → 2.0.1

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 CHANGED
@@ -24,13 +24,39 @@ var NodeType;
24
24
  NodeType["UNKNOWN"] = "UNKNOWN";
25
25
  NodeType["UNEXPECTED"] = "UNEXPECTED";
26
26
  })(NodeType || (NodeType = {}));
27
- class TemplateNode {
28
- constructor(children, loc) {
29
- Object.defineProperty(this, "children", {
27
+ class Traversal {
28
+ constructor() {
29
+ Object.defineProperty(this, "type", {
30
30
  enumerable: true,
31
31
  configurable: true,
32
32
  writable: true,
33
- value: children
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", {
42
+ enumerable: true,
43
+ configurable: true,
44
+ writable: true,
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,
@@ -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.isDirective(['else', 'elseif', 'elsif', 'elif'])) {
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(test, body, token.loc, token.strip);
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 {
@@ -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 (!compiler.blocks.has(value)) {
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
- const block = compiler.blocks.get(compiler.state.block)?.pop();
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,
@@ -1331,7 +1369,7 @@ const compilers$6 = {
1331
1369
  };
1332
1370
 
1333
1371
  async function compileInclude({ val: path, loc }, compiler) {
1334
- compiler.pushRaw(loc, `{`, `const partial=partials["${path.value}"];`, `if(partial){`, `s+=await partial(c,e,f);`, `}`, `}`);
1372
+ compiler.pushRaw(loc, `s+=await p["${path.value}"]?.()??"";`);
1335
1373
  }
1336
1374
  const compilers$5 = {
1337
1375
  [NodeType.INCLUDE]: compileInclude,
@@ -1365,7 +1403,6 @@ const compilers$3 = {
1365
1403
 
1366
1404
  async function compileSet({ val, loc }, compiler) {
1367
1405
  compiler.pushRaw(loc, new ExpCompiler().compile(val, compiler.context));
1368
- compiler.pushRaw(null, `});`);
1369
1406
  }
1370
1407
  const compilers$2 = {
1371
1408
  [NodeType.SET]: compileSet,
@@ -1418,7 +1455,7 @@ const compilerOptions = {
1418
1455
  const renderOptions = {
1419
1456
  ...compilerOptions,
1420
1457
  globals: {},
1421
- filters: { ...filters },
1458
+ filters,
1422
1459
  autoEscape: true,
1423
1460
  };
1424
1461
 
@@ -2356,7 +2393,7 @@ class Parser extends Tokenizer {
2356
2393
  const end = this.tokens.at(-1)?.loc.end ?? { line: 1, column: 1 };
2357
2394
  this.expParser = new ExpParser(template);
2358
2395
  this.cursor = 0;
2359
- return new TemplateNode(this.parseUntil(), { start, end });
2396
+ return new RootNode(this.parseUntil(), { start, end });
2360
2397
  }
2361
2398
  parseUntil(names) {
2362
2399
  const nodes = [];
@@ -2365,7 +2402,8 @@ class Parser extends Tokenizer {
2365
2402
  const token = this.peek();
2366
2403
  if (!token
2367
2404
  || (token.type === TokenType.DIRECTIVE
2368
- && names?.includes(token.name.toLowerCase()))) {
2405
+ && names
2406
+ && this.match(names, token))) {
2369
2407
  break;
2370
2408
  }
2371
2409
  if (prevToken?.strip?.after && token.type === TokenType.TEXT) {
@@ -2427,15 +2465,8 @@ class Parser extends Tokenizer {
2427
2465
  advance() {
2428
2466
  this.cursor++;
2429
2467
  }
2430
- match(name) {
2431
- return this.peek()?.name === name;
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());
2468
+ match(names, token = this.peek()) {
2469
+ return names.includes(token?.name);
2439
2470
  }
2440
2471
  requireExpression({ expression, name, loc }) {
2441
2472
  if (!expression) {
@@ -2464,31 +2495,31 @@ class Compiler extends Context {
2464
2495
  writable: true,
2465
2496
  value: void 0
2466
2497
  });
2467
- Object.defineProperty(this, "root", {
2498
+ Object.defineProperty(this, "state", {
2468
2499
  enumerable: true,
2469
2500
  configurable: true,
2470
2501
  writable: true,
2471
2502
  value: void 0
2472
2503
  });
2473
- Object.defineProperty(this, "partials", {
2504
+ Object.defineProperty(this, "isRoot", {
2474
2505
  enumerable: true,
2475
2506
  configurable: true,
2476
2507
  writable: true,
2477
2508
  value: void 0
2478
2509
  });
2479
- Object.defineProperty(this, "blocks", {
2510
+ Object.defineProperty(this, "rootNode", {
2480
2511
  enumerable: true,
2481
2512
  configurable: true,
2482
2513
  writable: true,
2483
2514
  value: void 0
2484
2515
  });
2485
- Object.defineProperty(this, "templateNode", {
2516
+ Object.defineProperty(this, "partials", {
2486
2517
  enumerable: true,
2487
2518
  configurable: true,
2488
2519
  writable: true,
2489
2520
  value: void 0
2490
2521
  });
2491
- Object.defineProperty(this, "state", {
2522
+ Object.defineProperty(this, "blocks", {
2492
2523
  enumerable: true,
2493
2524
  configurable: true,
2494
2525
  writable: true,
@@ -2499,105 +2530,91 @@ class Compiler extends Context {
2499
2530
  ...options,
2500
2531
  };
2501
2532
  }
2502
- async compile(template, root = true, blocks = new Map()) {
2533
+ async compile(template, root = true, partials = new Map(), blocks = new Map()) {
2503
2534
  this.template = template;
2504
- this.root = root;
2505
- this.partials = new Map();
2535
+ this.isRoot = root;
2536
+ this.partials = partials;
2506
2537
  this.blocks = blocks;
2507
- this.templateNode = new Parser(this.options).parse(template);
2538
+ this.rootNode = new Parser(this.options).parse(template);
2508
2539
  this.state = {};
2509
2540
  this.start();
2510
- if (this.root) {
2511
- await this.parseTemplates();
2512
- if (this.partials.size > 0) {
2513
- this.pushRaw(null, `const partials = {`);
2541
+ await this.parseTemplates();
2542
+ if (this.isRoot) {
2543
+ if (this.partials.size) {
2544
+ this.pushRaw(null, `const p={`);
2514
2545
  for (const [path, code] of this.partials) {
2515
- this.pushRaw(null, `"${path}":async function r(c,e,f){`, code, `},`);
2546
+ this.pushRaw(null, `"${path}":async()=>{`, code, `},`);
2547
+ }
2548
+ this.pushRaw(null, `};`);
2549
+ }
2550
+ if (this.blocks.size) {
2551
+ this.pushRaw(null, `const b={`);
2552
+ for (const [name, blocks] of this.blocks) {
2553
+ this.pushRaw(null, `"${name}":{`, `u:0,`, `s:[`);
2554
+ this.state.block = name;
2555
+ for (const block of blocks) {
2556
+ this.pushRaw(null, `async()=>{`, `let s="";`);
2557
+ await this.compileNodes(block.body);
2558
+ this.pushRaw(null, `return s;`, `},`);
2559
+ }
2560
+ this.state.block = undefined;
2561
+ this.pushRaw(null, `],`, `},`);
2516
2562
  }
2517
2563
  this.pushRaw(null, `};`);
2518
2564
  }
2519
2565
  }
2520
- await this.compileNode(this.templateNode);
2566
+ await this.compileNode(this.rootNode);
2521
2567
  this.end();
2522
2568
  return this;
2523
2569
  }
2524
2570
  async parseTemplates() {
2525
- const astMap = new Map();
2526
- const queue = [{ path: '$$', node: null, template: this.template }];
2527
- while (queue.length > 0) {
2528
- const { path, node, template } = queue.shift();
2529
- if (!astMap.has(path)) {
2530
- astMap.set(path, [node, template]);
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
- }
2571
+ const { partials, blocks } = this.collectPartialsAndBlocks();
2572
+ for (const [path, node] of partials) {
2573
+ if (!this.partials.has(path)) {
2574
+ let template;
2575
+ try {
2576
+ template = await this.options.loader(path);
2546
2577
  }
2547
- }
2548
- for (const block of blocks) {
2549
- const { val: { value } } = block;
2550
- if (!this.blocks.has(value)) {
2551
- this.blocks.set(value, []);
2578
+ catch {
2579
+ this.options.debug?.(new CompileError(`Failed to load template from "${path}"`, this.template, node.loc));
2580
+ return;
2581
+ }
2582
+ if (template) {
2583
+ await this.compilePartial(path, node, template);
2552
2584
  }
2553
- this.blocks.get(value).push(block);
2554
2585
  }
2555
2586
  }
2556
- astMap.delete('$$');
2557
- for (const [path, [node, template]] of astMap) {
2558
- if (!this.partials.has(path)) {
2559
- await this.compilePartial(path, node, template, this.blocks);
2587
+ for (const block of blocks) {
2588
+ const { val: { value } } = block;
2589
+ if (!this.blocks.has(value)) {
2590
+ this.blocks.set(value, []);
2560
2591
  }
2592
+ this.blocks.get(value).push(block);
2561
2593
  }
2562
2594
  }
2563
- collectIncludesAndBlocks() {
2564
- const includes = new Map();
2595
+ collectPartialsAndBlocks() {
2596
+ const partials = new Map();
2565
2597
  const blocks = [];
2566
2598
  const traverse = (node) => {
2567
2599
  if (node.type === NodeType.INCLUDE) {
2568
- if (!includes.has(node.val.value)) {
2569
- includes.set(node.val.value, node);
2600
+ if (!partials.has(node.val.value)) {
2601
+ partials.set(node.val.value, node);
2570
2602
  }
2571
2603
  }
2572
2604
  if (node.type === NodeType.BLOCK) {
2573
2605
  blocks.push(node);
2574
2606
  }
2575
- if (node.type === NodeType.TEMPLATE) {
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
- }
2607
+ node.traverse(traverse);
2591
2608
  };
2592
- traverse(this.templateNode);
2609
+ traverse(this.rootNode);
2593
2610
  return {
2594
- includes,
2611
+ partials,
2595
2612
  blocks,
2596
2613
  };
2597
2614
  }
2598
2615
  async compileNode(node) {
2599
2616
  if (node.type === NodeType.TEMPLATE) {
2600
- await this.compileNodes(node.children);
2617
+ await this.compileNodes(node.body);
2601
2618
  return;
2602
2619
  }
2603
2620
  if (this.options.compilers[node.type]) {
@@ -2611,9 +2628,9 @@ class Compiler extends Context {
2611
2628
  await this.compileNode(child);
2612
2629
  }
2613
2630
  }
2614
- async compilePartial(path, node, template, blocks) {
2631
+ async compilePartial(path, node, template) {
2615
2632
  try {
2616
- const { code } = await new Compiler(this.options).compile(template, false, blocks);
2633
+ const { code } = await new Compiler(this.options).compile(template, false, this.partials, this.blocks);
2617
2634
  this.partials.set(path, code);
2618
2635
  }
2619
2636
  catch {