@zessjs/compiler 1.1.3 → 1.1.5

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/compiler.cjs CHANGED
@@ -450,8 +450,12 @@ const keywords = new Set([
450
450
  ]);
451
451
  let currentContext;
452
452
  function compile(code, options = {}) {
453
- let prevFunctionParent;
454
- let prevHasThisInFunction;
453
+ let JSXScope;
454
+ let prevJSXScope;
455
+ let JSXExpression;
456
+ let prevJSXExpression;
457
+ let prevFunctionScope;
458
+ let prevHasThisInFunctionScope;
455
459
  const ast = (0, meriyah.parse)(code, {
456
460
  jsx: true,
457
461
  next: true,
@@ -466,16 +470,18 @@ function compile(code, options = {}) {
466
470
  sourceRoot: options.sourceRoot ?? "",
467
471
  modulePath: options.modulePath ?? "@zessjs/core"
468
472
  },
469
- ast,
470
473
  events: [],
474
+ functionScope: ast,
471
475
  eventsCache: /* @__PURE__ */ new Set(),
472
476
  generatedFunctions: /* @__PURE__ */ new WeakSet()
473
477
  };
474
478
  visit(ast, {
475
479
  JSXElement(node) {
476
- currentContext.JSXId ??= 0;
477
- currentContext.refId ??= 0;
478
- currentContext.thisId ??= 0;
480
+ if (!JSXExpression) {
481
+ currentContext.JSXId = 0;
482
+ currentContext.refId = 0;
483
+ currentContext.thisId = 0;
484
+ }
479
485
  const stmts = transformElement(node);
480
486
  let expression;
481
487
  if (stmts.length === 1) if (stmts[0].type === "VariableDeclaration") expression = stmts[0].declarations[0].init;
@@ -485,53 +491,63 @@ function compile(code, options = {}) {
485
491
  const callExpressionPosition = copyPosition(node);
486
492
  expression = createCallExpression(createArrowFunctionExpression(createBlockStatement([...stmts, createReturnStatement(id, callExpressionPosition)], callExpressionPosition), callExpressionPosition), [], callExpressionPosition);
487
493
  }
488
- currentContext.JSXParent ??= expression;
494
+ JSXExpression = expression;
495
+ prevJSXExpression = JSXExpression;
496
+ JSXScope = currentContext.functionScope;
497
+ prevJSXScope = JSXScope;
489
498
  this.replace(expression);
490
499
  },
491
500
  JSXFragment(node) {
492
- currentContext.JSXId ??= 0;
493
- currentContext.refId ??= 0;
494
- currentContext.thisId ??= 0;
501
+ if (!JSXExpression) {
502
+ currentContext.JSXId = 0;
503
+ currentContext.refId = 0;
504
+ currentContext.thisId = 0;
505
+ }
495
506
  const expression = transformFragment(node.children, copyPosition(node.openingFragment));
496
- currentContext.JSXParent ??= expression;
507
+ JSXExpression = expression;
508
+ prevJSXExpression = JSXExpression;
509
+ JSXScope = currentContext.functionScope;
510
+ prevJSXScope = JSXScope;
497
511
  this.replace(expression);
498
512
  },
499
513
  FunctionDeclaration(node) {
500
- prevFunctionParent = currentContext.functionParent;
501
- prevHasThisInFunction = currentContext.hasThisInFunction;
502
- currentContext.functionParent = node;
503
- currentContext.hasThisInFunction = false;
514
+ prevFunctionScope = currentContext.functionScope;
515
+ prevHasThisInFunctionScope = currentContext.hasThisInFunctionScope;
516
+ currentContext.functionScope = node;
517
+ currentContext.hasThisInFunctionScope = false;
504
518
  },
505
519
  FunctionExpression(node) {
506
520
  if (currentContext.generatedFunctions.has(node)) currentContext.generatedFunctions.delete(node);
507
521
  else {
508
- prevFunctionParent = currentContext.functionParent;
509
- prevHasThisInFunction = currentContext.hasThisInFunction;
510
- currentContext.functionParent = node;
511
- currentContext.hasThisInFunction = false;
522
+ prevFunctionScope = currentContext.functionScope;
523
+ prevHasThisInFunctionScope = currentContext.hasThisInFunctionScope;
524
+ currentContext.functionScope = node;
525
+ currentContext.hasThisInFunctionScope = false;
512
526
  }
513
527
  },
514
- ArrowFunctionExpression(node, parent, key) {
515
- if (parent?.type === "PropertyDefinition" && key === "value") {
516
- prevFunctionParent = currentContext.functionParent;
517
- prevHasThisInFunction = currentContext.hasThisInFunction;
518
- currentContext.functionParent = node;
519
- currentContext.hasThisInFunction = false;
528
+ PropertyDefinition(node) {
529
+ if (node.value) {
530
+ prevFunctionScope = currentContext.functionScope;
531
+ prevHasThisInFunctionScope = currentContext.hasThisInFunctionScope;
532
+ currentContext.functionScope = node;
533
+ currentContext.hasThisInFunctionScope = false;
520
534
  }
521
535
  },
522
536
  ThisExpression(node) {
523
- const expression = transformThisExpression(node);
524
- if (expression) this.replace(expression);
537
+ if (JSXScope === currentContext.functionScope) this.replace(transformThisExpression(node));
525
538
  },
526
539
  exit(node) {
527
- if (node === currentContext.functionParent) {
528
- currentContext.functionParent = prevFunctionParent;
529
- currentContext.hasThisInFunction = prevHasThisInFunction;
530
- } else if (node === currentContext.JSXParent) {
531
- currentContext.JSXId = void 0;
532
- currentContext.refId = void 0;
533
- currentContext.thisId = void 0;
534
- currentContext.JSXParent = void 0;
540
+ if (node === currentContext.functionScope) {
541
+ currentContext.functionScope = prevFunctionScope ?? ast;
542
+ currentContext.hasThisInFunctionScope = prevHasThisInFunctionScope;
543
+ } else if (node === JSXExpression) {
544
+ JSXExpression = prevJSXExpression;
545
+ JSXScope = prevJSXScope;
546
+ if (!JSXExpression) {
547
+ currentContext.JSXId = void 0;
548
+ currentContext.refId = void 0;
549
+ currentContext.thisId = void 0;
550
+ }
535
551
  }
536
552
  }
537
553
  });
@@ -635,26 +651,31 @@ function transformFragment(children, position, isInComponent) {
635
651
  if (!isInComponent) return createArrayExpression(elements, position);
636
652
  }
637
653
  function transformThisExpression(node) {
638
- if (!currentContext.JSXParent) return;
639
654
  const thisId = createIdentifier(getUniqueId("self", "thisId"), copyPosition(node));
640
- if (!currentContext.functionParent) {
641
- if (currentContext.hasThisInProgram) return thisId;
642
- const { body } = currentContext.ast;
643
- const programPosition = copyPosition(currentContext.ast);
644
- currentContext.hasThisInProgram = true;
645
- for (let i = 0; i < body.length; ++i) {
646
- if (body[i].type === "ImportDeclaration") continue;
647
- body.splice(i, 0, createVariableDeclaration(thisId, createIdentifier("globalThis", programPosition), programPosition));
648
- break;
649
- }
650
- } else if (!currentContext.hasThisInFunction) {
651
- const functionPosition = copyPosition(currentContext.functionParent);
652
- const block = currentContext.functionParent.body;
653
- const declaration = createVariableDeclaration(thisId, createThisExpression(functionPosition), functionPosition);
654
- currentContext.hasThisInFunction = true;
655
- if (block.type === "BlockStatement") block.body.unshift(declaration);
656
- else currentContext.functionParent.body = createBlockStatement([declaration, createReturnStatement(block, copyPosition(block))], functionPosition);
655
+ if (currentContext.hasThisInFunctionScope) return thisId;
656
+ let i = 0;
657
+ let functionScopePosition;
658
+ let block;
659
+ let thisExpression;
660
+ if (currentContext.functionScope.type === "PropertyDefinition") {
661
+ const { value } = currentContext.functionScope;
662
+ functionScopePosition = copyPosition(value);
663
+ if (value.type !== "ArrowFunctionExpression") {
664
+ block = createBlockStatement([createReturnStatement(value, functionScopePosition)], functionScopePosition);
665
+ currentContext.functionScope.value = createCallExpression(createArrowFunctionExpression(block, functionScopePosition), [], functionScopePosition);
666
+ } else if (value.body.type === "BlockStatement") block = value.body;
667
+ else value.body = block = createBlockStatement([createReturnStatement(value.body, functionScopePosition)], functionScopePosition);
668
+ } else {
669
+ functionScopePosition = copyPosition(currentContext.functionScope);
670
+ if (currentContext.functionScope.type === "Program") {
671
+ block = currentContext.functionScope;
672
+ thisExpression = createIdentifier("globalThis", functionScopePosition);
673
+ while (i < block.body.length && block.body[i].type === "ImportDeclaration") i++;
674
+ } else block = currentContext.functionScope.body;
657
675
  }
676
+ thisExpression ??= createThisExpression(functionScopePosition);
677
+ currentContext.hasThisInFunctionScope = true;
678
+ block.body.splice(i, 0, createVariableDeclaration(thisId, thisExpression, functionScopePosition));
658
679
  return thisId;
659
680
  }
660
681
  function injectRuntimeImport(ast) {
package/dist/compiler.js CHANGED
@@ -422,8 +422,12 @@ const keywords = new Set([
422
422
  ]);
423
423
  let currentContext;
424
424
  function compile(code, options = {}) {
425
- let prevFunctionParent;
426
- let prevHasThisInFunction;
425
+ let JSXScope;
426
+ let prevJSXScope;
427
+ let JSXExpression;
428
+ let prevJSXExpression;
429
+ let prevFunctionScope;
430
+ let prevHasThisInFunctionScope;
427
431
  const ast = parse(code, {
428
432
  jsx: true,
429
433
  next: true,
@@ -438,16 +442,18 @@ function compile(code, options = {}) {
438
442
  sourceRoot: options.sourceRoot ?? "",
439
443
  modulePath: options.modulePath ?? "@zessjs/core"
440
444
  },
441
- ast,
442
445
  events: [],
446
+ functionScope: ast,
443
447
  eventsCache: /* @__PURE__ */ new Set(),
444
448
  generatedFunctions: /* @__PURE__ */ new WeakSet()
445
449
  };
446
450
  visit(ast, {
447
451
  JSXElement(node) {
448
- currentContext.JSXId ??= 0;
449
- currentContext.refId ??= 0;
450
- currentContext.thisId ??= 0;
452
+ if (!JSXExpression) {
453
+ currentContext.JSXId = 0;
454
+ currentContext.refId = 0;
455
+ currentContext.thisId = 0;
456
+ }
451
457
  const stmts = transformElement(node);
452
458
  let expression;
453
459
  if (stmts.length === 1) if (stmts[0].type === "VariableDeclaration") expression = stmts[0].declarations[0].init;
@@ -457,53 +463,63 @@ function compile(code, options = {}) {
457
463
  const callExpressionPosition = copyPosition(node);
458
464
  expression = createCallExpression(createArrowFunctionExpression(createBlockStatement([...stmts, createReturnStatement(id, callExpressionPosition)], callExpressionPosition), callExpressionPosition), [], callExpressionPosition);
459
465
  }
460
- currentContext.JSXParent ??= expression;
466
+ JSXExpression = expression;
467
+ prevJSXExpression = JSXExpression;
468
+ JSXScope = currentContext.functionScope;
469
+ prevJSXScope = JSXScope;
461
470
  this.replace(expression);
462
471
  },
463
472
  JSXFragment(node) {
464
- currentContext.JSXId ??= 0;
465
- currentContext.refId ??= 0;
466
- currentContext.thisId ??= 0;
473
+ if (!JSXExpression) {
474
+ currentContext.JSXId = 0;
475
+ currentContext.refId = 0;
476
+ currentContext.thisId = 0;
477
+ }
467
478
  const expression = transformFragment(node.children, copyPosition(node.openingFragment));
468
- currentContext.JSXParent ??= expression;
479
+ JSXExpression = expression;
480
+ prevJSXExpression = JSXExpression;
481
+ JSXScope = currentContext.functionScope;
482
+ prevJSXScope = JSXScope;
469
483
  this.replace(expression);
470
484
  },
471
485
  FunctionDeclaration(node) {
472
- prevFunctionParent = currentContext.functionParent;
473
- prevHasThisInFunction = currentContext.hasThisInFunction;
474
- currentContext.functionParent = node;
475
- currentContext.hasThisInFunction = false;
486
+ prevFunctionScope = currentContext.functionScope;
487
+ prevHasThisInFunctionScope = currentContext.hasThisInFunctionScope;
488
+ currentContext.functionScope = node;
489
+ currentContext.hasThisInFunctionScope = false;
476
490
  },
477
491
  FunctionExpression(node) {
478
492
  if (currentContext.generatedFunctions.has(node)) currentContext.generatedFunctions.delete(node);
479
493
  else {
480
- prevFunctionParent = currentContext.functionParent;
481
- prevHasThisInFunction = currentContext.hasThisInFunction;
482
- currentContext.functionParent = node;
483
- currentContext.hasThisInFunction = false;
494
+ prevFunctionScope = currentContext.functionScope;
495
+ prevHasThisInFunctionScope = currentContext.hasThisInFunctionScope;
496
+ currentContext.functionScope = node;
497
+ currentContext.hasThisInFunctionScope = false;
484
498
  }
485
499
  },
486
- ArrowFunctionExpression(node, parent, key) {
487
- if (parent?.type === "PropertyDefinition" && key === "value") {
488
- prevFunctionParent = currentContext.functionParent;
489
- prevHasThisInFunction = currentContext.hasThisInFunction;
490
- currentContext.functionParent = node;
491
- currentContext.hasThisInFunction = false;
500
+ PropertyDefinition(node) {
501
+ if (node.value) {
502
+ prevFunctionScope = currentContext.functionScope;
503
+ prevHasThisInFunctionScope = currentContext.hasThisInFunctionScope;
504
+ currentContext.functionScope = node;
505
+ currentContext.hasThisInFunctionScope = false;
492
506
  }
493
507
  },
494
508
  ThisExpression(node) {
495
- const expression = transformThisExpression(node);
496
- if (expression) this.replace(expression);
509
+ if (JSXScope === currentContext.functionScope) this.replace(transformThisExpression(node));
497
510
  },
498
511
  exit(node) {
499
- if (node === currentContext.functionParent) {
500
- currentContext.functionParent = prevFunctionParent;
501
- currentContext.hasThisInFunction = prevHasThisInFunction;
502
- } else if (node === currentContext.JSXParent) {
503
- currentContext.JSXId = void 0;
504
- currentContext.refId = void 0;
505
- currentContext.thisId = void 0;
506
- currentContext.JSXParent = void 0;
512
+ if (node === currentContext.functionScope) {
513
+ currentContext.functionScope = prevFunctionScope ?? ast;
514
+ currentContext.hasThisInFunctionScope = prevHasThisInFunctionScope;
515
+ } else if (node === JSXExpression) {
516
+ JSXExpression = prevJSXExpression;
517
+ JSXScope = prevJSXScope;
518
+ if (!JSXExpression) {
519
+ currentContext.JSXId = void 0;
520
+ currentContext.refId = void 0;
521
+ currentContext.thisId = void 0;
522
+ }
507
523
  }
508
524
  }
509
525
  });
@@ -607,26 +623,31 @@ function transformFragment(children, position, isInComponent) {
607
623
  if (!isInComponent) return createArrayExpression(elements, position);
608
624
  }
609
625
  function transformThisExpression(node) {
610
- if (!currentContext.JSXParent) return;
611
626
  const thisId = createIdentifier(getUniqueId("self", "thisId"), copyPosition(node));
612
- if (!currentContext.functionParent) {
613
- if (currentContext.hasThisInProgram) return thisId;
614
- const { body } = currentContext.ast;
615
- const programPosition = copyPosition(currentContext.ast);
616
- currentContext.hasThisInProgram = true;
617
- for (let i = 0; i < body.length; ++i) {
618
- if (body[i].type === "ImportDeclaration") continue;
619
- body.splice(i, 0, createVariableDeclaration(thisId, createIdentifier("globalThis", programPosition), programPosition));
620
- break;
621
- }
622
- } else if (!currentContext.hasThisInFunction) {
623
- const functionPosition = copyPosition(currentContext.functionParent);
624
- const block = currentContext.functionParent.body;
625
- const declaration = createVariableDeclaration(thisId, createThisExpression(functionPosition), functionPosition);
626
- currentContext.hasThisInFunction = true;
627
- if (block.type === "BlockStatement") block.body.unshift(declaration);
628
- else currentContext.functionParent.body = createBlockStatement([declaration, createReturnStatement(block, copyPosition(block))], functionPosition);
627
+ if (currentContext.hasThisInFunctionScope) return thisId;
628
+ let i = 0;
629
+ let functionScopePosition;
630
+ let block;
631
+ let thisExpression;
632
+ if (currentContext.functionScope.type === "PropertyDefinition") {
633
+ const { value } = currentContext.functionScope;
634
+ functionScopePosition = copyPosition(value);
635
+ if (value.type !== "ArrowFunctionExpression") {
636
+ block = createBlockStatement([createReturnStatement(value, functionScopePosition)], functionScopePosition);
637
+ currentContext.functionScope.value = createCallExpression(createArrowFunctionExpression(block, functionScopePosition), [], functionScopePosition);
638
+ } else if (value.body.type === "BlockStatement") block = value.body;
639
+ else value.body = block = createBlockStatement([createReturnStatement(value.body, functionScopePosition)], functionScopePosition);
640
+ } else {
641
+ functionScopePosition = copyPosition(currentContext.functionScope);
642
+ if (currentContext.functionScope.type === "Program") {
643
+ block = currentContext.functionScope;
644
+ thisExpression = createIdentifier("globalThis", functionScopePosition);
645
+ while (i < block.body.length && block.body[i].type === "ImportDeclaration") i++;
646
+ } else block = currentContext.functionScope.body;
629
647
  }
648
+ thisExpression ??= createThisExpression(functionScopePosition);
649
+ currentContext.hasThisInFunctionScope = true;
650
+ block.body.splice(i, 0, createVariableDeclaration(thisId, thisExpression, functionScopePosition));
630
651
  return thisId;
631
652
  }
632
653
  function injectRuntimeImport(ast) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zessjs/compiler",
3
- "version": "1.1.3",
3
+ "version": "1.1.5",
4
4
  "description": "Zess JSX compiler 💥 Delivers efficient code conversion for super - responsive web experiences.",
5
5
  "type": "module",
6
6
  "keywords": [