janja 1.0.0 → 1.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.esm.js CHANGED
@@ -112,15 +112,16 @@ class Context {
112
112
  });
113
113
  }
114
114
  in() {
115
- const len = this.contexts.push(`${this.context}_${this.index++}`);
116
- return (this.context = this.contexts[len - 1]);
115
+ this.context = `${this.context}_${this.index++}`;
116
+ this.contexts.push(this.context);
117
+ return this.context;
117
118
  }
118
119
  out() {
119
120
  if (this.context === CONTEXT) {
120
121
  return;
121
122
  }
122
123
  this.contexts.pop();
123
- this.context = this.contexts[this.contexts.length - 1];
124
+ this.context = this.contexts.at(-1) ?? CONTEXT;
124
125
  }
125
126
  expect(name) {
126
127
  this.expected.push(name);
@@ -285,32 +286,32 @@ class Compiler {
285
286
  value: options
286
287
  });
287
288
  }
288
- async compile(tagToken, filepath) {
289
+ async compile(tag, filepath) {
289
290
  const ctx = new Context(this.options);
290
291
  const out = new OutScript(this.options);
291
292
  const sourcemap = new SourceMap(this.options);
292
293
  out.start();
293
- while (tagToken) {
294
- const compilers = (this.options.compilers[tagToken.name] ?? []);
294
+ while (tag) {
295
+ const compilers = (this.options.compilers[tag.name] ?? []);
295
296
  for (const compiler of compilers) {
296
297
  try {
297
298
  const r = await compiler.compile({
298
- token: tagToken,
299
+ tag,
299
300
  ctx,
300
301
  out,
301
302
  });
302
303
  if (r !== false) {
303
304
  if (r) {
304
- sourcemap.addMapping(tagToken, r);
305
+ sourcemap.addMapping(tag, r);
305
306
  }
306
307
  break;
307
308
  }
308
309
  }
309
310
  catch (error) {
310
- throw new CompileError(error.message, tagToken, filepath);
311
+ throw new CompileError(error.message, tag, filepath);
311
312
  }
312
313
  }
313
- tagToken = tagToken.next;
314
+ tag = tag.next;
314
315
  }
315
316
  try {
316
317
  ctx.validate();
@@ -523,6 +524,7 @@ var filters = /*#__PURE__*/Object.freeze({
523
524
  const expTokenOperators = {
524
525
  AND: '&&',
525
526
  OR: '||',
527
+ IS: '',
526
528
  NOT: '!',
527
529
  EQ: '===',
528
530
  NE: '!==',
@@ -545,11 +547,9 @@ const expTokenOperators = {
545
547
  COMMA: ',',
546
548
  DOT: '.',
547
549
  ID: '',
548
- STR: '',
549
- NUM: '',
550
+ LIT: '',
550
551
  IF: '',
551
552
  ELSE: '',
552
- BOOL: '',
553
553
  };
554
554
 
555
555
  class ExpCompiler {
@@ -578,18 +578,16 @@ class ExpCompiler {
578
578
  return this.compileNot(expression);
579
579
  case 'ID':
580
580
  return this.compileId(expression);
581
- case 'STR':
582
- return this.compileStr(expression);
583
- case 'NUM':
584
- return this.compileNum(expression);
585
- case 'BOOL':
586
- return this.compileBool(expression);
581
+ case 'LIT':
582
+ return this.compileLit(expression);
587
583
  case 'PIPE':
588
584
  return this.compilePipe(expression);
589
585
  case 'OF':
590
586
  return this.compileOf(expression);
591
587
  case 'SET':
592
588
  return this.compileSet(expression);
589
+ case 'IS':
590
+ return this.compileIs(expression);
593
591
  case 'AND':
594
592
  case 'OR':
595
593
  case 'EQ':
@@ -625,14 +623,8 @@ class ExpCompiler {
625
623
  }
626
624
  return s;
627
625
  }
628
- compileStr({ value }) {
629
- return JSON.stringify(value);
630
- }
631
- compileNum({ value }) {
632
- return value.toString();
633
- }
634
- compileBool({ value }) {
635
- return value.toString();
626
+ compileLit({ value }) {
627
+ return value === undefined ? 'undefined' : JSON.stringify(value);
636
628
  }
637
629
  compileSeq({ elements }) {
638
630
  return elements.map(element => this.compileExp(element)).toString();
@@ -659,6 +651,9 @@ class ExpCompiler {
659
651
  // destructuring
660
652
  return `Object.assign(${this.context},${this.filters}.pick.call(${this.context},${this.compileExp(right)},[${left.elements.map(({ value }) => `"${value}"`).join(',')}]));`;
661
653
  }
654
+ compileIs({ left, right }) {
655
+ return `(typeof ${this.compileExp(left)}===${this.compileExp(right)})`;
656
+ }
662
657
  compileBinary({ type, left, right }) {
663
658
  const ret = `(${this.compileExp(left)}${expTokenOperators[type]}${this.compileExp(right)})`;
664
659
  return type === 'NI' ? `(!${ret})` : ret;
@@ -677,6 +672,7 @@ const expTokenPrecedences = {
677
672
  RP: 20,
678
673
  OR: 25,
679
674
  AND: 30,
675
+ IS: 40,
680
676
  EQ: 40,
681
677
  NE: 40,
682
678
  GT: 40,
@@ -695,15 +691,14 @@ const expTokenPrecedences = {
695
691
  PIPE: 80,
696
692
  DOT: 90,
697
693
  ID: 90,
698
- STR: 90,
699
- NUM: 90,
700
- BOOL: 90,
694
+ LIT: 90,
701
695
  };
702
696
 
703
697
  const expTokenTypes = {
704
698
  'and': 'AND',
705
699
  'or': 'OR',
706
700
  'not': 'NOT',
701
+ 'is': 'IS',
707
702
  'eq': 'EQ',
708
703
  'ne': 'NE',
709
704
  'gt': 'GT',
@@ -720,8 +715,10 @@ const expTokenTypes = {
720
715
  '%': 'MOD',
721
716
  'if': 'IF',
722
717
  'else': 'ELSE',
723
- 'true': 'BOOL',
724
- 'false': 'BOOL',
718
+ 'true': 'LIT',
719
+ 'false': 'LIT',
720
+ 'null': 'LIT',
721
+ 'undefined': 'LIT',
725
722
  '|': 'PIPE',
726
723
  '=': 'SET',
727
724
  '(': 'LP',
@@ -835,7 +832,7 @@ class ExpTokenizer {
835
832
  // skip closing quote char
836
833
  this.cursor++;
837
834
  this.tokens.push({
838
- type: 'STR',
835
+ type: 'LIT',
839
836
  value,
840
837
  start,
841
838
  end: this.cursor,
@@ -861,7 +858,7 @@ class ExpTokenizer {
861
858
  this.cursor++;
862
859
  }
863
860
  this.tokens.push({
864
- type: 'NUM',
861
+ type: 'LIT',
865
862
  value: Number.parseFloat(value),
866
863
  start,
867
864
  end: this.cursor,
@@ -883,7 +880,17 @@ class ExpTokenizer {
883
880
  const type = value in expTokenTypes ? expTokenTypes[value] : 'ID';
884
881
  this.tokens.push({
885
882
  type,
886
- value: type === 'BOOL' ? value === 'true' : value,
883
+ value: type === 'LIT'
884
+ ? value === 'true'
885
+ ? true
886
+ : value === 'false'
887
+ ? false
888
+ : value === 'null'
889
+ ? null
890
+ : value === 'undefined'
891
+ ? undefined
892
+ : value
893
+ : value,
887
894
  start,
888
895
  end: this.cursor,
889
896
  raw: value,
@@ -1037,27 +1044,10 @@ class ExpParser {
1037
1044
  }
1038
1045
  continue;
1039
1046
  }
1040
- if (token.type === 'STR') {
1041
- left = {
1042
- ...token,
1043
- type: 'STR',
1044
- value: token.value,
1045
- };
1046
- continue;
1047
- }
1048
- if (token.type === 'NUM') {
1049
- left = {
1050
- ...token,
1051
- type: 'NUM',
1052
- value: token.value,
1053
- };
1054
- continue;
1055
- }
1056
- if (token.type === 'BOOL') {
1047
+ if (token.type === 'LIT') {
1057
1048
  left = {
1058
1049
  ...token,
1059
- type: 'BOOL',
1060
- value: token.value,
1050
+ type: 'LIT',
1061
1051
  };
1062
1052
  continue;
1063
1053
  }
@@ -1095,6 +1085,7 @@ class ExpParser {
1095
1085
  }
1096
1086
  if (token.type === 'AND'
1097
1087
  || token.type === 'OR'
1088
+ || token.type === 'IS'
1098
1089
  || token.type === 'EQ'
1099
1090
  || token.type === 'NE'
1100
1091
  || token.type === 'GT'
@@ -1273,7 +1264,7 @@ const ENDCALL = 'endcall';
1273
1264
  */
1274
1265
  const tag$7 = {
1275
1266
  names: [CALL, ENDCALL],
1276
- async compile({ token: { name, value }, ctx, out }) {
1267
+ async compile({ tag: { name, value }, ctx, out }) {
1277
1268
  if (name === CALL) {
1278
1269
  if (value?.type !== 'ID') {
1279
1270
  throw new Error(`"${CALL}" tag must specify a "macro" name`);
@@ -1299,7 +1290,7 @@ const ENDCOMMENT = 'endcomment';
1299
1290
  */
1300
1291
  const tag$6 = {
1301
1292
  names: [COMMENT, '#', ENDCOMMENT],
1302
- async compile({ token: { name, value }, ctx, out }) {
1293
+ async compile({ tag: { name, value }, ctx, out }) {
1303
1294
  // inline comment
1304
1295
  if (name === '#') {
1305
1296
  if (!value.value || out.options.stripComments) {
@@ -1328,7 +1319,7 @@ const EXPRESSION = '=';
1328
1319
  */
1329
1320
  const tag$5 = {
1330
1321
  names: [EXPRESSION],
1331
- compile({ token: { value }, ctx: { context }, out }) {
1322
+ compile({ tag: { value }, ctx: { context }, out }) {
1332
1323
  return out.pushVar(compiler.compile(value, context, FILTERS));
1333
1324
  },
1334
1325
  };
@@ -1343,15 +1334,16 @@ const ENDFOR = 'endfor';
1343
1334
  */
1344
1335
  const tag$4 = {
1345
1336
  names: [FOR, BREAK, CONTINUE, ENDFOR],
1346
- async compile({ token: { name, value }, ctx, out }) {
1337
+ async compile({ tag: { name, value }, ctx, out }) {
1347
1338
  if (name === FOR) {
1348
1339
  if (!value) {
1349
1340
  throw new Error(`"${FOR}" tag must have expression`);
1350
1341
  }
1351
1342
  ctx.expect(ENDFOR);
1352
1343
  const { context } = ctx;
1353
- if (value.left.type === 'SEQ') {
1354
- return out.pushLine(`{`, `for(${compiler.compile(value, context, FILTERS)}){`, `const ${ctx.in()}={`, `...${context},`, `${value.left.elements.map(({ value }) => value).join(',')}`, `};`);
1344
+ const { type, elements } = value.left;
1345
+ if (type === 'SEQ') {
1346
+ return out.pushLine(`{`, `for(${compiler.compile(value, context, FILTERS)}){`, `const ${ctx.in()}={`, `...${context},`, elements.map(({ value }) => value).join(','), `};`);
1355
1347
  }
1356
1348
  return out.pushLine(`{`, `for(${compiler.compile(value, context, FILTERS)}){`, `const ${ctx.in()}={`, `...${context},`, `${value.left.value},`, `};`);
1357
1349
  }
@@ -1386,7 +1378,7 @@ const ENDIF = 'endif';
1386
1378
  */
1387
1379
  const tag$3 = {
1388
1380
  names: [IF, ELIF, ELSE, ENDIF],
1389
- async compile({ token: { name, value }, ctx, out }) {
1381
+ async compile({ tag: { name, value }, ctx, out }) {
1390
1382
  if (name === IF) {
1391
1383
  if (!value) {
1392
1384
  throw new Error(`"${IF}" tag must have expression`);
@@ -1423,25 +1415,29 @@ const tag$3 = {
1423
1415
  const MACRO = 'macro';
1424
1416
  const CALLER = 'caller';
1425
1417
  const ENDMACRO = 'endmacro';
1418
+ const CALLER_PN = '_c';
1426
1419
  /**
1427
1420
  * @example {{ macro my_macro = (x, y) }}...{{ caller }}{{ endmacro }}{{ my_macro("foo", 1) }}
1428
1421
  */
1429
1422
  const tag$2 = {
1430
1423
  names: [MACRO, CALLER, ENDMACRO],
1431
- async compile({ token: { name, value }, ctx, out }) {
1424
+ async compile({ tag: { name, value }, ctx, out }) {
1432
1425
  if (name === MACRO) {
1433
1426
  if (value?.type !== 'SET') {
1434
1427
  throw new Error(`"${MACRO}" tag must have "SET" expression`);
1435
1428
  }
1436
1429
  ctx.expect(ENDMACRO);
1437
1430
  const { context } = ctx;
1438
- return out.pushLine(`${compiler.compile(value, context, FILTERS)}=>async(_c)=>{`, `const ${ctx.in()}={`, `...${context},`, `${(value.right.elements).map(el => el.type === 'SET' ? `${el.left.value}` : el.value).join(',')}`, `};`);
1431
+ const { elements } = value.right;
1432
+ return out.pushLine(`${compiler.compile(value, context, FILTERS)}=>async(${CALLER_PN})=>{`, `const ${ctx.in()}={`, `...${context},`, elements.map(el => el.type === 'SET'
1433
+ ? el.left.value
1434
+ : el.value).join(','), `};`);
1439
1435
  }
1440
1436
  if (name === CALLER) {
1441
1437
  if (!ctx.matchAny(ENDMACRO)) {
1442
1438
  throw new Error(`"${CALLER}" tag must inside a "${MACRO}" tag`);
1443
1439
  }
1444
- return out.pushLine(`await _c?.();`);
1440
+ return out.pushLine(`await ${CALLER_PN}?.();`);
1445
1441
  }
1446
1442
  if (name === ENDMACRO) {
1447
1443
  if (!ctx.consume(ENDMACRO)) {
@@ -1459,7 +1455,7 @@ const RAW = 'raw';
1459
1455
  */
1460
1456
  const tag$1 = {
1461
1457
  names: [RAW],
1462
- async compile({ token: { raw, previous, next }, out }) {
1458
+ async compile({ tag: { raw, previous, next }, out }) {
1463
1459
  return out.pushStr(raw, {
1464
1460
  trimStart: previous?.stripAfter,
1465
1461
  trimEnd: next?.stripBefore,
@@ -1475,7 +1471,7 @@ const ENDSET = 'endset';
1475
1471
  */
1476
1472
  const tag = {
1477
1473
  names: [SET, ENDSET],
1478
- async compile({ token: { name, value }, ctx, out }) {
1474
+ async compile({ tag: { name, value }, ctx, out }) {
1479
1475
  if (name === SET) {
1480
1476
  if (!value) {
1481
1477
  throw new Error(`"${SET}" tag must have expression`);
@@ -1583,30 +1579,29 @@ class Parser {
1583
1579
  let match;
1584
1580
  while ((match = tagRe.exec(template))) {
1585
1581
  if (match.index > this.index) {
1586
- this.strToken(this.index, match.index);
1582
+ this.addRaw(this.index, match.index);
1587
1583
  }
1588
- await this.tagToken(match);
1584
+ await this.addTag(match);
1589
1585
  this.index = match.index + match[0].length;
1590
1586
  }
1591
1587
  if (this.index < template.length) {
1592
- this.strToken(this.index, template.length);
1588
+ this.addRaw(this.index, template.length);
1593
1589
  }
1594
1590
  this.cursor = this.first;
1595
1591
  this.rebuildBlocks();
1596
1592
  return this.cursor;
1597
1593
  }
1598
- strToken(start, end) {
1599
- const raw = this.template.slice(start, end);
1594
+ addRaw(start, end) {
1600
1595
  this.push({
1601
1596
  name: RAW$1,
1602
- raw,
1597
+ raw: this.template.slice(start, end),
1603
1598
  previous: null,
1604
1599
  next: null,
1605
1600
  start,
1606
1601
  end,
1607
1602
  });
1608
1603
  }
1609
- async tagToken(match) {
1604
+ async addTag(match) {
1610
1605
  const start = match.index;
1611
1606
  const end = start + match[0].length;
1612
1607
  const base = {
@@ -1623,7 +1618,7 @@ class Parser {
1623
1618
  ...base,
1624
1619
  name: match[2],
1625
1620
  value: {
1626
- type: 'STR',
1621
+ type: 'LIT',
1627
1622
  value: unescapeTag(match[3]),
1628
1623
  },
1629
1624
  });
@@ -1654,7 +1649,7 @@ class Parser {
1654
1649
  }
1655
1650
  async layout(value) {
1656
1651
  const exp = parser.parse(value);
1657
- if (exp?.type !== 'STR' || !exp.value) {
1652
+ if (exp?.type !== 'LIT' || typeof exp.value !== 'string' || !exp.value) {
1658
1653
  throw new ParseError(`"layout" tag must have a file path`, {
1659
1654
  source: this.template,
1660
1655
  range: {
@@ -1667,7 +1662,7 @@ class Parser {
1667
1662
  }
1668
1663
  async include(value) {
1669
1664
  const exp = parser.parse(value.replace(/\?$/, ''));
1670
- if (exp?.type !== 'STR' || !exp.value) {
1665
+ if (exp?.type !== 'LIT' || typeof exp.value !== 'string' || !exp.value) {
1671
1666
  throw new ParseError(`"include" tag must have a file path`, {
1672
1667
  source: this.template,
1673
1668
  range: {
@@ -1698,9 +1693,9 @@ class Parser {
1698
1693
  cursor = cursor.next;
1699
1694
  }
1700
1695
  }
1701
- push(token) {
1702
- if (token.name === BLOCK) {
1703
- const { type, value } = token.value;
1696
+ push(tag) {
1697
+ if (tag.name === BLOCK) {
1698
+ const { type, value } = tag.value;
1704
1699
  if (type !== 'ID') {
1705
1700
  throw new ParseError(`"${BLOCK}" tag must have a title`, {
1706
1701
  source: this.template,
@@ -1713,31 +1708,31 @@ class Parser {
1713
1708
  this.group = value;
1714
1709
  // child blocks
1715
1710
  if (this.blocks[value]) {
1716
- this.blocks[value].push([token]);
1711
+ this.blocks[value].push([tag]);
1717
1712
  return;
1718
1713
  }
1719
1714
  // parent block
1720
- this.blocks[value] = [[token]];
1715
+ this.blocks[value] = [[tag]];
1721
1716
  }
1722
1717
  else if (this.group) {
1723
1718
  const chunks = this.blocks[this.group];
1724
- if (token.name === ENDBLOCK) {
1719
+ if (tag.name === ENDBLOCK) {
1725
1720
  this.group = '';
1726
1721
  }
1727
- chunks.at(-1).push(token);
1722
+ chunks.at(-1).push(tag);
1728
1723
  // child blocks
1729
1724
  if (chunks.length > 1) {
1730
1725
  return;
1731
1726
  }
1732
1727
  }
1733
1728
  if (this.cursor) {
1734
- token.previous = this.cursor;
1735
- this.cursor.next = token;
1729
+ tag.previous = this.cursor;
1730
+ this.cursor.next = tag;
1736
1731
  }
1737
1732
  else {
1738
- this.first = token;
1733
+ this.first = tag;
1739
1734
  }
1740
- this.cursor = token;
1735
+ this.cursor = tag;
1741
1736
  }
1742
1737
  /**
1743
1738
  * @note Block tags are hoisted to top level
@@ -1775,29 +1770,29 @@ class Parser {
1775
1770
  return [];
1776
1771
  }
1777
1772
  const chunk = [];
1778
- child.forEach((token) => {
1779
- if (token.name === SUPER) {
1773
+ child.forEach((tag) => {
1774
+ if (tag.name === SUPER) {
1780
1775
  this.rebuildBlockBody(parents.pop(), parents).forEach(t => chunk.push(t));
1781
1776
  }
1782
1777
  else {
1783
- chunk.push(token);
1778
+ chunk.push(tag);
1784
1779
  }
1785
1780
  });
1786
1781
  return chunk;
1787
1782
  }
1788
- rebuildBlockChain(tokens, previous, next) {
1789
- for (let i = 0; i < tokens.length; i++) {
1790
- tokens[i].previous = tokens[i - 1] || previous;
1791
- tokens[i].next = tokens[i + 1] || next;
1783
+ rebuildBlockChain(tags, previous, next) {
1784
+ for (let i = 0; i < tags.length; i++) {
1785
+ tags[i].previous = tags[i - 1] || previous;
1786
+ tags[i].next = tags[i + 1] || next;
1792
1787
  }
1793
1788
  if (previous) {
1794
- [previous.next] = tokens;
1789
+ [previous.next] = tags;
1795
1790
  }
1796
1791
  if (next) {
1797
- next.previous = tokens[tokens.length - 1];
1792
+ next.previous = tags[tags.length - 1];
1798
1793
  }
1799
1794
  if (!this.first) {
1800
- [this.first] = tokens;
1795
+ [this.first] = tags;
1801
1796
  this.cursor = this.first;
1802
1797
  }
1803
1798
  }