janja 2.4.2 → 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
@@ -1344,22 +1344,16 @@ const plugin$6 = {
1344
1344
  parsers: parsers$6,
1345
1345
  };
1346
1346
 
1347
- async function compileMacro({ val, loc, body }, compiler) {
1347
+ async function compileMacro({ val: { value, args }, loc, body }, compiler) {
1348
1348
  const { context } = compiler;
1349
1349
  const expCompiler = new ExpCompiler();
1350
- if (val.type === 'ID') {
1351
- compiler.pushRaw(loc, `${expCompiler.compile(val)}=()=>async(_c)=>{`, `const ${compiler.in()}={`, `...${context},`, '};');
1352
- }
1353
- else {
1354
- const { left, right: { elements }, } = val;
1355
- compiler.pushRaw(loc, `${expCompiler.compile(left)}=(${elements
1356
- .map((el) => el.type === 'ASSIGN'
1357
- ? `${el.left.value}=${expCompiler.compile(el.right)}`
1358
- : el.value)
1359
- .join(',')})=>async(_c)=>{`, `const ${compiler.in()}={`, `...${context},`, elements
1360
- .map((el) => (el.type === 'ASSIGN' ? el.left.value : el.value))
1361
- .join(','), '};');
1362
- }
1350
+ compiler.pushRaw(loc, `${context}.${value}=(${args
1351
+ ?.map((el) => el.type === 'ASSIGN'
1352
+ ? `${el.left.value}=${expCompiler.compile(el.right)}`
1353
+ : el.value)
1354
+ .join(',') ?? ''})=>async(_c)=>{`, `const ${compiler.in()}={`, `...${context},`, args
1355
+ ?.map((el) => (el.type === 'ASSIGN' ? el.left.value : el.value))
1356
+ .join(',') ?? '', '};');
1363
1357
  await compiler.compileNodes(body);
1364
1358
  compiler.out();
1365
1359
  compiler.pushRaw(null, '};');
@@ -1454,34 +1448,24 @@ async function* parseMacro(token, parser) {
1454
1448
  throw parser.options.debug?.(new CompileError(`Unclosed "${token.name}"`, parser.template, token.loc));
1455
1449
  }
1456
1450
  const val = parser.parseExp(token.expression);
1457
- if (val.type === 'ASSIGN') {
1458
- if (val.left.type !== 'ID') {
1459
- parser.options.debug?.(new CompileError('Invalid macro name', parser.template, token.loc));
1460
- return;
1461
- }
1462
- if (val.right.type !== 'SEQ') {
1463
- parser.options.debug?.(new CompileError('Invalid macro parameters', parser.template, token.loc));
1464
- return;
1465
- }
1466
- else {
1467
- for (const element of val.right.elements) {
1468
- if (element.type === 'ASSIGN') {
1469
- if (element.left.type !== 'ID') {
1470
- parser.options.debug?.(new CompileError('Invalid parameter name', parser.template, token.loc));
1471
- return;
1472
- }
1473
- }
1474
- else if (element.type !== 'ID') {
1475
- parser.options.debug?.(new CompileError('Invalid macro parameter', parser.template, token.loc));
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));
1476
1460
  return;
1477
1461
  }
1478
1462
  }
1463
+ else if (arg.type !== 'ID') {
1464
+ parser.options.debug?.(new CompileError('Invalid macro parameter definition', parser.template, token.loc));
1465
+ return;
1466
+ }
1479
1467
  }
1480
1468
  }
1481
- else if (val.type !== 'ID') {
1482
- parser.options.debug?.(new CompileError('Invalid macro definition', parser.template, token.loc));
1483
- return;
1484
- }
1485
1469
  yield new MacroNode(val, body, token.loc, token.strip);
1486
1470
  }
