@tsrx/react 0.0.6 → 0.0.7

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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/src/transform.js +111 -0
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "description": "React compiler built on @tsrx/core",
4
4
  "license": "MIT",
5
5
  "author": "Dominic Gannaway",
6
- "version": "0.0.6",
6
+ "version": "0.0.7",
7
7
  "type": "module",
8
8
  "publishConfig": {
9
9
  "access": "public"
package/src/transform.js CHANGED
@@ -881,6 +881,116 @@ function references_scope_bindings(node, scope_bindings) {
881
881
  return false;
882
882
  }
883
883
 
884
+ /**
885
+ * @param {AST.Literal} node
886
+ * @returns {boolean}
887
+ */
888
+ function is_static_literal(node) {
889
+ return (
890
+ node.value === null ||
891
+ typeof node.value === 'string' ||
892
+ typeof node.value === 'number' ||
893
+ typeof node.value === 'boolean' ||
894
+ typeof node.value === 'bigint'
895
+ );
896
+ }
897
+
898
+ /**
899
+ * @param {any} node
900
+ * @returns {boolean}
901
+ */
902
+ function is_hoist_safe_expression(node) {
903
+ if (!node || typeof node !== 'object') return false;
904
+
905
+ switch (node.type) {
906
+ case 'Literal':
907
+ return is_static_literal(node);
908
+ case 'TemplateLiteral':
909
+ return node.expressions.length === 0;
910
+ case 'UnaryExpression':
911
+ return node.operator !== 'delete' && is_hoist_safe_expression(node.argument);
912
+ case 'BinaryExpression':
913
+ case 'LogicalExpression':
914
+ return is_hoist_safe_expression(node.left) && is_hoist_safe_expression(node.right);
915
+ case 'ConditionalExpression':
916
+ return (
917
+ is_hoist_safe_expression(node.test) &&
918
+ is_hoist_safe_expression(node.consequent) &&
919
+ is_hoist_safe_expression(node.alternate)
920
+ );
921
+ case 'SequenceExpression':
922
+ return node.expressions.every(is_hoist_safe_expression);
923
+ case 'ParenthesizedExpression':
924
+ return is_hoist_safe_expression(node.expression);
925
+ case 'JSXElement':
926
+ return is_hoist_safe_jsx_node(node);
927
+ case 'JSXFragment':
928
+ return node.children.every(is_hoist_safe_jsx_child);
929
+ default:
930
+ return false;
931
+ }
932
+ }
933
+
934
+ /**
935
+ * @param {any} node
936
+ * @returns {boolean}
937
+ */
938
+ function is_hoist_safe_jsx_child(node) {
939
+ if (!node || typeof node !== 'object') return false;
940
+
941
+ switch (node.type) {
942
+ case 'JSXText':
943
+ return true;
944
+ case 'JSXElement':
945
+ return is_hoist_safe_jsx_node(node);
946
+ case 'JSXFragment':
947
+ return node.children.every(is_hoist_safe_jsx_child);
948
+ case 'JSXExpressionContainer':
949
+ return (
950
+ node.expression.type !== 'JSXEmptyExpression' && is_hoist_safe_expression(node.expression)
951
+ );
952
+ default:
953
+ return false;
954
+ }
955
+ }
956
+
957
+ /**
958
+ * @param {ESTreeJSX.JSXAttribute | ESTreeJSX.JSXSpreadAttribute} attribute
959
+ * @returns {boolean}
960
+ */
961
+ function is_hoist_safe_jsx_attribute(attribute) {
962
+ if (attribute.type === 'JSXSpreadAttribute') return false;
963
+ if (attribute.value == null) return true;
964
+
965
+ if (attribute.value.type === 'Literal') {
966
+ return is_static_literal(attribute.value);
967
+ }
968
+
969
+ if (attribute.value.type === 'JSXExpressionContainer') {
970
+ return (
971
+ attribute.value.expression.type !== 'JSXEmptyExpression' &&
972
+ is_hoist_safe_expression(attribute.value.expression)
973
+ );
974
+ }
975
+
976
+ return false;
977
+ }
978
+
979
+ /**
980
+ * @param {ESTreeJSX.JSXElement | ESTreeJSX.JSXFragment} node
981
+ * @returns {boolean}
982
+ */
983
+ function is_hoist_safe_jsx_node(node) {
984
+ if (node.type === 'JSXFragment') {
985
+ return node.children.every(is_hoist_safe_jsx_child);
986
+ }
987
+
988
+ return (
989
+ node.openingElement.attributes.every(is_hoist_safe_jsx_attribute) &&
990
+ node.children.every(is_hoist_safe_jsx_child)
991
+ );
992
+ }
993
+
884
994
  /**
885
995
  * Hoist static JSX elements from render_nodes to module level.
886
996
  * A JSX element is static if it doesn't reference any component-scope bindings.
@@ -896,6 +1006,7 @@ function hoist_static_render_nodes(render_nodes, transform_context) {
896
1006
  for (let i = 0; i < render_nodes.length; i++) {
897
1007
  const node = render_nodes[i];
898
1008
  if (node.type !== 'JSXElement') continue;
1009
+ if (!is_hoist_safe_jsx_node(node)) continue;
899
1010
  if (references_scope_bindings(node, transform_context.available_bindings)) continue;
900
1011
 
901
1012
  const name = create_helper_name(transform_context.helper_state, 'static');