janja 2.3.0 β 2.4.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +33 -1
- package/dist/index.cjs.js +103 -71
- package/dist/index.d.ts +81 -3
- package/dist/index.esm.js +91 -72
- package/dist/index.js +103 -71
- package/package.json +7 -7
package/README.md
CHANGED
|
@@ -1,3 +1,35 @@
|
|
|
1
|
+

|
|
2
|
+
|
|
3
|
+
The Reeds
|
|
4
|
+
Tr. ZHAO Yanchun
|
|
5
|
+
|
|
6
|
+
The reeds sway, green and green;
|
|
7
|
+
The white dew becomes frost.
|
|
8
|
+
The lady fair I mean
|
|
9
|
+
Looms yonder, all but lost.
|
|
10
|
+
Upstream to her I row;
|
|
11
|
+
The course is long and high.
|
|
12
|
+
Upstream to her I go;
|
|
13
|
+
On the waves she looks shy.
|
|
14
|
+
|
|
15
|
+
The reeds sway, clean and clean;
|
|
16
|
+
The white dew has not dried.
|
|
17
|
+
The lady fair I mean
|
|
18
|
+
Looms by the riverside.
|
|
19
|
+
Upstream to her I row;
|
|
20
|
+
The course is hard and high.
|
|
21
|
+
Upstream to her I go;
|
|
22
|
+
On the strand she seems nigh.
|
|
23
|
+
|
|
24
|
+
The reeds sway, sheen and sheen;
|
|
25
|
+
The white dew as eβer gleams.
|
|
26
|
+
The lady fair I mean
|
|
27
|
+
Stands on the bank, it seems.
|
|
28
|
+
Upstream to her I row;
|
|
29
|
+
The course turns left and right.
|
|
30
|
+
Upstream to her I go;
|
|
31
|
+
On the shoal she beams bright.
|
|
32
|
+
|
|
1
33
|
# Janja
|
|
2
34
|
|
|
3
35
|
<p align="center">A simple, fast, lightweight, and extensible template engine for Node.js and browsers.</p>
|
|
@@ -18,7 +50,7 @@
|
|
|
18
50
|
- π¨ **Custom directives and filters**: Ability to define custom directives and filters.
|
|
19
51
|
- π **Pre-compilation**: Enhance rendering performance by precompiling.
|
|
20
52
|
- π **Error handling and debugging**: Provides detailed error reporting and debugging support.
|
|
21
|
-
- π‘οΈ **Security**:
|
|
53
|
+
- π‘οΈ **Security**: Automatic escaping.
|
|
22
54
|
- π« **Asynchronous data loading**: Supports fetching and displaying remote data asynchronously.
|
|
23
55
|
- π **Universal compatibility**: Works in both Node.js and browser environments.
|
|
24
56
|
- π¦ **Zero dependencies**: Lightweight with no external dependencies.
|
package/dist/index.cjs.js
CHANGED
|
@@ -884,6 +884,7 @@ class CommentNode extends Traversal {
|
|
|
884
884
|
}
|
|
885
885
|
|
|
886
886
|
async function* parseComment({ val, loc, strip }) {
|
|
887
|
+
console.log(val);
|
|
887
888
|
yield 'NEXT';
|
|
888
889
|
yield new CommentNode(val, loc, strip);
|
|
889
890
|
}
|
|
@@ -896,20 +897,20 @@ const plugin$9 = {
|
|
|
896
897
|
parsers: parsers$9,
|
|
897
898
|
};
|
|
898
899
|
|
|
899
|
-
async function compileFor({ loop, body }, compiler) {
|
|
900
|
+
async function compileFor({ loop: { left, right, loc }, body }, compiler) {
|
|
900
901
|
const { context } = compiler;
|
|
901
902
|
const expCompiler = new ExpCompiler();
|
|
902
|
-
if (
|
|
903
|
-
compiler.pushRaw(
|
|
903
|
+
if (left.type === 'SEQ') {
|
|
904
|
+
compiler.pushRaw(loc, '{', `const a_=${expCompiler.compile(right, context)},n_=a_.length;`, 'let i_=0;', `for(const {${left.elements
|
|
904
905
|
.map((el) => el.type === 'ASSIGN'
|
|
905
906
|
? `${el.left.value}=${expCompiler.compile(el.right, context)}`
|
|
906
907
|
: el.value)
|
|
907
|
-
.join(',')}} of a_){`, `const ${compiler.in()}={`, `...${context},`, 'loop:{', 'first:i_===0,', 'index:i_++,', 'last:i_===n_,', '},', `${
|
|
908
|
+
.join(',')}} of a_){`, `const ${compiler.in()}={`, `...${context},`, 'loop:{', 'first:i_===0,', 'index:i_++,', 'last:i_===n_,', '},', `${left.elements
|
|
908
909
|
.map((el) => (el.type === 'ASSIGN' ? el.left.value : el.value))
|
|
909
910
|
.join(',')},`, '};');
|
|
910
911
|
}
|
|
911
912
|
else {
|
|
912
|
-
compiler.pushRaw(
|
|
913
|
+
compiler.pushRaw(loc, '{', `const a_=${expCompiler.compile(right, context)},n_=a_.length;`, 'let i_=0;', `for(const ${left.value} of a_){`, `const ${compiler.in()}={`, `...${context},`, 'loop:{', 'first:i_===0,', 'index:i_++,', 'last:i_===n_,', '},', `${left.value},`, '};');
|
|
913
914
|
}
|
|
914
915
|
await compiler.compileNodes(body);
|
|
915
916
|
compiler.out();
|
|
@@ -1639,11 +1640,11 @@ const plugin$2 = {
|
|
|
1639
1640
|
parsers: parsers$2,
|
|
1640
1641
|
};
|
|
1641
1642
|
|
|
1642
|
-
async function compileUnexpected
|
|
1643
|
-
compiler.pushRaw(loc, `<!--
|
|
1643
|
+
async function compileUnexpected({ name, loc }, compiler) {
|
|
1644
|
+
compiler.pushRaw(loc, `<!--Unexpected "${name}"-->`);
|
|
1644
1645
|
}
|
|
1645
1646
|
const compilers$1 = {
|
|
1646
|
-
UNEXPECTED: compileUnexpected
|
|
1647
|
+
UNEXPECTED: compileUnexpected,
|
|
1647
1648
|
};
|
|
1648
1649
|
|
|
1649
1650
|
class UnexpectedNode extends Traversal {
|
|
@@ -1684,7 +1685,7 @@ class UnexpectedNode extends Traversal {
|
|
|
1684
1685
|
|
|
1685
1686
|
async function* parseUnexpected(token, parser) {
|
|
1686
1687
|
const name = token.name ?? token.type;
|
|
1687
|
-
parser.options.debug?.(new CompileError(`Unexpected "${name}"
|
|
1688
|
+
parser.options.debug?.(new CompileError(`Unexpected "${name}"`, parser.template, token.loc));
|
|
1688
1689
|
yield 'NEXT';
|
|
1689
1690
|
yield new UnexpectedNode(name, token.val, token.loc, token.strip);
|
|
1690
1691
|
}
|
|
@@ -1697,11 +1698,11 @@ const plugin$1 = {
|
|
|
1697
1698
|
parsers: parsers$1,
|
|
1698
1699
|
};
|
|
1699
1700
|
|
|
1700
|
-
async function
|
|
1701
|
-
compiler.pushRaw(loc, `<!--
|
|
1701
|
+
async function compileUnknown({ name, loc }, compiler) {
|
|
1702
|
+
compiler.pushRaw(loc, `<!--Unknown "${name}"-->`);
|
|
1702
1703
|
}
|
|
1703
1704
|
const compilers = {
|
|
1704
|
-
|
|
1705
|
+
UNKNOWN: compileUnknown,
|
|
1705
1706
|
};
|
|
1706
1707
|
|
|
1707
1708
|
class UnknownNode extends Traversal {
|
|
@@ -1742,7 +1743,7 @@ class UnknownNode extends Traversal {
|
|
|
1742
1743
|
|
|
1743
1744
|
async function* parseUnknown(token, parser) {
|
|
1744
1745
|
const name = token.name ?? token.type;
|
|
1745
|
-
parser.options.debug?.(new CompileError(`Unknown "${name}"
|
|
1746
|
+
parser.options.debug?.(new CompileError(`Unknown "${name}"`, parser.template, token.loc));
|
|
1746
1747
|
yield 'NEXT';
|
|
1747
1748
|
yield new UnknownNode(name, token.val, token.loc, token.strip);
|
|
1748
1749
|
}
|
|
@@ -1781,7 +1782,7 @@ const baseOptions = {
|
|
|
1781
1782
|
const parserOptions = {
|
|
1782
1783
|
...baseOptions,
|
|
1783
1784
|
commentOpen: '{{#',
|
|
1784
|
-
commentClose: '}}',
|
|
1785
|
+
commentClose: '#}}',
|
|
1785
1786
|
directiveOpen: '{{',
|
|
1786
1787
|
directiveClose: '}}',
|
|
1787
1788
|
outputOpen: '{{=',
|
|
@@ -1816,6 +1817,20 @@ const mergeOptions = (defaultOptions, options) => {
|
|
|
1816
1817
|
return mergedOptions;
|
|
1817
1818
|
};
|
|
1818
1819
|
|
|
1820
|
+
class ExpError extends SyntaxError {
|
|
1821
|
+
constructor(message, loc) {
|
|
1822
|
+
super(message);
|
|
1823
|
+
Object.defineProperty(this, "loc", {
|
|
1824
|
+
enumerable: true,
|
|
1825
|
+
configurable: true,
|
|
1826
|
+
writable: true,
|
|
1827
|
+
value: loc
|
|
1828
|
+
});
|
|
1829
|
+
this.name = 'ExpError';
|
|
1830
|
+
ExpError.captureStackTrace?.(this, this.constructor);
|
|
1831
|
+
}
|
|
1832
|
+
}
|
|
1833
|
+
|
|
1819
1834
|
function updatePosition(str, pos) {
|
|
1820
1835
|
const newlines = (str.match(/\n/g) || []).length;
|
|
1821
1836
|
if (newlines > 0) {
|
|
@@ -1864,14 +1879,22 @@ const expTokenTypes = {
|
|
|
1864
1879
|
',': 'COMMA',
|
|
1865
1880
|
'.': 'DOT',
|
|
1866
1881
|
};
|
|
1882
|
+
function normalizeLitValue(value) {
|
|
1883
|
+
switch (value) {
|
|
1884
|
+
case 'true':
|
|
1885
|
+
return true;
|
|
1886
|
+
case 'false':
|
|
1887
|
+
return false;
|
|
1888
|
+
case 'null':
|
|
1889
|
+
return null;
|
|
1890
|
+
case 'undefined':
|
|
1891
|
+
return undefined;
|
|
1892
|
+
default:
|
|
1893
|
+
return value;
|
|
1894
|
+
}
|
|
1895
|
+
}
|
|
1867
1896
|
class ExpTokenizer {
|
|
1868
|
-
constructor(
|
|
1869
|
-
Object.defineProperty(this, "template", {
|
|
1870
|
-
enumerable: true,
|
|
1871
|
-
configurable: true,
|
|
1872
|
-
writable: true,
|
|
1873
|
-
value: template
|
|
1874
|
-
});
|
|
1897
|
+
constructor() {
|
|
1875
1898
|
Object.defineProperty(this, "val", {
|
|
1876
1899
|
enumerable: true,
|
|
1877
1900
|
configurable: true,
|
|
@@ -1926,19 +1949,19 @@ class ExpTokenizer {
|
|
|
1926
1949
|
this.readString(char);
|
|
1927
1950
|
continue;
|
|
1928
1951
|
}
|
|
1929
|
-
if (
|
|
1930
|
-
this.readNumber();
|
|
1952
|
+
if (isDigitStart(char)) {
|
|
1953
|
+
this.readNumber(char);
|
|
1931
1954
|
continue;
|
|
1932
1955
|
}
|
|
1933
|
-
if (
|
|
1934
|
-
this.readIdentifier();
|
|
1956
|
+
if (isIdentifierStart(char)) {
|
|
1957
|
+
this.readIdentifier(char);
|
|
1935
1958
|
continue;
|
|
1936
1959
|
}
|
|
1937
1960
|
if (isSymbol(char)) {
|
|
1938
1961
|
this.readSymbol(char);
|
|
1939
1962
|
continue;
|
|
1940
1963
|
}
|
|
1941
|
-
throw new
|
|
1964
|
+
throw new ExpError(`Unexpect "${char}"`, {
|
|
1942
1965
|
start: { line: this.line, column: this.column },
|
|
1943
1966
|
end: { line: this.line, column: this.column + 1 },
|
|
1944
1967
|
});
|
|
@@ -1981,10 +2004,11 @@ class ExpTokenizer {
|
|
|
1981
2004
|
raw: `${quoteChar}${value}${quoteChar}`,
|
|
1982
2005
|
});
|
|
1983
2006
|
}
|
|
1984
|
-
readNumber() {
|
|
1985
|
-
let value =
|
|
2007
|
+
readNumber(startChar) {
|
|
2008
|
+
let value = startChar;
|
|
1986
2009
|
let hasDot = false;
|
|
1987
2010
|
const start = { line: this.line, column: this.column };
|
|
2011
|
+
this.index++;
|
|
1988
2012
|
while (this.index < this.val.length) {
|
|
1989
2013
|
const char = this.val[this.index];
|
|
1990
2014
|
if (isDigit(char)) {
|
|
@@ -2009,12 +2033,13 @@ class ExpTokenizer {
|
|
|
2009
2033
|
raw: value,
|
|
2010
2034
|
});
|
|
2011
2035
|
}
|
|
2012
|
-
readIdentifier() {
|
|
2013
|
-
let value =
|
|
2036
|
+
readIdentifier(startChar) {
|
|
2037
|
+
let value = startChar;
|
|
2014
2038
|
const start = { line: this.line, column: this.column };
|
|
2039
|
+
this.index++;
|
|
2015
2040
|
while (this.index < this.val.length) {
|
|
2016
2041
|
const char = this.val[this.index];
|
|
2017
|
-
if (
|
|
2042
|
+
if (isIdentifier(char)) {
|
|
2018
2043
|
value += char;
|
|
2019
2044
|
this.index++;
|
|
2020
2045
|
continue;
|
|
@@ -2026,17 +2051,7 @@ class ExpTokenizer {
|
|
|
2026
2051
|
: 'ID';
|
|
2027
2052
|
this.tokens.push({
|
|
2028
2053
|
type,
|
|
2029
|
-
value: type === 'LIT'
|
|
2030
|
-
? value === 'true'
|
|
2031
|
-
? true
|
|
2032
|
-
: value === 'false'
|
|
2033
|
-
? false
|
|
2034
|
-
: value === 'null'
|
|
2035
|
-
? null
|
|
2036
|
-
: value === 'undefined'
|
|
2037
|
-
? undefined
|
|
2038
|
-
: value
|
|
2039
|
-
: value,
|
|
2054
|
+
value: type === 'LIT' ? normalizeLitValue(value) : value,
|
|
2040
2055
|
loc: {
|
|
2041
2056
|
start,
|
|
2042
2057
|
end: updatePosition(value, this),
|
|
@@ -2064,13 +2079,16 @@ function isWhitespace(char) {
|
|
|
2064
2079
|
function isSymbol(char) {
|
|
2065
2080
|
return /[+\-*/%|=(),.]/.test(char);
|
|
2066
2081
|
}
|
|
2067
|
-
function
|
|
2082
|
+
function isDigitStart(char) {
|
|
2068
2083
|
return /[-\d]/.test(char);
|
|
2069
2084
|
}
|
|
2070
|
-
function
|
|
2085
|
+
function isDigit(char) {
|
|
2086
|
+
return /\d/.test(char);
|
|
2087
|
+
}
|
|
2088
|
+
function isIdentifierStart(char) {
|
|
2071
2089
|
return /[a-z_$]/i.test(char);
|
|
2072
2090
|
}
|
|
2073
|
-
function
|
|
2091
|
+
function isIdentifier(char) {
|
|
2074
2092
|
return /[\w$]/.test(char);
|
|
2075
2093
|
}
|
|
2076
2094
|
|
|
@@ -2105,13 +2123,7 @@ const expTokenPrecedences = {
|
|
|
2105
2123
|
LIT: 90,
|
|
2106
2124
|
};
|
|
2107
2125
|
class ExpParser {
|
|
2108
|
-
constructor(
|
|
2109
|
-
Object.defineProperty(this, "template", {
|
|
2110
|
-
enumerable: true,
|
|
2111
|
-
configurable: true,
|
|
2112
|
-
writable: true,
|
|
2113
|
-
value: template
|
|
2114
|
-
});
|
|
2126
|
+
constructor() {
|
|
2115
2127
|
Object.defineProperty(this, "val", {
|
|
2116
2128
|
enumerable: true,
|
|
2117
2129
|
configurable: true,
|
|
@@ -2148,7 +2160,7 @@ class ExpParser {
|
|
|
2148
2160
|
this.index = 0;
|
|
2149
2161
|
this.line = loc.start.line;
|
|
2150
2162
|
this.column = loc.start.column;
|
|
2151
|
-
this.tokens = new ExpTokenizer(
|
|
2163
|
+
this.tokens = new ExpTokenizer().tokenize(val, loc);
|
|
2152
2164
|
return this.val ? this.parseExp() : null;
|
|
2153
2165
|
}
|
|
2154
2166
|
parseExp(checker) {
|
|
@@ -2168,12 +2180,12 @@ class ExpParser {
|
|
|
2168
2180
|
}
|
|
2169
2181
|
if (token.type === 'LP') {
|
|
2170
2182
|
if (!this.peek()) {
|
|
2171
|
-
throw new
|
|
2183
|
+
throw new ExpError('Unexpected end of expression', token.loc);
|
|
2172
2184
|
}
|
|
2173
2185
|
const elements = this.parseSequence();
|
|
2174
2186
|
const rp = this.consume('RP');
|
|
2175
2187
|
if (!rp) {
|
|
2176
|
-
throw new
|
|
2188
|
+
throw new ExpError('Expected "RP" after "LP"', token.loc);
|
|
2177
2189
|
}
|
|
2178
2190
|
left = {
|
|
2179
2191
|
...token,
|
|
@@ -2188,7 +2200,7 @@ class ExpParser {
|
|
|
2188
2200
|
}
|
|
2189
2201
|
if (token.type === 'NOT') {
|
|
2190
2202
|
if (!this.peek()) {
|
|
2191
|
-
throw new
|
|
2203
|
+
throw new ExpError('Unexpected end of expression', token.loc);
|
|
2192
2204
|
}
|
|
2193
2205
|
left = {
|
|
2194
2206
|
...token,
|
|
@@ -2210,7 +2222,7 @@ class ExpParser {
|
|
|
2210
2222
|
if (lp) {
|
|
2211
2223
|
left.args = this.parseSequence();
|
|
2212
2224
|
if (!this.consume('RP')) {
|
|
2213
|
-
throw new
|
|
2225
|
+
throw new ExpError('Expected "RP" after "LP"', lp.loc);
|
|
2214
2226
|
}
|
|
2215
2227
|
continue;
|
|
2216
2228
|
}
|
|
@@ -2226,7 +2238,7 @@ class ExpParser {
|
|
|
2226
2238
|
if (token.type === 'IF') {
|
|
2227
2239
|
const test = this.parseExp((t) => t.type === 'ELSE' ? 'BACK' : isHigher(token, t) ? 'BACK' : undefined);
|
|
2228
2240
|
if (!test) {
|
|
2229
|
-
throw new
|
|
2241
|
+
throw new ExpError('Expected test expression', token.loc);
|
|
2230
2242
|
}
|
|
2231
2243
|
left = {
|
|
2232
2244
|
...token,
|
|
@@ -2237,7 +2249,7 @@ class ExpParser {
|
|
|
2237
2249
|
if (this.consume('ELSE')) {
|
|
2238
2250
|
const alternative = this.parseExp((t) => isHigher(token, t) ? 'BACK' : undefined);
|
|
2239
2251
|
if (!alternative) {
|
|
2240
|
-
throw new
|
|
2252
|
+
throw new ExpError('Expected alternative expression', token.loc);
|
|
2241
2253
|
}
|
|
2242
2254
|
left.alternative = alternative;
|
|
2243
2255
|
}
|
|
@@ -2262,11 +2274,11 @@ class ExpParser {
|
|
|
2262
2274
|
token.type === 'MOD' ||
|
|
2263
2275
|
token.type === 'ASSIGN') {
|
|
2264
2276
|
if (!left) {
|
|
2265
|
-
throw new
|
|
2277
|
+
throw new ExpError(`No left operand for "${token.type}"`, token.loc);
|
|
2266
2278
|
}
|
|
2267
2279
|
const right = this.parseExp((t) => isHigher(token, t) ? 'BACK' : undefined);
|
|
2268
2280
|
if (!right) {
|
|
2269
|
-
throw new
|
|
2281
|
+
throw new ExpError(`No right operand for "${token.type}"`, token.loc);
|
|
2270
2282
|
}
|
|
2271
2283
|
left = {
|
|
2272
2284
|
...token,
|
|
@@ -2278,10 +2290,10 @@ class ExpParser {
|
|
|
2278
2290
|
}
|
|
2279
2291
|
if (token.type === 'PIPE') {
|
|
2280
2292
|
if (!left) {
|
|
2281
|
-
throw new
|
|
2293
|
+
throw new ExpError(`No left operand for "${token.type}"`, token.loc);
|
|
2282
2294
|
}
|
|
2283
2295
|
if (!this.match('ID')) {
|
|
2284
|
-
throw new
|
|
2296
|
+
throw new ExpError('Expected "ID" after "PIPE"', token.loc);
|
|
2285
2297
|
}
|
|
2286
2298
|
left = {
|
|
2287
2299
|
...token,
|
|
@@ -2305,10 +2317,10 @@ class ExpParser {
|
|
|
2305
2317
|
}
|
|
2306
2318
|
const token = this.next();
|
|
2307
2319
|
if (!token) {
|
|
2308
|
-
throw new
|
|
2320
|
+
throw new ExpError('Expected "ID" after "DOT"', dot.loc);
|
|
2309
2321
|
}
|
|
2310
2322
|
if (token.type !== 'ID') {
|
|
2311
|
-
throw new
|
|
2323
|
+
throw new ExpError('Expected "ID" after "DOT"', dot.loc);
|
|
2312
2324
|
}
|
|
2313
2325
|
ids.push({
|
|
2314
2326
|
...token,
|
|
@@ -2345,7 +2357,7 @@ class ExpParser {
|
|
|
2345
2357
|
right.args = args;
|
|
2346
2358
|
}
|
|
2347
2359
|
if (!this.consume('RP')) {
|
|
2348
|
-
throw new
|
|
2360
|
+
throw new ExpError('Expected "RP" after "LP"', lp.loc);
|
|
2349
2361
|
}
|
|
2350
2362
|
}
|
|
2351
2363
|
return right;
|
|
@@ -2577,7 +2589,7 @@ class Parser extends Tokenizer {
|
|
|
2577
2589
|
this.tokenize(template);
|
|
2578
2590
|
const start = this.tokens[0]?.loc.start ?? { line: 1, column: 1 };
|
|
2579
2591
|
const end = this.tokens.at(-1)?.loc.end ?? { line: 1, column: 1 };
|
|
2580
|
-
this.expParser = new ExpParser(
|
|
2592
|
+
this.expParser = new ExpParser();
|
|
2581
2593
|
this.cursor = 0;
|
|
2582
2594
|
return new RootNode(await this.parseUntil(), { start, end });
|
|
2583
2595
|
}
|
|
@@ -2626,7 +2638,14 @@ class Parser extends Tokenizer {
|
|
|
2626
2638
|
return nodes;
|
|
2627
2639
|
}
|
|
2628
2640
|
parseExp({ val, loc }) {
|
|
2629
|
-
|
|
2641
|
+
try {
|
|
2642
|
+
return this.expParser.parse(val, loc);
|
|
2643
|
+
}
|
|
2644
|
+
catch (error) {
|
|
2645
|
+
this.options.debug(error.loc
|
|
2646
|
+
? new CompileError(error.message, this.template, error.loc)
|
|
2647
|
+
: error);
|
|
2648
|
+
}
|
|
2630
2649
|
}
|
|
2631
2650
|
peek() {
|
|
2632
2651
|
return this.tokens.at(this.cursor) ?? null;
|
|
@@ -2638,7 +2657,7 @@ class Parser extends Tokenizer {
|
|
|
2638
2657
|
return names.includes(token?.name);
|
|
2639
2658
|
}
|
|
2640
2659
|
emitExpErr({ name, loc }, required = true) {
|
|
2641
|
-
this.options.debug
|
|
2660
|
+
this.options.debug(new CompileError(required
|
|
2642
2661
|
? `"${name}" requires expression`
|
|
2643
2662
|
: `"${name}" should not have expression`, this.template, loc));
|
|
2644
2663
|
}
|
|
@@ -2737,7 +2756,7 @@ class Compiler extends Context {
|
|
|
2737
2756
|
template = await this.options.loader(path);
|
|
2738
2757
|
}
|
|
2739
2758
|
catch {
|
|
2740
|
-
this.options.debug
|
|
2759
|
+
this.options.debug(new CompileError(`Failed to load template from "${path}"`, this.template, node.loc));
|
|
2741
2760
|
return;
|
|
2742
2761
|
}
|
|
2743
2762
|
if (template) {
|
|
@@ -2798,7 +2817,7 @@ class Compiler extends Context {
|
|
|
2798
2817
|
this.partials.set(path, code);
|
|
2799
2818
|
}
|
|
2800
2819
|
catch {
|
|
2801
|
-
this.options.debug
|
|
2820
|
+
this.options.debug(new CompileError('Failed to compile template', this.template, node.loc));
|
|
2802
2821
|
}
|
|
2803
2822
|
}
|
|
2804
2823
|
}
|
|
@@ -2880,6 +2899,19 @@ async function renderFile(filepath, data, options) {
|
|
|
2880
2899
|
return new Renderer(options).renderFile(filepath, data);
|
|
2881
2900
|
}
|
|
2882
2901
|
|
|
2902
|
+
exports.CompileError = CompileError;
|
|
2903
|
+
exports.Compiler = Compiler;
|
|
2904
|
+
exports.ExpCompiler = ExpCompiler;
|
|
2905
|
+
exports.ExpError = ExpError;
|
|
2906
|
+
exports.ExpParser = ExpParser;
|
|
2907
|
+
exports.ExpTokenizer = ExpTokenizer;
|
|
2908
|
+
exports.Parser = Parser;
|
|
2909
|
+
exports.RenderError = RenderError;
|
|
2883
2910
|
exports.Renderer = Renderer;
|
|
2911
|
+
exports.RootNode = RootNode;
|
|
2912
|
+
exports.Safe = Safe;
|
|
2913
|
+
exports.Tokenizer = Tokenizer;
|
|
2914
|
+
exports.Traversal = Traversal;
|
|
2915
|
+
exports.expTokenTypes = expTokenTypes;
|
|
2884
2916
|
exports.render = render;
|
|
2885
2917
|
exports.renderFile = renderFile;
|
package/dist/index.d.ts
CHANGED
|
@@ -163,7 +163,7 @@ declare class Parser extends Tokenizer {
|
|
|
163
163
|
protected cursor: number;
|
|
164
164
|
parse(template: string): Promise<RootNode>;
|
|
165
165
|
parseUntil(names?: string[]): Promise<SyntaxNode[]>;
|
|
166
|
-
parseExp<T extends Exp = Exp>({ val, loc }: DirectiveExpression): T;
|
|
166
|
+
parseExp<T extends Exp = Exp>({ val, loc }: DirectiveExpression): T | undefined;
|
|
167
167
|
peek(): Token | null;
|
|
168
168
|
private advance;
|
|
169
169
|
match(names: string[], token?: Token | null): boolean;
|
|
@@ -255,6 +255,20 @@ interface RendererOptions extends CompilerOptions {
|
|
|
255
255
|
autoEscape?: boolean;
|
|
256
256
|
}
|
|
257
257
|
|
|
258
|
+
declare class CompileError extends SyntaxError {
|
|
259
|
+
protected src: string;
|
|
260
|
+
protected loc?: Loc | undefined;
|
|
261
|
+
constructor(message: string, src: string, loc?: Loc | undefined);
|
|
262
|
+
get details(): string;
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
declare class RenderError extends CompileError {
|
|
266
|
+
protected src: string;
|
|
267
|
+
protected getLoc: () => Loc;
|
|
268
|
+
constructor(message: string, src: string, getLoc: () => Loc);
|
|
269
|
+
get details(): string;
|
|
270
|
+
}
|
|
271
|
+
|
|
258
272
|
declare class Renderer {
|
|
259
273
|
protected options: Required<RendererOptions>;
|
|
260
274
|
constructor(options?: RendererOptions);
|
|
@@ -262,8 +276,72 @@ declare class Renderer {
|
|
|
262
276
|
renderFile(filepath: string, globals: ObjectType): Promise<string>;
|
|
263
277
|
}
|
|
264
278
|
|
|
279
|
+
declare class Safe extends String {
|
|
280
|
+
constructor(value: unknown);
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
declare class ExpCompiler {
|
|
284
|
+
private context;
|
|
285
|
+
private filters;
|
|
286
|
+
compile(expression: Exp | null, context?: string, filters?: string): string;
|
|
287
|
+
private compileExp;
|
|
288
|
+
private compileNot;
|
|
289
|
+
private compileId;
|
|
290
|
+
private compileLit;
|
|
291
|
+
private compileSeq;
|
|
292
|
+
private compilePipe;
|
|
293
|
+
private compileAssign;
|
|
294
|
+
private compileIs;
|
|
295
|
+
private compileBinary;
|
|
296
|
+
private compileTernary;
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
declare class ExpError extends SyntaxError {
|
|
300
|
+
loc: Loc;
|
|
301
|
+
constructor(message: string, loc: Loc);
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
interface ExpToken<T = ExpTokenType> {
|
|
305
|
+
type: T;
|
|
306
|
+
value: Primitive;
|
|
307
|
+
loc: Loc;
|
|
308
|
+
raw?: string;
|
|
309
|
+
}
|
|
310
|
+
declare const expTokenTypes: Record<string, ExpTokenType>;
|
|
311
|
+
declare class ExpTokenizer implements Pos {
|
|
312
|
+
val: string;
|
|
313
|
+
tokens: ExpToken[];
|
|
314
|
+
index: number;
|
|
315
|
+
line: number;
|
|
316
|
+
column: number;
|
|
317
|
+
tokenize(val: string, loc: Loc): ExpToken<ExpTokenType>[];
|
|
318
|
+
private readString;
|
|
319
|
+
private readNumber;
|
|
320
|
+
private readIdentifier;
|
|
321
|
+
private readSymbol;
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
type Checker = (token: ExpToken) => 'BACK' | 'BREAK' | undefined;
|
|
325
|
+
declare class ExpParser implements Pos {
|
|
326
|
+
val: string;
|
|
327
|
+
tokens: ExpToken[];
|
|
328
|
+
index: number;
|
|
329
|
+
line: number;
|
|
330
|
+
column: number;
|
|
331
|
+
parse(val: string, loc: Loc): Exp | null;
|
|
332
|
+
private parseExp;
|
|
333
|
+
private parsePath;
|
|
334
|
+
private parseSequence;
|
|
335
|
+
private parsePipe;
|
|
336
|
+
private next;
|
|
337
|
+
private back;
|
|
338
|
+
private peek;
|
|
339
|
+
private match;
|
|
340
|
+
private consume;
|
|
341
|
+
}
|
|
342
|
+
|
|
265
343
|
declare function render(template: string, data: ObjectType, options?: RendererOptions): Promise<string>;
|
|
266
344
|
declare function renderFile(filepath: string, data: ObjectType, options?: RendererOptions): Promise<string>;
|
|
267
345
|
|
|
268
|
-
export { Renderer, TokenType, render, renderFile };
|
|
269
|
-
export type { BinaryExp, BinaryExpTokenType, BinaryExpType, CommentToken, CompilerFn, CompilerMap, DirectiveExpression, DirectiveToken, Exp, ExpBase, ExpTokenType, ExpType, Filters, IdExp, IfExp, LitExp, Loc, MaybePromise, NotExp, ObjectType, OutputToken, ParserFn, ParserMap, Plugin, Pos, Primitive, Script, SeqExp, Strip, TernaryExpTokenType, TernaryExpType, TextToken, Token, TokenBase, UnaryExpTokenType, UnaryExpType };
|
|
346
|
+
export { CompileError, Compiler, ExpCompiler, ExpError, ExpParser, ExpTokenizer, Parser, RenderError, Renderer, RootNode, Safe, TokenType, Tokenizer, Traversal, expTokenTypes, render, renderFile };
|
|
347
|
+
export type { BinaryExp, BinaryExpTokenType, BinaryExpType, Checker, CommentToken, CompilerFn, CompilerMap, DirectiveExpression, DirectiveToken, Exp, ExpBase, ExpToken, ExpTokenType, ExpType, Filters, IdExp, IfExp, LitExp, Loc, MaybePromise, NotExp, ObjectType, OutputToken, ParserFn, ParserMap, Plugin, Pos, Primitive, Script, SeqExp, Strip, SyntaxNode, TernaryExpTokenType, TernaryExpType, TextToken, Token, TokenBase, UnaryExpTokenType, UnaryExpType };
|