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