janja 2.1.2 → 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 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,
@@ -2779,12 +2780,8 @@ 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;
2786
- }
2787
- this.options.debug?.(new CompileError(`Unknown "${node.name}" node`, this.template, node.loc));
2783
+ const compiler = this.options.compilers[node.type] || this.options.compilers['UNKNOWN'];
2784
+ await compiler?.(node, this);
2788
2785
  }
2789
2786
  async compileNodes(nodes) {
2790
2787
  for (const child of nodes) {
@@ -2840,12 +2837,12 @@ class Renderer {
2840
2837
  const compiler = new Compiler(this.options);
2841
2838
  try {
2842
2839
  await compiler.compile(template);
2843
- return await compiler.script({ ...this.options.globals, ...globals }, (v) => {
2840
+ return await compiler.script({ ...globals, ...this.options.globals }, (v) => {
2844
2841
  if (v instanceof Safe) {
2845
2842
  return `${v}`;
2846
2843
  }
2847
2844
  return this.options.autoEscape ? escape(v) : v;
2848
- }, { ...this.options.filters, ..._filters });
2845
+ }, { ...filters, ...this.options.filters });
2849
2846
  }
2850
2847
  catch (error) {
2851
2848
  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,78 +218,8 @@ 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;
@@ -257,6 +229,7 @@ type ParserFn<T extends Token = any, U extends SyntaxNode = any> = (token: T, pa
257
229
  type ParserMap = Record<string, 'unexpected' | ParserFn>;
258
230
  type CompilerFn<T extends SyntaxNode = any> = (node: T, compiler: Compiler) => MaybePromise<void>;
259
231
  type CompilerMap = Record<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,
@@ -2777,12 +2778,8 @@ 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;
2784
- }
2785
- this.options.debug?.(new CompileError(`Unknown "${node.name}" node`, this.template, node.loc));
2781
+ const compiler = this.options.compilers[node.type] || this.options.compilers['UNKNOWN'];
2782
+ await compiler?.(node, this);
2786
2783
  }
2787
2784
  async compileNodes(nodes) {
2788
2785
  for (const child of nodes) {
@@ -2838,12 +2835,12 @@ class Renderer {
2838
2835
  const compiler = new Compiler(this.options);
2839
2836
  try {
2840
2837
  await compiler.compile(template);
2841
- return await compiler.script({ ...this.options.globals, ...globals }, (v) => {
2838
+ return await compiler.script({ ...globals, ...this.options.globals }, (v) => {
2842
2839
  if (v instanceof Safe) {
2843
2840
  return `${v}`;
2844
2841
  }
2845
2842
  return this.options.autoEscape ? escape(v) : v;
2846
- }, { ...this.options.filters, ..._filters });
2843
+ }, { ...filters, ...this.options.filters });
2847
2844
  }
2848
2845
  catch (error) {
2849
2846
  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,
@@ -2780,12 +2781,8 @@ ${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;
2787
- }
2788
- this.options.debug?.(new CompileError(`Unknown "${node.name}" node`, this.template, node.loc));
2784
+ const compiler = this.options.compilers[node.type] || this.options.compilers['UNKNOWN'];
2785
+ await compiler?.(node, this);
2789
2786
  }
2790
2787
  async compileNodes(nodes) {
2791
2788
  for (const child of nodes) {
@@ -2841,12 +2838,12 @@ ${output
2841
2838
  const compiler = new Compiler(this.options);
2842
2839
  try {
2843
2840
  await compiler.compile(template);
2844
- return await compiler.script({ ...this.options.globals, ...globals }, (v) => {
2841
+ return await compiler.script({ ...globals, ...this.options.globals }, (v) => {
2845
2842
  if (v instanceof Safe) {
2846
2843
  return `${v}`;
2847
2844
  }
2848
2845
  return this.options.autoEscape ? escape(v) : v;
2849
- }, { ...this.options.filters, ..._filters });
2846
+ }, { ...filters, ...this.options.filters });
2850
2847
  }
2851
2848
  catch (error) {
2852
2849
  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.1.3",
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",