janja 2.4.1 → 2.4.2

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,22 @@ 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, 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'
1354
- ? `${el.left.value}=${expCompiler.compile(el.right)}`
1355
- : el.value)
1356
- .join(',')})=>async(_c)=>{`, `const ${compiler.in()}={`, `...${context},`, elements
1357
- .map((el) => (el.type === 'ASSIGN' ? el.left.value : el.value))
1358
- .join(','), '};');
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
+ }
1359
1363
  await compiler.compileNodes(body);
1360
1364
  compiler.out();
1361
1365
  compiler.pushRaw(null, '};');
@@ -1449,7 +1453,36 @@ async function* parseMacro(token, parser) {
1449
1453
  else {
1450
1454
  throw parser.options.debug?.(new CompileError(`Unclosed "${token.name}"`, parser.template, token.loc));
1451
1455
  }
1452
- yield new MacroNode(parser.parseExp(token.expression), body, token.loc, token.strip);
1456
+ 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));
1476
+ return;
1477
+ }
1478
+ }
1479
+ }
1480
+ }
1481
+ else if (val.type !== 'ID') {
1482
+ parser.options.debug?.(new CompileError('Invalid macro definition', parser.template, token.loc));
1483
+ return;
1484
+ }
1485
+ yield new MacroNode(val, body, token.loc, token.strip);
1453
1486
  }
1454
1487
  async function* parseCaller(token, parser) {
1455
1488
  if (token.expression) {
@@ -2276,6 +2309,16 @@ class ExpParser {
2276
2309
  if (!left) {
2277
2310
  throw new ExpError(`No left operand for "${token.type}"`, token.loc);
2278
2311
  }
2312
+ if (token.type === 'ASSIGN') {
2313
+ if (left.type === 'SEQ') {
2314
+ if (left.elements.some((el) => el.type !== 'ID')) {
2315
+ throw new ExpError('Left operand of assignment must be an identifier or a sequence of identifiers', token.loc);
2316
+ }
2317
+ }
2318
+ else if (left.type !== 'ID') {
2319
+ throw new ExpError('Left operand of assignment must be an identifier or a sequence of identifiers', token.loc);
2320
+ }
2321
+ }
2279
2322
  const right = this.parseExp((t) => isHigher(token, t) ? 'BACK' : undefined);
2280
2323
  if (!right) {
2281
2324
  throw new ExpError(`No right operand for "${token.type}"`, token.loc);
@@ -2837,7 +2880,7 @@ class RenderError extends CompileError {
2837
2880
  writable: true,
2838
2881
  value: getLoc
2839
2882
  });
2840
- this.name = 'CompileError';
2883
+ this.name = 'RenderError';
2841
2884
  RenderError.captureStackTrace?.(this, this.constructor);
2842
2885
  }
2843
2886
  get details() {
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,22 @@ 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, 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'
1352
- ? `${el.left.value}=${expCompiler.compile(el.right)}`
1353
- : el.value)
1354
- .join(',')})=>async(_c)=>{`, `const ${compiler.in()}={`, `...${context},`, elements
1355
- .map((el) => (el.type === 'ASSIGN' ? el.left.value : el.value))
1356
- .join(','), '};');
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
+ }
1357
1361
  await compiler.compileNodes(body);
1358
1362
  compiler.out();
1359
1363
  compiler.pushRaw(null, '};');
@@ -1447,7 +1451,36 @@ async function* parseMacro(token, parser) {
1447
1451
  else {
1448
1452
  throw parser.options.debug?.(new CompileError(`Unclosed "${token.name}"`, parser.template, token.loc));
1449
1453
  }
1450
- yield new MacroNode(parser.parseExp(token.expression), body, token.loc, token.strip);
1454
+ 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));
1474
+ return;
1475
+ }
1476
+ }
1477
+ }
1478
+ }
1479
+ else if (val.type !== 'ID') {
1480
+ parser.options.debug?.(new CompileError('Invalid macro definition', parser.template, token.loc));
1481
+ return;
1482
+ }
1483
+ yield new MacroNode(val, body, token.loc, token.strip);
1451
1484
  }
