embedded-react 0.20.0 → 0.22.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.
- package/NOTICE +5 -0
- package/README.md +3 -2
- package/aot/app-file-loader.mjs +116 -0
- package/aot/compile.mjs +17 -47
- package/aot/compiler/alerts.mjs +604 -0
- package/aot/compiler/animations.mjs +299 -57
- package/aot/compiler/boot.mjs +27 -2
- package/aot/compiler/check.mjs +62 -10
- package/aot/compiler/collect.mjs +517 -137
- package/aot/compiler/contexts.mjs +391 -0
- package/aot/compiler/control-flow.mjs +276 -117
- package/aot/compiler/custom-hooks.mjs +686 -0
- package/aot/compiler/elements/button.mjs +182 -0
- package/aot/compiler/elements/image-background.mjs +76 -0
- package/aot/compiler/elements/image.mjs +2 -1
- package/aot/compiler/elements/props.mjs +47 -8
- package/aot/compiler/elements/rewrite.mjs +118 -0
- package/aot/compiler/elements/section-list.mjs +370 -0
- package/aot/compiler/expressions.mjs +1378 -97
- package/aot/compiler/handlers.mjs +1807 -354
- package/aot/compiler/helper-values.mjs +312 -0
- package/aot/compiler/ir/nodes.mjs +40 -1
- package/aot/compiler/ir/reads.mjs +206 -5
- package/aot/compiler/ir/text.mjs +5 -1
- package/aot/compiler/ir/types.mjs +12 -0
- package/aot/compiler/kept-states.mjs +177 -0
- package/aot/compiler/keyboard-dismiss.mjs +99 -0
- package/aot/compiler/keyboard.mjs +1 -1
- package/aot/compiler/list-items.mjs +226 -0
- package/aot/compiler/list-order.mjs +586 -0
- package/aot/compiler/list-split.mjs +259 -0
- package/aot/compiler/loops.mjs +582 -0
- package/aot/compiler/lower.mjs +203 -53
- package/aot/compiler/native-files.mjs +439 -0
- package/aot/compiler/native-header.mjs +335 -0
- package/aot/compiler/native-modules.mjs +1173 -0
- package/aot/compiler/nodes.mjs +45 -31
- package/aot/compiler/options.mjs +3 -2
- package/aot/compiler/out.mjs +18 -1
- package/aot/compiler/package/animations.mjs +133 -23
- package/aot/compiler/package/assemble.mjs +2 -4
- package/aot/compiler/package/bytecode.mjs +37 -8
- package/aot/compiler/package/checks.mjs +4 -0
- package/aot/compiler/package/codegen.mjs +906 -33
- package/aot/compiler/package/enums.mjs +10 -0
- package/aot/compiler/package/format.mjs +16 -3
- package/aot/compiler/package/lower.mjs +707 -176
- package/aot/compiler/package/string-caps.mjs +515 -0
- package/aot/compiler/package/tables.mjs +24 -0
- package/aot/compiler/package/writer.mjs +22 -0
- package/aot/compiler/parse.mjs +3 -1
- package/aot/compiler/record-shapes.mjs +246 -0
- package/aot/compiler/records.mjs +654 -0
- package/aot/compiler/reducers.mjs +272 -0
- package/aot/compiler/scroll-to.mjs +112 -0
- package/aot/compiler/static-eval.mjs +315 -7
- package/aot/compiler/style-text.mjs +20 -5
- package/aot/compiler/svg.mjs +13 -7
- package/aot/compiler/switch-cases.mjs +198 -0
- package/aot/compiler/tagged-records.mjs +450 -0
- package/aot/compiler/typing.mjs +307 -0
- package/aot/compiler/updaters.mjs +321 -0
- package/aot/compiler/window-dimensions.mjs +94 -0
- package/aot/compiler/worked-values.mjs +319 -0
- package/aot/demo-file.mjs +30 -0
- package/aot/style-map.mjs +94 -3
- package/assets/bake-font.mjs +187 -244
- package/assets/bake-image.mjs +47 -71
- package/assets/bake-svg.mjs +465 -533
- package/assets/build-builtin-font.mjs +14 -27
- package/assets/emit-c.mjs +124 -204
- package/assets/emit-container.mjs +129 -197
- package/assets/emit-pack.mjs +92 -136
- package/assets/font-config.mjs +19 -27
- package/assets/font-sizes.mjs +432 -507
- package/assets/glyph-coverage.mjs +170 -214
- package/assets/index.mjs +52 -97
- package/assets/rasterize.mjs +173 -194
- package/assets/svg-loader.mjs +42 -48
- package/build.mjs +85 -94
- package/cli.mjs +490 -586
- package/compile-bin.mjs +24 -36
- package/flash.mjs +297 -401
- package/hotreload/dev-device.mjs +229 -270
- package/hotreload/pack-app.mjs +118 -159
- package/hotreload/pack-split.mjs +146 -213
- package/hotreload/package-transfer.mjs +128 -131
- package/hotreload/protocol.mjs +118 -118
- package/hotreload/uploader.mjs +132 -159
- package/hotreload/watch-tree.mjs +56 -65
- package/pack-container.mjs +94 -116
- package/package.json +19 -16
- package/persist-transform.mjs +89 -105
- package/qjsc-wasm.mjs +47 -72
- package/sim/embedded-react.wasm +0 -0
- package/sim/index.html +1 -1
- package/sim-server.mjs +260 -354
- package/src/embedded-react/Alert.mjs +112 -0
- package/src/embedded-react/Animated.mjs +412 -0
- package/src/embedded-react/{AppRegistry.js → AppRegistry.mjs} +26 -25
- package/src/embedded-react/{Button.js → Button.mjs} +29 -53
- package/src/embedded-react/Dimensions.mjs +32 -0
- package/src/embedded-react/Easing.mjs +34 -0
- package/src/embedded-react/{FlatList.js → FlatList.mjs} +25 -42
- package/src/embedded-react/{ImageBackground.js → ImageBackground.mjs} +11 -22
- package/src/embedded-react/Keyboard.mjs +82 -0
- package/src/embedded-react/{LayoutAnimation.js → LayoutAnimation.mjs} +17 -28
- package/src/embedded-react/PanResponder.mjs +256 -0
- package/src/embedded-react/{Platform.js → Platform.mjs} +8 -9
- package/src/embedded-react/{Pressable.js → Pressable.mjs} +12 -25
- package/src/embedded-react/SectionList.mjs +103 -0
- package/src/embedded-react/{StyleSheet.js → StyleSheet.mjs} +16 -20
- package/src/embedded-react/{TouchableOpacity.js → TouchableOpacity.mjs} +47 -83
- package/src/embedded-react/background-image.mjs +321 -0
- package/src/embedded-react/{components.js → components.mjs} +1 -3
- package/src/embedded-react/{imperative.js → imperative.mjs} +27 -38
- package/src/embedded-react/index.d.ts +238 -5
- package/src/embedded-react/index.js +26 -17
- package/src/embedded-react/layout-anim-config.mjs +85 -0
- package/src/embedded-react/{list-child.js → list-child.mjs} +11 -20
- package/src/embedded-react/{named-colors.js → named-colors.mjs} +2 -6
- package/src/embedded-react/nativeModule.mjs +371 -0
- package/src/embedded-react/split-style.mjs +57 -0
- package/src/embedded-react/svg-ops.mjs +1075 -0
- package/src/embedded-react/{useHostValue.js → useHostValue.mjs} +2 -4
- package/src/embedded-react/{usePersistentState.js → usePersistentState.mjs} +34 -38
- package/src/embedded-react/{warn-props.js → warn-props.mjs} +32 -39
- package/src/host-config.mjs +512 -0
- package/src/{native-ui.js → native-ui.mjs} +1 -8
- package/src/props.mjs +341 -0
- package/src/{renderer.js → renderer.mjs} +30 -44
- package/src/embedded-react/Animated.js +0 -440
- package/src/embedded-react/Easing.js +0 -39
- package/src/embedded-react/PanResponder.js +0 -330
- package/src/embedded-react/SectionList.js +0 -148
- package/src/embedded-react/layout-anim-config.js +0 -101
- package/src/embedded-react/split-style.js +0 -58
- package/src/embedded-react/svg-ops.js +0 -1295
- package/src/host-config.js +0 -548
- package/src/props.js +0 -306
|
@@ -0,0 +1,272 @@
|
|
|
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
|
+
* useReducer: each `dispatch(action)` runs the reducer in place, as an updater with a block body. The action is bound
|
|
18
|
+
* where it is dispatched: its constant fields fold, so the branches its type rules out are dropped before the body is
|
|
19
|
+
* lowered, and a `throw` for an action no dispatch sends never runs.
|
|
20
|
+
*/
|
|
21
|
+
/*----------------------------------------------------------------------------------------------------------------------
|
|
22
|
+
- Imports
|
|
23
|
+
---------------------------------------------------------------------------------------------------------------------*/
|
|
24
|
+
import { lowerLocal } from './expressions.mjs';
|
|
25
|
+
import { exits, statementsOf } from './helper-values.mjs';
|
|
26
|
+
import { AstLeaf, isObjectTree, objectTreeOf } from './records.mjs';
|
|
27
|
+
import { RuntimeLeaf, evalStatic, foldScope, rethrowFoldLimit, withoutNames, } from './static-eval.mjs';
|
|
28
|
+
import { switchAsIf } from './switch-cases.mjs';
|
|
29
|
+
import { declaredIn } from './updaters.mjs';
|
|
30
|
+
/*----------------------------------------------------------------------------------------------------------------------
|
|
31
|
+
- Implementation
|
|
32
|
+
---------------------------------------------------------------------------------------------------------------------*/
|
|
33
|
+
/**
|
|
34
|
+
* What an expression folds to, if it does.
|
|
35
|
+
*
|
|
36
|
+
* @param node The expression.
|
|
37
|
+
* @param scope The constants a fold may read.
|
|
38
|
+
*
|
|
39
|
+
* @returns The value, or undefined when it does not fold.
|
|
40
|
+
*/
|
|
41
|
+
function folded(node, scope) {
|
|
42
|
+
try {
|
|
43
|
+
return { value: evalStatic(node, scope, 'strict') };
|
|
44
|
+
}
|
|
45
|
+
catch (error) {
|
|
46
|
+
rethrowFoldLimit(error);
|
|
47
|
+
return undefined;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* The local a runtime value is held in, worked out where it is written.
|
|
52
|
+
*
|
|
53
|
+
* @param node The value.
|
|
54
|
+
* @param environment The environment it is written in.
|
|
55
|
+
*
|
|
56
|
+
* @returns The local.
|
|
57
|
+
*/
|
|
58
|
+
function localOf(node, environment) {
|
|
59
|
+
return lowerLocal(node, environment);
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* An object's fields as the reducer reads them: a constant stays one, and anything else is worked out at the
|
|
63
|
+
* dispatch, so names the reducer declares can't change what it reads.
|
|
64
|
+
*
|
|
65
|
+
* @param tree The object.
|
|
66
|
+
* @param environment The dispatch's environment.
|
|
67
|
+
* @param scope The constants a fold may read there.
|
|
68
|
+
*
|
|
69
|
+
* @returns The object.
|
|
70
|
+
*/
|
|
71
|
+
function settledTree(tree, environment, scope) {
|
|
72
|
+
return Object.fromEntries(Object.entries(tree).map(([key, value]) => {
|
|
73
|
+
if (isObjectTree(value)) {
|
|
74
|
+
return [key, settledTree(value, environment, scope)];
|
|
75
|
+
}
|
|
76
|
+
if (!(value instanceof AstLeaf))
|
|
77
|
+
return [key, value];
|
|
78
|
+
const constant = value.locals.size
|
|
79
|
+
? undefined
|
|
80
|
+
: folded(value.node, scope);
|
|
81
|
+
if (constant && typeof constant.value !== 'object') {
|
|
82
|
+
return [key, constant.value];
|
|
83
|
+
}
|
|
84
|
+
const fieldEnvironment = value.locals.size
|
|
85
|
+
? {
|
|
86
|
+
...environment,
|
|
87
|
+
locals: new Map([...environment.locals, ...value.locals]),
|
|
88
|
+
}
|
|
89
|
+
: environment;
|
|
90
|
+
return [key, new RuntimeLeaf(localOf(value.node, fieldEnvironment))];
|
|
91
|
+
}));
|
|
92
|
+
}
|
|
93
|
+
/**
|
|
94
|
+
* An action that picks between two at run time: a ternary of actions, or an object with a field that is a ternary of
|
|
95
|
+
* two constants, `{type: up ? 'inc' : 'dec'}`. Each side is dispatched on its own, so its constant fields fold.
|
|
96
|
+
*
|
|
97
|
+
* @param action What is dispatched.
|
|
98
|
+
* @param scope The constants a fold may read.
|
|
99
|
+
*
|
|
100
|
+
* @returns The test and the action each way, or null when the action picks nothing.
|
|
101
|
+
*/
|
|
102
|
+
export function actionChoice(action, scope) {
|
|
103
|
+
if (action.type === 'ConditionalExpression' && !folded(action.test, scope)) {
|
|
104
|
+
return {
|
|
105
|
+
test: action.test,
|
|
106
|
+
whenTrue: action.consequent,
|
|
107
|
+
whenFalse: action.alternate,
|
|
108
|
+
};
|
|
109
|
+
}
|
|
110
|
+
if (action.type !== 'ObjectExpression')
|
|
111
|
+
return null;
|
|
112
|
+
for (const [index, property] of action.properties.entries()) {
|
|
113
|
+
if (property.type !== 'ObjectProperty' ||
|
|
114
|
+
property.value.type !== 'ConditionalExpression' ||
|
|
115
|
+
folded(property.value.test, scope)) {
|
|
116
|
+
continue;
|
|
117
|
+
}
|
|
118
|
+
const { test, consequent, alternate } = property.value;
|
|
119
|
+
const isConstant = (node) => {
|
|
120
|
+
const value = folded(node, scope);
|
|
121
|
+
return value !== undefined && typeof value.value !== 'object';
|
|
122
|
+
};
|
|
123
|
+
if (!isConstant(consequent) || !isConstant(alternate))
|
|
124
|
+
continue;
|
|
125
|
+
const withValue = (value) => ({
|
|
126
|
+
...action,
|
|
127
|
+
properties: action.properties.map((other, at) => at === index ? { ...property, value } : other),
|
|
128
|
+
});
|
|
129
|
+
return {
|
|
130
|
+
test,
|
|
131
|
+
whenTrue: withValue(consequent),
|
|
132
|
+
whenFalse: withValue(alternate),
|
|
133
|
+
};
|
|
134
|
+
}
|
|
135
|
+
return null;
|
|
136
|
+
}
|
|
137
|
+
/**
|
|
138
|
+
* The environment a reducer's body runs in for one dispatch: the action parameter bound to what is dispatched. A
|
|
139
|
+
* constant folds, an object is its fields, and anything else is a local holding the value.
|
|
140
|
+
*
|
|
141
|
+
* @param parameter The action parameter's name, if the reducer has one.
|
|
142
|
+
* @param action What the dispatch passes.
|
|
143
|
+
* @param environment The dispatch's environment.
|
|
144
|
+
*
|
|
145
|
+
* @returns The body's environment.
|
|
146
|
+
*/
|
|
147
|
+
export function actionEnvironment(parameter, action, environment) {
|
|
148
|
+
if (!parameter)
|
|
149
|
+
return environment;
|
|
150
|
+
const scope = foldScope(environment, environment.constants ?? {});
|
|
151
|
+
const locals = new Map(environment.locals);
|
|
152
|
+
const state = new Map(environment.state);
|
|
153
|
+
const constants = { ...environment.constants };
|
|
154
|
+
locals.delete(parameter);
|
|
155
|
+
state.delete(parameter);
|
|
156
|
+
delete constants[parameter];
|
|
157
|
+
const constant = folded(action, scope);
|
|
158
|
+
const tree = constant ? null : objectTreeOf(action, environment);
|
|
159
|
+
if (constant) {
|
|
160
|
+
constants[parameter] = constant.value;
|
|
161
|
+
}
|
|
162
|
+
else if (tree) {
|
|
163
|
+
constants[parameter] = settledTree(tree, environment, scope);
|
|
164
|
+
}
|
|
165
|
+
else {
|
|
166
|
+
locals.set(parameter, localOf(action, environment));
|
|
167
|
+
}
|
|
168
|
+
return { ...environment, locals, state, constants };
|
|
169
|
+
}
|
|
170
|
+
/**
|
|
171
|
+
* A value with each ternary whose test folds made the side it picks.
|
|
172
|
+
*
|
|
173
|
+
* @param node The value.
|
|
174
|
+
* @param scope The constants a fold may read.
|
|
175
|
+
*
|
|
176
|
+
* @returns The value.
|
|
177
|
+
*/
|
|
178
|
+
function prunedValue(node, scope) {
|
|
179
|
+
if (node.type !== 'ConditionalExpression')
|
|
180
|
+
return node;
|
|
181
|
+
const test = folded(node.test, scope);
|
|
182
|
+
if (test) {
|
|
183
|
+
return prunedValue(test.value ? node.consequent : node.alternate, scope);
|
|
184
|
+
}
|
|
185
|
+
return {
|
|
186
|
+
...node,
|
|
187
|
+
consequent: prunedValue(node.consequent, scope),
|
|
188
|
+
alternate: prunedValue(node.alternate, scope),
|
|
189
|
+
};
|
|
190
|
+
}
|
|
191
|
+
/**
|
|
192
|
+
* Statements with each branch whose test folds made the one it takes, in loops too. A branch taken that always ends
|
|
193
|
+
* drops the statements after it, which never run. Tests on the action's constant fields fold, so a `switch
|
|
194
|
+
* (action.type)` keeps only the case a dispatch takes. A name a block or a loop declares hides what it names outside,
|
|
195
|
+
* so no test folds through it.
|
|
196
|
+
*
|
|
197
|
+
* @param statements The statements.
|
|
198
|
+
* @param scope The constants a fold may read.
|
|
199
|
+
*
|
|
200
|
+
* @returns The statements.
|
|
201
|
+
*/
|
|
202
|
+
export function prunedStatements(statements, scope) {
|
|
203
|
+
const pruned = [];
|
|
204
|
+
scope = withoutNames(scope, declaredIn(statements));
|
|
205
|
+
for (const original of statements) {
|
|
206
|
+
const statement = original.type === 'SwitchStatement' ? switchAsIf(original) : original;
|
|
207
|
+
switch (statement.type) {
|
|
208
|
+
case 'IfStatement': {
|
|
209
|
+
const test = folded(statement.test, scope);
|
|
210
|
+
if (!test) {
|
|
211
|
+
pruned.push({
|
|
212
|
+
...statement,
|
|
213
|
+
consequent: {
|
|
214
|
+
type: 'BlockStatement',
|
|
215
|
+
body: prunedStatements(statementsOf(statement.consequent), scope),
|
|
216
|
+
directives: [],
|
|
217
|
+
},
|
|
218
|
+
alternate: statement.alternate
|
|
219
|
+
? {
|
|
220
|
+
type: 'BlockStatement',
|
|
221
|
+
body: prunedStatements(statementsOf(statement.alternate), scope),
|
|
222
|
+
directives: [],
|
|
223
|
+
}
|
|
224
|
+
: null,
|
|
225
|
+
});
|
|
226
|
+
continue;
|
|
227
|
+
}
|
|
228
|
+
// The branch taken runs in place. One that always ends is the last thing that runs.
|
|
229
|
+
const branch = test.value ? statement.consequent : statement.alternate;
|
|
230
|
+
const taken = branch
|
|
231
|
+
? prunedStatements(statementsOf(branch), scope)
|
|
232
|
+
: [];
|
|
233
|
+
if (exits(taken)) {
|
|
234
|
+
pruned.push(...taken);
|
|
235
|
+
return pruned;
|
|
236
|
+
}
|
|
237
|
+
if (taken.length) {
|
|
238
|
+
pruned.push({ type: 'BlockStatement', body: taken, directives: [] });
|
|
239
|
+
}
|
|
240
|
+
continue;
|
|
241
|
+
}
|
|
242
|
+
case 'BlockStatement': {
|
|
243
|
+
const body = prunedStatements(statement.body, scope);
|
|
244
|
+
pruned.push({ ...statement, body });
|
|
245
|
+
if (exits(body))
|
|
246
|
+
return pruned;
|
|
247
|
+
continue;
|
|
248
|
+
}
|
|
249
|
+
case 'ForStatement':
|
|
250
|
+
case 'ForOfStatement': {
|
|
251
|
+
const head = statement.type === 'ForStatement' ? statement.init : statement.left;
|
|
252
|
+
pruned.push({
|
|
253
|
+
...statement,
|
|
254
|
+
body: {
|
|
255
|
+
type: 'BlockStatement',
|
|
256
|
+
body: prunedStatements(statementsOf(statement.body), withoutNames(scope, declaredIn([head]))),
|
|
257
|
+
directives: [],
|
|
258
|
+
},
|
|
259
|
+
});
|
|
260
|
+
continue;
|
|
261
|
+
}
|
|
262
|
+
case 'ReturnStatement':
|
|
263
|
+
pruned.push(statement.argument
|
|
264
|
+
? { ...statement, argument: prunedValue(statement.argument, scope) }
|
|
265
|
+
: statement);
|
|
266
|
+
return pruned;
|
|
267
|
+
default:
|
|
268
|
+
pruned.push(statement);
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
return pruned;
|
|
272
|
+
}
|
|
@@ -0,0 +1,112 @@
|
|
|
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
|
+
* @file compiler/scroll-to.mts
|
|
18
|
+
* @brief Lowers `scrollTo(ref.current, {x, y})`: a scroll of the ScrollView the ref is on, and its position after.
|
|
19
|
+
*/
|
|
20
|
+
import { aotError } from './diagnostics.mjs';
|
|
21
|
+
import { lowerExpr } from './expressions.mjs';
|
|
22
|
+
/*----------------------------------------------------------------------------------------------------------------------
|
|
23
|
+
- Constants
|
|
24
|
+
---------------------------------------------------------------------------------------------------------------------*/
|
|
25
|
+
/** The fields of the position scrollTo returns, in the order the runtime gives them. */
|
|
26
|
+
export const SCROLL_POSITION_FIELDS = ['x', 'y', 'maxX', 'maxY'];
|
|
27
|
+
const HINT = 'call it as scrollTo(list.current, {y: 120}), where list is a useRef() on a <ScrollView>; leave an axis out ' +
|
|
28
|
+
'to keep it, or the offset out to only read the position.';
|
|
29
|
+
/*----------------------------------------------------------------------------------------------------------------------
|
|
30
|
+
- Implementation
|
|
31
|
+
---------------------------------------------------------------------------------------------------------------------*/
|
|
32
|
+
/**
|
|
33
|
+
* Whether an expression is a `scrollTo(…)` call.
|
|
34
|
+
*
|
|
35
|
+
* @param node The expression.
|
|
36
|
+
*
|
|
37
|
+
* @returns True when it is.
|
|
38
|
+
*/
|
|
39
|
+
export function isScrollToCall(node) {
|
|
40
|
+
return (node?.type === 'CallExpression' &&
|
|
41
|
+
node.callee.type === 'Identifier' &&
|
|
42
|
+
node.callee.name === 'scrollTo');
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* One axis of the offset, or null when the offset leaves it out.
|
|
46
|
+
*
|
|
47
|
+
* @param offset The offset's object literal.
|
|
48
|
+
* @param axis The axis.
|
|
49
|
+
* @param environment The expression environment.
|
|
50
|
+
*
|
|
51
|
+
* @returns The axis's value.
|
|
52
|
+
*/
|
|
53
|
+
function axisOf(offset, axis, environment) {
|
|
54
|
+
// The last of a key written twice is the one JS keeps.
|
|
55
|
+
const property = offset.properties.findLast(candidate => candidate.type === 'ObjectProperty' &&
|
|
56
|
+
candidate.key.type === 'Identifier' &&
|
|
57
|
+
candidate.key.name === axis);
|
|
58
|
+
return property
|
|
59
|
+
? lowerExpr(property.value, environment)
|
|
60
|
+
: null;
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Lowers a `scrollTo(ref.current, {x, y}?)` call.
|
|
64
|
+
*
|
|
65
|
+
* @param call The call.
|
|
66
|
+
* @param environment The expression environment.
|
|
67
|
+
* @param results The locals its position is kept in, when the code reads it.
|
|
68
|
+
*
|
|
69
|
+
* @returns The statement.
|
|
70
|
+
*/
|
|
71
|
+
export function lowerScrollTo(call, environment, results) {
|
|
72
|
+
const [handle, offset, ...rest] = call.arguments;
|
|
73
|
+
const ref = handle?.type === 'MemberExpression' &&
|
|
74
|
+
!handle.computed &&
|
|
75
|
+
handle.object.type === 'Identifier' &&
|
|
76
|
+
handle.property.type === 'Identifier' &&
|
|
77
|
+
handle.property.name === 'current'
|
|
78
|
+
? environment.refs?.get(handle.object.name)
|
|
79
|
+
: null;
|
|
80
|
+
if (ref?.kind !== 'node') {
|
|
81
|
+
throw aotError('AOT: scrollTo() takes the `.current` of a node ref first', HINT);
|
|
82
|
+
}
|
|
83
|
+
ref.used = true;
|
|
84
|
+
const isOffset = offset?.type === 'ObjectExpression' &&
|
|
85
|
+
offset.properties.every(property => property.type === 'ObjectProperty' &&
|
|
86
|
+
!property.computed &&
|
|
87
|
+
property.key.type === 'Identifier' &&
|
|
88
|
+
(property.key.name === 'x' || property.key.name === 'y'));
|
|
89
|
+
if (rest.length || (offset && !isOffset)) {
|
|
90
|
+
throw aotError('AOT: scrollTo() takes its offset as an object literal of x and y', HINT);
|
|
91
|
+
}
|
|
92
|
+
return {
|
|
93
|
+
kind: 'scrollTo',
|
|
94
|
+
node: ref.slotName,
|
|
95
|
+
x: offset ? axisOf(offset, 'x', environment) : null,
|
|
96
|
+
y: offset ? axisOf(offset, 'y', environment) : null,
|
|
97
|
+
results,
|
|
98
|
+
};
|
|
99
|
+
}
|
|
100
|
+
/**
|
|
101
|
+
* Refuses the position scrollTo returns where it can't be kept: anywhere but a const, or a field read in one.
|
|
102
|
+
*
|
|
103
|
+
* @param node The expression.
|
|
104
|
+
*
|
|
105
|
+
* @returns The error, or null when the expression is no scrollTo call.
|
|
106
|
+
*/
|
|
107
|
+
export function unkeptScrollError(node) {
|
|
108
|
+
return isScrollToCall(node)
|
|
109
|
+
? aotError('AOT: scrollTo() returns a position, which only a const keeps', 'keep it first, then read its fields: const position = scrollTo(list.current); or ' +
|
|
110
|
+
'const {y, maxY} = scrollTo(list.current);')
|
|
111
|
+
: null;
|
|
112
|
+
}
|