janja 2.4.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/dist/index.cjs.js +88 -57
- package/dist/index.d.ts +81 -3
- package/dist/index.esm.js +76 -58
- package/dist/index.js +88 -57
- package/package.json +7 -7
package/dist/index.cjs.js
CHANGED
|
@@ -1817,6 +1817,20 @@ const mergeOptions = (defaultOptions, options) => {
|
|
|
1817
1817
|
return mergedOptions;
|
|
1818
1818
|
};
|
|
1819
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
|
+
|
|
1820
1834
|
function updatePosition(str, pos) {
|
|
1821
1835
|
const newlines = (str.match(/\n/g) || []).length;
|
|
1822
1836
|
if (newlines > 0) {
|
|
@@ -1865,14 +1879,22 @@ const expTokenTypes = {
|
|
|
1865
1879
|
',': 'COMMA',
|
|
1866
1880
|
'.': 'DOT',
|
|
1867
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
|
+
}
|
|
1868
1896
|
class ExpTokenizer {
|
|
1869
|
-
constructor(
|
|
1870
|
-
Object.defineProperty(this, "template", {
|
|
1871
|
-
enumerable: true,
|
|
1872
|
-
configurable: true,
|
|
1873
|
-
writable: true,
|
|
1874
|
-
value: template
|
|
1875
|
-
});
|
|
1897
|
+
constructor() {
|
|
1876
1898
|
Object.defineProperty(this, "val", {
|
|
1877
1899
|
enumerable: true,
|
|
1878
1900
|
configurable: true,
|
|
@@ -1927,19 +1949,19 @@ class ExpTokenizer {
|
|
|
1927
1949
|
this.readString(char);
|
|
1928
1950
|
continue;
|
|
1929
1951
|
}
|
|
1930
|
-
if (
|
|
1931
|
-
this.readNumber();
|
|
1952
|
+
if (isDigitStart(char)) {
|
|
1953
|
+
this.readNumber(char);
|
|
1932
1954
|
continue;
|
|
1933
1955
|
}
|
|
1934
|
-
if (
|
|
1935
|
-
this.readIdentifier();
|
|
1956
|
+
if (isIdentifierStart(char)) {
|
|
1957
|
+
this.readIdentifier(char);
|
|
1936
1958
|
continue;
|
|
1937
1959
|
}
|
|
1938
1960
|
if (isSymbol(char)) {
|
|
1939
1961
|
this.readSymbol(char);
|
|
1940
1962
|
continue;
|
|
1941
1963
|
}
|
|
1942
|
-
throw new
|
|
1964
|
+
throw new ExpError(`Unexpect "${char}"`, {
|
|
1943
1965
|
start: { line: this.line, column: this.column },
|
|
1944
1966
|
end: { line: this.line, column: this.column + 1 },
|
|
1945
1967
|
});
|
|
@@ -1982,10 +2004,11 @@ class ExpTokenizer {
|
|
|
1982
2004
|
raw: `${quoteChar}${value}${quoteChar}`,
|
|
1983
2005
|
});
|
|
1984
2006
|
}
|
|
1985
|
-
readNumber() {
|
|
1986
|
-
let value =
|
|
2007
|
+
readNumber(startChar) {
|
|
2008
|
+
let value = startChar;
|
|
1987
2009
|
let hasDot = false;
|
|
1988
2010
|
const start = { line: this.line, column: this.column };
|
|
2011
|
+
this.index++;
|
|
1989
2012
|
while (this.index < this.val.length) {
|
|
1990
2013
|
const char = this.val[this.index];
|
|
1991
2014
|
if (isDigit(char)) {
|
|
@@ -2010,12 +2033,13 @@ class ExpTokenizer {
|
|
|
2010
2033
|
raw: value,
|
|
2011
2034
|
});
|
|
2012
2035
|
}
|
|
2013
|
-
readIdentifier() {
|
|
2014
|
-
let value =
|
|
2036
|
+
readIdentifier(startChar) {
|
|
2037
|
+
let value = startChar;
|
|
2015
2038
|
const start = { line: this.line, column: this.column };
|
|
2039
|
+
this.index++;
|
|
2016
2040
|
while (this.index < this.val.length) {
|
|
2017
2041
|
const char = this.val[this.index];
|
|
2018
|
-
if (
|
|
2042
|
+
if (isIdentifier(char)) {
|
|
2019
2043
|
value += char;
|
|
2020
2044
|
this.index++;
|
|
2021
2045
|
continue;
|
|
@@ -2027,17 +2051,7 @@ class ExpTokenizer {
|
|
|
2027
2051
|
: 'ID';
|
|
2028
2052
|
this.tokens.push({
|
|
2029
2053
|
type,
|
|
2030
|
-
value: type === 'LIT'
|
|
2031
|
-
? value === 'true'
|
|
2032
|
-
? true
|
|
2033
|
-
: value === 'false'
|
|
2034
|
-
? false
|
|
2035
|
-
: value === 'null'
|
|
2036
|
-
? null
|
|
2037
|
-
: value === 'undefined'
|
|
2038
|
-
? undefined
|
|
2039
|
-
: value
|
|
2040
|
-
: value,
|
|
2054
|
+
value: type === 'LIT' ? normalizeLitValue(value) : value,
|
|
2041
2055
|
loc: {
|
|
2042
2056
|
start,
|
|
2043
2057
|
end: updatePosition(value, this),
|
|
@@ -2065,13 +2079,16 @@ function isWhitespace(char) {
|
|
|
2065
2079
|
function isSymbol(char) {
|
|
2066
2080
|
return /[+\-*/%|=(),.]/.test(char);
|
|
2067
2081
|
}
|
|
2068
|
-
function
|
|
2082
|
+
function isDigitStart(char) {
|
|
2069
2083
|
return /[-\d]/.test(char);
|
|
2070
2084
|
}
|
|
2071
|
-
function
|
|
2085
|
+
function isDigit(char) {
|
|
2086
|
+
return /\d/.test(char);
|
|
2087
|
+
}
|
|
2088
|
+
function isIdentifierStart(char) {
|
|
2072
2089
|
return /[a-z_$]/i.test(char);
|
|
2073
2090
|
}
|
|
2074
|
-
function
|
|
2091
|
+
function isIdentifier(char) {
|
|
2075
2092
|
return /[\w$]/.test(char);
|
|
2076
2093
|
}
|
|
2077
2094
|
|
|
@@ -2106,13 +2123,7 @@ const expTokenPrecedences = {
|
|
|
2106
2123
|
LIT: 90,
|
|
2107
2124
|
};
|
|
2108
2125
|
class ExpParser {
|
|
2109
|
-
constructor(
|
|
2110
|
-
Object.defineProperty(this, "template", {
|
|
2111
|
-
enumerable: true,
|
|
2112
|
-
configurable: true,
|
|
2113
|
-
writable: true,
|
|
2114
|
-
value: template
|
|
2115
|
-
});
|
|
2126
|
+
constructor() {
|
|
2116
2127
|
Object.defineProperty(this, "val", {
|
|
2117
2128
|
enumerable: true,
|
|
2118
2129
|
configurable: true,
|
|
@@ -2149,7 +2160,7 @@ class ExpParser {
|
|
|
2149
2160
|
this.index = 0;
|
|
2150
2161
|
this.line = loc.start.line;
|
|
2151
2162
|
this.column = loc.start.column;
|
|
2152
|
-
this.tokens = new ExpTokenizer(
|
|
2163
|
+
this.tokens = new ExpTokenizer().tokenize(val, loc);
|
|
2153
2164
|
return this.val ? this.parseExp() : null;
|
|
2154
2165
|
}
|
|
2155
2166
|
parseExp(checker) {
|
|
@@ -2169,12 +2180,12 @@ class ExpParser {
|
|
|
2169
2180
|
}
|
|
2170
2181
|
if (token.type === 'LP') {
|
|
2171
2182
|
if (!this.peek()) {
|
|
2172
|
-
throw new
|
|
2183
|
+
throw new ExpError('Unexpected end of expression', token.loc);
|
|
2173
2184
|
}
|
|
2174
2185
|
const elements = this.parseSequence();
|
|
2175
2186
|
const rp = this.consume('RP');
|
|
2176
2187
|
if (!rp) {
|
|
2177
|
-
throw new
|
|
2188
|
+
throw new ExpError('Expected "RP" after "LP"', token.loc);
|
|
2178
2189
|
}
|
|
2179
2190
|
left = {
|
|
2180
2191
|
...token,
|
|
@@ -2189,7 +2200,7 @@ class ExpParser {
|
|
|
2189
2200
|
}
|
|
2190
2201
|
if (token.type === 'NOT') {
|
|
2191
2202
|
if (!this.peek()) {
|
|
2192
|
-
throw new
|
|
2203
|
+
throw new ExpError('Unexpected end of expression', token.loc);
|
|
2193
2204
|
}
|
|
2194
2205
|
left = {
|
|
2195
2206
|
...token,
|
|
@@ -2211,7 +2222,7 @@ class ExpParser {
|
|
|
2211
2222
|
if (lp) {
|
|
2212
2223
|
left.args = this.parseSequence();
|
|
2213
2224
|
if (!this.consume('RP')) {
|
|
2214
|
-
throw new
|
|
2225
|
+
throw new ExpError('Expected "RP" after "LP"', lp.loc);
|
|
2215
2226
|
}
|
|
2216
2227
|
continue;
|
|
2217
2228
|
}
|
|
@@ -2227,7 +2238,7 @@ class ExpParser {
|
|
|
2227
2238
|
if (token.type === 'IF') {
|
|
2228
2239
|
const test = this.parseExp((t) => t.type === 'ELSE' ? 'BACK' : isHigher(token, t) ? 'BACK' : undefined);
|
|
2229
2240
|
if (!test) {
|
|
2230
|
-
throw new
|
|
2241
|
+
throw new ExpError('Expected test expression', token.loc);
|
|
2231
2242
|
}
|
|
2232
2243
|
left = {
|
|
2233
2244
|
...token,
|
|
@@ -2238,7 +2249,7 @@ class ExpParser {
|
|
|
2238
2249
|
if (this.consume('ELSE')) {
|
|
2239
2250
|
const alternative = this.parseExp((t) => isHigher(token, t) ? 'BACK' : undefined);
|
|
2240
2251
|
if (!alternative) {
|
|
2241
|
-
throw new
|
|
2252
|
+
throw new ExpError('Expected alternative expression', token.loc);
|
|
2242
2253
|
}
|
|
2243
2254
|
left.alternative = alternative;
|
|
2244
2255
|
}
|
|
@@ -2263,11 +2274,11 @@ class ExpParser {
|
|
|
2263
2274
|
token.type === 'MOD' ||
|
|
2264
2275
|
token.type === 'ASSIGN') {
|
|
2265
2276
|
if (!left) {
|
|
2266
|
-
throw new
|
|
2277
|
+
throw new ExpError(`No left operand for "${token.type}"`, token.loc);
|
|
2267
2278
|
}
|
|
2268
2279
|
const right = this.parseExp((t) => isHigher(token, t) ? 'BACK' : undefined);
|
|
2269
2280
|
if (!right) {
|
|
2270
|
-
throw new
|
|
2281
|
+
throw new ExpError(`No right operand for "${token.type}"`, token.loc);
|
|
2271
2282
|
}
|
|
2272
2283
|
left = {
|
|
2273
2284
|
...token,
|
|
@@ -2279,10 +2290,10 @@ class ExpParser {
|
|
|
2279
2290
|
}
|
|
2280
2291
|
if (token.type === 'PIPE') {
|
|
2281
2292
|
if (!left) {
|
|
2282
|
-
throw new
|
|
2293
|
+
throw new ExpError(`No left operand for "${token.type}"`, token.loc);
|
|
2283
2294
|
}
|
|
2284
2295
|
if (!this.match('ID')) {
|
|
2285
|
-
throw new
|
|
2296
|
+
throw new ExpError('Expected "ID" after "PIPE"', token.loc);
|
|
2286
2297
|
}
|
|
2287
2298
|
left = {
|
|
2288
2299
|
...token,
|
|
@@ -2306,10 +2317,10 @@ class ExpParser {
|
|
|
2306
2317
|
}
|
|
2307
2318
|
const token = this.next();
|
|
2308
2319
|
if (!token) {
|
|
2309
|
-
throw new
|
|
2320
|
+
throw new ExpError('Expected "ID" after "DOT"', dot.loc);
|
|
2310
2321
|
}
|
|
2311
2322
|
if (token.type !== 'ID') {
|
|
2312
|
-
throw new
|
|
2323
|
+
throw new ExpError('Expected "ID" after "DOT"', dot.loc);
|
|
2313
2324
|
}
|
|
2314
2325
|
ids.push({
|
|
2315
2326
|
...token,
|
|
@@ -2346,7 +2357,7 @@ class ExpParser {
|
|
|
2346
2357
|
right.args = args;
|
|
2347
2358
|
}
|
|
2348
2359
|
if (!this.consume('RP')) {
|
|
2349
|
-
throw new
|
|
2360
|
+
throw new ExpError('Expected "RP" after "LP"', lp.loc);
|
|
2350
2361
|
}
|
|
2351
2362
|
}
|
|
2352
2363
|
return right;
|
|
@@ -2578,7 +2589,7 @@ class Parser extends Tokenizer {
|
|
|
2578
2589
|
this.tokenize(template);
|
|
2579
2590
|
const start = this.tokens[0]?.loc.start ?? { line: 1, column: 1 };
|
|
2580
2591
|
const end = this.tokens.at(-1)?.loc.end ?? { line: 1, column: 1 };
|
|
2581
|
-
this.expParser = new ExpParser(
|
|
2592
|
+
this.expParser = new ExpParser();
|
|
2582
2593
|
this.cursor = 0;
|
|
2583
2594
|
return new RootNode(await this.parseUntil(), { start, end });
|
|
2584
2595
|
}
|
|
@@ -2627,7 +2638,14 @@ class Parser extends Tokenizer {
|
|
|
2627
2638
|
return nodes;
|
|
2628
2639
|
}
|
|
2629
2640
|
parseExp({ val, loc }) {
|
|
2630
|
-
|
|
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
|
+
}
|
|
2631
2649
|
}
|
|
2632
2650
|
peek() {
|
|
2633
2651
|
return this.tokens.at(this.cursor) ?? null;
|
|
@@ -2639,7 +2657,7 @@ class Parser extends Tokenizer {
|
|
|
2639
2657
|
return names.includes(token?.name);
|
|
2640
2658
|
}
|
|
2641
2659
|
emitExpErr({ name, loc }, required = true) {
|
|
2642
|
-
this.options.debug
|
|
2660
|
+
this.options.debug(new CompileError(required
|
|
2643
2661
|
? `"${name}" requires expression`
|
|
2644
2662
|
: `"${name}" should not have expression`, this.template, loc));
|
|
2645
2663
|
}
|
|
@@ -2738,7 +2756,7 @@ class Compiler extends Context {
|
|
|
2738
2756
|
template = await this.options.loader(path);
|
|
2739
2757
|
}
|
|
2740
2758
|
catch {
|
|
2741
|
-
this.options.debug
|
|
2759
|
+
this.options.debug(new CompileError(`Failed to load template from "${path}"`, this.template, node.loc));
|
|
2742
2760
|
return;
|
|
2743
2761
|
}
|
|
2744
2762
|
if (template) {
|
|
@@ -2799,7 +2817,7 @@ class Compiler extends Context {
|
|
|
2799
2817
|
this.partials.set(path, code);
|
|
2800
2818
|
}
|
|
2801
2819
|
catch {
|
|
2802
|
-
this.options.debug
|
|
2820
|
+
this.options.debug(new CompileError('Failed to compile template', this.template, node.loc));
|
|
2803
2821
|
}
|
|
2804
2822
|
}
|
|
2805
2823
|
}
|
|
@@ -2881,6 +2899,19 @@ async function renderFile(filepath, data, options) {
|
|
|
2881
2899
|
return new Renderer(options).renderFile(filepath, data);
|
|
2882
2900
|
}
|
|
2883
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;
|
|
2884
2910
|
exports.Renderer = Renderer;
|
|
2911
|
+
exports.RootNode = RootNode;
|
|
2912
|
+
exports.Safe = Safe;
|
|
2913
|
+
exports.Tokenizer = Tokenizer;
|
|
2914
|
+
exports.Traversal = Traversal;
|
|
2915
|
+
exports.expTokenTypes = expTokenTypes;
|
|
2885
2916
|
exports.render = render;
|
|
2886
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 };
|
package/dist/index.esm.js
CHANGED
|
@@ -1815,6 +1815,20 @@ const mergeOptions = (defaultOptions, options) => {
|
|
|
1815
1815
|
return mergedOptions;
|
|
1816
1816
|
};
|
|
1817
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
|
+
|
|
1818
1832
|
function updatePosition(str, pos) {
|
|
1819
1833
|
const newlines = (str.match(/\n/g) || []).length;
|
|
1820
1834
|
if (newlines > 0) {
|
|
@@ -1863,14 +1877,22 @@ const expTokenTypes = {
|
|
|
1863
1877
|
',': 'COMMA',
|
|
1864
1878
|
'.': 'DOT',
|
|
1865
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
|
+
}
|
|
1866
1894
|
class ExpTokenizer {
|
|
1867
|
-
constructor(
|
|
1868
|
-
Object.defineProperty(this, "template", {
|
|
1869
|
-
enumerable: true,
|
|
1870
|
-
configurable: true,
|
|
1871
|
-
writable: true,
|
|
1872
|
-
value: template
|
|
1873
|
-
});
|
|
1895
|
+
constructor() {
|
|
1874
1896
|
Object.defineProperty(this, "val", {
|
|
1875
1897
|
enumerable: true,
|
|
1876
1898
|
configurable: true,
|
|
@@ -1925,19 +1947,19 @@ class ExpTokenizer {
|
|
|
1925
1947
|
this.readString(char);
|
|
1926
1948
|
continue;
|
|
1927
1949
|
}
|
|
1928
|
-
if (
|
|
1929
|
-
this.readNumber();
|
|
1950
|
+
if (isDigitStart(char)) {
|
|
1951
|
+
this.readNumber(char);
|
|
1930
1952
|
continue;
|
|
1931
1953
|
}
|
|
1932
|
-
if (
|
|
1933
|
-
this.readIdentifier();
|
|
1954
|
+
if (isIdentifierStart(char)) {
|
|
1955
|
+
this.readIdentifier(char);
|
|
1934
1956
|
continue;
|
|
1935
1957
|
}
|
|
1936
1958
|
if (isSymbol(char)) {
|
|
1937
1959
|
this.readSymbol(char);
|
|
1938
1960
|
continue;
|
|
1939
1961
|
}
|
|
1940
|
-
throw new
|
|
1962
|
+
throw new ExpError(`Unexpect "${char}"`, {
|
|
1941
1963
|
start: { line: this.line, column: this.column },
|
|
1942
1964
|
end: { line: this.line, column: this.column + 1 },
|
|
1943
1965
|
});
|
|
@@ -1980,10 +2002,11 @@ class ExpTokenizer {
|
|
|
1980
2002
|
raw: `${quoteChar}${value}${quoteChar}`,
|
|
1981
2003
|
});
|
|
1982
2004
|
}
|
|
1983
|
-
readNumber() {
|
|
1984
|
-
let value =
|
|
2005
|
+
readNumber(startChar) {
|
|
2006
|
+
let value = startChar;
|
|
1985
2007
|
let hasDot = false;
|
|
1986
2008
|
const start = { line: this.line, column: this.column };
|
|
2009
|
+
this.index++;
|
|
1987
2010
|
while (this.index < this.val.length) {
|
|
1988
2011
|
const char = this.val[this.index];
|
|
1989
2012
|
if (isDigit(char)) {
|
|
@@ -2008,12 +2031,13 @@ class ExpTokenizer {
|
|
|
2008
2031
|
raw: value,
|
|
2009
2032
|
});
|
|
2010
2033
|
}
|
|
2011
|
-
readIdentifier() {
|
|
2012
|
-
let value =
|
|
2034
|
+
readIdentifier(startChar) {
|
|
2035
|
+
let value = startChar;
|
|
2013
2036
|
const start = { line: this.line, column: this.column };
|
|
2037
|
+
this.index++;
|
|
2014
2038
|
while (this.index < this.val.length) {
|
|
2015
2039
|
const char = this.val[this.index];
|
|
2016
|
-
if (
|
|
2040
|
+
if (isIdentifier(char)) {
|
|
2017
2041
|
value += char;
|
|
2018
2042
|
this.index++;
|
|
2019
2043
|
continue;
|
|
@@ -2025,17 +2049,7 @@ class ExpTokenizer {
|
|
|
2025
2049
|
: 'ID';
|
|
2026
2050
|
this.tokens.push({
|
|
2027
2051
|
type,
|
|
2028
|
-
value: type === 'LIT'
|
|
2029
|
-
? value === 'true'
|
|
2030
|
-
? true
|
|
2031
|
-
: value === 'false'
|
|
2032
|
-
? false
|
|
2033
|
-
: value === 'null'
|
|
2034
|
-
? null
|
|
2035
|
-
: value === 'undefined'
|
|
2036
|
-
? undefined
|
|
2037
|
-
: value
|
|
2038
|
-
: value,
|
|
2052
|
+
value: type === 'LIT' ? normalizeLitValue(value) : value,
|
|
2039
2053
|
loc: {
|
|
2040
2054
|
start,
|
|
2041
2055
|
end: updatePosition(value, this),
|
|
@@ -2063,13 +2077,16 @@ function isWhitespace(char) {
|
|
|
2063
2077
|
function isSymbol(char) {
|
|
2064
2078
|
return /[+\-*/%|=(),.]/.test(char);
|
|
2065
2079
|
}
|
|
2066
|
-
function
|
|
2080
|
+
function isDigitStart(char) {
|
|
2067
2081
|
return /[-\d]/.test(char);
|
|
2068
2082
|
}
|
|
2069
|
-
function
|
|
2083
|
+
function isDigit(char) {
|
|
2084
|
+
return /\d/.test(char);
|
|
2085
|
+
}
|
|
2086
|
+
function isIdentifierStart(char) {
|
|
2070
2087
|
return /[a-z_$]/i.test(char);
|
|
2071
2088
|
}
|
|
2072
|
-
function
|
|
2089
|
+
function isIdentifier(char) {
|
|
2073
2090
|
return /[\w$]/.test(char);
|
|
2074
2091
|
}
|
|
2075
2092
|
|
|
@@ -2104,13 +2121,7 @@ const expTokenPrecedences = {
|
|
|
2104
2121
|
LIT: 90,
|
|
2105
2122
|
};
|
|
2106
2123
|
class ExpParser {
|
|
2107
|
-
constructor(
|
|
2108
|
-
Object.defineProperty(this, "template", {
|
|
2109
|
-
enumerable: true,
|
|
2110
|
-
configurable: true,
|
|
2111
|
-
writable: true,
|
|
2112
|
-
value: template
|
|
2113
|
-
});
|
|
2124
|
+
constructor() {
|
|
2114
2125
|
Object.defineProperty(this, "val", {
|
|
2115
2126
|
enumerable: true,
|
|
2116
2127
|
configurable: true,
|
|
@@ -2147,7 +2158,7 @@ class ExpParser {
|
|
|
2147
2158
|
this.index = 0;
|
|
2148
2159
|
this.line = loc.start.line;
|
|
2149
2160
|
this.column = loc.start.column;
|
|
2150
|
-
this.tokens = new ExpTokenizer(
|
|
2161
|
+
this.tokens = new ExpTokenizer().tokenize(val, loc);
|
|
2151
2162
|
return this.val ? this.parseExp() : null;
|
|
2152
2163
|
}
|
|
2153
2164
|
parseExp(checker) {
|
|
@@ -2167,12 +2178,12 @@ class ExpParser {
|
|
|
2167
2178
|
}
|
|
2168
2179
|
if (token.type === 'LP') {
|
|
2169
2180
|
if (!this.peek()) {
|
|
2170
|
-
throw new
|
|
2181
|
+
throw new ExpError('Unexpected end of expression', token.loc);
|
|
2171
2182
|
}
|
|
2172
2183
|
const elements = this.parseSequence();
|
|
2173
2184
|
const rp = this.consume('RP');
|
|
2174
2185
|
if (!rp) {
|
|
2175
|
-
throw new
|
|
2186
|
+
throw new ExpError('Expected "RP" after "LP"', token.loc);
|
|
2176
2187
|
}
|
|
2177
2188
|
left = {
|
|
2178
2189
|
...token,
|
|
@@ -2187,7 +2198,7 @@ class ExpParser {
|
|
|
2187
2198
|
}
|
|
2188
2199
|
if (token.type === 'NOT') {
|
|
2189
2200
|
if (!this.peek()) {
|
|
2190
|
-
throw new
|
|
2201
|
+
throw new ExpError('Unexpected end of expression', token.loc);
|
|
2191
2202
|
}
|
|
2192
2203
|
left = {
|
|
2193
2204
|
...token,
|
|
@@ -2209,7 +2220,7 @@ class ExpParser {
|
|
|
2209
2220
|
if (lp) {
|
|
2210
2221
|
left.args = this.parseSequence();
|
|
2211
2222
|
if (!this.consume('RP')) {
|
|
2212
|
-
throw new
|
|
2223
|
+
throw new ExpError('Expected "RP" after "LP"', lp.loc);
|
|
2213
2224
|
}
|
|
2214
2225
|
continue;
|
|
2215
2226
|
}
|
|
@@ -2225,7 +2236,7 @@ class ExpParser {
|
|
|
2225
2236
|
if (token.type === 'IF') {
|
|
2226
2237
|
const test = this.parseExp((t) => t.type === 'ELSE' ? 'BACK' : isHigher(token, t) ? 'BACK' : undefined);
|
|
2227
2238
|
if (!test) {
|
|
2228
|
-
throw new
|
|
2239
|
+
throw new ExpError('Expected test expression', token.loc);
|
|
2229
2240
|
}
|
|
2230
2241
|
left = {
|
|
2231
2242
|
...token,
|
|
@@ -2236,7 +2247,7 @@ class ExpParser {
|
|
|
2236
2247
|
if (this.consume('ELSE')) {
|
|
2237
2248
|
const alternative = this.parseExp((t) => isHigher(token, t) ? 'BACK' : undefined);
|
|
2238
2249
|
if (!alternative) {
|
|
2239
|
-
throw new
|
|
2250
|
+
throw new ExpError('Expected alternative expression', token.loc);
|
|
2240
2251
|
}
|
|
2241
2252
|
left.alternative = alternative;
|
|
2242
2253
|
}
|
|
@@ -2261,11 +2272,11 @@ class ExpParser {
|
|
|
2261
2272
|
token.type === 'MOD' ||
|
|
2262
2273
|
token.type === 'ASSIGN') {
|
|
2263
2274
|
if (!left) {
|
|
2264
|
-
throw new
|
|
2275
|
+
throw new ExpError(`No left operand for "${token.type}"`, token.loc);
|
|
2265
2276
|
}
|
|
2266
2277
|
const right = this.parseExp((t) => isHigher(token, t) ? 'BACK' : undefined);
|
|
2267
2278
|
if (!right) {
|
|
2268
|
-
throw new
|
|
2279
|
+
throw new ExpError(`No right operand for "${token.type}"`, token.loc);
|
|
2269
2280
|
}
|
|
2270
2281
|
left = {
|
|
2271
2282
|
...token,
|
|
@@ -2277,10 +2288,10 @@ class ExpParser {
|
|
|
2277
2288
|
}
|
|
2278
2289
|
if (token.type === 'PIPE') {
|
|
2279
2290
|
if (!left) {
|
|
2280
|
-
throw new
|
|
2291
|
+
throw new ExpError(`No left operand for "${token.type}"`, token.loc);
|
|
2281
2292
|
}
|
|
2282
2293
|
if (!this.match('ID')) {
|
|
2283
|
-
throw new
|
|
2294
|
+
throw new ExpError('Expected "ID" after "PIPE"', token.loc);
|
|
2284
2295
|
}
|
|
2285
2296
|
left = {
|
|
2286
2297
|
...token,
|
|
@@ -2304,10 +2315,10 @@ class ExpParser {
|
|
|
2304
2315
|
}
|
|
2305
2316
|
const token = this.next();
|
|
2306
2317
|
if (!token) {
|
|
2307
|
-
throw new
|
|
2318
|
+
throw new ExpError('Expected "ID" after "DOT"', dot.loc);
|
|
2308
2319
|
}
|
|
2309
2320
|
if (token.type !== 'ID') {
|
|
2310
|
-
throw new
|
|
2321
|
+
throw new ExpError('Expected "ID" after "DOT"', dot.loc);
|
|
2311
2322
|
}
|
|
2312
2323
|
ids.push({
|
|
2313
2324
|
...token,
|
|
@@ -2344,7 +2355,7 @@ class ExpParser {
|
|
|
2344
2355
|
right.args = args;
|
|
2345
2356
|
}
|
|
2346
2357
|
if (!this.consume('RP')) {
|
|
2347
|
-
throw new
|
|
2358
|
+
throw new ExpError('Expected "RP" after "LP"', lp.loc);
|
|
2348
2359
|
}
|
|
2349
2360
|
}
|
|
2350
2361
|
return right;
|
|
@@ -2576,7 +2587,7 @@ class Parser extends Tokenizer {
|
|
|
2576
2587
|
this.tokenize(template);
|
|
2577
2588
|
const start = this.tokens[0]?.loc.start ?? { line: 1, column: 1 };
|
|
2578
2589
|
const end = this.tokens.at(-1)?.loc.end ?? { line: 1, column: 1 };
|
|
2579
|
-
this.expParser = new ExpParser(
|
|
2590
|
+
this.expParser = new ExpParser();
|
|
2580
2591
|
this.cursor = 0;
|
|
2581
2592
|
return new RootNode(await this.parseUntil(), { start, end });
|
|
2582
2593
|
}
|
|
@@ -2625,7 +2636,14 @@ class Parser extends Tokenizer {
|
|
|
2625
2636
|
return nodes;
|
|
2626
2637
|
}
|
|
2627
2638
|
parseExp({ val, loc }) {
|
|
2628
|
-
|
|
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
|
+
}
|
|
2629
2647
|
}
|
|
2630
2648
|
peek() {
|
|
2631
2649
|
return this.tokens.at(this.cursor) ?? null;
|
|
@@ -2637,7 +2655,7 @@ class Parser extends Tokenizer {
|
|
|
2637
2655
|
return names.includes(token?.name);
|
|
2638
2656
|
}
|
|
2639
2657
|
emitExpErr({ name, loc }, required = true) {
|
|
2640
|
-
this.options.debug
|
|
2658
|
+
this.options.debug(new CompileError(required
|
|
2641
2659
|
? `"${name}" requires expression`
|
|
2642
2660
|
: `"${name}" should not have expression`, this.template, loc));
|
|
2643
2661
|
}
|
|
@@ -2736,7 +2754,7 @@ class Compiler extends Context {
|
|
|
2736
2754
|
template = await this.options.loader(path);
|
|
2737
2755
|
}
|
|
2738
2756
|
catch {
|
|
2739
|
-
this.options.debug
|
|
2757
|
+
this.options.debug(new CompileError(`Failed to load template from "${path}"`, this.template, node.loc));
|
|
2740
2758
|
return;
|
|
2741
2759
|
}
|
|
2742
2760
|
if (template) {
|
|
@@ -2797,7 +2815,7 @@ class Compiler extends Context {
|
|
|
2797
2815
|
this.partials.set(path, code);
|
|
2798
2816
|
}
|
|
2799
2817
|
catch {
|
|
2800
|
-
this.options.debug
|
|
2818
|
+
this.options.debug(new CompileError('Failed to compile template', this.template, node.loc));
|
|
2801
2819
|
}
|
|
2802
2820
|
}
|
|
2803
2821
|
}
|
|
@@ -2879,4 +2897,4 @@ async function renderFile(filepath, data, options) {
|
|
|
2879
2897
|
return new Renderer(options).renderFile(filepath, data);
|
|
2880
2898
|
}
|
|
2881
2899
|
|
|
2882
|
-
export { Renderer, render, renderFile };
|
|
2900
|
+
export { CompileError, Compiler, ExpCompiler, ExpError, ExpParser, ExpTokenizer, Parser, RenderError, Renderer, RootNode, Safe, Tokenizer, Traversal, expTokenTypes, render, renderFile };
|
package/dist/index.js
CHANGED
|
@@ -1818,6 +1818,20 @@ ${output
|
|
|
1818
1818
|
return mergedOptions;
|
|
1819
1819
|
};
|
|
1820
1820
|
|
|
1821
|
+
class ExpError extends SyntaxError {
|
|
1822
|
+
constructor(message, loc) {
|
|
1823
|
+
super(message);
|
|
1824
|
+
Object.defineProperty(this, "loc", {
|
|
1825
|
+
enumerable: true,
|
|
1826
|
+
configurable: true,
|
|
1827
|
+
writable: true,
|
|
1828
|
+
value: loc
|
|
1829
|
+
});
|
|
1830
|
+
this.name = 'ExpError';
|
|
1831
|
+
ExpError.captureStackTrace?.(this, this.constructor);
|
|
1832
|
+
}
|
|
1833
|
+
}
|
|
1834
|
+
|
|
1821
1835
|
function updatePosition(str, pos) {
|
|
1822
1836
|
const newlines = (str.match(/\n/g) || []).length;
|
|
1823
1837
|
if (newlines > 0) {
|
|
@@ -1866,14 +1880,22 @@ ${output
|
|
|
1866
1880
|
',': 'COMMA',
|
|
1867
1881
|
'.': 'DOT',
|
|
1868
1882
|
};
|
|
1883
|
+
function normalizeLitValue(value) {
|
|
1884
|
+
switch (value) {
|
|
1885
|
+
case 'true':
|
|
1886
|
+
return true;
|
|
1887
|
+
case 'false':
|
|
1888
|
+
return false;
|
|
1889
|
+
case 'null':
|
|
1890
|
+
return null;
|
|
1891
|
+
case 'undefined':
|
|
1892
|
+
return undefined;
|
|
1893
|
+
default:
|
|
1894
|
+
return value;
|
|
1895
|
+
}
|
|
1896
|
+
}
|
|
1869
1897
|
class ExpTokenizer {
|
|
1870
|
-
constructor(
|
|
1871
|
-
Object.defineProperty(this, "template", {
|
|
1872
|
-
enumerable: true,
|
|
1873
|
-
configurable: true,
|
|
1874
|
-
writable: true,
|
|
1875
|
-
value: template
|
|
1876
|
-
});
|
|
1898
|
+
constructor() {
|
|
1877
1899
|
Object.defineProperty(this, "val", {
|
|
1878
1900
|
enumerable: true,
|
|
1879
1901
|
configurable: true,
|
|
@@ -1928,19 +1950,19 @@ ${output
|
|
|
1928
1950
|
this.readString(char);
|
|
1929
1951
|
continue;
|
|
1930
1952
|
}
|
|
1931
|
-
if (
|
|
1932
|
-
this.readNumber();
|
|
1953
|
+
if (isDigitStart(char)) {
|
|
1954
|
+
this.readNumber(char);
|
|
1933
1955
|
continue;
|
|
1934
1956
|
}
|
|
1935
|
-
if (
|
|
1936
|
-
this.readIdentifier();
|
|
1957
|
+
if (isIdentifierStart(char)) {
|
|
1958
|
+
this.readIdentifier(char);
|
|
1937
1959
|
continue;
|
|
1938
1960
|
}
|
|
1939
1961
|
if (isSymbol(char)) {
|
|
1940
1962
|
this.readSymbol(char);
|
|
1941
1963
|
continue;
|
|
1942
1964
|
}
|
|
1943
|
-
throw new
|
|
1965
|
+
throw new ExpError(`Unexpect "${char}"`, {
|
|
1944
1966
|
start: { line: this.line, column: this.column },
|
|
1945
1967
|
end: { line: this.line, column: this.column + 1 },
|
|
1946
1968
|
});
|
|
@@ -1983,10 +2005,11 @@ ${output
|
|
|
1983
2005
|
raw: `${quoteChar}${value}${quoteChar}`,
|
|
1984
2006
|
});
|
|
1985
2007
|
}
|
|
1986
|
-
readNumber() {
|
|
1987
|
-
let value =
|
|
2008
|
+
readNumber(startChar) {
|
|
2009
|
+
let value = startChar;
|
|
1988
2010
|
let hasDot = false;
|
|
1989
2011
|
const start = { line: this.line, column: this.column };
|
|
2012
|
+
this.index++;
|
|
1990
2013
|
while (this.index < this.val.length) {
|
|
1991
2014
|
const char = this.val[this.index];
|
|
1992
2015
|
if (isDigit(char)) {
|
|
@@ -2011,12 +2034,13 @@ ${output
|
|
|
2011
2034
|
raw: value,
|
|
2012
2035
|
});
|
|
2013
2036
|
}
|
|
2014
|
-
readIdentifier() {
|
|
2015
|
-
let value =
|
|
2037
|
+
readIdentifier(startChar) {
|
|
2038
|
+
let value = startChar;
|
|
2016
2039
|
const start = { line: this.line, column: this.column };
|
|
2040
|
+
this.index++;
|
|
2017
2041
|
while (this.index < this.val.length) {
|
|
2018
2042
|
const char = this.val[this.index];
|
|
2019
|
-
if (
|
|
2043
|
+
if (isIdentifier(char)) {
|
|
2020
2044
|
value += char;
|
|
2021
2045
|
this.index++;
|
|
2022
2046
|
continue;
|
|
@@ -2028,17 +2052,7 @@ ${output
|
|
|
2028
2052
|
: 'ID';
|
|
2029
2053
|
this.tokens.push({
|
|
2030
2054
|
type,
|
|
2031
|
-
value: type === 'LIT'
|
|
2032
|
-
? value === 'true'
|
|
2033
|
-
? true
|
|
2034
|
-
: value === 'false'
|
|
2035
|
-
? false
|
|
2036
|
-
: value === 'null'
|
|
2037
|
-
? null
|
|
2038
|
-
: value === 'undefined'
|
|
2039
|
-
? undefined
|
|
2040
|
-
: value
|
|
2041
|
-
: value,
|
|
2055
|
+
value: type === 'LIT' ? normalizeLitValue(value) : value,
|
|
2042
2056
|
loc: {
|
|
2043
2057
|
start,
|
|
2044
2058
|
end: updatePosition(value, this),
|
|
@@ -2066,13 +2080,16 @@ ${output
|
|
|
2066
2080
|
function isSymbol(char) {
|
|
2067
2081
|
return /[+\-*/%|=(),.]/.test(char);
|
|
2068
2082
|
}
|
|
2069
|
-
function
|
|
2083
|
+
function isDigitStart(char) {
|
|
2070
2084
|
return /[-\d]/.test(char);
|
|
2071
2085
|
}
|
|
2072
|
-
function
|
|
2086
|
+
function isDigit(char) {
|
|
2087
|
+
return /\d/.test(char);
|
|
2088
|
+
}
|
|
2089
|
+
function isIdentifierStart(char) {
|
|
2073
2090
|
return /[a-z_$]/i.test(char);
|
|
2074
2091
|
}
|
|
2075
|
-
function
|
|
2092
|
+
function isIdentifier(char) {
|
|
2076
2093
|
return /[\w$]/.test(char);
|
|
2077
2094
|
}
|
|
2078
2095
|
|
|
@@ -2107,13 +2124,7 @@ ${output
|
|
|
2107
2124
|
LIT: 90,
|
|
2108
2125
|
};
|
|
2109
2126
|
class ExpParser {
|
|
2110
|
-
constructor(
|
|
2111
|
-
Object.defineProperty(this, "template", {
|
|
2112
|
-
enumerable: true,
|
|
2113
|
-
configurable: true,
|
|
2114
|
-
writable: true,
|
|
2115
|
-
value: template
|
|
2116
|
-
});
|
|
2127
|
+
constructor() {
|
|
2117
2128
|
Object.defineProperty(this, "val", {
|
|
2118
2129
|
enumerable: true,
|
|
2119
2130
|
configurable: true,
|
|
@@ -2150,7 +2161,7 @@ ${output
|
|
|
2150
2161
|
this.index = 0;
|
|
2151
2162
|
this.line = loc.start.line;
|
|
2152
2163
|
this.column = loc.start.column;
|
|
2153
|
-
this.tokens = new ExpTokenizer(
|
|
2164
|
+
this.tokens = new ExpTokenizer().tokenize(val, loc);
|
|
2154
2165
|
return this.val ? this.parseExp() : null;
|
|
2155
2166
|
}
|
|
2156
2167
|
parseExp(checker) {
|
|
@@ -2170,12 +2181,12 @@ ${output
|
|
|
2170
2181
|
}
|
|
2171
2182
|
if (token.type === 'LP') {
|
|
2172
2183
|
if (!this.peek()) {
|
|
2173
|
-
throw new
|
|
2184
|
+
throw new ExpError('Unexpected end of expression', token.loc);
|
|
2174
2185
|
}
|
|
2175
2186
|
const elements = this.parseSequence();
|
|
2176
2187
|
const rp = this.consume('RP');
|
|
2177
2188
|
if (!rp) {
|
|
2178
|
-
throw new
|
|
2189
|
+
throw new ExpError('Expected "RP" after "LP"', token.loc);
|
|
2179
2190
|
}
|
|
2180
2191
|
left = {
|
|
2181
2192
|
...token,
|
|
@@ -2190,7 +2201,7 @@ ${output
|
|
|
2190
2201
|
}
|
|
2191
2202
|
if (token.type === 'NOT') {
|
|
2192
2203
|
if (!this.peek()) {
|
|
2193
|
-
throw new
|
|
2204
|
+
throw new ExpError('Unexpected end of expression', token.loc);
|
|
2194
2205
|
}
|
|
2195
2206
|
left = {
|
|
2196
2207
|
...token,
|
|
@@ -2212,7 +2223,7 @@ ${output
|
|
|
2212
2223
|
if (lp) {
|
|
2213
2224
|
left.args = this.parseSequence();
|
|
2214
2225
|
if (!this.consume('RP')) {
|
|
2215
|
-
throw new
|
|
2226
|
+
throw new ExpError('Expected "RP" after "LP"', lp.loc);
|
|
2216
2227
|
}
|
|
2217
2228
|
continue;
|
|
2218
2229
|
}
|
|
@@ -2228,7 +2239,7 @@ ${output
|
|
|
2228
2239
|
if (token.type === 'IF') {
|
|
2229
2240
|
const test = this.parseExp((t) => t.type === 'ELSE' ? 'BACK' : isHigher(token, t) ? 'BACK' : undefined);
|
|
2230
2241
|
if (!test) {
|
|
2231
|
-
throw new
|
|
2242
|
+
throw new ExpError('Expected test expression', token.loc);
|
|
2232
2243
|
}
|
|
2233
2244
|
left = {
|
|
2234
2245
|
...token,
|
|
@@ -2239,7 +2250,7 @@ ${output
|
|
|
2239
2250
|
if (this.consume('ELSE')) {
|
|
2240
2251
|
const alternative = this.parseExp((t) => isHigher(token, t) ? 'BACK' : undefined);
|
|
2241
2252
|
if (!alternative) {
|
|
2242
|
-
throw new
|
|
2253
|
+
throw new ExpError('Expected alternative expression', token.loc);
|
|
2243
2254
|
}
|
|
2244
2255
|
left.alternative = alternative;
|
|
2245
2256
|
}
|
|
@@ -2264,11 +2275,11 @@ ${output
|
|
|
2264
2275
|
token.type === 'MOD' ||
|
|
2265
2276
|
token.type === 'ASSIGN') {
|
|
2266
2277
|
if (!left) {
|
|
2267
|
-
throw new
|
|
2278
|
+
throw new ExpError(`No left operand for "${token.type}"`, token.loc);
|
|
2268
2279
|
}
|
|
2269
2280
|
const right = this.parseExp((t) => isHigher(token, t) ? 'BACK' : undefined);
|
|
2270
2281
|
if (!right) {
|
|
2271
|
-
throw new
|
|
2282
|
+
throw new ExpError(`No right operand for "${token.type}"`, token.loc);
|
|
2272
2283
|
}
|
|
2273
2284
|
left = {
|
|
2274
2285
|
...token,
|
|
@@ -2280,10 +2291,10 @@ ${output
|
|
|
2280
2291
|
}
|
|
2281
2292
|
if (token.type === 'PIPE') {
|
|
2282
2293
|
if (!left) {
|
|
2283
|
-
throw new
|
|
2294
|
+
throw new ExpError(`No left operand for "${token.type}"`, token.loc);
|
|
2284
2295
|
}
|
|
2285
2296
|
if (!this.match('ID')) {
|
|
2286
|
-
throw new
|
|
2297
|
+
throw new ExpError('Expected "ID" after "PIPE"', token.loc);
|
|
2287
2298
|
}
|
|
2288
2299
|
left = {
|
|
2289
2300
|
...token,
|
|
@@ -2307,10 +2318,10 @@ ${output
|
|
|
2307
2318
|
}
|
|
2308
2319
|
const token = this.next();
|
|
2309
2320
|
if (!token) {
|
|
2310
|
-
throw new
|
|
2321
|
+
throw new ExpError('Expected "ID" after "DOT"', dot.loc);
|
|
2311
2322
|
}
|
|
2312
2323
|
if (token.type !== 'ID') {
|
|
2313
|
-
throw new
|
|
2324
|
+
throw new ExpError('Expected "ID" after "DOT"', dot.loc);
|
|
2314
2325
|
}
|
|
2315
2326
|
ids.push({
|
|
2316
2327
|
...token,
|
|
@@ -2347,7 +2358,7 @@ ${output
|
|
|
2347
2358
|
right.args = args;
|
|
2348
2359
|
}
|
|
2349
2360
|
if (!this.consume('RP')) {
|
|
2350
|
-
throw new
|
|
2361
|
+
throw new ExpError('Expected "RP" after "LP"', lp.loc);
|
|
2351
2362
|
}
|
|
2352
2363
|
}
|
|
2353
2364
|
return right;
|
|
@@ -2579,7 +2590,7 @@ ${output
|
|
|
2579
2590
|
this.tokenize(template);
|
|
2580
2591
|
const start = this.tokens[0]?.loc.start ?? { line: 1, column: 1 };
|
|
2581
2592
|
const end = this.tokens.at(-1)?.loc.end ?? { line: 1, column: 1 };
|
|
2582
|
-
this.expParser = new ExpParser(
|
|
2593
|
+
this.expParser = new ExpParser();
|
|
2583
2594
|
this.cursor = 0;
|
|
2584
2595
|
return new RootNode(await this.parseUntil(), { start, end });
|
|
2585
2596
|
}
|
|
@@ -2628,7 +2639,14 @@ ${output
|
|
|
2628
2639
|
return nodes;
|
|
2629
2640
|
}
|
|
2630
2641
|
parseExp({ val, loc }) {
|
|
2631
|
-
|
|
2642
|
+
try {
|
|
2643
|
+
return this.expParser.parse(val, loc);
|
|
2644
|
+
}
|
|
2645
|
+
catch (error) {
|
|
2646
|
+
this.options.debug(error.loc
|
|
2647
|
+
? new CompileError(error.message, this.template, error.loc)
|
|
2648
|
+
: error);
|
|
2649
|
+
}
|
|
2632
2650
|
}
|
|
2633
2651
|
peek() {
|
|
2634
2652
|
return this.tokens.at(this.cursor) ?? null;
|
|
@@ -2640,7 +2658,7 @@ ${output
|
|
|
2640
2658
|
return names.includes(token?.name);
|
|
2641
2659
|
}
|
|
2642
2660
|
emitExpErr({ name, loc }, required = true) {
|
|
2643
|
-
this.options.debug
|
|
2661
|
+
this.options.debug(new CompileError(required
|
|
2644
2662
|
? `"${name}" requires expression`
|
|
2645
2663
|
: `"${name}" should not have expression`, this.template, loc));
|
|
2646
2664
|
}
|
|
@@ -2739,7 +2757,7 @@ ${output
|
|
|
2739
2757
|
template = await this.options.loader(path);
|
|
2740
2758
|
}
|
|
2741
2759
|
catch {
|
|
2742
|
-
this.options.debug
|
|
2760
|
+
this.options.debug(new CompileError(`Failed to load template from "${path}"`, this.template, node.loc));
|
|
2743
2761
|
return;
|
|
2744
2762
|
}
|
|
2745
2763
|
if (template) {
|
|
@@ -2800,7 +2818,7 @@ ${output
|
|
|
2800
2818
|
this.partials.set(path, code);
|
|
2801
2819
|
}
|
|
2802
2820
|
catch {
|
|
2803
|
-
this.options.debug
|
|
2821
|
+
this.options.debug(new CompileError('Failed to compile template', this.template, node.loc));
|
|
2804
2822
|
}
|
|
2805
2823
|
}
|
|
2806
2824
|
}
|
|
@@ -2882,7 +2900,20 @@ ${output
|
|
|
2882
2900
|
return new Renderer(options).renderFile(filepath, data);
|
|
2883
2901
|
}
|
|
2884
2902
|
|
|
2903
|
+
exports.CompileError = CompileError;
|
|
2904
|
+
exports.Compiler = Compiler;
|
|
2905
|
+
exports.ExpCompiler = ExpCompiler;
|
|
2906
|
+
exports.ExpError = ExpError;
|
|
2907
|
+
exports.ExpParser = ExpParser;
|
|
2908
|
+
exports.ExpTokenizer = ExpTokenizer;
|
|
2909
|
+
exports.Parser = Parser;
|
|
2910
|
+
exports.RenderError = RenderError;
|
|
2885
2911
|
exports.Renderer = Renderer;
|
|
2912
|
+
exports.RootNode = RootNode;
|
|
2913
|
+
exports.Safe = Safe;
|
|
2914
|
+
exports.Tokenizer = Tokenizer;
|
|
2915
|
+
exports.Traversal = Traversal;
|
|
2916
|
+
exports.expTokenTypes = expTokenTypes;
|
|
2886
2917
|
exports.render = render;
|
|
2887
2918
|
exports.renderFile = renderFile;
|
|
2888
2919
|
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "janja",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "2.4.
|
|
4
|
+
"version": "2.4.1",
|
|
5
5
|
"packageManager": "yarn@4.9.2",
|
|
6
6
|
"description": "A simple, fast, lightweight, and extensible template engine for Node.js and browsers.",
|
|
7
7
|
"license": "MIT",
|
|
@@ -30,8 +30,8 @@
|
|
|
30
30
|
"@rollup/plugin-node-resolve": "^16.0.3",
|
|
31
31
|
"@rollup/plugin-typescript": "^12.3.0",
|
|
32
32
|
"@types/node": "^24.10.1",
|
|
33
|
-
"@vitest/coverage-v8": "4.0.
|
|
34
|
-
"@vitest/ui": "4.0.
|
|
33
|
+
"@vitest/coverage-v8": "4.0.15",
|
|
34
|
+
"@vitest/ui": "4.0.15",
|
|
35
35
|
"eslint": "^9.39.1",
|
|
36
36
|
"eslint-import-resolver-typescript": "^4.4.4",
|
|
37
37
|
"eslint-plugin-import": "^2.32.0",
|
|
@@ -40,13 +40,13 @@
|
|
|
40
40
|
"globals": "^16.5.0",
|
|
41
41
|
"husky": "^9.1.7",
|
|
42
42
|
"jiti": "^2.6.1",
|
|
43
|
-
"prettier": "^3.7.
|
|
43
|
+
"prettier": "^3.7.4",
|
|
44
44
|
"rollup": "^4.53.3",
|
|
45
45
|
"rollup-plugin-dts": "^6.3.0",
|
|
46
46
|
"tslib": "^2.8.1",
|
|
47
47
|
"typescript": "^5.9.3",
|
|
48
|
-
"typescript-eslint": "^8.48.
|
|
49
|
-
"vite": "^7.2.
|
|
50
|
-
"vitest": "^4.0.
|
|
48
|
+
"typescript-eslint": "^8.48.1",
|
|
49
|
+
"vite": "^7.2.6",
|
|
50
|
+
"vitest": "^4.0.15"
|
|
51
51
|
}
|
|
52
52
|
}
|