1452
1485
  async function* parseCaller(token, parser) {
1453
1486
  if (token.expression) {
@@ -2274,6 +2307,16 @@ class ExpParser {
2274
2307
  if (!left) {
2275
2308
  throw new ExpError(`No left operand for "${token.type}"`, token.loc);
2276
2309
  }
2310
+ if (token.type === 'ASSIGN') {
2311
+ if (left.type === 'SEQ') {
2312
+ if (left.elements.some((el) => el.type !== 'ID')) {
2313
+ throw new ExpError('Left operand of assignment must be an identifier or a sequence of identifiers', token.loc);
2314
+ }
2315
+ }
2316
+ else if (left.type !== 'ID') {
2317
+ throw new ExpError('Left operand of assignment must be an identifier or a sequence of identifiers', token.loc);
2318
+ }
2319
+ }
2277
2320
  const right = this.parseExp((t) => isHigher(token, t) ? 'BACK' : undefined);
2278
2321
  if (!right) {
2279
2322
  throw new ExpError(`No right operand for "${token.type}"`, token.loc);
@@ -2835,7 +2878,7 @@ class RenderError extends CompileError {
2835
2878
  writable: true,
2836
2879
  value: getLoc
2837
2880
  });
2838
- this.name = 'CompileError';
2881
+ this.name = 'RenderError';
2839
2882
  RenderError.captureStackTrace?.(this, this.constructor);
2840
2883
  }
2841
2884
  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,22 @@ ${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, 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'
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(','), '};');
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
+ }
1360
1364
  await compiler.compileNodes(body);
1361
1365
  compiler.out();
1362
1366
  compiler.pushRaw(null, '};');
@@ -1450,7 +1454,36 @@ ${output
1450
1454
  else {
1451
1455
  throw parser.options.debug?.(new CompileError(`Unclosed "${token.name}"`, parser.template, token.loc));
1452
1456
  }
1453
- yield new MacroNode(parser.parseExp(token.expression), body, token.loc, token.strip);
1457
+ 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));
1477
+ return;
1478
+ }
1479
+ }
1480
+ }
1481
+ }
1482
+ else if (val.type !== 'ID') {
1483
+ parser.options.debug?.(new CompileError('Invalid macro definition', parser.template, token.loc));
1484
+ return;
1485
+ }
1486
+ yield new MacroNode(val, body, token.loc, token.strip);
1454
1487
  }
1455
1488
  async function* parseCaller(token, parser) {
1456
1489
  if (token.expression) {
@@ -2277,6 +2310,16 @@ ${output
2277
2310
  if (!left) {
2278
2311
  throw new ExpError(`No left operand for "${token.type}"`, token.loc);
2279
2312
  }
2313
+ if (token.type === 'ASSIGN') {
2314
+ if (left.type === 'SEQ') {
2315
+ if (left.elements.some((el) => el.type !== 'ID')) {
2316
+ throw new ExpError('Left operand of assignment must be an identifier or a sequence of identifiers', token.loc);
2317
+ }
2318
+ }
2319
+ else if (left.type !== 'ID') {
2320
+ throw new ExpError('Left operand of assignment must be an identifier or a sequence of identifiers', token.loc);
2321
+ }
2322
+ }
2280
2323
  const right = this.parseExp((t) => isHigher(token, t) ? 'BACK' : undefined);
2281
2324
  if (!right) {
2282
2325
  throw new ExpError(`No right operand for "${token.type}"`, token.loc);
@@ -2838,7 +2881,7 @@ ${output
2838
2881
  writable: true,
2839
2882
  value: getLoc
2840
2883
  });
2841
- this.name = 'CompileError';
2884
+ this.name = 'RenderError';
2842
2885
  RenderError.captureStackTrace?.(this, this.constructor);
2843
2886
  }
2844
2887
  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.2",
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",