embedded-react 0.19.0 → 0.21.0

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 (148) hide show
  1. package/NOTICE +5 -0
  2. package/README.md +8 -3
  3. package/aot/app-file-loader.mjs +116 -0
  4. package/aot/compile.mjs +180 -90
  5. package/aot/compiler/animations.mjs +568 -317
  6. package/aot/compiler/boot.mjs +97 -35
  7. package/aot/compiler/branch-merge.mjs +4 -3
  8. package/aot/compiler/check.mjs +70 -18
  9. package/aot/compiler/collect.mjs +787 -288
  10. package/aot/compiler/contexts.mjs +391 -0
  11. package/aot/compiler/control-flow.mjs +439 -268
  12. package/aot/compiler/custom-hooks.mjs +686 -0
  13. package/aot/compiler/diagnostics.mjs +22 -25
  14. package/aot/compiler/elements/activity-indicator.mjs +25 -24
  15. package/aot/compiler/elements/button.mjs +182 -0
  16. package/aot/compiler/elements/dial.mjs +91 -77
  17. package/aot/compiler/elements/flat-list.mjs +24 -22
  18. package/aot/compiler/elements/image-background.mjs +76 -0
  19. package/aot/compiler/elements/image.mjs +52 -50
  20. package/aot/compiler/elements/modal.mjs +31 -30
  21. package/aot/compiler/elements/props.mjs +61 -26
  22. package/aot/compiler/elements/rewrite.mjs +118 -0
  23. package/aot/compiler/elements/section-list.mjs +370 -0
  24. package/aot/compiler/elements/switch.mjs +42 -34
  25. package/aot/compiler/elements/text-input.mjs +34 -32
  26. package/aot/compiler/expressions.mjs +1685 -392
  27. package/aot/compiler/handlers.mjs +2029 -547
  28. package/aot/compiler/helper-values.mjs +312 -0
  29. package/aot/compiler/ir/functions.mjs +11 -1
  30. package/aot/compiler/ir/nodes.mjs +43 -4
  31. package/aot/compiler/ir/reads.mjs +223 -22
  32. package/aot/compiler/ir/text.mjs +5 -1
  33. package/aot/compiler/ir/types.mjs +26 -14
  34. package/aot/compiler/ir/vectors.mjs +5 -1
  35. package/aot/compiler/kept-states.mjs +177 -0
  36. package/aot/compiler/keyboard.mjs +17 -7
  37. package/aot/compiler/list-items.mjs +226 -0
  38. package/aot/compiler/list-order.mjs +586 -0
  39. package/aot/compiler/list-split.mjs +259 -0
  40. package/aot/compiler/loops.mjs +582 -0
  41. package/aot/compiler/lower.mjs +246 -111
  42. package/aot/compiler/native-files.mjs +439 -0
  43. package/aot/compiler/native-header.mjs +335 -0
  44. package/aot/compiler/native-modules.mjs +1173 -0
  45. package/aot/compiler/nodes.mjs +195 -172
  46. package/aot/compiler/options.mjs +37 -31
  47. package/aot/compiler/out.mjs +32 -23
  48. package/aot/compiler/package/animations.mjs +779 -0
  49. package/aot/compiler/package/arena.mjs +252 -0
  50. package/aot/compiler/package/assemble.mjs +175 -0
  51. package/aot/compiler/package/bytecode.mjs +315 -0
  52. package/aot/compiler/package/checks.mjs +1026 -0
  53. package/aot/compiler/package/codegen.mjs +2168 -0
  54. package/aot/compiler/package/enums.mjs +148 -0
  55. package/aot/compiler/package/format.mjs +771 -0
  56. package/aot/compiler/package/layouts.mjs +136 -0
  57. package/aot/compiler/package/lower.mjs +2103 -0
  58. package/aot/compiler/package/props.mjs +118 -0
  59. package/aot/compiler/package/string-caps.mjs +515 -0
  60. package/aot/compiler/package/tables.mjs +127 -0
  61. package/aot/compiler/package/vectors.mjs +502 -0
  62. package/aot/compiler/package/writer.mjs +686 -0
  63. package/aot/compiler/pan-responder.mjs +69 -61
  64. package/aot/compiler/parse.mjs +23 -21
  65. package/aot/compiler/record-shapes.mjs +246 -0
  66. package/aot/compiler/records.mjs +654 -0
  67. package/aot/compiler/reducers.mjs +272 -0
  68. package/aot/compiler/static-eval.mjs +340 -30
  69. package/aot/compiler/style-text.mjs +178 -174
  70. package/aot/compiler/svg.mjs +480 -251
  71. package/aot/compiler/switch-cases.mjs +198 -0
  72. package/aot/compiler/tagged-records.mjs +450 -0
  73. package/aot/compiler/typing.mjs +307 -0
  74. package/aot/compiler/updaters.mjs +320 -0
  75. package/aot/compiler/window-dimensions.mjs +94 -0
  76. package/aot/compiler/worked-values.mjs +319 -0
  77. package/aot/demo-file.mjs +30 -0
  78. package/aot/screenshot-smoke.mjs +54 -40
  79. package/aot/style-map.mjs +152 -92
  80. package/assets/bake-font.mjs +123 -77
  81. package/assets/bake-image.mjs +39 -22
  82. package/assets/bake-svg.mjs +361 -262
  83. package/assets/build-builtin-font.mjs +11 -7
  84. package/assets/emit-c.mjs +77 -58
  85. package/assets/emit-container.mjs +123 -37
  86. package/assets/emit-pack.mjs +77 -67
  87. package/assets/font-config.mjs +9 -6
  88. package/assets/font-sizes.mjs +163 -123
  89. package/assets/glyph-coverage.mjs +74 -54
  90. package/assets/index.mjs +31 -9
  91. package/assets/rasterize.mjs +126 -99
  92. package/assets/svg-loader.mjs +8 -8
  93. package/build.mjs +23 -13
  94. package/cli.mjs +402 -172
  95. package/compile-bin.mjs +3 -3
  96. package/flash.mjs +537 -0
  97. package/hotreload/dev-device.mjs +90 -33
  98. package/hotreload/pack-app.mjs +55 -43
  99. package/hotreload/pack-split.mjs +62 -64
  100. package/hotreload/package-transfer.mjs +175 -0
  101. package/hotreload/protocol.mjs +50 -37
  102. package/hotreload/uploader.mjs +38 -27
  103. package/hotreload/watch-tree.mjs +19 -19
  104. package/pack-container.mjs +37 -21
  105. package/package.json +7 -8
  106. package/persist-transform.mjs +34 -28
  107. package/qjsc-wasm.mjs +25 -14
  108. package/sim/embedded-react.cjs +1 -1
  109. package/sim/embedded-react.js +1 -1
  110. package/sim/embedded-react.wasm +0 -0
  111. package/sim/index.html +94 -85
  112. package/sim-server.mjs +113 -94
  113. package/src/embedded-react/Animated.js +24 -24
  114. package/src/embedded-react/Dimensions.js +37 -0
  115. package/src/embedded-react/Easing.js +1 -1
  116. package/src/embedded-react/FlatList.js +3 -3
  117. package/src/embedded-react/LayoutAnimation.js +8 -5
  118. package/src/embedded-react/PanResponder.js +75 -71
  119. package/src/embedded-react/Pressable.js +2 -8
  120. package/src/embedded-react/SectionList.js +7 -9
  121. package/src/embedded-react/StyleSheet.js +4 -3
  122. package/src/embedded-react/TouchableOpacity.js +13 -8
  123. package/src/embedded-react/index.d.ts +88 -8
  124. package/src/embedded-react/index.js +2 -0
  125. package/src/embedded-react/layout-anim-config.js +10 -4
  126. package/src/embedded-react/list-child.js +5 -5
  127. package/src/embedded-react/named-colors.js +1 -1
  128. package/src/embedded-react/nativeModule.js +462 -0
  129. package/src/embedded-react/split-style.js +11 -11
  130. package/src/embedded-react/svg-ops.js +760 -593
  131. package/src/embedded-react/useHostValue.js +3 -3
  132. package/src/embedded-react/usePersistentState.js +6 -6
  133. package/src/embedded-react/warn-props.js +1 -1
  134. package/src/host-config.js +60 -53
  135. package/src/props.js +49 -41
  136. package/src/renderer.js +7 -4
  137. package/aot/compiler/assemble.mjs +0 -944
  138. package/aot/compiler/c/animations.mjs +0 -436
  139. package/aot/compiler/c/boot.mjs +0 -127
  140. package/aot/compiler/c/expressions.mjs +0 -270
  141. package/aot/compiler/c/functions.mjs +0 -268
  142. package/aot/compiler/c/keyboard.mjs +0 -96
  143. package/aot/compiler/c/nodes.mjs +0 -303
  144. package/aot/compiler/c/pan-responder.mjs +0 -136
  145. package/aot/compiler/c/statements.mjs +0 -319
  146. package/aot/compiler/c/text.mjs +0 -83
  147. package/aot/compiler/c/vectors.mjs +0 -381
  148. package/aot/compiler/c-syntax.mjs +0 -127
