@vxrn/compiler 2.0.0-beta.33.1 → 2.0.0-beta.45.1

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 (94) hide show
  1. package/dist/cjs/cache.cjs +112 -109
  2. package/dist/cjs/configure.cjs +26 -28
  3. package/dist/cjs/constants.cjs +56 -60
  4. package/dist/cjs/index.cjs +420 -438
  5. package/dist/cjs/reactNativeCodegen.cjs +105 -114
  6. package/dist/cjs/reactNativeViewConfig.cjs +362 -381
  7. package/dist/cjs/refresh-runtime.cjs +390 -397
  8. package/dist/cjs/transformBabel.cjs +278 -218
  9. package/dist/cjs/transformHermesAsync.cjs +36 -47
  10. package/dist/cjs/transformHermesLoops.cjs +279 -281
  11. package/dist/cjs/transformSWC.cjs +183 -200
  12. package/dist/cjs/transformWorklets.cjs +107 -95
  13. package/dist/cjs/types.cjs +9 -11
  14. package/dist/cjs/worklets/autoworklet.cjs +277 -222
  15. package/dist/cjs/worklets/globals.cjs +124 -60
  16. package/dist/cjs/worklets/hash.cjs +23 -27
  17. package/dist/cjs/worklets/scope.cjs +338 -359
  18. package/dist/cjs/worklets/serialize.cjs +73 -75
  19. package/dist/cjs/worklets/transform.cjs +217 -222
  20. package/dist/cjs/worklets/types.cjs +9 -11
  21. package/dist/esm/cache.mjs +96 -89
  22. package/dist/esm/cache.mjs.map +1 -1
  23. package/dist/esm/configure.mjs +11 -10
  24. package/dist/esm/configure.mjs.map +1 -1
  25. package/dist/esm/constants.mjs +41 -41
  26. package/dist/esm/constants.mjs.map +1 -1
  27. package/dist/esm/index.js +417 -420
  28. package/dist/esm/index.js.map +1 -1
  29. package/dist/esm/index.mjs +417 -420
  30. package/dist/esm/index.mjs.map +1 -1
  31. package/dist/esm/reactNativeCodegen.mjs +90 -88
  32. package/dist/esm/reactNativeCodegen.mjs.map +1 -1
  33. package/dist/esm/reactNativeViewConfig.mjs +347 -362
  34. package/dist/esm/reactNativeViewConfig.mjs.map +1 -1
  35. package/dist/esm/refresh-runtime.mjs +372 -376
  36. package/dist/esm/refresh-runtime.mjs.map +1 -1
  37. package/dist/esm/transformBabel.mjs +261 -194
  38. package/dist/esm/transformBabel.mjs.map +1 -1
  39. package/dist/esm/transformHermesAsync.mjs +20 -21
  40. package/dist/esm/transformHermesAsync.mjs.map +1 -1
  41. package/dist/esm/transformHermesLoops.mjs +264 -255
  42. package/dist/esm/transformHermesLoops.mjs.map +1 -1
  43. package/dist/esm/transformSWC.mjs +163 -171
  44. package/dist/esm/transformSWC.mjs.map +1 -1
  45. package/dist/esm/transformWorklets.mjs +89 -69
  46. package/dist/esm/transformWorklets.mjs.map +1 -1
  47. package/dist/esm/types.mjs +0 -2
  48. package/dist/esm/worklets/autoworklet.mjs +258 -200
  49. package/dist/esm/worklets/autoworklet.mjs.map +1 -1
  50. package/dist/esm/worklets/globals.mjs +109 -42
  51. package/dist/esm/worklets/globals.mjs.map +1 -1
  52. package/dist/esm/worklets/hash.mjs +10 -9
  53. package/dist/esm/worklets/hash.mjs.map +1 -1
  54. package/dist/esm/worklets/scope.mjs +325 -341
  55. package/dist/esm/worklets/scope.mjs.map +1 -1
  56. package/dist/esm/worklets/serialize.mjs +60 -58
  57. package/dist/esm/worklets/serialize.mjs.map +1 -1
  58. package/dist/esm/worklets/transform.mjs +201 -197
  59. package/dist/esm/worklets/transform.mjs.map +1 -1
  60. package/dist/esm/worklets/types.mjs +0 -2
  61. package/package.json +7 -6
  62. package/src/cache.ts +37 -7
  63. package/src/index.ts +33 -18
  64. package/src/transformBabel.test.ts +311 -1
  65. package/src/transformBabel.ts +110 -13
  66. package/types/cache.d.ts +2 -2
  67. package/types/cache.d.ts.map +1 -1
  68. package/types/index.d.ts.map +1 -1
  69. package/types/transformBabel.d.ts +16 -1
  70. package/types/transformBabel.d.ts.map +1 -1
  71. package/types/transformSWC.d.ts +1 -4
  72. package/types/transformSWC.d.ts.map +1 -1
  73. package/dist/cjs/transformBabel.test.cjs +0 -360
  74. package/dist/cjs/transformHermesLoops.test.cjs +0 -257
  75. package/dist/cjs/transformSWC.test.cjs +0 -117
  76. package/dist/cjs/transformWorklets.test.cjs +0 -604
  77. package/dist/esm/transformBabel.test.mjs +0 -337
  78. package/dist/esm/transformBabel.test.mjs.map +0 -1
  79. package/dist/esm/transformHermesLoops.test.mjs +0 -234
  80. package/dist/esm/transformHermesLoops.test.mjs.map +0 -1
  81. package/dist/esm/transformSWC.test.mjs +0 -118
  82. package/dist/esm/transformSWC.test.mjs.map +0 -1
  83. package/dist/esm/transformWorklets.test.mjs +0 -581
  84. package/dist/esm/transformWorklets.test.mjs.map +0 -1
  85. package/dist/esm/types.mjs.map +0 -1
  86. package/dist/esm/worklets/types.mjs.map +0 -1
  87. package/types/transformBabel.test.d.ts +0 -2
  88. package/types/transformBabel.test.d.ts.map +0 -1
  89. package/types/transformHermesLoops.test.d.ts +0 -2
  90. package/types/transformHermesLoops.test.d.ts.map +0 -1
  91. package/types/transformSWC.test.d.ts +0 -2
  92. package/types/transformSWC.test.d.ts.map +0 -1
  93. package/types/transformWorklets.test.d.ts +0 -2
  94. package/types/transformWorklets.test.d.ts.map +0 -1
