janja 1.1.0 → 2.0.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,35 +1,59 @@
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
+ 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);
33
57
  }
34
58
 
35
59
  /**
@@ -44,95 +68,214 @@ declare class OutScript {
44
68
  * - sourceMap:
45
69
  * Mapping {
46
70
  * source: {
47
- * start: 3,
48
- * end: 6,
71
+ * start: {
72
+ * line: 2,
73
+ * column: 4,
74
+ * },
75
+ * end: {
76
+ * line: 3,
77
+ * column: 5,
78
+ * },
49
79
  * },
50
80
  * target: {
51
- * start: 3,
52
- * end: 8,
81
+ * start: {
82
+ * line: 2,
83
+ * column: 3,
84
+ * },
85
+ * end: {
86
+ * line: 3,
87
+ * column: 8,
88
+ * },
53
89
  * }
54
90
  * }
55
91
  */
56
92
  declare class SourceMap {
57
- options: Config;
58
93
  mappings: Mapping[];
59
- constructor(options: Config);
60
- addMapping(source: Range, target: Range): void;
61
- getRanges(offset: number): Range[];
94
+ addMapping(source: Loc, target: Loc): void;
95
+ getSourceLoc({ line, column }: Pos): Loc[];
96
+ }
97
+
98
+ declare class OutScript extends SourceMap {
99
+ code: string;
100
+ get script(): Script;
101
+ start(): void;
102
+ end(): void;
103
+ pushRaw(loc: Loc | null, ...lines: string[]): void;
104
+ pushStr(loc: Loc | null, s: string): Pos | void;
105
+ pushVar(loc: Loc | null, v: string): void;
106
+ }
107
+
108
+ declare class Context extends OutScript {
109
+ context: string;
110
+ private contexts;
111
+ private index;
112
+ constructor();
113
+ in(): string;
114
+ out(): void;
115
+ }
116
+
117
+ declare class Compiler extends Context {
118
+ options: Required<CompilerOptions>;
119
+ template: string;
120
+ state: Record<string, any>;
121
+ private isRoot;
122
+ private rootNode;
123
+ private partials;
124
+ private blocks;
125
+ constructor(options?: CompilerOptions);
126
+ compile(template: string, root?: boolean, partials?: Map<string, string>, blocks?: Map<string, BlockNode[]>): Promise<this>;
127
+ private parseTemplates;
128
+ private collectPartialsAndBlocks;
129
+ private compileNode;
130
+ compileNodes(nodes: ASTNode[]): Promise<void>;
131
+ private compilePartial;
132
+ }
133
+
134
+ declare class Tokenizer implements Pos {
135
+ options: Required<ParserOptions>;
136
+ template: string;
137
+ protected length: number;
138
+ protected index: number;
139
+ protected tokens: Token[];
140
+ private tokenPatterns;
141
+ line: number;
142
+ column: number;
143
+ constructor(options?: ParserOptions);
144
+ tokenize(template: string): Token[];
145
+ private consumeSpecialTokens;
146
+ private consumeTextToken;
147
+ private lookahead;
148
+ private findNextSpecialIndex;
149
+ }
150
+
151
+ declare class Parser extends Tokenizer {
152
+ private expParser;
153
+ protected cursor: number;
154
+ 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;
162
+ parseExp({ val, loc }: DirectiveExpression): Exp;
163
+ peek(): TextToken | null;
164
+ advance(): void;
165
+ match(names: string[], token?: TextToken | null): boolean;
166
+ requireExpression({ expression, name, loc }: DirectiveToken): void;
167
+ requireNoExpression({ expression, name, loc }: DirectiveToken): void;
62
168
  }
63
169
 
64
170
  type MaybePromise<T> = T | Promise<T>;
65
171
  type ObjectType = Record<string, any>;
