@zessjs/compiler 1.1.3 → 1.1.4
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 +45 -36
- package/dist/compiler.js +45 -36
- package/package.json +1 -1
package/dist/compiler.cjs
CHANGED
|
@@ -450,8 +450,9 @@ const keywords = new Set([
|
|
|
450
450
|
]);
|
|
451
451
|
let currentContext;
|
|
452
452
|
function compile(code, options = {}) {
|
|
453
|
-
let
|
|
454
|
-
let
|
|
453
|
+
let JSXRootNode;
|
|
454
|
+
let prevFunctionScope;
|
|
455
|
+
let prevHasThisInFunctionScope;
|
|
455
456
|
const ast = (0, meriyah.parse)(code, {
|
|
456
457
|
jsx: true,
|
|
457
458
|
next: true,
|
|
@@ -485,7 +486,7 @@ function compile(code, options = {}) {
|
|
|
485
486
|
const callExpressionPosition = copyPosition(node);
|
|
486
487
|
expression = createCallExpression(createArrowFunctionExpression(createBlockStatement([...stmts, createReturnStatement(id, callExpressionPosition)], callExpressionPosition), callExpressionPosition), [], callExpressionPosition);
|
|
487
488
|
}
|
|
488
|
-
|
|
489
|
+
JSXRootNode ??= expression;
|
|
489
490
|
this.replace(expression);
|
|
490
491
|
},
|
|
491
492
|
JSXFragment(node) {
|
|
@@ -493,45 +494,44 @@ function compile(code, options = {}) {
|
|
|
493
494
|
currentContext.refId ??= 0;
|
|
494
495
|
currentContext.thisId ??= 0;
|
|
495
496
|
const expression = transformFragment(node.children, copyPosition(node.openingFragment));
|
|
496
|
-
|
|
497
|
+
JSXRootNode ??= expression;
|
|
497
498
|
this.replace(expression);
|
|
498
499
|
},
|
|
499
500
|
FunctionDeclaration(node) {
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
currentContext.
|
|
503
|
-
currentContext.
|
|
501
|
+
prevFunctionScope = currentContext.functionScope;
|
|
502
|
+
prevHasThisInFunctionScope = currentContext.hasThisInFunctionScope;
|
|
503
|
+
currentContext.functionScope = node;
|
|
504
|
+
currentContext.hasThisInFunctionScope = false;
|
|
504
505
|
},
|
|
505
506
|
FunctionExpression(node) {
|
|
506
507
|
if (currentContext.generatedFunctions.has(node)) currentContext.generatedFunctions.delete(node);
|
|
507
508
|
else {
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
currentContext.
|
|
511
|
-
currentContext.
|
|
509
|
+
prevFunctionScope = currentContext.functionScope;
|
|
510
|
+
prevHasThisInFunctionScope = currentContext.hasThisInFunctionScope;
|
|
511
|
+
currentContext.functionScope = node;
|
|
512
|
+
currentContext.hasThisInFunctionScope = false;
|
|
512
513
|
}
|
|
513
514
|
},
|
|
514
|
-
|
|
515
|
-
if (
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
currentContext.
|
|
519
|
-
currentContext.
|
|
515
|
+
PropertyDefinition(node) {
|
|
516
|
+
if (node.value) {
|
|
517
|
+
prevFunctionScope = currentContext.functionScope;
|
|
518
|
+
prevHasThisInFunctionScope = currentContext.hasThisInFunctionScope;
|
|
519
|
+
currentContext.functionScope = node;
|
|
520
|
+
currentContext.hasThisInFunctionScope = false;
|
|
520
521
|
}
|
|
521
522
|
},
|
|
522
523
|
ThisExpression(node) {
|
|
523
|
-
|
|
524
|
-
if (expression) this.replace(expression);
|
|
524
|
+
if (JSXRootNode) this.replace(transformThisExpression(node));
|
|
525
525
|
},
|
|
526
526
|
exit(node) {
|
|
527
|
-
if (node === currentContext.
|
|
528
|
-
currentContext.
|
|
529
|
-
currentContext.
|
|
530
|
-
} else if (node ===
|
|
527
|
+
if (node === currentContext.functionScope) {
|
|
528
|
+
currentContext.functionScope = prevFunctionScope;
|
|
529
|
+
currentContext.hasThisInFunctionScope = prevHasThisInFunctionScope;
|
|
530
|
+
} else if (node === JSXRootNode) {
|
|
531
531
|
currentContext.JSXId = void 0;
|
|
532
532
|
currentContext.refId = void 0;
|
|
533
533
|
currentContext.thisId = void 0;
|
|
534
|
-
|
|
534
|
+
JSXRootNode = void 0;
|
|
535
535
|
}
|
|
536
536
|
}
|
|
537
537
|
});
|
|
@@ -635,25 +635,34 @@ function transformFragment(children, position, isInComponent) {
|
|
|
635
635
|
if (!isInComponent) return createArrayExpression(elements, position);
|
|
636
636
|
}
|
|
637
637
|
function transformThisExpression(node) {
|
|
638
|
-
if (!currentContext.JSXParent) return;
|
|
639
638
|
const thisId = createIdentifier(getUniqueId("self", "thisId"), copyPosition(node));
|
|
640
|
-
if (!currentContext.
|
|
641
|
-
if (currentContext.
|
|
639
|
+
if (!currentContext.functionScope) {
|
|
640
|
+
if (currentContext.hasThisInGlobalScope) return thisId;
|
|
642
641
|
const { body } = currentContext.ast;
|
|
643
642
|
const programPosition = copyPosition(currentContext.ast);
|
|
644
|
-
currentContext.
|
|
643
|
+
currentContext.hasThisInGlobalScope = true;
|
|
645
644
|
for (let i = 0; i < body.length; ++i) {
|
|
646
645
|
if (body[i].type === "ImportDeclaration") continue;
|
|
647
646
|
body.splice(i, 0, createVariableDeclaration(thisId, createIdentifier("globalThis", programPosition), programPosition));
|
|
648
647
|
break;
|
|
649
648
|
}
|
|
650
|
-
} else if (!currentContext.
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
649
|
+
} else if (!currentContext.hasThisInFunctionScope) {
|
|
650
|
+
let block;
|
|
651
|
+
let functionScopePosition;
|
|
652
|
+
if (currentContext.functionScope.type === "PropertyDefinition") {
|
|
653
|
+
const { value } = currentContext.functionScope;
|
|
654
|
+
functionScopePosition = copyPosition(value);
|
|
655
|
+
if (value.type !== "ArrowFunctionExpression") {
|
|
656
|
+
block = createBlockStatement([createReturnStatement(value, functionScopePosition)], functionScopePosition);
|
|
657
|
+
currentContext.functionScope.value = createCallExpression(createArrowFunctionExpression(block, functionScopePosition), [], functionScopePosition);
|
|
658
|
+
} else if (value.body.type === "BlockStatement") block = value.body;
|
|
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));
|
|
657
666
|
}
|
|
658
667
|
return thisId;
|
|
659
668
|
}
|
package/dist/compiler.js
CHANGED
|
@@ -422,8 +422,9 @@ const keywords = new Set([
|
|
|
422
422
|
]);
|
|
423
423
|
let currentContext;
|
|
424
424
|
function compile(code, options = {}) {
|
|
425
|
-
let
|
|
426
|
-
let
|
|
425
|
+
let JSXRootNode;
|
|
426
|
+
let prevFunctionScope;
|
|
427
|
+
let prevHasThisInFunctionScope;
|
|
427
428
|
const ast = parse(code, {
|
|
428
429
|
jsx: true,
|
|
429
430
|
next: true,
|
|
@@ -457,7 +458,7 @@ function compile(code, options = {}) {
|
|
|
457
458
|
const callExpressionPosition = copyPosition(node);
|
|
458
459
|
expression = createCallExpression(createArrowFunctionExpression(createBlockStatement([...stmts, createReturnStatement(id, callExpressionPosition)], callExpressionPosition), callExpressionPosition), [], callExpressionPosition);
|
|
459
460
|
}
|
|
460
|
-
|
|
461
|
+
JSXRootNode ??= expression;
|
|
461
462
|
this.replace(expression);
|
|
462
463
|
},
|
|
463
464
|
JSXFragment(node) {
|
|
@@ -465,45 +466,44 @@ function compile(code, options = {}) {
|
|
|
465
466
|
currentContext.refId ??= 0;
|
|
466
467
|
currentContext.thisId ??= 0;
|
|
467
468
|
const expression = transformFragment(node.children, copyPosition(node.openingFragment));
|
|
468
|
-
|
|
469
|
+
JSXRootNode ??= expression;
|
|
469
470
|
this.replace(expression);
|
|
470
471
|
},
|
|
471
472
|
FunctionDeclaration(node) {
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
currentContext.
|
|
475
|
-
currentContext.
|
|
473
|
+
prevFunctionScope = currentContext.functionScope;
|
|
474
|
+
prevHasThisInFunctionScope = currentContext.hasThisInFunctionScope;
|
|
475
|
+
currentContext.functionScope = node;
|
|
476
|
+
currentContext.hasThisInFunctionScope = false;
|
|
476
477
|
},
|
|
477
478
|
FunctionExpression(node) {
|
|
478
479
|
if (currentContext.generatedFunctions.has(node)) currentContext.generatedFunctions.delete(node);
|
|
479
480
|
else {
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
currentContext.
|
|
483
|
-
currentContext.
|
|
481
|
+
prevFunctionScope = currentContext.functionScope;
|
|
482
|
+
prevHasThisInFunctionScope = currentContext.hasThisInFunctionScope;
|
|
483
|
+
currentContext.functionScope = node;
|
|
484
|
+
currentContext.hasThisInFunctionScope = false;
|
|
484
485
|
}
|
|
485
486
|
},
|
|
486
|
-
|
|
487
|
-
if (
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
currentContext.
|
|
491
|
-
currentContext.
|
|
487
|
+
PropertyDefinition(node) {
|
|
488
|
+
if (node.value) {
|
|
489
|
+
prevFunctionScope = currentContext.functionScope;
|
|
490
|
+
prevHasThisInFunctionScope = currentContext.hasThisInFunctionScope;
|
|
491
|
+
currentContext.functionScope = node;
|
|
492
|
+
currentContext.hasThisInFunctionScope = false;
|
|
492
493
|
}
|
|
493
494
|
},
|
|
494
495
|
ThisExpression(node) {
|
|
495
|
-
|
|
496
|
-
if (expression) this.replace(expression);
|
|
496
|
+
if (JSXRootNode) this.replace(transformThisExpression(node));
|
|
497
497
|
},
|
|
498
498
|
exit(node) {
|
|
499
|
-
if (node === currentContext.
|
|
500
|
-
currentContext.
|
|
501
|
-
currentContext.
|
|
502
|
-
} else if (node ===
|
|
499
|
+
if (node === currentContext.functionScope) {
|
|
500
|
+
currentContext.functionScope = prevFunctionScope;
|
|
501
|
+
currentContext.hasThisInFunctionScope = prevHasThisInFunctionScope;
|
|
502
|
+
} else if (node === JSXRootNode) {
|
|
503
503
|
currentContext.JSXId = void 0;
|
|
504
504
|
currentContext.refId = void 0;
|
|
505
505
|
currentContext.thisId = void 0;
|
|
506
|
-
|
|
506
|
+
JSXRootNode = void 0;
|
|
507
507
|
}
|
|
508
508
|
}
|
|
509
509
|
});
|
|
@@ -607,25 +607,34 @@ function transformFragment(children, position, isInComponent) {
|
|
|
607
607
|
if (!isInComponent) return createArrayExpression(elements, position);
|
|
608
608
|
}
|
|
609
609
|
function transformThisExpression(node) {
|
|
610
|
-
if (!currentContext.JSXParent) return;
|
|
611
610
|
const thisId = createIdentifier(getUniqueId("self", "thisId"), copyPosition(node));
|
|
612
|
-
if (!currentContext.
|
|
613
|
-
if (currentContext.
|
|
611
|
+
if (!currentContext.functionScope) {
|
|
612
|
+
if (currentContext.hasThisInGlobalScope) return thisId;
|
|
614
613
|
const { body } = currentContext.ast;
|
|
615
614
|
const programPosition = copyPosition(currentContext.ast);
|
|
616
|
-
currentContext.
|
|
615
|
+
currentContext.hasThisInGlobalScope = true;
|
|
617
616
|
for (let i = 0; i < body.length; ++i) {
|
|
618
617
|
if (body[i].type === "ImportDeclaration") continue;
|
|
619
618
|
body.splice(i, 0, createVariableDeclaration(thisId, createIdentifier("globalThis", programPosition), programPosition));
|
|
620
619
|
break;
|
|
621
620
|
}
|
|
622
|
-
} else if (!currentContext.
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
621
|
+
} else if (!currentContext.hasThisInFunctionScope) {
|
|
622
|
+
let block;
|
|
623
|
+
let functionScopePosition;
|
|
624
|
+
if (currentContext.functionScope.type === "PropertyDefinition") {
|
|
625
|
+
const { value } = currentContext.functionScope;
|
|
626
|
+
functionScopePosition = copyPosition(value);
|
|
627
|
+
if (value.type !== "ArrowFunctionExpression") {
|
|
628
|
+
block = createBlockStatement([createReturnStatement(value, functionScopePosition)], functionScopePosition);
|
|
629
|
+
currentContext.functionScope.value = createCallExpression(createArrowFunctionExpression(block, functionScopePosition), [], functionScopePosition);
|
|
630
|
+
} else if (value.body.type === "BlockStatement") block = value.body;
|
|
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));
|
|
629
638
|
}
|
|
630
639
|
return thisId;
|
|
631
640
|
}
|