@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 +74 -53
- package/dist/compiler.js +74 -53
- package/package.json +1 -1
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
|
|
454
|
-
let
|
|
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
|
-
|
|
477
|
-
|
|
478
|
-
|
|
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
|
-
|
|
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
|
-
|
|
493
|
-
|
|
494
|
-
|
|
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
|
-
|
|
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
|
-
|
|
501
|
-
|
|
502
|
-
currentContext.
|
|
503
|
-
currentContext.
|
|
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
|
-
|
|
509
|
-
|
|
510
|
-
currentContext.
|
|
511
|
-
currentContext.
|
|
522
|
+
prevFunctionScope = currentContext.functionScope;
|
|
523
|
+
prevHasThisInFunctionScope = currentContext.hasThisInFunctionScope;
|
|
524
|
+
currentContext.functionScope = node;
|
|
525
|
+
currentContext.hasThisInFunctionScope = false;
|
|
512
526
|
}
|
|
513
527
|
},
|
|
514
|
-
|
|
515
|
-
if (
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
currentContext.
|
|
519
|
-
currentContext.
|
|
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
|
-
|
|
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.
|
|
528
|
-
currentContext.
|
|
529
|
-
currentContext.
|
|
530
|
-
} else if (node ===
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
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 (
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
if (
|
|
656
|
-
|
|
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
|
|
426
|
-
let
|
|
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
|
-
|
|
449
|
-
|
|
450
|
-
|
|
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
|
-
|
|
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
|
-
|
|
465
|
-
|
|
466
|
-
|
|
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
|
-
|
|
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
|
-
|
|
473
|
-
|
|
474
|
-
currentContext.
|
|
475
|
-
currentContext.
|
|
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
|
-
|
|
481
|
-
|
|
482
|
-
currentContext.
|
|
483
|
-
currentContext.
|
|
494
|
+
prevFunctionScope = currentContext.functionScope;
|
|
495
|
+
prevHasThisInFunctionScope = currentContext.hasThisInFunctionScope;
|
|
496
|
+
currentContext.functionScope = node;
|
|
497
|
+
currentContext.hasThisInFunctionScope = false;
|
|
484
498
|
}
|
|
485
499
|
},
|
|
486
|
-
|
|
487
|
-
if (
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
currentContext.
|
|
491
|
-
currentContext.
|
|
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
|
-
|
|
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.
|
|
500
|
-
currentContext.
|
|
501
|
-
currentContext.
|
|
502
|
-
} else if (node ===
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
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 (
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
if (
|
|
628
|
-
|
|
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) {
|