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.cjs.js +1858 -1040
- package/dist/index.d.ts +242 -142
- package/dist/index.esm.js +1858 -1036
- package/dist/index.js +1858 -1040
- package/package.json +6 -26
- package/LICENSE +0 -21
- package/README.md +0 -39
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,103 +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;
|
|
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:
|
|
72
|
-
target:
|
|
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' | '
|
|
75
|
-
interface ExpToken<T = ExpTokenType>
|
|
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' | '
|
|
82
|
-
interface
|
|
83
|
-
type:
|
|
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
|
|
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
|
|
92
|
-
type: '
|
|
93
|
-
value:
|
|
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
|
|
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
|
|
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
|
|
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
|
|
250
|
+
interface PipeExp extends ExpBase {
|
|
119
251
|
type: 'PIPE';
|
|
120
252
|
left: Exp;
|
|
121
253
|
right: Exp;
|
|
122
254
|
}
|
|
123
|
-
interface SeqExp extends
|
|
255
|
+
interface SeqExp extends ExpBase {
|
|
124
256
|
type: 'SEQ';
|
|
125
257
|
elements: Exp[];
|
|
126
258
|
}
|
|
127
|
-
type Exp = IdExp |
|
|
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:
|
|
134
|
-
interface
|
|
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:
|
|
139
|
-
next:
|
|
140
|
-
|
|
141
|
-
|
|
266
|
+
previous: Tag | null;
|
|
267
|
+
next: Tag | null;
|
|
268
|
+
strip?: Strip;
|
|
269
|
+
loc?: Loc;
|
|
142
270
|
}
|
|
143
|
-
interface
|
|
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
|
-
|
|
279
|
+
tag: Tag;
|
|
152
280
|
ctx: Context;
|
|
153
281
|
out: OutScript;
|
|
154
|
-
}) => MaybePromise<
|
|
282
|
+
}) => MaybePromise<Loc | void | false>;
|
|
155
283
|
}
|
|
156
284
|
interface CacheValue {
|
|
157
285
|
template: string;
|
|
158
|
-
|
|
159
|
-
sourcemap: SourceMap;
|
|
286
|
+
code: string;
|
|
160
287
|
}
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
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
|
-
|
|
178
|
-
|
|
179
|
-
|
|
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
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
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
|
|
213
|
-
declare
|
|
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 {
|
|
227
|
-
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 };
|