janja 2.1.0 → 2.1.2

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.d.ts CHANGED
@@ -1,61 +1,3 @@
1
- declare enum NodeType {
2
- TEMPLATE = "TEMPLATE",
3
- COMMENT = "COMMENT",
4
- OUTPUT = "OUTPUT",
5
- TEXT = "TEXT",
6
- IF = "IF",
7
- ELSE = "ELSE",
8
- ELSE_IF = "ELSE_IF",
9
- FOR = "FOR",
10
- BREAK = "BREAK",
11
- CONTINUE = "CONTINUE",
12
- EXP = "EXP",
13
- INCLUDE = "INCLUDE",
14
- BLOCK = "BLOCK",
15
- SUPER = "SUPER",
16
- MACRO = "MACRO",
17
- CALLER = "CALLER",
18
- CALL = "CALL",
19
- SET = "SET",
20
- CAPTURE = "CAPTURE",
21
- UNKNOWN = "UNKNOWN",
22
- UNEXPECTED = "UNEXPECTED"
23
- }
24
- interface ASTNode {
25
- type: NodeType;
26
- loc: Loc;
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;
39
- readonly type = NodeType.TEMPLATE;
40
- constructor(body: ASTNode[], loc: Loc);
41
- }
42
- declare class UnexpectedDirectiveNode extends Traversal {
43
- readonly name: string;
44
- readonly val: string;
45
- readonly loc: Loc;
46
- readonly strip: Strip;
47
- readonly type = NodeType.UNEXPECTED;
48
- constructor(name: string, val: string, loc: Loc, strip: Strip);
49
- }
50
- declare class BlockNode extends Traversal {
51
- readonly val: IdExp;
52
- readonly body: ASTNode[];
53
- readonly loc: Loc;
54
- readonly strip: Strip;
55
- readonly type = NodeType.BLOCK;
56
- constructor(val: IdExp, body: ASTNode[], loc: Loc, strip: Strip);
57
- }
58
-
59
1
  /**
60
2
  * Contains the source map information.
61
3
  * @example
@@ -113,6 +55,34 @@ declare class Context extends OutScript {
113
55
  out(): void;
114
56
  }
115
57
 
58
+ interface SyntaxNode {
59
+ type: string;
60
+ loc: Loc;
61
+ body?: SyntaxNode[];
62
+ traverse: (cb: (node: SyntaxNode) => void) => void;
63
+ }
64
+ declare class Traversal implements SyntaxNode {
65
+ readonly type: string;
66
+ readonly loc: Loc;
67
+ body?: SyntaxNode[];
68
+ traverse(cb: (node: SyntaxNode) => void): void;
69
+ }
70
+ declare class RootNode extends Traversal {
71
+ readonly body: SyntaxNode[];
72
+ readonly loc: Loc;
73
+ readonly type = "TEMPLATE";
74
+ constructor(body: SyntaxNode[], loc: Loc);
75
+ }
76
+
77
+ declare class BlockNode extends Traversal {
78
+ readonly val: IdExp;
79
+ readonly body: SyntaxNode[];
80
+ readonly loc: Loc;
81
+ readonly strip: Strip;
82
+ readonly type = "BLOCK";
83
+ constructor(val: IdExp, body: SyntaxNode[], loc: Loc, strip: Strip);
84
+ }
85
+
116
86
  declare class Compiler extends Context {
117
87
  options: Required<CompilerOptions>;
118
88
  template: string;
@@ -126,7 +96,7 @@ declare class Compiler extends Context {
126
96
  private parseTemplates;
127
97
  private collectPartialsAndBlocks;
128
98
  private compileNode;
129
- compileNodes(nodes: ASTNode[]): Promise<void>;
99
+ compileNodes(nodes: SyntaxNode[]): Promise<void>;
130
100
  private compilePartial;
131
101
  }
132
102
 
@@ -150,20 +120,13 @@ declare class Tokenizer implements Pos {
150
120
  declare class Parser extends Tokenizer {
151
121
  private expParser;
152
122
  protected cursor: number;
153
- parse(template: string): RootNode;
154
- parseUntil(names?: string[]): ASTNode[];
155
- private createComment;
156
- private createOutput;
157
- private createText;
158
- private createDirective;
159
- createUnexpectedDirective(token: DirectiveToken): UnexpectedDirectiveNode;
160
- private createUnknownDirective;
123
+ parse(template: string): Promise<RootNode>;
124
+ parseUntil(names?: string[]): Promise<SyntaxNode[]>;
161
125
  parseExp({ val, loc }: DirectiveExpression): Exp;
162
126
  peek(): Token | null;
163
- advance(): void;
127
+ private advance;
164
128
  match(names: string[], token?: Token | null): boolean;
165
- requireExpression({ expression, name, loc }: DirectiveToken): void;
166
- requireNoExpression({ expression, name, loc }: DirectiveToken): void;
129
+ emitExpErr({ name, loc }: DirectiveToken, required?: boolean): void;
167
130
  }
168
131
 
169
132
  type MaybePromise<T> = T | Promise<T>;
@@ -217,53 +180,50 @@ interface Mapping {
217
180
  source: Loc;
218
181
  target: Loc;
219
182
  }
220
- type ExpTokenType = 'AND' | 'OR' | 'IS' | 'NOT' | 'EQ' | 'NE' | 'GT' | 'LT' | 'GE' | 'LE' | 'IN' | 'NI' | 'OF' | 'ADD' | 'SUB' | 'MUL' | 'DIV' | 'MOD' | 'PIPE' | 'IF' | 'ELSE' | 'SET' | 'LP' | 'RP' | 'COMMA' | 'DOT' | 'ID' | 'LIT';
183
+ type UnaryExpTokenType = 'NOT';
184
+ type BinaryExpTokenType = 'AND' | 'OR' | 'IS' | 'EQ' | 'NE' | 'GT' | 'LT' | 'GE' | 'LE' | 'IN' | 'NI' | 'OF' | 'ADD' | 'SUB' | 'MUL' | 'DIV' | 'MOD' | 'PIPE' | 'ASSIGN';
185
+ type TernaryExpTokenType = 'IF' | 'ELSE';
186
+ type ExpTokenType = UnaryExpTokenType | BinaryExpTokenType | TernaryExpTokenType | 'LP' | 'RP' | 'COMMA' | 'DOT' | 'ID' | 'LIT';
187
+ type Primitive = string | number | boolean | null | undefined;
221
188
  interface ExpToken<T = ExpTokenType> {
222
189
  type: T;
223
- value: string | number | boolean | null | undefined;
190
+ value: Primitive;
224
191
  loc: Loc;
225
192
  raw?: string;
226
193
  }
227
194
  type Checker = (token: ExpToken) => 'BACK' | 'BREAK' | undefined;
228
- type ExpType = 'AND' | 'OR' | 'IS' | 'NOT' | 'EQ' | 'NE' | 'GT' | 'LT' | 'GE' | 'LE' | 'IN' | 'NI' | 'OF' | 'ADD' | 'SUB' | 'MUL' | 'DIV' | 'MOD' | 'PIPE' | 'IF' | 'SET' | 'SEQ' | 'ID' | 'LIT';
195
+ type UnaryExpType = UnaryExpTokenType;
196
+ type BinaryExpType = BinaryExpTokenType;
197
+ type TernaryExpType = TernaryExpTokenType;
198
+ type ExpType = UnaryExpType | BinaryExpType | TernaryExpType | 'SEQ' | 'ID' | 'LIT';
229
199
  interface ExpBase<T = ExpType> {
230
200
  type: T;
231
201
  loc: Loc;
232
202
  }
233
- interface IdExp extends ExpBase {
234
- type: 'ID';
235
- value: string;
236
- path?: IdExp[];
237
- args?: Exp[];
238
- }
239
- interface LitExp<T = string | number | boolean | null | undefined> extends ExpBase {
240
- type: 'LIT';
241
- value: T;
242
- }
243
- interface NotExp extends ExpBase {
244
- type: 'NOT';
203
+ interface NotExp extends ExpBase<'NOT'> {
245
204
  argument: Exp;
246
205
  }
247
- interface BinaryExp<T = 'AND' | 'OR' | 'IS' | 'EQ' | 'NE' | 'GT' | 'LT' | 'GE' | 'LE' | 'IN' | 'NI' | 'OF' | 'ADD' | 'SUB' | 'MUL' | 'DIV' | 'MOD' | 'SET'> extends ExpBase<T> {
206
+ interface BinaryExp<T extends BinaryExpType = any> extends ExpBase<T> {
248
207
  left: Exp;
249
208
  right: Exp;
250
209
  }
251
- interface IfExp extends ExpBase {
252
- type: 'IF';
210
+ interface IfExp extends ExpBase<'IF'> {
253
211
  test: Exp;
254
212
  consequent: Exp;
255
213
  alternative?: Exp;
256
214
  }
257
- interface PipeExp extends ExpBase {
258
- type: 'PIPE';
259
- left: Exp;
260
- right: Exp;
215
+ interface IdExp extends ExpBase<'ID'> {
216
+ value: string;
217
+ path?: IdExp[];
218
+ args?: Exp[];
261
219
  }
262
- interface SeqExp extends ExpBase {
263
- type: 'SEQ';
220
+ interface LitExp<T = Primitive> extends ExpBase<'LIT'> {
221
+ value: T;
222
+ }
223
+ interface SeqExp extends ExpBase<'SEQ'> {
264
224
  elements: Exp[];
265
225
  }
266
- type Exp = IdExp | LitExp | NotExp | BinaryExp | IfExp | PipeExp | SeqExp;
226
+ type Exp = NotExp | BinaryExp | IfExp | IdExp | LitExp | SeqExp;
267
227
  type Filters = Record<string, (value: any, ...args: any[]) => any>;
268
228
  type Script = (globals: ObjectType, escape: (v: any) => any, filters: Filters) => Promise<string>;
269
229
  interface Tag {
@@ -293,12 +253,16 @@ interface Plugin {
293
253
  parsers?: ParserMap;
294
254
  compilers?: CompilerMap;
295
255
  }
296
- type ParserFn<T = ASTNode> = (token: DirectiveToken, parser: Parser) => T | void;
297
- type ParserMap = Record<string, ParserFn>;
298
- type CompilerFn<T = ASTNode> = (node: T, compiler: Compiler) => MaybePromise<void>;
299
- type CompilerMap = Partial<Record<NodeType, CompilerFn>>;
256
+ type ParserFn<T extends Token = any, U extends SyntaxNode = any> = (token: T, parser: Parser) => AsyncGenerator<U | 'NEXT' | void>;
257
+ type ParserMap = Record<string, 'unexpected' | ParserFn>;
258
+ type CompilerFn<T extends SyntaxNode = any> = (node: T, compiler: Compiler) => MaybePromise<void>;
259
+ type CompilerMap = Record<string, CompilerFn>;
300
260
  interface BaseOptions {
301
261
  debug?: (error: Error) => any;
262
+ filters?: Filters;
263
+ parsers?: ParserMap;
264
+ compilers?: CompilerMap;
265
+ plugins?: Plugin[];
302
266
  }
303
267
  interface ParserOptions extends BaseOptions {
304
268
  commentOpen?: string;
@@ -307,24 +271,20 @@ interface ParserOptions extends BaseOptions {
307
271
  directiveClose?: string;
308
272
  outputOpen?: string;
309
273
  outputClose?: string;
310
- parsers?: ParserMap;
311
274
  }
312
275
  interface CompilerOptions extends ParserOptions {
313
276
  trimWhitespace?: boolean;
314
277
  stripComments?: boolean;
315
- compilers?: CompilerMap;
316
278
  loader?: (path: string) => Promise<string>;
317
279
  }
318
280
  interface RendererOptions extends CompilerOptions {
319
281
  globals?: ObjectType;
320
- filters?: Filters;
321
282
  autoEscape?: boolean;
322
- plugins?: Plugin[];
323
283
  }
324
284
 
325
285
  declare class Renderer {
326
286
  protected options: Required<RendererOptions>;
327
- constructor({ globals, filters, parsers, compilers, plugins, ...options }?: RendererOptions);
287
+ constructor(options: RendererOptions);
328
288
  render(template: string, globals: ObjectType): Promise<string>;
329
289
  renderFile(filepath: string, globals: ObjectType): Promise<string>;
330
290
  }
@@ -333,4 +293,4 @@ declare function render(template: string, data: ObjectType, options?: RendererOp
333
293
  declare function renderFile(filepath: string, data: ObjectType, options?: RendererOptions): Promise<string>;
334
294
 
335
295
  export { Renderer, TokenType, render, renderFile };
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 };
296
+ export type { BaseOptions, BinaryExp, BinaryExpTokenType, BinaryExpType, 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, Plugin, Pos, Primitive, RendererOptions, Script, SeqExp, Strip, Tag, TernaryExpTokenType, TernaryExpType, TextToken, Token, TokenBase, UnaryExpTokenType, UnaryExpType };