@@ -1,6 +1,7 @@
1
1
  import { createRequire } from "node:module";
2
2
  import { basename, join } from "node:path";
3
3
  import { parseSync } from "oxc-parser";
4
+
4
5
  const SELF_URL = typeof __filename !== "undefined" ? __filename : import.meta.url;
5
6
  const COLOR_ATTRIBUTE = "require('react-native/Libraries/Components/View/ReactNativeStyleAttributes').colorAttribute";
6
7
  const COLOR_ARRAY_ATTRIBUTE = "{\n process: (req => 'default' in req ? req.default : req)(require('react-native/Libraries/StyleSheet/processColorArray'))\n }";
@@ -9,349 +10,334 @@ const POINT_ATTRIBUTE = "{\n diff: (req => 'default' in req ? req.default :
9
10
  const EDGE_INSETS_ATTRIBUTE = "{\n diff: (req => 'default' in req ? req.default : req)(require('react-native/Libraries/Utilities/differ/insetsDiffer'))\n }";
10
11
  const IMPORT_REGISTRY = "const NativeComponentRegistry = require('react-native/Libraries/NativeComponent/NativeComponentRegistry');";
11
12
  const IMPORT_IGNORE = "const {ConditionallyIgnoredEventHandlers} = require('react-native/Libraries/NativeComponent/ViewConfigIgnore');";
12
- const IMPORT_DISPATCH = 'const {dispatchCommand} = require("react-native/Libraries/ReactNative/RendererProxy");';
13
- const IMPORT_UIMANAGER = 'const {UIManager} = require("react-native")';
13
+ const IMPORT_DISPATCH = "const {dispatchCommand} = require(\"react-native/Libraries/ReactNative/RendererProxy\");";
14
+ const IMPORT_UIMANAGER = "const {UIManager} = require(\"react-native\")";
14
15
  let hermesParser = void 0;
15
16
  function getHermesParser(projectRoot) {
16
- if (hermesParser === void 0) {
17
- try {
18
- const req = projectRoot ? createRequire(join(projectRoot, "package.json")) : createRequire(SELF_URL);
19
- hermesParser = req("hermes-parser");
20
- } catch {
21
- try {
22
- hermesParser = createRequire(SELF_URL)("hermes-parser");
23
- } catch {
24
- hermesParser = null;
25
- }
26
- }
27
- }
28
- return hermesParser;
17
+ if (hermesParser === void 0) {
18
+ try {
19
+ const req = projectRoot ? createRequire(join(projectRoot, "package.json")) : createRequire(SELF_URL);
20
+ hermesParser = req("hermes-parser");
21
+ } catch {
22
+ try {
23
+ hermesParser = createRequire(SELF_URL)("hermes-parser");
24
+ } catch {
25
+ hermesParser = null;
26
+ }
27
+ }
28
+ }
29
+ return hermesParser;
29
30
  }
