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