@zessjs/compiler 1.1.4 → 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 +56 -44
- package/dist/compiler.js +56 -44
- package/package.json +1 -1
package/dist/compiler.cjs
CHANGED
|
@@ -450,7 +450,10 @@ const keywords = new Set([
|
|
|
450
450
|
]);
|
|
451
451
|
let currentContext;
|
|
452
452
|
function compile(code, options = {}) {
|
|
453
|
-
let
|
|
453
|
+
let JSXScope;
|
|
454
|
+
let prevJSXScope;
|
|
455
|
+
let JSXExpression;
|
|
456
|
+
let prevJSXExpression;
|
|
454
457
|
let prevFunctionScope;
|
|
455
458
|
let prevHasThisInFunctionScope;
|
|
456
459
|
const ast = (0, meriyah.parse)(code, {
|
|
@@ -467,16 +470,18 @@ function compile(code, options = {}) {
|
|
|
467
470
|
sourceRoot: options.sourceRoot ?? "",
|
|
468
471
|
modulePath: options.modulePath ?? "@zessjs/core"
|
|
469
472
|
},
|
|
470
|
-
ast,
|
|
471
473
|
events: [],
|
|
474
|
+
functionScope: ast,
|
|
472
475
|
eventsCache: /* @__PURE__ */ new Set(),
|
|
473
476
|
generatedFunctions: /* @__PURE__ */ new WeakSet()
|
|
474
477
|
};
|
|
475
478
|
visit(ast, {
|
|
476
479
|
JSXElement(node) {
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
+
if (!JSXExpression) {
|
|
481
|
+
currentContext.JSXId = 0;
|
|
482
|
+
currentContext.refId = 0;
|
|
483
|
+
currentContext.thisId = 0;
|
|
484
|
+
}
|
|
480
485
|
const stmts = transformElement(node);
|
|
481
486
|
let expression;
|
|
482
487
|
if (stmts.length === 1) if (stmts[0].type === "VariableDeclaration") expression = stmts[0].declarations[0].init;
|
|
@@ -486,15 +491,23 @@ function compile(code, options = {}) {
|
|
|
486
491
|
const callExpressionPosition = copyPosition(node);
|
|
487
492
|
expression = createCallExpression(createArrowFunctionExpression(createBlockStatement([...stmts, createReturnStatement(id, callExpressionPosition)], callExpressionPosition), callExpressionPosition), [], callExpressionPosition);
|
|
488
493
|
}
|
|
489
|
-
|
|
494
|
+
JSXExpression = expression;
|
|
495
|
+
prevJSXExpression = JSXExpression;
|
|
496
|
+
JSXScope = currentContext.functionScope;
|
|
497
|
+
prevJSXScope = JSXScope;
|
|
490
498
|
this.replace(expression);
|
|
491
499
|
},
|
|
492
500
|
JSXFragment(node) {
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
501
|
+
if (!JSXExpression) {
|
|
502
|
+
currentContext.JSXId = 0;
|
|
503
|
+
currentContext.refId = 0;
|
|
504
|
+
currentContext.thisId = 0;
|
|
505
|
+
}
|
|
496
506
|
const expression = transformFragment(node.children, copyPosition(node.openingFragment));
|
|
497
|
-
|
|
507
|
+
JSXExpression = expression;
|
|
508
|
+
prevJSXExpression = JSXExpression;
|
|
509
|
+
JSXScope = currentContext.functionScope;
|
|
510
|
+
prevJSXScope = JSXScope;
|
|
498
511
|
this.replace(expression);
|
|
499
512
|
},
|
|
500
513
|
FunctionDeclaration(node) {
|
|
@@ -521,17 +534,20 @@ function compile(code, options = {}) {
|
|
|
521
534
|
}
|
|
522
535
|
},
|
|
523
536
|
ThisExpression(node) {
|
|
524
|
-
if (
|
|
537
|
+
if (JSXScope === currentContext.functionScope) this.replace(transformThisExpression(node));
|
|
525
538
|
},
|
|
526
539
|
exit(node) {
|
|
527
540
|
if (node === currentContext.functionScope) {
|
|
528
|
-
currentContext.functionScope = prevFunctionScope;
|
|
541
|
+
currentContext.functionScope = prevFunctionScope ?? ast;
|
|
529
542
|
currentContext.hasThisInFunctionScope = prevHasThisInFunctionScope;
|
|
530
|
-
} else if (node ===
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
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
|
});
|
|
@@ -636,34 +652,30 @@ function transformFragment(children, position, isInComponent) {
|
|
|
636
652
|
}
|
|
637
653
|
function transformThisExpression(node) {
|
|
638
654
|
const thisId = createIdentifier(getUniqueId("self", "thisId"), copyPosition(node));
|
|
639
|
-
if (
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
else value.body = block = createBlockStatement([createReturnStatement(value.body, functionScopePosition)], functionScopePosition);
|
|
660
|
-
} else {
|
|
661
|
-
block = currentContext.functionScope.body;
|
|
662
|
-
functionScopePosition = copyPosition(currentContext.functionScope);
|
|
663
|
-
}
|
|
664
|
-
currentContext.hasThisInFunctionScope = true;
|
|
665
|
-
block.body.unshift(createVariableDeclaration(thisId, createThisExpression(functionScopePosition), functionScopePosition));
|
|
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;
|
|
666
675
|
}
|
|
676
|
+
thisExpression ??= createThisExpression(functionScopePosition);
|
|
677
|
+
currentContext.hasThisInFunctionScope = true;
|
|
678
|
+
block.body.splice(i, 0, createVariableDeclaration(thisId, thisExpression, functionScopePosition));
|
|
667
679
|
return thisId;
|
|
668
680
|
}
|
|
669
681
|
function injectRuntimeImport(ast) {
|
package/dist/compiler.js
CHANGED
|
@@ -422,7 +422,10 @@ const keywords = new Set([
|
|
|
422
422
|
]);
|
|
423
423
|
let currentContext;
|
|
424
424
|
function compile(code, options = {}) {
|
|
425
|
-
let
|
|
425
|
+
let JSXScope;
|
|
426
|
+
let prevJSXScope;
|
|
427
|
+
let JSXExpression;
|
|
428
|
+
let prevJSXExpression;
|
|
426
429
|
let prevFunctionScope;
|
|
427
430
|
let prevHasThisInFunctionScope;
|
|
428
431
|
const ast = parse(code, {
|
|
@@ -439,16 +442,18 @@ function compile(code, options = {}) {
|
|
|
439
442
|
sourceRoot: options.sourceRoot ?? "",
|
|
440
443
|
modulePath: options.modulePath ?? "@zessjs/core"
|
|
441
444
|
},
|
|
442
|
-
ast,
|
|
443
445
|
events: [],
|
|
446
|
+
functionScope: ast,
|
|
444
447
|
eventsCache: /* @__PURE__ */ new Set(),
|
|
445
448
|
generatedFunctions: /* @__PURE__ */ new WeakSet()
|
|
446
449
|
};
|
|
447
450
|
visit(ast, {
|
|
448
451
|
JSXElement(node) {
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
+
if (!JSXExpression) {
|
|
453
|
+
currentContext.JSXId = 0;
|
|
454
|
+
currentContext.refId = 0;
|
|
455
|
+
currentContext.thisId = 0;
|
|
456
|
+
}
|
|
452
457
|
const stmts = transformElement(node);
|
|
453
458
|
let expression;
|
|
454
459
|
if (stmts.length === 1) if (stmts[0].type === "VariableDeclaration") expression = stmts[0].declarations[0].init;
|
|
@@ -458,15 +463,23 @@ function compile(code, options = {}) {
|
|
|
458
463
|
const callExpressionPosition = copyPosition(node);
|
|
459
464
|
expression = createCallExpression(createArrowFunctionExpression(createBlockStatement([...stmts, createReturnStatement(id, callExpressionPosition)], callExpressionPosition), callExpressionPosition), [], callExpressionPosition);
|
|
460
465
|
}
|
|
461
|
-
|
|
466
|
+
JSXExpression = expression;
|
|
467
|
+
prevJSXExpression = JSXExpression;
|
|
468
|
+
JSXScope = currentContext.functionScope;
|
|
469
|
+
prevJSXScope = JSXScope;
|
|
462
470
|
this.replace(expression);
|
|
463
471
|
},
|
|
464
472
|
JSXFragment(node) {
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
473
|
+
if (!JSXExpression) {
|
|
474
|
+
currentContext.JSXId = 0;
|
|
475
|
+
currentContext.refId = 0;
|
|
476
|
+
currentContext.thisId = 0;
|
|
477
|
+
}
|
|
468
478
|
const expression = transformFragment(node.children, copyPosition(node.openingFragment));
|
|
469
|
-
|
|
479
|
+
JSXExpression = expression;
|
|
480
|
+
prevJSXExpression = JSXExpression;
|
|
481
|
+
JSXScope = currentContext.functionScope;
|
|
482
|
+
prevJSXScope = JSXScope;
|
|
470
483
|
this.replace(expression);
|
|
471
484
|
},
|
|
472
485
|
FunctionDeclaration(node) {
|
|
@@ -493,17 +506,20 @@ function compile(code, options = {}) {
|
|
|
493
506
|
}
|
|
494
507
|
},
|
|
495
508
|
ThisExpression(node) {
|
|
496
|
-
if (
|
|
509
|
+
if (JSXScope === currentContext.functionScope) this.replace(transformThisExpression(node));
|
|
497
510
|
},
|
|
498
511
|
exit(node) {
|
|
499
512
|
if (node === currentContext.functionScope) {
|
|
500
|
-
currentContext.functionScope = prevFunctionScope;
|
|
513
|
+
currentContext.functionScope = prevFunctionScope ?? ast;
|
|
501
514
|
currentContext.hasThisInFunctionScope = prevHasThisInFunctionScope;
|
|
502
|
-
} else if (node ===
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
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
|
});
|
|
@@ -608,34 +624,30 @@ function transformFragment(children, position, isInComponent) {
|
|
|
608
624
|
}
|
|
609
625
|
function transformThisExpression(node) {
|
|
610
626
|
const thisId = createIdentifier(getUniqueId("self", "thisId"), copyPosition(node));
|
|
611
|
-
if (
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
else value.body = block = createBlockStatement([createReturnStatement(value.body, functionScopePosition)], functionScopePosition);
|
|
632
|
-
} else {
|
|
633
|
-
block = currentContext.functionScope.body;
|
|
634
|
-
functionScopePosition = copyPosition(currentContext.functionScope);
|
|
635
|
-
}
|
|
636
|
-
currentContext.hasThisInFunctionScope = true;
|
|
637
|
-
block.body.unshift(createVariableDeclaration(thisId, createThisExpression(functionScopePosition), functionScopePosition));
|
|
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;
|
|
638
647
|
}
|
|
648
|
+
thisExpression ??= createThisExpression(functionScopePosition);
|
|
649
|
+
currentContext.hasThisInFunctionScope = true;
|
|
650
|
+
block.body.splice(i, 0, createVariableDeclaration(thisId, thisExpression, functionScopePosition));
|
|
639
651
|
return thisId;
|
|
640
652
|
}
|
|
641
653
|
function injectRuntimeImport(ast) {
|