janja 2.0.0 → 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 +304 -288
- package/dist/index.d.ts +43 -34
- package/dist/index.esm.js +304 -288
- package/dist/index.js +304 -288
- package/package.json +4 -3
package/dist/index.d.ts
CHANGED
|
@@ -24,26 +24,34 @@ declare enum NodeType {
|
|
|
24
24
|
interface ASTNode {
|
|
25
25
|
type: NodeType;
|
|
26
26
|
loc: Loc;
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
27
|
+
body?: ASTNode[];
|
|
28
|
+
traverse: (cb: (node: ASTNode) => void) => void;
|
|
29
|
+
}
|
|
30
|
+
declare class Traversal implements ASTNode {
|
|
31
|
+
readonly type: NodeType;
|
|
32
|
+
readonly loc: Loc;
|
|
33
|
+
body?: ASTNode[];
|
|
34
|
+
traverse(cb: (node: ASTNode) => void): void;
|
|
35
|
+
}
|
|
36
|
+
declare class RootNode extends Traversal {
|
|
37
|
+
readonly body: ASTNode[];
|
|
38
|
+
readonly loc: Loc;
|
|
31
39
|
readonly type = NodeType.TEMPLATE;
|
|
32
|
-
constructor(
|
|
40
|
+
constructor(body: ASTNode[], loc: Loc);
|
|
33
41
|
}
|
|
34
|
-
declare class UnexpectedDirectiveNode
|
|
35
|
-
name: string;
|
|
36
|
-
val: string;
|
|
37
|
-
loc: Loc;
|
|
38
|
-
strip: Strip;
|
|
42
|
+
declare class UnexpectedDirectiveNode extends Traversal {
|
|
43
|
+
readonly name: string;
|
|
44
|
+
readonly val: string;
|
|
45
|
+
readonly loc: Loc;
|
|
46
|
+
readonly strip: Strip;
|
|
39
47
|
readonly type = NodeType.UNEXPECTED;
|
|
40
48
|
constructor(name: string, val: string, loc: Loc, strip: Strip);
|
|
41
49
|
}
|
|
42
|
-
declare class BlockNode
|
|
43
|
-
val: IdExp;
|
|
44
|
-
body: ASTNode[];
|
|
45
|
-
loc: Loc;
|
|
46
|
-
strip: Strip;
|
|
50
|
+
declare class BlockNode extends Traversal {
|
|
51
|
+
readonly val: IdExp;
|
|
52
|
+
readonly body: ASTNode[];
|
|
53
|
+
readonly loc: Loc;
|
|
54
|
+
readonly strip: Strip;
|
|
47
55
|
readonly type = NodeType.BLOCK;
|
|
48
56
|
constructor(val: IdExp, body: ASTNode[], loc: Loc, strip: Strip);
|
|
49
57
|
}
|
|
@@ -101,7 +109,6 @@ declare class Context extends OutScript {
|
|
|
101
109
|
context: string;
|
|
102
110
|
private contexts;
|
|
103
111
|
private index;
|
|
104
|
-
constructor();
|
|
105
112
|
in(): string;
|
|
106
113
|
out(): void;
|
|
107
114
|
}
|
|
@@ -109,15 +116,15 @@ declare class Context extends OutScript {
|
|
|
109
116
|
declare class Compiler extends Context {
|
|
110
117
|
options: Required<CompilerOptions>;
|
|
111
118
|
template: string;
|
|
112
|
-
private root;
|
|
113
|
-
private partials;
|
|
114
|
-
blocks: Map<string, BlockNode[]>;
|
|
115
|
-
private templateNode;
|
|
116
119
|
state: Record<string, any>;
|
|
120
|
+
private isRoot;
|
|
121
|
+
private rootNode;
|
|
122
|
+
private partials;
|
|
123
|
+
private blocks;
|
|
117
124
|
constructor(options?: CompilerOptions);
|
|
118
|
-
compile(template: string, root?: boolean, blocks?: Map<string, BlockNode[]>): Promise<this>;
|
|
125
|
+
compile(template: string, root?: boolean, partials?: Map<string, string>, blocks?: Map<string, BlockNode[]>): Promise<this>;
|
|
119
126
|
private parseTemplates;
|
|
120
|
-
private
|
|
127
|
+
private collectPartialsAndBlocks;
|
|
121
128
|
private compileNode;
|
|
122
129
|
compileNodes(nodes: ASTNode[]): Promise<void>;
|
|
123
130
|
private compilePartial;
|
|
@@ -143,7 +150,7 @@ declare class Tokenizer implements Pos {
|
|
|
143
150
|
declare class Parser extends Tokenizer {
|
|
144
151
|
private expParser;
|
|
145
152
|
protected cursor: number;
|
|
146
|
-
parse(template: string):
|
|
153
|
+
parse(template: string): RootNode;
|
|
147
154
|
parseUntil(names?: string[]): ASTNode[];
|
|
148
155
|
private createComment;
|
|
149
156
|
private createOutput;
|
|
@@ -152,16 +159,15 @@ declare class Parser extends Tokenizer {
|
|
|
152
159
|
createUnexpectedDirective(token: DirectiveToken): UnexpectedDirectiveNode;
|
|
153
160
|
private createUnknownDirective;
|
|
154
161
|
parseExp({ val, loc }: DirectiveExpression): Exp;
|
|
155
|
-
peek():
|
|
162
|
+
peek(): Token | null;
|
|
156
163
|
advance(): void;
|
|
157
|
-
match(
|
|
158
|
-
isDirective(names: string[]): boolean;
|
|
164
|
+
match(names: string[], token?: Token | null): boolean;
|
|
159
165
|
requireExpression({ expression, name, loc }: DirectiveToken): void;
|
|
160
166
|
requireNoExpression({ expression, name, loc }: DirectiveToken): void;
|
|
161
167
|
}
|
|
162
168
|
|
|
163
169
|
type MaybePromise<T> = T | Promise<T>;
|
|
164
|
-
type ObjectType = Record<string,
|
|
170
|
+
type ObjectType<T = any> = Record<string, T>;
|
|
165
171
|
declare enum TokenType {
|
|
166
172
|
TEXT = "TEXT",
|
|
167
173
|
OUTPUT = "OUTPUT",
|
|
@@ -200,6 +206,7 @@ interface OutputToken extends TokenBase {
|
|
|
200
206
|
type: TokenType.OUTPUT;
|
|
201
207
|
}
|
|
202
208
|
interface TextToken extends TokenBase {
|
|
209
|
+
type: TokenType.TEXT;
|
|
203
210
|
}
|
|
204
211
|
interface DirectiveExpression {
|
|
205
212
|
val: string;
|
|
@@ -258,7 +265,7 @@ interface SeqExp extends ExpBase {
|
|
|
258
265
|
}
|
|
259
266
|
type Exp = IdExp | LitExp | NotExp | BinaryExp | IfExp | PipeExp | SeqExp;
|
|
260
267
|
type Filters = Record<string, (value: any, ...args: any[]) => any>;
|
|
261
|
-
type Script = (globals: ObjectType, escape: (v:
|
|
268
|
+
type Script = (globals: ObjectType, escape: (v: any) => any, filters: Filters) => Promise<string>;
|
|
262
269
|
interface Tag {
|
|
263
270
|
name: string;
|
|
264
271
|
value?: Exp | null;
|
|
@@ -281,16 +288,17 @@ interface NodeCompiler {
|
|
|
281
288
|
out: OutScript;
|
|
282
289
|
}) => MaybePromise<Loc | void | false>;
|
|
283
290
|
}
|
|
284
|
-
interface
|
|
285
|
-
|
|
286
|
-
|
|
291
|
+
interface Plugin {
|
|
292
|
+
filters?: Filters;
|
|
293
|
+
parsers?: ParserMap;
|
|
294
|
+
compilers?: CompilerMap;
|
|
287
295
|
}
|
|
288
296
|
type ParserFn<T = ASTNode> = (token: DirectiveToken, parser: Parser) => T | void;
|
|
289
297
|
type ParserMap = Record<string, ParserFn>;
|
|
290
298
|
type CompilerFn<T = ASTNode> = (node: T, compiler: Compiler) => MaybePromise<void>;
|
|
291
299
|
type CompilerMap = Partial<Record<NodeType, CompilerFn>>;
|
|
292
300
|
interface BaseOptions {
|
|
293
|
-
debug?: (error: Error) =>
|
|
301
|
+
debug?: (error: Error) => any;
|
|
294
302
|
}
|
|
295
303
|
interface ParserOptions extends BaseOptions {
|
|
296
304
|
commentOpen?: string;
|
|
@@ -311,11 +319,12 @@ interface RendererOptions extends CompilerOptions {
|
|
|
311
319
|
globals?: ObjectType;
|
|
312
320
|
filters?: Filters;
|
|
313
321
|
autoEscape?: boolean;
|
|
322
|
+
plugins?: Plugin[];
|
|
314
323
|
}
|
|
315
324
|
|
|
316
325
|
declare class Renderer {
|
|
317
326
|
protected options: Required<RendererOptions>;
|
|
318
|
-
constructor({ globals, filters, parsers, compilers, ...options }?: RendererOptions);
|
|
327
|
+
constructor({ globals, filters, parsers, compilers, plugins, ...options }?: RendererOptions);
|
|
319
328
|
render(template: string, globals: ObjectType): Promise<string>;
|
|
320
329
|
renderFile(filepath: string, globals: ObjectType): Promise<string>;
|
|
321
330
|
}
|
|
@@ -324,4 +333,4 @@ declare function render(template: string, data: ObjectType, options?: RendererOp
|
|
|
324
333
|
declare function renderFile(filepath: string, data: ObjectType, options?: RendererOptions): Promise<string>;
|
|
325
334
|
|
|
326
335
|
export { Renderer, TokenType, render, renderFile };
|
|
327
|
-
export type { BaseOptions, BinaryExp,
|
|
336
|
+
export type { BaseOptions, BinaryExp, Checker, CommentToken, CompilerFn, CompilerMap, CompilerOptions, DirectiveExpression, DirectiveToken, Exp, ExpBase, ExpToken, ExpTokenType, ExpType, Filters, IdExp, IfExp, LitExp, Loc, Mapping, MaybePromise, NodeCompiler, NotExp, ObjectType, OutputToken, ParserFn, ParserMap, ParserOptions, PipeExp, Plugin, Pos, RendererOptions, Script, SeqExp, Strip, Tag, TextToken, Token, TokenBase };
|