janja 2.4.1 → 2.4.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
@@ -884,7 +884,6 @@ class CommentNode extends Traversal {
884
884
  }
885
885
 
886
886
  async function* parseComment({ val, loc, strip }) {
887
- console.log(val);
888
887
  yield 'NEXT';
889
888
  yield new CommentNode(val, loc, strip);
890
889
  }
@@ -1345,17 +1344,16 @@ const plugin$6 = {
1345
1344
  parsers: parsers$6,
1346
1345
  };
1347
1346
 
1348
- async function compileMacro({ val: { left, right }, loc, body }, compiler) {
1347
+ async function compileMacro({ val: { value, args }, loc, body }, compiler) {
1349
1348
  const { context } = compiler;
1350
- const { elements } = right;
1351
1349
  const expCompiler = new ExpCompiler();
1352
- compiler.pushRaw(loc, `${expCompiler.compile(left)}=(${elements
1353
- .map((el) => el.type === 'ASSIGN'
1350
+ compiler.pushRaw(loc, `${context}.${value}=(${args
1351
+ ?.map((el) => el.type === 'ASSIGN'
1354
1352
  ? `${el.left.value}=${expCompiler.compile(el.right)}`
1355
1353
  : el.value)
1356
- .join(',')})=>async(_c)=>{`, `const ${compiler.in()}={`, `...${context},`, elements
1357
- .map((el) => (el.type === 'ASSIGN' ? el.left.value : el.value))
1358
- .join(','), '};');
1354
+ .join(',') ?? ''})=>async(_c)=>{`, `const ${compiler.in()}={`, `...${context},`, args
1355
+ ?.map((el) => (el.type === 'ASSIGN' ? el.left.value : el.value))
1356
+ .join(',') ?? '', '};');
1359
1357
  await compiler.compileNodes(body);
1360
1358
  compiler.out();
1361
1359
  compiler.pushRaw(null, '};');
@@ -1449,7 +1447,26 @@ async function* parseMacro(token, parser) {
1449
1447
  else {
1450
1448
  throw parser.options.debug?.(new CompileError(`Unclosed "${token.name}"`, parser.template, token.loc));
1451
1449
  }
1452
- yield new MacroNode(parser.parseExp(token.expression), body, token.loc, token.strip);
1450
+ const val = parser.parseExp(token.expression);
1451
+ if (val.type !== 'ID') {
1452
+ parser.options.debug?.(new CompileError('Invalid macro definition', parser.template, token.loc));
1453
+ return;
1454
+ }
1455
+ if (val.args?.length) {
1456
+ for (const arg of val.args) {
1457
+ if (arg.type === 'ASSIGN') {
1458
+ if (arg.left.type !== 'ID') {
1459
+ parser.options.debug?.(new CompileError('Invalid macro parameter definition', parser.template, token.loc));
1460
+ return;
1461
+ }
1462
+ }
1463
+ else if (arg.type !== 'ID') {
1464
+ parser.options.debug?.(new CompileError('Invalid macro parameter definition', parser.template, token.loc));
1465
+ return;
1466
+ }
1467
+ }
1468
+ }
1469
+ yield new MacroNode(val, body, token.loc, token.strip);
1453
1470
  }
1454
1471
  async function* parseCaller(token, parser) {
1455
1472
  if (token.expression) {
@@ -2276,6 +2293,16 @@ class ExpParser {
2276
2293
  if (!left) {
2277
2294
  throw new ExpError(`No left operand for "${token.type}"`, token.loc);
2278
2295
  }
2296
+ if (token.type === 'ASSIGN') {
2297
+ if (left.type === 'SEQ') {
2298
+ if (left.elements.some((el) => el.type !== 'ID')) {
2299
+ throw new ExpError('Left operand of assignment must be an identifier or a sequence of identifiers', token.loc);
2300
+ }
2301
+ }
2302
+ else if (left.type !== 'ID') {
2303
+ throw new ExpError('Left operand of assignment must be an identifier or a sequence of identifiers', token.loc);
2304
+ }
2305
+ }
2279
2306
  const right = this.parseExp((t) => isHigher(token, t) ? 'BACK' : undefined);
2280
2307
  if (!right) {
2281
2308
  throw new ExpError(`No right operand for "${token.type}"`, token.loc);
@@ -2352,10 +2379,7 @@ class ExpParser {
2352
2379
  };
2353
2380
  const lp = this.consume('LP');
2354
2381
  if (lp) {
2355
- const args = this.parseSequence();
2356
- if (args.length) {
2357
- right.args = args;
2358
- }
2382
+ right.args = this.parseSequence();
2359
2383
  if (!this.consume('RP')) {
2360
2384
  throw new ExpError('Expected "RP" after "LP"', lp.loc);
2361
2385
  }
@@ -2837,7 +2861,7 @@ class RenderError extends CompileError {
2837
2861
  writable: true,
2838
2862
  value: getLoc
2839
2863
  });
2840
- this.name = 'CompileError';
2864
+ this.name = 'RenderError';
2841
2865
  RenderError.captureStackTrace?.(this, this.constructor);
2842
2866
  }
2843
2867
  get details() {
package/dist/index.d.ts CHANGED
@@ -83,10 +83,10 @@ interface IfExp extends ExpBase<'IF'> {
83
83
  consequent: Exp;
84
84
  alternative?: Exp;
85
85
  }
86
- interface IdExp extends ExpBase<'ID'> {
86
+ interface IdExp<T extends Exp = Exp> extends ExpBase<'ID'> {
87
87
  value: string;
88
88
  path?: IdExp[];
89
- args?: Exp[];
89
+ args?: T[];
90
90
  }
91
91
  interface LitExp<T = Primitive> extends ExpBase<'LIT'> {
92
92
  value: T;
package/dist/index.esm.js CHANGED
@@ -882,7 +882,6 @@ class CommentNode extends Traversal {
882
882
  }
883
883
 
884
884
  async function* parseComment({ val, loc, strip }) {
885
- console.log(val);
886
885
  yield 'NEXT';
887
886
  yield new CommentNode(val, loc, strip);
888
887
  }
@@ -1343,17 +1342,16 @@ const plugin$6 = {
1343
1342
  parsers: parsers$6,
1344
1343
  };
1345
1344
 
1346
- async function compileMacro({ val: { left, right }, loc, body }, compiler) {
1345
+ async function compileMacro({ val: { value, args }, loc, body }, compiler) {
1347
1346
  const { context } = compiler;
1348
- const { elements } = right;
1349
1347
  const expCompiler = new ExpCompiler();
1350
- compiler.pushRaw(loc, `${expCompiler.compile(left)}=(${elements
1351
- .map((el) => el.type === 'ASSIGN'
1348
+ compiler.pushRaw(loc, `${context}.${value}=(${args
1349
+ ?.map((el) => el.type === 'ASSIGN'
1352
1350
  ? `${el.left.value}=${expCompiler.compile(el.right)}`
1353
1351
  : el.value)
1354
- .join(',')})=>async(_c)=>{`, `const ${compiler.in()}={`, `...${context},`, elements
1355
- .map((el) => (el.type === 'ASSIGN' ? el.left.value : el.value))
1356
- .join(','), '};');
1352
+ .join(',') ?? ''})=>async(_c)=>{`, `const ${compiler.in()}={`, `...${context},`, args
1353
+ ?.map((el) => (el.type === 'ASSIGN' ? el.left.value : el.value))
1354
+ .join(',') ?? '', '};');
1357
1355
  await compiler.compileNodes(body);
1358
1356
  compiler.out();
1359
1357
  compiler.pushRaw(null, '};');
@@ -1447,7 +1445,26 @@ async function* parseMacro(token, parser) {
1447
1445
  else {
1448
1446
  throw parser.options.debug?.(new CompileError(`Unclosed "${token.name}"`, parser.template, token.loc));
1449
1447
  }
1450
- yield new MacroNode(parser.parseExp(token.expression), body, token.loc, token.strip);
1448
+ const val = parser.parseExp(token.expression);
1449
+ if (val.type !== 'ID') {
1450
+ parser.options.debug?.(new CompileError('Invalid macro definition', parser.template, token.loc));
1451
+ return;
1452
+ }
1453
+ if (val.args?.length) {
1454
+ for (const arg of val.args) {
1455
+ if (arg.type === 'ASSIGN') {
1456
+ if (arg.left.type !== 'ID') {
1457
+ parser.options.debug?.(new CompileError('Invalid macro parameter definition', parser.template, token.loc));
1458
+ return;
1459
+ }
1460
+ }
1461
+ else if (arg.type !== 'ID') {
1462
+ parser.options.debug?.(new CompileError('Invalid macro parameter definition', parser.template, token.loc));
1463
+ return;
1464
+ }
1465
+ }
1466
+ }
1467
+ yield new MacroNode(val, body, token.loc, token.strip);
1451
1468
  }
1452
1469
  async function* parseCaller(token, parser) {
1453
1470
  if (token.expression) {
@@ -2274,6 +2291,16 @@ class ExpParser {
2274
2291
  if (!left) {
2275
2292
  throw new ExpError(`No left operand for "${token.type}"`, token.loc);
2276
2293
  }
2294
+ if (token.type === 'ASSIGN') {
2295
+ if (left.type === 'SEQ') {
2296
+ if (left.elements.some((el) => el.type !== 'ID')) {
2297
+ throw new ExpError('Left operand of assignment must be an identifier or a sequence of identifiers', token.loc);
2298
+ }
2299
+ }
2300
+ else if (left.type !== 'ID') {
2301
+ throw new ExpError('Left operand of assignment must be an identifier or a sequence of identifiers', token.loc);
2302
+ }
2303
+ }
2277
2304
  const right = this.parseExp((t) => isHigher(token, t) ? 'BACK' : undefined);
2278
2305
  if (!right) {
2279
2306
  throw new ExpError(`No right operand for "${token.type}"`, token.loc);
@@ -2350,10 +2377,7 @@ class ExpParser {
2350
2377
  };
2351
2378
  const lp = this.consume('LP');
2352
2379
  if (lp) {
2353
- const args = this.parseSequence();
2354
- if (args.length) {
2355
- right.args = args;
2356
- }
2380
+ right.args = this.parseSequence();
2357
2381
  if (!this.consume('RP')) {
2358
2382
  throw new ExpError('Expected "RP" after "LP"', lp.loc);
2359
2383
  }
@@ -2835,7 +2859,7 @@ class RenderError extends CompileError {
2835
2859
  writable: true,
2836
2860
  value: getLoc
2837
2861
  });
2838
- this.name = 'CompileError';
2862
+ this.name = 'RenderError';
2839
2863
  RenderError.captureStackTrace?.(this, this.constructor);
2840
2864
  }
2841
2865
  get details() {
package/dist/index.js CHANGED
@@ -885,7 +885,6 @@ ${output
885
885
  }
886
886
 
887
887
  async function* parseComment({ val, loc, strip }) {
888
- console.log(val);
889
888
  yield 'NEXT';
890
889
  yield new CommentNode(val, loc, strip);
891
890
  }
@@ -1346,17 +1345,16 @@ ${output
1346
1345
  parsers: parsers$6,
1347
1346
  };
1348
1347
 
1349
- async function compileMacro({ val: { left, right }, loc, body }, compiler) {
1348
+ async function compileMacro({ val: { value, args }, loc, body }, compiler) {
1350
1349
  const { context } = compiler;
1351
- const { elements } = right;
1352
1350
  const expCompiler = new ExpCompiler();
1353
- compiler.pushRaw(loc, `${expCompiler.compile(left)}=(${elements
1354
- .map((el) => el.type === 'ASSIGN'
1351
+ compiler.pushRaw(loc, `${context}.${value}=(${args
1352
+ ?.map((el) => el.type === 'ASSIGN'
1355
1353
  ? `${el.left.value}=${expCompiler.compile(el.right)}`
1356
1354
  : el.value)
1357
- .join(',')})=>async(_c)=>{`, `const ${compiler.in()}={`, `...${context},`, elements
1358
- .map((el) => (el.type === 'ASSIGN' ? el.left.value : el.value))
1359
- .join(','), '};');
1355
+ .join(',') ?? ''})=>async(_c)=>{`, `const ${compiler.in()}={`, `...${context},`, args
1356
+ ?.map((el) => (el.type === 'ASSIGN' ? el.left.value : el.value))
1357
+ .join(',') ?? '', '};');
1360
1358
  await compiler.compileNodes(body);
1361
1359
  compiler.out();
1362
1360
  compiler.pushRaw(null, '};');
@@ -1450,7 +1448,26 @@ ${output
1450
1448
  else {
1451
1449
  throw parser.options.debug?.(new CompileError(`Unclosed "${token.name}"`, parser.template, token.loc));
1452
1450
  }
1453
- yield new MacroNode(parser.parseExp(token.expression), body, token.loc, token.strip);
1451
+ const val = parser.parseExp(token.expression);
1452
+ if (val.type !== 'ID') {
1453
+ parser.options.debug?.(new CompileError('Invalid macro definition', parser.template, token.loc));
1454
+ return;
1455
+ }
1456
+ if (val.args?.length) {
1457
+ for (const arg of val.args) {
1458
+ if (arg.type === 'ASSIGN') {
1459
+ if (arg.left.type !== 'ID') {
1460
+ parser.options.debug?.(new CompileError('Invalid macro parameter definition', parser.template, token.loc));
1461
+ return;
1462
+ }
1463
+ }
1464
+ else if (arg.type !== 'ID') {
1465
+ parser.options.debug?.(new CompileError('Invalid macro parameter definition', parser.template, token.loc));
1466
+ return;
1467
+ }
1468
+ }
1469
+ }
1470
+ yield new MacroNode(val, body, token.loc, token.strip);
1454
1471
  }
1455
1472
  async function* parseCaller(token, parser) {
1456
1473
  if (token.expression) {
@@ -2277,6 +2294,16 @@ ${output
2277
2294
  if (!left) {
2278
2295
  throw new ExpError(`No left operand for "${token.type}"`, token.loc);
2279
2296
  }
2297
+ if (token.type === 'ASSIGN') {
2298
+ if (left.type === 'SEQ') {
2299
+ if (left.elements.some((el) => el.type !== 'ID')) {
2300
+ throw new ExpError('Left operand of assignment must be an identifier or a sequence of identifiers', token.loc);
2301
+ }
2302
+ }
2303
+ else if (left.type !== 'ID') {
2304
+ throw new ExpError('Left operand of assignment must be an identifier or a sequence of identifiers', token.loc);
2305
+ }
2306
+ }
2280
2307
  const right = this.parseExp((t) => isHigher(token, t) ? 'BACK' : undefined);
2281
2308
  if (!right) {
2282
2309
  throw new ExpError(`No right operand for "${token.type}"`, token.loc);
@@ -2353,10 +2380,7 @@ ${output
2353
2380
  };
2354
2381
  const lp = this.consume('LP');
2355
2382
  if (lp) {
2356
- const args = this.parseSequence();
2357
- if (args.length) {
2358
- right.args = args;
2359
- }
2383
+ right.args = this.parseSequence();
2360
2384
  if (!this.consume('RP')) {
2361
2385
  throw new ExpError('Expected "RP" after "LP"', lp.loc);
2362
2386
  }
@@ -2838,7 +2862,7 @@ ${output
2838
2862
  writable: true,
2839
2863
  value: getLoc
2840
2864
  });
2841
- this.name = 'CompileError';
2865
+ this.name = 'RenderError';
2842
2866
  RenderError.captureStackTrace?.(this, this.constructor);
2843
2867
  }
2844
2868
  get details() {
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "janja",
3
3
  "type": "module",
4
- "version": "2.4.1",
4
+ "version": "2.4.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",