janja 2.1.0 → 2.1.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.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,42 @@ 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
+ declare class UnexpectedNode extends Traversal {
77
+ readonly name: string;
78
+ readonly val: string;
79
+ readonly loc: Loc;
80
+ readonly strip: Strip;
81
+ readonly type = "UNEXPECTED";
82
+ constructor(name: string, val: string, loc: Loc, strip: Strip);
83
+ }
84
+
85
+ declare class BlockNode extends Traversal {
86
+ readonly val: IdExp;
87
+ readonly body: SyntaxNode[];
88
+ readonly loc: Loc;
89
+ readonly strip: Strip;
90
+ readonly type = "BLOCK";
91
+ constructor(val: IdExp, body: SyntaxNode[], loc: Loc, strip: Strip);
92
+ }
93
+
116
94
  declare class Compiler extends Context {
117
95
  options: Required<CompilerOptions>;
118
96
  template: string;
@@ -126,7 +104,7 @@ declare class Compiler extends Context {
126
104
  private parseTemplates;
127
105
  private collectPartialsAndBlocks;
128
106
  private compileNode;
129
- compileNodes(nodes: ASTNode[]): Promise<void>;
107
+ compileNodes(nodes: SyntaxNode[]): Promise<void>;
130
108
  private compilePartial;
131
109
  }
132
110
 
@@ -151,19 +129,14 @@ declare class Parser extends Tokenizer {
151
129
  private expParser;
152
130
  protected cursor: number;
153
131
  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;
132
+ parseUntil(names?: string[]): SyntaxNode[];
133
+ createUnexpectedNode(token: Token): UnexpectedNode;
134
+ private createUnknownNode;
161
135
  parseExp({ val, loc }: DirectiveExpression): Exp;
162
136
  peek(): Token | null;
163
137
  advance(): void;
164
138
  match(names: string[], token?: Token | null): boolean;
165
- requireExpression({ expression, name, loc }: DirectiveToken): void;
166
- requireNoExpression({ expression, name, loc }: DirectiveToken): void;
139
+ emitExpErr({ name, loc }: DirectiveToken, required?: boolean): void;
167
140
  }
168
141
 
169
142
  type MaybePromise<T> = T | Promise<T>;
@@ -217,7 +190,10 @@ interface Mapping {
217
190
  source: Loc;
218
191
  target: Loc;
219
192
  }
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';
193
+ type UnaryExpTokenType = 'NOT';
194
+ type BinaryExpTokenType = 'AND' | 'OR' | 'IS' | 'EQ' | 'NE' | 'GT' | 'LT' | 'GE' | 'LE' | 'IN' | 'NI' | 'OF' | 'ADD' | 'SUB' | 'MUL' | 'DIV' | 'MOD' | 'PIPE' | 'ASSIGN';
195
+ type TernaryExpTokenType = 'IF' | 'ELSE';
196
+ type ExpTokenType = UnaryExpTokenType | BinaryExpTokenType | TernaryExpTokenType | 'LP' | 'RP' | 'COMMA' | 'DOT' | 'ID' | 'LIT';
221
197
  interface ExpToken<T = ExpTokenType> {
222
198
  type: T;
223
199
  value: string | number | boolean | null | undefined;
@@ -225,45 +201,38 @@ interface ExpToken<T = ExpTokenType> {
225
201
  raw?: string;
226
202
  }
227
203
  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';
204
+ type UnaryExpType = UnaryExpTokenType;
205
+ type BinaryExpType = BinaryExpTokenType;
206
+ type TernaryExpType = TernaryExpTokenType;
207
+ type ExpType = UnaryExpType | BinaryExpType | TernaryExpType | 'SEQ' | 'ID' | 'LIT';
229
208
  interface ExpBase<T = ExpType> {
230
209
  type: T;
231
210
  loc: Loc;
232
211
  }
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';
212
+ interface NotExp extends ExpBase<'NOT'> {
245
213
  argument: Exp;
246
214
  }
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> {
215
+ interface BinaryExp<T extends BinaryExpType = BinaryExpType> extends ExpBase<T> {
248
216
  left: Exp;
249
217
  right: Exp;
250
218
  }
251
- interface IfExp extends ExpBase {
252
- type: 'IF';
219
+ interface IfExp extends ExpBase<'IF'> {
253
220
  test: Exp;
254
221
  consequent: Exp;
255
222
  alternative?: Exp;
256
223
  }
257
- interface PipeExp extends ExpBase {
258
- type: 'PIPE';
259
- left: Exp;
260
- right: Exp;
224
+ interface IdExp extends ExpBase<'ID'> {
225
+ value: string;
226
+ path?: IdExp[];
227
+ args?: Exp[];
228
+ }
229
+ interface LitExp<T = string | number | boolean | null | undefined> extends ExpBase<'LIT'> {
230
+ value: T;
261
231
  }
262
- interface SeqExp extends ExpBase {
263
- type: 'SEQ';
232
+ interface SeqExp extends ExpBase<'SEQ'> {
264
233
  elements: Exp[];
265
234
  }
266
- type Exp = IdExp | LitExp | NotExp | BinaryExp | IfExp | PipeExp | SeqExp;
235
+ type Exp = NotExp | BinaryExp | IfExp | IdExp | LitExp | SeqExp;
267
236
  type Filters = Record<string, (value: any, ...args: any[]) => any>;
268
237
  type Script = (globals: ObjectType, escape: (v: any) => any, filters: Filters) => Promise<string>;
269
238
  interface Tag {
@@ -293,12 +262,16 @@ interface Plugin {
293
262
  parsers?: ParserMap;
294
263
  compilers?: CompilerMap;
295
264
  }
296
- type ParserFn<T = ASTNode> = (token: DirectiveToken, parser: Parser) => T | void;
265
+ type ParserFn<T extends SyntaxNode = SyntaxNode> = (token: Token, parser: Parser) => T | void;
297
266
  type ParserMap = Record<string, ParserFn>;
298
- type CompilerFn<T = ASTNode> = (node: T, compiler: Compiler) => MaybePromise<void>;
299
- type CompilerMap = Partial<Record<NodeType, CompilerFn>>;
267
+ type CompilerFn<T extends SyntaxNode = SyntaxNode> = (node: T, compiler: Compiler) => MaybePromise<void>;
268
+ type CompilerMap = Partial<Record<string, CompilerFn>>;
300
269
  interface BaseOptions {
301
270
  debug?: (error: Error) => any;
271
+ filters?: Filters;
272
+ parsers?: ParserMap;
273
+ compilers?: CompilerMap;
274
+ plugins?: Plugin[];
302
275
  }
303
276
  interface ParserOptions extends BaseOptions {
304
277
  commentOpen?: string;
@@ -307,24 +280,20 @@ interface ParserOptions extends BaseOptions {
307
280
  directiveClose?: string;
308
281
  outputOpen?: string;
309
282
  outputClose?: string;
310
- parsers?: ParserMap;
311
283
  }
312
284
  interface CompilerOptions extends ParserOptions {
313
285
  trimWhitespace?: boolean;
314
286
  stripComments?: boolean;
315
- compilers?: CompilerMap;
316
287
  loader?: (path: string) => Promise<string>;
317
288
  }
318
289
  interface RendererOptions extends CompilerOptions {
319
290
  globals?: ObjectType;
320
- filters?: Filters;
321
291
  autoEscape?: boolean;
322
- plugins?: Plugin[];
323
292
  }
324
293
 
325
294
  declare class Renderer {
326
295
  protected options: Required<RendererOptions>;
327
- constructor({ globals, filters, parsers, compilers, plugins, ...options }?: RendererOptions);
296
+ constructor(options: RendererOptions);
328
297
  render(template: string, globals: ObjectType): Promise<string>;
329
298
  renderFile(filepath: string, globals: ObjectType): Promise<string>;
330
299
  }
@@ -333,4 +302,4 @@ declare function render(template: string, data: ObjectType, options?: RendererOp
333
302
  declare function renderFile(filepath: string, data: ObjectType, options?: RendererOptions): Promise<string>;
334
303
 
335
304
  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 };
305
+ 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, RendererOptions, Script, SeqExp, Strip, Tag, TernaryExpTokenType, TernaryExpType, TextToken, Token, TokenBase, UnaryExpTokenType, UnaryExpType };