embedded-react 0.15.0 → 0.17.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 (62) hide show
  1. package/aot/compile.mjs +173 -8272
  2. package/aot/compiler/animations.mjs +1001 -0
  3. package/aot/compiler/assemble.mjs +922 -0
  4. package/aot/compiler/boot.mjs +441 -0
  5. package/aot/compiler/branch-merge.mjs +319 -0
  6. package/aot/compiler/c/animations.mjs +362 -0
  7. package/aot/compiler/c/boot.mjs +127 -0
  8. package/aot/compiler/c/expressions.mjs +270 -0
  9. package/aot/compiler/c/functions.mjs +268 -0
  10. package/aot/compiler/c/keyboard.mjs +96 -0
  11. package/aot/compiler/c/nodes.mjs +294 -0
  12. package/aot/compiler/c/pan-responder.mjs +136 -0
  13. package/aot/compiler/c/statements.mjs +271 -0
  14. package/aot/compiler/c/text.mjs +83 -0
  15. package/aot/compiler/c/vectors.mjs +381 -0
  16. package/aot/compiler/c-syntax.mjs +127 -0
  17. package/aot/compiler/check.mjs +165 -0
  18. package/aot/compiler/collect.mjs +785 -0
  19. package/aot/compiler/control-flow.mjs +696 -0
  20. package/aot/compiler/diagnostics.mjs +137 -0
  21. package/aot/compiler/elements/activity-indicator.mjs +160 -0
  22. package/aot/compiler/elements/dial.mjs +346 -0
  23. package/aot/compiler/elements/flat-list.mjs +139 -0
  24. package/aot/compiler/elements/image.mjs +151 -0
  25. package/aot/compiler/elements/modal.mjs +158 -0
  26. package/aot/compiler/elements/props.mjs +474 -0
  27. package/aot/compiler/elements/switch.mjs +183 -0
  28. package/aot/compiler/elements/text-input.mjs +171 -0
  29. package/aot/compiler/expressions.mjs +1579 -0
  30. package/aot/compiler/handlers.mjs +1072 -0
  31. package/aot/compiler/ir/animations.mjs +16 -0
  32. package/aot/compiler/ir/app.mjs +16 -0
  33. package/aot/compiler/ir/boot.mjs +16 -0
  34. package/aot/compiler/ir/expressions.mjs +16 -0
  35. package/aot/compiler/ir/functions.mjs +16 -0
  36. package/aot/compiler/ir/keyboard.mjs +16 -0
  37. package/aot/compiler/ir/nodes.mjs +107 -0
  38. package/aot/compiler/ir/pan-responder.mjs +16 -0
  39. package/aot/compiler/ir/reads.mjs +351 -0
  40. package/aot/compiler/ir/source.mjs +16 -0
  41. package/aot/compiler/ir/statements.mjs +16 -0
  42. package/aot/compiler/ir/text.mjs +16 -0
  43. package/aot/compiler/ir/types.mjs +185 -0
  44. package/aot/compiler/ir/vectors.mjs +16 -0
  45. package/aot/compiler/keyboard.mjs +152 -0
  46. package/aot/compiler/lower.mjs +311 -0
  47. package/aot/compiler/nodes.mjs +688 -0
  48. package/aot/compiler/options.mjs +55 -0
  49. package/aot/compiler/out.mjs +94 -0
  50. package/aot/compiler/pan-responder.mjs +354 -0
  51. package/aot/compiler/parse.mjs +300 -0
  52. package/aot/compiler/static-eval.mjs +454 -0
  53. package/aot/compiler/style-text.mjs +807 -0
  54. package/aot/compiler/svg.mjs +1167 -0
  55. package/aot/compiler/types.mjs +16 -0
  56. package/aot/screenshot-smoke.mjs +95 -55
  57. package/aot/style-map.mjs +366 -304
  58. package/cli.mjs +5 -8
  59. package/package.json +14 -6
  60. package/sim/embedded-react.wasm +0 -0
  61. package/src/embedded-react/FlatList.js +4 -4
  62. package/src/props.js +1 -1
