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.cjs.js +1829 -1006
- package/dist/index.d.ts +235 -126
- package/dist/index.esm.js +1829 -1002
- package/dist/index.js +1829 -1006
- package/package.json +4 -4
package/dist/index.d.ts
CHANGED
|
@@ -1,35 +1,51 @@
|
|
|
1
|
-
declare
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
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
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
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:
|
|
48
|
-
*
|
|
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:
|
|
52
|
-
*
|
|
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
|
-
|
|
60
|
-
|
|
61
|
-
|
|
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
|
-
|
|
67
|
-
|
|
68
|
-
|
|
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:
|
|
72
|
-
target:
|
|
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>
|
|
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
|
|
83
|
-
type:
|
|
222
|
+
interface ExpBase<T = ExpType> {
|
|
223
|
+
type: T;
|
|
224
|
+
loc: Loc;
|
|
84
225
|
}
|
|
85
|
-
interface IdExp extends
|
|
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
|
|
232
|
+
interface LitExp<T = string | number | boolean | null | undefined> extends ExpBase {
|
|
92
233
|
type: 'LIT';
|
|
93
|
-
value:
|
|
234
|
+
value: T;
|
|
94
235
|
}
|
|
95
|
-
interface NotExp extends
|
|
236
|
+
interface NotExp extends ExpBase {
|
|
96
237
|
type: 'NOT';
|
|
97
238
|
argument: Exp;
|
|
98
239
|
}
|
|
99
|
-
interface BinaryExp extends
|
|
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
|
|
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
|
|
250
|
+
interface PipeExp extends ExpBase {
|
|
111
251
|
type: 'PIPE';
|
|
112
252
|
left: Exp;
|
|
113
253
|
right: Exp;
|
|
114
254
|
}
|
|
115
|
-
interface SeqExp extends
|
|
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:
|
|
126
|
-
interface Tag
|
|
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
|
-
|
|
133
|
-
|
|
268
|
+
strip?: Strip;
|
|
269
|
+
loc?: Loc;
|
|
134
270
|
}
|
|
135
|
-
interface
|
|
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<
|
|
282
|
+
}) => MaybePromise<Loc | void | false>;
|
|
147
283
|
}
|
|
148
284
|
interface CacheValue {
|
|
149
285
|
template: string;
|
|
150
|
-
|
|
151
|
-
sourcemap: SourceMap;
|
|
286
|
+
code: string;
|
|
152
287
|
}
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
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
|
-
|
|
170
|
-
|
|
171
|
-
|
|
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
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
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
|
|
204
|
-
declare
|
|
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 {
|
|
218
|
-
export type {
|
|
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 };
|