@tsrx/vue 0.3.5 → 0.4.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.
- package/package.json +8 -7
- package/src/index.js +3 -1
- package/src/interop.js +137 -0
- package/src/template-yield.js +60 -0
- package/src/transform.js +1952 -167
package/src/transform.js
CHANGED
|
@@ -4,25 +4,55 @@
|
|
|
4
4
|
|
|
5
5
|
import { walk } from 'zimmerframe';
|
|
6
6
|
import is_reference from 'is-reference';
|
|
7
|
+
import { find_deferred_template_yields } from './template-yield.js';
|
|
7
8
|
import {
|
|
8
9
|
builders as b,
|
|
9
10
|
addJsxSetupDeclaration,
|
|
10
11
|
clone_ast_node,
|
|
11
12
|
clone_identifier,
|
|
12
|
-
contains_component_jsx,
|
|
13
13
|
createHookSafeHelper,
|
|
14
14
|
create_generated_identifier,
|
|
15
15
|
createJsxTransform,
|
|
16
|
-
|
|
16
|
+
destructure_for_of_item,
|
|
17
|
+
ERROR_BOUNDARY_INTERNAL_NAME,
|
|
17
18
|
error,
|
|
19
|
+
FRAGMENT_INTERNAL_NAME,
|
|
20
|
+
forOfPatternReadsIndex as for_of_pattern_reads_index,
|
|
18
21
|
has_location,
|
|
22
|
+
IDENTIFIER_OBFUSCATION_PREFIX,
|
|
23
|
+
insertAfterDirectives,
|
|
19
24
|
is_component_like_element,
|
|
25
|
+
JOIN_SCOPE_CLASS_INTERNAL_NAME,
|
|
20
26
|
MERGE_REFS_INTERNAL_NAME,
|
|
21
27
|
NORMALIZE_SPREAD_PROPS_FOR_REF_ATTR_INTERNAL_NAME,
|
|
22
28
|
NORMALIZE_SPREAD_PROPS_INTERNAL_NAME,
|
|
23
29
|
setLocation,
|
|
30
|
+
SUSPENSE_INTERNAL_NAME,
|
|
31
|
+
TSRX_ERRORS,
|
|
24
32
|
} from '@tsrx/core';
|
|
25
33
|
|
|
34
|
+
/** The local name of Vue's `computed` in a keyed slot that destructures its item. */
|
|
35
|
+
const VUE_COMPUTED_INTERNAL_NAME = IDENTIFIER_OBFUSCATION_PREFIX + 'vapor_computed';
|
|
36
|
+
/** The `computed` such a slot destructures its item in, numbered per slot. */
|
|
37
|
+
const VAPOR_BINDINGS_NAME = IDENTIFIER_OBFUSCATION_PREFIX + 'vapor_bindings';
|
|
38
|
+
/**
|
|
39
|
+
* How many slots each compile has given a bindings `computed`, so each slot's
|
|
40
|
+
* has its own name and a nested slot's can't capture the outer slot's names.
|
|
41
|
+
* @type {WeakMap<TransformContext, number>}
|
|
42
|
+
*/
|
|
43
|
+
const vapor_bindings_counts = new WeakMap();
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Local name for the `VaporFor` the transform imports for `@for`, so an
|
|
47
|
+
* authored binding named `VaporFor` neither collides with the import nor
|
|
48
|
+
* replaces the loop. `defineVaporComponent` keeps its public name:
|
|
49
|
+
* vue-jsx-vapor finds components by that callee name for its macros, HMR,
|
|
50
|
+
* SSR and interop output.
|
|
51
|
+
*/
|
|
52
|
+
const VAPOR_FOR_INTERNAL_NAME = '__VaporFor';
|
|
53
|
+
|
|
54
|
+
const DEFINE_VAPOR_COMPONENT = 'defineVaporComponent';
|
|
55
|
+
|
|
26
56
|
/**
|
|
27
57
|
* Minimal Vue platform descriptor consumed by `createJsxTransform`.
|
|
28
58
|
*
|
|
@@ -53,8 +83,17 @@ const vue_platform = {
|
|
|
53
83
|
},
|
|
54
84
|
jsx: {
|
|
55
85
|
rewriteClassAttr: false,
|
|
86
|
+
// `class` also takes an object or a list.
|
|
87
|
+
classValue: 'list',
|
|
88
|
+
// A static `className` renders as a `classname` attribute.
|
|
89
|
+
rewriteClassNameAttr: true,
|
|
56
90
|
multiRefStrategy: 'merge-refs',
|
|
57
91
|
hostSpreadRefStrategy: 'explicit-ref-attr',
|
|
92
|
+
// vue-jsx-vapor evaluates a spread inside the element's render effect and
|
|
93
|
+
// sets its template ref after the props, so a bag bound in place is read
|
|
94
|
+
// again when its expression changes and is assigned before its `ref`.
|
|
95
|
+
hostSpreadRefBinding: 'in-place',
|
|
96
|
+
refAfterProps: true,
|
|
58
97
|
// vue-jsx-vapor escapes a string child into its HTML template, where a
|
|
59
98
|
// `<script>` element's text isn't decoded, and writes a string
|
|
60
99
|
// `innerHTML` there as an attribute. `v-html` sets the body as written
|
|
@@ -69,13 +108,17 @@ const vue_platform = {
|
|
|
69
108
|
initialState: () => ({
|
|
70
109
|
needs_define_vapor_component: false,
|
|
71
110
|
needs_vapor_for: false,
|
|
111
|
+
needs_use_slots: false,
|
|
72
112
|
}),
|
|
73
113
|
wrapHelperComponent(helper_fn, helper_id, ctx, source_node) {
|
|
74
114
|
ctx.needs_define_vapor_component = true;
|
|
75
115
|
return wrap_helper_component(helper_fn, helper_id, source_node);
|
|
76
116
|
},
|
|
77
|
-
canHoistStaticNode(
|
|
78
|
-
|
|
117
|
+
canHoistStaticNode() {
|
|
118
|
+
// Vapor JSX evaluates to a DOM node, so a module-level static would be
|
|
119
|
+
// one node that every use moves into place. vue-jsx-vapor already
|
|
120
|
+
// hoists the parsed template and clones it per evaluation.
|
|
121
|
+
return false;
|
|
79
122
|
},
|
|
80
123
|
preprocessElementAttributes(attrs, ctx) {
|
|
81
124
|
return preprocess_ref_attributes(attrs, ctx);
|
|
@@ -88,8 +131,8 @@ const vue_platform = {
|
|
|
88
131
|
},
|
|
89
132
|
renderForOf: (node, loop_params, body_statements, ctx) =>
|
|
90
133
|
render_for_of_as_vapor_for(node, loop_params, body_statements, ctx),
|
|
91
|
-
createPendingBoundary(try_content, fallback_content) {
|
|
92
|
-
return create_vapor_pending_boundary(try_content, fallback_content);
|
|
134
|
+
createPendingBoundary(try_content, fallback_content, ctx) {
|
|
135
|
+
return create_vapor_pending_boundary(try_content, fallback_content, ctx);
|
|
93
136
|
},
|
|
94
137
|
createErrorFallbackComponent(catch_body_nodes, catch_params, ctx, node) {
|
|
95
138
|
// Only the runtime `@pending` boundary renders the component (see
|
|
@@ -113,16 +156,11 @@ const vue_platform = {
|
|
|
113
156
|
const fallback_component = info?.fallbackComponent ?? null;
|
|
114
157
|
const default_slot = ctx.typeOnly
|
|
115
158
|
? b.arrow([], jsx_child_to_expression(raw_try_content))
|
|
116
|
-
:
|
|
117
|
-
raw_try_content,
|
|
118
|
-
fallback_fn,
|
|
119
|
-
fallback_component,
|
|
120
|
-
node.block,
|
|
121
|
-
node,
|
|
122
|
-
);
|
|
159
|
+
: create_error_boundary_slot(raw_try_content, fallback_fn, node);
|
|
123
160
|
const suspense = create_vapor_pending_boundary_from_default_slot(
|
|
124
161
|
default_slot,
|
|
125
162
|
fallback_content,
|
|
163
|
+
ctx,
|
|
126
164
|
);
|
|
127
165
|
const boundary = create_vapor_error_boundary(suspense, fallback_fn);
|
|
128
166
|
for (const statement of fallback_component?.setup_statements ?? []) {
|
|
@@ -136,14 +174,14 @@ const vue_platform = {
|
|
|
136
174
|
transformElementChildren(node, walked_children, raw_children, attributes, ctx) {
|
|
137
175
|
return rewrite_host_text_children(node, walked_children, raw_children, attributes);
|
|
138
176
|
},
|
|
177
|
+
findDeferredTemplateYields: find_deferred_template_yields,
|
|
139
178
|
validateComponentAwait(await_expression, _component, ctx) {
|
|
140
179
|
error(
|
|
141
|
-
|
|
180
|
+
TSRX_ERRORS.TARGET_COMPONENT_AWAIT_UNSUPPORTED(ctx.platform.name),
|
|
142
181
|
ctx?.filename ?? null,
|
|
143
182
|
await_expression,
|
|
144
183
|
ctx?.errors,
|
|
145
184
|
ctx?.comments,
|
|
146
|
-
DIAGNOSTIC_CODES.TARGET_AWAIT_UNSUPPORTED,
|
|
147
185
|
);
|
|
148
186
|
},
|
|
149
187
|
injectImports(program, ctx) {
|
|
@@ -158,23 +196,29 @@ export const transform = createJsxTransform(vue_platform);
|
|
|
158
196
|
/**
|
|
159
197
|
* @param {ESTreeJSX.JSXRenderNode} try_content
|
|
160
198
|
* @param {ESTreeJSX.JSXRenderNode} fallback_content
|
|
199
|
+
* @param {TransformContext} ctx
|
|
161
200
|
* @returns {AST.TSRXJSXElement}
|
|
162
201
|
*/
|
|
163
|
-
function create_vapor_pending_boundary(try_content, fallback_content) {
|
|
202
|
+
function create_vapor_pending_boundary(try_content, fallback_content, ctx) {
|
|
164
203
|
return create_vapor_pending_boundary_from_default_slot(
|
|
165
204
|
b.arrow([], jsx_child_to_expression(try_content)),
|
|
166
205
|
fallback_content,
|
|
206
|
+
ctx,
|
|
167
207
|
);
|
|
168
208
|
}
|
|
169
209
|
|
|
170
210
|
/**
|
|
171
211
|
* @param {AST.ArrowFunctionExpression} default_slot
|
|
172
212
|
* @param {ESTreeJSX.JSXRenderNode} fallback_content
|
|
213
|
+
* @param {TransformContext} ctx
|
|
173
214
|
* @returns {AST.TSRXJSXElement}
|
|
174
215
|
*/
|
|
175
|
-
function create_vapor_pending_boundary_from_default_slot(default_slot, fallback_content) {
|
|
216
|
+
function create_vapor_pending_boundary_from_default_slot(default_slot, fallback_content, ctx) {
|
|
176
217
|
const fallback_expression = jsx_child_to_expression(fallback_content);
|
|
177
|
-
const slots_properties = [b.init('
|
|
218
|
+
const slots_properties = [b.init('default', default_slot)];
|
|
219
|
+
// Vue's stable-slots flag isn't a slot, so `Suspense`'s JSX slot types reject
|
|
220
|
+
// it in the type-only output.
|
|
221
|
+
if (!ctx.typeOnly) slots_properties.unshift(b.init('_', b.literal(1)));
|
|
178
222
|
|
|
179
223
|
if (fallback_expression.type !== 'Literal' || fallback_expression.value !== null) {
|
|
180
224
|
slots_properties.push(b.init('fallback', b.arrow([], fallback_expression)));
|
|
@@ -184,7 +228,7 @@ function create_vapor_pending_boundary_from_default_slot(default_slot, fallback_
|
|
|
184
228
|
|
|
185
229
|
const boundary = b.jsx_element_fresh(
|
|
186
230
|
b.jsx_opening_element(
|
|
187
|
-
b.jsx_id(
|
|
231
|
+
b.jsx_id(SUSPENSE_INTERNAL_NAME),
|
|
188
232
|
[b.jsx_attribute(b.jsx_id('v-slots'), to_jsx_expression_container(slots))],
|
|
189
233
|
true,
|
|
190
234
|
),
|
|
@@ -210,78 +254,23 @@ function create_module_scoped_error_fallback_component(catch_body_nodes, catch_p
|
|
|
210
254
|
}
|
|
211
255
|
|
|
212
256
|
/**
|
|
213
|
-
*
|
|
214
|
-
* Suspense
|
|
215
|
-
*
|
|
257
|
+
* Render the try body in the Suspense default slot through its own error
|
|
258
|
+
* boundary. Suspense mounts its default content after the outer boundary has
|
|
259
|
+
* rendered, so an error thrown there, while setting up or from a rejected
|
|
260
|
+
* async child, has to be caught inside the slot. A boundary there, unlike a
|
|
261
|
+
* plain `try`, also gives `@catch` a `reset` that renders the body again.
|
|
216
262
|
*
|
|
217
263
|
* @param {ESTreeJSX.JSXRenderNode} content
|
|
218
264
|
* @param {AST.ArrowFunctionExpression} fallback_fn
|
|
219
|
-
* @param {JsxHelperComponent | null} fallback_component
|
|
220
|
-
* @param {AST.BlockStatement} source_block
|
|
221
265
|
* @param {AST.TryStatement} source_try
|
|
222
266
|
* @returns {AST.ArrowFunctionExpression}
|
|
223
267
|
*/
|
|
224
|
-
function
|
|
225
|
-
content,
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
source_try,
|
|
230
|
-
) {
|
|
231
|
-
const error_id = create_generated_identifier('_error');
|
|
232
|
-
const content_expression = jsx_child_to_expression(content);
|
|
233
|
-
const fallback_expression = fallback_component
|
|
234
|
-
? create_fallback_component_element(fallback_component, fallback_fn, [
|
|
235
|
-
error_id,
|
|
236
|
-
b.arrow([], b.block([])),
|
|
237
|
-
])
|
|
238
|
-
: b.call(b.parenthesized(fallback_fn), clone_identifier(error_id), b.arrow([], b.block([])));
|
|
239
|
-
const try_block = setLocation(
|
|
240
|
-
b.block([b.return(content_expression)]),
|
|
241
|
-
has_location(source_block) ? source_block : undefined,
|
|
242
|
-
true,
|
|
243
|
-
);
|
|
244
|
-
const try_statement = setLocation(
|
|
245
|
-
b.try(
|
|
246
|
-
try_block,
|
|
247
|
-
b.catch_clause(error_id, null, b.block([b.return(fallback_expression)])),
|
|
248
|
-
null,
|
|
249
|
-
null,
|
|
250
|
-
),
|
|
251
|
-
has_location(source_try) ? source_try : undefined,
|
|
252
|
-
true,
|
|
268
|
+
function create_error_boundary_slot(content, fallback_fn, source_try) {
|
|
269
|
+
const boundary = create_vapor_error_boundary(content, clone_ast_node(fallback_fn, false));
|
|
270
|
+
return b.arrow(
|
|
271
|
+
[],
|
|
272
|
+
setLocation(boundary, has_location(source_try) ? source_try : undefined, true),
|
|
253
273
|
);
|
|
254
|
-
return b.arrow([], b.block([try_statement]));
|
|
255
|
-
}
|
|
256
|
-
|
|
257
|
-
/**
|
|
258
|
-
* @param {JsxHelperComponent} fallback_component
|
|
259
|
-
* @param {AST.ArrowFunctionExpression} fallback_fn
|
|
260
|
-
* @param {AST.Expression[]} [replacement_args]
|
|
261
|
-
* @returns {AST.TSRXJSXElement}
|
|
262
|
-
*/
|
|
263
|
-
function create_fallback_component_element(fallback_component, fallback_fn, replacement_args = []) {
|
|
264
|
-
const element = clone_ast_node(fallback_component.component_element, false);
|
|
265
|
-
/** @type {Map<string, AST.Expression>} */
|
|
266
|
-
const replacements = new Map();
|
|
267
|
-
for (let i = 0; i < fallback_fn.params.length && i < replacement_args.length; i += 1) {
|
|
268
|
-
const param = fallback_fn.params[i];
|
|
269
|
-
if (param?.type === 'Identifier') {
|
|
270
|
-
replacements.set(param.name, replacement_args[i]);
|
|
271
|
-
}
|
|
272
|
-
}
|
|
273
|
-
|
|
274
|
-
for (const attr of element.openingElement?.attributes ?? []) {
|
|
275
|
-
if (attr.type !== 'JSXAttribute' || attr.name.type !== 'JSXIdentifier') continue;
|
|
276
|
-
const attr_name = attr.name.name;
|
|
277
|
-
if (!attr_name || !replacements.has(attr_name)) continue;
|
|
278
|
-
attr.value = to_jsx_expression_container(
|
|
279
|
-
/** @type {AST.Expression} */ (replacements.get(attr_name)),
|
|
280
|
-
attr.value ?? attr,
|
|
281
|
-
);
|
|
282
|
-
}
|
|
283
|
-
|
|
284
|
-
return element;
|
|
285
274
|
}
|
|
286
275
|
|
|
287
276
|
/**
|
|
@@ -314,7 +303,7 @@ function jsx_child_to_expression(child) {
|
|
|
314
303
|
function create_vapor_error_boundary(content, fallback_fn) {
|
|
315
304
|
return b.jsx_element_fresh(
|
|
316
305
|
b.jsx_opening_element(
|
|
317
|
-
b.jsx_id(
|
|
306
|
+
b.jsx_id(ERROR_BOUNDARY_INTERNAL_NAME),
|
|
318
307
|
[
|
|
319
308
|
b.jsx_attribute(b.jsx_id('fallback'), to_jsx_expression_container(fallback_fn)),
|
|
320
309
|
b.jsx_attribute(
|
|
@@ -378,74 +367,1353 @@ function wrap_helper_component(helper_fn, helper_id, source_node) {
|
|
|
378
367
|
*/
|
|
379
368
|
function wrap_native_function_components(program, ctx) {
|
|
380
369
|
const wrapped = walk(program, ctx, {
|
|
381
|
-
FunctionDeclaration
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
370
|
+
FunctionDeclaration: visit_function_component,
|
|
371
|
+
FunctionExpression: visit_function_component,
|
|
372
|
+
ArrowFunctionExpression: visit_function_component,
|
|
373
|
+
Program: visit_statement_list_owner,
|
|
374
|
+
BlockStatement: visit_statement_list_owner,
|
|
375
|
+
StaticBlock: visit_statement_list_owner,
|
|
376
|
+
});
|
|
377
|
+
program.body = /** @type {AST.Program} */ (wrapped).body;
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
/**
|
|
381
|
+
* @param {AST.Program | AST.BlockStatement | AST.StaticBlock} node
|
|
382
|
+
* @param {import('zimmerframe').Context<AST.Node, TransformContext>} context
|
|
383
|
+
* @returns {AST.Node | void}
|
|
384
|
+
*/
|
|
385
|
+
function visit_statement_list_owner(node, { next, state }) {
|
|
386
|
+
const walked = /** @type {typeof node} */ (next() ?? node);
|
|
387
|
+
const statements = split_default_exported_components(/** @type {AST.Node[]} */ (walked.body));
|
|
388
|
+
const hoisted = hoist_component_bindings(statements, state);
|
|
389
|
+
if (hoisted === walked.body) {
|
|
390
|
+
return walked === node ? undefined : walked;
|
|
391
|
+
}
|
|
392
|
+
return /** @type {AST.Node} */ ({ ...walked, body: hoisted });
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
/**
|
|
396
|
+
* `export default function Card` binds `Card` in the module too, so a wrapped
|
|
397
|
+
* one becomes `const Card = defineVaporComponent(…); export default Card;`.
|
|
398
|
+
*
|
|
399
|
+
* @param {AST.Node[]} statements
|
|
400
|
+
* @returns {AST.Node[]}
|
|
401
|
+
*/
|
|
402
|
+
function split_default_exported_components(statements) {
|
|
403
|
+
if (!statements.some((statement) => get_default_exported_component(statement))) {
|
|
404
|
+
return statements;
|
|
405
|
+
}
|
|
406
|
+
return statements.flatMap((statement) => {
|
|
407
|
+
const declaration = get_default_exported_component(statement);
|
|
408
|
+
if (!declaration) return [statement];
|
|
409
|
+
const name = /** @type {string} */ (declaration.metadata.vue_component_binding);
|
|
410
|
+
return [declaration, b.export_default(b.id(name))];
|
|
411
|
+
});
|
|
412
|
+
}
|
|
413
|
+
|
|
414
|
+
/**
|
|
415
|
+
* @param {AST.Node} statement
|
|
416
|
+
* @returns {AST.VariableDeclaration | null}
|
|
417
|
+
*/
|
|
418
|
+
function get_default_exported_component(statement) {
|
|
419
|
+
const declaration = /** @type {AST.Node | null} */ (
|
|
420
|
+
statement.type === 'ExportDefaultDeclaration' ? statement.declaration : null
|
|
421
|
+
);
|
|
422
|
+
return declaration?.type === 'VariableDeclaration' && declaration.metadata?.vue_component_binding
|
|
423
|
+
? declaration
|
|
424
|
+
: null;
|
|
425
|
+
}
|
|
426
|
+
|
|
427
|
+
/**
|
|
428
|
+
* Wrapping turns a component's function declaration into a `const`, which,
|
|
429
|
+
* unlike the declaration, is not hoisted. When a statement above the
|
|
430
|
+
* component reads it while its scope runs, such as
|
|
431
|
+
* `const registry = { inner: Inner }` above `function Inner`, move the
|
|
432
|
+
* component above that statement, but only when that changes nothing else:
|
|
433
|
+
* vue-jsx-vapor evaluates a destructured prop default that names a binding
|
|
434
|
+
* (`{ label = PREFIX }`) when the component is defined, so a default that
|
|
435
|
+
* reads a binding declared, assigned or mutated in the statements the
|
|
436
|
+
* component would move past is reported instead (TSRX2028).
|
|
437
|
+
*
|
|
438
|
+
* Reads that run later or not at all do not count: inside a function, a class
|
|
439
|
+
* instance field or method, a type, an `export { … }` specifier, a branch a
|
|
440
|
+
* constant condition skips, or a scope with its own binding of the name.
|
|
441
|
+
* Calls are not followed, so these still go wrong: a call or IIFE that runs a
|
|
442
|
+
* function reading the component during module evaluation hits the constant
|
|
443
|
+
* before it is initialized, and a call in the skipped statements to a
|
|
444
|
+
* function that assigns a binding a default reads (`bump()` setting `size`)
|
|
445
|
+
* runs after the moved component has captured the old value. So do a read
|
|
446
|
+
* from another `case` of the `switch` that declares the component and a
|
|
447
|
+
* circular import that reads it before this module finishes.
|
|
448
|
+
*
|
|
449
|
+
* @param {AST.Node[]} statements
|
|
450
|
+
* @param {TransformContext} ctx
|
|
451
|
+
* @returns {AST.Node[]}
|
|
452
|
+
*/
|
|
453
|
+
function hoist_component_bindings(statements, ctx) {
|
|
454
|
+
let result = statements;
|
|
455
|
+
/** @type {Map<string, AST.Literal['value']> | null} */
|
|
456
|
+
let constants = null;
|
|
457
|
+
for (const statement of statements) {
|
|
458
|
+
const declaration = get_component_declaration(statement);
|
|
459
|
+
if (!declaration) continue;
|
|
460
|
+
const name = /** @type {string} */ (declaration.metadata.vue_component_binding);
|
|
461
|
+
|
|
462
|
+
const index = result.indexOf(statement);
|
|
463
|
+
constants ??= collect_literal_constants(statements);
|
|
464
|
+
let first_read = -1;
|
|
465
|
+
/** @type {AST.Node | null} */
|
|
466
|
+
let read = null;
|
|
467
|
+
for (let i = 0; i < index && !read; i += 1) {
|
|
468
|
+
read = find_eager_read(result[i], name, /** @type {Map<string, any>} */ (constants));
|
|
469
|
+
if (read) first_read = i;
|
|
470
|
+
}
|
|
471
|
+
if (!read) continue;
|
|
472
|
+
|
|
473
|
+
const passed = result.slice(first_read, index);
|
|
474
|
+
if (initializer_reads_declared(declaration, passed)) {
|
|
475
|
+
error(
|
|
476
|
+
TSRX_ERRORS.TARGET_COMPONENT_READ_BEFORE_DECLARATION(ctx.platform.name, name),
|
|
477
|
+
ctx.filename,
|
|
478
|
+
read,
|
|
479
|
+
ctx.errors,
|
|
480
|
+
ctx.comments,
|
|
385
481
|
);
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
482
|
+
continue;
|
|
483
|
+
}
|
|
484
|
+
|
|
485
|
+
result = [...result.slice(0, first_read), statement, ...passed, ...result.slice(index + 1)];
|
|
486
|
+
}
|
|
487
|
+
return result;
|
|
488
|
+
}
|
|
489
|
+
|
|
490
|
+
/**
|
|
491
|
+
* @param {AST.Node} statement
|
|
492
|
+
* @returns {(AST.VariableDeclaration & { metadata: { vue_component_binding: string } }) | null}
|
|
493
|
+
*/
|
|
494
|
+
function get_component_declaration(statement) {
|
|
495
|
+
const declaration =
|
|
496
|
+
statement.type === 'ExportNamedDeclaration' ? statement.declaration : statement;
|
|
497
|
+
return declaration?.type === 'VariableDeclaration' && declaration.metadata?.vue_component_binding
|
|
498
|
+
? /** @type {any} */ (declaration)
|
|
499
|
+
: null;
|
|
500
|
+
}
|
|
501
|
+
|
|
502
|
+
/**
|
|
503
|
+
* `const NAME = <literal>` declarations in a statement list, for skipping the
|
|
504
|
+
* branches a constant condition never takes (`const DEBUG = false; if (DEBUG)`).
|
|
505
|
+
*
|
|
506
|
+
* @param {AST.Node[]} statements
|
|
507
|
+
* @returns {Map<string, AST.Literal['value']>}
|
|
508
|
+
*/
|
|
509
|
+
function collect_literal_constants(statements) {
|
|
510
|
+
/** @type {Map<string, AST.Literal['value']>} */
|
|
511
|
+
const constants = new Map();
|
|
512
|
+
for (const statement of statements) {
|
|
513
|
+
const declaration =
|
|
514
|
+
statement.type === 'ExportNamedDeclaration' ? statement.declaration : statement;
|
|
515
|
+
if (declaration?.type !== 'VariableDeclaration' || declaration.kind !== 'const') continue;
|
|
516
|
+
for (const declarator of declaration.declarations) {
|
|
517
|
+
if (declarator.id.type === 'Identifier' && declarator.init?.type === 'Literal') {
|
|
518
|
+
constants.set(declarator.id.name, declarator.init.value);
|
|
519
|
+
}
|
|
520
|
+
}
|
|
521
|
+
}
|
|
522
|
+
return constants;
|
|
523
|
+
}
|
|
524
|
+
|
|
525
|
+
/**
|
|
526
|
+
* @param {AST.Node | null | undefined} node
|
|
527
|
+
* @param {Map<string, AST.Literal['value']>} constants
|
|
528
|
+
* @returns {boolean | null} the condition's truthiness, or null when unknown
|
|
529
|
+
*/
|
|
530
|
+
function constant_truthiness(node, constants) {
|
|
531
|
+
if (node?.type === 'Literal') return !!node.value || 'regex' in node;
|
|
532
|
+
if (node?.type === 'Identifier') {
|
|
533
|
+
if (node.name === 'undefined') return false;
|
|
534
|
+
return constants.has(node.name) ? !!constants.get(node.name) : null;
|
|
535
|
+
}
|
|
536
|
+
if (node?.type === 'UnaryExpression' && node.operator === '!') {
|
|
537
|
+
const value = constant_truthiness(node.argument, constants);
|
|
538
|
+
return value === null ? null : !value;
|
|
539
|
+
}
|
|
540
|
+
return null;
|
|
541
|
+
}
|
|
542
|
+
|
|
543
|
+
/**
|
|
544
|
+
* TypeScript nodes that hold runtime code. Every other `TS*` node is a type,
|
|
545
|
+
* which is erased and never reads a binding.
|
|
546
|
+
*/
|
|
547
|
+
const RUNTIME_TS_NODE_TYPES = new Set([
|
|
548
|
+
'TSAsExpression',
|
|
549
|
+
'TSSatisfiesExpression',
|
|
550
|
+
'TSNonNullExpression',
|
|
551
|
+
'TSInstantiationExpression',
|
|
552
|
+
'TSTypeAssertion',
|
|
553
|
+
'TSEnumDeclaration',
|
|
554
|
+
'TSEnumMember',
|
|
555
|
+
'TSModuleDeclaration',
|
|
556
|
+
'TSModuleBlock',
|
|
557
|
+
'TSExportAssignment',
|
|
558
|
+
'TSParameterProperty',
|
|
559
|
+
]);
|
|
560
|
+
|
|
561
|
+
/**
|
|
562
|
+
* The first read of `name` that evaluating `root` performs: not inside a
|
|
563
|
+
* function, a class instance field or method, a type or an `export { … }`
|
|
564
|
+
* specifier, not in a branch a constant condition skips, and not in a scope
|
|
565
|
+
* with its own binding of `name`.
|
|
566
|
+
*
|
|
567
|
+
* @param {AST.Node} root
|
|
568
|
+
* @param {string} name
|
|
569
|
+
* @param {Map<string, AST.Literal['value']>} constants
|
|
570
|
+
* @returns {AST.Node | null}
|
|
571
|
+
*/
|
|
572
|
+
function find_eager_read(root, name, constants) {
|
|
573
|
+
/** @type {AST.Node | null} */
|
|
574
|
+
let found = null;
|
|
575
|
+
// The literal constants visible where the walk is: a scope that declares
|
|
576
|
+
// its own binding of a name forgets that name's value.
|
|
577
|
+
let visible = constants;
|
|
578
|
+
|
|
579
|
+
/**
|
|
580
|
+
* @param {AST.Node[]} bindings patterns or declarations a scope introduces
|
|
581
|
+
* @param {() => void} walk_scope
|
|
582
|
+
*/
|
|
583
|
+
const in_scope = (bindings, walk_scope) => {
|
|
584
|
+
const saved = visible;
|
|
585
|
+
visible = forget_declared(visible, bindings);
|
|
586
|
+
walk_scope();
|
|
587
|
+
visible = saved;
|
|
588
|
+
};
|
|
589
|
+
|
|
590
|
+
/**
|
|
591
|
+
* @param {unknown} value
|
|
592
|
+
* @param {AST.Node | undefined} parent
|
|
593
|
+
*/
|
|
594
|
+
const visit = (value, parent) => {
|
|
595
|
+
if (found || !value || typeof value !== 'object') return;
|
|
596
|
+
if (Array.isArray(value)) {
|
|
597
|
+
for (const child of value) visit(child, parent);
|
|
598
|
+
return;
|
|
599
|
+
}
|
|
600
|
+
const node = /** @type {AST.Node} */ (value);
|
|
601
|
+
if (typeof node.type !== 'string') return;
|
|
602
|
+
|
|
603
|
+
switch (node.type) {
|
|
604
|
+
case 'FunctionDeclaration':
|
|
605
|
+
case 'FunctionExpression':
|
|
606
|
+
case 'ArrowFunctionExpression':
|
|
607
|
+
case 'ExportSpecifier':
|
|
608
|
+
return;
|
|
609
|
+
case 'Identifier':
|
|
610
|
+
if (node.name === name && parent && is_reference(node, parent)) found = node;
|
|
611
|
+
return;
|
|
612
|
+
case 'JSXIdentifier':
|
|
613
|
+
if (
|
|
614
|
+
node.name === name &&
|
|
615
|
+
((parent?.type === 'JSXOpeningElement' && parent.name === node) ||
|
|
616
|
+
(parent?.type === 'JSXMemberExpression' && parent.object === node))
|
|
617
|
+
) {
|
|
618
|
+
found = node;
|
|
619
|
+
}
|
|
620
|
+
return;
|
|
621
|
+
case 'BlockStatement':
|
|
622
|
+
case 'StaticBlock':
|
|
623
|
+
if (declares_lexically(node.body, name)) return;
|
|
624
|
+
in_scope(node.body, () => visit(node.body, node));
|
|
625
|
+
return;
|
|
626
|
+
case 'SwitchStatement': {
|
|
627
|
+
visit(node.discriminant, node);
|
|
628
|
+
const declarations = node.cases.flatMap((c) => c.consequent);
|
|
629
|
+
if (!declares_lexically(declarations, name)) {
|
|
630
|
+
in_scope(declarations, () => visit(node.cases, node));
|
|
631
|
+
}
|
|
632
|
+
return;
|
|
633
|
+
}
|
|
634
|
+
case 'ForStatement':
|
|
635
|
+
case 'ForInStatement':
|
|
636
|
+
case 'ForOfStatement': {
|
|
637
|
+
const head = node.type === 'ForStatement' ? node.init : node.left;
|
|
638
|
+
if (head?.type === 'VariableDeclaration' && head.kind !== 'var') {
|
|
639
|
+
if (declares_lexically([head], name)) return;
|
|
640
|
+
in_scope([head], () => visit_children(node));
|
|
641
|
+
return;
|
|
642
|
+
}
|
|
643
|
+
break;
|
|
644
|
+
}
|
|
645
|
+
case 'CatchClause':
|
|
646
|
+
if (node.param && get_pattern_names([node.param]).has(name)) return;
|
|
647
|
+
if (node.param) {
|
|
648
|
+
in_scope([node.param], () => visit(node.body, node));
|
|
649
|
+
return;
|
|
650
|
+
}
|
|
651
|
+
break;
|
|
652
|
+
case 'IfStatement': {
|
|
653
|
+
visit(node.test, node);
|
|
654
|
+
const truthy = constant_truthiness(node.test, visible);
|
|
655
|
+
if (truthy !== false) visit(node.consequent, node);
|
|
656
|
+
if (truthy !== true) visit(node.alternate, node);
|
|
657
|
+
return;
|
|
658
|
+
}
|
|
659
|
+
case 'ConditionalExpression': {
|
|
660
|
+
visit(node.test, node);
|
|
661
|
+
const truthy = constant_truthiness(node.test, visible);
|
|
662
|
+
if (truthy !== false) visit(node.consequent, node);
|
|
663
|
+
if (truthy !== true) visit(node.alternate, node);
|
|
664
|
+
return;
|
|
665
|
+
}
|
|
666
|
+
case 'LogicalExpression': {
|
|
667
|
+
visit(node.left, node);
|
|
668
|
+
const truthy = constant_truthiness(node.left, visible);
|
|
669
|
+
if (
|
|
670
|
+
!(node.operator === '&&' && truthy === false) &&
|
|
671
|
+
!(node.operator === '||' && truthy === true)
|
|
672
|
+
) {
|
|
673
|
+
visit(node.right, node);
|
|
674
|
+
}
|
|
675
|
+
return;
|
|
676
|
+
}
|
|
677
|
+
case 'PropertyDefinition':
|
|
678
|
+
case 'MethodDefinition':
|
|
679
|
+
// An instance field runs when an instance is created, and a method
|
|
680
|
+
// when it is called; only a computed key, a decorator or a static
|
|
681
|
+
// field runs with the class.
|
|
682
|
+
if (node.computed) visit(node.key, node);
|
|
683
|
+
visit(/** @type {{ decorators?: AST.Node[] }} */ (node).decorators, node);
|
|
684
|
+
if (node.type === 'PropertyDefinition' && node.static) visit(node.value, node);
|
|
685
|
+
return;
|
|
686
|
+
}
|
|
687
|
+
|
|
688
|
+
if (node.type.startsWith('TS') && !RUNTIME_TS_NODE_TYPES.has(node.type)) return;
|
|
689
|
+
visit_children(node);
|
|
690
|
+
};
|
|
691
|
+
|
|
692
|
+
/** @param {AST.Node} node */
|
|
693
|
+
const visit_children = (node) => {
|
|
694
|
+
const record = /** @type {Record<string, unknown>} */ (/** @type {unknown} */ (node));
|
|
695
|
+
for (const key of Object.keys(record)) {
|
|
696
|
+
if (key === 'metadata' || key === 'loc' || key === 'start' || key === 'end') continue;
|
|
697
|
+
visit(record[key], node);
|
|
698
|
+
}
|
|
699
|
+
};
|
|
700
|
+
|
|
701
|
+
visit(root, undefined);
|
|
702
|
+
return found;
|
|
703
|
+
}
|
|
704
|
+
|
|
705
|
+
/**
|
|
706
|
+
* `constants` without the names `bindings` declare, which a nested scope
|
|
707
|
+
* shadows with values this pass does not know.
|
|
708
|
+
*
|
|
709
|
+
* @param {Map<string, AST.Literal['value']>} constants
|
|
710
|
+
* @param {AST.Node[]} bindings statements, declarations or patterns
|
|
711
|
+
* @returns {Map<string, AST.Literal['value']>}
|
|
712
|
+
*/
|
|
713
|
+
function forget_declared(constants, bindings) {
|
|
714
|
+
if (constants.size === 0) return constants;
|
|
715
|
+
/** @type {Set<string>} */
|
|
716
|
+
const declared = new Set();
|
|
717
|
+
for (const binding of bindings) {
|
|
718
|
+
const node =
|
|
719
|
+
binding.type === 'ExportNamedDeclaration' && binding.declaration
|
|
720
|
+
? binding.declaration
|
|
721
|
+
: binding;
|
|
722
|
+
if (node.type === 'VariableDeclaration') {
|
|
723
|
+
for (const declarator of node.declarations) collect_pattern_names(declarator.id, declared);
|
|
724
|
+
} else if (
|
|
725
|
+
(node.type === 'FunctionDeclaration' || node.type === 'ClassDeclaration') &&
|
|
726
|
+
node.id
|
|
727
|
+
) {
|
|
728
|
+
declared.add(node.id.name);
|
|
729
|
+
} else {
|
|
730
|
+
collect_pattern_names(node, declared);
|
|
731
|
+
}
|
|
732
|
+
}
|
|
733
|
+
if (![...declared].some((name) => constants.has(name))) return constants;
|
|
734
|
+
const visible = new Map(constants);
|
|
735
|
+
for (const name of declared) visible.delete(name);
|
|
736
|
+
return visible;
|
|
737
|
+
}
|
|
738
|
+
|
|
739
|
+
/**
|
|
740
|
+
* Whether a statement list declares `name` in its own scope.
|
|
741
|
+
*
|
|
742
|
+
* @param {AST.Node[]} statements
|
|
743
|
+
* @param {string} name
|
|
744
|
+
* @returns {boolean}
|
|
745
|
+
*/
|
|
746
|
+
function declares_lexically(statements, name) {
|
|
747
|
+
for (const statement of statements) {
|
|
748
|
+
const declaration =
|
|
749
|
+
statement.type === 'ExportNamedDeclaration' && statement.declaration
|
|
750
|
+
? statement.declaration
|
|
751
|
+
: statement;
|
|
752
|
+
if (declaration.type === 'VariableDeclaration' && declaration.kind !== 'var') {
|
|
753
|
+
if (get_pattern_names(declaration.declarations.map((d) => d.id)).has(name)) return true;
|
|
754
|
+
} else if (
|
|
755
|
+
(declaration.type === 'FunctionDeclaration' || declaration.type === 'ClassDeclaration') &&
|
|
756
|
+
declaration.id?.name === name
|
|
757
|
+
) {
|
|
758
|
+
return true;
|
|
759
|
+
}
|
|
760
|
+
}
|
|
761
|
+
return false;
|
|
762
|
+
}
|
|
763
|
+
|
|
764
|
+
/**
|
|
765
|
+
* Whether defining the component reads a binding that `passed`, the
|
|
766
|
+
* statements it would move above, declares, assigns or mutates. vue-jsx-vapor evaluates a
|
|
767
|
+
* destructured prop default that names a binding when the component is
|
|
768
|
+
* defined; every name read in the props pattern's defaults is counted.
|
|
769
|
+
*
|
|
770
|
+
* @param {AST.VariableDeclaration} declaration
|
|
771
|
+
* @param {AST.Node[]} passed
|
|
772
|
+
* @returns {boolean}
|
|
773
|
+
*/
|
|
774
|
+
function initializer_reads_declared(declaration, passed) {
|
|
775
|
+
const call = /** @type {AST.CallExpression} */ (declaration.declarations[0]?.init);
|
|
776
|
+
const fn = /** @type {AST.Function | undefined} */ (call?.arguments?.[0]);
|
|
777
|
+
const reads = new Set();
|
|
778
|
+
collect_default_reads(fn?.params?.[0], undefined, reads);
|
|
779
|
+
if (reads.size === 0) return false;
|
|
780
|
+
|
|
781
|
+
/** @type {Set<string>} */
|
|
782
|
+
const declared = new Set();
|
|
783
|
+
for (const statement of passed) {
|
|
784
|
+
const node =
|
|
785
|
+
(statement.type === 'ExportNamedDeclaration' ||
|
|
786
|
+
statement.type === 'ExportDefaultDeclaration') &&
|
|
787
|
+
statement.declaration
|
|
788
|
+
? statement.declaration
|
|
789
|
+
: statement;
|
|
790
|
+
if (node.type === 'VariableDeclaration') {
|
|
791
|
+
for (const declarator of node.declarations) collect_pattern_names(declarator.id, declared);
|
|
792
|
+
} else if (
|
|
793
|
+
(node.type === 'FunctionDeclaration' || node.type === 'ClassDeclaration') &&
|
|
794
|
+
node.id
|
|
795
|
+
) {
|
|
796
|
+
declared.add(node.id.name);
|
|
797
|
+
} else if (
|
|
798
|
+
// An enum, a namespace or an `import x = …` alias compiles to a binding
|
|
799
|
+
// it sets when it runs; an ambient or type-only one has no code.
|
|
800
|
+
(node.type === 'TSEnumDeclaration' ||
|
|
801
|
+
node.type === 'TSModuleDeclaration' ||
|
|
802
|
+
(node.type === 'TSImportEqualsDeclaration' && node.importKind !== 'type')) &&
|
|
803
|
+
!('declare' in node && node.declare) &&
|
|
804
|
+
node.id.type === 'Identifier'
|
|
805
|
+
) {
|
|
806
|
+
declared.add(node.id.name);
|
|
807
|
+
}
|
|
808
|
+
collect_function_var_names(statement, declared);
|
|
809
|
+
collect_assigned_names(statement, declared);
|
|
810
|
+
}
|
|
811
|
+
for (const read of reads) {
|
|
812
|
+
if (declared.has(read)) return true;
|
|
813
|
+
}
|
|
814
|
+
return false;
|
|
815
|
+
}
|
|
816
|
+
|
|
817
|
+
/**
|
|
818
|
+
* The bindings `node` assigns, updates or mutates the properties of, at any
|
|
819
|
+
* depth: a default that reads one of them could see a different value once
|
|
820
|
+
* the component moves above the assignment.
|
|
821
|
+
*
|
|
822
|
+
* @param {unknown} value
|
|
823
|
+
* @param {Set<string>} names
|
|
824
|
+
* @returns {void}
|
|
825
|
+
*/
|
|
826
|
+
function collect_assigned_names(value, names) {
|
|
827
|
+
if (!value || typeof value !== 'object') return;
|
|
828
|
+
if (Array.isArray(value)) {
|
|
829
|
+
for (const child of value) collect_assigned_names(child, names);
|
|
830
|
+
return;
|
|
831
|
+
}
|
|
832
|
+
const node = /** @type {AST.Node} */ (value);
|
|
833
|
+
if (typeof node.type !== 'string') return;
|
|
834
|
+
if (node.type === 'AssignmentExpression') collect_assigned_target(node.left, names);
|
|
835
|
+
else if (node.type === 'UpdateExpression') collect_assigned_target(node.argument, names);
|
|
836
|
+
else if (node.type === 'UnaryExpression' && node.operator === 'delete') {
|
|
837
|
+
collect_assigned_target(node.argument, names);
|
|
838
|
+
} else if (
|
|
839
|
+
(node.type === 'ForInStatement' || node.type === 'ForOfStatement') &&
|
|
840
|
+
node.left.type !== 'VariableDeclaration'
|
|
841
|
+
) {
|
|
842
|
+
collect_assigned_target(node.left, names);
|
|
843
|
+
}
|
|
844
|
+
const record = /** @type {Record<string, unknown>} */ (/** @type {unknown} */ (node));
|
|
845
|
+
for (const key of Object.keys(record)) {
|
|
846
|
+
if (key === 'metadata' || key === 'loc' || key === 'start' || key === 'end') continue;
|
|
847
|
+
collect_assigned_names(record[key], names);
|
|
848
|
+
}
|
|
849
|
+
}
|
|
850
|
+
|
|
851
|
+
/**
|
|
852
|
+
* The bindings an assignment target writes or mutates: each name in a
|
|
853
|
+
* pattern, and the object a member target belongs to, seen through
|
|
854
|
+
* parentheses, optional chains and TypeScript wrappers such as
|
|
855
|
+
* `(cfg as any).size` and `cfg!.size`.
|
|
856
|
+
*
|
|
857
|
+
* @param {AST.Node | null | undefined} target
|
|
858
|
+
* @param {Set<string>} names
|
|
859
|
+
* @returns {void}
|
|
860
|
+
*/
|
|
861
|
+
function collect_assigned_target(target, names) {
|
|
862
|
+
if (!target) return;
|
|
863
|
+
switch (target.type) {
|
|
864
|
+
case 'Identifier':
|
|
865
|
+
names.add(target.name);
|
|
866
|
+
return;
|
|
867
|
+
case 'MemberExpression':
|
|
868
|
+
collect_assigned_target(/** @type {AST.Node} */ (target.object), names);
|
|
869
|
+
return;
|
|
870
|
+
case 'RestElement':
|
|
871
|
+
collect_assigned_target(target.argument, names);
|
|
872
|
+
return;
|
|
873
|
+
case 'AssignmentPattern':
|
|
874
|
+
collect_assigned_target(target.left, names);
|
|
875
|
+
return;
|
|
876
|
+
case 'ArrayPattern':
|
|
877
|
+
for (const element of target.elements) collect_assigned_target(element, names);
|
|
878
|
+
return;
|
|
879
|
+
case 'ObjectPattern':
|
|
880
|
+
for (const property of target.properties) {
|
|
881
|
+
collect_assigned_target(property.type === 'Property' ? property.value : property, names);
|
|
882
|
+
}
|
|
883
|
+
return;
|
|
884
|
+
}
|
|
885
|
+
if (
|
|
886
|
+
(target.type === 'ParenthesizedExpression' ||
|
|
887
|
+
target.type === 'ChainExpression' ||
|
|
888
|
+
RUNTIME_TS_NODE_TYPES.has(target.type)) &&
|
|
889
|
+
'expression' in target
|
|
890
|
+
) {
|
|
891
|
+
collect_assigned_target(/** @type {AST.Node} */ (target.expression), names);
|
|
892
|
+
}
|
|
893
|
+
}
|
|
894
|
+
|
|
895
|
+
/**
|
|
896
|
+
* The names read by the default values in a parameter pattern, outside
|
|
897
|
+
* nested functions.
|
|
898
|
+
*
|
|
899
|
+
* @param {unknown} value
|
|
900
|
+
* @param {AST.Node | undefined} parent
|
|
901
|
+
* @param {Set<string>} reads
|
|
902
|
+
* @param {boolean} [in_default]
|
|
903
|
+
* @returns {void}
|
|
904
|
+
*/
|
|
905
|
+
function collect_default_reads(value, parent, reads, in_default = false) {
|
|
906
|
+
if (!value || typeof value !== 'object') return;
|
|
907
|
+
if (Array.isArray(value)) {
|
|
908
|
+
for (const child of value) collect_default_reads(child, parent, reads, in_default);
|
|
909
|
+
return;
|
|
910
|
+
}
|
|
911
|
+
const node = /** @type {AST.Node} */ (value);
|
|
912
|
+
if (typeof node.type !== 'string') return;
|
|
913
|
+
if (
|
|
914
|
+
node.type === 'FunctionExpression' ||
|
|
915
|
+
node.type === 'ArrowFunctionExpression' ||
|
|
916
|
+
(node.type.startsWith('TS') && !RUNTIME_TS_NODE_TYPES.has(node.type))
|
|
917
|
+
) {
|
|
918
|
+
return;
|
|
919
|
+
}
|
|
920
|
+
if (node.type === 'AssignmentPattern') {
|
|
921
|
+
collect_default_reads(node.left, node, reads, in_default);
|
|
922
|
+
collect_default_reads(node.right, node, reads, true);
|
|
923
|
+
return;
|
|
924
|
+
}
|
|
925
|
+
if (node.type === 'Identifier') {
|
|
926
|
+
if (in_default && parent && is_reference(node, parent)) reads.add(node.name);
|
|
927
|
+
return;
|
|
928
|
+
}
|
|
929
|
+
const record = /** @type {Record<string, unknown>} */ (/** @type {unknown} */ (node));
|
|
930
|
+
for (const key of Object.keys(record)) {
|
|
931
|
+
if (key === 'metadata' || key === 'loc' || key === 'start' || key === 'end') continue;
|
|
932
|
+
collect_default_reads(record[key], node, reads, in_default);
|
|
933
|
+
}
|
|
934
|
+
}
|
|
935
|
+
|
|
936
|
+
/**
|
|
937
|
+
* @param {AST.Function} node
|
|
938
|
+
* @param {import('zimmerframe').Context<AST.Node, TransformContext>} context
|
|
939
|
+
* @returns {AST.Node}
|
|
940
|
+
*/
|
|
941
|
+
function visit_function_component(node, { next, path, state }) {
|
|
942
|
+
const inner = /** @type {AST.Function} */ (next() ?? node);
|
|
943
|
+
return wrap_native_function_component(inner, node, state, path) ?? inner;
|
|
944
|
+
}
|
|
945
|
+
|
|
946
|
+
/**
|
|
947
|
+
* @param {AST.Function} fn the walked function, which is a copy of `source`
|
|
948
|
+
* when the walk replaced something inside it
|
|
949
|
+
* @param {AST.Function} source the function as it appears in `path`
|
|
950
|
+
* @param {TransformContext} ctx
|
|
951
|
+
* @param {AST.Node[]} path
|
|
952
|
+
* @returns {AST.Expression | AST.VariableDeclaration | null}
|
|
953
|
+
*/
|
|
954
|
+
function wrap_native_function_component(fn, source, ctx, path) {
|
|
955
|
+
const parent = path.at(-1);
|
|
956
|
+
const name = get_function_component_name(source, parent);
|
|
957
|
+
if (!name || !/^[A-Z]/.test(name)) {
|
|
958
|
+
return null;
|
|
959
|
+
}
|
|
960
|
+
|
|
961
|
+
if (!fn.metadata?.native_tsrx_function && !returns_local_jsx_binding(fn)) {
|
|
962
|
+
return null;
|
|
963
|
+
}
|
|
964
|
+
|
|
965
|
+
ctx.needs_define_vapor_component = true;
|
|
966
|
+
validate_prop_defaults(fn, ctx);
|
|
967
|
+
validate_props_param(fn, ctx);
|
|
968
|
+
if (!ctx.typeOnly) {
|
|
969
|
+
fn = read_children_from_default_slot(fn, name, ctx);
|
|
970
|
+
}
|
|
971
|
+
|
|
972
|
+
const call = setLocation(
|
|
973
|
+
create_define_vapor_component_call(
|
|
974
|
+
function_declaration_to_expression(fn),
|
|
975
|
+
fn.metadata?.generated_helpers || [],
|
|
976
|
+
fn.metadata?.generated_statics || [],
|
|
977
|
+
),
|
|
978
|
+
has_location(fn) ? fn : undefined,
|
|
979
|
+
true,
|
|
980
|
+
);
|
|
981
|
+
|
|
982
|
+
if (fn.type !== 'FunctionDeclaration') {
|
|
983
|
+
return call;
|
|
984
|
+
}
|
|
985
|
+
|
|
986
|
+
if (!fn.id) {
|
|
987
|
+
return call;
|
|
988
|
+
}
|
|
989
|
+
|
|
990
|
+
const declaration = setLocation(
|
|
991
|
+
b.declaration('const', [b.declarator(create_generated_identifier(fn.id.name), call)]),
|
|
992
|
+
has_location(fn) ? fn : undefined,
|
|
993
|
+
true,
|
|
994
|
+
);
|
|
995
|
+
declaration.metadata = { ...declaration.metadata, vue_component_binding: fn.id.name };
|
|
996
|
+
return declaration;
|
|
997
|
+
}
|
|
998
|
+
|
|
999
|
+
/**
|
|
1000
|
+
* vue-jsx-vapor turns the defaults in a component's destructured props
|
|
1001
|
+
* (`{ size = 1 }`) into prop options, and keeps only a string, number, boolean
|
|
1002
|
+
* or `null` literal, an identifier, an object or array literal, or a function,
|
|
1003
|
+
* seen through `as` and `!`. It drops any other default, such as `cfg.size`,
|
|
1004
|
+
* `-1`, `SIZE + 1` or a template literal, so the prop is `undefined` when the
|
|
1005
|
+
* caller leaves it out. Report each one (TSRX2029).
|
|
1006
|
+
*
|
|
1007
|
+
* @param {AST.Function} fn
|
|
1008
|
+
* @param {TransformContext} ctx
|
|
1009
|
+
* @returns {void}
|
|
1010
|
+
*/
|
|
1011
|
+
function validate_prop_defaults(fn, ctx) {
|
|
1012
|
+
const param = fn.params[0];
|
|
1013
|
+
if (param?.type !== 'ObjectPattern') return;
|
|
1014
|
+
for (const property of param.properties) {
|
|
1015
|
+
if (
|
|
1016
|
+
property.type !== 'Property' ||
|
|
1017
|
+
property.computed ||
|
|
1018
|
+
property.key.type !== 'Identifier' ||
|
|
1019
|
+
// A `children` default moves out of the props pattern into its reads
|
|
1020
|
+
// (see `read_children_from_default_slot`), so the macro never sees it.
|
|
1021
|
+
property.key.name === 'children' ||
|
|
1022
|
+
property.value.type !== 'AssignmentPattern' ||
|
|
1023
|
+
is_kept_prop_default(property.value.right)
|
|
1024
|
+
) {
|
|
1025
|
+
continue;
|
|
1026
|
+
}
|
|
1027
|
+
const name = property.key.name;
|
|
1028
|
+
error(
|
|
1029
|
+
TSRX_ERRORS.TARGET_PROP_DEFAULT_UNSUPPORTED(
|
|
1030
|
+
ctx.platform.name,
|
|
1031
|
+
name,
|
|
1032
|
+
get_prop_read_example(property.value, `props.${name}`, ctx),
|
|
1033
|
+
),
|
|
1034
|
+
ctx.filename,
|
|
1035
|
+
property.value.right,
|
|
1036
|
+
ctx.errors,
|
|
1037
|
+
ctx.comments,
|
|
1038
|
+
);
|
|
1039
|
+
}
|
|
1040
|
+
}
|
|
1041
|
+
|
|
1042
|
+
/**
|
|
1043
|
+
* Check a component's props parameter the way vue-jsx-vapor's props macro
|
|
1044
|
+
* reads it (TSRX2031, and TSRX2029 for a default inside a prop):
|
|
1045
|
+
*
|
|
1046
|
+
* - It skips a props parameter with a default (`{ s } = {}`), so the
|
|
1047
|
+
* component gets no prop options and Vue passes it no props.
|
|
1048
|
+
* - It reads destructured props by name, and reads only keys that are names:
|
|
1049
|
+
* it reads `{ [k]: v }` as the prop `k`, whatever `k` holds, and leaves out
|
|
1050
|
+
* any other key (`{ 'a-b': v }`, `{ [1]: v }`), so its binding never gets
|
|
1051
|
+
* the prop. That holds at every level, and a pattern with no key it reads
|
|
1052
|
+
* also gets no prop options.
|
|
1053
|
+
* - Inside a prop, it applies a default only when the whole prop is missing.
|
|
1054
|
+
* - It doesn't keep a rest element inside a prop as the rest of that prop:
|
|
1055
|
+
* the pattern's last rest element gets the component's attrs, an array's
|
|
1056
|
+
* gets the whole array, and any other gets the rest as it was at setup.
|
|
1057
|
+
*
|
|
1058
|
+
* @param {AST.Function} fn
|
|
1059
|
+
* @param {TransformContext} ctx
|
|
1060
|
+
* @returns {void}
|
|
1061
|
+
*/
|
|
1062
|
+
function validate_props_param(fn, ctx) {
|
|
1063
|
+
const param = fn.params[0];
|
|
1064
|
+
if (param?.type === 'AssignmentPattern') {
|
|
1065
|
+
error(
|
|
1066
|
+
TSRX_ERRORS.TARGET_PROPS_PARAM_DEFAULT(ctx.platform.name, get_inline_text(param.right, ctx)),
|
|
1067
|
+
ctx.filename,
|
|
1068
|
+
param.right,
|
|
1069
|
+
ctx.errors,
|
|
1070
|
+
ctx.comments,
|
|
1071
|
+
);
|
|
1072
|
+
return;
|
|
1073
|
+
}
|
|
1074
|
+
if (param?.type === 'ObjectPattern') validate_prop_keys(param, 'props', false, ctx);
|
|
1075
|
+
}
|
|
1076
|
+
|
|
1077
|
+
/**
|
|
1078
|
+
* @param {AST.RestElement} rest a rest element inside a prop
|
|
1079
|
+
* @param {string} object how the body reads the value it destructures
|
|
1080
|
+
* @param {TransformContext} ctx
|
|
1081
|
+
* @returns {void}
|
|
1082
|
+
*/
|
|
1083
|
+
function report_prop_rest(rest, object, ctx) {
|
|
1084
|
+
error(
|
|
1085
|
+
TSRX_ERRORS.TARGET_PROP_NESTED_REST(ctx.platform.name, get_prop_path(object)),
|
|
1086
|
+
ctx.filename,
|
|
1087
|
+
rest,
|
|
1088
|
+
ctx.errors,
|
|
1089
|
+
ctx.comments,
|
|
1090
|
+
);
|
|
1091
|
+
}
|
|
1092
|
+
|
|
1093
|
+
/**
|
|
1094
|
+
* @param {AST.ObjectPattern} pattern
|
|
1095
|
+
* @param {string} object how the body reads the value the pattern destructures
|
|
1096
|
+
* @param {boolean} nested whether the pattern is inside a prop
|
|
1097
|
+
* @param {TransformContext} ctx
|
|
1098
|
+
* @returns {void}
|
|
1099
|
+
*/
|
|
1100
|
+
function validate_prop_keys(pattern, object, nested, ctx) {
|
|
1101
|
+
for (const property of pattern.properties) {
|
|
1102
|
+
if (property.type === 'RestElement') {
|
|
1103
|
+
// The pattern's own rest element gets the attrs: the props it doesn't
|
|
1104
|
+
// declare.
|
|
1105
|
+
if (nested) report_prop_rest(property, object, ctx);
|
|
1106
|
+
continue;
|
|
1107
|
+
}
|
|
1108
|
+
const key = property.key;
|
|
1109
|
+
if (!property.computed && key.type === 'Identifier') {
|
|
1110
|
+
validate_prop_value(
|
|
1111
|
+
property.value,
|
|
1112
|
+
join_prop_member(object, `.${key.name}`, nested),
|
|
1113
|
+
nested,
|
|
1114
|
+
ctx,
|
|
391
1115
|
);
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
1116
|
+
continue;
|
|
1117
|
+
}
|
|
1118
|
+
const written = get_inline_text(key, ctx);
|
|
1119
|
+
const example = get_prop_read_example(
|
|
1120
|
+
property.value,
|
|
1121
|
+
join_prop_member(object, get_prop_member_access(key, written, !nested), nested),
|
|
1122
|
+
ctx,
|
|
1123
|
+
);
|
|
1124
|
+
error(
|
|
1125
|
+
property.computed && key.type === 'Identifier'
|
|
1126
|
+
? TSRX_ERRORS.TARGET_PROP_KEY_COMPUTED(ctx.platform.name, key.name, example)
|
|
1127
|
+
: TSRX_ERRORS.TARGET_PROP_KEY_UNSUPPORTED(
|
|
1128
|
+
ctx.platform.name,
|
|
1129
|
+
property.computed ? `[${written}]` : written,
|
|
1130
|
+
example,
|
|
1131
|
+
),
|
|
1132
|
+
ctx.filename,
|
|
1133
|
+
key,
|
|
1134
|
+
ctx.errors,
|
|
1135
|
+
ctx.comments,
|
|
1136
|
+
);
|
|
1137
|
+
}
|
|
1138
|
+
}
|
|
1139
|
+
|
|
1140
|
+
/**
|
|
1141
|
+
* Check the pattern a prop, or a value inside one, is destructured with. A
|
|
1142
|
+
* prop's own default is checked as TSRX2029 by `validate_prop_defaults`.
|
|
1143
|
+
*
|
|
1144
|
+
* @param {AST.Pattern} pattern
|
|
1145
|
+
* @param {string} object how the body reads the value
|
|
1146
|
+
* @param {boolean} nested whether the value is inside a prop
|
|
1147
|
+
* @param {TransformContext} ctx
|
|
1148
|
+
* @returns {void}
|
|
1149
|
+
*/
|
|
1150
|
+
function validate_prop_value(pattern, object, nested, ctx) {
|
|
1151
|
+
if (pattern.type === 'AssignmentPattern') {
|
|
1152
|
+
if (nested) {
|
|
1153
|
+
error(
|
|
1154
|
+
TSRX_ERRORS.TARGET_PROP_NESTED_DEFAULT_UNSUPPORTED(
|
|
1155
|
+
ctx.platform.name,
|
|
1156
|
+
get_prop_path(object),
|
|
1157
|
+
get_prop_read_example(pattern, object, ctx),
|
|
1158
|
+
),
|
|
1159
|
+
ctx.filename,
|
|
1160
|
+
pattern.right,
|
|
1161
|
+
ctx.errors,
|
|
1162
|
+
ctx.comments,
|
|
397
1163
|
);
|
|
1164
|
+
}
|
|
1165
|
+
validate_prop_value(pattern.left, object, nested, ctx);
|
|
1166
|
+
} else if (pattern.type === 'ObjectPattern') {
|
|
1167
|
+
validate_prop_keys(pattern, object, true, ctx);
|
|
1168
|
+
} else if (pattern.type === 'ArrayPattern') {
|
|
1169
|
+
pattern.elements.forEach((element, index) => {
|
|
1170
|
+
if (element?.type === 'RestElement') {
|
|
1171
|
+
report_prop_rest(element, object, ctx);
|
|
1172
|
+
} else if (element) {
|
|
1173
|
+
validate_prop_value(element, join_prop_member(object, `[${index}]`, true), true, ctx);
|
|
1174
|
+
}
|
|
1175
|
+
});
|
|
1176
|
+
}
|
|
1177
|
+
}
|
|
1178
|
+
|
|
1179
|
+
/**
|
|
1180
|
+
* Read a member of a prop's value with optional chaining, since the prop
|
|
1181
|
+
* itself can be missing.
|
|
1182
|
+
*
|
|
1183
|
+
* @param {string} object
|
|
1184
|
+
* @param {string} member `.name` or `[key]`
|
|
1185
|
+
* @param {boolean} nested whether `object` is a prop's value, not the props
|
|
1186
|
+
* @returns {string}
|
|
1187
|
+
*/
|
|
1188
|
+
function join_prop_member(object, member, nested) {
|
|
1189
|
+
return nested
|
|
1190
|
+
? `${object}?.${member.startsWith('.') ? member.slice(1) : member}`
|
|
1191
|
+
: object + member;
|
|
1192
|
+
}
|
|
1193
|
+
|
|
1194
|
+
/**
|
|
1195
|
+
* A value's path in the props as the messages show it, such as `s.a`.
|
|
1196
|
+
*
|
|
1197
|
+
* @param {string} object
|
|
1198
|
+
* @returns {string}
|
|
1199
|
+
*/
|
|
1200
|
+
function get_prop_path(object) {
|
|
1201
|
+
return object.slice('props.'.length).replace(/\?\.(\[)?/g, (_, bracket) => bracket ?? '.');
|
|
1202
|
+
}
|
|
1203
|
+
|
|
1204
|
+
/**
|
|
1205
|
+
* How the body reads the member at `key`. Vue camelizes a prop's name, so a
|
|
1206
|
+
* top-level `'a-b'` prop is `props.aB`.
|
|
1207
|
+
*
|
|
1208
|
+
* @param {AST.Expression | AST.PrivateIdentifier} key
|
|
1209
|
+
* @param {string} written the key as written
|
|
1210
|
+
* @param {boolean} top whether the key names a prop
|
|
1211
|
+
* @returns {string}
|
|
1212
|
+
*/
|
|
1213
|
+
function get_prop_member_access(key, written, top) {
|
|
1214
|
+
const name =
|
|
1215
|
+
key.type === 'Literal' && typeof key.value === 'string'
|
|
1216
|
+
? key.value
|
|
1217
|
+
: key.type === 'TemplateLiteral' && key.expressions.length === 0
|
|
1218
|
+
? key.quasis[0].value.cooked
|
|
1219
|
+
: null;
|
|
1220
|
+
if (name == null) return `[${written}]`;
|
|
1221
|
+
const member = top ? name.replace(/-(\w)/g, (_, letter) => letter.toUpperCase()) : name;
|
|
1222
|
+
return /^[A-Za-z_$][\w$]*$/.test(member) ? `.${member}` : `[${JSON.stringify(member)}]`;
|
|
1223
|
+
}
|
|
1224
|
+
|
|
1225
|
+
/**
|
|
1226
|
+
* An expression that reads the value at `access` where it is used, with the
|
|
1227
|
+
* pattern's default, or an empty string when the pattern binds more than a
|
|
1228
|
+
* name or its default spans lines.
|
|
1229
|
+
*
|
|
1230
|
+
* @param {AST.Pattern} value
|
|
1231
|
+
* @param {string} access
|
|
1232
|
+
* @param {TransformContext} ctx
|
|
1233
|
+
* @returns {string}
|
|
1234
|
+
*/
|
|
1235
|
+
function get_prop_read_example(value, access, ctx) {
|
|
1236
|
+
if (value.type === 'Identifier') return `{${access}}`;
|
|
1237
|
+
if (value.type !== 'AssignmentPattern' || value.left.type !== 'Identifier') return '';
|
|
1238
|
+
const fallback = get_source_text(value.right, ctx);
|
|
1239
|
+
// A default replaces only `undefined`, so `??`, which also replaces `null`,
|
|
1240
|
+
// would not read the same.
|
|
1241
|
+
return fallback === null ? '' : `{${access} === undefined ? ${fallback} : ${access}}`;
|
|
1242
|
+
}
|
|
1243
|
+
|
|
1244
|
+
/**
|
|
1245
|
+
* A node's source text, on one line.
|
|
1246
|
+
*
|
|
1247
|
+
* @param {AST.Node} node
|
|
1248
|
+
* @param {TransformContext} ctx
|
|
1249
|
+
* @returns {string}
|
|
1250
|
+
*/
|
|
1251
|
+
function get_inline_text(node, ctx) {
|
|
1252
|
+
if (ctx.source && node.start != null && node.end != null) {
|
|
1253
|
+
return ctx.source.slice(node.start, node.end).replace(/\s*\n\s*/g, ' ');
|
|
1254
|
+
}
|
|
1255
|
+
return node.type === 'Identifier'
|
|
1256
|
+
? node.name
|
|
1257
|
+
: node.type === 'Literal'
|
|
1258
|
+
? String(node.value)
|
|
1259
|
+
: '…';
|
|
1260
|
+
}
|
|
1261
|
+
|
|
1262
|
+
/**
|
|
1263
|
+
* @param {AST.Node} node
|
|
1264
|
+
* @param {TransformContext} ctx
|
|
1265
|
+
* @returns {string | null} the node's source text, when it is on one line
|
|
1266
|
+
*/
|
|
1267
|
+
function get_source_text(node, ctx) {
|
|
1268
|
+
const text =
|
|
1269
|
+
ctx.source && node.start != null && node.end != null
|
|
1270
|
+
? ctx.source.slice(node.start, node.end)
|
|
1271
|
+
: null;
|
|
1272
|
+
return text && !text.includes('\n') ? text : null;
|
|
1273
|
+
}
|
|
1274
|
+
|
|
1275
|
+
/**
|
|
1276
|
+
* @param {AST.Node} node
|
|
1277
|
+
* @returns {boolean}
|
|
1278
|
+
*/
|
|
1279
|
+
function is_kept_prop_default(node) {
|
|
1280
|
+
switch (node.type) {
|
|
1281
|
+
case 'TSAsExpression':
|
|
1282
|
+
case 'TSNonNullExpression':
|
|
1283
|
+
case 'ParenthesizedExpression':
|
|
1284
|
+
return is_kept_prop_default(/** @type {AST.Node} */ (node.expression));
|
|
1285
|
+
case 'Literal':
|
|
1286
|
+
// vue-jsx-vapor parses a regular expression or a bigint as its own
|
|
1287
|
+
// literal kind, which it drops.
|
|
1288
|
+
return !('regex' in node && node.regex) && !('bigint' in node && node.bigint);
|
|
1289
|
+
case 'Identifier':
|
|
1290
|
+
case 'ObjectExpression':
|
|
1291
|
+
case 'ArrayExpression':
|
|
1292
|
+
case 'ArrowFunctionExpression':
|
|
1293
|
+
case 'FunctionExpression':
|
|
1294
|
+
return true;
|
|
1295
|
+
default:
|
|
1296
|
+
return false;
|
|
1297
|
+
}
|
|
1298
|
+
}
|
|
1299
|
+
|
|
1300
|
+
/**
|
|
1301
|
+
* Whether `fn` builds its output in a local binding and returns that binding
|
|
1302
|
+
* (`const element = <div />; return element;`). The core transform marks only
|
|
1303
|
+
* functions that return JSX directly or have an `@{ … }` body as components.
|
|
1304
|
+
*
|
|
1305
|
+
* @param {AST.Function} fn
|
|
1306
|
+
* @returns {boolean}
|
|
1307
|
+
*/
|
|
1308
|
+
function returns_local_jsx_binding(fn) {
|
|
1309
|
+
if (fn.body?.type !== 'BlockStatement') {
|
|
1310
|
+
return false;
|
|
1311
|
+
}
|
|
1312
|
+
|
|
1313
|
+
/** @type {{ jsx_bindings: Set<string>, returned: Set<string> }} */
|
|
1314
|
+
const state = { jsx_bindings: new Set(), returned: new Set() };
|
|
1315
|
+
walk(/** @type {AST.Node} */ (fn.body), state, {
|
|
1316
|
+
FunctionDeclaration() {},
|
|
1317
|
+
FunctionExpression() {},
|
|
1318
|
+
ArrowFunctionExpression() {},
|
|
1319
|
+
VariableDeclarator(node, { state, next }) {
|
|
1320
|
+
if (node.id.type === 'Identifier' && contains_jsx(node.init)) {
|
|
1321
|
+
state.jsx_bindings.add(node.id.name);
|
|
1322
|
+
}
|
|
1323
|
+
next();
|
|
1324
|
+
},
|
|
1325
|
+
AssignmentExpression(node, { state, next }) {
|
|
1326
|
+
if (node.left.type === 'Identifier' && contains_jsx(node.right)) {
|
|
1327
|
+
state.jsx_bindings.add(node.left.name);
|
|
1328
|
+
}
|
|
1329
|
+
next();
|
|
1330
|
+
},
|
|
1331
|
+
ReturnStatement(node, { state, next }) {
|
|
1332
|
+
if (node.argument?.type === 'Identifier') {
|
|
1333
|
+
state.returned.add(node.argument.name);
|
|
1334
|
+
}
|
|
1335
|
+
next();
|
|
398
1336
|
},
|
|
399
1337
|
});
|
|
400
|
-
|
|
1338
|
+
|
|
1339
|
+
for (const name of state.returned) {
|
|
1340
|
+
if (state.jsx_bindings.has(name)) {
|
|
1341
|
+
return true;
|
|
1342
|
+
}
|
|
1343
|
+
}
|
|
1344
|
+
return false;
|
|
1345
|
+
}
|
|
1346
|
+
|
|
1347
|
+
/**
|
|
1348
|
+
* @param {AST.Node | null | undefined} node
|
|
1349
|
+
* @returns {boolean}
|
|
1350
|
+
*/
|
|
1351
|
+
function contains_jsx(node) {
|
|
1352
|
+
if (!node) {
|
|
1353
|
+
return false;
|
|
1354
|
+
}
|
|
1355
|
+
|
|
1356
|
+
let found = false;
|
|
1357
|
+
walk(node, null, {
|
|
1358
|
+
FunctionDeclaration() {},
|
|
1359
|
+
FunctionExpression() {},
|
|
1360
|
+
ArrowFunctionExpression() {},
|
|
1361
|
+
JSXElement() {
|
|
1362
|
+
found = true;
|
|
1363
|
+
},
|
|
1364
|
+
JSXFragment() {
|
|
1365
|
+
found = true;
|
|
1366
|
+
},
|
|
1367
|
+
});
|
|
1368
|
+
return found;
|
|
1369
|
+
}
|
|
1370
|
+
|
|
1371
|
+
/** Local name of the `useSlots` import a component reads its children through. */
|
|
1372
|
+
const USE_SLOTS_INTERNAL_NAME = `${IDENTIFIER_OBFUSCATION_PREFIX}useSlots`;
|
|
1373
|
+
|
|
1374
|
+
/**
|
|
1375
|
+
* vue-jsx-vapor passes a component's nested children as its default slot, not
|
|
1376
|
+
* as a `children` prop. Read `props.children`, or a `children` binding
|
|
1377
|
+
* destructured from the props parameter or from `props` at the top of the
|
|
1378
|
+
* body, as the default slot when the caller passes nested children and as the
|
|
1379
|
+
* `children` prop otherwise, so the same source renders its children on every
|
|
1380
|
+
* target. Nested children win over a `children` prop, as on React.
|
|
1381
|
+
*
|
|
1382
|
+
* Each place that renders the children renders the slot there
|
|
1383
|
+
* (`_$_Card_slots.default?.() ?? props.children`), so the children are set up
|
|
1384
|
+
* when that place shows them and torn down when it hides them, as React
|
|
1385
|
+
* mounts and unmounts them. A read that only tests the children (the
|
|
1386
|
+
* condition of `?:` or `if`, an operand of `!`, the left side of `&&`) checks
|
|
1387
|
+
* whether the slot exists instead (`_$_Card_slots.default ?? props.children`),
|
|
1388
|
+
* so `props.children ? <div>{props.children}</div> : …` sets them up once.
|
|
1389
|
+
* A slot exists whenever the caller writes nested children, even when they
|
|
1390
|
+
* render nothing.
|
|
1391
|
+
*
|
|
1392
|
+
* @param {AST.Function} fn
|
|
1393
|
+
* @param {string} name the component's name
|
|
1394
|
+
* @param {TransformContext} ctx
|
|
1395
|
+
* @returns {AST.Function}
|
|
1396
|
+
*/
|
|
1397
|
+
function read_children_from_default_slot(fn, name, ctx) {
|
|
1398
|
+
const param = fn.params[0];
|
|
1399
|
+
/** @type {ChildrenReadState} */
|
|
1400
|
+
const state = {
|
|
1401
|
+
props_name: param?.type === 'Identifier' ? param.name : null,
|
|
1402
|
+
children_names: new Set(),
|
|
1403
|
+
children_defaults: new Map(),
|
|
1404
|
+
shadowed_names: new Set(),
|
|
1405
|
+
slots_id: create_generated_identifier(
|
|
1406
|
+
`${IDENTIFIER_OBFUSCATION_PREFIX}${name.replace(/[^\w$]/g, '_')}_slots`,
|
|
1407
|
+
),
|
|
1408
|
+
// Shared by reference: the scope visitors copy the state for each scope.
|
|
1409
|
+
rewrites: { count: 0 },
|
|
1410
|
+
};
|
|
1411
|
+
let params = fn.params;
|
|
1412
|
+
if (param?.type === 'ObjectPattern') {
|
|
1413
|
+
add_children_binding_names(param, state.children_names);
|
|
1414
|
+
const without_defaults = remove_children_defaults(param, state.children_defaults);
|
|
1415
|
+
if (without_defaults !== param) params = [without_defaults, ...fn.params.slice(1)];
|
|
1416
|
+
}
|
|
1417
|
+
// `const { children } = props;` at the top of the component body.
|
|
1418
|
+
for (const statement of fn.body.type === 'BlockStatement' ? fn.body.body : []) {
|
|
1419
|
+
if (statement.type !== 'VariableDeclaration') continue;
|
|
1420
|
+
for (const declarator of statement.declarations) {
|
|
1421
|
+
if (
|
|
1422
|
+
declarator.id.type === 'ObjectPattern' &&
|
|
1423
|
+
declarator.init?.type === 'Identifier' &&
|
|
1424
|
+
declarator.init.name === state.props_name
|
|
1425
|
+
) {
|
|
1426
|
+
add_children_binding_names(declarator.id, state.children_names);
|
|
1427
|
+
}
|
|
1428
|
+
}
|
|
1429
|
+
}
|
|
1430
|
+
if (!state.props_name && state.children_names.size === 0) {
|
|
1431
|
+
return fn;
|
|
1432
|
+
}
|
|
1433
|
+
|
|
1434
|
+
/**
|
|
1435
|
+
* @param {AST.Node} node
|
|
1436
|
+
* @param {import('zimmerframe').Context<AST.Node, ChildrenReadState>} context
|
|
1437
|
+
*/
|
|
1438
|
+
const shadow_head = (node, { state, next }) => {
|
|
1439
|
+
/** @type {AST.Node[]} */
|
|
1440
|
+
const bindings = [];
|
|
1441
|
+
if (node.type === 'CatchClause' && node.param) bindings.push(node.param);
|
|
1442
|
+
const head =
|
|
1443
|
+
node.type === 'ForStatement'
|
|
1444
|
+
? node.init
|
|
1445
|
+
: node.type === 'ForInStatement' || node.type === 'ForOfStatement'
|
|
1446
|
+
? node.left
|
|
1447
|
+
: null;
|
|
1448
|
+
if (head?.type === 'VariableDeclaration') {
|
|
1449
|
+
for (const declarator of head.declarations) bindings.push(declarator.id);
|
|
1450
|
+
}
|
|
1451
|
+
if (bindings.length === 0) return next();
|
|
1452
|
+
const shadowed_names = new Set(state.shadowed_names);
|
|
1453
|
+
for (const binding of bindings) collect_pattern_names(binding, shadowed_names);
|
|
1454
|
+
return next({ ...state, shadowed_names });
|
|
1455
|
+
};
|
|
1456
|
+
|
|
1457
|
+
const body = /** @type {AST.BlockStatement | AST.Expression} */ (
|
|
1458
|
+
walk(/** @type {AST.Node} */ (fn.body), state, {
|
|
1459
|
+
ChainExpression(node, { path, state, next }) {
|
|
1460
|
+
if (
|
|
1461
|
+
node.expression.type === 'MemberExpression' &&
|
|
1462
|
+
is_props_children_read(node.expression, path.at(-1), state)
|
|
1463
|
+
) {
|
|
1464
|
+
return read_children_or_default_slot(node, path.at(-1), state);
|
|
1465
|
+
}
|
|
1466
|
+
return next();
|
|
1467
|
+
},
|
|
1468
|
+
MemberExpression(node, { path, state, next }) {
|
|
1469
|
+
if (is_props_children_read(node, path.at(-1), state)) {
|
|
1470
|
+
return read_children_or_default_slot(node, path.at(-1), state);
|
|
1471
|
+
}
|
|
1472
|
+
return next();
|
|
1473
|
+
},
|
|
1474
|
+
Property(node, { path, state, next }) {
|
|
1475
|
+
const value = node.value;
|
|
1476
|
+
if (
|
|
1477
|
+
node.shorthand &&
|
|
1478
|
+
path.at(-1)?.type === 'ObjectExpression' &&
|
|
1479
|
+
value.type === 'Identifier' &&
|
|
1480
|
+
is_children_binding(value, state)
|
|
1481
|
+
) {
|
|
1482
|
+
return {
|
|
1483
|
+
...node,
|
|
1484
|
+
shorthand: false,
|
|
1485
|
+
value: read_children_or_default_slot(value, node, state),
|
|
1486
|
+
};
|
|
1487
|
+
}
|
|
1488
|
+
return next();
|
|
1489
|
+
},
|
|
1490
|
+
Identifier(node, { path, state, next }) {
|
|
1491
|
+
const parent = path.at(-1);
|
|
1492
|
+
if (
|
|
1493
|
+
is_children_binding(node, state) &&
|
|
1494
|
+
parent &&
|
|
1495
|
+
is_runtime_reference(node, parent) &&
|
|
1496
|
+
!is_assignment_target(node, parent) &&
|
|
1497
|
+
!is_binding_position(node, parent, path.at(-2))
|
|
1498
|
+
) {
|
|
1499
|
+
return read_children_or_default_slot(node, parent, state);
|
|
1500
|
+
}
|
|
1501
|
+
return next();
|
|
1502
|
+
},
|
|
1503
|
+
FunctionDeclaration: rewrite_function_shadowed_refs,
|
|
1504
|
+
FunctionExpression: rewrite_function_shadowed_refs,
|
|
1505
|
+
ArrowFunctionExpression: rewrite_function_shadowed_refs,
|
|
1506
|
+
CatchClause: shadow_head,
|
|
1507
|
+
ForStatement: shadow_head,
|
|
1508
|
+
ForInStatement: shadow_head,
|
|
1509
|
+
ForOfStatement: shadow_head,
|
|
1510
|
+
BlockStatement(node, context) {
|
|
1511
|
+
// The component body declares the destructured `children` binding;
|
|
1512
|
+
// it cannot redeclare the props parameter or its bindings.
|
|
1513
|
+
if (context.path.length === 0) return context.next();
|
|
1514
|
+
return rewrite_block_shadowed_refs(node, context);
|
|
1515
|
+
},
|
|
1516
|
+
})
|
|
1517
|
+
);
|
|
1518
|
+
if (state.rewrites.count === 0) {
|
|
1519
|
+
return fn;
|
|
1520
|
+
}
|
|
1521
|
+
|
|
1522
|
+
ctx.needs_use_slots = true;
|
|
1523
|
+
// `const _$_Card_slots = _$_useSlots();`
|
|
1524
|
+
const slot_declaration = b.const(state.slots_id, b.call(USE_SLOTS_INTERNAL_NAME));
|
|
1525
|
+
const statements =
|
|
1526
|
+
body.type === 'BlockStatement' ? body.body : [b.return(/** @type {AST.Expression} */ (body))];
|
|
1527
|
+
return /** @type {AST.Function} */ ({
|
|
1528
|
+
...fn,
|
|
1529
|
+
params,
|
|
1530
|
+
body: b.block(
|
|
1531
|
+
[slot_declaration, ...statements],
|
|
1532
|
+
body.type === 'BlockStatement' && has_location(body) ? body : undefined,
|
|
1533
|
+
),
|
|
1534
|
+
...(fn.type === 'ArrowFunctionExpression' ? { expression: false } : null),
|
|
1535
|
+
});
|
|
1536
|
+
}
|
|
1537
|
+
|
|
1538
|
+
/**
|
|
1539
|
+
* @typedef {{
|
|
1540
|
+
* props_name: string | null,
|
|
1541
|
+
* children_names: Set<string>,
|
|
1542
|
+
* children_defaults: Map<string, AST.Expression>,
|
|
1543
|
+
* shadowed_names: Set<string>,
|
|
1544
|
+
* slots_id: AST.Identifier,
|
|
1545
|
+
* rewrites: { count: number },
|
|
1546
|
+
* }} ChildrenReadState
|
|
1547
|
+
*/
|
|
1548
|
+
|
|
1549
|
+
/**
|
|
1550
|
+
* @param {AST.ObjectPattern} pattern
|
|
1551
|
+
* @param {Set<string>} names
|
|
1552
|
+
* @returns {void}
|
|
1553
|
+
*/
|
|
1554
|
+
function add_children_binding_names(pattern, names) {
|
|
1555
|
+
for (const property of pattern.properties) {
|
|
1556
|
+
if (property.type !== 'Property' || property.computed) continue;
|
|
1557
|
+
if (get_static_name(property.key) !== 'children') continue;
|
|
1558
|
+
const value =
|
|
1559
|
+
property.value.type === 'AssignmentPattern' ? property.value.left : property.value;
|
|
1560
|
+
if (value.type === 'Identifier') names.add(value.name);
|
|
1561
|
+
}
|
|
1562
|
+
}
|
|
1563
|
+
|
|
1564
|
+
/**
|
|
1565
|
+
* Drop the default of a destructured `children` parameter (`{ children = x }`)
|
|
1566
|
+
* and record it, so a read can fall back from the slot to the prop to the
|
|
1567
|
+
* default. Left in place, vue-jsx-vapor would turn it into the prop's default,
|
|
1568
|
+
* which would hide the slot.
|
|
1569
|
+
*
|
|
1570
|
+
* @param {AST.ObjectPattern} pattern
|
|
1571
|
+
* @param {Map<string, AST.Expression>} defaults
|
|
1572
|
+
* @returns {AST.ObjectPattern}
|
|
1573
|
+
*/
|
|
1574
|
+
function remove_children_defaults(pattern, defaults) {
|
|
1575
|
+
let changed = false;
|
|
1576
|
+
const properties = pattern.properties.map((property) => {
|
|
1577
|
+
if (
|
|
1578
|
+
property.type !== 'Property' ||
|
|
1579
|
+
property.computed ||
|
|
1580
|
+
get_static_name(property.key) !== 'children' ||
|
|
1581
|
+
property.value.type !== 'AssignmentPattern' ||
|
|
1582
|
+
property.value.left.type !== 'Identifier'
|
|
1583
|
+
) {
|
|
1584
|
+
return property;
|
|
1585
|
+
}
|
|
1586
|
+
changed = true;
|
|
1587
|
+
defaults.set(property.value.left.name, property.value.right);
|
|
1588
|
+
return { ...property, value: property.value.left };
|
|
1589
|
+
});
|
|
1590
|
+
return changed ? { ...pattern, properties } : pattern;
|
|
1591
|
+
}
|
|
1592
|
+
|
|
1593
|
+
/**
|
|
1594
|
+
* @param {AST.Identifier} node
|
|
1595
|
+
* @param {AST.Node} parent
|
|
1596
|
+
* @param {AST.Node | undefined} grandparent
|
|
1597
|
+
* @returns {boolean}
|
|
1598
|
+
*/
|
|
1599
|
+
function is_binding_position(node, parent, grandparent) {
|
|
1600
|
+
return (
|
|
1601
|
+
(parent.type === 'VariableDeclarator' && parent.id === node) ||
|
|
1602
|
+
(parent.type === 'Property' && grandparent?.type === 'ObjectPattern') ||
|
|
1603
|
+
parent.type === 'ArrayPattern' ||
|
|
1604
|
+
parent.type === 'RestElement' ||
|
|
1605
|
+
parent.type === 'CatchClause' ||
|
|
1606
|
+
(parent.type === 'AssignmentPattern' && parent.left === node)
|
|
1607
|
+
);
|
|
1608
|
+
}
|
|
1609
|
+
|
|
1610
|
+
/**
|
|
1611
|
+
* @param {AST.MemberExpression} node
|
|
1612
|
+
* @param {AST.Node | undefined} parent
|
|
1613
|
+
* @param {ChildrenReadState} state
|
|
1614
|
+
* @returns {boolean}
|
|
1615
|
+
*/
|
|
1616
|
+
function is_props_children_read(node, parent, state) {
|
|
1617
|
+
return (
|
|
1618
|
+
!!state.props_name &&
|
|
1619
|
+
!state.shadowed_names.has(state.props_name) &&
|
|
1620
|
+
node.object.type === 'Identifier' &&
|
|
1621
|
+
node.object.name === state.props_name &&
|
|
1622
|
+
(node.computed
|
|
1623
|
+
? node.property.type === 'Literal' && node.property.value === 'children'
|
|
1624
|
+
: node.property.type === 'Identifier' && node.property.name === 'children') &&
|
|
1625
|
+
!is_assignment_target(node, parent) &&
|
|
1626
|
+
!(parent?.type === 'UnaryExpression' && parent.operator === 'delete')
|
|
1627
|
+
);
|
|
401
1628
|
}
|
|
402
1629
|
|
|
403
1630
|
/**
|
|
404
|
-
* @param {AST.
|
|
405
|
-
* @param {
|
|
406
|
-
* @
|
|
407
|
-
* @returns {AST.Expression | AST.VariableDeclaration | null}
|
|
1631
|
+
* @param {AST.Identifier} node
|
|
1632
|
+
* @param {ChildrenReadState} state
|
|
1633
|
+
* @returns {boolean}
|
|
408
1634
|
*/
|
|
409
|
-
function
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
}
|
|
413
|
-
|
|
414
|
-
const parent = path.at(-1);
|
|
415
|
-
const name = get_function_component_name(fn, parent);
|
|
416
|
-
if (!name || !/^[A-Z]/.test(name)) {
|
|
417
|
-
return null;
|
|
418
|
-
}
|
|
419
|
-
|
|
420
|
-
ctx.needs_define_vapor_component = true;
|
|
1635
|
+
function is_children_binding(node, state) {
|
|
1636
|
+
return state.children_names.has(node.name) && !state.shadowed_names.has(node.name);
|
|
1637
|
+
}
|
|
421
1638
|
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
1639
|
+
/**
|
|
1640
|
+
* @param {AST.Node} node
|
|
1641
|
+
* @param {AST.Node | undefined} parent
|
|
1642
|
+
* @returns {boolean}
|
|
1643
|
+
*/
|
|
1644
|
+
function is_assignment_target(node, parent) {
|
|
1645
|
+
return (
|
|
1646
|
+
(parent?.type === 'AssignmentExpression' && parent.left === node) ||
|
|
1647
|
+
parent?.type === 'UpdateExpression'
|
|
430
1648
|
);
|
|
1649
|
+
}
|
|
431
1650
|
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
1651
|
+
/**
|
|
1652
|
+
* `_$_Card_slots.default?.() ?? <read>` where the read renders the children,
|
|
1653
|
+
* `_$_Card_slots.default ?? <read>` where it only tests them. A destructured
|
|
1654
|
+
* default applies to the prop as it did in the pattern, only when the prop is
|
|
1655
|
+
* `undefined` (`children === void 0 ? <default> : children`), so an explicit
|
|
1656
|
+
* `children={null}` stays `null`.
|
|
1657
|
+
*
|
|
1658
|
+
* @param {AST.Expression} read
|
|
1659
|
+
* @param {AST.Node | undefined} parent
|
|
1660
|
+
* @param {ChildrenReadState} state
|
|
1661
|
+
* @returns {AST.Expression}
|
|
1662
|
+
*/
|
|
1663
|
+
function read_children_or_default_slot(read, parent, state) {
|
|
1664
|
+
state.rewrites.count += 1;
|
|
1665
|
+
const slot = b.member(clone_identifier(state.slots_id), 'default');
|
|
1666
|
+
const fallback = read.type === 'Identifier' ? state.children_defaults.get(read.name) : undefined;
|
|
1667
|
+
const prop =
|
|
1668
|
+
read.type === 'Identifier' && fallback
|
|
1669
|
+
? b.conditional(
|
|
1670
|
+
b.binary('===', read, b.unary('void', b.literal(0))),
|
|
1671
|
+
clone_ast_node(fallback),
|
|
1672
|
+
clone_identifier(read),
|
|
1673
|
+
)
|
|
1674
|
+
: read;
|
|
1675
|
+
return b.logical('??', is_test_position(read, parent) ? slot : b.maybe_call(slot), prop);
|
|
1676
|
+
}
|
|
439
1677
|
|
|
440
|
-
|
|
441
|
-
|
|
1678
|
+
/**
|
|
1679
|
+
* Whether only the truthiness or presence of `node` is used: the condition of
|
|
1680
|
+
* `?:`, `if` or a loop, the operand of `!`, the left side of `&&` (whose value
|
|
1681
|
+
* is used only when it is falsy, and a slot never is), the argument of
|
|
1682
|
+
* `Boolean(…)`, or a side of an equality with `null` or `undefined`.
|
|
1683
|
+
*
|
|
1684
|
+
* @param {AST.Node} node
|
|
1685
|
+
* @param {AST.Node | undefined} parent
|
|
1686
|
+
* @returns {boolean}
|
|
1687
|
+
*/
|
|
1688
|
+
function is_test_position(node, parent) {
|
|
1689
|
+
switch (parent?.type) {
|
|
1690
|
+
case 'BinaryExpression': {
|
|
1691
|
+
if (!['==', '!=', '===', '!=='].includes(parent.operator)) return false;
|
|
1692
|
+
const other = parent.left === node ? parent.right : parent.left;
|
|
1693
|
+
return (
|
|
1694
|
+
(other.type === 'Literal' && other.value === null && !('regex' in other)) ||
|
|
1695
|
+
(other.type === 'Identifier' && other.name === 'undefined')
|
|
1696
|
+
);
|
|
1697
|
+
}
|
|
1698
|
+
case 'CallExpression':
|
|
1699
|
+
return (
|
|
1700
|
+
parent.callee.type === 'Identifier' &&
|
|
1701
|
+
parent.callee.name === 'Boolean' &&
|
|
1702
|
+
parent.arguments[0] === node
|
|
1703
|
+
);
|
|
1704
|
+
case 'ConditionalExpression':
|
|
1705
|
+
case 'IfStatement':
|
|
1706
|
+
case 'WhileStatement':
|
|
1707
|
+
case 'DoWhileStatement':
|
|
1708
|
+
case 'ForStatement':
|
|
1709
|
+
return parent.test === node;
|
|
1710
|
+
case 'UnaryExpression':
|
|
1711
|
+
return parent.operator === '!';
|
|
1712
|
+
case 'LogicalExpression':
|
|
1713
|
+
return parent.operator === '&&' && parent.left === node;
|
|
1714
|
+
default:
|
|
1715
|
+
return false;
|
|
442
1716
|
}
|
|
443
|
-
|
|
444
|
-
return setLocation(
|
|
445
|
-
b.declaration('const', [b.declarator(create_generated_identifier(fn.id.name), call)]),
|
|
446
|
-
has_location(fn) ? fn : undefined,
|
|
447
|
-
true,
|
|
448
|
-
);
|
|
449
1717
|
}
|
|
450
1718
|
|
|
451
1719
|
/**
|
|
@@ -526,6 +1794,14 @@ function render_for_of_as_vapor_for(node, loop_params, body_statements, transfor
|
|
|
526
1794
|
return null;
|
|
527
1795
|
}
|
|
528
1796
|
|
|
1797
|
+
// A default in the head's pattern that reads the index can't be read in
|
|
1798
|
+
// the parameters, where the index comes second. The shared lowering
|
|
1799
|
+
// destructures the item in the callback for a body that can skip a row.
|
|
1800
|
+
const pattern_reads_index = for_of_pattern_reads_index(loop_params[0], node.index);
|
|
1801
|
+
if (pattern_reads_index && expression_can_skip_rendering(statement.argument)) {
|
|
1802
|
+
return null;
|
|
1803
|
+
}
|
|
1804
|
+
|
|
529
1805
|
const rendered = statement.argument;
|
|
530
1806
|
if (expression_can_skip_rendering(rendered)) {
|
|
531
1807
|
return render_for_of_as_flat_map(node, loop_params, rendered);
|
|
@@ -536,7 +1812,7 @@ function render_for_of_as_vapor_for(node, loop_params, body_statements, transfor
|
|
|
536
1812
|
: (find_jsx_key_expression(rendered) ?? (node.index ? clone_ast_node(node.index) : null));
|
|
537
1813
|
|
|
538
1814
|
const slot = key_expression
|
|
539
|
-
? create_keyed_vapor_for_slot(loop_params, rendered)
|
|
1815
|
+
? create_keyed_vapor_for_slot(loop_params, rendered, transform_context)
|
|
540
1816
|
: { params: loop_params, body: rendered, expression: true };
|
|
541
1817
|
if (!slot) {
|
|
542
1818
|
return null;
|
|
@@ -545,7 +1821,7 @@ function render_for_of_as_vapor_for(node, loop_params, body_statements, transfor
|
|
|
545
1821
|
transform_context.needs_vapor_for = true;
|
|
546
1822
|
|
|
547
1823
|
if (key_expression) {
|
|
548
|
-
strip_top_level_jsx_keys(slot.body);
|
|
1824
|
+
strip_top_level_jsx_keys(slot.rendered ?? slot.body);
|
|
549
1825
|
}
|
|
550
1826
|
|
|
551
1827
|
const attributes = [
|
|
@@ -553,18 +1829,25 @@ function render_for_of_as_vapor_for(node, loop_params, body_statements, transfor
|
|
|
553
1829
|
];
|
|
554
1830
|
|
|
555
1831
|
if (key_expression) {
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
1832
|
+
let get_key;
|
|
1833
|
+
if (pattern_reads_index) {
|
|
1834
|
+
const item = destructure_for_of_item(
|
|
1835
|
+
loop_params.map((param) => clone_ast_node(param)),
|
|
1836
|
+
/** @type {'const' | 'let' | 'var'} */ (
|
|
1837
|
+
/** @type {AST.VariableDeclaration} */ (node.left).kind
|
|
1838
|
+
),
|
|
1839
|
+
);
|
|
1840
|
+
get_key = b.arrow(item.params, b.block([item.declaration, b.return(key_expression)]));
|
|
1841
|
+
} else {
|
|
1842
|
+
get_key = create_loop_callback(loop_params, key_expression, true);
|
|
1843
|
+
}
|
|
1844
|
+
attributes.push(b.jsx_attribute(b.jsx_id('getKey'), to_jsx_expression_container(get_key)));
|
|
562
1845
|
}
|
|
563
1846
|
|
|
564
1847
|
return to_jsx_expression_container(
|
|
565
1848
|
b.jsx_element_fresh(
|
|
566
|
-
b.jsx_opening_element(b.jsx_id(
|
|
567
|
-
b.jsx_closing_element(b.jsx_id(
|
|
1849
|
+
b.jsx_opening_element(b.jsx_id(VAPOR_FOR_INTERNAL_NAME), attributes),
|
|
1850
|
+
b.jsx_closing_element(b.jsx_id(VAPOR_FOR_INTERNAL_NAME)),
|
|
568
1851
|
[to_jsx_expression_container(create_loop_callback(slot.params, slot.body, slot.expression))],
|
|
569
1852
|
),
|
|
570
1853
|
);
|
|
@@ -693,7 +1976,7 @@ function strip_top_level_jsx_keys(node) {
|
|
|
693
1976
|
|
|
694
1977
|
/**
|
|
695
1978
|
* @param {AST.Pattern[]} loop_params
|
|
696
|
-
* @param {AST.Expression} body
|
|
1979
|
+
* @param {AST.Expression | AST.BlockStatement} body
|
|
697
1980
|
* @param {boolean} expression
|
|
698
1981
|
* @returns {AST.ArrowFunctionExpression}
|
|
699
1982
|
*/
|
|
@@ -709,9 +1992,10 @@ function create_loop_callback(loop_params, body, expression) {
|
|
|
709
1992
|
/**
|
|
710
1993
|
* @param {AST.Pattern[]} loop_params
|
|
711
1994
|
* @param {AST.Expression} rendered
|
|
712
|
-
* @
|
|
1995
|
+
* @param {TransformContext} transform_context
|
|
1996
|
+
* @returns {{ params: AST.Pattern[], body: AST.Expression | AST.BlockStatement, expression: boolean, rendered?: AST.Expression } | null}
|
|
713
1997
|
*/
|
|
714
|
-
function create_keyed_vapor_for_slot(loop_params, rendered) {
|
|
1998
|
+
function create_keyed_vapor_for_slot(loop_params, rendered, transform_context) {
|
|
715
1999
|
if (loop_params[0]?.type === 'Identifier') {
|
|
716
2000
|
return {
|
|
717
2001
|
params: loop_params,
|
|
@@ -721,6 +2005,16 @@ function create_keyed_vapor_for_slot(loop_params, rendered) {
|
|
|
721
2005
|
}
|
|
722
2006
|
|
|
723
2007
|
const item_ref = create_generated_identifier('__vapor_item');
|
|
2008
|
+
if (contains_array_pattern(loop_params[0])) {
|
|
2009
|
+
transform_context.needs_vapor_computed = true;
|
|
2010
|
+
return create_destructured_keyed_vapor_for_slot(
|
|
2011
|
+
loop_params,
|
|
2012
|
+
rendered,
|
|
2013
|
+
item_ref,
|
|
2014
|
+
transform_context,
|
|
2015
|
+
);
|
|
2016
|
+
}
|
|
2017
|
+
|
|
724
2018
|
const item_ref_value = create_value_member_expression(item_ref);
|
|
725
2019
|
const replacements = create_pattern_replacements(loop_params[0], item_ref_value);
|
|
726
2020
|
if (!replacements) {
|
|
@@ -741,15 +2035,106 @@ function create_keyed_vapor_for_slot(loop_params, rendered) {
|
|
|
741
2035
|
};
|
|
742
2036
|
}
|
|
743
2037
|
|
|
2038
|
+
/**
|
|
2039
|
+
* A keyed slot for an item pattern that property reads can't stand in for:
|
|
2040
|
+
* an array pattern destructures through the item's iterator, so `[a, b]` of
|
|
2041
|
+
* a `Set` or a string is not `item[0]` and `item[1]`. The slot destructures
|
|
2042
|
+
* the item with the authored pattern in a `computed`, which runs once per
|
|
2043
|
+
* item value and keeps the row's reads reactive, and reads each name from it.
|
|
2044
|
+
*
|
|
2045
|
+
* @param {AST.Pattern[]} loop_params
|
|
2046
|
+
* @param {AST.Expression} rendered
|
|
2047
|
+
* @param {AST.Identifier} item_ref
|
|
2048
|
+
* @param {TransformContext} transform_context
|
|
2049
|
+
* @returns {{ params: AST.Pattern[], body: AST.BlockStatement, expression: boolean, rendered: AST.Expression }}
|
|
2050
|
+
*/
|
|
2051
|
+
function create_destructured_keyed_vapor_for_slot(
|
|
2052
|
+
loop_params,
|
|
2053
|
+
rendered,
|
|
2054
|
+
item_ref,
|
|
2055
|
+
transform_context,
|
|
2056
|
+
) {
|
|
2057
|
+
const count = (vapor_bindings_counts.get(transform_context) ?? 0) + 1;
|
|
2058
|
+
vapor_bindings_counts.set(transform_context, count);
|
|
2059
|
+
const bindings_ref = create_generated_identifier(`${VAPOR_BINDINGS_NAME}_${count}`);
|
|
2060
|
+
/** @type {Set<string>} */
|
|
2061
|
+
const names = new Set();
|
|
2062
|
+
collect_pattern_names(loop_params[0], names);
|
|
2063
|
+
|
|
2064
|
+
/** @type {Map<string, AST.Expression>} */
|
|
2065
|
+
const replacements = new Map();
|
|
2066
|
+
for (const name of names) {
|
|
2067
|
+
replacements.set(
|
|
2068
|
+
name,
|
|
2069
|
+
b.member(create_value_member_expression(bindings_ref), create_generated_identifier(name)),
|
|
2070
|
+
);
|
|
2071
|
+
}
|
|
2072
|
+
|
|
2073
|
+
const bindings = b.const(
|
|
2074
|
+
bindings_ref,
|
|
2075
|
+
b.call(
|
|
2076
|
+
VUE_COMPUTED_INTERNAL_NAME,
|
|
2077
|
+
b.arrow(
|
|
2078
|
+
[],
|
|
2079
|
+
b.block([
|
|
2080
|
+
// A default in the pattern reads the index through its ref too.
|
|
2081
|
+
b.const(
|
|
2082
|
+
rewrite_vapor_for_keyed_slot_refs(loop_params[0], loop_params.slice(1)),
|
|
2083
|
+
create_value_member_expression(item_ref),
|
|
2084
|
+
),
|
|
2085
|
+
b.return(
|
|
2086
|
+
b.object([...names].map((name) => b.prop('init', b.id(name), b.id(name), false, true))),
|
|
2087
|
+
),
|
|
2088
|
+
]),
|
|
2089
|
+
),
|
|
2090
|
+
),
|
|
2091
|
+
);
|
|
2092
|
+
const rewritten_rendered = rewrite_vapor_for_keyed_slot_refs(
|
|
2093
|
+
rendered,
|
|
2094
|
+
loop_params.slice(1),
|
|
2095
|
+
replacements,
|
|
2096
|
+
);
|
|
2097
|
+
|
|
2098
|
+
return {
|
|
2099
|
+
params: [item_ref, ...loop_params.slice(1)],
|
|
2100
|
+
body: b.block([bindings, b.return(rewritten_rendered)]),
|
|
2101
|
+
expression: false,
|
|
2102
|
+
rendered: rewritten_rendered,
|
|
2103
|
+
};
|
|
2104
|
+
}
|
|
2105
|
+
|
|
2106
|
+
/**
|
|
2107
|
+
* @param {AST.Node | null} node
|
|
2108
|
+
* @returns {boolean}
|
|
2109
|
+
*/
|
|
2110
|
+
function contains_array_pattern(node) {
|
|
2111
|
+
if (!node) return false;
|
|
2112
|
+
switch (node.type) {
|
|
2113
|
+
case 'ArrayPattern':
|
|
2114
|
+
return true;
|
|
2115
|
+
case 'ObjectPattern':
|
|
2116
|
+
return node.properties.some((property) =>
|
|
2117
|
+
contains_array_pattern(property.type === 'RestElement' ? property : property.value),
|
|
2118
|
+
);
|
|
2119
|
+
case 'AssignmentPattern':
|
|
2120
|
+
return contains_array_pattern(node.left);
|
|
2121
|
+
case 'RestElement':
|
|
2122
|
+
return contains_array_pattern(node.argument);
|
|
2123
|
+
default:
|
|
2124
|
+
return false;
|
|
2125
|
+
}
|
|
2126
|
+
}
|
|
2127
|
+
|
|
744
2128
|
/**
|
|
745
2129
|
* Vue's `VaporFor` passes plain item values to unkeyed slots, but keyed slots
|
|
746
2130
|
* receive shallow refs so row instances can update in place. Match that runtime
|
|
747
2131
|
* shape by reading loop params through `.value` inside the slot body.
|
|
748
2132
|
*
|
|
749
|
-
* @
|
|
2133
|
+
* @template {AST.Node} T
|
|
2134
|
+
* @param {T} node
|
|
750
2135
|
* @param {AST.Pattern[]} loop_params
|
|
751
2136
|
* @param {Map<string, AST.Expression>} [replacements]
|
|
752
|
-
* @returns {
|
|
2137
|
+
* @returns {T}
|
|
753
2138
|
*/
|
|
754
2139
|
function rewrite_vapor_for_keyed_slot_refs(node, loop_params, replacements = new Map()) {
|
|
755
2140
|
const loop_param_names = new Set();
|
|
@@ -761,14 +2146,29 @@ function rewrite_vapor_for_keyed_slot_refs(node, loop_params, replacements = new
|
|
|
761
2146
|
return node;
|
|
762
2147
|
}
|
|
763
2148
|
|
|
764
|
-
return /** @type {
|
|
2149
|
+
return /** @type {T} */ (
|
|
765
2150
|
walk(
|
|
766
2151
|
node,
|
|
767
|
-
{ loop_param_names, shadowed_names: new Set() },
|
|
2152
|
+
{ loop_param_names, shadowed_names: new Set(), in_type: false },
|
|
768
2153
|
{
|
|
2154
|
+
_(child, { state, next }) {
|
|
2155
|
+
// A type names no runtime value, so a type parameter or type
|
|
2156
|
+
// reference spelled like a loop param stays as written; only a
|
|
2157
|
+
// `typeof` in a type reads the param.
|
|
2158
|
+
if (child.type === 'TSTypeQuery') {
|
|
2159
|
+
next({ ...state, in_type: false });
|
|
2160
|
+
} else if (is_type_only_node(child)) {
|
|
2161
|
+
next({ ...state, in_type: true });
|
|
2162
|
+
} else {
|
|
2163
|
+
next();
|
|
2164
|
+
}
|
|
2165
|
+
},
|
|
769
2166
|
Identifier(identifier, { path, state, next }) {
|
|
770
2167
|
const parent = path.at(-1);
|
|
771
2168
|
if (
|
|
2169
|
+
!state.in_type &&
|
|
2170
|
+
// `typeof Types.item` reads `Types`; `item` is a member name.
|
|
2171
|
+
!(parent?.type === 'TSQualifiedName' && parent.right === identifier) &&
|
|
772
2172
|
(state.loop_param_names.has(identifier.name) || replacements.has(identifier.name)) &&
|
|
773
2173
|
!state.shadowed_names.has(identifier.name) &&
|
|
774
2174
|
parent &&
|
|
@@ -792,6 +2192,28 @@ function rewrite_vapor_for_keyed_slot_refs(node, loop_params, replacements = new
|
|
|
792
2192
|
);
|
|
793
2193
|
}
|
|
794
2194
|
|
|
2195
|
+
/**
|
|
2196
|
+
* Whether `node` is TypeScript type syntax: a type annotation, type parameters
|
|
2197
|
+
* or arguments, the type of an `as`, `satisfies`, or `<T>` assertion, or a
|
|
2198
|
+
* type alias or interface declaration.
|
|
2199
|
+
*
|
|
2200
|
+
* @param {AST.Node} node
|
|
2201
|
+
* @returns {boolean}
|
|
2202
|
+
*/
|
|
2203
|
+
function is_type_only_node(node) {
|
|
2204
|
+
return (
|
|
2205
|
+
node.type === 'TSTypeAnnotation' ||
|
|
2206
|
+
node.type === 'TSTypeAliasDeclaration' ||
|
|
2207
|
+
node.type === 'TSInterfaceDeclaration' ||
|
|
2208
|
+
node.type === 'TSTypeParameterDeclaration' ||
|
|
2209
|
+
node.type === 'TSTypeParameterInstantiation' ||
|
|
2210
|
+
node.type === 'TSTypeReference' ||
|
|
2211
|
+
node.type === 'TSTypeLiteral' ||
|
|
2212
|
+
node.type === 'TSTypeOperator' ||
|
|
2213
|
+
/^TS[A-Za-z]*(Type|Keyword)$/.test(node.type)
|
|
2214
|
+
);
|
|
2215
|
+
}
|
|
2216
|
+
|
|
795
2217
|
/**
|
|
796
2218
|
* @param {AST.Identifier} identifier
|
|
797
2219
|
* @param {AST.Node} parent
|
|
@@ -871,8 +2293,9 @@ function collect_pattern_replacements(pattern, source, replacements) {
|
|
|
871
2293
|
}
|
|
872
2294
|
|
|
873
2295
|
/**
|
|
2296
|
+
* @template {{ shadowed_names: Set<string> }} S
|
|
874
2297
|
* @param {AST.Function} node
|
|
875
|
-
* @param {import('zimmerframe').Context<AST.Node,
|
|
2298
|
+
* @param {import('zimmerframe').Context<AST.Node, S>} context
|
|
876
2299
|
* @returns {AST.Node | void}
|
|
877
2300
|
*/
|
|
878
2301
|
function rewrite_function_shadowed_refs(node, { state, next }) {
|
|
@@ -888,8 +2311,9 @@ function rewrite_function_shadowed_refs(node, { state, next }) {
|
|
|
888
2311
|
}
|
|
889
2312
|
|
|
890
2313
|
/**
|
|
2314
|
+
* @template {{ shadowed_names: Set<string> }} S
|
|
891
2315
|
* @param {AST.BlockStatement} node
|
|
892
|
-
* @param {import('zimmerframe').Context<AST.Node,
|
|
2316
|
+
* @param {import('zimmerframe').Context<AST.Node, S>} context
|
|
893
2317
|
* @returns {AST.Node | void}
|
|
894
2318
|
*/
|
|
895
2319
|
function rewrite_block_shadowed_refs(node, { state, next }) {
|
|
@@ -941,7 +2365,9 @@ function collect_function_var_names(node, names) {
|
|
|
941
2365
|
node.type === 'FunctionExpression' ||
|
|
942
2366
|
node.type === 'ArrowFunctionExpression' ||
|
|
943
2367
|
node.type === 'ClassDeclaration' ||
|
|
944
|
-
node.type === 'ClassExpression'
|
|
2368
|
+
node.type === 'ClassExpression' ||
|
|
2369
|
+
// A namespace's `var`s are its own.
|
|
2370
|
+
node.type === 'TSModuleDeclaration'
|
|
945
2371
|
) {
|
|
946
2372
|
return;
|
|
947
2373
|
}
|
|
@@ -1189,6 +2615,300 @@ function to_jsx_expression_container(expression, source_node = expression) {
|
|
|
1189
2615
|
);
|
|
1190
2616
|
}
|
|
1191
2617
|
|
|
2618
|
+
/**
|
|
2619
|
+
* Whether `specifier` of `declaration` imports `defineVaporComponent` from
|
|
2620
|
+
* `vue-jsx-vapor` under its own name, as a value or as a type.
|
|
2621
|
+
*
|
|
2622
|
+
* @param {AST.ImportDeclaration | AST.TSRXImportDeclaration} declaration
|
|
2623
|
+
* @param {AST.ImportDeclaration['specifiers'][number]} specifier
|
|
2624
|
+
* @returns {boolean}
|
|
2625
|
+
*/
|
|
2626
|
+
function imports_define_vapor_component(declaration, specifier) {
|
|
2627
|
+
return (
|
|
2628
|
+
declaration.source.type === 'Literal' &&
|
|
2629
|
+
declaration.source.value === 'vue-jsx-vapor' &&
|
|
2630
|
+
specifier.type === 'ImportSpecifier' &&
|
|
2631
|
+
imported_name(specifier) === DEFINE_VAPOR_COMPONENT &&
|
|
2632
|
+
specifier.local.name === DEFINE_VAPOR_COMPONENT
|
|
2633
|
+
);
|
|
2634
|
+
}
|
|
2635
|
+
|
|
2636
|
+
/**
|
|
2637
|
+
* The name an import specifier imports, written as an identifier or quoted
|
|
2638
|
+
* (`import { "a" as a }`).
|
|
2639
|
+
*
|
|
2640
|
+
* @param {AST.ImportSpecifier} specifier
|
|
2641
|
+
* @returns {string | undefined}
|
|
2642
|
+
*/
|
|
2643
|
+
function imported_name(specifier) {
|
|
2644
|
+
const imported = specifier.imported;
|
|
2645
|
+
if (imported?.type === 'Identifier') return imported.name;
|
|
2646
|
+
if (imported?.type === 'Literal' && typeof imported.value === 'string') return imported.value;
|
|
2647
|
+
return undefined;
|
|
2648
|
+
}
|
|
2649
|
+
|
|
2650
|
+
/**
|
|
2651
|
+
* Whether the module may bind `Fragment` to something other than Vue's: any
|
|
2652
|
+
* identifier `Fragment` in a value position, at any depth, other than in a
|
|
2653
|
+
* value import of `Fragment` from `vue`. Declarations of every form count, and
|
|
2654
|
+
* so do references, which costs only the row's key; a property name or a type
|
|
2655
|
+
* doesn't.
|
|
2656
|
+
*
|
|
2657
|
+
* @param {AST.Program} program
|
|
2658
|
+
* @returns {boolean}
|
|
2659
|
+
*/
|
|
2660
|
+
function declares_other_fragment(program) {
|
|
2661
|
+
let found = false;
|
|
2662
|
+
walk(/** @type {AST.Node} */ (program), null, {
|
|
2663
|
+
_(node, { next }) {
|
|
2664
|
+
// A type names no runtime value.
|
|
2665
|
+
if (!found && !is_type_only_node(node)) next();
|
|
2666
|
+
},
|
|
2667
|
+
ImportDeclaration(node) {
|
|
2668
|
+
for (const specifier of node.specifiers) {
|
|
2669
|
+
if (specifier.local.name !== 'Fragment') continue;
|
|
2670
|
+
const vue_value =
|
|
2671
|
+
/** @type {AST.Literal} */ (node.source).value === 'vue' &&
|
|
2672
|
+
node.importKind !== 'type' &&
|
|
2673
|
+
specifier.type === 'ImportSpecifier' &&
|
|
2674
|
+
specifier.importKind !== 'type' &&
|
|
2675
|
+
imported_name(specifier) === 'Fragment';
|
|
2676
|
+
if (!vue_value) found = true;
|
|
2677
|
+
}
|
|
2678
|
+
},
|
|
2679
|
+
Identifier(node, { path }) {
|
|
2680
|
+
if (node.name !== 'Fragment') return;
|
|
2681
|
+
const parent = /** @type {AST.Node & { computed?: boolean, shorthand?: boolean }} */ (
|
|
2682
|
+
path.at(-1)
|
|
2683
|
+
);
|
|
2684
|
+
const name_only =
|
|
2685
|
+
!parent.computed &&
|
|
2686
|
+
((parent.type === 'MemberExpression' && parent.property === node) ||
|
|
2687
|
+
((parent.type === 'Property' ||
|
|
2688
|
+
parent.type === 'MethodDefinition' ||
|
|
2689
|
+
parent.type === 'PropertyDefinition') &&
|
|
2690
|
+
parent.key === node &&
|
|
2691
|
+
!parent.shorthand));
|
|
2692
|
+
if (!name_only) found = true;
|
|
2693
|
+
},
|
|
2694
|
+
});
|
|
2695
|
+
return found;
|
|
2696
|
+
}
|
|
2697
|
+
|
|
2698
|
+
/**
|
|
2699
|
+
* Rewrites the `<__Fragment key>` rows the shared lowering generated (their
|
|
2700
|
+
* names have no source location). vue-jsx-vapor's server build passes a
|
|
2701
|
+
* fragment's children as an array only when its tag is spelled `Fragment`;
|
|
2702
|
+
* under any other name it passes them as slots, which Vue's `Fragment`
|
|
2703
|
+
* doesn't render. So a row becomes `<Fragment key>`, with `Fragment` imported
|
|
2704
|
+
* from `vue`, unless the module declares something else by that name, and then
|
|
2705
|
+
* an unkeyed `<>` fragment: a loop that doesn't render through `VaporFor`
|
|
2706
|
+
* recreates its rows when they move (#178), so the key keeps no row state.
|
|
2707
|
+
*
|
|
2708
|
+
* @param {AST.Program} program
|
|
2709
|
+
* @param {boolean} keyed
|
|
2710
|
+
* @returns {void}
|
|
2711
|
+
*/
|
|
2712
|
+
function rewrite_generated_fragments(program, keyed) {
|
|
2713
|
+
/** @param {ESTreeJSX.JSXElement['openingElement']['name'] | undefined} name */
|
|
2714
|
+
const is_generated = (name) =>
|
|
2715
|
+
name?.type === 'JSXIdentifier' && name.name === FRAGMENT_INTERNAL_NAME && !has_location(name);
|
|
2716
|
+
const rewritten = walk(/** @type {AST.Node} */ (program), null, {
|
|
2717
|
+
JSXElement(node, { next }) {
|
|
2718
|
+
const element = /** @type {ESTreeJSX.JSXElement} */ (/** @type {unknown} */ (node));
|
|
2719
|
+
if (!is_generated(element.openingElement.name)) return next();
|
|
2720
|
+
const walked = /** @type {ESTreeJSX.JSXElement} */ (next() ?? element);
|
|
2721
|
+
if (!keyed) {
|
|
2722
|
+
return /** @type {AST.Node} */ (/** @type {unknown} */ (b.jsx_fragment(walked.children)));
|
|
2723
|
+
}
|
|
2724
|
+
for (const name of [walked.openingElement.name, walked.closingElement?.name]) {
|
|
2725
|
+
if (is_generated(name)) /** @type {ESTreeJSX.JSXIdentifier} */ (name).name = 'Fragment';
|
|
2726
|
+
}
|
|
2727
|
+
return walked;
|
|
2728
|
+
},
|
|
2729
|
+
});
|
|
2730
|
+
program.body = /** @type {AST.Program} */ (rewritten).body;
|
|
2731
|
+
}
|
|
2732
|
+
|
|
2733
|
+
/**
|
|
2734
|
+
* Turn an authored type-only import of `defineVaporComponent` from
|
|
2735
|
+
* `vue-jsx-vapor` into the value import the generated components need. The
|
|
2736
|
+
* specifier is replaced by one without a location, as for a name the compiler
|
|
2737
|
+
* adds, so the editor maps no edit of the whole import back to the source.
|
|
2738
|
+
*
|
|
2739
|
+
* @param {AST.Program} program
|
|
2740
|
+
* @returns {void}
|
|
2741
|
+
*/
|
|
2742
|
+
function promote_type_only_define_vapor_component_import(program) {
|
|
2743
|
+
for (const statement of program.body) {
|
|
2744
|
+
if (statement.type !== 'ImportDeclaration') continue;
|
|
2745
|
+
const index = statement.specifiers.findIndex(
|
|
2746
|
+
(specifier) =>
|
|
2747
|
+
imports_define_vapor_component(statement, specifier) &&
|
|
2748
|
+
(statement.importKind === 'type' ||
|
|
2749
|
+
/** @type {AST.ImportSpecifier} */ (specifier).importKind === 'type'),
|
|
2750
|
+
);
|
|
2751
|
+
if (index === -1) continue;
|
|
2752
|
+
if (statement.importKind === 'type') {
|
|
2753
|
+
statement.importKind = 'value';
|
|
2754
|
+
for (const specifier of statement.specifiers) {
|
|
2755
|
+
if (specifier.type === 'ImportSpecifier') specifier.importKind = 'type';
|
|
2756
|
+
}
|
|
2757
|
+
}
|
|
2758
|
+
statement.specifiers[index] = create_import_specifier(DEFINE_VAPOR_COMPONENT);
|
|
2759
|
+
return;
|
|
2760
|
+
}
|
|
2761
|
+
}
|
|
2762
|
+
|
|
2763
|
+
/**
|
|
2764
|
+
* Report each authored binding named `defineVaporComponent`. vue-jsx-vapor
|
|
2765
|
+
* finds components by calls to that name, and the compiler wraps each
|
|
2766
|
+
* component in the one from `vue-jsx-vapor`, so the module can't declare or
|
|
2767
|
+
* shadow it. An import of it from `vue-jsx-vapor` itself is the binding the
|
|
2768
|
+
* compiler needs.
|
|
2769
|
+
*
|
|
2770
|
+
* @param {AST.Program} program
|
|
2771
|
+
* @param {TransformContext} transform_context
|
|
2772
|
+
* @returns {boolean} whether it reported one
|
|
2773
|
+
*/
|
|
2774
|
+
function report_define_vapor_component_bindings(program, transform_context) {
|
|
2775
|
+
/** @type {Set<number>} */
|
|
2776
|
+
const reported = new Set();
|
|
2777
|
+
|
|
2778
|
+
/** @param {AST.Node | null | undefined} pattern */
|
|
2779
|
+
const check = (pattern) => {
|
|
2780
|
+
for (const id of pattern_identifiers(pattern)) {
|
|
2781
|
+
if (id.name !== DEFINE_VAPOR_COMPONENT || !has_location(id)) continue;
|
|
2782
|
+
// The transform can carry one authored node to more than one place.
|
|
2783
|
+
if (reported.has(id.start)) continue;
|
|
2784
|
+
reported.add(id.start);
|
|
2785
|
+
error(
|
|
2786
|
+
TSRX_ERRORS.DEFINE_VAPOR_COMPONENT_BINDING,
|
|
2787
|
+
transform_context.filename,
|
|
2788
|
+
id,
|
|
2789
|
+
transform_context.errors,
|
|
2790
|
+
transform_context.comments,
|
|
2791
|
+
);
|
|
2792
|
+
}
|
|
2793
|
+
};
|
|
2794
|
+
|
|
2795
|
+
/** @param {AST.Function} node */
|
|
2796
|
+
const check_function = (node) => {
|
|
2797
|
+
if (node.type !== 'ArrowFunctionExpression') check(node.id);
|
|
2798
|
+
for (const param of node.params) check(param);
|
|
2799
|
+
};
|
|
2800
|
+
|
|
2801
|
+
walk(/** @type {AST.Node} */ (program), null, {
|
|
2802
|
+
ImportDeclaration(node) {
|
|
2803
|
+
for (const specifier of node.specifiers) {
|
|
2804
|
+
if (!imports_define_vapor_component(node, specifier)) check(specifier.local);
|
|
2805
|
+
}
|
|
2806
|
+
},
|
|
2807
|
+
VariableDeclarator(node, { next }) {
|
|
2808
|
+
check(node.id);
|
|
2809
|
+
next();
|
|
2810
|
+
},
|
|
2811
|
+
FunctionDeclaration(node, { next }) {
|
|
2812
|
+
check_function(node);
|
|
2813
|
+
next();
|
|
2814
|
+
},
|
|
2815
|
+
FunctionExpression(node, { next }) {
|
|
2816
|
+
check_function(node);
|
|
2817
|
+
next();
|
|
2818
|
+
},
|
|
2819
|
+
ArrowFunctionExpression(node, { next }) {
|
|
2820
|
+
check_function(node);
|
|
2821
|
+
next();
|
|
2822
|
+
},
|
|
2823
|
+
// `declare function`: its parameters only name types.
|
|
2824
|
+
TSDeclareFunction(node) {
|
|
2825
|
+
check(node.id);
|
|
2826
|
+
},
|
|
2827
|
+
ClassDeclaration(node, { next }) {
|
|
2828
|
+
check(node.id);
|
|
2829
|
+
next();
|
|
2830
|
+
},
|
|
2831
|
+
ClassExpression(node, { next }) {
|
|
2832
|
+
check(node.id);
|
|
2833
|
+
next();
|
|
2834
|
+
},
|
|
2835
|
+
CatchClause(node, { next }) {
|
|
2836
|
+
check(node.param);
|
|
2837
|
+
next();
|
|
2838
|
+
},
|
|
2839
|
+
TSEnumDeclaration(node, { next }) {
|
|
2840
|
+
check(node.id);
|
|
2841
|
+
next();
|
|
2842
|
+
},
|
|
2843
|
+
TSModuleDeclaration(node, { next }) {
|
|
2844
|
+
if (is_instantiated_namespace(node)) check(node.id);
|
|
2845
|
+
next();
|
|
2846
|
+
},
|
|
2847
|
+
TSImportEqualsDeclaration(node, { next }) {
|
|
2848
|
+
check(node.id);
|
|
2849
|
+
next();
|
|
2850
|
+
},
|
|
2851
|
+
});
|
|
2852
|
+
|
|
2853
|
+
return reported.size > 0;
|
|
2854
|
+
}
|
|
2855
|
+
|
|
2856
|
+
/**
|
|
2857
|
+
* Whether a namespace declares a value, as TypeScript decides: a namespace of
|
|
2858
|
+
* interfaces, type aliases and namespaces of those binds only a type.
|
|
2859
|
+
*
|
|
2860
|
+
* @param {AST.TSModuleDeclaration} node
|
|
2861
|
+
* @returns {boolean}
|
|
2862
|
+
*/
|
|
2863
|
+
function is_instantiated_namespace(node) {
|
|
2864
|
+
const body = node.body;
|
|
2865
|
+
if (!body) return false;
|
|
2866
|
+
if (body.type === 'TSModuleDeclaration') return is_instantiated_namespace(body);
|
|
2867
|
+
return body.body.some((statement) => {
|
|
2868
|
+
const declaration = /** @type {AST.Node} */ (
|
|
2869
|
+
statement.type === 'ExportNamedDeclaration' && statement.declaration
|
|
2870
|
+
? statement.declaration
|
|
2871
|
+
: statement
|
|
2872
|
+
);
|
|
2873
|
+
switch (declaration.type) {
|
|
2874
|
+
case 'TSInterfaceDeclaration':
|
|
2875
|
+
case 'TSTypeAliasDeclaration':
|
|
2876
|
+
return false;
|
|
2877
|
+
case 'TSModuleDeclaration':
|
|
2878
|
+
return is_instantiated_namespace(declaration);
|
|
2879
|
+
default:
|
|
2880
|
+
return true;
|
|
2881
|
+
}
|
|
2882
|
+
});
|
|
2883
|
+
}
|
|
2884
|
+
|
|
2885
|
+
/**
|
|
2886
|
+
* The identifiers a binding pattern declares.
|
|
2887
|
+
*
|
|
2888
|
+
* @param {AST.Node | null | undefined} node
|
|
2889
|
+
* @returns {AST.Identifier[]}
|
|
2890
|
+
*/
|
|
2891
|
+
function pattern_identifiers(node) {
|
|
2892
|
+
switch (node?.type) {
|
|
2893
|
+
case 'Identifier':
|
|
2894
|
+
return [node];
|
|
2895
|
+
case 'RestElement':
|
|
2896
|
+
return pattern_identifiers(node.argument);
|
|
2897
|
+
case 'AssignmentPattern':
|
|
2898
|
+
return pattern_identifiers(node.left);
|
|
2899
|
+
case 'ArrayPattern':
|
|
2900
|
+
return node.elements.flatMap(pattern_identifiers);
|
|
2901
|
+
case 'ObjectPattern':
|
|
2902
|
+
return node.properties.flatMap((property) =>
|
|
2903
|
+
pattern_identifiers(property.type === 'Property' ? property.value : property),
|
|
2904
|
+
);
|
|
2905
|
+
case 'TSParameterProperty':
|
|
2906
|
+
return pattern_identifiers(/** @type {AST.Node} */ (/** @type {unknown} */ (node.parameter)));
|
|
2907
|
+
default:
|
|
2908
|
+
return [];
|
|
2909
|
+
}
|
|
2910
|
+
}
|
|
2911
|
+
|
|
1192
2912
|
/**
|
|
1193
2913
|
* @param {import('estree').Program} program
|
|
1194
2914
|
* @param {TransformContext} transform_context
|
|
@@ -1199,15 +2919,49 @@ function inject_vue_imports(program, transform_context) {
|
|
|
1199
2919
|
const ref_prop_source = transform_context.platform.imports.refProp;
|
|
1200
2920
|
|
|
1201
2921
|
if (transform_context.needs_define_vapor_component) {
|
|
1202
|
-
|
|
2922
|
+
promote_type_only_define_vapor_component_import(program);
|
|
2923
|
+
}
|
|
2924
|
+
const reported_define_vapor_component = report_define_vapor_component_bindings(
|
|
2925
|
+
program,
|
|
2926
|
+
transform_context,
|
|
2927
|
+
);
|
|
2928
|
+
|
|
2929
|
+
// After the error the authored binding keeps the name, and an import could
|
|
2930
|
+
// only declare it a second time.
|
|
2931
|
+
if (transform_context.needs_define_vapor_component && !reported_define_vapor_component) {
|
|
2932
|
+
ensure_named_import(program, 'vue-jsx-vapor', DEFINE_VAPOR_COMPONENT);
|
|
1203
2933
|
}
|
|
1204
2934
|
|
|
1205
2935
|
if (transform_context.needs_vapor_for) {
|
|
1206
|
-
ensure_named_import(program, 'vue-jsx-vapor', 'VaporFor');
|
|
2936
|
+
ensure_named_import(program, 'vue-jsx-vapor', 'VaporFor', VAPOR_FOR_INTERNAL_NAME);
|
|
2937
|
+
}
|
|
2938
|
+
|
|
2939
|
+
if (transform_context.needs_vapor_computed) {
|
|
2940
|
+
// Its own declaration: an authored `import type` or `import * as` from
|
|
2941
|
+
// 'vue' can't take a value specifier.
|
|
2942
|
+
insertAfterDirectives(program, [
|
|
2943
|
+
b.imports([['computed', VUE_COMPUTED_INTERNAL_NAME, 'value']], 'vue'),
|
|
2944
|
+
]);
|
|
2945
|
+
}
|
|
2946
|
+
|
|
2947
|
+
if (transform_context.needs_use_slots) {
|
|
2948
|
+
ensure_named_import(program, 'vue', 'useSlots', USE_SLOTS_INTERNAL_NAME);
|
|
1207
2949
|
}
|
|
1208
2950
|
|
|
1209
2951
|
if (transform_context.needs_suspense) {
|
|
1210
|
-
ensure_named_import(program, 'vue', 'Suspense');
|
|
2952
|
+
ensure_named_import(program, 'vue', 'Suspense', SUSPENSE_INTERNAL_NAME);
|
|
2953
|
+
}
|
|
2954
|
+
|
|
2955
|
+
// A keyed row whose body is a fragment, in a loop that doesn't render
|
|
2956
|
+
// through `VaporFor`, is a `<Fragment key>`, which vue-jsx-vapor lowers to a
|
|
2957
|
+
// keyed fragment.
|
|
2958
|
+
if (transform_context.needs_fragment) {
|
|
2959
|
+
if (!declares_other_fragment(program)) {
|
|
2960
|
+
rewrite_generated_fragments(program, true);
|
|
2961
|
+
ensure_named_import(program, 'vue', 'Fragment');
|
|
2962
|
+
} else {
|
|
2963
|
+
rewrite_generated_fragments(program, false);
|
|
2964
|
+
}
|
|
1211
2965
|
}
|
|
1212
2966
|
|
|
1213
2967
|
if (transform_context.needs_error_boundary) {
|
|
@@ -1215,6 +2969,7 @@ function inject_vue_imports(program, transform_context) {
|
|
|
1215
2969
|
program,
|
|
1216
2970
|
transform_context.platform.imports.errorBoundary,
|
|
1217
2971
|
'TsrxErrorBoundary',
|
|
2972
|
+
ERROR_BOUNDARY_INTERNAL_NAME,
|
|
1218
2973
|
);
|
|
1219
2974
|
}
|
|
1220
2975
|
|
|
@@ -1222,6 +2977,15 @@ function inject_vue_imports(program, transform_context) {
|
|
|
1222
2977
|
ensure_named_import(program, merge_refs_source, 'mergeRefs', MERGE_REFS_INTERNAL_NAME);
|
|
1223
2978
|
}
|
|
1224
2979
|
|
|
2980
|
+
if (transform_context.needs_join_scope_class && ref_prop_source) {
|
|
2981
|
+
ensure_named_import(
|
|
2982
|
+
program,
|
|
2983
|
+
ref_prop_source,
|
|
2984
|
+
'join_scope_class',
|
|
2985
|
+
JOIN_SCOPE_CLASS_INTERNAL_NAME,
|
|
2986
|
+
);
|
|
2987
|
+
}
|
|
2988
|
+
|
|
1225
2989
|
if (transform_context.needs_normalize_spread_props && ref_prop_source) {
|
|
1226
2990
|
ensure_named_import(
|
|
1227
2991
|
program,
|
|
@@ -1242,6 +3006,12 @@ function inject_vue_imports(program, transform_context) {
|
|
|
1242
3006
|
}
|
|
1243
3007
|
|
|
1244
3008
|
/**
|
|
3009
|
+
* Import `name` as `local` from `source` unless a named value import of
|
|
3010
|
+
* `source` already has it, in any declaration: an authored
|
|
3011
|
+
* `import { Suspense } from 'vue'` can follow the keyed slot's `computed`
|
|
3012
|
+
* import. Otherwise add it to the first named value import of `source`, or
|
|
3013
|
+
* give it an import of its own.
|
|
3014
|
+
*
|
|
1245
3015
|
* @param {import('estree').Program} program
|
|
1246
3016
|
* @param {string} source
|
|
1247
3017
|
* @param {string} name
|
|
@@ -1249,27 +3019,42 @@ function inject_vue_imports(program, transform_context) {
|
|
|
1249
3019
|
* @returns {void}
|
|
1250
3020
|
*/
|
|
1251
3021
|
function ensure_named_import(program, source, name, local = name) {
|
|
3022
|
+
/** @type {import('estree').ImportDeclaration | undefined} */
|
|
3023
|
+
let first;
|
|
1252
3024
|
for (const statement of program.body) {
|
|
1253
3025
|
if (statement.type !== 'ImportDeclaration' || statement.source?.value !== source) {
|
|
1254
3026
|
continue;
|
|
1255
3027
|
}
|
|
3028
|
+
// A type-only import is erased from the build, and a namespace import
|
|
3029
|
+
// takes no named specifiers: the helper gets an import of its own.
|
|
3030
|
+
if (
|
|
3031
|
+
statement.importKind === 'type' ||
|
|
3032
|
+
statement.specifiers.some((specifier) => specifier.type === 'ImportNamespaceSpecifier')
|
|
3033
|
+
) {
|
|
3034
|
+
continue;
|
|
3035
|
+
}
|
|
1256
3036
|
|
|
1257
3037
|
const has_specifier = statement.specifiers.some(
|
|
1258
3038
|
(specifier) =>
|
|
1259
3039
|
specifier.type === 'ImportSpecifier' &&
|
|
1260
|
-
specifier.
|
|
1261
|
-
specifier
|
|
3040
|
+
specifier.importKind !== 'type' &&
|
|
3041
|
+
imported_name(specifier) === name &&
|
|
1262
3042
|
specifier.local?.name === local,
|
|
1263
3043
|
);
|
|
1264
3044
|
|
|
1265
|
-
if (
|
|
1266
|
-
|
|
3045
|
+
if (has_specifier) {
|
|
3046
|
+
return;
|
|
1267
3047
|
}
|
|
1268
3048
|
|
|
3049
|
+
first ??= statement;
|
|
3050
|
+
}
|
|
3051
|
+
|
|
3052
|
+
if (first) {
|
|
3053
|
+
first.specifiers.push(create_import_specifier(name, local));
|
|
1269
3054
|
return;
|
|
1270
3055
|
}
|
|
1271
3056
|
|
|
1272
|
-
program
|
|
3057
|
+
insertAfterDirectives(program, [b.imports([[name, local, 'value']], source)]);
|
|
1273
3058
|
}
|
|
1274
3059
|
|
|
1275
3060
|
/**
|