janja 2.1.1 → 2.1.3
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 +314 -235
- package/dist/index.d.ts +53 -89
- package/dist/index.esm.js +314 -235
- package/dist/index.js +314 -235
- package/package.json +6 -6
package/dist/index.d.ts
CHANGED
|
@@ -1,3 +1,7 @@
|
|
|
1
|
+
interface Mapping {
|
|
2
|
+
source: Loc;
|
|
3
|
+
target: Loc;
|
|
4
|
+
}
|
|
1
5
|
/**
|
|
2
6
|
* Contains the source map information.
|
|
3
7
|
* @example
|
|
@@ -55,6 +59,43 @@ declare class Context extends OutScript {
|
|
|
55
59
|
out(): void;
|
|
56
60
|
}
|
|
57
61
|
|
|
62
|
+
type UnaryExpTokenType = 'NOT';
|
|
63
|
+
type BinaryExpTokenType = 'AND' | 'OR' | 'IS' | 'EQ' | 'NE' | 'GT' | 'LT' | 'GE' | 'LE' | 'IN' | 'NI' | 'OF' | 'ADD' | 'SUB' | 'MUL' | 'DIV' | 'MOD' | 'PIPE' | 'ASSIGN';
|
|
64
|
+
type TernaryExpTokenType = 'IF' | 'ELSE';
|
|
65
|
+
type ExpTokenType = UnaryExpTokenType | BinaryExpTokenType | TernaryExpTokenType | 'LP' | 'RP' | 'COMMA' | 'DOT' | 'ID' | 'LIT';
|
|
66
|
+
type UnaryExpType = UnaryExpTokenType;
|
|
67
|
+
type BinaryExpType = BinaryExpTokenType;
|
|
68
|
+
type TernaryExpType = TernaryExpTokenType;
|
|
69
|
+
type ExpType = UnaryExpType | BinaryExpType | TernaryExpType | 'SEQ' | 'ID' | 'LIT';
|
|
70
|
+
interface ExpBase<T = ExpType> {
|
|
71
|
+
type: T;
|
|
72
|
+
loc: Loc;
|
|
73
|
+
}
|
|
74
|
+
interface NotExp extends ExpBase<'NOT'> {
|
|
75
|
+
argument: Exp;
|
|
76
|
+
}
|
|
77
|
+
interface BinaryExp<T extends BinaryExpType = BinaryExpType, U extends Exp = Exp, V extends Exp = Exp> extends ExpBase<T> {
|
|
78
|
+
left: U;
|
|
79
|
+
right: V;
|
|
80
|
+
}
|
|
81
|
+
interface IfExp extends ExpBase<'IF'> {
|
|
82
|
+
test: Exp;
|
|
83
|
+
consequent: Exp;
|
|
84
|
+
alternative?: Exp;
|
|
85
|
+
}
|
|
86
|
+
interface IdExp extends ExpBase<'ID'> {
|
|
87
|
+
value: string;
|
|
88
|
+
path?: IdExp[];
|
|
89
|
+
args?: Exp[];
|
|
90
|
+
}
|
|
91
|
+
interface LitExp<T = Primitive> extends ExpBase<'LIT'> {
|
|
92
|
+
value: T;
|
|
93
|
+
}
|
|
94
|
+
interface SeqExp<T extends Exp = Exp> extends ExpBase<'SEQ'> {
|
|
95
|
+
elements: T[];
|
|
96
|
+
}
|
|
97
|
+
type Exp = NotExp | BinaryExp | IfExp | IdExp | LitExp | SeqExp;
|
|
98
|
+
|
|
58
99
|
interface SyntaxNode {
|
|
59
100
|
type: string;
|
|
60
101
|
loc: Loc;
|
|
@@ -73,14 +114,6 @@ declare class RootNode extends Traversal {
|
|
|
73
114
|
readonly type = "TEMPLATE";
|
|
74
115
|
constructor(body: SyntaxNode[], loc: Loc);
|
|
75
116
|
}
|
|
76
|
-
declare class UnexpectedNode extends Traversal {
|
|
77
|
-
readonly name: string;
|
|
78
|
-
readonly val: string;
|
|
79
|
-
readonly loc: Loc;
|
|
80
|
-
readonly strip: Strip;
|
|
81
|
-
readonly type = "UNEXPECTED";
|
|
82
|
-
constructor(name: string, val: string, loc: Loc, strip: Strip);
|
|
83
|
-
}
|
|
84
117
|
|
|
85
118
|
declare class BlockNode extends Traversal {
|
|
86
119
|
readonly val: IdExp;
|
|
@@ -128,19 +161,18 @@ declare class Tokenizer implements Pos {
|
|
|
128
161
|
declare class Parser extends Tokenizer {
|
|
129
162
|
private expParser;
|
|
130
163
|
protected cursor: number;
|
|
131
|
-
parse(template: string): RootNode
|
|
132
|
-
parseUntil(names?: string[]): SyntaxNode[]
|
|
133
|
-
|
|
134
|
-
private createUnknownNode;
|
|
135
|
-
parseExp({ val, loc }: DirectiveExpression): Exp;
|
|
164
|
+
parse(template: string): Promise<RootNode>;
|
|
165
|
+
parseUntil(names?: string[]): Promise<SyntaxNode[]>;
|
|
166
|
+
parseExp<T extends Exp = Exp>({ val, loc }: DirectiveExpression): T;
|
|
136
167
|
peek(): Token | null;
|
|
137
|
-
advance
|
|
168
|
+
private advance;
|
|
138
169
|
match(names: string[], token?: Token | null): boolean;
|
|
139
170
|
emitExpErr({ name, loc }: DirectiveToken, required?: boolean): void;
|
|
140
171
|
}
|
|
141
172
|
|
|
142
173
|
type MaybePromise<T> = T | Promise<T>;
|
|
143
174
|
type ObjectType<T = any> = Record<string, T>;
|
|
175
|
+
type Primitive = string | number | boolean | null | undefined;
|
|
144
176
|
declare enum TokenType {
|
|
145
177
|
TEXT = "TEXT",
|
|
146
178
|
OUTPUT = "OUTPUT",
|
|
@@ -186,86 +218,18 @@ interface DirectiveExpression {
|
|
|
186
218
|
loc: Loc;
|
|
187
219
|
}
|
|
188
220
|
type Token = CommentToken | DirectiveToken | OutputToken | TextToken;
|
|
189
|
-
interface Mapping {
|
|
190
|
-
source: Loc;
|
|
191
|
-
target: Loc;
|
|
192
|
-
}
|
|
193
|
-
type UnaryExpTokenType = 'NOT';
|
|
194
|
-
type BinaryExpTokenType = 'AND' | 'OR' | 'IS' | 'EQ' | 'NE' | 'GT' | 'LT' | 'GE' | 'LE' | 'IN' | 'NI' | 'OF' | 'ADD' | 'SUB' | 'MUL' | 'DIV' | 'MOD' | 'PIPE' | 'ASSIGN';
|
|
195
|
-
type TernaryExpTokenType = 'IF' | 'ELSE';
|
|
196
|
-
type ExpTokenType = UnaryExpTokenType | BinaryExpTokenType | TernaryExpTokenType | 'LP' | 'RP' | 'COMMA' | 'DOT' | 'ID' | 'LIT';
|
|
197
|
-
interface ExpToken<T = ExpTokenType> {
|
|
198
|
-
type: T;
|
|
199
|
-
value: string | number | boolean | null | undefined;
|
|
200
|
-
loc: Loc;
|
|
201
|
-
raw?: string;
|
|
202
|
-
}
|
|
203
|
-
type Checker = (token: ExpToken) => 'BACK' | 'BREAK' | undefined;
|
|
204
|
-
type UnaryExpType = UnaryExpTokenType;
|
|
205
|
-
type BinaryExpType = BinaryExpTokenType;
|
|
206
|
-
type TernaryExpType = TernaryExpTokenType;
|
|
207
|
-
type ExpType = UnaryExpType | BinaryExpType | TernaryExpType | 'SEQ' | 'ID' | 'LIT';
|
|
208
|
-
interface ExpBase<T = ExpType> {
|
|
209
|
-
type: T;
|
|
210
|
-
loc: Loc;
|
|
211
|
-
}
|
|
212
|
-
interface NotExp extends ExpBase<'NOT'> {
|
|
213
|
-
argument: Exp;
|
|
214
|
-
}
|
|
215
|
-
interface BinaryExp<T extends BinaryExpType = BinaryExpType> extends ExpBase<T> {
|
|
216
|
-
left: Exp;
|
|
217
|
-
right: Exp;
|
|
218
|
-
}
|
|
219
|
-
interface IfExp extends ExpBase<'IF'> {
|
|
220
|
-
test: Exp;
|
|
221
|
-
consequent: Exp;
|
|
222
|
-
alternative?: Exp;
|
|
223
|
-
}
|
|
224
|
-
interface IdExp extends ExpBase<'ID'> {
|
|
225
|
-
value: string;
|
|
226
|
-
path?: IdExp[];
|
|
227
|
-
args?: Exp[];
|
|
228
|
-
}
|
|
229
|
-
interface LitExp<T = string | number | boolean | null | undefined> extends ExpBase<'LIT'> {
|
|
230
|
-
value: T;
|
|
231
|
-
}
|
|
232
|
-
interface SeqExp extends ExpBase<'SEQ'> {
|
|
233
|
-
elements: Exp[];
|
|
234
|
-
}
|
|
235
|
-
type Exp = NotExp | BinaryExp | IfExp | IdExp | LitExp | SeqExp;
|
|
236
221
|
type Filters = Record<string, (value: any, ...args: any[]) => any>;
|
|
237
222
|
type Script = (globals: ObjectType, escape: (v: any) => any, filters: Filters) => Promise<string>;
|
|
238
|
-
interface Tag {
|
|
239
|
-
name: string;
|
|
240
|
-
value?: Exp | null;
|
|
241
|
-
raw: string;
|
|
242
|
-
previous: Tag | null;
|
|
243
|
-
next: Tag | null;
|
|
244
|
-
strip?: Strip;
|
|
245
|
-
loc?: Loc;
|
|
246
|
-
}
|
|
247
|
-
interface NodeCompiler {
|
|
248
|
-
names: string[];
|
|
249
|
-
/**
|
|
250
|
-
* - Return `Range` to indicate the range of the tag.
|
|
251
|
-
* - Return `false` to pass the control to the next tag.
|
|
252
|
-
* - Return `void` to continue compiling.
|
|
253
|
-
*/
|
|
254
|
-
compile: (arg: {
|
|
255
|
-
tag: Tag;
|
|
256
|
-
ctx: Context;
|
|
257
|
-
out: OutScript;
|
|
258
|
-
}) => MaybePromise<Loc | void | false>;
|
|
259
|
-
}
|
|
260
223
|
interface Plugin {
|
|
261
224
|
filters?: Filters;
|
|
262
225
|
parsers?: ParserMap;
|
|
263
226
|
compilers?: CompilerMap;
|
|
264
227
|
}
|
|
265
|
-
type ParserFn<T extends SyntaxNode =
|
|
266
|
-
type ParserMap = Record<string, ParserFn>;
|
|
267
|
-
type CompilerFn<T extends SyntaxNode =
|
|
268
|
-
type CompilerMap =
|
|
228
|
+
type ParserFn<T extends Token = any, U extends SyntaxNode = any> = (token: T, parser: Parser) => AsyncGenerator<U | 'NEXT' | void>;
|
|
229
|
+
type ParserMap = Record<string, 'unexpected' | ParserFn>;
|
|
230
|
+
type CompilerFn<T extends SyntaxNode = any> = (node: T, compiler: Compiler) => MaybePromise<void>;
|
|
231
|
+
type CompilerMap = Record<string, CompilerFn>;
|
|
232
|
+
|
|
269
233
|
interface BaseOptions {
|
|
270
234
|
debug?: (error: Error) => any;
|
|
271
235
|
filters?: Filters;
|
|
@@ -293,7 +257,7 @@ interface RendererOptions extends CompilerOptions {
|
|
|
293
257
|
|
|
294
258
|
declare class Renderer {
|
|
295
259
|
protected options: Required<RendererOptions>;
|
|
296
|
-
constructor(options
|
|
260
|
+
constructor(options?: RendererOptions);
|
|
297
261
|
render(template: string, globals: ObjectType): Promise<string>;
|
|
298
262
|
renderFile(filepath: string, globals: ObjectType): Promise<string>;
|
|
299
263
|
}
|
|
@@ -302,4 +266,4 @@ declare function render(template: string, data: ObjectType, options?: RendererOp
|
|
|
302
266
|
declare function renderFile(filepath: string, data: ObjectType, options?: RendererOptions): Promise<string>;
|
|
303
267
|
|
|
304
268
|
export { Renderer, TokenType, render, renderFile };
|
|
305
|
-
export type {
|
|
269
|
+
export type { BinaryExp, BinaryExpTokenType, BinaryExpType, CommentToken, CompilerFn, CompilerMap, DirectiveExpression, DirectiveToken, Exp, ExpBase, ExpTokenType, ExpType, Filters, IdExp, IfExp, LitExp, Loc, MaybePromise, NotExp, ObjectType, OutputToken, ParserFn, ParserMap, Plugin, Pos, Primitive, Script, SeqExp, Strip, TernaryExpTokenType, TernaryExpType, TextToken, Token, TokenBase, UnaryExpTokenType, UnaryExpType };
|