@@ -0,0 +1,439 @@
1
+ /*
2
+ * Copyright 2026 Cory Lamming
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
16
+ /*
17
+ * The files an app's native modules come from, read ahead so the compile itself stays pure: the local files it
18
+ * imports a module's interface from, and the files and packages it imports declared modules from, resolved the Node
19
+ * way. A module is read from its .ts source, so a package that ships only JavaScript or .d.ts is kept as a file with
20
+ * no source, for the compiler to refuse.
21
+ */
22
+ /*----------------------------------------------------------------------------------------------------------------------
23
+ - Imports
24
+ ---------------------------------------------------------------------------------------------------------------------*/
25
+ import { existsSync, readFileSync, realpathSync, statSync } from 'node:fs';
26
+ import { dirname, isAbsolute, join, relative, resolve } from 'node:path';
27
+ import { parse } from '@babel/parser';
28
+ import { declarationsOf, declaredMembers, localImportOf, } from './native-modules.mjs';
29
+ /*----------------------------------------------------------------------------------------------------------------------
30
+ - Constants
31
+ ---------------------------------------------------------------------------------------------------------------------*/
32
+ /** What a local .ts file may be called, after the path it is imported by. */
33
+ const SOURCE_EXTENSIONS = ['.ts', '.mts', '.tsx', '/index.ts'];
34
+ /** What a JavaScript or .d.ts file may be called, after the path it is imported by. */
35
+ const BUILT_EXTENSIONS = ['.js', '.mjs', '.cjs', '.jsx', '.d.ts', '/index.js'];
36
+ /** What a module's mock may be called, after `<name>.mock`. */
37
+ const MOCK_EXTENSIONS = ['.ts', '.tsx', '.js', '.jsx'];
38
+ /** The conditions a package's `exports` is read with, as Node imports it, and as bundlers do. */
39
+ const CONDITIONS = new Set(['import', 'module', 'node', 'default']);
40
+ /** A JavaScript file that declares a native module. */
41
+ const DECLARES_MODULE = /\bnativeModule(?:\.optional)?\s*\(\s*['"`]/;
42
+ /*----------------------------------------------------------------------------------------------------------------------
43
+ - Implementation
44
+ ---------------------------------------------------------------------------------------------------------------------*/
45
+ /**
46
+ * Whether a file is .ts source, not a .d.ts.
47
+ *
48
+ * @param path The file.
49
+ *
50
+ * @returns True when it is.
51
+ */
52
+ const isSourceFile = (path) => /\.[cm]?tsx?$/.test(path) && !/\.d\.[cm]?ts$/.test(path);
53
+ /**
54
+ * Whether a file is JavaScript, JSX included.
55
+ *
56
+ * @param path The file.
57
+ *
58
+ * @returns True when it is.
59
+ */
60
+ const isBuiltFile = (path) => /\.[cm]?jsx?$/.test(path);
61
+ /**
62
+ * Whether a path is a file.
63
+ *
64
+ * @param path The path.
65
+ *
66
+ * @returns True when it is.
67
+ */
68
+ const isFile = (path) => existsSync(path) && statSync(path).isFile();
69
+ /**
70
+ * Parses a file, types and all.
71
+ *
72
+ * @param source Its source.
73
+ *
74
+ * @returns Its program.
75
+ */
76
+ const parseFile = (source) => parse(source, { sourceType: 'module', plugins: ['jsx', 'typescript'] }).program;
77
+ /**
78
+ * The .ts file a path names: `./sensors`, `./sensors.ts`, or `./sensors.js` as NodeNext writes it.
79
+ *
80
+ * @param base The path, resolved.
81
+ *
82
+ * @returns The file, or undefined when there is none.
83
+ */
84
+ function sourceFileOf(base) {
85
+ const candidates = [
86
+ base,
87
+ base.replace(/\.(m?)js$/, '.$1ts'),
88
+ ...SOURCE_EXTENSIONS.map(extension => base + extension),
89
+ ];
90
+ return candidates.find(path => isSourceFile(path) && isFile(path));
91
+ }
92
+ /**
93
+ * The file a path names, .ts source first, then JavaScript or a .d.ts.
94
+ *
95
+ * @param base The path, resolved.
96
+ *
97
+ * @returns The file, or undefined when there is none.
98
+ */
99
+ const fileOf = (base) => sourceFileOf(base) ??
100
+ [base, ...BUILT_EXTENSIONS.map(extension => base + extension)].find(isFile);
101
+ /**
102
+ * The path a package's `exports` gives for a condition object, array or path.
103
+ *
104
+ * @param target The export's value.
105
+ *
106
+ * @returns The path, or null when no condition matches.
107
+ */
108
+ function exportTarget(target) {
109
+ if (typeof target === 'string')
110
+ return target;
111
+ if (Array.isArray(target)) {
112
+ for (const entry of target) {
113
+ const path = exportTarget(entry);
114
+ if (path)
115
+ return path;
116
+ }
117
+ return null;
118
+ }
119
+ if (target && typeof target === 'object') {
120
+ for (const [condition, value] of Object.entries(target)) {
121
+ if (!CONDITIONS.has(condition))
122
+ continue;
123
+ const path = exportTarget(value);
124
+ if (path)
125
+ return path;
126
+ }
127
+ }
128
+ return null;
129
+ }
130
+ /**
131
+ * The path a package's `exports` gives a subpath: an exact key, or one with a `*`.
132
+ *
133
+ * @param exports The package's `exports`.
134
+ * @param subpath The subpath, `.` for the package itself.
135
+ *
136
+ * @returns The path in the package, or null when it isn't exported.
137
+ */
138
+ function exportedPath(exports, subpath) {
139
+ const isSubpathMap = !!exports &&
140
+ typeof exports === 'object' &&
141
+ !Array.isArray(exports) &&
142
+ Object.keys(exports).some(key => key.startsWith('.'));
143
+ if (!isSubpathMap) {
144
+ return subpath === '.' ? packageTarget(exportTarget(exports)) : null;
145
+ }
146
+ const map = exports;
147
+ if (Object.hasOwn(map, subpath)) {
148
+ return packageTarget(exportTarget(map[subpath]));
149
+ }
150
+ // As Node does, the pattern with the longest part before its `*` wins, then the longest pattern.
151
+ let best = null;
152
+ for (const key of Object.keys(map)) {
153
+ const star = key.indexOf('*');
154
+ if (star < 0 || key.indexOf('*', star + 1) >= 0)
155
+ continue;
156
+ const prefix = key.slice(0, star);
157
+ const suffix = key.slice(star + 1);
158
+ const matches = subpath.length >= key.length - 1 &&
159
+ subpath.startsWith(prefix) &&
160
+ subpath.endsWith(suffix);
161
+ if (matches &&
162
+ (!best ||
163
+ prefix.length > best.prefix.length ||
164
+ (prefix.length === best.prefix.length && key.length > best.key.length))) {
165
+ best = { key, prefix, suffix };
166
+ }
167
+ }
168
+ if (!best)
169
+ return null;
170
+ const match = subpath.slice(best.prefix.length, subpath.length - best.suffix.length);
171
+ return packageTarget(exportTarget(map[best.key])?.replaceAll('*', match) ?? null);
172
+ }
173
+ /**
174
+ * An export's path, when it stays in its package as Node requires: `./` then no empty, `.`, `..` or node_modules part.
175
+ *
176
+ * @param path The path the export gives.
177
+ *
178
+ * @returns The path, or null when Node would refuse it.
179
+ */
180
+ function packageTarget(path) {
181
+ if (!path?.startsWith('./'))
182
+ return null;
183
+ const parts = path.slice(2).split(/[\\/]/);
184
+ return parts.some(part => part === '' ||
185
+ part === '.' ||
186
+ part === '..' ||
187
+ part.toLowerCase() === 'node_modules')
188
+ ? null
189
+ : path;
190
+ }
191
+ /**
192
+ * The file a package import names: the package is found in a node_modules folder up from the importer's, and the
193
+ * file by its `exports`, or else its `main` or the path itself.
194
+ *
195
+ * @param fromDirectory The importer's folder.
196
+ * @param specifier The import's path: `my-device`, `@acme/device` or `my-device/can`.
197
+ *
198
+ * @returns The file, or undefined when there is none.
199
+ */
200
+ function packageFileOf(fromDirectory, specifier) {
201
+ const parts = specifier.split('/');
202
+ const nameLength = specifier.startsWith('@') ? 2 : 1;
203
+ const name = parts.slice(0, nameLength).join('/');
204
+ const subpath = ['.', ...parts.slice(nameLength)].join('/');
205
+ for (let directory = fromDirectory;; directory = dirname(directory)) {
206
+ const manifestPath = join(directory, 'node_modules', name, 'package.json');
207
+ if (isFile(manifestPath)) {
208
+ // A workspace or file: package is a link to its own folder.
209
+ return packageEntryOf(realpathSync(dirname(manifestPath)), subpath);
210
+ }
211
+ if (dirname(directory) === directory)
212
+ return undefined;
213
+ }
214
+ }
215
+ /**
216
+ * The file a package gives a subpath, by its `exports`, or else its `main` or the path itself.
217
+ *
218
+ * @param packageDirectory The package's folder.
219
+ * @param subpath The subpath, `.` for the package itself.
220
+ *
221
+ * @returns The file, or undefined when there is none.
222
+ */
223
+ function packageEntryOf(packageDirectory, subpath) {
224
+ const manifest = JSON.parse(readFileSync(join(packageDirectory, 'package.json'), 'utf8'));
225
+ if (manifest.exports !== undefined) {
226
+ const path = exportedPath(manifest.exports, subpath);
227
+ const file = path && resolve(packageDirectory, path);
228
+ return file && isFile(file) ? file : undefined;
229
+ }
230
+ return fileOf(resolve(packageDirectory, subpath === '.' ? (manifest.main ?? 'index') : subpath));
231
+ }
232
+ /**
233
+ * The file an import names, resolved the Node way: a relative or absolute path, or a package.
234
+ *
235
+ * @param fromDirectory The importer's folder.
236
+ * @param specifier The import's path.
237
+ *
238
+ * @returns The file, or undefined when there is none.
239
+ */
240
+ const importedFileOf = (fromDirectory, specifier) => specifier.startsWith('.') || isAbsolute(specifier)
241
+ ? fileOf(resolve(fromDirectory, specifier))
242
+ : packageFileOf(fromDirectory, specifier);
243
+ /**
244
+ * The local .ts file an interface is imported from.
245
+ *
246
+ * @param fromDirectory The importer's folder.
247
+ * @param importPath The path it is imported by.
248
+ *
249
+ * @returns The file, or undefined when there is none.
250
+ */
251
+ export const interfaceFileOf = (fromDirectory, importPath) => sourceFileOf(resolve(fromDirectory, importPath));
252
+ /**
253
+ * The local files a file's module declarations import their interfaces from, by the path each is imported by.
254
+ *
255
+ * @param program The file.
256
+ * @param directory Its folder.
257
+ *
258
+ * @returns The interfaces' files, each found by its import path.
259
+ */
260
+ function interfaceFilesOf(program, directory) {
261
+ const found = new Map();
262
+ for (const declarator of declarationsOf(program)) {
263
+ const typeArgument = declarator.init.typeParameters
264
+ ?.params[0];
265
+ if (typeArgument?.type !== 'TSTypeReference' ||
266
+ typeArgument.typeName.type !== 'Identifier' ||
267
+ declaredMembers(program, typeArgument.typeName.name) !== null) {
268
+ continue;
269
+ }
270
+ const local = localImportOf(program, typeArgument.typeName.name);
271
+ if (!local || found.has(local.path))
272
+ continue;
273
+ const file = interfaceFileOf(directory, local.path);
274
+ if (file) {
275
+ found.set(local.path, file);
276
+ }
277
+ }
278
+ return found;
279
+ }
280
+ /**
281
+ * Reads a .ts file, with the interface files its module declarations import.
282
+ *
283
+ * @param path The file.
284
+ * @param appDirectory The app's folder, which a file's path is shown from.
285
+ *
286
+ * @returns The file.
287
+ */
288
+ function readSourceFile(path, appDirectory) {
289
+ const source = readFileSync(path, 'utf8');
290
+ const imports = {};
291
+ if (/\bnativeModule\b/.test(source)) {
292
+ for (const [importPath, file] of interfaceFilesOf(parseFile(source), dirname(path))) {
293
+ imports[importPath] = {
294
+ path: shownPath(appDirectory, file),
295
+ source: readFileSync(file, 'utf8'),
296
+ imports: {},
297
+ };
298
+ }
299
+ }
300
+ return { path: shownPath(appDirectory, path), source, imports };
301
+ }
302
+ /**
303
+ * A file's path as an error shows it, from the app's folder.
304
+ *
305
+ * @param appDirectory The app's folder.
306
+ * @param path The file.
307
+ *
308
+ * @returns The path.
309
+ */
310
+ const shownPath = (appDirectory, path) => relative(realpathSync(appDirectory), realpathSync(path)).replace(/\\/g, '/');
311
+ /**
312
+ * Whether a file passes on what another file declares, `export * from` or `export {Can} from`, and one of those files
313
+ * declares native modules, or passes them on in turn.
314
+ *
315
+ * @param source The file's source.
316
+ * @param path The file.
317
+ * @param seen The files already looked at, so a loop ends.
318
+ *
319
+ * @returns True when it does.
320
+ */
321
+ function passesOnModules(source, path, seen = new Set([path])) {
322
+ if (!/\bexport\b[^;]*\bfrom\b/.test(source))
323
+ return false;
324
+ return parseFile(source).body.some(statement => {
325
+ if ((statement.type !== 'ExportAllDeclaration' &&
326
+ statement.type !== 'ExportNamedDeclaration') ||
327
+ !statement.source ||
328
+ statement.exportKind === 'type') {
329
+ return false;
330
+ }
331
+ const file = importedFileOf(dirname(path), statement.source.value);
332
+ if (!file || seen.has(file))
333
+ return false;
334
+ seen.add(file);
335
+ const passed = readFileSync(file, 'utf8');
336
+ if (!isSourceFile(file)) {
337
+ return isBuiltFile(file) && DECLARES_MODULE.test(passed);
338
+ }
339
+ return (/\bnativeModule\b/.test(passed) || passesOnModules(passed, file, seen));
340
+ });
341
+ }
342
+ /**
343
+ * Reads the files an app's native modules come from: the local files it imports a module's interface from, and the
344
+ * files and packages it imports modules from.
345
+ *
346
+ * @param source The app's source.
347
+ * @param appDirectory The app's folder, which its imports are resolved from.
348
+ *
349
+ * @returns Each file, by the path the app imports it by.
350
+ */
351
+ export function readNativeFiles(source, appDirectory) {
352
+ const program = parseFile(source);
353
+ const files = {};
354
+ for (const [importPath, file] of interfaceFilesOf(program, appDirectory)) {
355
+ files[importPath] = readSourceFile(file, appDirectory);
356
+ }
357
+ // The modules it imports: a file that may declare one, or a built one that does.
358
+ for (const statement of program.body) {
359
+ if (statement.type !== 'ImportDeclaration' ||
360
+ statement.importKind === 'type' ||
361
+ statement.source.value === 'embedded-react' ||
362
+ statement.source.value in files ||
363
+ !statement.specifiers.some(specifier => specifier.type === 'ImportSpecifier' &&
364
+ specifier.importKind !== 'type')) {
365
+ continue;
366
+ }
367
+ const importPath = statement.source.value;
368
+ const file = importedFileOf(appDirectory, importPath);
369
+ if (!file)
370
+ continue;
371
+ if (isSourceFile(file)) {
372
+ const source = readFileSync(file, 'utf8');
373
+ // A file that passes modules on is read too, for the compiler to refuse.
374
+ if (/\bnativeModule\b/.test(source) || passesOnModules(source, file)) {
375
+ files[importPath] = readSourceFile(file, appDirectory);
376
+ }
377
+ }
378
+ else if (isBuiltFile(file) &&
379
+ DECLARES_MODULE.test(readFileSync(file, 'utf8'))) {
380
+ files[importPath] = {
381
+ path: shownPath(appDirectory, file),
382
+ source: null,
383
+ imports: {},
384
+ };
385
+ }
386
+ }
387
+ return files;
388
+ }
389
+ /**
390
+ * Reads the file a C header is generated from, with the files its modules' interfaces are in.
391
+ *
392
+ * @param target A file, a package's folder, or a package's name as an app imports it.
393
+ * @param fromDirectory The folder a path is from, and a package is looked for from.
394
+ *
395
+ * @returns The file, its source, and its interface files.
396
+ */
397
+ export function readModuleFile(target, fromDirectory) {
398
+ const path = resolve(fromDirectory, target);
399
+ const file = isFile(join(path, 'package.json'))
400
+ ? packageEntryOf(path, '.')
401
+ : (fileOf(path) ?? packageFileOf(fromDirectory, target));
402
+ if (!file) {
403
+ throw new Error(`No file or package at ${target}. Pass the .ts file that declares the modules, or the package's folder or name.`);
404
+ }
405
+ if (!isSourceFile(file)) {
406
+ throw new Error(`${file} is JavaScript or a .d.ts, which a native module isn't read from. ` +
407
+ 'Ship the package\'s .ts source, and point its exports at it: "exports": "./index.ts".');
408
+ }
409
+ const source = readFileSync(file, 'utf8');
410
+ return { path: file, source, files: readNativeFiles(source, dirname(file)) };
411
+ }
412
+ /**
413
+ * Where each module's mock for the simulators may be: `<name>.mock.ts` (or .tsx, .js, .jsx) beside its interface.
414
+ *
415
+ * @param source A file's source.
416
+ * @param baseDirectory The file's folder.
417
+ *
418
+ * @returns The paths to look for, in order, by module name.
419
+ */
420
+ export function nativeMockFiles(source, baseDirectory) {
421
+ const program = parseFile(source);
422
+ const interfaceFiles = interfaceFilesOf(program, baseDirectory);
423
+ const mocks = new Map();
424
+ for (const declarator of declarationsOf(program)) {
425
+ const call = declarator.init;
426
+ const [name] = call.arguments;
427
+ if (name?.type !== 'StringLiteral')
428
+ continue;
429
+ const typeArgument = call.typeParameters?.params[0];
430
+ const local = typeArgument?.type === 'TSTypeReference' &&
431
+ typeArgument.typeName.type === 'Identifier'
432
+ ? localImportOf(program, typeArgument.typeName.name)
433
+ : null;
434
+ const interfaceFile = local ? interfaceFiles.get(local.path) : undefined;
435
+ const directory = interfaceFile ? dirname(interfaceFile) : baseDirectory;
436
+ mocks.set(name.value, MOCK_EXTENSIONS.map(extension => resolve(directory, `${name.value}.mock${extension}`)));
437
+ }
438
+ return mocks;
439
+ }