30
31
  function readType(node) {
31
- if (!node) return {
32
- name: "",
33
- args: []
34
- };
35
- switch (node.type) {
36
- // flow
37
- case "NullableTypeAnnotation":
38
- case "TypeAnnotation":
39
- return readType(node.typeAnnotation);
40
- case "GenericTypeAnnotation":
41
- return {
42
- name: readTypeName(node.id),
43
- args: (node.typeParameters?.params ?? []).map(readType)
44
- };
45
- case "ObjectTypeAnnotation":
46
- return {
47
- name: "__object",
48
- args: []
49
- };
50
- case "StringTypeAnnotation":
51
- return {
52
- name: "string",
53
- args: []
54
- };
55
- case "BooleanTypeAnnotation":
56
- return {
57
- name: "boolean",
58
- args: []
59
- };
60
- case "NumberTypeAnnotation":
61
- return {
62
- name: "number",
63
- args: []
64
- };
65
- case "UnionTypeAnnotation":
66
- return readUnion(node);
67
- case "FunctionTypeAnnotation":
68
- return {
69
- name: "__function",
70
- args: []
71
- };
72
- // typescript
73
- case "TSTypeAnnotation":
74
- return readType(node.typeAnnotation);
75
- case "TSTypeReference":
76
- return {
77
- name: readTypeName(node.typeName),
78
- args: (node.typeArguments ?? node.typeParameters)?.params?.map(readType) ?? []
79
- };
80
- case "TSTypeLiteral":
81
- return {
82
- name: "__object",
83
- args: []
84
- };
85
- case "TSStringKeyword":
86
- return {
87
- name: "string",
88
- args: []
89
- };
90
- case "TSBooleanKeyword":
91
- return {
92
- name: "boolean",
93
- args: []
94
- };
95
- case "TSNumberKeyword":
96
- return {
97
- name: "number",
98
- args: []
99
- };
100
- case "TSUnionType":
101
- return readUnion(node);
102
- case "TSFunctionType":
103
- return {
104
- name: "__function",
105
- args: []
106
- };
107
- case "TSArrayType":
108
- return {
109
- name: "Array",
110
- args: [readType(node.elementType)]
111
- };
112
- }
113
- return {
114
- name: "__unknown",
115
- args: []
116
- };
32
+ if (!node) return {
33
+ name: "",
34
+ args: []
35
+ };
36
+ switch (node.type) {
37
+ case "NullableTypeAnnotation":
38
+ case "TypeAnnotation": return readType(node.typeAnnotation);
39
+ case "GenericTypeAnnotation": return {
40
+ name: readTypeName(node.id),
41
+ args: (node.typeParameters?.params ?? []).map(readType)
42
+ };
43
+ case "ObjectTypeAnnotation": return {
44
+ name: "__object",
45
+ args: []
46
+ };
47
+ case "StringTypeAnnotation": return {
48
+ name: "string",
49
+ args: []
50
+ };
51
+ case "BooleanTypeAnnotation": return {
52
+ name: "boolean",
53
+ args: []
54
+ };
55
+ case "NumberTypeAnnotation": return {
56
+ name: "number",
57
+ args: []
58
+ };
59
+ case "UnionTypeAnnotation": return readUnion(node);
60
+ case "FunctionTypeAnnotation": return {
61
+ name: "__function",
62
+ args: []
63
+ };
64
+ case "TSTypeAnnotation": return readType(node.typeAnnotation);
65
+ case "TSTypeReference": return {
66
+ name: readTypeName(node.typeName),
67
+ args: (node.typeArguments ?? node.typeParameters)?.params?.map(readType) ?? []
68
+ };
69
+ case "TSTypeLiteral": return {
70
+ name: "__object",
71
+ args: []
72
+ };
73
+ case "TSStringKeyword": return {
74
+ name: "string",
75
+ args: []
76
+ };
77
+ case "TSBooleanKeyword": return {
78
+ name: "boolean",
79
+ args: []
80
+ };
81
+ case "TSNumberKeyword": return {
82
+ name: "number",
83
+ args: []
84
+ };
85
+ case "TSUnionType": return readUnion(node);
86
+ case "TSFunctionType": return {
87
+ name: "__function",
88
+ args: []
89
+ };
90
+ case "TSArrayType": return {
91
+ name: "Array",
92
+ args: [readType(node.elementType)]
93
+ };
94
+ }
95
+ return {
96
+ name: "__unknown",
97
+ args: []
98
+ };
117
99
  }
118
- const NULLISH_TYPES = /* @__PURE__ */new Set(["TSUndefinedKeyword", "TSNullKeyword", "TSVoidKeyword", "NullLiteralTypeAnnotation", "VoidTypeAnnotation"]);
100
+ const NULLISH_TYPES = /* @__PURE__ */ new Set([
101
+ "TSUndefinedKeyword",
102
+ "TSNullKeyword",
103
+ "TSVoidKeyword",
104
+ "NullLiteralTypeAnnotation",
105
+ "VoidTypeAnnotation"
106
+ ]);
119
107
  function readUnion(node) {
120
- const real = (node.types ?? []).filter(t => !NULLISH_TYPES.has(t.type));
121
- if (real.length === 1) return readType(real[0]);
122
- return {
123
- name: "__union",
124
- args: []
125
- };
108
+ const real = (node.types ?? []).filter((t) => !NULLISH_TYPES.has(t.type));
109
+ if (real.length === 1) return readType(real[0]);
110
+ return {
111
+ name: "__union",
112
+ args: []
113
+ };
126
114
  }
127
115
  function readTypeName(id) {
128
- if (!id) return "";
129
- if (id.type === "Identifier") return id.name;
130
- if (id.type === "QualifiedTypeIdentifier") return readTypeName(id.id);
131
- if (id.type === "TSQualifiedName") return readTypeName(id.right);
132
- return "";
116
+ if (!id) return "";
117
+ if (id.type === "Identifier") return id.name;
118
+ if (id.type === "QualifiedTypeIdentifier") return readTypeName(id.id);
119
+ if (id.type === "TSQualifiedName") return readTypeName(id.right);
120
+ return "";
133
121
  }
