janja 2.1.2 → 2.2.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 CHANGED
@@ -365,7 +365,7 @@ function values(value = {}) {
365
365
  return Object.values(value);
366
366
  }
367
367
 
368
- var _filters = /*#__PURE__*/Object.freeze({
368
+ var filters = /*#__PURE__*/Object.freeze({
369
369
  __proto__: null,
370
370
  abs: abs,
371
371
  add: add,
@@ -660,7 +660,8 @@ class ExpCompiler {
660
660
  return this.compileTernary(expression);
661
661
  case 'SEQ':
662
662
  return this.compileSeq(expression);
663
- // no default
663
+ default:
664
+ return '';
664
665
  }
665
666
  }
666
667
  compileNot({ argument }) {
@@ -1354,7 +1355,7 @@ async function compileMacro({ val: { left, right }, loc, body }, compiler) {
1354
1355
  ? `${el.left.value}=${expCompiler.compile(el.right)}`
1355
1356
  : el.value)
1356
1357
  .join(',')})=>async(_c)=>{`, `const ${compiler.in()}={`, `...${context},`, elements
1357
- .map((el) => el.type === 'ASSIGN' ? el.left.value : el.value)
1358
+ .map((el) => (el.type === 'ASSIGN' ? el.left.value : el.value))
1358
1359
  .join(','), '};');
1359
1360
  await compiler.compileNodes(body);
1360
1361
  compiler.out();
@@ -1773,7 +1774,7 @@ const plugins = [
1773
1774
  ];
1774
1775
 
1775
1776
  const baseOptions = {
1776
- filters: _filters,
1777
+ filters,
1777
1778
  parsers: {},
1778
1779
  compilers: {},
1779
1780
  plugins,
@@ -2601,8 +2602,8 @@ class Parser extends Tokenizer {
2601
2602
  }
2602
2603
  let parser = this.options.parsers[token.name?.toLowerCase() ??
2603
2604
  token.type?.toLowerCase()];
2604
- if (parser === 'unexpected') {
2605
- parser = this.options.parsers['unexpected'];
2605
+ if (typeof parser === 'string') {
2606
+ parser = this.options.parsers[parser];
2606
2607
  }
2607
2608
  if (!parser) {
2608
2609
  parser = this.options.parsers['unknown'];
@@ -2779,12 +2780,14 @@ class Compiler extends Context {
2779
2780
  await this.compileNodes(node.body);
2780
2781
  return;
2781
2782
  }
2782
- const compiler = this.options.compilers[node.type];
2783
- if (compiler) {
2784
- await compiler(node, this);
2785
- return;
2783
+ let compiler = this.options.compilers[node.type];
2784
+ if (typeof compiler === 'string') {
2785
+ compiler = this.options.compilers[compiler];
2786
+ }
2787
+ if (!compiler) {
2788
+ compiler = this.options.compilers['UNKNOWN'];
2786
2789
  }
2787
- this.options.debug?.(new CompileError(`Unknown "${node.name}" node`, this.template, node.loc));
2790
+ await compiler?.(node, this);
2788
2791
  }
2789
2792
  async compileNodes(nodes) {
2790
2793
  for (const child of nodes) {
@@ -2840,12 +2843,12 @@ class Renderer {
2840
2843
  const compiler = new Compiler(this.options);
2841
2844
  try {
2842
2845
  await compiler.compile(template);
2843
- return await compiler.script({ ...this.options.globals, ...globals }, (v) => {
2846
+ return await compiler.script({ ...globals, ...this.options.globals }, (v) => {
2844
2847
  if (v instanceof Safe) {
2845
2848
  return `${v}`;
2846
2849
  }
2847
2850
  return this.options.autoEscape ? escape(v) : v;
2848
- }, { ...this.options.filters, ..._filters });
2851
+ }, { ...filters, ...this.options.filters });
2849
2852
  }
2850
2853
  catch (error) {
2851
2854
  this.options.debug((() => {
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;
@@ -122,7 +163,7 @@ declare class Parser extends Tokenizer {
122
163
  protected cursor: number;
123
164
  parse(template: string): Promise<RootNode>;
124
165
  parseUntil(names?: string[]): Promise<SyntaxNode[]>;
125
- parseExp({ val, loc }: DirectiveExpression): Exp;
166
+ parseExp<T extends Exp = Exp>({ val, loc }: DirectiveExpression): T;
126
167
  peek(): Token | null;
127
168
  private advance;
128
169
  match(names: string[], token?: Token | null): boolean;
@@ -131,6 +172,7 @@ declare class Parser extends Tokenizer {
131
172
 
132
173
  type MaybePromise<T> = T | Promise<T>;
133
174
  type ObjectType<T = any> = Record<string, T>;
175
+ type Primitive = string | number | boolean | null | undefined;
134
176
  declare enum TokenType {
135
177
  TEXT = "TEXT",
136
178
  OUTPUT = "OUTPUT",
@@ -176,87 +218,18 @@ interface DirectiveExpression {
176
218
  loc: Loc;
177
219
  }
178
220
  type Token = CommentToken | DirectiveToken | OutputToken | TextToken;
179
- interface Mapping {
180
- source: Loc;
181
- target: Loc;
182
- }
183
- type UnaryExpTokenType = 'NOT';
184
- type BinaryExpTokenType = 'AND' | 'OR' | 'IS' | 'EQ' | 'NE' | 'GT' | 'LT' | 'GE' | 'LE' | 'IN' | 'NI' | 'OF' | 'ADD' | 'SUB' | 'MUL' | 'DIV' | 'MOD' | 'PIPE' | 'ASSIGN';
185
- type TernaryExpTokenType = 'IF' | 'ELSE';
186
- type ExpTokenType = UnaryExpTokenType | BinaryExpTokenType | TernaryExpTokenType | 'LP' | 'RP' | 'COMMA' | 'DOT' | 'ID' | 'LIT';
187
- type Primitive = string | number | boolean | null | undefined;
188
- interface ExpToken<T = ExpTokenType> {
189
- type: T;
190
- value: Primitive;
191
- loc: Loc;
192
- raw?: string;
193
- }
194
- type Checker = (token: ExpToken) => 'BACK' | 'BREAK' | undefined;
195
- type UnaryExpType = UnaryExpTokenType;
196
- type BinaryExpType = BinaryExpTokenType;
197
- type TernaryExpType = TernaryExpTokenType;
198
- type ExpType = UnaryExpType | BinaryExpType | TernaryExpType | 'SEQ' | 'ID' | 'LIT';
199
- interface ExpBase<T = ExpType> {
200
- type: T;
201
- loc: Loc;
202
- }
203
- interface NotExp extends ExpBase<'NOT'> {
204
- argument: Exp;
205
- }
206
- interface BinaryExp<T extends BinaryExpType = any> extends ExpBase<T> {
207
- left: Exp;
208
- right: Exp;
209
- }
210
- interface IfExp extends ExpBase<'IF'> {
211
- test: Exp;
212
- consequent: Exp;
213
- alternative?: Exp;
214
- }
215
- interface IdExp extends ExpBase<'ID'> {
216
- value: string;
217
- path?: IdExp[];
218
- args?: Exp[];
219
- }
220
- interface LitExp<T = Primitive> extends ExpBase<'LIT'> {
221
- value: T;
222
- }
223
- interface SeqExp extends ExpBase<'SEQ'> {
224
- elements: Exp[];
225
- }
226
- type Exp = NotExp | BinaryExp | IfExp | IdExp | LitExp | SeqExp;
227
221
  type Filters = Record<string, (value: any, ...args: any[]) => any>;
228
222
  type Script = (globals: ObjectType, escape: (v: any) => any, filters: Filters) => Promise<string>;
229
- interface Tag {
230
- name: string;
231
- value?: Exp | null;
232
- raw: string;
233
- previous: Tag | null;
234
- next: Tag | null;
235
- strip?: Strip;
236
- loc?: Loc;
237
- }
238
- interface NodeCompiler {
239
- names: string[];
240
- /**
241
- * - Return `Range` to indicate the range of the tag.
242
- * - Return `false` to pass the control to the next tag.
243
- * - Return `void` to continue compiling.
244
- */
245
- compile: (arg: {
246
- tag: Tag;
247
- ctx: Context;
248
- out: OutScript;
249
- }) => MaybePromise<Loc | void | false>;
250
- }
251
223
  interface Plugin {
252
224
  filters?: Filters;
253
225
  parsers?: ParserMap;
254
226
  compilers?: CompilerMap;
255
227
  }
256
228
  type ParserFn<T extends Token = any, U extends SyntaxNode = any> = (token: T, parser: Parser) => AsyncGenerator<U | 'NEXT' | void>;
257
- type ParserMap = Record<string, 'unexpected' | ParserFn>;
229
+ type ParserMap = Record<string, string | ParserFn>;
258
230
  type CompilerFn<T extends SyntaxNode = any> = (node: T, compiler: Compiler) => MaybePromise<void>;
259
- type CompilerMap = Record<string, CompilerFn>;
231
+ type CompilerMap = Record<string, string | CompilerFn>;
232
+
260
233
  interface BaseOptions {
261
234
  debug?: (error: Error) => any;
262
235
  filters?: Filters;
@@ -284,7 +257,7 @@ interface RendererOptions extends CompilerOptions {
284
257
 
285
258
  declare class Renderer {
286
259
  protected options: Required<RendererOptions>;
287
- constructor(options: RendererOptions);
260
+ constructor(options?: RendererOptions);
288
261
  render(template: string, globals: ObjectType): Promise<string>;
289
262
  renderFile(filepath: string, globals: ObjectType): Promise<string>;
290
263
  }
@@ -293,4 +266,4 @@ declare function render(template: string, data: ObjectType, options?: RendererOp
293
266
  declare function renderFile(filepath: string, data: ObjectType, options?: RendererOptions): Promise<string>;
294
267
 
295
268
  export { Renderer, TokenType, render, renderFile };
296
- export type { BaseOptions, BinaryExp, BinaryExpTokenType, BinaryExpType, 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, Plugin, Pos, Primitive, RendererOptions, Script, SeqExp, Strip, Tag, TernaryExpTokenType, TernaryExpType, TextToken, Token, TokenBase, UnaryExpTokenType, UnaryExpType };
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 };
package/dist/index.esm.js CHANGED
@@ -363,7 +363,7 @@ function values(value = {}) {
363
363
  return Object.values(value);
364
364
  }
365
365
 
366
- var _filters = /*#__PURE__*/Object.freeze({
366
+ var filters = /*#__PURE__*/Object.freeze({
367
367
  __proto__: null,
368
368
  abs: abs,
369
369
  add: add,
@@ -658,7 +658,8 @@ class ExpCompiler {
658
658
  return this.compileTernary(expression);
659
659
  case 'SEQ':
660
660
  return this.compileSeq(expression);
661
- // no default
661
+ default:
662
+ return '';
662
663
  }
663
664
  }
664
665
  compileNot({ argument }) {
@@ -1352,7 +1353,7 @@ async function compileMacro({ val: { left, right }, loc, body }, compiler) {
1352
1353
  ? `${el.left.value}=${expCompiler.compile(el.right)}`
1353
1354
  : el.value)
1354
1355
  .join(',')})=>async(_c)=>{`, `const ${compiler.in()}={`, `...${context},`, elements
1355
- .map((el) => el.type === 'ASSIGN' ? el.left.value : el.value)
1356
+ .map((el) => (el.type === 'ASSIGN' ? el.left.value : el.value))
1356
1357
  .join(','), '};');
1357
1358
  await compiler.compileNodes(body);
1358
1359
  compiler.out();
@@ -1771,7 +1772,7 @@ const plugins = [
1771
1772
  ];
1772
1773
 
1773
1774
  const baseOptions = {
1774
- filters: _filters,
1775
+ filters,
1775
1776
  parsers: {},
1776
1777
  compilers: {},
1777
1778
  plugins,
@@ -2599,8 +2600,8 @@ class Parser extends Tokenizer {
2599
2600
  }
2600
2601
  let parser = this.options.parsers[token.name?.toLowerCase() ??
2601
2602
  token.type?.toLowerCase()];
2602
- if (parser === 'unexpected') {
2603
- parser = this.options.parsers['unexpected'];
2603
+ if (typeof parser === 'string') {
2604
+ parser = this.options.parsers[parser];
2604
2605
  }
2605
2606
  if (!parser) {
2606
2607
  parser = this.options.parsers['unknown'];
@@ -2777,12 +2778,14 @@ class Compiler extends Context {
2777
2778
  await this.compileNodes(node.body);
2778
2779
  return;
2779
2780
  }
2780
- const compiler = this.options.compilers[node.type];
2781
- if (compiler) {
2782
- await compiler(node, this);
2783
- return;
2781
+ let compiler = this.options.compilers[node.type];
2782
+ if (typeof compiler === 'string') {
2783
+ compiler = this.options.compilers[compiler];
2784
+ }
2785
+ if (!compiler) {
2786
+ compiler = this.options.compilers['UNKNOWN'];
2784
2787
  }
2785
- this.options.debug?.(new CompileError(`Unknown "${node.name}" node`, this.template, node.loc));
2788
+ await compiler?.(node, this);
2786
2789
  }
2787
2790
  async compileNodes(nodes) {
2788
2791
  for (const child of nodes) {
@@ -2838,12 +2841,12 @@ class Renderer {
2838
2841
  const compiler = new Compiler(this.options);
2839
2842
  try {
2840
2843
  await compiler.compile(template);
2841
- return await compiler.script({ ...this.options.globals, ...globals }, (v) => {
2844
+ return await compiler.script({ ...globals, ...this.options.globals }, (v) => {
2842
2845
  if (v instanceof Safe) {
2843
2846
  return `${v}`;
2844
2847
  }
2845
2848
  return this.options.autoEscape ? escape(v) : v;
2846
- }, { ...this.options.filters, ..._filters });
2849
+ }, { ...filters, ...this.options.filters });
2847
2850
  }
2848
2851
  catch (error) {
2849
2852
  this.options.debug((() => {
package/dist/index.js CHANGED
@@ -366,7 +366,7 @@ ${output
366
366
  return Object.values(value);
367
367
  }
368
368
 
369
- var _filters = /*#__PURE__*/Object.freeze({
369
+ var filters = /*#__PURE__*/Object.freeze({
370
370
  __proto__: null,
371
371
  abs: abs,
372
372
  add: add,
@@ -661,7 +661,8 @@ ${output
661
661
  return this.compileTernary(expression);
662
662
  case 'SEQ':
663
663
  return this.compileSeq(expression);
664
- // no default
664
+ default:
665
+ return '';
665
666
  }
666
667
  }
667
668
  compileNot({ argument }) {
@@ -1355,7 +1356,7 @@ ${output
1355
1356
  ? `${el.left.value}=${expCompiler.compile(el.right)}`
1356
1357
  : el.value)
1357
1358
  .join(',')})=>async(_c)=>{`, `const ${compiler.in()}={`, `...${context},`, elements
1358
- .map((el) => el.type === 'ASSIGN' ? el.left.value : el.value)
1359
+ .map((el) => (el.type === 'ASSIGN' ? el.left.value : el.value))
1359
1360
  .join(','), '};');
1360
1361
  await compiler.compileNodes(body);
1361
1362
  compiler.out();
@@ -1774,7 +1775,7 @@ ${output
1774
1775
  ];
1775
1776
 
1776
1777
  const baseOptions = {
1777
- filters: _filters,
1778
+ filters,
1778
1779
  parsers: {},
1779
1780
  compilers: {},
1780
1781
  plugins,
@@ -2602,8 +2603,8 @@ ${output
2602
2603
  }
2603
2604
  let parser = this.options.parsers[token.name?.toLowerCase() ??
2604
2605
  token.type?.toLowerCase()];
2605
- if (parser === 'unexpected') {
2606
- parser = this.options.parsers['unexpected'];
2606
+ if (typeof parser === 'string') {
2607
+ parser = this.options.parsers[parser];
2607
2608
  }
2608
2609
  if (!parser) {
2609
2610
  parser = this.options.parsers['unknown'];
@@ -2780,12 +2781,14 @@ ${output
2780
2781
  await this.compileNodes(node.body);
2781
2782
  return;
2782
2783
  }
2783
- const compiler = this.options.compilers[node.type];
2784
- if (compiler) {
2785
- await compiler(node, this);
2786
- return;
2784
+ let compiler = this.options.compilers[node.type];
2785
+ if (typeof compiler === 'string') {
2786
+ compiler = this.options.compilers[compiler];
2787
+ }
2788
+ if (!compiler) {
2789
+ compiler = this.options.compilers['UNKNOWN'];
2787
2790
  }
2788
- this.options.debug?.(new CompileError(`Unknown "${node.name}" node`, this.template, node.loc));
2791
+ await compiler?.(node, this);
2789
2792
  }
2790
2793
  async compileNodes(nodes) {
2791
2794
  for (const child of nodes) {
@@ -2841,12 +2844,12 @@ ${output
2841
2844
  const compiler = new Compiler(this.options);
2842
2845
  try {
2843
2846
  await compiler.compile(template);
2844
- return await compiler.script({ ...this.options.globals, ...globals }, (v) => {
2847
+ return await compiler.script({ ...globals, ...this.options.globals }, (v) => {
2845
2848
  if (v instanceof Safe) {
2846
2849
  return `${v}`;
2847
2850
  }
2848
2851
  return this.options.autoEscape ? escape(v) : v;
2849
- }, { ...this.options.filters, ..._filters });
2852
+ }, { ...filters, ...this.options.filters });
2850
2853
  }
2851
2854
  catch (error) {
2852
2855
  this.options.debug((() => {
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "janja",
3
3
  "type": "module",
4
- "version": "2.1.2",
4
+ "version": "2.2.0",
5
5
  "packageManager": "yarn@4.9.2",
6
6
  "description": "A simple, fast, lightweight, and extensible template engine for Node.js and browsers.",
7
7
  "license": "MIT",