janja 1.1.0 → 2.0.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.d.ts CHANGED
@@ -1,35 +1,51 @@
1
- declare class Context {
2
- options: Config;
3
- context: string;
4
- private index;
5
- private contexts;
6
- private expected;
7
- constructor(options: Config);
8
- in(): string;
9
- out(): void;
10
- expect(name: string): void;
11
- matchAny(name: string): boolean;
12
- match(name: string): boolean;
13
- consume(name: string): boolean;
14
- validate(): void;
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"
15
23
  }
16
-
17
- declare class OutScript {
18
- options: Config;
19
- private content;
20
- private strOffset;
21
- private varOffset;
22
- constructor(options: Config);
23
- get value(): string;
24
- get script(): Script;
25
- start(): void;
26
- end(): void;
27
- pushLine(...lines: string[]): Range;
28
- pushStr(s: string, o?: {
29
- trimStart?: boolean;
30
- trimEnd?: boolean;
31
- }): Range | void;
32
- pushVar(v: string): Range;
24
+ interface ASTNode {
25
+ type: NodeType;
26
+ loc: Loc;
27
+ }
28
+ declare class TemplateNode implements ASTNode {
29
+ children: ASTNode[];
30
+ loc: Loc;
31
+ readonly type = NodeType.TEMPLATE;
32
+ constructor(children: ASTNode[], loc: Loc);
33
+ }
34
+ declare class UnexpectedDirectiveNode implements ASTNode {
35
+ name: string;
36
+ val: string;
37
+ loc: Loc;
38
+ strip: Strip;
39
+ readonly type = NodeType.UNEXPECTED;
40
+ constructor(name: string, val: string, loc: Loc, strip: Strip);
41
+ }
42
+ declare class BlockNode implements ASTNode {
43
+ val: IdExp;
44
+ body: ASTNode[];
45
+ loc: Loc;
46
+ strip: Strip;
47
+ readonly type = NodeType.BLOCK;
48
+ constructor(val: IdExp, body: ASTNode[], loc: Loc, strip: Strip);
33
49
  }
34
50
 
35
51
  /**
@@ -44,95 +60,215 @@ declare class OutScript {
44
60
  * - sourceMap:
45
61
  * Mapping {
46
62
  * source: {
47
- * start: 3,
48
- * end: 6,
63
+ * start: {
64
+ * line: 2,
65
+ * column: 4,
66
+ * },
67
+ * end: {
68
+ * line: 3,
69
+ * column: 5,
70
+ * },
49
71
  * },
50
72
  * target: {
51
- * start: 3,
52
- * end: 8,
73
+ * start: {
74
+ * line: 2,
75
+ * column: 3,
76
+ * },
77
+ * end: {
78
+ * line: 3,
79
+ * column: 8,
80
+ * },
53
81
  * }
54
82
  * }
55
83
  */
56
84
  declare class SourceMap {
57
- options: Config;
58
85
  mappings: Mapping[];
59
- constructor(options: Config);
60
- addMapping(source: Range, target: Range): void;
61
- getRanges(offset: number): Range[];
86
+ addMapping(source: Loc, target: Loc): void;
87
+ getSourceLoc({ line, column }: Pos): Loc[];
88
+ }
89
+
90
+ declare class OutScript extends SourceMap {
91
+ code: string;
92
+ get script(): Script;
93
+ start(): void;
94
+ end(): void;
95
+ pushRaw(loc: Loc | null, ...lines: string[]): void;
96
+ pushStr(loc: Loc | null, s: string): Pos | void;
97
+ pushVar(loc: Loc | null, v: string): void;
98
+ }
99
+
100
+ declare class Context extends OutScript {
101
+ context: string;
102
+ private contexts;
103
+ private index;
104
+ constructor();
105
+ in(): string;
106
+ out(): void;
107
+ }
108
+
109
+ declare class Compiler extends Context {
110
+ options: Required<CompilerOptions>;
111
+ template: string;
112
+ private root;
113
+ private partials;
114
+ blocks: Map<string, BlockNode[]>;
115
+ private templateNode;
116
+ state: Record<string, any>;
117
+ constructor(options?: CompilerOptions);
118
+ compile(template: string, root?: boolean, blocks?: Map<string, BlockNode[]>): Promise<this>;
119
+ private parseTemplates;
120
+ private collectIncludesAndBlocks;
121
+ private compileNode;
122
+ compileNodes(nodes: ASTNode[]): Promise<void>;
123
+ private compilePartial;
124
+ }
125
+
126
+ declare class Tokenizer implements Pos {
127
+ options: Required<ParserOptions>;
128
+ template: string;
129
+ protected length: number;
130
+ protected index: number;
131
+ protected tokens: Token[];
132
+ private tokenPatterns;
133
+ line: number;
134
+ column: number;
135
+ constructor(options?: ParserOptions);
136
+ tokenize(template: string): Token[];
137
+ private consumeSpecialTokens;
138
+ private consumeTextToken;
139
+ private lookahead;
140
+ private findNextSpecialIndex;
141
+ }
142
+
143
+ declare class Parser extends Tokenizer {
144
+ private expParser;
145
+ protected cursor: number;
146
+ parse(template: string): TemplateNode;
147
+ parseUntil(names?: string[]): ASTNode[];
148
+ private createComment;
149
+ private createOutput;
150
+ private createText;
151
+ private createDirective;
152
+ createUnexpectedDirective(token: DirectiveToken): UnexpectedDirectiveNode;
153
+ private createUnknownDirective;
154
+ parseExp({ val, loc }: DirectiveExpression): Exp;
155
+ peek(): TextToken | null;
156
+ advance(): void;
157
+ match(name: string): boolean;
158
+ isDirective(names: string[]): boolean;
159
+ requireExpression({ expression, name, loc }: DirectiveToken): void;
160
+ requireNoExpression({ expression, name, loc }: DirectiveToken): void;
62
161
  }
63
162
 
64
163
  type MaybePromise<T> = T | Promise<T>;
65
164
  type ObjectType = Record<string, any>;
66
- interface Range {
67
- start: number;
68
- end: number;
165
+ declare enum TokenType {
166
+ TEXT = "TEXT",
167
+ OUTPUT = "OUTPUT",
168
+ DIRECTIVE = "DIRECTIVE",
169
+ COMMENT = "COMMENT"
170
+ }
171
+ interface Pos {
172
+ line: number;
173
+ column: number;
174
+ }
175
+ interface Loc {
176
+ start: Pos;
177
+ end: Pos;
178
+ }
179
+ interface Strip {
180
+ before?: boolean;
181
+ after?: boolean;
182
+ start?: boolean;
183
+ end?: boolean;
184
+ }
185
+ interface TokenBase {
186
+ type: TokenType;
187
+ val: string;
188
+ loc: Loc;
189
+ strip: Strip;
190
+ }
191
+ interface CommentToken extends TokenBase {
192
+ type: TokenType.COMMENT;
193
+ }
194
+ interface DirectiveToken extends TokenBase {
195
+ type: TokenType.DIRECTIVE;
196
+ name: string;
197
+ expression: DirectiveExpression | null;
198
+ }
199
+ interface OutputToken extends TokenBase {
200
+ type: TokenType.OUTPUT;
201
+ }
202
+ interface TextToken extends TokenBase {
203
+ }
204
+ interface DirectiveExpression {
205
+ val: string;
206
+ loc: Loc;
69
207
  }
208
+ type Token = CommentToken | DirectiveToken | OutputToken | TextToken;
70
209
  interface Mapping {
71
- source: Range;
72
- target: Range;
210
+ source: Loc;
211
+ target: Loc;
73
212
  }
74
213
  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';
75
- interface ExpToken<T = ExpTokenType> extends Range {
214
+ interface ExpToken<T = ExpTokenType> {
76
215
  type: T;
77
216
  value: string | number | boolean | null | undefined;
217
+ loc: Loc;
78
218
  raw?: string;
79
219
  }
80
220
  type Checker = (token: ExpToken) => 'BACK' | 'BREAK' | undefined;
81
221
  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';
82
- interface BaseExp extends Range {
83
- type: ExpType;
222
+ interface ExpBase<T = ExpType> {
223
+ type: T;
224
+ loc: Loc;
84
225
  }
85
- interface IdExp extends BaseExp {
226
+ interface IdExp extends ExpBase {
86
227
  type: 'ID';
87
228
  value: string;
88
229
  path?: IdExp[];
89
230
  args?: Exp[];
90
231
  }
91
- interface LitExp extends BaseExp {
232
+ interface LitExp<T = string | number | boolean | null | undefined> extends ExpBase {
92
233
  type: 'LIT';
93
- value: string | number | boolean | null | undefined;
234
+ value: T;
94
235
  }
95
- interface NotExp extends BaseExp {
236
+ interface NotExp extends ExpBase {
96
237
  type: 'NOT';
97
238
  argument: Exp;
98
239
  }
99
- interface BinaryExp extends BaseExp {
100
- type: 'AND' | 'OR' | 'IS' | 'EQ' | 'NE' | 'GT' | 'LT' | 'GE' | 'LE' | 'IN' | 'NI' | 'OF' | 'ADD' | 'SUB' | 'MUL' | 'DIV' | 'MOD' | 'SET';
240
+ interface BinaryExp<T = 'AND' | 'OR' | 'IS' | 'EQ' | 'NE' | 'GT' | 'LT' | 'GE' | 'LE' | 'IN' | 'NI' | 'OF' | 'ADD' | 'SUB' | 'MUL' | 'DIV' | 'MOD' | 'SET'> extends ExpBase<T> {
101
241
  left: Exp;
102
242
  right: Exp;
103
243
  }
104
- interface IfExp extends BaseExp {
244
+ interface IfExp extends ExpBase {
105
245
  type: 'IF';
106
246
  test: Exp;
107
247
  consequent: Exp;
108
248
  alternative?: Exp;
109
249
  }
110
- interface PipeExp extends BaseExp {
250
+ interface PipeExp extends ExpBase {
111
251
  type: 'PIPE';
112
252
  left: Exp;
113
253
  right: Exp;
114
254
  }
115
- interface SeqExp extends BaseExp {
255
+ interface SeqExp extends ExpBase {
116
256
  type: 'SEQ';
117
257
  elements: Exp[];
118
258
  }
119
259
  type Exp = IdExp | LitExp | NotExp | BinaryExp | IfExp | PipeExp | SeqExp;
120
- interface Globals {
121
- translations?: Record<string, string>;
122
- [key: string]: any;
123
- }
124
260
  type Filters = Record<string, (value: any, ...args: any[]) => any>;
125
- type Script = (globals: Globals, filters: Filters, escape: (v: unknown) => unknown) => Promise<string>;
126
- interface Tag extends Range {
261
+ type Script = (globals: ObjectType, escape: (v: unknown) => unknown, filters: Filters) => Promise<string>;
262
+ interface Tag {
127
263
  name: string;
128
264
  value?: Exp | null;
129
265
  raw: string;
130
266
  previous: Tag | null;
131
267
  next: Tag | null;
132
- stripBefore?: boolean;
133
- stripAfter?: boolean;
268
+ strip?: Strip;
269
+ loc?: Loc;
134
270
  }
135
- interface TagCompiler {
271
+ interface NodeCompiler {
136
272
  names: string[];
137
273
  /**
138
274
  * - Return `Range` to indicate the range of the tag.
@@ -143,76 +279,49 @@ interface TagCompiler {
143
279
  tag: Tag;
144
280
  ctx: Context;
145
281
  out: OutScript;
146
- }) => MaybePromise<Range | void | false>;
282
+ }) => MaybePromise<Loc | void | false>;
147
283
  }
148
284
  interface CacheValue {
149
285
  template: string;
150
- script: Script;
151
- sourcemap: SourceMap;
286
+ code: string;
152
287
  }
153
- interface Config {
154
- globals?: Globals;
155
- filters?: Filters;
156
- compilers?: Record<string, TagCompiler[]>;
157
- strictMode?: boolean;
158
- autoEscape?: boolean;
159
- stripComments?: boolean;
288
+ type ParserFn<T = ASTNode> = (token: DirectiveToken, parser: Parser) => T | void;
289
+ type ParserMap = Record<string, ParserFn>;
290
+ type CompilerFn<T = ASTNode> = (node: T, compiler: Compiler) => MaybePromise<void>;
291
+ type CompilerMap = Partial<Record<NodeType, CompilerFn>>;
292
+ interface BaseOptions {
293
+ debug?: (error: Error) => unknown;
294
+ }
295
+ interface ParserOptions extends BaseOptions {
296
+ commentOpen?: string;
297
+ commentClose?: string;
298
+ directiveOpen?: string;
299
+ directiveClose?: string;
300
+ outputOpen?: string;
301
+ outputClose?: string;
302
+ parsers?: ParserMap;
303
+ }
304
+ interface CompilerOptions extends ParserOptions {
160
305
  trimWhitespace?: boolean;
306
+ stripComments?: boolean;
307
+ compilers?: CompilerMap;
161
308
  loader?: (path: string) => Promise<string>;
162
- cache?: {
163
- has: (path: string) => boolean;
164
- get: (path: string) => CacheValue | undefined;
165
- set: (path: string, content: CacheValue) => void;
166
- };
167
309
  }
168
-
169
- declare class ExpCompiler {
170
- private context;
171
- private filters;
172
- compile(expression: Exp | null, context: string, filters: string): string;
173
- private compileExp;
174
- private compileNot;
175
- private compileId;
176
- private compileLit;
177
- private compileSeq;
178
- private compilePipe;
179
- private compileOf;
180
- private compileSet;
181
- private compileIs;
182
- private compileBinary;
183
- private compileTernary;
310
+ interface RendererOptions extends CompilerOptions {
311
+ globals?: ObjectType;
312
+ filters?: Filters;
313
+ autoEscape?: boolean;
184
314
  }
185
315
 
186
- declare class ExpParser {
187
- template: string;
188
- tokens: ExpToken[];
189
- length: number;
190
- cursor: number;
191
- parse(template: string): Exp | null;
192
- private parseExp;
193
- private parsePath;
194
- private parseSequence;
195
- private parsePipe;
196
- private next;
197
- private back;
198
- private peek;
199
- private check;
200
- private consume;
316
+ declare class Renderer {
317
+ protected options: Required<RendererOptions>;
318
+ constructor({ globals, filters, parsers, compilers, ...options }?: RendererOptions);
319
+ render(template: string, globals: ObjectType): Promise<string>;
320
+ renderFile(filepath: string, globals: ObjectType): Promise<string>;
201
321
  }
202
322
 
203
- declare const compiler: ExpCompiler;
204
- declare const parser: ExpParser;
205
-
206
- declare class Engine {
207
- protected options: Config;
208
- constructor({ globals, filters, compilers, ...options }?: Config);
209
- render(template: string, globals: Globals): Promise<string>;
210
- renderFile(filepath: string, globals: Globals): Promise<string>;
211
- private _compile;
212
- private _render;
213
- }
214
- declare function render(template: string, data: ObjectType, options?: Config): Promise<string>;
215
- declare function renderFile(filepath: string, data: ObjectType, options?: Config): Promise<string>;
323
+ declare function render(template: string, data: ObjectType, options?: RendererOptions): Promise<string>;
324
+ declare function renderFile(filepath: string, data: ObjectType, options?: RendererOptions): Promise<string>;
216
325
 
217
- export { Engine, ExpCompiler, ExpParser, compiler, parser, render, renderFile };
218
- export type { BaseExp, BinaryExp, CacheValue, Checker, Config, Exp, ExpToken, ExpTokenType, ExpType, Filters, Globals, IdExp, IfExp, LitExp, Mapping, MaybePromise, NotExp, ObjectType, PipeExp, Range, Script, SeqExp, Tag, TagCompiler };
326
+ export { Renderer, TokenType, render, renderFile };
327
+ 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 };