66
- interface Range {
67
- start: number;
68
- end: number;
172
+ declare enum TokenType {
173
+ TEXT = "TEXT",
174
+ OUTPUT = "OUTPUT",
175
+ DIRECTIVE = "DIRECTIVE",
176
+ COMMENT = "COMMENT"
177
+ }
178
+ interface Pos {
179
+ line: number;
180
+ column: number;
181
+ }
182
+ interface Loc {
183
+ start: Pos;
184
+ end: Pos;
185
+ }
186
+ interface Strip {
187
+ before?: boolean;
188
+ after?: boolean;
189
+ start?: boolean;
190
+ end?: boolean;
191
+ }
192
+ interface TokenBase {
193
+ type: TokenType;
194
+ val: string;
195
+ loc: Loc;
196
+ strip: Strip;
197
+ }
198
+ interface CommentToken extends TokenBase {
199
+ type: TokenType.COMMENT;
200
+ }
201
+ interface DirectiveToken extends TokenBase {
202
+ type: TokenType.DIRECTIVE;
203
+ name: string;
204
+ expression: DirectiveExpression | null;
205
+ }
206
+ interface OutputToken extends TokenBase {
207
+ type: TokenType.OUTPUT;
208
+ }
209
+ interface TextToken extends TokenBase {
210
+ }
211
+ interface DirectiveExpression {
212
+ val: string;
213
+ loc: Loc;
69
214
  }
215
+ type Token = CommentToken | DirectiveToken | OutputToken | TextToken;
70
216
  interface Mapping {
71
- source: Range;
72
- target: Range;
217
+ source: Loc;
218
+ target: Loc;
73
219
  }
74
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';
75
- interface ExpToken<T = ExpTokenType> extends Range {
221
+ interface ExpToken<T = ExpTokenType> {
76
222
  type: T;
77
223
  value: string | number | boolean | null | undefined;
224
+ loc: Loc;
78
225
  raw?: string;
79
226
  }
80
227
  type Checker = (token: ExpToken) => 'BACK' | 'BREAK' | undefined;
81
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';
82
- interface BaseExp extends Range {
83
- type: ExpType;
229
+ interface ExpBase<T = ExpType> {
230
+ type: T;
231
+ loc: Loc;
84
232
  }
85
- interface IdExp extends BaseExp {
233
+ interface IdExp extends ExpBase {
86
234
  type: 'ID';
87
235
  value: string;
88
236
  path?: IdExp[];
89
237
  args?: Exp[];
90
238
  }
91
- interface LitExp extends BaseExp {
239
+ interface LitExp<T = string | number | boolean | null | undefined> extends ExpBase {
92
240
  type: 'LIT';
93
- value: string | number | boolean | null | undefined;
241
+ value: T;
94
242
  }
95
- interface NotExp extends BaseExp {
243
+ interface NotExp extends ExpBase {
96
244
  type: 'NOT';
97
245
  argument: Exp;
98
246
  }
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';
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> {
101
248
  left: Exp;
102
249
  right: Exp;
103
250
  }
104
- interface IfExp extends BaseExp {
251
+ interface IfExp extends ExpBase {
105
252
  type: 'IF';
106
253
  test: Exp;
107
254
  consequent: Exp;
108
255
  alternative?: Exp;
109
256
  }
110
- interface PipeExp extends BaseExp {
257
+ interface PipeExp extends ExpBase {
111
258
  type: 'PIPE';
112
259
  left: Exp;
113
260
  right: Exp;
114
261
  }
115
- interface SeqExp extends BaseExp {
262
+ interface SeqExp extends ExpBase {
116
263
  type: 'SEQ';
117
264
  elements: Exp[];
118
265
  }
119
266
  type Exp = IdExp | LitExp | NotExp | BinaryExp | IfExp | PipeExp | SeqExp;
120
- interface Globals {
121
- translations?: Record<string, string>;
122
- [key: string]: any;
123
- }
124
267
  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 {
268
+ type Script = (globals: ObjectType, escape: (v: unknown) => unknown, filters: Filters) => Promise<string>;
269
+ interface Tag {
127
270
  name: string;
128
271
  value?: Exp | null;
129
272
  raw: string;
130
273
  previous: Tag | null;
131
274
  next: Tag | null;
132
- stripBefore?: boolean;
133
- stripAfter?: boolean;
275
+ strip?: Strip;
276
+ loc?: Loc;
134
277
  }
135
- interface TagCompiler {
278
+ interface NodeCompiler {
136
279
  names: string[];
137
280
  /**
138
281
  * - Return `Range` to indicate the range of the tag.
@@ -143,76 +286,49 @@ interface TagCompiler {
143
286
  tag: Tag;
144
287
  ctx: Context;
145
288
  out: OutScript;
146
- }) => MaybePromise<Range | void | false>;
289
+ }) => MaybePromise<Loc | void | false>;
147
290
  }
148
291
  interface CacheValue {
149
292
  template: string;
150
- script: Script;
151
- sourcemap: SourceMap;
293
+ code: string;
152
294
  }
153
- interface Config {
154
- globals?: Globals;
155
- filters?: Filters;
156
- compilers?: Record<string, TagCompiler[]>;
157
- strictMode?: boolean;
158
- autoEscape?: boolean;
159
- stripComments?: boolean;
295
+ type ParserFn<T = ASTNode> = (token: DirectiveToken, parser: Parser) => T | void;
296
+ type ParserMap = Record<string, ParserFn>;
297
+ type CompilerFn<T = ASTNode> = (node: T, compiler: Compiler) => MaybePromise<void>;
298
+ type CompilerMap = Partial<Record<NodeType, CompilerFn>>;
299
+ interface BaseOptions {
300
+ debug?: (error: Error) => unknown;
301
+ }
302
+ interface ParserOptions extends BaseOptions {
303
+ commentOpen?: string;
304
+ commentClose?: string;
305
+ directiveOpen?: string;
306
+ directiveClose?: string;
307
+ outputOpen?: string;
308
+ outputClose?: string;
309
+ parsers?: ParserMap;
310
+ }
311
+ interface CompilerOptions extends ParserOptions {
160
312
  trimWhitespace?: boolean;
313
+ stripComments?: boolean;
314
+ compilers?: CompilerMap;
161
315
  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
316
  }
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;
317
+ interface RendererOptions extends CompilerOptions {
318
+ globals?: ObjectType;
319
+ filters?: Filters;
320
+ autoEscape?: boolean;
184
321
  }
185
322
 
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;
323
+ declare class Renderer {
324
+ protected options: Required<RendererOptions>;
325
+ constructor({ globals, filters, parsers, compilers, ...options }?: RendererOptions);
326
+ render(template: string, globals: ObjectType): Promise<string>;
327
+ renderFile(filepath: string, globals: ObjectType): Promise<string>;
201
328
  }
202
329
 
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>;
330
+ declare function render(template: string, data: ObjectType, options?: RendererOptions): Promise<string>;
331
+ declare function renderFile(filepath: string, data: ObjectType, options?: RendererOptions): Promise<string>;
216
332
 
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 };
333
+ 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 };