@@ -0,0 +1,300 @@
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
+ - Imports
18
+ ---------------------------------------------------------------------------------------------------------------------*/
19
+ import { parse } from '@babel/parser';
20
+ import { aotError } from './diagnostics.mjs';
21
+ import { isUndeclaredAttribute } from './elements/props.mjs';
22
+ /*----------------------------------------------------------------------------------------------------------------------
23
+ - Constants
24
+ ---------------------------------------------------------------------------------------------------------------------*/
25
+ // Type-only expression wrappers: `x as T`, `x satisfies T`, `x!`, `<T>x`, `f<T>` — unwrap to the inner expr.
26
+ const TS_EXPR_WRAPPERS = new Set([
27
+ 'TSAsExpression',
28
+ 'TSSatisfiesExpression',
29
+ 'TSNonNullExpression',
30
+ 'TSTypeAssertion',
31
+ 'TSInstantiationExpression',
32
+ ]);
33
+ // Type-only declarations (no runtime presence) — dropped from any statement/body list.
34
+ const TS_TYPE_DECLS = new Set([
35
+ 'TSInterfaceDeclaration',
36
+ 'TSTypeAliasDeclaration',
37
+ 'TSDeclareFunction',
38
+ ]);
39
+ // Type-only fields hung off otherwise-runtime nodes — deleted so nothing downstream traverses into them.
40
+ const TS_TYPE_FIELDS = [
41
+ 'typeAnnotation',
42
+ 'returnType',
43
+ 'typeParameters',
44
+ 'typeArguments',
45
+ 'accessibility',
46
+ 'definite',
47
+ 'declare',
48
+ 'readonly',
49
+ 'override',
50
+ 'abstract',
51
+ ];
52
+ // Keys that never hold child AST nodes — skip them so the scrub stays cheap and never mangles metadata.
53
+ const TS_SKIP_KEYS = new Set([
54
+ 'loc',
55
+ 'start',
56
+ 'end',
57
+ 'range',
58
+ 'leadingComments',
59
+ 'trailingComments',
60
+ 'innerComments',
61
+ 'comments',
62
+ 'extra',
63
+ 'tokens',
64
+ ]);
65
+ /*----------------------------------------------------------------------------------------------------------------------
66
+ - Implementation
67
+ ---------------------------------------------------------------------------------------------------------------------*/
68
+ /**
69
+ * Is this app a TypeScript entry? Driven by the filename extension, or an explicit opts.ts (for tests).
70
+ *
71
+ * @param opts compileSource's options.
72
+ *
73
+ * @returns Whether to parse the source as TypeScript.
74
+ */
75
+ const isTsEntry = (opts) => opts.ts ?? /\.[cm]?tsx$/.test(opts.filename || '');
76
+ /**
77
+ * The Babel parser plugins for an entry.
78
+ *
79
+ * @param isTypeScript Whether the entry is TypeScript.
80
+ *
81
+ * @returns JSX, plus TypeScript when asked.
82
+ */
83
+ const parserPlugins = (isTypeScript) => isTypeScript ? ['jsx', 'typescript'] : ['jsx'];
84
+ /**
85
+ * True for nodes that carry no runtime meaning and must be removed from a statement/specifier list.
86
+ *
87
+ * @param node An element of an AST list.
88
+ *
89
+ * @returns Whether it is type-only.
90
+ */
91
+ const isTypeOnly = (node) => !!node &&
92
+ (TS_TYPE_DECLS.has(node.type) ||
93
+ // `import type ... ` / `export type ...`, and per-specifier `import { type X }`.
94
+ ((node.type === 'ImportDeclaration' || node.type === 'ImportSpecifier') &&
95
+ node.importKind === 'type') ||
96
+ ((node.type === 'ExportNamedDeclaration' ||
97
+ node.type === 'ExportSpecifier') &&
98
+ node.exportKind === 'type'));
99
+ /**
100
+ * Follows a chain of type-only expression wrappers to the runtime expression underneath.
101
+ *
102
+ * @param node An expression, possibly wrapped in `as` / `satisfies` / `!` / `<T>`.
103
+ *
104
+ * @returns The expression under every wrapper.
105
+ */
106
+ const unwrapTs = (node) => {
107
+ while (node && TS_EXPR_WRAPPERS.has(node.type)) {
108
+ node = node.expression;
109
+ }
110
+ return node;
111
+ };
112
+ /**
113
+ * Strips every TypeScript-only construct from a parsed AST, in place: drop type declarations and type
114
+ * imports, unwrap `as`/`!`/`<T>` expression wrappers, and delete type-annotation fields. Remaining nodes
115
+ * keep their .loc, so the compiler's error code-frames stay accurate.
116
+ *
117
+ * @param root The parsed file.
118
+ *
119
+ * @returns The same file, scrubbed.
120
+ */
121
+ function stripTypeScript(root) {
122
+ // A walk over every key of every node, so a node here is a plain record of child values.
123
+ const visit = (node) => {
124
+ if (!node || typeof node !== 'object')
125
+ return;
126
+ // Delete the node's own type-only fields, then walk its children.
127
+ for (const field of TS_TYPE_FIELDS) {
128
+ if (field in node) {
129
+ delete node[field];
130
+ }
131
+ }
132
+ for (const key of Object.keys(node)) {
133
+ if (TS_SKIP_KEYS.has(key))
134
+ continue;
135
+ let value = node[key];
136
+ if (Array.isArray(value)) {
137
+ // A list (statements, specifiers, arguments …): drop type-only entries and unwrap wrapped ones.
138
+ const kept = [];
139
+ for (let item of value) {
140
+ if (isTypeOnly(item))
141
+ continue;
142
+ if (item && TS_EXPR_WRAPPERS.has(item.type)) {
143
+ item = unwrapTs(item);
144
+ }
145
+ kept.push(item);
146
+ visit(item);
147
+ }
148
+ node[key] = kept;
149
+ }
150
+ else if (value && typeof value.type === 'string') {
151
+ // A single child node: replace a type-only wrapper with the expression under it.
152
+ if (TS_EXPR_WRAPPERS.has(value.type)) {
153
+ value = unwrapTs(value);
154
+ node[key] = value;
155
+ }
156
+ visit(value);
157
+ }
158
+ else if (value && typeof value === 'object') {
159
+ visit(value);
160
+ }
161
+ }
162
+ };
163
+ visit(root);
164
+ return root;
165
+ }
166
+ /**
167
+ * One pass over the parsed program that fixes what `undefined` means before anything is compiled.
168
+ *
169
+ * - A JSX attribute whose value is `{undefined}` is DROPPED. Flow A's buildProps omits an undefined prop,
170
+ * so the node keeps its default — `visible={undefined}` stays visible. Doing it here gives the twenty-odd
171
+ * prop readers that view at once, instead of each deciding what an undefined value means.
172
+ * A value that only becomes undefined in some scope — a prop a child was never given — is dropped per
173
+ * emission by omitUndefinedAttrs. This literal pass still matters for <Svg> shapes, which bypass it. An
174
+ * attribute a built-in element does not declare is kept, for the element to refuse by name.
175
+ * - A BINDING named `undefined` is refused. JS allows shadowing the global, but Flow B gives `undefined`
176
+ * a meaning of its own (an omitted prop, empty text), and a shadowing local would silently lose to it.
177
+ *
178
+ * @param ast The parsed file, rewritten in place.
179
+ */
180
+ export function normalizeUndefined(ast) {
181
+ // Refuse an identifier that binds the name `undefined`, pointing the error at it.
182
+ const rejectUndefinedName = (id) => {
183
+ if (id?.type !== 'Identifier' || id.name !== 'undefined')
184
+ return;
185
+ const error = aotError('AOT: `undefined` cannot be used as a name', 'Flow B reads `undefined` as the global everywhere — an omitted prop, or empty text — ' +
186
+ 'so a binding with that name would never be read. Rename it.');
187
+ if (id.loc) {
188
+ error.aotLoc = id.loc.start;
189
+ }
190
+ throw error;
191
+ };
192
+ // Refuse `undefined` anywhere in a binding pattern: plain, destructured, defaulted or rest.
193
+ const rejectUndefinedBindings = (patternNode) => {
194
+ if (!patternNode)
195
+ return;
196
+ if (patternNode.type === 'Identifier') {
197
+ rejectUndefinedName(patternNode);
198
+ }
199
+ else if (patternNode.type === 'ArrayPattern') {
200
+ patternNode.elements.forEach(rejectUndefinedBindings);
201
+ }
202
+ else if (patternNode.type === 'ObjectPattern') {
203
+ patternNode.properties.forEach(property => rejectUndefinedBindings(property.type === 'RestElement' ? property.argument : property.value));
204
+ }
205
+ else if (patternNode.type === 'AssignmentPattern') {
206
+ rejectUndefinedBindings(patternNode.left);
207
+ }
208
+ else if (patternNode.type === 'RestElement') {
209
+ rejectUndefinedBindings(patternNode.argument);
210
+ }
211
+ };
212
+ // A JSX attribute written as exactly `name={undefined}`.
213
+ const isUndefinedAttr = (attr) => attr.type === 'JSXAttribute' &&
214
+ attr.value?.type === 'JSXExpressionContainer' &&
215
+ attr.value.expression.type === 'Identifier' &&
216
+ attr.value.expression.name === 'undefined';
217
+ // Keys that never hold child nodes.
218
+ const SKIP = new Set([
219
+ 'loc',
220
+ 'start',
221
+ 'end',
222
+ 'extra',
223
+ 'leadingComments',
224
+ 'trailingComments',
225
+ 'innerComments',
226
+ ]);
227
+ // A walk over every key of every node, so a node here is a plain record of child values.
228
+ const visit = (node) => {
229
+ // Check the names each declaring node binds and drop `{undefined}` attributes from a JSX element.
230
+ if (!node || typeof node.type !== 'string')
231
+ return;
232
+ switch (node.type) {
233
+ case 'VariableDeclarator':
234
+ rejectUndefinedBindings(node.id);
235
+ break;
236
+ case 'FunctionDeclaration':
237
+ case 'FunctionExpression':
238
+ case 'ArrowFunctionExpression':
239
+ case 'ObjectMethod':
240
+ case 'ClassMethod':
241
+ if (node.id) {
242
+ rejectUndefinedName(node.id);
243
+ }
244
+ node.params.forEach(rejectUndefinedBindings);
245
+ break;
246
+ case 'CatchClause':
247
+ rejectUndefinedBindings(node.param);
248
+ break;
249
+ case 'ClassDeclaration':
250
+ case 'ClassExpression':
251
+ if (node.id) {
252
+ rejectUndefinedName(node.id);
253
+ }
254
+ break;
255
+ case 'ImportSpecifier':
256
+ case 'ImportDefaultSpecifier':
257
+ case 'ImportNamespaceSpecifier':
258
+ rejectUndefinedName(node.local);
259
+ break;
260
+ case 'JSXOpeningElement':
261
+ // An undeclared prop stays, so the element refuses it by name whatever its value.
262
+ node.attributes = node.attributes.filter((attr) => !isUndefinedAttr(attr) ||
263
+ isUndeclaredAttribute(node, attr));
264
+ break;
265
+ }
266
+ // Then walk every child node.
267
+ for (const key of Object.keys(node)) {
268
+ if (SKIP.has(key))
269
+ continue;
270
+ const value = node[key];
271
+ if (Array.isArray(value)) {
272
+ value.forEach(visit);
273
+ }
274
+ else if (value && typeof value.type === 'string') {
275
+ visit(value);
276
+ }
277
+ }
278
+ };
279
+ visit(ast);
280
+ }
281
+ /**
282
+ * Parses an app entry to a JS+JSX AST, transparently stripping TypeScript when the entry is .tsx (JSX is not
283
+ * allowed in a .ts file). The strip is faithful: an App.tsx compiles to the same C as its untyped App.jsx twin.
284
+ *
285
+ * @param src The entry's source.
286
+ * @param opts compileSource's options: its filename (or `ts`) decides whether it is TypeScript.
287
+ *
288
+ * @returns The parsed file.
289
+ */
290
+ export function parseApp(src, opts = {}) {
291
+ const isTypeScript = isTsEntry(opts);
292
+ const ast = parse(src, {
293
+ sourceType: 'module',
294
+ plugins: parserPlugins(isTypeScript),
295
+ });
296
+ if (isTypeScript) {
297
+ stripTypeScript(ast);
298
+ }
299
+ return ast;
300
+ }