@reckona/mreact-compiler 0.0.153 → 0.0.155

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/src/oxc.ts CHANGED
@@ -70,6 +70,12 @@ import {
70
70
  analyzeOxcJsxNode,
71
71
  type OxcChildAnalysisContext,
72
72
  } from "./oxc-child-analysis.js";
73
+ import {
74
+ analyzeCompatCreateElementRoot,
75
+ collectCompatCreateElementNames,
76
+ collectFunctionShadowedNames,
77
+ hasLowerableCompatCreateElementReturn,
78
+ } from "./oxc-compat-create-element.js";
73
79
  import { lowerOxcDomNodeExpression } from "./oxc-dom-lowering.js";
74
80
  import {
75
81
  lowerOxcCompatObjectExpression,
@@ -239,6 +245,12 @@ function analyzeOxcToIr(
239
245
  : undefined;
240
246
  const localJsxReturnFunctionNames =
241
247
  target === "server" ? collectOxcLocalJsxReturnFunctionNames(program) : new Set<string>();
248
+ // Stream emit keeps interpreting compat trees for now; only the string
249
+ // pipeline lowers createElement calls.
250
+ const compatCreateElementNames =
251
+ target === "server" && options?.serverOutput !== "stream"
252
+ ? collectCompatCreateElementNames(program)
253
+ : new Set<string>();
242
254
  const localJsxHelperHtmlParameters =
243
255
  target === "server"
244
256
  ? collectLocalJsxHelperHtmlParameters(program, localJsxReturnFunctionNames)
@@ -265,6 +277,7 @@ function analyzeOxcToIr(
265
277
 
266
278
  if (
267
279
  isOxcJsxComponentStatement(statement, localJsxReturnFunctionNames) ||
280
+ isCompatCreateElementComponentStatement(code, statement, compatCreateElementNames) ||
268
281
  (options?.compatReactNodeReturn === true && isOxcExportedFunctionLike(statement))
269
282
  ) {
270
283
  const declaration = readObject(readObject(statement).declaration);
@@ -328,6 +341,7 @@ function analyzeOxcToIr(
328
341
  target,
329
342
  diagnostics,
330
343
  options?.bodyStatementJsx ?? "dom-node",
344
+ compatCreateElementNames,
331
345
  moduleRenderValueBindings,
332
346
  options?.compatReactNodeReturn === true,
333
347
  options?.serverOutput,
@@ -476,6 +490,97 @@ function collectOxcReturnExpressions(
476
490
  return [];
477
491
  }
478
492
 
493
+ interface CompatCreateElementComponent {
494
+ name: string;
495
+ initializer: Record<string, unknown>;
496
+ }
497
+
498
+ function readCompatCreateElementFunctionLike(
499
+ code: string,
500
+ expression: Record<string, unknown>,
501
+ names: ReadonlySet<string>,
502
+ ): Record<string, unknown> | undefined {
503
+ const functionLike = unwrapOxcComponentFunctionLikeInitializer(expression);
504
+
505
+ return functionLike !== undefined &&
506
+ hasLowerableCompatCreateElementReturn(code, functionLike, names)
507
+ ? functionLike
508
+ : undefined;
509
+ }
510
+
511
+ function readCompatCreateElementPlainComponent(
512
+ code: string,
513
+ statement: unknown,
514
+ names: ReadonlySet<string>,
515
+ ): CompatCreateElementComponent | undefined {
516
+ if (names.size === 0) {
517
+ return undefined;
518
+ }
519
+
520
+ const object = readObject(statement);
521
+
522
+ if (
523
+ object.type === "FunctionDeclaration" &&
524
+ hasLowerableCompatCreateElementReturn(code, object, names)
525
+ ) {
526
+ const id = readObject(object.id);
527
+ return typeof id.name === "string" ? { name: id.name, initializer: object } : undefined;
528
+ }
529
+
530
+ if (object.type !== "VariableDeclaration") {
531
+ return undefined;
532
+ }
533
+
534
+ for (const declarator of readArray(object.declarations)) {
535
+ const declaratorObject = readObject(declarator);
536
+ const id = readObject(declaratorObject.id);
537
+
538
+ if (typeof id.name !== "string" || !/^[A-Z]/.test(id.name)) {
539
+ continue;
540
+ }
541
+
542
+ const initializer = readCompatCreateElementFunctionLike(
543
+ code,
544
+ readObject(declaratorObject.init),
545
+ names,
546
+ );
547
+
548
+ if (initializer !== undefined) {
549
+ return { name: id.name, initializer };
550
+ }
551
+ }
552
+
553
+ return undefined;
554
+ }
555
+
556
+ function isCompatCreateElementComponentStatement(
557
+ code: string,
558
+ statement: unknown,
559
+ names: ReadonlySet<string>,
560
+ ): boolean {
561
+ if (names.size === 0) {
562
+ return false;
563
+ }
564
+
565
+ const object = readObject(statement);
566
+
567
+ if (object.type === "ExportDefaultDeclaration") {
568
+ return readCompatCreateElementFunctionLike(code, readObject(object.declaration), names) !== undefined;
569
+ }
570
+
571
+ if (object.type === "ExportNamedDeclaration") {
572
+ const declaration = readObject(object.declaration);
573
+
574
+ if (declaration.type === "FunctionDeclaration") {
575
+ return hasLowerableCompatCreateElementReturn(code, declaration, names);
576
+ }
577
+
578
+ return readCompatCreateElementPlainComponent(code, declaration, names) !== undefined;
579
+ }
580
+
581
+ return readCompatCreateElementPlainComponent(code, statement, names) !== undefined;
582
+ }
583
+
479
584
  function analyzeOxcComponent(
480
585
  code: string,
481
586
  statement: unknown,
@@ -483,6 +588,7 @@ function analyzeOxcComponent(
483
588
  target: CompileTarget,
484
589
  diagnostics: Diagnostic[],
485
590
  bodyStatementJsx: OxcBodyStatementJsxMode,
591
+ compatCreateElementNames: ReadonlySet<string>,
486
592
  moduleRenderValueBindings: Set<string>,
487
593
  compatReactNodeReturn: boolean,
488
594
  serverOutput: AnalyzeModuleOptions["serverOutput"],
@@ -497,7 +603,11 @@ function analyzeOxcComponent(
497
603
  if (object.type === "ExportDefaultDeclaration") {
498
604
  const declaration = unwrapOxcComponentFunctionLikeInitializer(readObject(object.declaration));
499
605
 
500
- if (declaration === undefined || !hasOxcFunctionLikeComponentReturn(declaration)) {
606
+ if (
607
+ declaration === undefined ||
608
+ (!hasOxcFunctionLikeComponentReturn(declaration) &&
609
+ !hasLowerableCompatCreateElementReturn(code, declaration, compatCreateElementNames))
610
+ ) {
501
611
  return [];
502
612
  }
503
613
  const id = readObject(declaration.id);
@@ -513,6 +623,7 @@ function analyzeOxcComponent(
513
623
  target,
514
624
  diagnostics,
515
625
  bodyStatementJsx,
626
+ compatCreateElementNames,
516
627
  moduleRenderValueBindings,
517
628
  compatReactNodeReturn,
518
629
  serverOutput,
@@ -527,7 +638,9 @@ function analyzeOxcComponent(
527
638
  }
528
639
 
529
640
  if (object.type !== "ExportNamedDeclaration") {
530
- const plainComponent = readOxcPlainComponent(statement);
641
+ const plainComponent =
642
+ readOxcPlainComponent(statement) ??
643
+ readCompatCreateElementPlainComponent(code, statement, compatCreateElementNames);
531
644
 
532
645
  if (plainComponent === undefined) {
533
646
  return [];
@@ -544,6 +657,7 @@ function analyzeOxcComponent(
544
657
  target,
545
658
  diagnostics,
546
659
  bodyStatementJsx,
660
+ compatCreateElementNames,
547
661
  moduleRenderValueBindings,
548
662
  compatReactNodeReturn,
549
663
  serverOutput,
@@ -561,7 +675,9 @@ function analyzeOxcComponent(
561
675
  const declaration = readObject(object.declaration);
562
676
 
563
677
  if (declaration.type === "VariableDeclaration") {
564
- const variableComponent = readOxcVariableComponentDeclaration(declaration);
678
+ const variableComponent =
679
+ readOxcVariableComponentDeclaration(declaration) ??
680
+ readCompatCreateElementPlainComponent(code, declaration, compatCreateElementNames);
565
681
 
566
682
  if (variableComponent === undefined) {
567
683
  return [];
@@ -577,6 +693,7 @@ function analyzeOxcComponent(
577
693
  target,
578
694
  diagnostics,
579
695
  bodyStatementJsx,
696
+ compatCreateElementNames,
580
697
  moduleRenderValueBindings,
581
698
  compatReactNodeReturn,
582
699
  serverOutput,
@@ -593,7 +710,8 @@ function analyzeOxcComponent(
593
710
  declaration.type !== "FunctionDeclaration" ||
594
711
  (!compatReactNodeReturn &&
595
712
  !hasComponentReturn(declaration.body) &&
596
- !hasLocalJsxHelperCallReturn(declaration.body, localJsxReturnFunctionNames))
713
+ !hasLocalJsxHelperCallReturn(declaration.body, localJsxReturnFunctionNames) &&
714
+ !hasLowerableCompatCreateElementReturn(code, declaration, compatCreateElementNames))
597
715
  ) {
598
716
  return [];
599
717
  }
@@ -614,6 +732,7 @@ function analyzeOxcComponent(
614
732
  target,
615
733
  diagnostics,
616
734
  bodyStatementJsx,
735
+ compatCreateElementNames,
617
736
  moduleRenderValueBindings,
618
737
  compatReactNodeReturn,
619
738
  serverOutput,
@@ -663,6 +782,7 @@ function analyzeOxcFunctionLikeComponent(
663
782
  target: CompileTarget,
664
783
  diagnostics: Diagnostic[],
665
784
  bodyStatementJsx: OxcBodyStatementJsxMode,
785
+ compatCreateElementNames: ReadonlySet<string>,
666
786
  moduleRenderValueBindings: Set<string>,
667
787
  compatReactNodeReturn: boolean,
668
788
  serverOutput: AnalyzeModuleOptions["serverOutput"],
@@ -743,6 +863,12 @@ function analyzeOxcFunctionLikeComponent(
743
863
  const root =
744
864
  analyzeOxcEarlyIfRootReturn(code, earlyIfRootReturn, childAnalysisContext, bodyStatementJsx) ??
745
865
  analyzeOxcSwitchRootReturn(code, rootStatement, childAnalysisContext, bodyStatementJsx) ??
866
+ (compatCreateElementNames.size === 0
867
+ ? undefined
868
+ : analyzeCompatCreateElementRoot(code, returnExpression, {
869
+ names: compatCreateElementNames,
870
+ shadowed: collectFunctionShadowedNames(functionLike, compatCreateElementNames),
871
+ })) ??
746
872
  (isJsxRoot(returnExpression.type) || returnExpression.type === "JSXFragment"
747
873
  ? analyzeOxcJsxNode(code, returnExpression, childAnalysisContext)
748
874
  : isOxcComponentCallExpression(returnExpression)