134
- const READONLY_ARRAYS = /* @__PURE__ */new Set(["ReadonlyArray", "$ReadOnlyArray", "Array"]);
135
- const TRANSPARENT = /* @__PURE__ */new Set(["WithDefault", "UnsafeMixed", "Readonly", "$ReadOnly"]);
122
+ const READONLY_ARRAYS = /* @__PURE__ */ new Set([
123
+ "ReadonlyArray",
124
+ "$ReadOnlyArray",
125
+ "Array"
126
+ ]);
127
+ const TRANSPARENT = /* @__PURE__ */ new Set([
128
+ "WithDefault",
129
+ "UnsafeMixed",
130
+ "Readonly",
131
+ "$ReadOnly"
132
+ ]);
136
133
  function unwrap(type) {
137
- let cur = type;
138
- while (TRANSPARENT.has(cur.name) && cur.args.length > 0) {
139
- cur = cur.args[0];
140
- }
141
- return cur;
134
+ let cur = type;
135
+ while (TRANSPARENT.has(cur.name) && cur.args.length > 0) {
136
+ cur = cur.args[0];
137
+ }
138
+ return cur;
142
139
  }
143
140
  function attributeValue(type) {
144
- const t = unwrap(type);
145
- switch (t.name) {
146
- case "ColorValue":
147
- case "ProcessedColorValue":
148
- return COLOR_ATTRIBUTE;
149
- case "ImageSource":
150
- return IMAGE_ATTRIBUTE;
151
- case "PointValue":
152
- return POINT_ATTRIBUTE;
153
- case "EdgeInsetsValue":
154
- return EDGE_INSETS_ATTRIBUTE;
155
- }
156
- if (READONLY_ARRAYS.has(t.name) && t.args.length === 1) {
157
- const el = unwrap(t.args[0]);
158
- if (el.name === "ColorValue" || el.name === "ProcessedColorValue") {
159
- return COLOR_ARRAY_ATTRIBUTE;
160
- }
161
- }
162
- return "true";
141
+ const t = unwrap(type);
142
+ switch (t.name) {
143
+ case "ColorValue":
144
+ case "ProcessedColorValue": return COLOR_ATTRIBUTE;
145
+ case "ImageSource": return IMAGE_ATTRIBUTE;
146
+ case "PointValue": return POINT_ATTRIBUTE;
147
+ case "EdgeInsetsValue": return EDGE_INSETS_ATTRIBUTE;
148
+ }
149
+ if (READONLY_ARRAYS.has(t.name) && t.args.length === 1) {
150
+ const el = unwrap(t.args[0]);
151
+ if (el.name === "ColorValue" || el.name === "ProcessedColorValue") {
152
+ return COLOR_ARRAY_ATTRIBUTE;
153
+ }
154
+ }
155
+ return "true";
163
156
  }
164
157
  function eventOf(name, type) {
165
- const t = unwrap(type);
166
- if (t.name !== "DirectEventHandler" && t.name !== "BubblingEventHandler") return null;
167
- const override = t.args[1]?.name;
168
- return {
169
- name,
170
- bubbling: t.name === "BubblingEventHandler",
171
- topName: normalizeInputEventName(override && override !== "__unknown" ? override : name)
172
- };
158
+ const t = unwrap(type);
159
+ if (t.name !== "DirectEventHandler" && t.name !== "BubblingEventHandler") return null;
160
+ const override = t.args[1]?.name;
161
+ return {
162
+ name,
163
+ bubbling: t.name === "BubblingEventHandler",
164
+ topName: normalizeInputEventName(override && override !== "__unknown" ? override : name)
165
+ };
173
166
  }
174
167
  function normalizeInputEventName(name) {
175
- if (name.startsWith("on")) return name.replace(/^on/, "top");
176
- if (!name.startsWith("top")) return `top${name[0].toUpperCase()}${name.slice(1)}`;
177
- return name;
168
+ if (name.startsWith("on")) return name.replace(/^on/, "top");
169
+ if (!name.startsWith("top")) return `top${name[0].toUpperCase()}${name.slice(1)}`;
170
+ return name;
178
171
  }
179
172
  function collectDeclarations(body) {
180
- const out = /* @__PURE__ */new Map();
181
- for (const raw of body) {
182
- const node = raw?.type === "ExportNamedDeclaration" ? raw.declaration : raw;
183
- if (!node) continue;
184
- switch (node.type) {
185
- case "TSTypeAliasDeclaration":
186
- case "TypeAlias":
187
- case "TSInterfaceDeclaration":
188
- case "InterfaceDeclaration":
189
- if (node.id?.name) out.set(node.id.name, node);
190
- break;
191
- }
192
- }
193
- return out;
173
+ const out = /* @__PURE__ */ new Map();
174
+ for (const raw of body) {
175
+ const node = raw?.type === "ExportNamedDeclaration" ? raw.declaration : raw;
176
+ if (!node) continue;
177
+ switch (node.type) {
178
+ case "TSTypeAliasDeclaration":
179
+ case "TypeAlias":
180
+ case "TSInterfaceDeclaration":
181
+ case "InterfaceDeclaration":
182
+ if (node.id?.name) out.set(node.id.name, node);
183
+ break;
184
+ }
185
+ }
186
+ return out;
194
187
  }
