janja 2.0.1 → 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 +173 -174
- package/dist/index.d.ts +13 -11
- package/dist/index.esm.js +173 -174
- package/dist/index.js +173 -174
- package/package.json +4 -3
package/dist/index.cjs.js
CHANGED
|
@@ -178,7 +178,7 @@ class IfNode extends Traversal {
|
|
|
178
178
|
}
|
|
179
179
|
traverse(cb) {
|
|
180
180
|
this.body?.forEach(cb);
|
|
181
|
-
this.alternatives?.forEach(node => node.body?.forEach(cb));
|
|
181
|
+
this.alternatives?.forEach((node) => node.body?.forEach(cb));
|
|
182
182
|
}
|
|
183
183
|
}
|
|
184
184
|
class ElseIfNode extends Traversal {
|
|
@@ -694,7 +694,7 @@ class CaptureNode extends Traversal {
|
|
|
694
694
|
}
|
|
695
695
|
}
|
|
696
696
|
|
|
697
|
-
function highlightSource(message, source, { start: { line: l1, column: c1
|
|
697
|
+
function highlightSource(message, source, { start: { line: l1, column: c1 }, end: { line: l2, column: c2 } }) {
|
|
698
698
|
const output = [];
|
|
699
699
|
const caretLines = new Set();
|
|
700
700
|
const addCaretLine = (index) => {
|
|
@@ -970,13 +970,13 @@ function add(value = 0, addend = 0) {
|
|
|
970
970
|
return value + addend;
|
|
971
971
|
}
|
|
972
972
|
function capitalize(value = '') {
|
|
973
|
-
return value.replace(/\b\w/g, match => match.toUpperCase());
|
|
973
|
+
return value.replace(/\b\w/g, (match) => match.toUpperCase());
|
|
974
974
|
}
|
|
975
975
|
function ceil(value = 0) {
|
|
976
976
|
return Math.ceil(value);
|
|
977
977
|
}
|
|
978
978
|
function compact(value = []) {
|
|
979
|
-
return value.filter(v => v != null);
|
|
979
|
+
return value.filter((v) => v != null);
|
|
980
980
|
}
|
|
981
981
|
function div(value = 0, divisor = 1) {
|
|
982
982
|
return value / divisor;
|
|
@@ -988,13 +988,13 @@ function even(value = 0) {
|
|
|
988
988
|
return +value % 2 === 0;
|
|
989
989
|
}
|
|
990
990
|
function fallback(value, defaultValue, anyFalsy = false) {
|
|
991
|
-
return anyFalsy ?
|
|
991
|
+
return anyFalsy ? value || defaultValue : (value ?? defaultValue);
|
|
992
992
|
}
|
|
993
993
|
function first(value = []) {
|
|
994
994
|
return [...value][0];
|
|
995
995
|
}
|
|
996
996
|
function get(value, ...path) {
|
|
997
|
-
return path.reduce((acc, cur) =>
|
|
997
|
+
return path.reduce((acc, cur) => acc && typeof acc === 'object' && cur in acc ? acc[cur] : undefined, value);
|
|
998
998
|
}
|
|
999
999
|
function groupby(value = [], key) {
|
|
1000
1000
|
return value.reduce((o, v) => {
|
|
@@ -1024,13 +1024,13 @@ function lower(value = '') {
|
|
|
1024
1024
|
return value.toLowerCase();
|
|
1025
1025
|
}
|
|
1026
1026
|
function map(value = [], key, defaultValue) {
|
|
1027
|
-
return value.map(o => o[key] ?? defaultValue);
|
|
1027
|
+
return value.map((o) => o[key] ?? defaultValue);
|
|
1028
1028
|
}
|
|
1029
|
-
function max(value = 0, ...
|
|
1030
|
-
return Math.max(value, ...
|
|
1029
|
+
function max(value = 0, ...rest) {
|
|
1030
|
+
return Math.max(value, ...rest);
|
|
1031
1031
|
}
|
|
1032
|
-
function min(value = 0, ...
|
|
1033
|
-
return Math.min(value, ...
|
|
1032
|
+
function min(value = 0, ...rest) {
|
|
1033
|
+
return Math.min(value, ...rest);
|
|
1034
1034
|
}
|
|
1035
1035
|
function mul(value = 0, multiplier = 1) {
|
|
1036
1036
|
return value * multiplier;
|
|
@@ -1038,17 +1038,17 @@ function mul(value = 0, multiplier = 1) {
|
|
|
1038
1038
|
function odd(value = 0) {
|
|
1039
1039
|
return +value % 2 === 1;
|
|
1040
1040
|
}
|
|
1041
|
-
function omit(value = {}, ...
|
|
1042
|
-
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 }), {});
|
|
1043
1043
|
}
|
|
1044
|
-
function pick(value = {}, ...
|
|
1045
|
-
return
|
|
1044
|
+
function pick(value = {}, ...pickKeys) {
|
|
1045
|
+
return pickKeys.reduce((o, k) => ({ ...o, [k]: value[k] }), {});
|
|
1046
1046
|
}
|
|
1047
1047
|
function repeat(value = '', count = 0) {
|
|
1048
1048
|
return value.repeat(count);
|
|
1049
1049
|
}
|
|
1050
|
-
function replace(value = '', search,
|
|
1051
|
-
return value.replace(new RegExp(search, 'g'),
|
|
1050
|
+
function replace(value = '', search, replacement) {
|
|
1051
|
+
return value.replace(new RegExp(search, 'g'), replacement);
|
|
1052
1052
|
}
|
|
1053
1053
|
function reverse(value = []) {
|
|
1054
1054
|
return Array.isArray(value) ? value.reverse() : [...value].reverse().join('');
|
|
@@ -1074,8 +1074,8 @@ function sum(value = []) {
|
|
|
1074
1074
|
function trim(value = '') {
|
|
1075
1075
|
return value.trim();
|
|
1076
1076
|
}
|
|
1077
|
-
function truncate(value = '',
|
|
1078
|
-
return value.slice(0,
|
|
1077
|
+
function truncate(value = '', maxLength = 0, truncateStr = '...') {
|
|
1078
|
+
return (value.slice(0, maxLength) + (value.length > maxLength ? truncateStr : ''));
|
|
1079
1079
|
}
|
|
1080
1080
|
function unique(value = '') {
|
|
1081
1081
|
return Array.isArray(value)
|
|
@@ -1095,7 +1095,7 @@ function values(value = {}) {
|
|
|
1095
1095
|
return Object.values(value);
|
|
1096
1096
|
}
|
|
1097
1097
|
|
|
1098
|
-
var
|
|
1098
|
+
var _filters = /*#__PURE__*/Object.freeze({
|
|
1099
1099
|
__proto__: null,
|
|
1100
1100
|
abs: abs,
|
|
1101
1101
|
add: add,
|
|
@@ -1142,7 +1142,7 @@ var filters = /*#__PURE__*/Object.freeze({
|
|
|
1142
1142
|
});
|
|
1143
1143
|
|
|
1144
1144
|
async function compileBlock({ val: { value }, loc }, compiler) {
|
|
1145
|
-
compiler.pushRaw(loc, `if(b["${value}"]&&!(b["${value}"].u++)){`, `s+=await b["${value}"].s?.pop()?.()??"";`,
|
|
1145
|
+
compiler.pushRaw(loc, `if(b["${value}"]&&!(b["${value}"].u++)){`, `s+=await b["${value}"].s?.pop()?.()??"";`, '}');
|
|
1146
1146
|
}
|
|
1147
1147
|
async function compileSuper({ loc }, compiler) {
|
|
1148
1148
|
if (!compiler.state.block) {
|
|
@@ -1247,6 +1247,8 @@ class ExpCompiler {
|
|
|
1247
1247
|
return this.compileTernary(expression);
|
|
1248
1248
|
case 'SEQ':
|
|
1249
1249
|
return this.compileSeq(expression);
|
|
1250
|
+
default:
|
|
1251
|
+
return '';
|
|
1250
1252
|
}
|
|
1251
1253
|
}
|
|
1252
1254
|
compileNot({ argument }) {
|
|
@@ -1255,10 +1257,10 @@ class ExpCompiler {
|
|
|
1255
1257
|
compileId({ value, path, args }) {
|
|
1256
1258
|
let s = `${this.context}.${value}`;
|
|
1257
1259
|
if (path) {
|
|
1258
|
-
s += path.map(p => `.${p.value}`).join('');
|
|
1260
|
+
s += path.map((p) => `.${p.value}`).join('');
|
|
1259
1261
|
}
|
|
1260
1262
|
if (args) {
|
|
1261
|
-
s += `(${args.map(arg => this.compileExp(arg)).join(',')})`;
|
|
1263
|
+
s += `(${args.map((arg) => this.compileExp(arg)).join(',')})`;
|
|
1262
1264
|
}
|
|
1263
1265
|
return s;
|
|
1264
1266
|
}
|
|
@@ -1266,11 +1268,11 @@ class ExpCompiler {
|
|
|
1266
1268
|
return value === undefined ? 'undefined' : JSON.stringify(value);
|
|
1267
1269
|
}
|
|
1268
1270
|
compileSeq({ elements }) {
|
|
1269
|
-
return elements.map(element => this.compileExp(element)).toString();
|
|
1271
|
+
return elements.map((element) => this.compileExp(element)).toString();
|
|
1270
1272
|
}
|
|
1271
1273
|
compilePipe({ left, right }) {
|
|
1272
1274
|
const { value: name, args = [] } = right;
|
|
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(',')}))`;
|
|
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(',')}))`;
|
|
1274
1276
|
}
|
|
1275
1277
|
compileOf({ left, right }) {
|
|
1276
1278
|
// destructuring
|
|
@@ -1301,7 +1303,7 @@ class ExpCompiler {
|
|
|
1301
1303
|
async function compileCall({ val, body, loc }, compiler) {
|
|
1302
1304
|
compiler.pushRaw(loc, `await ${new ExpCompiler().compile(val, compiler.context)}(async()=>{`);
|
|
1303
1305
|
await compiler.compileNodes(body);
|
|
1304
|
-
compiler.pushRaw(null,
|
|
1306
|
+
compiler.pushRaw(null, '});');
|
|
1305
1307
|
}
|
|
1306
1308
|
const compilers$a = {
|
|
1307
1309
|
[NodeType.CALL]: compileCall,
|
|
@@ -1310,7 +1312,7 @@ const compilers$a = {
|
|
|
1310
1312
|
async function compileCapture({ val, body, loc }, compiler) {
|
|
1311
1313
|
compiler.pushRaw(loc, `${compiler.context}.${val.value}=await(async(s)=>{`);
|
|
1312
1314
|
await compiler.compileNodes(body);
|
|
1313
|
-
compiler.pushRaw(null,
|
|
1315
|
+
compiler.pushRaw(null, 'return s;})("");');
|
|
1314
1316
|
}
|
|
1315
1317
|
const compilers$9 = {
|
|
1316
1318
|
[NodeType.CAPTURE]: compileCapture,
|
|
@@ -1328,20 +1330,20 @@ const compilers$8 = {
|
|
|
1328
1330
|
async function compileFor({ loop, body }, compiler) {
|
|
1329
1331
|
const { context } = compiler;
|
|
1330
1332
|
if (loop.left.type === 'SEQ') {
|
|
1331
|
-
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(',')},`, '};');
|
|
1332
1334
|
}
|
|
1333
1335
|
else {
|
|
1334
|
-
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},`, '};');
|
|
1335
1337
|
}
|
|
1336
1338
|
await compiler.compileNodes(body);
|
|
1337
1339
|
compiler.out();
|
|
1338
1340
|
compiler.pushRaw(null, '}');
|
|
1339
1341
|
}
|
|
1340
1342
|
function compileBreak({ loc }, compiler) {
|
|
1341
|
-
compiler.pushRaw(loc,
|
|
1343
|
+
compiler.pushRaw(loc, 'break;');
|
|
1342
1344
|
}
|
|
1343
1345
|
function compileContinue({ loc }, compiler) {
|
|
1344
|
-
compiler.pushRaw(loc,
|
|
1346
|
+
compiler.pushRaw(loc, 'continue;');
|
|
1345
1347
|
}
|
|
1346
1348
|
const compilers$7 = {
|
|
1347
1349
|
[NodeType.FOR]: compileFor,
|
|
@@ -1368,8 +1370,8 @@ const compilers$6 = {
|
|
|
1368
1370
|
[NodeType.IF]: compileIf,
|
|
1369
1371
|
};
|
|
1370
1372
|
|
|
1371
|
-
async function compileInclude({ val:
|
|
1372
|
-
compiler.pushRaw(loc, `s+=await p["${
|
|
1373
|
+
async function compileInclude({ val: { value }, loc }, compiler) {
|
|
1374
|
+
compiler.pushRaw(loc, `s+=await p["${value}"]?.()??"";`);
|
|
1373
1375
|
}
|
|
1374
1376
|
const compilers$5 = {
|
|
1375
1377
|
[NodeType.INCLUDE]: compileInclude,
|
|
@@ -1379,15 +1381,19 @@ async function compileMacro({ val: { left, right }, loc, body }, compiler) {
|
|
|
1379
1381
|
const { context } = compiler;
|
|
1380
1382
|
const { elements } = right;
|
|
1381
1383
|
const expCompiler = new ExpCompiler();
|
|
1382
|
-
compiler.pushRaw(loc, `${expCompiler.compile(left)}=(${
|
|
1383
|
-
|
|
1384
|
-
|
|
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(','), '};');
|
|
1385
1391
|
await compiler.compileNodes(body);
|
|
1386
1392
|
compiler.out();
|
|
1387
1393
|
compiler.pushRaw(null, '};');
|
|
1388
1394
|
}
|
|
1389
1395
|
function compileCaller({ loc }, compiler) {
|
|
1390
|
-
compiler.pushRaw(loc,
|
|
1396
|
+
compiler.pushRaw(loc, 'await _c?.();');
|
|
1391
1397
|
}
|
|
1392
1398
|
const compilers$4 = {
|
|
1393
1399
|
[NodeType.MACRO]: compileMacro,
|
|
@@ -1455,8 +1461,9 @@ const compilerOptions = {
|
|
|
1455
1461
|
const renderOptions = {
|
|
1456
1462
|
...compilerOptions,
|
|
1457
1463
|
globals: {},
|
|
1458
|
-
filters,
|
|
1464
|
+
filters: _filters,
|
|
1459
1465
|
autoEscape: true,
|
|
1466
|
+
plugins: [],
|
|
1460
1467
|
};
|
|
1461
1468
|
|
|
1462
1469
|
/**
|
|
@@ -1509,10 +1516,10 @@ class SourceMap {
|
|
|
1509
1516
|
}
|
|
1510
1517
|
getSourceLoc({ line, column }) {
|
|
1511
1518
|
return this.mappings
|
|
1512
|
-
.filter(({ target: { start, end } }) => start.line <= line
|
|
1513
|
-
|
|
1514
|
-
|
|
1515
|
-
|
|
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))
|
|
1516
1523
|
.map(({ source }) => source);
|
|
1517
1524
|
}
|
|
1518
1525
|
}
|
|
@@ -1528,6 +1535,7 @@ class OutScript extends SourceMap {
|
|
|
1528
1535
|
});
|
|
1529
1536
|
}
|
|
1530
1537
|
get script() {
|
|
1538
|
+
// eslint-disable-next-line no-new-func
|
|
1531
1539
|
return new Function(CONTEXT, ESCAPE, FILTERS, this.code);
|
|
1532
1540
|
}
|
|
1533
1541
|
start() {
|
|
@@ -1570,7 +1578,7 @@ class OutScript extends SourceMap {
|
|
|
1570
1578
|
|
|
1571
1579
|
class Context extends OutScript {
|
|
1572
1580
|
constructor() {
|
|
1573
|
-
super();
|
|
1581
|
+
super(...arguments);
|
|
1574
1582
|
Object.defineProperty(this, "context", {
|
|
1575
1583
|
enumerable: true,
|
|
1576
1584
|
configurable: true,
|
|
@@ -1600,7 +1608,7 @@ class Context extends OutScript {
|
|
|
1600
1608
|
return;
|
|
1601
1609
|
}
|
|
1602
1610
|
this.contexts.pop();
|
|
1603
|
-
this.context = this.contexts.at(-1)
|
|
1611
|
+
this.context = this.contexts.at(-1);
|
|
1604
1612
|
}
|
|
1605
1613
|
}
|
|
1606
1614
|
|
|
@@ -1636,30 +1644,30 @@ const expTokenPrecedences = {
|
|
|
1636
1644
|
};
|
|
1637
1645
|
|
|
1638
1646
|
const expTokenTypes = {
|
|
1639
|
-
|
|
1640
|
-
|
|
1641
|
-
|
|
1642
|
-
|
|
1643
|
-
|
|
1644
|
-
|
|
1645
|
-
|
|
1646
|
-
|
|
1647
|
-
|
|
1648
|
-
|
|
1649
|
-
|
|
1650
|
-
|
|
1651
|
-
|
|
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',
|
|
1652
1660
|
'+': 'ADD',
|
|
1653
1661
|
'-': 'SUB',
|
|
1654
1662
|
'*': 'MUL',
|
|
1655
1663
|
'/': 'DIV',
|
|
1656
1664
|
'%': 'MOD',
|
|
1657
|
-
|
|
1658
|
-
|
|
1659
|
-
|
|
1660
|
-
|
|
1661
|
-
|
|
1662
|
-
|
|
1665
|
+
if: 'IF',
|
|
1666
|
+
else: 'ELSE',
|
|
1667
|
+
true: 'LIT',
|
|
1668
|
+
false: 'LIT',
|
|
1669
|
+
null: 'LIT',
|
|
1670
|
+
undefined: 'LIT',
|
|
1663
1671
|
'|': 'PIPE',
|
|
1664
1672
|
'=': 'SET',
|
|
1665
1673
|
'(': 'LP',
|
|
@@ -1746,7 +1754,7 @@ class ExpTokenizer {
|
|
|
1746
1754
|
this.index++;
|
|
1747
1755
|
continue;
|
|
1748
1756
|
}
|
|
1749
|
-
if (char === '
|
|
1757
|
+
if (char === "'" || char === '"' || char === '`') {
|
|
1750
1758
|
this.readString(char);
|
|
1751
1759
|
continue;
|
|
1752
1760
|
}
|
|
@@ -1845,7 +1853,9 @@ class ExpTokenizer {
|
|
|
1845
1853
|
}
|
|
1846
1854
|
break;
|
|
1847
1855
|
}
|
|
1848
|
-
const type = value in expTokenTypes
|
|
1856
|
+
const type = value in expTokenTypes
|
|
1857
|
+
? expTokenTypes[value]
|
|
1858
|
+
: 'ID';
|
|
1849
1859
|
this.tokens.push({
|
|
1850
1860
|
type,
|
|
1851
1861
|
value: type === 'LIT'
|
|
@@ -1960,12 +1970,12 @@ class ExpParser {
|
|
|
1960
1970
|
}
|
|
1961
1971
|
if (token.type === 'LP') {
|
|
1962
1972
|
if (!this.peek()) {
|
|
1963
|
-
throw new CompileError(
|
|
1973
|
+
throw new CompileError('unexpected end of expression', this.template, token.loc);
|
|
1964
1974
|
}
|
|
1965
1975
|
const elements = this.parseSequence();
|
|
1966
1976
|
const rp = this.consume('RP');
|
|
1967
1977
|
if (!rp) {
|
|
1968
|
-
throw new CompileError(
|
|
1978
|
+
throw new CompileError('expected "RP" after "LP"', this.template, token.loc);
|
|
1969
1979
|
}
|
|
1970
1980
|
left = {
|
|
1971
1981
|
...token,
|
|
@@ -1980,12 +1990,12 @@ class ExpParser {
|
|
|
1980
1990
|
}
|
|
1981
1991
|
if (token.type === 'NOT') {
|
|
1982
1992
|
if (!this.peek()) {
|
|
1983
|
-
throw new CompileError(
|
|
1993
|
+
throw new CompileError('unexpected end of expression', this.template, token.loc);
|
|
1984
1994
|
}
|
|
1985
1995
|
left = {
|
|
1986
1996
|
...token,
|
|
1987
1997
|
type: 'NOT',
|
|
1988
|
-
argument: this.parseExp(t => isHigher(token, t) ? 'BACK' : undefined),
|
|
1998
|
+
argument: this.parseExp((t) => isHigher(token, t) ? 'BACK' : undefined),
|
|
1989
1999
|
};
|
|
1990
2000
|
continue;
|
|
1991
2001
|
}
|
|
@@ -2002,7 +2012,7 @@ class ExpParser {
|
|
|
2002
2012
|
if (lp) {
|
|
2003
2013
|
left.args = this.parseSequence();
|
|
2004
2014
|
if (!this.consume('RP')) {
|
|
2005
|
-
throw new CompileError(
|
|
2015
|
+
throw new CompileError('expected "RP" after "LP"', this.template, lp.loc);
|
|
2006
2016
|
}
|
|
2007
2017
|
continue;
|
|
2008
2018
|
}
|
|
@@ -2016,7 +2026,7 @@ class ExpParser {
|
|
|
2016
2026
|
continue;
|
|
2017
2027
|
}
|
|
2018
2028
|
if (token.type === 'IF') {
|
|
2019
|
-
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);
|
|
2020
2030
|
if (!test) {
|
|
2021
2031
|
throw new CompileError('expected test expression', this.template, token.loc);
|
|
2022
2032
|
}
|
|
@@ -2027,7 +2037,7 @@ class ExpParser {
|
|
|
2027
2037
|
consequent: left,
|
|
2028
2038
|
};
|
|
2029
2039
|
if (this.consume('ELSE')) {
|
|
2030
|
-
const alternative = this.parseExp(t => isHigher(token, t) ? 'BACK' : undefined);
|
|
2040
|
+
const alternative = this.parseExp((t) => isHigher(token, t) ? 'BACK' : undefined);
|
|
2031
2041
|
if (!alternative) {
|
|
2032
2042
|
throw new CompileError('expected alternative expression', this.template, token.loc);
|
|
2033
2043
|
}
|
|
@@ -2035,28 +2045,28 @@ class ExpParser {
|
|
|
2035
2045
|
}
|
|
2036
2046
|
continue;
|
|
2037
2047
|
}
|
|
2038
|
-
if (token.type === 'AND'
|
|
2039
|
-
|
|
2040
|
-
|
|
2041
|
-
|
|
2042
|
-
|
|
2043
|
-
|
|
2044
|
-
|
|
2045
|
-
|
|
2046
|
-
|
|
2047
|
-
|
|
2048
|
-
|
|
2049
|
-
|
|
2050
|
-
|
|
2051
|
-
|
|
2052
|
-
|
|
2053
|
-
|
|
2054
|
-
|
|
2055
|
-
|
|
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') {
|
|
2056
2066
|
if (!left) {
|
|
2057
2067
|
throw new CompileError(`no left operand for "${token.type}"`, this.template, token.loc);
|
|
2058
2068
|
}
|
|
2059
|
-
const right = this.parseExp(t => isHigher(token, t) ? 'BACK' : undefined);
|
|
2069
|
+
const right = this.parseExp((t) => isHigher(token, t) ? 'BACK' : undefined);
|
|
2060
2070
|
if (!right) {
|
|
2061
2071
|
throw new CompileError(`no right operand for "${token.type}"`, this.template, token.loc);
|
|
2062
2072
|
}
|
|
@@ -2073,7 +2083,7 @@ class ExpParser {
|
|
|
2073
2083
|
throw new CompileError(`no left operand for "${token.type}"`, this.template, token.loc);
|
|
2074
2084
|
}
|
|
2075
2085
|
if (!this.check('ID')) {
|
|
2076
|
-
throw new CompileError(
|
|
2086
|
+
throw new CompileError('expected "ID" after "PIPE"', this.template, token.loc);
|
|
2077
2087
|
}
|
|
2078
2088
|
left = {
|
|
2079
2089
|
...token,
|
|
@@ -2097,10 +2107,10 @@ class ExpParser {
|
|
|
2097
2107
|
}
|
|
2098
2108
|
const token = this.next();
|
|
2099
2109
|
if (!token) {
|
|
2100
|
-
throw new CompileError(
|
|
2110
|
+
throw new CompileError('expected "ID" after "DOT"', this.template, dot.loc);
|
|
2101
2111
|
}
|
|
2102
2112
|
if (token.type !== 'ID') {
|
|
2103
|
-
throw new CompileError(
|
|
2113
|
+
throw new CompileError('expected "ID" after "DOT"', this.template, dot.loc);
|
|
2104
2114
|
}
|
|
2105
2115
|
ids.push({
|
|
2106
2116
|
...token,
|
|
@@ -2113,7 +2123,7 @@ class ExpParser {
|
|
|
2113
2123
|
parseSequence() {
|
|
2114
2124
|
const elements = [];
|
|
2115
2125
|
while (!this.check('RP')) {
|
|
2116
|
-
const element = this.parseExp(t => t.type === 'COMMA' ? 'BREAK' : undefined);
|
|
2126
|
+
const element = this.parseExp((t) => t.type === 'COMMA' ? 'BREAK' : undefined);
|
|
2117
2127
|
if (element) {
|
|
2118
2128
|
elements.push(element);
|
|
2119
2129
|
}
|
|
@@ -2137,7 +2147,7 @@ class ExpParser {
|
|
|
2137
2147
|
right.args = args;
|
|
2138
2148
|
}
|
|
2139
2149
|
if (!this.consume('RP')) {
|
|
2140
|
-
throw new CompileError(
|
|
2150
|
+
throw new CompileError('expected "RP" after "LP"', this.template, lp.loc);
|
|
2141
2151
|
}
|
|
2142
2152
|
}
|
|
2143
2153
|
return right;
|
|
@@ -2231,23 +2241,11 @@ class Tokenizer {
|
|
|
2231
2241
|
...options,
|
|
2232
2242
|
};
|
|
2233
2243
|
const { commentOpen, commentClose, directiveOpen, directiveClose, outputOpen, outputClose, } = this.options;
|
|
2234
|
-
this.tokenPatterns =
|
|
2235
|
-
[
|
|
2236
|
-
|
|
2237
|
-
|
|
2238
|
-
|
|
2239
|
-
],
|
|
2240
|
-
[
|
|
2241
|
-
TokenType.DIRECTIVE,
|
|
2242
|
-
directiveOpen,
|
|
2243
|
-
directiveClose,
|
|
2244
|
-
],
|
|
2245
|
-
[
|
|
2246
|
-
TokenType.OUTPUT,
|
|
2247
|
-
outputOpen,
|
|
2248
|
-
outputClose,
|
|
2249
|
-
],
|
|
2250
|
-
].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);
|
|
2251
2249
|
}
|
|
2252
2250
|
tokenize(template) {
|
|
2253
2251
|
this.template = template;
|
|
@@ -2316,7 +2314,9 @@ class Tokenizer {
|
|
|
2316
2314
|
: {
|
|
2317
2315
|
val: val.slice(i + 1),
|
|
2318
2316
|
loc: {
|
|
2319
|
-
start: updatePosition(val.slice(0, i + 1), updatePosition(`${openMarker}${stripBefore ? '-' : ''}`, {
|
|
2317
|
+
start: updatePosition(val.slice(0, i + 1), updatePosition(`${openMarker}${stripBefore ? '-' : ''}`, {
|
|
2318
|
+
...start,
|
|
2319
|
+
})),
|
|
2320
2320
|
end: {
|
|
2321
2321
|
line: this.line,
|
|
2322
2322
|
column: this.column - closeMarker.length - +stripAfter,
|
|
@@ -2331,41 +2331,32 @@ class Tokenizer {
|
|
|
2331
2331
|
}
|
|
2332
2332
|
consumeTextToken() {
|
|
2333
2333
|
const nextSpecialIndex = this.findNextSpecialIndex();
|
|
2334
|
-
|
|
2335
|
-
|
|
2336
|
-
|
|
2337
|
-
|
|
2338
|
-
|
|
2339
|
-
|
|
2340
|
-
|
|
2341
|
-
|
|
2342
|
-
|
|
2343
|
-
|
|
2344
|
-
|
|
2345
|
-
|
|
2346
|
-
|
|
2347
|
-
|
|
2348
|
-
|
|
2349
|
-
this.index = nextSpecialIndex;
|
|
2350
|
-
}
|
|
2351
|
-
else {
|
|
2352
|
-
updatePosition(this.template[this.index++], this);
|
|
2353
|
-
}
|
|
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;
|
|
2354
2349
|
}
|
|
2355
2350
|
lookahead(marker, offset = 0) {
|
|
2356
2351
|
return this.template.startsWith(marker, this.index + offset);
|
|
2357
2352
|
}
|
|
2358
2353
|
findNextSpecialIndex() {
|
|
2359
|
-
const { outputOpen, directiveOpen, commentOpen
|
|
2354
|
+
const { outputOpen, directiveOpen, commentOpen } = this.options;
|
|
2360
2355
|
const commentIndex = this.template.indexOf(commentOpen, this.index);
|
|
2361
2356
|
const directiveIndex = this.template.indexOf(directiveOpen, this.index);
|
|
2362
2357
|
const outputIndex = this.template.indexOf(outputOpen, this.index);
|
|
2363
|
-
const indexes = [
|
|
2364
|
-
|
|
2365
|
-
directiveIndex,
|
|
2366
|
-
outputIndex,
|
|
2367
|
-
]
|
|
2368
|
-
.filter(index => index !== -1)
|
|
2358
|
+
const indexes = [commentIndex, directiveIndex, outputIndex]
|
|
2359
|
+
.filter((index) => index !== -1)
|
|
2369
2360
|
.sort((a, b) => a - b);
|
|
2370
2361
|
return indexes.length ? indexes[0] : this.length;
|
|
2371
2362
|
}
|
|
@@ -2400,10 +2391,10 @@ class Parser extends Tokenizer {
|
|
|
2400
2391
|
let prevToken = null;
|
|
2401
2392
|
while (true) {
|
|
2402
2393
|
const token = this.peek();
|
|
2403
|
-
if (!token
|
|
2404
|
-
|
|
2405
|
-
&&
|
|
2406
|
-
|
|
2394
|
+
if (!token ||
|
|
2395
|
+
(token.type === TokenType.DIRECTIVE &&
|
|
2396
|
+
names &&
|
|
2397
|
+
this.match(names, token))) {
|
|
2407
2398
|
break;
|
|
2408
2399
|
}
|
|
2409
2400
|
if (prevToken?.strip?.after && token.type === TokenType.TEXT) {
|
|
@@ -2443,8 +2434,8 @@ class Parser extends Tokenizer {
|
|
|
2443
2434
|
return new TextNode(val, loc, strip);
|
|
2444
2435
|
}
|
|
2445
2436
|
createDirective(token) {
|
|
2446
|
-
return this.options.parsers[token.name.toLowerCase()]?.(token, this)
|
|
2447
|
-
|
|
2437
|
+
return (this.options.parsers[token.name.toLowerCase()]?.(token, this) ??
|
|
2438
|
+
this.createUnknownDirective(token));
|
|
2448
2439
|
}
|
|
2449
2440
|
createUnexpectedDirective(token) {
|
|
2450
2441
|
this.options.debug?.(new CompileError(`Unexpected "${token.name}" directive`, this.template, token.loc));
|
|
@@ -2541,26 +2532,26 @@ class Compiler extends Context {
|
|
|
2541
2532
|
await this.parseTemplates();
|
|
2542
2533
|
if (this.isRoot) {
|
|
2543
2534
|
if (this.partials.size) {
|
|
2544
|
-
this.pushRaw(null,
|
|
2535
|
+
this.pushRaw(null, 'const p={');
|
|
2545
2536
|
for (const [path, code] of this.partials) {
|
|
2546
|
-
this.pushRaw(null, `"${path}":async()=>{`, code,
|
|
2537
|
+
this.pushRaw(null, `"${path}":async()=>{`, code, '},');
|
|
2547
2538
|
}
|
|
2548
|
-
this.pushRaw(null,
|
|
2539
|
+
this.pushRaw(null, '};');
|
|
2549
2540
|
}
|
|
2550
2541
|
if (this.blocks.size) {
|
|
2551
|
-
this.pushRaw(null,
|
|
2552
|
-
for (const [name,
|
|
2553
|
-
this.pushRaw(null, `"${name}":{`,
|
|
2542
|
+
this.pushRaw(null, 'const b={');
|
|
2543
|
+
for (const [name, _blocks] of this.blocks) {
|
|
2544
|
+
this.pushRaw(null, `"${name}":{`, 'u:0,', 's:[');
|
|
2554
2545
|
this.state.block = name;
|
|
2555
|
-
for (const block of
|
|
2556
|
-
this.pushRaw(null,
|
|
2546
|
+
for (const block of _blocks) {
|
|
2547
|
+
this.pushRaw(null, 'async()=>{', 'let s="";');
|
|
2557
2548
|
await this.compileNodes(block.body);
|
|
2558
|
-
this.pushRaw(null,
|
|
2549
|
+
this.pushRaw(null, 'return s;', '},');
|
|
2559
2550
|
}
|
|
2560
2551
|
this.state.block = undefined;
|
|
2561
|
-
this.pushRaw(null,
|
|
2552
|
+
this.pushRaw(null, '],', '},');
|
|
2562
2553
|
}
|
|
2563
|
-
this.pushRaw(null,
|
|
2554
|
+
this.pushRaw(null, '};');
|
|
2564
2555
|
}
|
|
2565
2556
|
}
|
|
2566
2557
|
await this.compileNode(this.rootNode);
|
|
@@ -2585,7 +2576,7 @@ class Compiler extends Context {
|
|
|
2585
2576
|
}
|
|
2586
2577
|
}
|
|
2587
2578
|
for (const block of blocks) {
|
|
2588
|
-
const { val: { value } } = block;
|
|
2579
|
+
const { val: { value }, } = block;
|
|
2589
2580
|
if (!this.blocks.has(value)) {
|
|
2590
2581
|
this.blocks.set(value, []);
|
|
2591
2582
|
}
|
|
@@ -2634,7 +2625,7 @@ class Compiler extends Context {
|
|
|
2634
2625
|
this.partials.set(path, code);
|
|
2635
2626
|
}
|
|
2636
2627
|
catch {
|
|
2637
|
-
this.options.debug?.(new CompileError(
|
|
2628
|
+
this.options.debug?.(new CompileError('Failed to compile template', this.template, node.loc));
|
|
2638
2629
|
}
|
|
2639
2630
|
}
|
|
2640
2631
|
}
|
|
@@ -2664,7 +2655,7 @@ class RenderError extends CompileError {
|
|
|
2664
2655
|
}
|
|
2665
2656
|
|
|
2666
2657
|
class Renderer {
|
|
2667
|
-
constructor({ globals, filters, parsers, compilers, ...options } = {}) {
|
|
2658
|
+
constructor({ globals, filters, parsers, compilers, plugins, ...options } = {}) {
|
|
2668
2659
|
Object.defineProperty(this, "options", {
|
|
2669
2660
|
enumerable: true,
|
|
2670
2661
|
configurable: true,
|
|
@@ -2676,6 +2667,11 @@ class Renderer {
|
|
|
2676
2667
|
Object.assign(this.options.filters, filters);
|
|
2677
2668
|
Object.assign(this.options.parsers, parsers);
|
|
2678
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
|
+
});
|
|
2679
2675
|
}
|
|
2680
2676
|
async render(template, globals) {
|
|
2681
2677
|
const compiler = new Compiler(this.options);
|
|
@@ -2686,24 +2682,27 @@ class Renderer {
|
|
|
2686
2682
|
return `${v}`;
|
|
2687
2683
|
}
|
|
2688
2684
|
return this.options.autoEscape ? escape(v) : v;
|
|
2689
|
-
}, { ...this.options.filters, ...
|
|
2685
|
+
}, { ...this.options.filters, ..._filters });
|
|
2690
2686
|
}
|
|
2691
2687
|
catch (error) {
|
|
2692
|
-
|
|
2693
|
-
|
|
2694
|
-
|
|
2695
|
-
throw new RenderError(error.message, template, () => {
|
|
2696
|
-
const matched = error.stack.match(/<anonymous>:(\d+):(\d+)\)/);
|
|
2697
|
-
if (!matched) {
|
|
2698
|
-
return error.stack ?? '';
|
|
2688
|
+
this.options.debug((() => {
|
|
2689
|
+
if (error.name === 'CompileError') {
|
|
2690
|
+
return error;
|
|
2699
2691
|
}
|
|
2700
|
-
|
|
2701
|
-
|
|
2702
|
-
|
|
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;
|
|
2703
2702
|
});
|
|
2704
|
-
|
|
2705
|
-
});
|
|
2703
|
+
})());
|
|
2706
2704
|
}
|
|
2705
|
+
return '';
|
|
2707
2706
|
}
|
|
2708
2707
|
async renderFile(filepath, globals) {
|
|
2709
2708
|
return this.render(await this.options.loader(filepath), globals);
|