janja 1.0.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,103 +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;
69
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;
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
- type ExpTokenType = 'AND' | 'OR' | 'NOT' | 'EQ' | 'NE' | 'GT' | 'LT' | 'GE' | 'LE' | 'IN' | 'NI' | 'OF' | 'ADD' | 'SUB' | 'MUL' | 'DIV' | 'MOD' | 'PIPE' | 'IF' | 'ELSE' | 'SET' | 'LP' | 'RP' | 'COMMA' | 'DOT' | 'STR' | 'NUM' | 'BOOL' | 'ID';
75
- interface ExpToken<T = ExpTokenType> extends Range {
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';
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
- type ExpType = 'AND' | 'OR' | 'NOT' | 'EQ' | 'NE' | 'GT' | 'LT' | 'GE' | 'LE' | 'IN' | 'NI' | 'OF' | 'ADD' | 'SUB' | 'MUL' | 'DIV' | 'MOD' | 'PIPE' | 'IF' | 'SET' | 'SEQ' | 'STR' | 'NUM' | 'BOOL' | 'ID';
82
- interface BaseExp extends Range {
83
- type: ExpType;
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';
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 StrExp extends BaseExp {
92
- type: 'STR';
93
- value: string;
94
- }
95
- interface NumExp extends BaseExp {
96
- type: 'NUM';
97
- value: number;
232
+ interface LitExp<T = string | number | boolean | null | undefined> extends ExpBase {
233
+ type: 'LIT';
234
+ value: T;
98
235
  }
99
- interface BoolExp extends BaseExp {
100
- type: 'BOOL';
101
- value: boolean;
102
- }
103
- interface NotExp extends BaseExp {
236
+ interface NotExp extends ExpBase {
104
237
  type: 'NOT';
105
238
  argument: Exp;
106
239
  }
107
- interface BinaryExp extends BaseExp {
108
- type: 'AND' | 'OR' | '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> {
109
241
  left: Exp;
110
242
  right: Exp;
111
243
  }
112
- interface IfExp extends BaseExp {
244
+ interface IfExp extends ExpBase {
113
245
  type: 'IF';
114
246
  test: Exp;
115
247
  consequent: Exp;
116
248
  alternative?: Exp;
117
249
  }
118
- interface PipeExp extends BaseExp {
250
+ interface PipeExp extends ExpBase {
119
251
  type: 'PIPE';
120
252
  left: Exp;
121
253
  right: Exp;
122
254
  }
123
- interface SeqExp extends BaseExp {
255
+ interface SeqExp extends ExpBase {
124
256
  type: 'SEQ';
125
257
  elements: Exp[];
126
258
  }
127
- type Exp = IdExp | StrExp | NumExp | BoolExp | NotExp | BinaryExp | IfExp | PipeExp | SeqExp;
128
- interface Globals {
129
- translations?: Record<string, string>;
130
- [key: string]: any;
131
- }
259
+ type Exp = IdExp | LitExp | NotExp | BinaryExp | IfExp | PipeExp | SeqExp;
132
260
  type Filters = Record<string, (value: any, ...args: any[]) => any>;
133
- type Script = (globals: Globals, filters: Filters, escape: (v: unknown) => unknown) => Promise<string>;
134
- interface TagToken extends Range {
261
+ type Script = (globals: ObjectType, escape: (v: unknown) => unknown, filters: Filters) => Promise<string>;
262
+ interface Tag {
135
263
  name: string;
136
264
  value?: Exp | null;
137
265
  raw: string;
138
- previous: TagToken | null;
139
- next: TagToken | null;
140
- stripBefore?: boolean;
141
- stripAfter?: boolean;
266
+ previous: Tag | null;
267
+ next: Tag | null;
268
+ strip?: Strip;
269
+ loc?: Loc;
142
270
  }
143
- interface TagCompiler {
271
+ interface NodeCompiler {
144
272
  names: string[];
145
273
  /**
146
274
  * - Return `Range` to indicate the range of the tag.
@@ -148,80 +276,52 @@ interface TagCompiler {
148
276
  * - Return `void` to continue compiling.
149
277
  */
150
278
  compile: (arg: {
151
- token: TagToken;
279
+ tag: Tag;
152
280
  ctx: Context;
153
281
  out: OutScript;
154
- }) => MaybePromise<Range | void | false>;
282
+ }) => MaybePromise<Loc | void | false>;
155
283
  }
156
284
  interface CacheValue {
157
285
  template: string;
158
- script: Script;
159
- sourcemap: SourceMap;
286
+ code: string;
160
287
  }
161
- interface Config {
162
- globals?: Globals;
163
- filters?: Filters;
164
- compilers?: Record<string, TagCompiler[]>;
165
- strictMode?: boolean;
166
- autoEscape?: boolean;
167
- 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 {
168
305
  trimWhitespace?: boolean;
306
+ stripComments?: boolean;
307
+ compilers?: CompilerMap;
169
308
  loader?: (path: string) => Promise<string>;
170
- cache?: {
171
- has: (path: string) => boolean;
172
- get: (path: string) => CacheValue | undefined;
173
- set: (path: string, content: CacheValue) => void;
174
- };
175
309
  }
176
-
177
- declare class ExpCompiler {
178
- private context;
179
- private filters;
180
- compile(expression: Exp | null, context: string, filters: string): string;
181
- private compileExp;
182
- private compileNot;
183
- private compileId;
184
- private compileStr;
185
- private compileNum;
186
- private compileBool;
187
- private compileSeq;
188
- private compilePipe;
189
- private compileOf;
190
- private compileSet;
191
- private compileBinary;
192
- private compileTernary;
310
+ interface RendererOptions extends CompilerOptions {
311
+ globals?: ObjectType;
312
+ filters?: Filters;
313
+ autoEscape?: boolean;
193
314
  }
194
315
 
195
- declare class ExpParser {
196
- template: string;
197
- tokens: ExpToken[];
198
- length: number;
199
- cursor: number;
200
- parse(template: string): Exp | null;
201
- private parseExp;
202
- private parsePath;
203
- private parseSequence;
204
- private parsePipe;
205
- private next;
206
- private back;
207
- private peek;
208
- private check;
209
- 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>;
210
321
  }
211
322
 
212
- declare const compiler: ExpCompiler;
213
- declare const parser: ExpParser;
214
-
215
- declare class Engine {
216
- protected options: Config;
217
- constructor({ globals, filters, compilers, ...options }?: Config);
218
- render(template: string, globals: Globals): Promise<string>;
219
- renderFile(filepath: string, globals: Globals): Promise<string>;
220
- private _compile;
221
- private _render;
222
- }
223
- declare function render(template: string, data: ObjectType, options?: Config): Promise<string>;
224
- 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>;
225
325
 
226
- export { Engine, ExpCompiler, ExpParser, compiler, parser, render, renderFile };
227
- export type { BaseExp, BinaryExp, BoolExp, CacheValue, Checker, Config, Exp, ExpToken, ExpTokenType, ExpType, Filters, Globals, IdExp, IfExp, Mapping, MaybePromise, NotExp, NumExp, ObjectType, PipeExp, Range, Script, SeqExp, StrExp, TagCompiler, TagToken };
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 };