@tsrx/hono 0.2.4 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +4 -1
- package/package.json +6 -2
- package/src/compiler.js +3 -1
- package/src/platform.js +854 -28
- package/src/try-body.js +15 -0
- package/types/dom/error-boundary.d.ts +14 -1
- package/types/dynamic.d.ts +9 -3
- package/types/error-boundary.d.ts +14 -1
- package/types/try-body.d.ts +7 -0
package/README.md
CHANGED
|
@@ -46,7 +46,10 @@ export function App() @{
|
|
|
46
46
|
The DOM validator diagnoses explicit async component references that it can
|
|
47
47
|
resolve statically within the module. It does not infer arbitrary Promise returns,
|
|
48
48
|
inter-module values, or object members that may have been overwritten by unknown
|
|
49
|
-
spreads or computed properties.
|
|
49
|
+
spreads or computed properties. It also stays silent about a member that may be
|
|
50
|
+
replaced before render: one written through any name, or one whose object reaches
|
|
51
|
+
code the validator does not follow, such as a function argument, a method call, a
|
|
52
|
+
component prop, or an object or array that holds it.
|
|
50
53
|
|
|
51
54
|
## Scripts
|
|
52
55
|
|
package/package.json
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
"description": "Hono JSX compiler target for TSRX",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"author": "Dominic Gannaway",
|
|
6
|
-
"version": "0.
|
|
6
|
+
"version": "0.3.0",
|
|
7
7
|
"type": "module",
|
|
8
8
|
"publishConfig": {
|
|
9
9
|
"access": "public"
|
|
@@ -33,6 +33,10 @@
|
|
|
33
33
|
"types": "./types/dom/error-boundary.d.ts",
|
|
34
34
|
"default": "./src/dom/error-boundary.js"
|
|
35
35
|
},
|
|
36
|
+
"./try-body": {
|
|
37
|
+
"types": "./types/try-body.d.ts",
|
|
38
|
+
"default": "./src/try-body.js"
|
|
39
|
+
},
|
|
36
40
|
"./ref": {
|
|
37
41
|
"types": "./types/ref.d.ts",
|
|
38
42
|
"default": "./src/ref.js"
|
|
@@ -43,7 +47,7 @@
|
|
|
43
47
|
}
|
|
44
48
|
},
|
|
45
49
|
"dependencies": {
|
|
46
|
-
"@tsrx/core": "0.
|
|
50
|
+
"@tsrx/core": "0.6.0"
|
|
47
51
|
},
|
|
48
52
|
"peerDependencies": {
|
|
49
53
|
"hono": "^4.13.7"
|
package/src/compiler.js
CHANGED
|
@@ -121,7 +121,9 @@ export function createCompiler(transform, settings = {}) {
|
|
|
121
121
|
mappings: dedupeMappings(result.mappings),
|
|
122
122
|
};
|
|
123
123
|
|
|
124
|
-
return uses_platform_flags
|
|
124
|
+
return uses_platform_flags
|
|
125
|
+
? withPlatformTypes(deduped, options?.platform, transformed.ast)
|
|
126
|
+
: deduped;
|
|
125
127
|
}
|
|
126
128
|
|
|
127
129
|
return { parse, compile, compile_to_volar_mappings };
|
package/src/platform.js
CHANGED
|
@@ -1,21 +1,90 @@
|
|
|
1
1
|
/** @import * as AST from 'estree' */
|
|
2
2
|
/** @import * as ESTreeJSX from 'estree-jsx' */
|
|
3
3
|
/** @import { JsxPlatform, JsxTransformContext, TSRXAnalysisResult } from '@tsrx/core/types' */
|
|
4
|
+
/**
|
|
5
|
+
* How an alias reaches a root binding: the alias's value at `held` is the
|
|
6
|
+
* root's value at `path`. A `*` segment stands for any one key.
|
|
7
|
+
* @typedef {{ root: import('@tsrx/core/types').Binding, path: string[], held: string[] }} HonoAliasLink
|
|
8
|
+
*/
|
|
4
9
|
|
|
5
10
|
import {
|
|
6
11
|
builders as b,
|
|
7
12
|
createJsxTransform,
|
|
8
|
-
|
|
13
|
+
ERROR_BOUNDARY_INTERNAL_NAME,
|
|
9
14
|
error,
|
|
15
|
+
extractIdentifiers,
|
|
10
16
|
findFirstTopLevelAwait,
|
|
11
17
|
findFirstTopLevelAwaitInTsrxFunctionBody,
|
|
12
18
|
is_component_like_element as isComponentLikeElement,
|
|
13
19
|
isFunctionNode,
|
|
20
|
+
TSRX_ERRORS,
|
|
14
21
|
} from '@tsrx/core';
|
|
15
22
|
|
|
23
|
+
/**
|
|
24
|
+
* Work the DOM async-component check has done, for tests that check it grows
|
|
25
|
+
* with the source rather than with every order of its aliases. Not public.
|
|
26
|
+
*/
|
|
27
|
+
export const hono_dom_validation_work = { resolution_steps: 0, exposure_updates: 0 };
|
|
28
|
+
|
|
16
29
|
const HONO_SERVER_SOURCE = 'hono/jsx';
|
|
17
30
|
const HONO_DOM_SOURCE = 'hono/jsx/dom';
|
|
18
31
|
const AST_METADATA_KEYS = new Set(['loc', 'start', 'end', 'metadata']);
|
|
32
|
+
// Built-ins that write properties of their first argument.
|
|
33
|
+
const HONO_PROPERTY_WRITERS = new Map([
|
|
34
|
+
['Object', new Set(['assign', 'defineProperty', 'defineProperties'])],
|
|
35
|
+
['Reflect', new Set(['set', 'defineProperty'])],
|
|
36
|
+
]);
|
|
37
|
+
// Built-ins that don't write their arguments, keep them or return any part of
|
|
38
|
+
// them. Every `console` method qualifies.
|
|
39
|
+
const HONO_READ_ONLY_CALLS = new Map([
|
|
40
|
+
[
|
|
41
|
+
'Object',
|
|
42
|
+
new Set([
|
|
43
|
+
'getOwnPropertyNames',
|
|
44
|
+
'getOwnPropertySymbols',
|
|
45
|
+
'hasOwn',
|
|
46
|
+
'is',
|
|
47
|
+
'isExtensible',
|
|
48
|
+
'isFrozen',
|
|
49
|
+
'isSealed',
|
|
50
|
+
'keys',
|
|
51
|
+
]),
|
|
52
|
+
],
|
|
53
|
+
['Reflect', new Set(['has', 'isExtensible', 'ownKeys'])],
|
|
54
|
+
['JSON', new Set(['stringify'])],
|
|
55
|
+
['Array', new Set(['isArray'])],
|
|
56
|
+
]);
|
|
57
|
+
// Built-ins that don't write their argument but return it (`freeze`), its
|
|
58
|
+
// members or its prototype, which other code can then write members of.
|
|
59
|
+
const HONO_MEMBER_CALLS = new Map([
|
|
60
|
+
[
|
|
61
|
+
'Object',
|
|
62
|
+
new Set([
|
|
63
|
+
'entries',
|
|
64
|
+
'freeze',
|
|
65
|
+
'getOwnPropertyDescriptor',
|
|
66
|
+
'getOwnPropertyDescriptors',
|
|
67
|
+
'getPrototypeOf',
|
|
68
|
+
'values',
|
|
69
|
+
]),
|
|
70
|
+
],
|
|
71
|
+
['Reflect', new Set(['getOwnPropertyDescriptor', 'getPrototypeOf'])],
|
|
72
|
+
]);
|
|
73
|
+
const HONO_READ_ONLY_FUNCTIONS = new Set(['String', 'Number', 'Boolean']);
|
|
74
|
+
// Alias chains can repeat member paths. Deeper exposures are cut to this
|
|
75
|
+
// depth, and a binding with more paths keeps one that covers them all; both
|
|
76
|
+
// only make them cover more.
|
|
77
|
+
const HONO_MAX_EXPOSED_DEPTH = 16;
|
|
78
|
+
const HONO_MAX_EXPOSED_PATHS = 16;
|
|
79
|
+
// `Object.prototype` methods that read the object they are called on.
|
|
80
|
+
const HONO_READ_ONLY_METHODS = new Set([
|
|
81
|
+
'hasOwnProperty',
|
|
82
|
+
'isPrototypeOf',
|
|
83
|
+
'propertyIsEnumerable',
|
|
84
|
+
'toLocaleString',
|
|
85
|
+
'toString',
|
|
86
|
+
'valueOf',
|
|
87
|
+
]);
|
|
19
88
|
|
|
20
89
|
/**
|
|
21
90
|
* The Hono JSX runtimes accept authored `class` attributes and expose their
|
|
@@ -45,12 +114,11 @@ function create_hono_platform(mode) {
|
|
|
45
114
|
},
|
|
46
115
|
validateComponentAwait(await_node, _component, ctx) {
|
|
47
116
|
error(
|
|
48
|
-
|
|
117
|
+
TSRX_ERRORS.HONO_DOM_COMPONENT_AWAIT_UNSUPPORTED,
|
|
49
118
|
ctx.filename,
|
|
50
119
|
await_node,
|
|
51
120
|
ctx.errors,
|
|
52
121
|
ctx.comments,
|
|
53
|
-
DIAGNOSTIC_CODES.TARGET_AWAIT_UNSUPPORTED,
|
|
54
122
|
);
|
|
55
123
|
},
|
|
56
124
|
}
|
|
@@ -65,6 +133,7 @@ function create_hono_platform(mode) {
|
|
|
65
133
|
dynamic: '@tsrx/hono/dynamic',
|
|
66
134
|
dynamicFactory: {},
|
|
67
135
|
errorBoundary: is_dom ? '@tsrx/hono/dom/error-boundary' : '@tsrx/hono/error-boundary',
|
|
136
|
+
tryBody: '@tsrx/hono/try-body',
|
|
68
137
|
mergeRefs: '@tsrx/hono/ref',
|
|
69
138
|
refProp: '@tsrx/hono/ref',
|
|
70
139
|
forOfIterableHelper: '@tsrx/hono/runtime/iterable',
|
|
@@ -78,7 +147,19 @@ function create_hono_platform(mode) {
|
|
|
78
147
|
},
|
|
79
148
|
jsx: {
|
|
80
149
|
rewriteClassAttr: false,
|
|
150
|
+
// Hono renders `className` as `class`.
|
|
151
|
+
hostSpreadClassProps: ['class', 'className'],
|
|
152
|
+
// Hono's server renderer prints `class` and `className` as two `class`
|
|
153
|
+
// attributes, and a browser reads the first; the DOM renderer applies
|
|
154
|
+
// the last.
|
|
155
|
+
classPropsPrintedApart: !is_dom,
|
|
156
|
+
// Hono's server renderer prints a boolean class as text; the DOM
|
|
157
|
+
// renderer leaves it out.
|
|
158
|
+
classValue: is_dom ? 'text' : 'text-both',
|
|
81
159
|
multiRefStrategy: 'merge-refs',
|
|
160
|
+
// Hono keeps a ref's cleanup per DOM node, so elements can share one
|
|
161
|
+
// merged ref.
|
|
162
|
+
shareMergedRefs: true,
|
|
82
163
|
hostSpreadRefBinding: 'in-place',
|
|
83
164
|
// Hono's server renderer escapes a string child of `<script>` like
|
|
84
165
|
// text; `dangerouslySetInnerHTML` renders the body as written in both
|
|
@@ -110,12 +191,11 @@ function create_hono_error_boundary(try_content, fallback_fn, ctx, node) {
|
|
|
110
191
|
const reset_param = node.handler?.resetParam;
|
|
111
192
|
if (reset_param) {
|
|
112
193
|
error(
|
|
113
|
-
|
|
194
|
+
TSRX_ERRORS.TARGET_CATCH_RESET_UNSUPPORTED,
|
|
114
195
|
ctx.filename,
|
|
115
196
|
reset_param,
|
|
116
197
|
ctx.errors,
|
|
117
198
|
ctx.comments,
|
|
118
|
-
DIAGNOSTIC_CODES.TARGET_CATCH_RESET_UNSUPPORTED,
|
|
119
199
|
);
|
|
120
200
|
}
|
|
121
201
|
|
|
@@ -126,14 +206,14 @@ function create_hono_error_boundary(try_content, fallback_fn, ctx, node) {
|
|
|
126
206
|
...fallback_fn,
|
|
127
207
|
params: fallback_fn.params.slice(0, 1),
|
|
128
208
|
});
|
|
129
|
-
const name = b.jsx_id(
|
|
209
|
+
const name = b.jsx_id(ERROR_BOUNDARY_INTERNAL_NAME);
|
|
130
210
|
const fallback_render = b.jsx_attribute(
|
|
131
211
|
b.jsx_id('fallbackRender'),
|
|
132
212
|
b.jsx_expression_container(hono_fallback_fn),
|
|
133
213
|
);
|
|
134
214
|
return b.jsx_element_fresh(
|
|
135
215
|
b.jsx_opening_element(name, [fallback_render], false),
|
|
136
|
-
b.jsx_closing_element(b.jsx_id(
|
|
216
|
+
b.jsx_closing_element(b.jsx_id(ERROR_BOUNDARY_INTERNAL_NAME)),
|
|
137
217
|
[try_content],
|
|
138
218
|
);
|
|
139
219
|
}
|
|
@@ -162,12 +242,11 @@ export function validate_hono_dom_components(ast, filename, context) {
|
|
|
162
242
|
if (!component) return;
|
|
163
243
|
|
|
164
244
|
error(
|
|
165
|
-
|
|
245
|
+
TSRX_ERRORS.HONO_DOM_ASYNC_COMPONENT_UNSUPPORTED,
|
|
166
246
|
filename,
|
|
167
247
|
component,
|
|
168
248
|
context.errors,
|
|
169
249
|
context.comments,
|
|
170
|
-
DIAGNOSTIC_CODES.TARGET_AWAIT_UNSUPPORTED,
|
|
171
250
|
);
|
|
172
251
|
}
|
|
173
252
|
|
|
@@ -207,6 +286,123 @@ function find_hono_dom_async_component(ast, analysis, is_custom_diagnostic_candi
|
|
|
207
286
|
const jsx_references = [];
|
|
208
287
|
/** @type {Map<import('@tsrx/core/types').Binding, string[]>} */
|
|
209
288
|
const destructured_paths = new Map();
|
|
289
|
+
// Each alias maps to the bindings it can reach. Code that can write
|
|
290
|
+
// through an alias can write the part of each root the alias reaches.
|
|
291
|
+
/** @type {Map<import('@tsrx/core/types').Binding, HonoAliasLink[]>} */
|
|
292
|
+
const alias_roots = new Map();
|
|
293
|
+
// `r ||= UI` aliases `UI` only if it assigns, which is decided once every
|
|
294
|
+
// write to `r` is known.
|
|
295
|
+
/** @type {Array<{ alias: import('@tsrx/core/types').Binding, operator: AST.AssignmentOperator, links: HonoAliasLink[] }>} */
|
|
296
|
+
const logical_aliases = [];
|
|
297
|
+
/** @type {Map<import('@tsrx/core/types').Binding, number>} */
|
|
298
|
+
const write_counts = new Map();
|
|
299
|
+
/** @type {Map<import('@tsrx/core/types').Binding, AST.Expression | null>} */
|
|
300
|
+
const plain_initializers = new Map();
|
|
301
|
+
// For each binding, the member paths below which a write, or code this
|
|
302
|
+
// pass does not follow, can replace members of its value.
|
|
303
|
+
/** @type {Map<import('@tsrx/core/types').Binding, Array<{ path: string[], propagated: boolean }>>} */
|
|
304
|
+
const exposed = new Map();
|
|
305
|
+
// Functions resolved from an initializer and a member path, without and
|
|
306
|
+
// with exposure ignored.
|
|
307
|
+
/** @type {[Map<AST.Node, Map<string, AST.Function | null>>, Map<AST.Node, Map<string, AST.Function | null>>]} */
|
|
308
|
+
const resolved_functions = [new Map(), new Map()];
|
|
309
|
+
// How often the `seen` guard has stopped a resolution, so a result it cut
|
|
310
|
+
// short isn't cached.
|
|
311
|
+
let cycle_cuts = 0;
|
|
312
|
+
|
|
313
|
+
/**
|
|
314
|
+
* @param {AST.Pattern} target
|
|
315
|
+
* @param {AST.Expression} value
|
|
316
|
+
* @param {import('@tsrx/core/types').ScopeInterface | null} scope
|
|
317
|
+
* @param {AST.AssignmentOperator} [operator]
|
|
318
|
+
* @param {boolean} [element] whether the target takes each element of the value
|
|
319
|
+
*/
|
|
320
|
+
function add_hono_aliases(target, value, scope, operator = '=', element = false) {
|
|
321
|
+
const logical = is_hono_logical_operator(operator);
|
|
322
|
+
if (operator !== '=' && !logical) return;
|
|
323
|
+
for (const { identifier, path } of get_hono_alias_targets(target)) {
|
|
324
|
+
const alias = scope?.get(identifier.name);
|
|
325
|
+
if (!alias) continue;
|
|
326
|
+
/** @type {HonoAliasLink[]} */
|
|
327
|
+
const links = [];
|
|
328
|
+
collect_hono_alias_roots(value, scope, links, element ? ['*', ...path] : path);
|
|
329
|
+
if (!links.length) continue;
|
|
330
|
+
if (logical) logical_aliases.push({ alias, operator, links });
|
|
331
|
+
else link_hono_alias(alias, links);
|
|
332
|
+
}
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
/**
|
|
336
|
+
* @param {import('@tsrx/core/types').Binding} alias
|
|
337
|
+
* @param {HonoAliasLink[]} links
|
|
338
|
+
*/
|
|
339
|
+
function link_hono_alias(alias, links) {
|
|
340
|
+
alias_roots.set(alias, [...(alias_roots.get(alias) ?? []), ...links]);
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
/**
|
|
344
|
+
* Record that members of a binding's value below `path` can be replaced.
|
|
345
|
+
* A recorded path can already cover it, and it replaces the paths it
|
|
346
|
+
* covers. Aliases reassigned from each other can make a path for every
|
|
347
|
+
* order of their members, so a path an alias passes on is cut when long,
|
|
348
|
+
* and past a few such paths a binding keeps one that covers them all. Both
|
|
349
|
+
* only widen what is exposed, and each widening covers more, so alias
|
|
350
|
+
* cycles end quickly. A path the binding's own references expose is kept
|
|
351
|
+
* as it is.
|
|
352
|
+
*
|
|
353
|
+
* @param {import('@tsrx/core/types').Binding} binding
|
|
354
|
+
* @param {string[]} path
|
|
355
|
+
* @param {boolean} [propagated] whether an alias passed the path on
|
|
356
|
+
* @returns {string[] | null} the path the binding's exposure grew by
|
|
357
|
+
*/
|
|
358
|
+
function add_hono_exposure(binding, path, propagated = false) {
|
|
359
|
+
hono_dom_validation_work.exposure_updates++;
|
|
360
|
+
const added = propagated ? path.slice(0, HONO_MAX_EXPOSED_DEPTH) : path;
|
|
361
|
+
const known = exposed.get(binding) ?? [];
|
|
362
|
+
if (known.some((other) => covers_hono_path(other.path, added))) return null;
|
|
363
|
+
let kept = known.filter((other) => !covers_hono_path(added, other.path));
|
|
364
|
+
let grown = added;
|
|
365
|
+
const others = kept.filter((other) => other.propagated);
|
|
366
|
+
if (propagated && others.length >= HONO_MAX_EXPOSED_PATHS) {
|
|
367
|
+
grown = get_hono_covering_path([...others.map((other) => other.path), added]);
|
|
368
|
+
kept = kept.filter((other) => !covers_hono_path(grown, other.path));
|
|
369
|
+
}
|
|
370
|
+
exposed.set(binding, [...kept, { path: grown, propagated }]);
|
|
371
|
+
return grown;
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
/**
|
|
375
|
+
* @param {AST.Node} target
|
|
376
|
+
* @param {import('@tsrx/core/types').ScopeInterface | null} scope
|
|
377
|
+
*/
|
|
378
|
+
function count_hono_writes(target, scope) {
|
|
379
|
+
const pattern = unwrap_hono_transparent_expression(target);
|
|
380
|
+
if (!pattern) return;
|
|
381
|
+
for (const identifier of extractIdentifiers(/** @type {AST.Pattern} */ (pattern))) {
|
|
382
|
+
const binding = scope?.get(identifier.name);
|
|
383
|
+
if (binding) write_counts.set(binding, (write_counts.get(binding) ?? 0) + 1);
|
|
384
|
+
}
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
/**
|
|
388
|
+
* Whether `binding ||= …`, `&&= …` or `??= …` can assign. It is known only
|
|
389
|
+
* for a `let` written nowhere else that starts as a literal-like value or
|
|
390
|
+
* as `undefined`.
|
|
391
|
+
*
|
|
392
|
+
* @param {import('@tsrx/core/types').Binding} binding
|
|
393
|
+
* @param {AST.AssignmentOperator} operator
|
|
394
|
+
* @returns {boolean}
|
|
395
|
+
*/
|
|
396
|
+
function can_hono_logical_assign(binding, operator) {
|
|
397
|
+
if (binding.declaration_kind !== 'let' || write_counts.get(binding) !== 1) return true;
|
|
398
|
+
const initializer = plain_initializers.get(binding);
|
|
399
|
+
if (initializer === undefined) return true;
|
|
400
|
+
const value = get_hono_static_truthiness(initializer);
|
|
401
|
+
if (!value) return true;
|
|
402
|
+
if (operator === '||=') return !value.truthy;
|
|
403
|
+
if (operator === '&&=') return value.truthy;
|
|
404
|
+
return value.nullish;
|
|
405
|
+
}
|
|
210
406
|
|
|
211
407
|
/** @param {AST.Node | null | undefined} node @param {import('@tsrx/core/types').ScopeInterface | null} scope */
|
|
212
408
|
function walk(node, scope) {
|
|
@@ -224,33 +420,104 @@ function find_hono_dom_async_component(ast, analysis, is_custom_diagnostic_candi
|
|
|
224
420
|
jsx_references.push({ node: reference, scope });
|
|
225
421
|
}
|
|
226
422
|
}
|
|
423
|
+
if (node.type === 'VariableDeclarator') {
|
|
424
|
+
const binding = node.id.type === 'Identifier' ? scope?.get(node.id.name) : null;
|
|
425
|
+
if (binding) plain_initializers.set(binding, node.init ?? null);
|
|
426
|
+
}
|
|
227
427
|
if (node.type === 'VariableDeclarator' && node.init) {
|
|
228
428
|
collect_hono_destructured_paths(node.id, scope, destructured_paths);
|
|
429
|
+
add_hono_aliases(node.id, node.init, scope);
|
|
430
|
+
}
|
|
431
|
+
if (node.type === 'AssignmentExpression') {
|
|
432
|
+
count_hono_writes(node.left, scope);
|
|
433
|
+
add_hono_aliases(node.left, node.right, scope, node.operator);
|
|
434
|
+
}
|
|
435
|
+
if (node.type === 'AssignmentPattern') {
|
|
436
|
+
// A default, of a parameter or in destructuring, can be the value.
|
|
437
|
+
add_hono_aliases(node.left, node.right, scope);
|
|
438
|
+
}
|
|
439
|
+
if (node.type === 'UpdateExpression') {
|
|
440
|
+
count_hono_writes(node.argument, scope);
|
|
441
|
+
}
|
|
442
|
+
if (
|
|
443
|
+
(node.type === 'ForOfStatement' ||
|
|
444
|
+
node.type === 'ForInStatement' ||
|
|
445
|
+
node.type === 'JSXForExpression') &&
|
|
446
|
+
'left' in node
|
|
447
|
+
) {
|
|
448
|
+
// The loop writes each value it takes to its head, declared there or not.
|
|
449
|
+
const left = node.left;
|
|
450
|
+
const targets =
|
|
451
|
+
left.type === 'VariableDeclaration'
|
|
452
|
+
? left.declarations.map((declarator) => declarator.id)
|
|
453
|
+
: [left];
|
|
454
|
+
for (const target of targets) {
|
|
455
|
+
count_hono_writes(target, scope);
|
|
456
|
+
// A `for…of` head takes each element of the iterated value.
|
|
457
|
+
if (
|
|
458
|
+
node.type === 'ForOfStatement' ||
|
|
459
|
+
(node.type === 'JSXForExpression' && node.statementType === 'ForOfStatement')
|
|
460
|
+
) {
|
|
461
|
+
add_hono_aliases(target, node.right, scope, '=', true);
|
|
462
|
+
}
|
|
463
|
+
}
|
|
229
464
|
}
|
|
230
465
|
|
|
231
466
|
for_each_hono_child(node, (child) => walk(child, scope));
|
|
232
467
|
}
|
|
233
468
|
|
|
234
469
|
/**
|
|
470
|
+
* @param {AST.Node} node
|
|
471
|
+
* @returns {AST.Function | null}
|
|
472
|
+
*/
|
|
473
|
+
function resolve_async_component(node) {
|
|
474
|
+
const value = resolve_hono_function(node, [], new Set(), false);
|
|
475
|
+
if (!value?.async) return null;
|
|
476
|
+
if (is_custom_diagnostic_candidate && !is_custom_diagnostic_candidate(value)) return null;
|
|
477
|
+
return value;
|
|
478
|
+
}
|
|
479
|
+
|
|
480
|
+
/**
|
|
481
|
+
* The function a static reference resolves to through known initializers.
|
|
482
|
+
* With `ignore_exposure`, a member that a write or other code may replace
|
|
483
|
+
* still resolves to its initializer.
|
|
484
|
+
*
|
|
235
485
|
* @param {AST.Node | null | undefined} node
|
|
236
486
|
* @param {string[]} property_path
|
|
237
487
|
* @param {Set<AST.Node>} seen
|
|
488
|
+
* @param {boolean} ignore_exposure
|
|
238
489
|
* @returns {AST.Function | null}
|
|
239
490
|
*/
|
|
240
|
-
function
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
491
|
+
function resolve_hono_function(node, property_path, seen, ignore_exposure) {
|
|
492
|
+
hono_dom_validation_work.resolution_steps++;
|
|
493
|
+
if (!node) return null;
|
|
494
|
+
if (seen.has(node)) {
|
|
495
|
+
cycle_cuts++;
|
|
496
|
+
return null;
|
|
497
|
+
}
|
|
498
|
+
seen.add(node);
|
|
499
|
+
const value = resolve_hono_function_step(node, property_path, seen, ignore_exposure);
|
|
500
|
+
seen.delete(node);
|
|
501
|
+
return value;
|
|
502
|
+
}
|
|
245
503
|
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
504
|
+
/**
|
|
505
|
+
* One step of {@link resolve_hono_function}, with `node` on the `seen` stack.
|
|
506
|
+
*
|
|
507
|
+
* @param {AST.Node} node
|
|
508
|
+
* @param {string[]} property_path
|
|
509
|
+
* @param {Set<AST.Node>} seen
|
|
510
|
+
* @param {boolean} ignore_exposure
|
|
511
|
+
* @returns {AST.Function | null}
|
|
512
|
+
*/
|
|
513
|
+
function resolve_hono_function_step(node, property_path, seen, ignore_exposure) {
|
|
514
|
+
const expression = unwrap_hono_transparent_expression(node);
|
|
515
|
+
if (expression !== node) {
|
|
516
|
+
return resolve_hono_function(expression, property_path, seen, ignore_exposure);
|
|
252
517
|
}
|
|
253
518
|
|
|
519
|
+
if (isFunctionNode(node)) return property_path.length ? null : node;
|
|
520
|
+
|
|
254
521
|
if (node.type === 'ObjectExpression' && property_path.length) {
|
|
255
522
|
// Later properties replace earlier ones. A spread or dynamic key can
|
|
256
523
|
// replace any earlier member, so stop unless a later static definition
|
|
@@ -268,26 +535,105 @@ function find_hono_dom_async_component(ast, analysis, is_custom_diagnostic_candi
|
|
|
268
535
|
// Accessors also replace data properties, but resolving their return
|
|
269
536
|
// values would require execution rather than static reference lookup.
|
|
270
537
|
return property.kind === 'init'
|
|
271
|
-
?
|
|
538
|
+
? resolve_hono_function(property.value, property_path.slice(1), seen, ignore_exposure)
|
|
272
539
|
: null;
|
|
273
540
|
}
|
|
274
541
|
return null;
|
|
275
542
|
}
|
|
276
543
|
|
|
277
544
|
const reference = resolve_hono_reference(node, scopes.get(node) ?? analysis.scope);
|
|
278
|
-
if (
|
|
545
|
+
if (
|
|
546
|
+
!reference ||
|
|
547
|
+
reference.binding.reassigned ||
|
|
548
|
+
(!ignore_exposure &&
|
|
549
|
+
is_hono_exposed(reference.binding, [...reference.path, ...property_path])) ||
|
|
550
|
+
!reference.binding.initial
|
|
551
|
+
) {
|
|
552
|
+
return null;
|
|
553
|
+
}
|
|
279
554
|
|
|
280
555
|
// Combine destructuring and member segments in root-to-leaf order.
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
556
|
+
const initial = reference.binding.initial;
|
|
557
|
+
const path = [
|
|
558
|
+
...(destructured_paths.get(reference.binding) ?? []),
|
|
559
|
+
...reference.path,
|
|
560
|
+
...property_path,
|
|
561
|
+
];
|
|
562
|
+
// Each step leads to one next step, so a value resolves the same from
|
|
563
|
+
// wherever it is reached, unless the `seen` guard cut it short: a node
|
|
564
|
+
// already on the stack can still resolve with another member path.
|
|
565
|
+
const cache = resolved_functions[ignore_exposure ? 1 : 0];
|
|
566
|
+
const key = path.join('\0');
|
|
567
|
+
const values = cache.get(initial) ?? new Map();
|
|
568
|
+
cache.set(initial, values);
|
|
569
|
+
if (values.has(key)) return values.get(key) ?? null;
|
|
570
|
+
const cuts = cycle_cuts;
|
|
571
|
+
const value = resolve_hono_function(initial, path, seen, ignore_exposure);
|
|
572
|
+
if (value || cycle_cuts === cuts) values.set(key, value);
|
|
573
|
+
return value;
|
|
574
|
+
}
|
|
575
|
+
|
|
576
|
+
/**
|
|
577
|
+
* Whether a method call's callee is an arrow function, which can't reach
|
|
578
|
+
* the object it's called on through `this`. Writes don't matter: one that
|
|
579
|
+
* could replace the method exposes the object anyway.
|
|
580
|
+
*
|
|
581
|
+
* @param {AST.MemberExpression} callee
|
|
582
|
+
* @returns {boolean}
|
|
583
|
+
*/
|
|
584
|
+
function is_hono_arrow_method(callee) {
|
|
585
|
+
return resolve_hono_function(callee, [], new Set(), true)?.type === 'ArrowFunctionExpression';
|
|
586
|
+
}
|
|
587
|
+
|
|
588
|
+
/**
|
|
589
|
+
* Whether a write, or code this pass does not follow, can replace the
|
|
590
|
+
* member at `path` of the binding's value.
|
|
591
|
+
*
|
|
592
|
+
* @param {import('@tsrx/core/types').Binding} binding
|
|
593
|
+
* @param {string[]} path
|
|
594
|
+
* @returns {boolean}
|
|
595
|
+
*/
|
|
596
|
+
function is_hono_exposed(binding, path) {
|
|
597
|
+
return !!exposed
|
|
598
|
+
.get(binding)
|
|
599
|
+
?.some(
|
|
600
|
+
(exposure) => exposure.path.length < path.length && covers_hono_path(exposure.path, path),
|
|
601
|
+
);
|
|
286
602
|
}
|
|
287
603
|
|
|
288
604
|
walk(ast, analysis.scope);
|
|
605
|
+
for (const { alias, operator, links } of logical_aliases) {
|
|
606
|
+
if (can_hono_logical_assign(alias, operator)) link_hono_alias(alias, links);
|
|
607
|
+
}
|
|
608
|
+
for (const scope of new Set([analysis.scope, ...analysis.scopes.values()])) {
|
|
609
|
+
for (const binding of scope.declarations.values()) {
|
|
610
|
+
for (const reference of binding.references) {
|
|
611
|
+
if (reference.node === binding.node) continue;
|
|
612
|
+
const path = get_hono_exposed_path(
|
|
613
|
+
reference.node,
|
|
614
|
+
reference.path,
|
|
615
|
+
scopes,
|
|
616
|
+
is_hono_arrow_method,
|
|
617
|
+
);
|
|
618
|
+
if (path) add_hono_exposure(binding, path);
|
|
619
|
+
}
|
|
620
|
+
}
|
|
621
|
+
}
|
|
622
|
+
// An exposed alias exposes the part of each root it can reach.
|
|
623
|
+
/** @type {Array<[import('@tsrx/core/types').Binding, string[]]>} */
|
|
624
|
+
const pending = [...exposed].flatMap(([binding, exposures]) =>
|
|
625
|
+
exposures.map(({ path }) => /** @type {[typeof binding, string[]]} */ ([binding, path])),
|
|
626
|
+
);
|
|
627
|
+
for (let entry = pending.pop(); entry; entry = pending.pop()) {
|
|
628
|
+
const [alias, path] = entry;
|
|
629
|
+
for (const link of alias_roots.get(alias) ?? []) {
|
|
630
|
+
const root_path = get_hono_root_exposure(link, path);
|
|
631
|
+
const grown = root_path && add_hono_exposure(link.root, root_path, true);
|
|
632
|
+
if (grown) pending.push([link.root, grown]);
|
|
633
|
+
}
|
|
634
|
+
}
|
|
289
635
|
for (const reference of jsx_references) {
|
|
290
|
-
const found = resolve_async_component(reference.node
|
|
636
|
+
const found = resolve_async_component(reference.node);
|
|
291
637
|
if (found) return found;
|
|
292
638
|
}
|
|
293
639
|
return null;
|
|
@@ -345,6 +691,486 @@ function is_hono_transparent_expression_wrapper(parent, child) {
|
|
|
345
691
|
);
|
|
346
692
|
}
|
|
347
693
|
|
|
694
|
+
/**
|
|
695
|
+
* Collect the bindings an alias of `node[path]` can reach, and where. A
|
|
696
|
+
* holder (an object or array literal) holds each value at its key or index.
|
|
697
|
+
* A dynamic key, a member read by number, an element after a spread and an
|
|
698
|
+
* iteration step are `*`. A spread copies members, so it holds nothing.
|
|
699
|
+
*
|
|
700
|
+
* @param {AST.Node | null | undefined} node
|
|
701
|
+
* @param {import('@tsrx/core/types').ScopeInterface | null} scope
|
|
702
|
+
* @param {HonoAliasLink[]} links
|
|
703
|
+
* @param {string[]} [path] the member path the alias takes below `node`
|
|
704
|
+
* @param {string[]} [held] where `node` sits in the alias's value
|
|
705
|
+
*/
|
|
706
|
+
function collect_hono_alias_roots(node, scope, links, path = [], held = []) {
|
|
707
|
+
const expression = unwrap_hono_transparent_expression(node);
|
|
708
|
+
if (!expression || !scope) return;
|
|
709
|
+
if (expression.type === 'Identifier') {
|
|
710
|
+
const binding = scope.get(expression.name);
|
|
711
|
+
if (binding) links.push({ root: binding, path, held });
|
|
712
|
+
} else if (expression.type === 'MemberExpression') {
|
|
713
|
+
const name = get_hono_member_name(expression) ?? '*';
|
|
714
|
+
collect_hono_alias_roots(expression.object, scope, links, [name, ...path], held);
|
|
715
|
+
} else if (expression.type === 'ConditionalExpression') {
|
|
716
|
+
collect_hono_alias_roots(expression.consequent, scope, links, path, held);
|
|
717
|
+
collect_hono_alias_roots(expression.alternate, scope, links, path, held);
|
|
718
|
+
} else if (expression.type === 'LogicalExpression') {
|
|
719
|
+
collect_hono_alias_roots(expression.left, scope, links, path, held);
|
|
720
|
+
collect_hono_alias_roots(expression.right, scope, links, path, held);
|
|
721
|
+
} else if (expression.type === 'SequenceExpression') {
|
|
722
|
+
collect_hono_alias_roots(expression.expressions.at(-1), scope, links, path, held);
|
|
723
|
+
} else if (expression.type === 'AssignmentExpression') {
|
|
724
|
+
if (expression.operator !== '=') {
|
|
725
|
+
collect_hono_alias_roots(expression.left, scope, links, path, held);
|
|
726
|
+
}
|
|
727
|
+
if (expression.operator === '=' || is_hono_logical_operator(expression.operator)) {
|
|
728
|
+
collect_hono_alias_roots(expression.right, scope, links, path, held);
|
|
729
|
+
}
|
|
730
|
+
} else if (expression.type === 'ObjectExpression' || expression.type === 'ArrayExpression') {
|
|
731
|
+
/** @type {Array<{ key: string, value: AST.Node }>} */
|
|
732
|
+
const entries = [];
|
|
733
|
+
if (expression.type === 'ObjectExpression') {
|
|
734
|
+
for (const property of expression.properties) {
|
|
735
|
+
if (property.type === 'Property') {
|
|
736
|
+
entries.push({ key: get_hono_property_key(property), value: property.value });
|
|
737
|
+
}
|
|
738
|
+
}
|
|
739
|
+
} else {
|
|
740
|
+
// A spread moves every later element to an index that is not known.
|
|
741
|
+
let spread = false;
|
|
742
|
+
expression.elements.forEach((element, index) => {
|
|
743
|
+
if (element?.type === 'SpreadElement') spread = true;
|
|
744
|
+
else if (element) entries.push({ key: spread ? '*' : String(index), value: element });
|
|
745
|
+
});
|
|
746
|
+
}
|
|
747
|
+
for (const { key, value } of entries) {
|
|
748
|
+
if (!path.length) {
|
|
749
|
+
collect_hono_alias_roots(value, scope, links, [], [...held, key]);
|
|
750
|
+
} else if (may_match_hono_segment(key, path[0])) {
|
|
751
|
+
collect_hono_alias_roots(value, scope, links, path.slice(1), held);
|
|
752
|
+
}
|
|
753
|
+
}
|
|
754
|
+
}
|
|
755
|
+
}
|
|
756
|
+
|
|
757
|
+
/**
|
|
758
|
+
* Where exposing an alias's value below `path` exposes a root it reaches, if
|
|
759
|
+
* at all. Exposing it above where the root sits exposes all of the root.
|
|
760
|
+
*
|
|
761
|
+
* @param {HonoAliasLink} link
|
|
762
|
+
* @param {string[]} path
|
|
763
|
+
* @returns {string[] | null}
|
|
764
|
+
*/
|
|
765
|
+
function get_hono_root_exposure(link, path) {
|
|
766
|
+
const shared = Math.min(path.length, link.held.length);
|
|
767
|
+
for (let index = 0; index < shared; index++) {
|
|
768
|
+
if (!may_match_hono_segment(path[index], link.held[index])) return null;
|
|
769
|
+
}
|
|
770
|
+
return path.length < link.held.length
|
|
771
|
+
? link.path
|
|
772
|
+
: [...link.path, ...path.slice(link.held.length)];
|
|
773
|
+
}
|
|
774
|
+
|
|
775
|
+
/**
|
|
776
|
+
* Whether `prefix` covers the start of `path`. A `*` in `prefix` covers any
|
|
777
|
+
* key.
|
|
778
|
+
*
|
|
779
|
+
* @param {string[]} prefix
|
|
780
|
+
* @param {string[]} path
|
|
781
|
+
* @returns {boolean}
|
|
782
|
+
*/
|
|
783
|
+
function covers_hono_path(prefix, path) {
|
|
784
|
+
return (
|
|
785
|
+
prefix.length <= path.length &&
|
|
786
|
+
prefix.every((segment, index) => segment === '*' || segment === path[index])
|
|
787
|
+
);
|
|
788
|
+
}
|
|
789
|
+
|
|
790
|
+
/**
|
|
791
|
+
* The longest path that covers every one of `paths`, with `*` where they
|
|
792
|
+
* differ.
|
|
793
|
+
*
|
|
794
|
+
* @param {string[][]} paths
|
|
795
|
+
* @returns {string[]}
|
|
796
|
+
*/
|
|
797
|
+
function get_hono_covering_path(paths) {
|
|
798
|
+
const length = Math.min(...paths.map((path) => path.length));
|
|
799
|
+
return Array.from({ length }, (_, index) =>
|
|
800
|
+
paths.every((path) => path[index] === paths[0][index]) ? paths[0][index] : '*',
|
|
801
|
+
);
|
|
802
|
+
}
|
|
803
|
+
|
|
804
|
+
/**
|
|
805
|
+
* @param {string} left
|
|
806
|
+
* @param {string} right
|
|
807
|
+
* @returns {boolean}
|
|
808
|
+
*/
|
|
809
|
+
function may_match_hono_segment(left, right) {
|
|
810
|
+
return left === '*' || right === '*' || left === right;
|
|
811
|
+
}
|
|
812
|
+
|
|
813
|
+
/**
|
|
814
|
+
* @param {AST.Property} property
|
|
815
|
+
* @returns {string}
|
|
816
|
+
*/
|
|
817
|
+
function get_hono_property_key(property) {
|
|
818
|
+
if (
|
|
819
|
+
property.computed &&
|
|
820
|
+
(property.key.type !== 'Literal' || typeof property.key.value !== 'string')
|
|
821
|
+
) {
|
|
822
|
+
return '*';
|
|
823
|
+
}
|
|
824
|
+
return get_hono_static_name(property.key, true) ?? '*';
|
|
825
|
+
}
|
|
826
|
+
|
|
827
|
+
/**
|
|
828
|
+
* @param {string} operator
|
|
829
|
+
* @returns {boolean}
|
|
830
|
+
*/
|
|
831
|
+
function is_hono_logical_operator(operator) {
|
|
832
|
+
return operator === '||=' || operator === '&&=' || operator === '??=';
|
|
833
|
+
}
|
|
834
|
+
|
|
835
|
+
/**
|
|
836
|
+
* The member path below which a reference's value can have members
|
|
837
|
+
* replaced: code this pass does not follow that the value reaches, as an
|
|
838
|
+
* argument, the object of a method call, a returned or yielded value, a
|
|
839
|
+
* component's prop or a value stored in a member, or a write through it. A
|
|
840
|
+
* write to `x.k` counts as replacing any member of `x`, since a setter can
|
|
841
|
+
* run. A dynamic key is `*`. Inside a holder the path stops, since later
|
|
842
|
+
* member steps act on the holder. `null` means the reference only reads the
|
|
843
|
+
* value, copies its members, or hands it to an alias this pass tracks.
|
|
844
|
+
*
|
|
845
|
+
* @param {AST.Identifier} identifier
|
|
846
|
+
* @param {AST.Node[]} ancestors
|
|
847
|
+
* @param {Map<AST.Node, import('@tsrx/core/types').ScopeInterface | null>} scopes
|
|
848
|
+
* @param {(callee: AST.MemberExpression) => boolean} is_arrow_method
|
|
849
|
+
* @returns {string[] | null}
|
|
850
|
+
*/
|
|
851
|
+
function get_hono_exposed_path(identifier, ancestors, scopes, is_arrow_method) {
|
|
852
|
+
/** @type {string[]} */
|
|
853
|
+
const path = [];
|
|
854
|
+
// Inside a holder, member steps act on the holder, not on the reference.
|
|
855
|
+
let held = false;
|
|
856
|
+
// The member step just taken, which a call or a write acts on.
|
|
857
|
+
/** @type {{ node: AST.MemberExpression, object_path: string[] } | null} */
|
|
858
|
+
let member = null;
|
|
859
|
+
/** @type {AST.Node} */
|
|
860
|
+
let child = identifier;
|
|
861
|
+
|
|
862
|
+
for (let index = ancestors.length - 1; index >= 0; index--) {
|
|
863
|
+
const parent = ancestors[index];
|
|
864
|
+
if (is_hono_transparent_expression_wrapper(parent, child)) {
|
|
865
|
+
child = parent;
|
|
866
|
+
continue;
|
|
867
|
+
}
|
|
868
|
+
const step = member;
|
|
869
|
+
member = null;
|
|
870
|
+
switch (parent.type) {
|
|
871
|
+
case 'MemberExpression': {
|
|
872
|
+
// A computed key is read, not exposed.
|
|
873
|
+
if (parent.object !== child) return null;
|
|
874
|
+
member = { node: parent, object_path: [...path] };
|
|
875
|
+
if (!held) path.push(get_hono_member_name(parent) ?? '*');
|
|
876
|
+
break;
|
|
877
|
+
}
|
|
878
|
+
case 'Property':
|
|
879
|
+
if (parent.value !== child) return null;
|
|
880
|
+
if (ancestors[index - 1]?.type !== 'ObjectExpression') return step?.object_path ?? null;
|
|
881
|
+
held = true;
|
|
882
|
+
index--;
|
|
883
|
+
child = /** @type {AST.Node} */ (ancestors[index]);
|
|
884
|
+
continue;
|
|
885
|
+
case 'ArrayExpression':
|
|
886
|
+
held = true;
|
|
887
|
+
break;
|
|
888
|
+
case 'SpreadElement': {
|
|
889
|
+
// A spread into a literal copies; a spread argument is passed.
|
|
890
|
+
const holder = ancestors[index - 1]?.type;
|
|
891
|
+
if (holder === 'ObjectExpression' || holder === 'ArrayExpression') return null;
|
|
892
|
+
break;
|
|
893
|
+
}
|
|
894
|
+
case 'ConditionalExpression':
|
|
895
|
+
if (parent.test === child) return null;
|
|
896
|
+
break;
|
|
897
|
+
case 'LogicalExpression':
|
|
898
|
+
break;
|
|
899
|
+
case 'SequenceExpression':
|
|
900
|
+
if (parent.expressions.at(-1) !== child) return null;
|
|
901
|
+
break;
|
|
902
|
+
case 'AssignmentExpression':
|
|
903
|
+
if (parent.left === child) return step?.object_path ?? null;
|
|
904
|
+
if (parent.operator !== '=' && !is_hono_logical_operator(parent.operator)) return null;
|
|
905
|
+
if (has_hono_member_target(parent.left)) return path;
|
|
906
|
+
break;
|
|
907
|
+
case 'AssignmentPattern':
|
|
908
|
+
if (parent.left === child) return step?.object_path ?? null;
|
|
909
|
+
return has_hono_member_target(parent.left) ? path : null;
|
|
910
|
+
case 'ArrayPattern':
|
|
911
|
+
case 'ObjectPattern':
|
|
912
|
+
case 'RestElement':
|
|
913
|
+
case 'UpdateExpression':
|
|
914
|
+
return step?.object_path ?? null;
|
|
915
|
+
case 'UnaryExpression':
|
|
916
|
+
return parent.operator === 'delete' ? (step?.object_path ?? null) : null;
|
|
917
|
+
case 'ForOfStatement':
|
|
918
|
+
case 'ForInStatement':
|
|
919
|
+
case 'JSXForExpression':
|
|
920
|
+
return 'left' in parent && parent.left === child ? (step?.object_path ?? null) : null;
|
|
921
|
+
case 'CallExpression':
|
|
922
|
+
case 'TaggedTemplateExpression': {
|
|
923
|
+
const callee = parent.type === 'CallExpression' ? parent.callee : parent.tag;
|
|
924
|
+
if (callee === child) {
|
|
925
|
+
// A method call passes its object as `this`, which an arrow function
|
|
926
|
+
// ignores.
|
|
927
|
+
if (!step) return null;
|
|
928
|
+
const method = get_hono_member_name(step.node);
|
|
929
|
+
if (method !== null && HONO_READ_ONLY_METHODS.has(method)) return null;
|
|
930
|
+
return is_arrow_method(step.node) ? null : step.object_path;
|
|
931
|
+
}
|
|
932
|
+
if (parent.type === 'TaggedTemplateExpression') return null;
|
|
933
|
+
const scope = scopes.get(parent) ?? null;
|
|
934
|
+
if (
|
|
935
|
+
is_hono_known_call(callee, scope, HONO_READ_ONLY_CALLS) &&
|
|
936
|
+
!has_hono_function_replacer(parent)
|
|
937
|
+
) {
|
|
938
|
+
return null;
|
|
939
|
+
}
|
|
940
|
+
// It hands its argument's members on, so any member can have its
|
|
941
|
+
// own members replaced.
|
|
942
|
+
if (is_hono_known_call(callee, scope, HONO_MEMBER_CALLS)) {
|
|
943
|
+
return held ? path : [...path, '*'];
|
|
944
|
+
}
|
|
945
|
+
// A built-in writer replaces members of its first argument.
|
|
946
|
+
// `Object.assign` copies the members of its sources, unless a holder
|
|
947
|
+
// passes the value itself.
|
|
948
|
+
if (
|
|
949
|
+
get_hono_property_writer(callee, scope) === 'assign' &&
|
|
950
|
+
parent.arguments[0] !== child &&
|
|
951
|
+
!held
|
|
952
|
+
) {
|
|
953
|
+
return null;
|
|
954
|
+
}
|
|
955
|
+
return path;
|
|
956
|
+
}
|
|
957
|
+
case 'TemplateLiteral':
|
|
958
|
+
return ancestors[index - 1]?.type === 'TaggedTemplateExpression' ? path : null;
|
|
959
|
+
case 'JSXExpressionContainer': {
|
|
960
|
+
// The tag is only read. A host element renders its children and reads
|
|
961
|
+
// its attributes, except `ref`, which it writes `current` to.
|
|
962
|
+
const owner = ancestors[index - 1];
|
|
963
|
+
if (owner?.type === 'JSXOpeningElement') return owner.name === parent ? null : path;
|
|
964
|
+
if (owner?.type === 'JSXFragment') return null;
|
|
965
|
+
if (owner?.type === 'JSXElement') return is_hono_host_element(owner) ? null : path;
|
|
966
|
+
if (owner?.type === 'JSXAttribute') {
|
|
967
|
+
const element = ancestors[index - 3];
|
|
968
|
+
const is_ref = owner.name.type === 'JSXIdentifier' && owner.name.name === 'ref';
|
|
969
|
+
return !is_ref && element?.type === 'JSXElement' && is_hono_host_element(element)
|
|
970
|
+
? null
|
|
971
|
+
: path;
|
|
972
|
+
}
|
|
973
|
+
return path;
|
|
974
|
+
}
|
|
975
|
+
case 'JSXSpreadAttribute': {
|
|
976
|
+
// A host element reads a spread's members. A component receives them as
|
|
977
|
+
// its props, so any member can have its own members replaced.
|
|
978
|
+
const element = ancestors[index - 2];
|
|
979
|
+
if (element?.type === 'JSXElement' && is_hono_host_element(element)) return null;
|
|
980
|
+
return held ? path : [...path, '*'];
|
|
981
|
+
}
|
|
982
|
+
case 'NewExpression':
|
|
983
|
+
return parent.callee === child ? null : path;
|
|
984
|
+
case 'ArrowFunctionExpression':
|
|
985
|
+
return parent.body === child ? path : null;
|
|
986
|
+
case 'ExpressionStatement':
|
|
987
|
+
case 'BinaryExpression':
|
|
988
|
+
case 'IfStatement':
|
|
989
|
+
case 'WhileStatement':
|
|
990
|
+
case 'DoWhileStatement':
|
|
991
|
+
case 'ForStatement':
|
|
992
|
+
case 'SwitchStatement':
|
|
993
|
+
case 'SwitchCase':
|
|
994
|
+
case 'JSXIfExpression':
|
|
995
|
+
case 'JSXSwitchExpression':
|
|
996
|
+
case 'VariableDeclarator':
|
|
997
|
+
case 'ExportSpecifier':
|
|
998
|
+
case 'ExportDefaultDeclaration':
|
|
999
|
+
case 'ClassDeclaration':
|
|
1000
|
+
case 'ClassExpression':
|
|
1001
|
+
case 'FunctionDeclaration':
|
|
1002
|
+
case 'FunctionExpression':
|
|
1003
|
+
return null;
|
|
1004
|
+
default:
|
|
1005
|
+
return path;
|
|
1006
|
+
}
|
|
1007
|
+
child = parent;
|
|
1008
|
+
}
|
|
1009
|
+
return null;
|
|
1010
|
+
}
|
|
1011
|
+
|
|
1012
|
+
/**
|
|
1013
|
+
* The names a pattern binds to its value or to a part of it, each with the
|
|
1014
|
+
* member path it takes: an array element's is its index, and a dynamic
|
|
1015
|
+
* key's is `*`. A rest element binds a new object or array, so it is left
|
|
1016
|
+
* out.
|
|
1017
|
+
*
|
|
1018
|
+
* @param {AST.Node | null} node
|
|
1019
|
+
* @param {string[]} [path]
|
|
1020
|
+
* @returns {Array<{ identifier: AST.Identifier, path: string[] }>}
|
|
1021
|
+
*/
|
|
1022
|
+
function get_hono_alias_targets(node, path = []) {
|
|
1023
|
+
const pattern = unwrap_hono_transparent_expression(node);
|
|
1024
|
+
if (pattern?.type === 'Identifier') return [{ identifier: pattern, path }];
|
|
1025
|
+
if (pattern?.type === 'AssignmentPattern') return get_hono_alias_targets(pattern.left, path);
|
|
1026
|
+
if (pattern?.type === 'ObjectPattern') {
|
|
1027
|
+
return pattern.properties.flatMap((property) =>
|
|
1028
|
+
property.type === 'RestElement'
|
|
1029
|
+
? []
|
|
1030
|
+
: get_hono_alias_targets(property.value, [...path, get_hono_property_key(property)]),
|
|
1031
|
+
);
|
|
1032
|
+
}
|
|
1033
|
+
if (pattern?.type === 'ArrayPattern') {
|
|
1034
|
+
return pattern.elements.flatMap((element, index) =>
|
|
1035
|
+
!element || element.type === 'RestElement'
|
|
1036
|
+
? []
|
|
1037
|
+
: get_hono_alias_targets(element, [...path, String(index)]),
|
|
1038
|
+
);
|
|
1039
|
+
}
|
|
1040
|
+
return [];
|
|
1041
|
+
}
|
|
1042
|
+
|
|
1043
|
+
/**
|
|
1044
|
+
* The truthiness of a value written as a literal, or of a missing
|
|
1045
|
+
* initializer, which leaves `undefined`.
|
|
1046
|
+
*
|
|
1047
|
+
* @param {AST.Expression | null} node
|
|
1048
|
+
* @returns {{ truthy: boolean, nullish: boolean } | null}
|
|
1049
|
+
*/
|
|
1050
|
+
function get_hono_static_truthiness(node) {
|
|
1051
|
+
const expression = node && unwrap_hono_transparent_expression(node);
|
|
1052
|
+
if (!expression) return { truthy: false, nullish: true };
|
|
1053
|
+
if (expression.type === 'Literal' && !('regex' in expression)) {
|
|
1054
|
+
return { truthy: !!expression.value, nullish: expression.value === null };
|
|
1055
|
+
}
|
|
1056
|
+
return expression.type === 'ObjectExpression' ||
|
|
1057
|
+
expression.type === 'ArrayExpression' ||
|
|
1058
|
+
expression.type === 'NewExpression' ||
|
|
1059
|
+
expression.type === 'ClassExpression' ||
|
|
1060
|
+
expression.type === 'JSXElement' ||
|
|
1061
|
+
expression.type === 'JSXFragment' ||
|
|
1062
|
+
isFunctionNode(expression)
|
|
1063
|
+
? { truthy: true, nullish: false }
|
|
1064
|
+
: null;
|
|
1065
|
+
}
|
|
1066
|
+
|
|
1067
|
+
/**
|
|
1068
|
+
* The method name of a built-in that writes its first argument's properties,
|
|
1069
|
+
* unless a local binding shadows the built-in's name, as `is_hono_known_call`
|
|
1070
|
+
* checks.
|
|
1071
|
+
*
|
|
1072
|
+
* @param {AST.Node} callee
|
|
1073
|
+
* @param {import('@tsrx/core/types').ScopeInterface | null} scope
|
|
1074
|
+
* @returns {string | null}
|
|
1075
|
+
*/
|
|
1076
|
+
function get_hono_property_writer(callee, scope) {
|
|
1077
|
+
const expression = unwrap_hono_transparent_expression(callee);
|
|
1078
|
+
if (
|
|
1079
|
+
expression?.type !== 'MemberExpression' ||
|
|
1080
|
+
expression.object.type !== 'Identifier' ||
|
|
1081
|
+
scope?.get(expression.object.name)
|
|
1082
|
+
) {
|
|
1083
|
+
return null;
|
|
1084
|
+
}
|
|
1085
|
+
const name = get_hono_member_name(expression);
|
|
1086
|
+
return name !== null && HONO_PROPERTY_WRITERS.get(expression.object.name)?.has(name)
|
|
1087
|
+
? name
|
|
1088
|
+
: null;
|
|
1089
|
+
}
|
|
1090
|
+
|
|
1091
|
+
/**
|
|
1092
|
+
* Whether a `JSON.stringify` call may have a function replacer, which gets
|
|
1093
|
+
* every value it serializes and can write it.
|
|
1094
|
+
*
|
|
1095
|
+
* @param {AST.CallExpression} call
|
|
1096
|
+
* @returns {boolean}
|
|
1097
|
+
*/
|
|
1098
|
+
function has_hono_function_replacer(call) {
|
|
1099
|
+
const callee = unwrap_hono_transparent_expression(call.callee);
|
|
1100
|
+
if (
|
|
1101
|
+
callee?.type !== 'MemberExpression' ||
|
|
1102
|
+
callee.object.type !== 'Identifier' ||
|
|
1103
|
+
callee.object.name !== 'JSON'
|
|
1104
|
+
) {
|
|
1105
|
+
return false;
|
|
1106
|
+
}
|
|
1107
|
+
const replacer = call.arguments[1];
|
|
1108
|
+
return !(
|
|
1109
|
+
!replacer ||
|
|
1110
|
+
replacer.type === 'Literal' ||
|
|
1111
|
+
replacer.type === 'ArrayExpression' ||
|
|
1112
|
+
(replacer.type === 'Identifier' && replacer.name === 'undefined')
|
|
1113
|
+
);
|
|
1114
|
+
}
|
|
1115
|
+
|
|
1116
|
+
/**
|
|
1117
|
+
* Whether a call's callee is one of `calls`, the global functions when they
|
|
1118
|
+
* are the read-only ones, and every `console` method, unless a local binding
|
|
1119
|
+
* shadows its name.
|
|
1120
|
+
*
|
|
1121
|
+
* @param {AST.Node} callee
|
|
1122
|
+
* @param {import('@tsrx/core/types').ScopeInterface | null} scope
|
|
1123
|
+
* @param {Map<string, Set<string>>} calls
|
|
1124
|
+
* @returns {boolean}
|
|
1125
|
+
*/
|
|
1126
|
+
function is_hono_known_call(callee, scope, calls) {
|
|
1127
|
+
const read_only = calls === HONO_READ_ONLY_CALLS;
|
|
1128
|
+
const expression = unwrap_hono_transparent_expression(callee);
|
|
1129
|
+
if (expression?.type === 'Identifier') {
|
|
1130
|
+
return (
|
|
1131
|
+
read_only && HONO_READ_ONLY_FUNCTIONS.has(expression.name) && !scope?.get(expression.name)
|
|
1132
|
+
);
|
|
1133
|
+
}
|
|
1134
|
+
if (expression?.type !== 'MemberExpression' || expression.object.type !== 'Identifier') {
|
|
1135
|
+
return false;
|
|
1136
|
+
}
|
|
1137
|
+
const global = expression.object.name;
|
|
1138
|
+
if (scope?.get(global)) return false;
|
|
1139
|
+
const name = get_hono_member_name(expression);
|
|
1140
|
+
return (read_only && global === 'console') || (name !== null && !!calls.get(global)?.has(name));
|
|
1141
|
+
}
|
|
1142
|
+
|
|
1143
|
+
/**
|
|
1144
|
+
* @param {AST.TSRXJSXElement | ESTreeJSX.JSXElement} element
|
|
1145
|
+
* @returns {boolean}
|
|
1146
|
+
*/
|
|
1147
|
+
function is_hono_host_element(element) {
|
|
1148
|
+
return element.openingElement.name.type === 'JSXIdentifier' && !isComponentLikeElement(element);
|
|
1149
|
+
}
|
|
1150
|
+
|
|
1151
|
+
/**
|
|
1152
|
+
* Whether an assignment target stores into a member, at any depth of a
|
|
1153
|
+
* destructuring pattern.
|
|
1154
|
+
*
|
|
1155
|
+
* @param {AST.Node | null} node
|
|
1156
|
+
* @returns {boolean}
|
|
1157
|
+
*/
|
|
1158
|
+
function has_hono_member_target(node) {
|
|
1159
|
+
const pattern = unwrap_hono_transparent_expression(node);
|
|
1160
|
+
if (pattern?.type === 'MemberExpression') return true;
|
|
1161
|
+
if (pattern?.type === 'AssignmentPattern') return has_hono_member_target(pattern.left);
|
|
1162
|
+
if (pattern?.type === 'RestElement') return has_hono_member_target(pattern.argument);
|
|
1163
|
+
if (pattern?.type === 'ObjectPattern') {
|
|
1164
|
+
return pattern.properties.some((property) =>
|
|
1165
|
+
has_hono_member_target(property.type === 'RestElement' ? property : property.value),
|
|
1166
|
+
);
|
|
1167
|
+
}
|
|
1168
|
+
if (pattern?.type === 'ArrayPattern') {
|
|
1169
|
+
return pattern.elements.some((element) => has_hono_member_target(element));
|
|
1170
|
+
}
|
|
1171
|
+
return false;
|
|
1172
|
+
}
|
|
1173
|
+
|
|
348
1174
|
/**
|
|
349
1175
|
* Resolve an identifier or static member without evaluating computed
|
|
350
1176
|
* expressions. `obj['App']` is statically known; `obj[key]` is not.
|
package/src/try-body.js
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/** @import { Child } from 'hono/jsx' */
|
|
2
|
+
/** @import { TsrxTryBodyProps } from '../types/try-body' */
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Renders a `@try` body as its own component, so a throw or suspend in the
|
|
6
|
+
* body's setup statements reaches the enclosing `ErrorBoundary` or `Suspense`
|
|
7
|
+
* instead of escaping while the parent builds its JSX. Works with both the
|
|
8
|
+
* server and the DOM renderer.
|
|
9
|
+
*
|
|
10
|
+
* @param {TsrxTryBodyProps} props
|
|
11
|
+
* @returns {Child}
|
|
12
|
+
*/
|
|
13
|
+
export function TsrxTryBody(props) {
|
|
14
|
+
return props.render();
|
|
15
|
+
}
|
|
@@ -1 +1,14 @@
|
|
|
1
|
-
|
|
1
|
+
import type { Child, FC, PropsWithChildren } from 'hono/jsx';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Hono's `ErrorBoundary` from `hono/jsx/dom`. Hono types the caught error as
|
|
5
|
+
* `Error`, but `fallbackRender` and `onError` receive the thrown value as it is,
|
|
6
|
+
* which can be anything.
|
|
7
|
+
*/
|
|
8
|
+
export declare const TsrxErrorBoundary: FC<
|
|
9
|
+
PropsWithChildren<{
|
|
10
|
+
fallback?: Child;
|
|
11
|
+
fallbackRender?: (error: unknown) => Child;
|
|
12
|
+
onError?: (error: unknown) => void;
|
|
13
|
+
}>
|
|
14
|
+
>;
|
package/types/dynamic.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { FC, JSX
|
|
1
|
+
import type { FC, JSX } from 'hono/jsx';
|
|
2
2
|
|
|
3
3
|
type DynamicIntrinsicElements = JSX.IntrinsicElements;
|
|
4
4
|
type DynamicIntrinsicElement = Extract<keyof DynamicIntrinsicElements, string>;
|
|
@@ -18,14 +18,20 @@ export type DynamicProps<T extends DynamicElementType> = DynamicComponentProps<
|
|
|
18
18
|
is: T | null | undefined | false;
|
|
19
19
|
};
|
|
20
20
|
|
|
21
|
+
/**
|
|
22
|
+
* What a Hono function component returns. Hono 4.13's `JSX.ElementType` only
|
|
23
|
+
* accepts a component that returns it, so `Dynamic` can be a JSX tag.
|
|
24
|
+
*/
|
|
25
|
+
type DynamicResult = ReturnType<FC>;
|
|
26
|
+
|
|
21
27
|
/** Type-only helper used by TSRX's Volar output for dynamic tags. */
|
|
22
28
|
export declare function Dynamic<T extends DynamicIntrinsicElement>(
|
|
23
29
|
props: DynamicIntrinsicElements[T] & {
|
|
24
30
|
is: T | null | undefined | false;
|
|
25
31
|
},
|
|
26
|
-
):
|
|
32
|
+
): DynamicResult;
|
|
27
33
|
export declare function Dynamic<T extends DynamicElementType>(
|
|
28
34
|
props: DynamicComponentProps<DynamicTarget<NoInfer<T>>> & {
|
|
29
35
|
is: T | null | undefined | false;
|
|
30
36
|
},
|
|
31
|
-
):
|
|
37
|
+
): DynamicResult;
|
|
@@ -1 +1,14 @@
|
|
|
1
|
-
|
|
1
|
+
import type { Child, FC, PropsWithChildren } from 'hono/jsx';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Hono's `ErrorBoundary` from `hono/jsx`. Hono types the caught error as
|
|
5
|
+
* `Error`, but `fallbackRender` and `onError` receive the thrown value as it is,
|
|
6
|
+
* which can be anything.
|
|
7
|
+
*/
|
|
8
|
+
export declare const TsrxErrorBoundary: FC<
|
|
9
|
+
PropsWithChildren<{
|
|
10
|
+
fallback?: Child;
|
|
11
|
+
fallbackRender?: (error: unknown) => Child;
|
|
12
|
+
onError?: (error: unknown) => void;
|
|
13
|
+
}>
|
|
14
|
+
>;
|