@zessjs/compiler 1.1.4 → 1.1.6
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 +66 -44
- package/dist/compiler.js +66 -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,26 @@ function compile(code, options = {}) {
|
|
|
521
534
|
}
|
|
522
535
|
},
|
|
523
536
|
ThisExpression(node) {
|
|
524
|
-
if (
|
|
537
|
+
if (JSXScope === currentContext.functionScope) this.replace(transformThisExpression(node));
|
|
538
|
+
},
|
|
539
|
+
ImportDeclaration() {
|
|
540
|
+
this.skip();
|
|
541
|
+
},
|
|
542
|
+
ExportAllDeclaration() {
|
|
543
|
+
this.skip();
|
|
525
544
|
},
|
|
526
545
|
exit(node) {
|
|
527
546
|
if (node === currentContext.functionScope) {
|
|
528
|
-
currentContext.functionScope = prevFunctionScope;
|
|
547
|
+
currentContext.functionScope = prevFunctionScope ?? ast;
|
|
529
548
|
currentContext.hasThisInFunctionScope = prevHasThisInFunctionScope;
|
|
530
|
-
} else if (node ===
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
549
|
+
} else if (node === JSXExpression) {
|
|
550
|
+
JSXExpression = prevJSXExpression;
|
|
551
|
+
JSXScope = prevJSXScope;
|
|
552
|
+
if (!JSXExpression) {
|
|
553
|
+
currentContext.JSXId = void 0;
|
|
554
|
+
currentContext.refId = void 0;
|
|
555
|
+
currentContext.thisId = void 0;
|
|
556
|
+
}
|
|
535
557
|
}
|
|
536
558
|
}
|
|
537
559
|
});
|
|
@@ -636,34 +658,30 @@ function transformFragment(children, position, isInComponent) {
|
|
|
636
658
|
}
|
|
637
659
|
function transformThisExpression(node) {
|
|
638
660
|
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));
|
|
661
|
+
if (currentContext.hasThisInFunctionScope) return thisId;
|
|
662
|
+
let i = 0;
|
|
663
|
+
let functionScopePosition;
|
|
664
|
+
let block;
|
|
665
|
+
let thisExpression;
|
|
666
|
+
if (currentContext.functionScope.type === "PropertyDefinition") {
|
|
667
|
+
const { value } = currentContext.functionScope;
|
|
668
|
+
functionScopePosition = copyPosition(value);
|
|
669
|
+
if (value.type !== "ArrowFunctionExpression") {
|
|
670
|
+
block = createBlockStatement([createReturnStatement(value, functionScopePosition)], functionScopePosition);
|
|
671
|
+
currentContext.functionScope.value = createCallExpression(createArrowFunctionExpression(block, functionScopePosition), [], functionScopePosition);
|
|
672
|
+
} else if (value.body.type === "BlockStatement") block = value.body;
|
|
673
|
+
else value.body = block = createBlockStatement([createReturnStatement(value.body, functionScopePosition)], functionScopePosition);
|
|
674
|
+
} else {
|
|
675
|
+
functionScopePosition = copyPosition(currentContext.functionScope);
|
|
676
|
+
if (currentContext.functionScope.type === "Program") {
|
|
677
|
+
block = currentContext.functionScope;
|
|
678
|
+
thisExpression = createIdentifier("globalThis", functionScopePosition);
|
|
679
|
+
while (i < block.body.length && block.body[i].type === "ImportDeclaration") i++;
|
|
680
|
+
} else block = currentContext.functionScope.body;
|
|
666
681
|
}
|
|
682
|
+
thisExpression ??= createThisExpression(functionScopePosition);
|
|
683
|
+
currentContext.hasThisInFunctionScope = true;
|
|
684
|
+
block.body.splice(i, 0, createVariableDeclaration(thisId, thisExpression, functionScopePosition));
|
|
667
685
|
return thisId;
|
|
668
686
|
}
|
|
669
687
|
function injectRuntimeImport(ast) {
|
|
@@ -1365,6 +1383,10 @@ function isDynamicExpression(node, checkTags) {
|
|
|
1365
1383
|
isDynamic = true;
|
|
1366
1384
|
this.break();
|
|
1367
1385
|
},
|
|
1386
|
+
TaggedTemplateExpression() {
|
|
1387
|
+
isDynamic = true;
|
|
1388
|
+
this.break();
|
|
1389
|
+
},
|
|
1368
1390
|
JSXElement() {
|
|
1369
1391
|
if (checkTags) {
|
|
1370
1392
|
isDynamic = true;
|
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,26 @@ function compile(code, options = {}) {
|
|
|
493
506
|
}
|
|
494
507
|
},
|
|
495
508
|
ThisExpression(node) {
|
|
496
|
-
if (
|
|
509
|
+
if (JSXScope === currentContext.functionScope) this.replace(transformThisExpression(node));
|
|
510
|
+
},
|
|
511
|
+
ImportDeclaration() {
|
|
512
|
+
this.skip();
|
|
513
|
+
},
|
|
514
|
+
ExportAllDeclaration() {
|
|
515
|
+
this.skip();
|
|
497
516
|
},
|
|
498
517
|
exit(node) {
|
|
499
518
|
if (node === currentContext.functionScope) {
|
|
500
|
-
currentContext.functionScope = prevFunctionScope;
|
|
519
|
+
currentContext.functionScope = prevFunctionScope ?? ast;
|
|
501
520
|
currentContext.hasThisInFunctionScope = prevHasThisInFunctionScope;
|
|
502
|
-
} else if (node ===
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
521
|
+
} else if (node === JSXExpression) {
|
|
522
|
+
JSXExpression = prevJSXExpression;
|
|
523
|
+
JSXScope = prevJSXScope;
|
|
524
|
+
if (!JSXExpression) {
|
|
525
|
+
currentContext.JSXId = void 0;
|
|
526
|
+
currentContext.refId = void 0;
|
|
527
|
+
currentContext.thisId = void 0;
|
|
528
|
+
}
|
|
507
529
|
}
|
|
508
530
|
}
|
|
509
531
|
});
|
|
@@ -608,34 +630,30 @@ function transformFragment(children, position, isInComponent) {
|
|
|
608
630
|
}
|
|
609
631
|
function transformThisExpression(node) {
|
|
610
632
|
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));
|
|
633
|
+
if (currentContext.hasThisInFunctionScope) return thisId;
|
|
634
|
+
let i = 0;
|
|
635
|
+
let functionScopePosition;
|
|
636
|
+
let block;
|
|
637
|
+
let thisExpression;
|
|
638
|
+
if (currentContext.functionScope.type === "PropertyDefinition") {
|
|
639
|
+
const { value } = currentContext.functionScope;
|
|
640
|
+
functionScopePosition = copyPosition(value);
|
|
641
|
+
if (value.type !== "ArrowFunctionExpression") {
|
|
642
|
+
block = createBlockStatement([createReturnStatement(value, functionScopePosition)], functionScopePosition);
|
|
643
|
+
currentContext.functionScope.value = createCallExpression(createArrowFunctionExpression(block, functionScopePosition), [], functionScopePosition);
|
|
644
|
+
} else if (value.body.type === "BlockStatement") block = value.body;
|
|
645
|
+
else value.body = block = createBlockStatement([createReturnStatement(value.body, functionScopePosition)], functionScopePosition);
|
|
646
|
+
} else {
|
|
647
|
+
functionScopePosition = copyPosition(currentContext.functionScope);
|
|
648
|
+
if (currentContext.functionScope.type === "Program") {
|
|
649
|
+
block = currentContext.functionScope;
|
|
650
|
+
thisExpression = createIdentifier("globalThis", functionScopePosition);
|
|
651
|
+
while (i < block.body.length && block.body[i].type === "ImportDeclaration") i++;
|
|
652
|
+
} else block = currentContext.functionScope.body;
|
|
638
653
|
}
|
|
654
|
+
thisExpression ??= createThisExpression(functionScopePosition);
|
|
655
|
+
currentContext.hasThisInFunctionScope = true;
|
|
656
|
+
block.body.splice(i, 0, createVariableDeclaration(thisId, thisExpression, functionScopePosition));
|
|
639
657
|
return thisId;
|
|
640
658
|
}
|
|
641
659
|
function injectRuntimeImport(ast) {
|
|
@@ -1337,6 +1355,10 @@ function isDynamicExpression(node, checkTags) {
|
|
|
1337
1355
|
isDynamic = true;
|
|
1338
1356
|
this.break();
|
|
1339
1357
|
},
|
|
1358
|
+
TaggedTemplateExpression() {
|
|
1359
|
+
isDynamic = true;
|
|
1360
|
+
this.break();
|
|
1361
|
+
},
|
|
1340
1362
|
JSXElement() {
|
|
1341
1363
|
if (checkTags) {
|
|
1342
1364
|
isDynamic = true;
|