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/dist/index.esm.js
CHANGED
|
@@ -882,6 +882,7 @@ class CommentNode extends Traversal {
|
|
|
882
882
|
}
|
|
883
883
|
|
|
884
884
|
async function* parseComment({ val, loc, strip }) {
|
|
885
|
+
console.log(val);
|
|
885
886
|
yield 'NEXT';
|
|
886
887
|
yield new CommentNode(val, loc, strip);
|
|
887
888
|
}
|
|
@@ -894,20 +895,20 @@ const plugin$9 = {
|
|
|
894
895
|
parsers: parsers$9,
|
|
895
896
|
};
|
|
896
897
|
|
|
897
|
-
async function compileFor({ loop, body }, compiler) {
|
|
898
|
+
async function compileFor({ loop: { left, right, loc }, body }, compiler) {
|
|
898
899
|
const { context } = compiler;
|
|
899
900
|
const expCompiler = new ExpCompiler();
|
|
900
|
-
if (
|
|
901
|
-
compiler.pushRaw(
|
|
901
|
+
if (left.type === 'SEQ') {
|
|
902
|
+
compiler.pushRaw(loc, '{', `const a_=${expCompiler.compile(right, context)},n_=a_.length;`, 'let i_=0;', `for(const {${left.elements
|
|
902
903
|
.map((el) => el.type === 'ASSIGN'
|
|
903
904
|
? `${el.left.value}=${expCompiler.compile(el.right, context)}`
|
|
904
905
|
: el.value)
|
|
905
|
-
.join(',')}} of a_){`, `const ${compiler.in()}={`, `...${context},`, 'loop:{', 'first:i_===0,', 'index:i_++,', 'last:i_===n_,', '},', `${
|
|
906
|
+
.join(',')}} of a_){`, `const ${compiler.in()}={`, `...${context},`, 'loop:{', 'first:i_===0,', 'index:i_++,', 'last:i_===n_,', '},', `${left.elements
|
|
906
907
|
.map((el) => (el.type === 'ASSIGN' ? el.left.value : el.value))
|
|
907
908
|
.join(',')},`, '};');
|
|
908
909
|
}
|
|
909
910
|
else {
|
|
910
|
-
compiler.pushRaw(
|
|
911
|
+
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},`, '};');
|
|
911
912
|
}
|
|
912
913
|
await compiler.compileNodes(body);
|
|
913
914
|
compiler.out();
|
|
@@ -1637,11 +1638,11 @@ const plugin$2 = {
|
|
|
1637
1638
|
parsers: parsers$2,
|
|
1638
1639
|
};
|
|
1639
1640
|
|
|
1640
|
-
async function compileUnexpected
|
|
1641
|
-
compiler.pushRaw(loc, `<!--
|
|
1641
|
+
async function compileUnexpected({ name, loc }, compiler) {
|
|
1642
|
+
compiler.pushRaw(loc, `<!--Unexpected "${name}"-->`);
|
|
1642
1643
|
}
|
|
1643
1644
|
const compilers$1 = {
|
|
1644
|
-
UNEXPECTED: compileUnexpected
|
|
1645
|
+
UNEXPECTED: compileUnexpected,
|
|
1645
1646
|
};
|
|
1646
1647
|
|
|
1647
1648
|
class UnexpectedNode extends Traversal {
|
|
@@ -1682,7 +1683,7 @@ class UnexpectedNode extends Traversal {
|
|
|
1682
1683
|
|
|
1683
1684
|
async function* parseUnexpected(token, parser) {
|
|
1684
1685
|
const name = token.name ?? token.type;
|
|
1685
|
-
parser.options.debug?.(new CompileError(`Unexpected "${name}"
|
|
1686
|
+
parser.options.debug?.(new CompileError(`Unexpected "${name}"`, parser.template, token.loc));
|
|
1686
1687
|
yield 'NEXT';
|
|
1687
1688
|
yield new UnexpectedNode(name, token.val, token.loc, token.strip);
|
|
1688
1689
|
}
|
|
@@ -1695,11 +1696,11 @@ const plugin$1 = {
|
|
|
1695
1696
|
parsers: parsers$1,
|
|
1696
1697
|
};
|
|
1697
1698
|
|
|
1698
|
-
async function
|
|
1699
|
-
compiler.pushRaw(loc, `<!--
|
|
1699
|
+
async function compileUnknown({ name, loc }, compiler) {
|
|
1700
|
+
compiler.pushRaw(loc, `<!--Unknown "${name}"-->`);
|
|
1700
1701
|
}
|
|
1701
1702
|
const compilers = {
|
|
1702
|
-
|
|
1703
|
+
UNKNOWN: compileUnknown,
|
|
1703
1704
|
};
|
|
1704
1705
|
|
|
1705
1706
|
class UnknownNode extends Traversal {
|
|
@@ -1740,7 +1741,7 @@ class UnknownNode extends Traversal {
|
|
|
1740
1741
|
|
|
1741
1742
|
async function* parseUnknown(token, parser) {
|
|
1742
1743
|
const name = token.name ?? token.type;
|
|
1743
|
-
parser.options.debug?.(new CompileError(`Unknown "${name}"
|
|
1744
|
+
parser.options.debug?.(new CompileError(`Unknown "${name}"`, parser.template, token.loc));
|
|
1744
1745
|
yield 'NEXT';
|
|
1745
1746
|
yield new UnknownNode(name, token.val, token.loc, token.strip);
|
|
1746
1747
|
}
|
|
@@ -1779,7 +1780,7 @@ const baseOptions = {
|
|
|
1779
1780
|
const parserOptions = {
|
|
1780
1781
|
...baseOptions,
|
|
1781
1782
|
commentOpen: '{{#',
|
|
1782
|
-
commentClose: '}}',
|
|
1783
|
+
commentClose: '#}}',
|
|
1783
1784
|
directiveOpen: '{{',
|
|
1784
1785
|
directiveClose: '}}',
|
|
1785
1786
|
outputOpen: '{{=',
|
|
@@ -1814,6 +1815,20 @@ const mergeOptions = (defaultOptions, options) => {
|
|
|
1814
1815
|
return mergedOptions;
|
|
1815
1816
|
};
|
|
1816
1817
|
|
|
1818
|
+
class ExpError extends SyntaxError {
|
|
1819
|
+
constructor(message, loc) {
|
|
1820
|
+
super(message);
|
|
1821
|
+
Object.defineProperty(this, "loc", {
|
|
1822
|
+
enumerable: true,
|
|
1823
|
+
configurable: true,
|
|
1824
|
+
writable: true,
|
|
1825
|
+
value: loc
|
|
1826
|
+
});
|
|
1827
|
+
this.name = 'ExpError';
|
|
1828
|
+
ExpError.captureStackTrace?.(this, this.constructor);
|
|
1829
|
+
}
|
|
1830
|
+
}
|
|
1831
|
+
|
|
1817
1832
|
function updatePosition(str, pos) {
|
|
1818
1833
|
const newlines = (str.match(/\n/g) || []).length;
|
|
1819
1834
|
if (newlines > 0) {
|
|
@@ -1862,14 +1877,22 @@ const expTokenTypes = {
|
|
|
1862
1877
|
',': 'COMMA',
|
|
1863
1878
|
'.': 'DOT',
|
|
1864
1879
|
};
|
|
1880
|
+
function normalizeLitValue(value) {
|
|
1881
|
+
switch (value) {
|
|
1882
|
+
case 'true':
|
|
1883
|
+
return true;
|
|
1884
|
+
case 'false':
|
|
1885
|
+
return false;
|
|
1886
|
+
case 'null':
|
|
1887
|
+
return null;
|
|
1888
|
+
case 'undefined':
|
|
1889
|
+
return undefined;
|
|
1890
|
+
default:
|
|
1891
|
+
return value;
|
|
1892
|
+
}
|
|
1893
|
+
}
|
|
1865
1894
|
class ExpTokenizer {
|
|
1866
|
-
constructor(
|
|
1867
|
-
Object.defineProperty(this, "template", {
|
|
1868
|
-
enumerable: true,
|
|
1869
|
-
configurable: true,
|
|
1870
|
-
writable: true,
|
|
1871
|
-
value: template
|
|
1872
|
-
});
|
|
1895
|
+
constructor() {
|
|
1873
1896
|
Object.defineProperty(this, "val", {
|
|
1874
1897
|
enumerable: true,
|
|
1875
1898
|
configurable: true,
|
|
@@ -1924,19 +1947,19 @@ class ExpTokenizer {
|
|
|
1924
1947
|
this.readString(char);
|
|
1925
1948
|
continue;
|
|
1926
1949
|
}
|
|
1927
|
-
if (
|
|
1928
|
-
this.readNumber();
|
|
1950
|
+
if (isDigitStart(char)) {
|
|
1951
|
+
this.readNumber(char);
|
|
1929
1952
|
continue;
|
|
1930
1953
|
}
|
|
1931
|
-
if (
|
|
1932
|
-
this.readIdentifier();
|
|
1954
|
+
if (isIdentifierStart(char)) {
|
|
1955
|
+
this.readIdentifier(char);
|
|
1933
1956
|
continue;
|
|
1934
1957
|
}
|
|
1935
1958
|
if (isSymbol(char)) {
|
|
1936
1959
|
this.readSymbol(char);
|
|
1937
1960
|
continue;
|
|
1938
1961
|
}
|
|
1939
|
-
throw new
|
|
1962
|
+
throw new ExpError(`Unexpect "${char}"`, {
|
|
1940
1963
|
start: { line: this.line, column: this.column },
|
|
1941
1964
|
end: { line: this.line, column: this.column + 1 },
|
|
1942
1965
|
});
|
|
@@ -1979,10 +2002,11 @@ class ExpTokenizer {
|
|
|
1979
2002
|
raw: `${quoteChar}${value}${quoteChar}`,
|
|
1980
2003
|
});
|
|
1981
2004
|
}
|
|
1982
|
-
readNumber() {
|
|
1983
|
-
let value =
|
|
2005
|
+
readNumber(startChar) {
|
|
2006
|
+
let value = startChar;
|
|
1984
2007
|
let hasDot = false;
|
|
1985
2008
|
const start = { line: this.line, column: this.column };
|
|
2009
|
+
this.index++;
|
|
1986
2010
|
while (this.index < this.val.length) {
|
|
1987
2011
|
const char = this.val[this.index];
|
|
1988
2012
|
if (isDigit(char)) {
|
|
@@ -2007,12 +2031,13 @@ class ExpTokenizer {
|
|
|
2007
2031
|
raw: value,
|
|
2008
2032
|
});
|
|
2009
2033
|
}
|
|
2010
|
-
readIdentifier() {
|
|
2011
|
-
let value =
|
|
2034
|
+
readIdentifier(startChar) {
|
|
2035
|
+
let value = startChar;
|
|
2012
2036
|
const start = { line: this.line, column: this.column };
|
|
2037
|
+
this.index++;
|
|
2013
2038
|
while (this.index < this.val.length) {
|
|
2014
2039
|
const char = this.val[this.index];
|
|
2015
|
-
if (
|
|
2040
|
+
if (isIdentifier(char)) {
|
|
2016
2041
|
value += char;
|
|
2017
2042
|
this.index++;
|
|
2018
2043
|
continue;
|
|
@@ -2024,17 +2049,7 @@ class ExpTokenizer {
|
|
|
2024
2049
|
: 'ID';
|
|
2025
2050
|
this.tokens.push({
|
|
2026
2051
|
type,
|
|
2027
|
-
value: type === 'LIT'
|
|
2028
|
-
? value === 'true'
|
|
2029
|
-
? true
|
|
2030
|
-
: value === 'false'
|
|
2031
|
-
? false
|
|
2032
|
-
: value === 'null'
|
|
2033
|
-
? null
|
|
2034
|
-
: value === 'undefined'
|
|
2035
|
-
? undefined
|
|
2036
|
-
: value
|
|
2037
|
-
: value,
|
|
2052
|
+
value: type === 'LIT' ? normalizeLitValue(value) : value,
|
|
2038
2053
|
loc: {
|
|
2039
2054
|
start,
|
|
2040
2055
|
end: updatePosition(value, this),
|
|
@@ -2062,13 +2077,16 @@ function isWhitespace(char) {
|
|
|
2062
2077
|
function isSymbol(char) {
|
|
2063
2078
|
return /[+\-*/%|=(),.]/.test(char);
|
|
2064
2079
|
}
|
|
2065
|
-
function
|
|
2080
|
+
function isDigitStart(char) {
|
|
2066
2081
|
return /[-\d]/.test(char);
|
|
2067
2082
|
}
|
|
2068
|
-
function
|
|
2083
|
+
function isDigit(char) {
|
|
2084
|
+
return /\d/.test(char);
|
|
2085
|
+
}
|
|
2086
|
+
function isIdentifierStart(char) {
|
|
2069
2087
|
return /[a-z_$]/i.test(char);
|
|
2070
2088
|
}
|
|
2071
|
-
function
|
|
2089
|
+
function isIdentifier(char) {
|
|
2072
2090
|
return /[\w$]/.test(char);
|
|
2073
2091
|
}
|
|
2074
2092
|
|
|
@@ -2103,13 +2121,7 @@ const expTokenPrecedences = {
|
|
|
2103
2121
|
LIT: 90,
|
|
2104
2122
|
};
|
|
2105
2123
|
class ExpParser {
|
|
2106
|
-
constructor(
|
|
2107
|
-
Object.defineProperty(this, "template", {
|
|
2108
|
-
enumerable: true,
|
|
2109
|
-
configurable: true,
|
|
2110
|
-
writable: true,
|
|
2111
|
-
value: template
|
|
2112
|
-
});
|
|
2124
|
+
constructor() {
|
|
2113
2125
|
Object.defineProperty(this, "val", {
|
|
2114
2126
|
enumerable: true,
|
|
2115
2127
|
configurable: true,
|
|
@@ -2146,7 +2158,7 @@ class ExpParser {
|
|
|
2146
2158
|
this.index = 0;
|
|
2147
2159
|
this.line = loc.start.line;
|
|
2148
2160
|
this.column = loc.start.column;
|
|
2149
|
-
this.tokens = new ExpTokenizer(
|
|
2161
|
+
this.tokens = new ExpTokenizer().tokenize(val, loc);
|
|
2150
2162
|
return this.val ? this.parseExp() : null;
|
|
2151
2163
|
}
|
|
2152
2164
|
parseExp(checker) {
|
|
@@ -2166,12 +2178,12 @@ class ExpParser {
|
|
|
2166
2178
|
}
|
|
2167
2179
|
if (token.type === 'LP') {
|
|
2168
2180
|
if (!this.peek()) {
|
|
2169
|
-
throw new
|
|
2181
|
+
throw new ExpError('Unexpected end of expression', token.loc);
|
|
2170
2182
|
}
|
|
2171
2183
|
const elements = this.parseSequence();
|
|
2172
2184
|
const rp = this.consume('RP');
|
|
2173
2185
|
if (!rp) {
|
|
2174
|
-
throw new
|
|
2186
|
+
throw new ExpError('Expected "RP" after "LP"', token.loc);
|
|
2175
2187
|
}
|
|
2176
2188
|
left = {
|
|
2177
2189
|
...token,
|
|
@@ -2186,7 +2198,7 @@ class ExpParser {
|
|
|
2186
2198
|
}
|
|
2187
2199
|
if (token.type === 'NOT') {
|
|
2188
2200
|
if (!this.peek()) {
|
|
2189
|
-
throw new
|
|
2201
|
+
throw new ExpError('Unexpected end of expression', token.loc);
|
|
2190
2202
|
}
|
|
2191
2203
|
left = {
|
|
2192
2204
|
...token,
|
|
@@ -2208,7 +2220,7 @@ class ExpParser {
|
|
|
2208
2220
|
if (lp) {
|
|
2209
2221
|
left.args = this.parseSequence();
|
|
2210
2222
|
if (!this.consume('RP')) {
|
|
2211
|
-
throw new
|
|
2223
|
+
throw new ExpError('Expected "RP" after "LP"', lp.loc);
|
|
2212
2224
|
}
|
|
2213
2225
|
continue;
|
|
2214
2226
|
}
|
|
@@ -2224,7 +2236,7 @@ class ExpParser {
|
|
|
2224
2236
|
if (token.type === 'IF') {
|
|
2225
2237
|
const test = this.parseExp((t) => t.type === 'ELSE' ? 'BACK' : isHigher(token, t) ? 'BACK' : undefined);
|
|
2226
2238
|
if (!test) {
|
|
2227
|
-
throw new
|
|
2239
|
+
throw new ExpError('Expected test expression', token.loc);
|
|
2228
2240
|
}
|
|
2229
2241
|
left = {
|
|
2230
2242
|
...token,
|
|
@@ -2235,7 +2247,7 @@ class ExpParser {
|
|
|
2235
2247
|
if (this.consume('ELSE')) {
|
|
2236
2248
|
const alternative = this.parseExp((t) => isHigher(token, t) ? 'BACK' : undefined);
|
|
2237
2249
|
if (!alternative) {
|
|
2238
|
-
throw new
|
|
2250
|
+
throw new ExpError('Expected alternative expression', token.loc);
|
|
2239
2251
|
}
|
|
2240
2252
|
left.alternative = alternative;
|
|
2241
2253
|
}
|
|
@@ -2260,11 +2272,11 @@ class ExpParser {
|
|
|
2260
2272
|
token.type === 'MOD' ||
|
|
2261
2273
|
token.type === 'ASSIGN') {
|
|
2262
2274
|
if (!left) {
|
|
2263
|
-
throw new
|
|
2275
|
+
throw new ExpError(`No left operand for "${token.type}"`, token.loc);
|
|
2264
2276
|
}
|
|
2265
2277
|
const right = this.parseExp((t) => isHigher(token, t) ? 'BACK' : undefined);
|
|
2266
2278
|
if (!right) {
|
|
2267
|
-
throw new
|
|
2279
|
+
throw new ExpError(`No right operand for "${token.type}"`, token.loc);
|
|
2268
2280
|
}
|
|
2269
2281
|
left = {
|
|
2270
2282
|
...token,
|
|
@@ -2276,10 +2288,10 @@ class ExpParser {
|
|
|
2276
2288
|
}
|
|
2277
2289
|
if (token.type === 'PIPE') {
|
|
2278
2290
|
if (!left) {
|
|
2279
|
-
throw new
|
|
2291
|
+
throw new ExpError(`No left operand for "${token.type}"`, token.loc);
|
|
2280
2292
|
}
|
|
2281
2293
|
if (!this.match('ID')) {
|
|
2282
|
-
throw new
|
|
2294
|
+
throw new ExpError('Expected "ID" after "PIPE"', token.loc);
|
|
2283
2295
|
}
|
|
2284
2296
|
left = {
|
|
2285
2297
|
...token,
|
|
@@ -2303,10 +2315,10 @@ class ExpParser {
|
|
|
2303
2315
|
}
|
|
2304
2316
|
const token = this.next();
|
|
2305
2317
|
if (!token) {
|
|
2306
|
-
throw new
|
|
2318
|
+
throw new ExpError('Expected "ID" after "DOT"', dot.loc);
|
|
2307
2319
|
}
|
|
2308
2320
|
if (token.type !== 'ID') {
|
|
2309
|
-
throw new
|
|
2321
|
+
throw new ExpError('Expected "ID" after "DOT"', dot.loc);
|
|
2310
2322
|
}
|
|
2311
2323
|
ids.push({
|
|
2312
2324
|
...token,
|
|
@@ -2343,7 +2355,7 @@ class ExpParser {
|
|
|
2343
2355
|
right.args = args;
|
|
2344
2356
|
}
|
|
2345
2357
|
if (!this.consume('RP')) {
|
|
2346
|
-
throw new
|
|
2358
|
+
throw new ExpError('Expected "RP" after "LP"', lp.loc);
|
|
2347
2359
|
}
|
|
2348
2360
|
}
|
|
2349
2361
|
return right;
|
|
@@ -2575,7 +2587,7 @@ class Parser extends Tokenizer {
|
|
|
2575
2587
|
this.tokenize(template);
|
|
2576
2588
|
const start = this.tokens[0]?.loc.start ?? { line: 1, column: 1 };
|
|
2577
2589
|
const end = this.tokens.at(-1)?.loc.end ?? { line: 1, column: 1 };
|
|
2578
|
-
this.expParser = new ExpParser(
|
|
2590
|
+
this.expParser = new ExpParser();
|
|
2579
2591
|
this.cursor = 0;
|
|
2580
2592
|
return new RootNode(await this.parseUntil(), { start, end });
|
|
2581
2593
|
}
|
|
@@ -2624,7 +2636,14 @@ class Parser extends Tokenizer {
|
|
|
2624
2636
|
return nodes;
|
|
2625
2637
|
}
|
|
2626
2638
|
parseExp({ val, loc }) {
|
|
2627
|
-
|
|
2639
|
+
try {
|
|
2640
|
+
return this.expParser.parse(val, loc);
|
|
2641
|
+
}
|
|
2642
|
+
catch (error) {
|
|
2643
|
+
this.options.debug(error.loc
|
|
2644
|
+
? new CompileError(error.message, this.template, error.loc)
|
|
2645
|
+
: error);
|
|
2646
|
+
}
|
|
2628
2647
|
}
|
|
2629
2648
|
peek() {
|
|
2630
2649
|
return this.tokens.at(this.cursor) ?? null;
|
|
@@ -2636,7 +2655,7 @@ class Parser extends Tokenizer {
|
|
|
2636
2655
|
return names.includes(token?.name);
|
|
2637
2656
|
}
|
|
2638
2657
|
emitExpErr({ name, loc }, required = true) {
|
|
2639
|
-
this.options.debug
|
|
2658
|
+
this.options.debug(new CompileError(required
|
|
2640
2659
|
? `"${name}" requires expression`
|
|
2641
2660
|
: `"${name}" should not have expression`, this.template, loc));
|
|
2642
2661
|
}
|
|
@@ -2735,7 +2754,7 @@ class Compiler extends Context {
|
|
|
2735
2754
|
template = await this.options.loader(path);
|
|
2736
2755
|
}
|
|
2737
2756
|
catch {
|
|
2738
|
-
this.options.debug
|
|
2757
|
+
this.options.debug(new CompileError(`Failed to load template from "${path}"`, this.template, node.loc));
|
|
2739
2758
|
return;
|
|
2740
2759
|
}
|
|
2741
2760
|
if (template) {
|
|
@@ -2796,7 +2815,7 @@ class Compiler extends Context {
|
|
|
2796
2815
|
this.partials.set(path, code);
|
|
2797
2816
|
}
|
|
2798
2817
|
catch {
|
|
2799
|
-
this.options.debug
|
|
2818
|
+
this.options.debug(new CompileError('Failed to compile template', this.template, node.loc));
|
|
2800
2819
|
}
|
|
2801
2820
|
}
|
|
2802
2821
|
}
|
|
@@ -2878,4 +2897,4 @@ async function renderFile(filepath, data, options) {
|
|
|
2878
2897
|
return new Renderer(options).renderFile(filepath, data);
|
|
2879
2898
|
}
|
|
2880
2899
|
|
|
2881
|
-
export { Renderer, render, renderFile };
|
|
2900
|
+
export { CompileError, Compiler, ExpCompiler, ExpError, ExpParser, ExpTokenizer, Parser, RenderError, Renderer, RootNode, Safe, Tokenizer, Traversal, expTokenTypes, render, renderFile };
|