1487
1471
  async function* parseCaller(token, parser) {
@@ -2395,10 +2379,7 @@ class ExpParser {
2395
2379
  };
2396
2380
  const lp = this.consume('LP');
2397
2381
  if (lp) {
2398
- const args = this.parseSequence();
2399
- if (args.length) {
2400
- right.args = args;
2401
- }
2382
+ right.args = this.parseSequence();
2402
2383
  if (!this.consume('RP')) {
2403
2384
  throw new ExpError('Expected "RP" after "LP"', lp.loc);
2404
2385
  }
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
@@ -1342,22 +1342,16 @@ const plugin$6 = {
1342
1342
  parsers: parsers$6,
1343
1343
  };
1344
1344
 
1345
- async function compileMacro({ val, loc, body }, compiler) {
1345
+ async function compileMacro({ val: { value, args }, loc, body }, compiler) {
1346
1346
  const { context } = compiler;
1347
1347
  const expCompiler = new ExpCompiler();
1348
- if (val.type === 'ID') {
1349
- compiler.pushRaw(loc, `${expCompiler.compile(val)}=()=>async(_c)=>{`, `const ${compiler.in()}={`, `...${context},`, '};');
1350
- }
1351
- else {
1352
- const { left, right: { elements }, } = val;
1353
- compiler.pushRaw(loc, `${expCompiler.compile(left)}=(${elements
1354
- .map((el) => el.type === 'ASSIGN'
1355
- ? `${el.left.value}=${expCompiler.compile(el.right)}`
1356
- : el.value)
1357
- .join(',')})=>async(_c)=>{`, `const ${compiler.in()}={`, `...${context},`, elements
1358
- .map((el) => (el.type === 'ASSIGN' ? el.left.value : el.value))
1359
- .join(','), '};');
1360
- }
1348
+ compiler.pushRaw(loc, `${context}.${value}=(${args
1349
+ ?.map((el) => el.type === 'ASSIGN'
1350
+ ? `${el.left.value}=${expCompiler.compile(el.right)}`
1351
+ : el.value)
1352
+ .join(',') ?? ''})=>async(_c)=>{`, `const ${compiler.in()}={`, `...${context},`, args
1353
+ ?.map((el) => (el.type === 'ASSIGN' ? el.left.value : el.value))
1354
+ .join(',') ?? '', '};');
1361
1355
  await compiler.compileNodes(body);
1362
1356
  compiler.out();
1363
1357
  compiler.pushRaw(null, '};');
@@ -1452,34 +1446,24 @@ async function* parseMacro(token, parser) {
1452
1446
  throw parser.options.debug?.(new CompileError(`Unclosed "${token.name}"`, parser.template, token.loc));
1453
1447
  }
1454
1448
  const val = parser.parseExp(token.expression);
1455
- if (val.type === 'ASSIGN') {
1456
- if (val.left.type !== 'ID') {
1457
- parser.options.debug?.(new CompileError('Invalid macro name', parser.template, token.loc));
1458
- return;
1459
- }
1460
- if (val.right.type !== 'SEQ') {
1461
- parser.options.debug?.(new CompileError('Invalid macro parameters', parser.template, token.loc));
1462
- return;
1463
- }
1464
- else {
1465
- for (const element of val.right.elements) {
1466
- if (element.type === 'ASSIGN') {
1467
- if (element.left.type !== 'ID') {
1468
- parser.options.debug?.(new CompileError('Invalid parameter name', parser.template, token.loc));
1469
- return;
1470
- }
1471
- }
1472
- else if (element.type !== 'ID') {
1473
- parser.options.debug?.(new CompileError('Invalid macro parameter', parser.template, token.loc));
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));
1474
1458
  return;
1475
1459
  }
1476
1460
  }
1461
+ else if (arg.type !== 'ID') {
1462
+ parser.options.debug?.(new CompileError('Invalid macro parameter definition', parser.template, token.loc));
1463
+ return;
1464
+ }
1477
1465
  }
1478
1466
  }
1479
- else if (val.type !== 'ID') {
1480
- parser.options.debug?.(new CompileError('Invalid macro definition', parser.template, token.loc));
1481
- return;
1482
- }
1483
1467
  yield new MacroNode(val, body, token.loc, token.strip);
1484
1468
  }
1485
1469
  async function* parseCaller(token, parser) {
@@ -2393,10 +2377,7 @@ class ExpParser {
2393
2377
  };
2394
2378
  const lp = this.consume('LP');
2395
2379
  if (lp) {
2396
- const args = this.parseSequence();
2397
- if (args.length) {
2398
- right.args = args;
2399
- }
2380
+ right.args = this.parseSequence();
2400
2381
  if (!this.consume('RP')) {
2401
2382
  throw new ExpError('Expected "RP" after "LP"', lp.loc);
2402
2383
  }
package/dist/index.js CHANGED
@@ -1345,22 +1345,16 @@ ${output
1345
1345
  parsers: parsers$6,
1346
1346
  };
1347
1347
 
1348
- async function compileMacro({ val, loc, body }, compiler) {
1348
+ async function compileMacro({ val: { value, args }, loc, body }, compiler) {
1349
1349
  const { context } = compiler;
1350
1350
  const expCompiler = new ExpCompiler();
1351
- if (val.type === 'ID') {
1352
- compiler.pushRaw(loc, `${expCompiler.compile(val)}=()=>async(_c)=>{`, `const ${compiler.in()}={`, `...${context},`, '};');
1353
- }
1354
- else {
1355
- const { left, right: { elements }, } = val;
1356
- compiler.pushRaw(loc, `${expCompiler.compile(left)}=(${elements
1357
- .map((el) => el.type === 'ASSIGN'
1358
- ? `${el.left.value}=${expCompiler.compile(el.right)}`
1359
- : el.value)
1360
- .join(',')})=>async(_c)=>{`, `const ${compiler.in()}={`, `...${context},`, elements
1361
- .map((el) => (el.type === 'ASSIGN' ? el.left.value : el.value))
1362
- .join(','), '};');
1363
- }
1351
+ compiler.pushRaw(loc, `${context}.${value}=(${args
1352
+ ?.map((el) => el.type === 'ASSIGN'
1353
+ ? `${el.left.value}=${expCompiler.compile(el.right)}`
1354
+ : el.value)
1355
+ .join(',') ?? ''})=>async(_c)=>{`, `const ${compiler.in()}={`, `...${context},`, args
1356
+ ?.map((el) => (el.type === 'ASSIGN' ? el.left.value : el.value))
1357
+ .join(',') ?? '', '};');
1364
1358
  await compiler.compileNodes(body);
1365
1359
  compiler.out();
1366
1360
  compiler.pushRaw(null, '};');
@@ -1455,34 +1449,24 @@ ${output
1455
1449
  throw parser.options.debug?.(new CompileError(`Unclosed "${token.name}"`, parser.template, token.loc));
1456
1450
  }
1457
1451
  const val = parser.parseExp(token.expression);
1458
- if (val.type === 'ASSIGN') {
1459
- if (val.left.type !== 'ID') {
1460
- parser.options.debug?.(new CompileError('Invalid macro name', parser.template, token.loc));
1461
- return;
1462
- }
1463
- if (val.right.type !== 'SEQ') {
1464
- parser.options.debug?.(new CompileError('Invalid macro parameters', parser.template, token.loc));
1465
- return;
1466
- }
1467
- else {
1468
- for (const element of val.right.elements) {
1469
- if (element.type === 'ASSIGN') {
1470
- if (element.left.type !== 'ID') {
1471
- parser.options.debug?.(new CompileError('Invalid parameter name', parser.template, token.loc));
1472
- return;
1473
- }
1474
- }
1475
- else if (element.type !== 'ID') {
1476
- parser.options.debug?.(new CompileError('Invalid macro parameter', parser.template, token.loc));
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));
1477
1461
  return;
1478
1462
  }
1479
1463
  }
1464
+ else if (arg.type !== 'ID') {
1465
+ parser.options.debug?.(new CompileError('Invalid macro parameter definition', parser.template, token.loc));
1466
+ return;
1467
+ }
1480
1468
  }
1481
1469
  }
1482
- else if (val.type !== 'ID') {
1483
- parser.options.debug?.(new CompileError('Invalid macro definition', parser.template, token.loc));
1484
- return;
1485
- }
1486
1470
  yield new MacroNode(val, body, token.loc, token.strip);
1487
1471
  }
1488
1472
  async function* parseCaller(token, parser) {
@@ -2396,10 +2380,7 @@ ${output
2396
2380
  };
2397
2381
  const lp = this.consume('LP');
2398
2382
  if (lp) {
2399
- const args = this.parseSequence();
2400
- if (args.length) {
2401
- right.args = args;
2402
- }
2383
+ right.args = this.parseSequence();
2403
2384
  if (!this.consume('RP')) {
2404
2385
  throw new ExpError('Expected "RP" after "LP"', lp.loc);
2405
2386
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "janja",
3
3
  "type": "module",
4
- "version": "2.4.2",
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",