195
- function flattenMembers(typeName, decls, seen = /* @__PURE__ */new Set()) {
196
- if (typeName === "ViewProps" || seen.has(typeName)) return [];
197
- seen.add(typeName);
198
- const decl = decls.get(typeName);
199
- if (!decl) return [];
200
- const out = [];
201
- const heritage = decl.extends ?? decl.heritage ?? [];
202
- for (const h of heritage) {
203
- const name = readTypeName(h.id ?? h.expression ?? h.typeName);
204
- if (name) out.push(...flattenMembers(name, decls, seen));
205
- }
206
- const bodyNode = decl.body ??
207
- // interface
208
- decl.right ??
209
- // type alias
210
- decl.typeAnnotation;
211
- out.push(...readObjectMembers(bodyNode, decls, seen));
212
- return out;
188
+ function flattenMembers(typeName, decls, seen = /* @__PURE__ */ new Set()) {
189
+ if (typeName === "ViewProps" || seen.has(typeName)) return [];
190
+ seen.add(typeName);
191
+ const decl = decls.get(typeName);
192
+ if (!decl) return [];
193
+ const out = [];
194
+ const heritage = decl.extends ?? decl.heritage ?? [];
195
+ for (const h of heritage) {
196
+ const name = readTypeName(h.id ?? h.expression ?? h.typeName);
197
+ if (name) out.push(...flattenMembers(name, decls, seen));
198
+ }
199
+ const bodyNode = decl.body ?? decl.right ?? decl.typeAnnotation;
200
+ out.push(...readObjectMembers(bodyNode, decls, seen));
201
+ return out;
213
202
  }
214
203
  function readObjectMembers(node, decls, seen) {
215
- if (!node) return [];
216
- if (node.type === "TSTypeReference" || node.type === "GenericTypeAnnotation") {
217
- const name = readTypeName(node.typeName ?? node.id);
218
- const args = (node.typeArguments ?? node.typeParameters)?.params ?? [];
219
- if (TRANSPARENT.has(name) && args.length) {
220
- return readObjectMembers(args[0], decls, seen);
221
- }
222
- return flattenMembers(name, decls, seen);
223
- }
224
- if (node.type === "TSIntersectionType" || node.type === "IntersectionTypeAnnotation") {
225
- const out2 = [];
226
- for (const t of node.types ?? []) out2.push(...readObjectMembers(t, decls, seen));
227
- return out2;
228
- }
229
- const members = node.members ?? node.body ?? node.properties ?? [];
230
- const out = [];
231
- for (const m of members) {
232
- switch (m.type) {
233
- case "TSPropertySignature":
234
- case "ObjectTypeProperty":
235
- {
236
- const name = m.key?.name ?? m.key?.value;
237
- if (!name) break;
238
- out.push({
239
- name,
240
- type: readType(m.typeAnnotation ?? m.value)
241
- });
242
- break;
243
- }
244
- // flow's `...OtherProps` inside an object type
245
- case "ObjectTypeSpreadProperty":
246
- {
247
- const name = readTypeName(m.argument?.id ?? m.argument);
248
- if (name) out.push(...flattenMembers(name, decls, seen));
249
- break;
250
- }
251
- }
252
- }
253
- return out;
204
+ if (!node) return [];
205
+ if (node.type === "TSTypeReference" || node.type === "GenericTypeAnnotation") {
206
+ const name = readTypeName(node.typeName ?? node.id);
207
+ const args = (node.typeArguments ?? node.typeParameters)?.params ?? [];
208
+ if (TRANSPARENT.has(name) && args.length) {
209
+ return readObjectMembers(args[0], decls, seen);
210
+ }
211
+ return flattenMembers(name, decls, seen);
212
+ }
213
+ if (node.type === "TSIntersectionType" || node.type === "IntersectionTypeAnnotation") {
214
+ const out2 = [];
215
+ for (const t of node.types ?? []) out2.push(...readObjectMembers(t, decls, seen));
216
+ return out2;
217
+ }
218
+ const members = node.members ?? node.body ?? node.properties ?? [];
219
+ const out = [];
220
+ for (const m of members) {
221
+ switch (m.type) {
222
+ case "TSPropertySignature":
223
+ case "ObjectTypeProperty": {
224
+ const name = m.key?.name ?? m.key?.value;
225
+ if (!name) break;
226
+ out.push({
227
+ name,
228
+ type: readType(m.typeAnnotation ?? m.value)
229
+ });
230
+ break;
231
+ }
232
+ case "ObjectTypeSpreadProperty": {
233
+ const name = readTypeName(m.argument?.id ?? m.argument);
234
+ if (name) out.push(...flattenMembers(name, decls, seen));
235
+ break;
236
+ }
237
+ }
238
+ }
239
+ return out;
254
240
  }
255
241
  function findComponentCall(body) {
256
- for (const node of body) {
257
- if (node.type !== "ExportDefaultDeclaration") continue;
258
- let expr = node.declaration;
259
- while (expr) {
260
- if (expr.type === "CallExpression") break;
261
- expr = expr.expression ?? (expr.type === "SequenceExpression" ? expr.expressions[expr.expressions.length - 1] : null);
262
- }
263
- if (expr?.type === "CallExpression" && expr.callee?.name === "codegenNativeComponent") {
264
- return expr;
265
- }
266
- }
267
- return null;
242
+ for (const node of body) {
243
+ if (node.type !== "ExportDefaultDeclaration") continue;
244
+ let expr = node.declaration;
245
+ while (expr) {
246
+ if (expr.type === "CallExpression") break;
247
+ expr = expr.expression ?? (expr.type === "SequenceExpression" ? expr.expressions[expr.expressions.length - 1] : null);
248
+ }
249
+ if (expr?.type === "CallExpression" && expr.callee?.name === "codegenNativeComponent") {
250
+ return expr;
251
+ }
252
+ }
253
+ return null;
268
254
  }
269
255
  function findCommandsCall(body) {
270
- for (const raw of body) {
271
- const node = raw?.type === "ExportNamedDeclaration" ? raw.declaration : raw;
272
- const decl = node?.declarations?.[0];
273
- let init = decl?.init;
274
- while (init && init.type !== "CallExpression") init = init.expression;
275
- if (init?.type === "CallExpression" && init.callee?.name === "codegenNativeCommands") {
276
- return init;
277
- }
278
- }
279
- return null;
256
+ for (const raw of body) {
257
+ const node = raw?.type === "ExportNamedDeclaration" ? raw.declaration : raw;
258
+ const decl = node?.declarations?.[0];
259
+ let init = decl?.init;
260
+ while (init && init.type !== "CallExpression") init = init.expression;
261
+ if (init?.type === "CallExpression" && init.callee?.name === "codegenNativeCommands") {
262
+ return init;
263
+ }
264
+ }
265
+ return null;
280
266
  }
281
267
  function readStringOption(objectExpression, key) {
282
- for (const p of objectExpression?.properties ?? []) {
283
- if ((p.key?.name ?? p.key?.value) === key && typeof p.value?.value === "string") {
284
- return p.value.value;
285
- }
286
- }
287
- return null;
268
+ for (const p of objectExpression?.properties ?? []) {
269
+ if ((p.key?.name ?? p.key?.value) === key && typeof p.value?.value === "string") {
270
+ return p.value.value;
271
+ }
272
+ }
273
+ return null;
288
274
  }
289
275
  function readCommands(call, decls) {
290
- const typeName = readTypeName((call.typeArguments ?? call.typeParameters)?.params?.[0]?.typeName ?? (call.typeArguments ?? call.typeParameters)?.params?.[0]?.id);
291
- const supported = [];
292
- for (const p of call.arguments?.[0]?.properties ?? []) {
293
- if ((p.key?.name ?? p.key?.value) !== "supportedCommands") continue;
294
- for (const el of p.value?.elements ?? []) {
295
- if (typeof el?.value === "string") supported.push(el.value);
296
- }
297
- }
298
- if (!supported.length) return [];
299
- const decl = decls.get(typeName);
300
- const byName = /* @__PURE__ */new Map();
301
- const commandMembers = decl?.body?.body ?? decl?.body?.members ?? decl?.body?.properties ?? [];
302
- for (const m of commandMembers) {
303
- const name = m.key?.name ?? m.key?.value;
304
- if (!name) continue;
305
- const fn = m.typeAnnotation?.typeAnnotation ?? m.value ?? m.typeAnnotation;
306
- const params = (fn?.params ?? fn?.parameters ?? []).slice(1);
307
- byName.set(name, params.map((p, i) => p.name?.name ?? p.name ?? `arg${i}`));
308
- }
309
- return supported.map(name => ({
310
- name,
311
- params: byName.get(name) ?? []
312
- }));
276
+ const typeName = readTypeName((call.typeArguments ?? call.typeParameters)?.params?.[0]?.typeName ?? (call.typeArguments ?? call.typeParameters)?.params?.[0]?.id);
277
+ const supported = [];
278
+ for (const p of call.arguments?.[0]?.properties ?? []) {
279
+ if ((p.key?.name ?? p.key?.value) !== "supportedCommands") continue;
280
+ for (const el of p.value?.elements ?? []) {
281
+ if (typeof el?.value === "string") supported.push(el.value);
282
+ }
283
+ }
284
+ if (!supported.length) return [];
285
+ const decl = decls.get(typeName);
286
+ const byName = /* @__PURE__ */ new Map();
287
+ const commandMembers = decl?.body?.body ?? decl?.body?.members ?? decl?.body?.properties ?? [];
288
+ for (const m of commandMembers) {
289
+ const name = m.key?.name ?? m.key?.value;
290
+ if (!name) continue;
291
+ const fn = m.typeAnnotation?.typeAnnotation ?? m.value ?? m.typeAnnotation;
292
+ const params = (fn?.params ?? fn?.parameters ?? []).slice(1);
293
+ byName.set(name, params.map((p, i) => p.name?.name ?? p.name ?? `arg${i}`));
294
+ }
295
+ return supported.map((name) => ({
296
+ name,
297
+ params: byName.get(name) ?? []
298
+ }));
313
299
  }
314
300
  function indent(text, spaces) {
315
- const pad = " ".repeat(spaces);
316
- return text.split("\n").map((line, i) => i === 0 || line === "" ? line : pad + line).join("\n");
301
+ const pad = " ".repeat(spaces);
302
+ return text.split("\n").map((line, i) => i === 0 || line === "" ? line : pad + line).join("\n");
317
303
  }
318
304
  function printViewConfig(componentName, attributes, events) {
319
- const lines = [` uiViewClassName: ${JSON.stringify(componentName)}`];
320
- const bubbling = events.filter(e => e.bubbling);
321
- if (bubbling.length) {
322
- lines.push(` bubblingEventTypes: {
323
- ${bubbling.map(e => ` ${e.topName}: {
305
+ const lines = [` uiViewClassName: ${JSON.stringify(componentName)}`];
306
+ const bubbling = events.filter((e) => e.bubbling);
307
+ if (bubbling.length) {
308
+ lines.push(` bubblingEventTypes: {
309
+ ${bubbling.map((e) => ` ${e.topName}: {
324
310
  phasedRegistrationNames: {
325
311
  captured: ${JSON.stringify(`${e.name}Capture`)},
326
312
  bubbled: ${JSON.stringify(e.name)}
327
313
  }
328
314
  }`).join(",\n")}
329
315
  }`);
330
- }
331
- const direct = events.filter(e => !e.bubbling);
332
- if (direct.length) {
333
- lines.push(` directEventTypes: {
334
- ${direct.map(e => ` ${e.topName}: {
316
+ }
317
+ const direct = events.filter((e) => !e.bubbling);
318
+ if (direct.length) {
319
+ lines.push(` directEventTypes: {
320
+ ${direct.map((e) => ` ${e.topName}: {
335
321
  registrationName: ${JSON.stringify(e.name)}
336
322
  }`).join(",\n")}
337
323
  }`);
338
- }
339
- const attrLines = attributes.map(a => ` ${a.name}: ${indent(a.value, 4)}`);
340
- if (events.length) {
341
- attrLines.push(` ...ConditionallyIgnoredEventHandlers({
342
- ${events.map(e => ` ${e.name}: true`).join(",\n")}
324
+ }
325
+ const attrLines = attributes.map((a) => ` ${a.name}: ${indent(a.value, 4)}`);
326
+ if (events.length) {
327
+ attrLines.push(` ...ConditionallyIgnoredEventHandlers({
328
+ ${events.map((e) => ` ${e.name}: true`).join(",\n")}
343
329
  })`);
344
- }
345
- lines.push(attrLines.length ? ` validAttributes: {
330
+ }
331
+ lines.push(attrLines.length ? ` validAttributes: {
346
332
  ${attrLines.join(",\n")}
347
333
  }` : " validAttributes: {}");
348
- return `{
334
+ return `{
349
335
  ${lines.join(",\n")}
350
336
  }`;
351
337
  }
352
338
  function printCommands(commands) {
353
- return `export const Commands = {
354
- ${commands.map(c => ` ${c.name}(ref${c.params.map(p => `, ${p}`).join("")}) {
339
+ return `export const Commands = {
340
+ ${commands.map((c) => ` ${c.name}(ref${c.params.map((p) => `, ${p}`).join("")}) {
355
341
  dispatchCommand(ref, ${JSON.stringify(c.name)}, [${c.params.join(", ")}]);
356
342
  }`).join(",\n")}
357
343
  };`;
@@ -370,81 +356,79 @@ const FILE_HEADER = `/**
370
356
  'use strict';
371
357
  `;
372
358
  function parseSpec(code, filename, projectRoot) {
373
- const isTS = /\.[cm]?tsx?$/.test(filename);
374
- if (isTS) {
375
- const result = parseSync(filename, code, {
376
- lang: filename.endsWith("x") ? "tsx" : "ts"
377
- });
378
- if (result.errors?.length) return null;
379
- return result.program;
380
- }
381
- const hermes = getHermesParser(projectRoot);
382
- if (!hermes) return null;
383
- try {
384
- return hermes.parse(code, {
385
- babel: false,
386
- flow: "all",
387
- sourceFilename: filename
388
- });
389
- } catch {
390
- return null;
391
- }
359
+ const isTS = /\.[cm]?tsx?$/.test(filename);
360
+ if (isTS) {
361
+ const result = parseSync(filename, code, { lang: filename.endsWith("x") ? "tsx" : "ts" });
362
+ if (result.errors?.length) return null;
363
+ return result.program;
364
+ }
365
+ const hermes = getHermesParser(projectRoot);
366
+ if (!hermes) return null;
367
+ try {
368
+ return hermes.parse(code, {
369
+ babel: false,
370
+ flow: "all",
371
+ sourceFilename: filename
372
+ });
373
+ } catch {
374
+ return null;
375
+ }
392
376
  }
393
377
  function generateViewConfig(program) {
394
- const body = program?.body;
395
- if (!body) return null;
396
- const call = findComponentCall(body);
397
- if (!call) return null;
398
- const componentNameArg = call.arguments?.[0];
399
- if (typeof componentNameArg?.value !== "string") return null;
400
- const componentName = componentNameArg.value;
401
- const options = call.arguments?.[1];
402
- const paperComponentName = readStringOption(options, "paperComponentName");
403
- const paperComponentNameDeprecated = readStringOption(options, "paperComponentNameDeprecated");
404
- const propsTypeName = readTypeName((call.typeArguments ?? call.typeParameters)?.params?.[0]?.typeName ?? (call.typeArguments ?? call.typeParameters)?.params?.[0]?.id);
405
- if (!propsTypeName) return null;
406
- const decls = collectDeclarations(body);
407
- const members = flattenMembers(propsTypeName, decls);
408
- const attributes = [];
409
- const events = [];
410
- for (const m of members) {
411
- const event = eventOf(m.name, m.type);
412
- if (event) {
413
- events.push(event);
414
- continue;
415
- }
416
- attributes.push({
417
- name: m.name,
418
- value: attributeValue(m.type)
419
- });
420
- }
421
- const commandsCall = findCommandsCall(body);
422
- const commands = commandsCall ? readCommands(commandsCall, decls) : [];
423
- const imports = /* @__PURE__ */new Set([IMPORT_REGISTRY]);
424
- if (events.length) imports.add(IMPORT_IGNORE);
425
- if (commands.length) imports.add(IMPORT_DISPATCH);
426
- if (paperComponentNameDeprecated) imports.add(IMPORT_UIMANAGER);
427
- const nativeName = paperComponentName ?? componentName;
428
- const parts = [`let nativeComponentName = '${nativeName}';`];
429
- if (paperComponentNameDeprecated) {
430
- parts.push(`if (UIManager.hasViewManagerConfig('${componentName}')) {
378
+ const body = program?.body;
379
+ if (!body) return null;
380
+ const call = findComponentCall(body);
381
+ if (!call) return null;
382
+ const componentNameArg = call.arguments?.[0];
383
+ if (typeof componentNameArg?.value !== "string") return null;
384
+ const componentName = componentNameArg.value;
385
+ const options = call.arguments?.[1];
386
+ const paperComponentName = readStringOption(options, "paperComponentName");
387
+ const paperComponentNameDeprecated = readStringOption(options, "paperComponentNameDeprecated");
388
+ const propsTypeName = readTypeName((call.typeArguments ?? call.typeParameters)?.params?.[0]?.typeName ?? (call.typeArguments ?? call.typeParameters)?.params?.[0]?.id);
389
+ if (!propsTypeName) return null;
390
+ const decls = collectDeclarations(body);
391
+ const members = flattenMembers(propsTypeName, decls);
392
+ const attributes = [];
393
+ const events = [];
394
+ for (const m of members) {
395
+ const event = eventOf(m.name, m.type);
396
+ if (event) {
397
+ events.push(event);
398
+ continue;
399
+ }
400
+ attributes.push({
401
+ name: m.name,
402
+ value: attributeValue(m.type)
403
+ });
404
+ }
405
+ const commandsCall = findCommandsCall(body);
406
+ const commands = commandsCall ? readCommands(commandsCall, decls) : [];
407
+ const imports = /* @__PURE__ */ new Set([IMPORT_REGISTRY]);
408
+ if (events.length) imports.add(IMPORT_IGNORE);
409
+ if (commands.length) imports.add(IMPORT_DISPATCH);
410
+ if (paperComponentNameDeprecated) imports.add(IMPORT_UIMANAGER);
411
+ const nativeName = paperComponentName ?? componentName;
412
+ const parts = [`let nativeComponentName = '${nativeName}';`];
413
+ if (paperComponentNameDeprecated) {
414
+ parts.push(`if (UIManager.hasViewManagerConfig('${componentName}')) {
431
415
  nativeComponentName = '${componentName}';
432
416
  } else if (UIManager.hasViewManagerConfig('${paperComponentNameDeprecated}')) {
433
417
  nativeComponentName = '${paperComponentNameDeprecated}';
434
418
  } else {
435
419
  throw new Error('Failed to find native component for either "${componentName}" or "${paperComponentNameDeprecated}"');
436
420
  }`);
437
- }
438
- parts.push(`export const __INTERNAL_VIEW_CONFIG = ${printViewConfig(nativeName, attributes, events)};`);
439
- parts.push("export default NativeComponentRegistry.get(nativeComponentName, () => __INTERNAL_VIEW_CONFIG);");
440
- if (commands.length) parts.push(printCommands(commands));
441
- return {
442
- imports: [...imports].sort(),
443
- body: parts.join("\n")
444
- };
421
+ }
422
+ parts.push(`export const __INTERNAL_VIEW_CONFIG = ${printViewConfig(nativeName, attributes, events)};`);
423
+ parts.push("export default NativeComponentRegistry.get(nativeComponentName, () => __INTERNAL_VIEW_CONFIG);");
424
+ if (commands.length) parts.push(printCommands(commands));
425
+ return {
426
+ imports: [...imports].sort(),
427
+ body: parts.join("\n")
428
+ };
445
429
  }
446
430
  function renderViewConfigModule(result) {
447
- return `
431
+ return `
448
432
  ${FILE_HEADER}
449
433
  ${result.imports.join("\n")}
450
434
 
@@ -452,9 +436,10 @@ ${result.body}
452
436
  `;
453
437
  }
454
438
  function getLibraryName(filename) {
455
- const base = basename(filename);
456
- const replaced = base.replace(/NativeComponent\.[cm]?[jt]sx?$/, "");
457
- return replaced === base ? base.replace(/\.[cm]?[jt]sx?$/, "") : replaced;
439
+ const base = basename(filename);
440
+ const replaced = base.replace(/NativeComponent\.[cm]?[jt]sx?$/, "");
441
+ return replaced === base ? base.replace(/\.[cm]?[jt]sx?$/, "") : replaced;
458
442
  }
443
+
459
444
  export { generateViewConfig, getLibraryName, parseSpec, renderViewConfigModule };
460
445
  //# sourceMappingURL=reactNativeViewConfig.mjs.map