janja 2.0.1 → 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
@@ -109,11 +51,46 @@ declare class Context extends OutScript {
109
51
  context: string;
110
52
  private contexts;
111
53
  private index;
112
- constructor();
113
54
  in(): string;
114
55
  out(): void;
115
56
  }
116
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
+
117
94
  declare class Compiler extends Context {
118
95
  options: Required<CompilerOptions>;
119
96
  template: string;
@@ -127,7 +104,7 @@ declare class Compiler extends Context {
127
104
  private parseTemplates;
128
105
  private collectPartialsAndBlocks;
129
106
  private compileNode;
130
- compileNodes(nodes: ASTNode[]): Promise<void>;
107
+ compileNodes(nodes: SyntaxNode[]): Promise<void>;
131
108
  private compilePartial;
132
109
  }
133
110
 
@@ -152,23 +129,18 @@ declare class Parser extends Tokenizer {
152
129
  private expParser;
153
130
  protected cursor: number;
154
131
  parse(template: string): RootNode;
155
- parseUntil(names?: string[]): ASTNode[];
156
- private createComment;
157
- private createOutput;
158
- private createText;
159
- private createDirective;
160
- createUnexpectedDirective(token: DirectiveToken): UnexpectedDirectiveNode;
161
- private createUnknownDirective;
132
+ parseUntil(names?: string[]): SyntaxNode[];
133
+ createUnexpectedNode(token: Token): UnexpectedNode;
134
+ private createUnknownNode;
162
135
  parseExp({ val, loc }: DirectiveExpression): Exp;
163
- peek(): TextToken | null;
136
+ peek(): Token | null;
164
137
  advance(): void;
165
- match(names: string[], token?: TextToken | null): boolean;
166
- requireExpression({ expression, name, loc }: DirectiveToken): void;
167
- requireNoExpression({ expression, name, loc }: DirectiveToken): void;
138
+ match(names: string[], token?: Token | null): boolean;
139
+ emitExpErr({ name, loc }: DirectiveToken, required?: boolean): void;
168
140
  }
169
141
 
170
142
  type MaybePromise<T> = T | Promise<T>;
171
- type ObjectType = Record<string, any>;
143
+ type ObjectType<T = any> = Record<string, T>;
172
144
  declare enum TokenType {
173
145
  TEXT = "TEXT",
174
146
  OUTPUT = "OUTPUT",
@@ -207,6 +179,7 @@ interface OutputToken extends TokenBase {
207
179
  type: TokenType.OUTPUT;
208
180
  }
209
181
  interface TextToken extends TokenBase {
182
+ type: TokenType.TEXT;
210
183
  }
211
184
  interface DirectiveExpression {
212
185
  val: string;
@@ -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,47 +201,40 @@ 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
- type Script = (globals: ObjectType, escape: (v: unknown) => unknown, filters: Filters) => Promise<string>;
237
+ type Script = (globals: ObjectType, escape: (v: any) => any, filters: Filters) => Promise<string>;
269
238
  interface Tag {
270
239
  name: string;
271
240
  value?: Exp | null;
@@ -288,16 +257,21 @@ interface NodeCompiler {
288
257
  out: OutScript;
289
258
  }) => MaybePromise<Loc | void | false>;
290
259
  }
291
- interface CacheValue {
292
- template: string;
293
- code: string;
260
+ interface Plugin {
261
+ filters?: Filters;
262
+ parsers?: ParserMap;
263
+ compilers?: CompilerMap;
294
264
  }
295
- type ParserFn<T = ASTNode> = (token: DirectiveToken, parser: Parser) => T | void;
265
+ type ParserFn<T extends SyntaxNode = SyntaxNode> = (token: Token, parser: Parser) => T | void;
296
266
  type ParserMap = Record<string, ParserFn>;
297
- type CompilerFn<T = ASTNode> = (node: T, compiler: Compiler) => MaybePromise<void>;
298
- 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>>;
299
269
  interface BaseOptions {
300
- debug?: (error: Error) => unknown;
270
+ debug?: (error: Error) => any;
271
+ filters?: Filters;
272
+ parsers?: ParserMap;
273
+ compilers?: CompilerMap;
274
+ plugins?: Plugin[];
301
275
  }
302
276
  interface ParserOptions extends BaseOptions {
303
277
  commentOpen?: string;
@@ -306,23 +280,20 @@ interface ParserOptions extends BaseOptions {
306
280
  directiveClose?: string;
307
281
  outputOpen?: string;
308
282
  outputClose?: string;
309
- parsers?: ParserMap;
310
283
  }
311
284
  interface CompilerOptions extends ParserOptions {
312
285
  trimWhitespace?: boolean;
313
286
  stripComments?: boolean;
314
- compilers?: CompilerMap;
315
287
  loader?: (path: string) => Promise<string>;
316
288
  }
317
289
  interface RendererOptions extends CompilerOptions {
318
290
  globals?: ObjectType;
319
- filters?: Filters;
320
291
  autoEscape?: boolean;
321
292
  }
322
293
 
323
294
  declare class Renderer {
324
295
  protected options: Required<RendererOptions>;
325
- constructor({ globals, filters, parsers, compilers, ...options }?: RendererOptions);
296
+ constructor(options: RendererOptions);
326
297
  render(template: string, globals: ObjectType): Promise<string>;
327
298
  renderFile(filepath: string, globals: ObjectType): Promise<string>;
328
299
  }
@@ -331,4 +302,4 @@ declare function render(template: string, data: ObjectType, options?: RendererOp
331
302
  declare function renderFile(filepath: string, data: ObjectType, options?: RendererOptions): Promise<string>;
332
303
 
333
304
  export { Renderer, TokenType, render, renderFile };
334
- export type { BaseOptions, BinaryExp, CacheValue, 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, 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 };