@llui/vite-plugin 0.0.31 → 0.0.33
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 +8 -9
- package/dist/binding-descriptors.d.ts +98 -16
- package/dist/binding-descriptors.d.ts.map +1 -1
- package/dist/binding-descriptors.js +319 -61
- package/dist/binding-descriptors.js.map +1 -1
- package/dist/cross-file-resolver.d.ts +109 -0
- package/dist/cross-file-resolver.d.ts.map +1 -0
- package/dist/cross-file-resolver.js +457 -0
- package/dist/cross-file-resolver.js.map +1 -0
- package/dist/index.d.ts +0 -24
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +241 -33
- package/dist/index.js.map +1 -1
- package/dist/msg-annotations.d.ts +47 -5
- package/dist/msg-annotations.d.ts.map +1 -1
- package/dist/msg-annotations.js +85 -9
- package/dist/msg-annotations.js.map +1 -1
- package/dist/msg-schema.d.ts +81 -5
- package/dist/msg-schema.d.ts.map +1 -1
- package/dist/msg-schema.js +201 -13
- package/dist/msg-schema.js.map +1 -1
- package/dist/transform.d.ts +47 -1
- package/dist/transform.d.ts.map +1 -1
- package/dist/transform.js +155 -61
- package/dist/transform.js.map +1 -1
- package/package.json +1 -1
- package/dist/diagnostics.d.ts +0 -14
- package/dist/diagnostics.js +0 -846
- package/dist/diagnostics.js.map +0 -1
package/README.md
CHANGED
|
@@ -44,12 +44,11 @@ The compiler runs 3 passes over every `.ts`/`.tsx` file using the TypeScript Com
|
|
|
44
44
|
|
|
45
45
|
## Diagnostics
|
|
46
46
|
|
|
47
|
-
The compiler
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
| Spread children | Spread in children array defeats static analysis |
|
|
47
|
+
Static analysis lives in `@llui/eslint-plugin`. The compiler used to emit
|
|
48
|
+
its own warnings during `transform()`; those have all migrated to ESLint
|
|
49
|
+
rules so they surface as editor squiggles instead of build-time console
|
|
50
|
+
output. Install the plugin and enable its `recommended` config to get the
|
|
51
|
+
full set — `empty-props`, `namespace-import`, `spread-in-children`,
|
|
52
|
+
`map-on-state-array`, `exhaustive-update`, `accessibility`,
|
|
53
|
+
`controlled-input`, `child-static-props`, `bitmask-overflow`,
|
|
54
|
+
`static-on`, and the `agent-*` annotation rules.
|
|
@@ -1,23 +1,105 @@
|
|
|
1
|
-
|
|
2
|
-
variant: string;
|
|
3
|
-
};
|
|
1
|
+
import ts from 'typescript';
|
|
4
2
|
/**
|
|
5
|
-
*
|
|
6
|
-
*
|
|
7
|
-
*
|
|
3
|
+
* Compiler passes that surface the Msg variants currently dispatchable
|
|
4
|
+
* from rendered UI to the agent layer's `list_actions`. Two passes
|
|
5
|
+
* cover two distinct dispatch shapes:
|
|
8
6
|
*
|
|
9
|
-
*
|
|
10
|
-
*
|
|
11
|
-
*
|
|
12
|
-
*
|
|
13
|
-
*
|
|
14
|
-
*
|
|
7
|
+
* 1. `tagEventHandlerSends` — tags event-handler arrow functions
|
|
8
|
+
* (`onClick`, `onInput`, …) whose body contains literal
|
|
9
|
+
* `<id>({type: 'X', …})` call sites. Wraps with
|
|
10
|
+
* `Object.assign(arrow, { __lluiVariants: ['X', …] })`. The
|
|
11
|
+
* runtime in `@llui/dom` `elements.ts` / `el-split.ts` reads the
|
|
12
|
+
* metadata at bind time and registers the variants on the active
|
|
13
|
+
* component instance for the lifetime of the binding's scope.
|
|
15
14
|
*
|
|
16
|
-
*
|
|
17
|
-
*
|
|
18
|
-
*
|
|
15
|
+
* 2. `injectScopeVariantRegistrations` — handles the
|
|
16
|
+
* dispatch-translation case that pass (1) can't follow:
|
|
17
|
+
* `<bag>.connect(get, sendFn, …)` where `sendFn` is a user-
|
|
18
|
+
* defined function that translates library Msgs into app Msgs via
|
|
19
|
+
* `dispatch({type: 'X', …})`. Static analysis of the library's
|
|
20
|
+
* internal onClick can't see across this hop. The pass detects the
|
|
21
|
+
* `*.connect(get, sendFn, …)` syntactic pattern, follows `sendFn`
|
|
22
|
+
* to its declaration, scans the body for literal dispatches, and
|
|
23
|
+
* inserts a runtime `__registerScopeVariants(['X', …])` call
|
|
24
|
+
* immediately before the connect call. Lifetime semantics fall
|
|
25
|
+
* out of the render context: the call's active scope is whatever
|
|
26
|
+
* `each(...)` / `branch(...)` / root scope happens to be live when
|
|
27
|
+
* the view evaluates that statement.
|
|
28
|
+
*
|
|
29
|
+
* Both passes are gated on `devMode || emitAgentMetadata` in
|
|
30
|
+
* `transform.ts`. Production bundles without agent integration get
|
|
31
|
+
* neither the per-handler `Object.assign` cost nor the registration
|
|
32
|
+
* statements.
|
|
33
|
+
*
|
|
34
|
+
* False positives are deliberate. The alternative — proving the
|
|
35
|
+
* callee resolves to the destructured `send` from a `View` bag, or
|
|
36
|
+
* tracing function calls across files — would require full scope
|
|
37
|
+
* tracking the compiler doesn't do. In practice the patterns
|
|
38
|
+
* `<id>({type:'X',…})` and `*.connect(get, fn, …)` are reliable
|
|
39
|
+
* shape-level signals; an extra entry in the live descriptor
|
|
40
|
+
* registry just means the agent sees one more "affordable variant"
|
|
41
|
+
* than necessary — never a wrong dispatch, never a runtime error.
|
|
42
|
+
*
|
|
43
|
+
* False negatives stay where they were: non-literal `type` values
|
|
44
|
+
* (`send({ type: nextStep })`), and dispatch translators bound via
|
|
45
|
+
* patterns other than `*.connect(get, fn, …)`. Apps that hit those
|
|
46
|
+
* cases declare `agentAffordances` on the component def — the
|
|
47
|
+
* documented escape hatch.
|
|
19
48
|
*
|
|
20
49
|
* @see agent spec §5.2, §12.2
|
|
50
|
+
* @see @llui/dom binding-descriptors.ts (runtime registry + helper)
|
|
51
|
+
*/
|
|
52
|
+
/**
|
|
53
|
+
* Walks every `ArrowFunction` and `FunctionExpression` in the source
|
|
54
|
+
* and wraps any whose body contains literal `<id>({type:'X', …})`
|
|
55
|
+
* dispatches with `Object.assign(fn, {__lluiVariants: ['X', …]})`.
|
|
56
|
+
*
|
|
57
|
+
* The runtime (in `@llui/dom` `elements.ts` / `el-split.ts`) reads
|
|
58
|
+
* `__lluiVariants` from event-handler bindings only — so tags placed
|
|
59
|
+
* on functions in non-handler positions (a const declared but never
|
|
60
|
+
* bound, an arrow passed to `Array.filter`, a view function whose
|
|
61
|
+
* body has nested handlers with dispatches) are runtime-inert. The
|
|
62
|
+
* compiler tags generously; the runtime registers selectively.
|
|
63
|
+
*
|
|
64
|
+
* Universal scope means three concrete patterns all surface their
|
|
65
|
+
* variants without the app author having to think about it:
|
|
66
|
+
*
|
|
67
|
+
* 1. **Inline event-handler arrows** —
|
|
68
|
+
* `onClick: () => send({type:'X'})` (the original Pass 1 case).
|
|
69
|
+
* 2. **Const-bound translator functions** —
|
|
70
|
+
* `const sendMenu = (m) => dispatch({type:'Y'})` paired with
|
|
71
|
+
* `*.connect(get, sendMenu, …)` (the original Pass 3 case). The
|
|
72
|
+
* tag travels with the function reference; library connect impls
|
|
73
|
+
* use `tagSend(send, libVariants, fn)` to propagate it onto
|
|
74
|
+
* returned handlers.
|
|
75
|
+
* 3. **Positional-arg handlers** —
|
|
76
|
+
* `helper(label, () => send({type:'Z'}))` where `helper` is an
|
|
77
|
+
* app-defined wrapper like `navButton(label, onClick)` that
|
|
78
|
+
* eventually binds the function as an event listener. The arrow
|
|
79
|
+
* is still tagged at its declaration site, and the runtime reads
|
|
80
|
+
* the tag when the wrapper binds it.
|
|
81
|
+
*
|
|
82
|
+
* False positives are deliberate. The alternative — proving that a
|
|
83
|
+
* tagged arrow actually reaches an event-handler binding — would
|
|
84
|
+
* require cross-function, cross-file flow analysis the compiler
|
|
85
|
+
* doesn't do. In practice the cost of an over-tagged arrow is bytes,
|
|
86
|
+
* not behavior: the runtime never reads the tag from non-handler
|
|
87
|
+
* bindings.
|
|
88
|
+
*
|
|
89
|
+
* Pass 2's `collectLocalFns` resolves identifiers to their original
|
|
90
|
+
* arrow/function initializers; this pass replaces those initializers
|
|
91
|
+
* with `Object.assign(arrow, {…})` wrappers. Run Pass 2 BEFORE Pass 1
|
|
92
|
+
* so the resolver still sees raw arrows.
|
|
93
|
+
*
|
|
94
|
+
* Already-wrapped functions (CallExpressions, including user-applied
|
|
95
|
+
* `tagSend(...)` or this pass's own prior output) are skipped — the
|
|
96
|
+
* pass only fires on bare arrow/function expressions.
|
|
21
97
|
*/
|
|
22
|
-
export declare function
|
|
98
|
+
export declare function tagDispatchHandlers(node: ts.SourceFile, f: ts.NodeFactory): ts.SourceFile;
|
|
99
|
+
export interface InjectResult {
|
|
100
|
+
sf: ts.SourceFile;
|
|
101
|
+
/** True when at least one `__registerScopeVariants(...)` call was inserted. */
|
|
102
|
+
injected: boolean;
|
|
103
|
+
}
|
|
104
|
+
export declare function injectScopeVariantRegistrations(node: ts.SourceFile, f: ts.NodeFactory): InjectResult;
|
|
23
105
|
//# sourceMappingURL=binding-descriptors.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"binding-descriptors.d.ts","sourceRoot":"","sources":["../src/binding-descriptors.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"binding-descriptors.d.ts","sourceRoot":"","sources":["../src/binding-descriptors.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,YAAY,CAAA;AAE3B;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAiDG;AAIH;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA6CG;AACH,wBAAgB,mBAAmB,CAAC,IAAI,EAAE,EAAE,CAAC,UAAU,EAAE,CAAC,EAAE,EAAE,CAAC,WAAW,GAAG,EAAE,CAAC,UAAU,CA0BzF;AA8BD,MAAM,WAAW,YAAY;IAC3B,EAAE,EAAE,EAAE,CAAC,UAAU,CAAA;IACjB,+EAA+E;IAC/E,QAAQ,EAAE,OAAO,CAAA;CAClB;AAED,wBAAgB,+BAA+B,CAC7C,IAAI,EAAE,EAAE,CAAC,UAAU,EACnB,CAAC,EAAE,EAAE,CAAC,WAAW,GAChB,YAAY,CA0Fd"}
|
|
@@ -1,82 +1,340 @@
|
|
|
1
1
|
import ts from 'typescript';
|
|
2
2
|
/**
|
|
3
|
-
*
|
|
4
|
-
*
|
|
5
|
-
*
|
|
3
|
+
* Compiler passes that surface the Msg variants currently dispatchable
|
|
4
|
+
* from rendered UI to the agent layer's `list_actions`. Two passes
|
|
5
|
+
* cover two distinct dispatch shapes:
|
|
6
6
|
*
|
|
7
|
-
*
|
|
8
|
-
*
|
|
9
|
-
*
|
|
10
|
-
*
|
|
11
|
-
*
|
|
12
|
-
*
|
|
7
|
+
* 1. `tagEventHandlerSends` — tags event-handler arrow functions
|
|
8
|
+
* (`onClick`, `onInput`, …) whose body contains literal
|
|
9
|
+
* `<id>({type: 'X', …})` call sites. Wraps with
|
|
10
|
+
* `Object.assign(arrow, { __lluiVariants: ['X', …] })`. The
|
|
11
|
+
* runtime in `@llui/dom` `elements.ts` / `el-split.ts` reads the
|
|
12
|
+
* metadata at bind time and registers the variants on the active
|
|
13
|
+
* component instance for the lifetime of the binding's scope.
|
|
13
14
|
*
|
|
14
|
-
*
|
|
15
|
-
*
|
|
16
|
-
*
|
|
15
|
+
* 2. `injectScopeVariantRegistrations` — handles the
|
|
16
|
+
* dispatch-translation case that pass (1) can't follow:
|
|
17
|
+
* `<bag>.connect(get, sendFn, …)` where `sendFn` is a user-
|
|
18
|
+
* defined function that translates library Msgs into app Msgs via
|
|
19
|
+
* `dispatch({type: 'X', …})`. Static analysis of the library's
|
|
20
|
+
* internal onClick can't see across this hop. The pass detects the
|
|
21
|
+
* `*.connect(get, sendFn, …)` syntactic pattern, follows `sendFn`
|
|
22
|
+
* to its declaration, scans the body for literal dispatches, and
|
|
23
|
+
* inserts a runtime `__registerScopeVariants(['X', …])` call
|
|
24
|
+
* immediately before the connect call. Lifetime semantics fall
|
|
25
|
+
* out of the render context: the call's active scope is whatever
|
|
26
|
+
* `each(...)` / `branch(...)` / root scope happens to be live when
|
|
27
|
+
* the view evaluates that statement.
|
|
28
|
+
*
|
|
29
|
+
* Both passes are gated on `devMode || emitAgentMetadata` in
|
|
30
|
+
* `transform.ts`. Production bundles without agent integration get
|
|
31
|
+
* neither the per-handler `Object.assign` cost nor the registration
|
|
32
|
+
* statements.
|
|
33
|
+
*
|
|
34
|
+
* False positives are deliberate. The alternative — proving the
|
|
35
|
+
* callee resolves to the destructured `send` from a `View` bag, or
|
|
36
|
+
* tracing function calls across files — would require full scope
|
|
37
|
+
* tracking the compiler doesn't do. In practice the patterns
|
|
38
|
+
* `<id>({type:'X',…})` and `*.connect(get, fn, …)` are reliable
|
|
39
|
+
* shape-level signals; an extra entry in the live descriptor
|
|
40
|
+
* registry just means the agent sees one more "affordable variant"
|
|
41
|
+
* than necessary — never a wrong dispatch, never a runtime error.
|
|
42
|
+
*
|
|
43
|
+
* False negatives stay where they were: non-literal `type` values
|
|
44
|
+
* (`send({ type: nextStep })`), and dispatch translators bound via
|
|
45
|
+
* patterns other than `*.connect(get, fn, …)`. Apps that hit those
|
|
46
|
+
* cases declare `agentAffordances` on the component def — the
|
|
47
|
+
* documented escape hatch.
|
|
17
48
|
*
|
|
18
49
|
* @see agent spec §5.2, §12.2
|
|
50
|
+
* @see @llui/dom binding-descriptors.ts (runtime registry + helper)
|
|
19
51
|
*/
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
52
|
+
// ── Pass 1: dispatch-handler tagger (universal) ─────────────────────
|
|
53
|
+
/**
|
|
54
|
+
* Walks every `ArrowFunction` and `FunctionExpression` in the source
|
|
55
|
+
* and wraps any whose body contains literal `<id>({type:'X', …})`
|
|
56
|
+
* dispatches with `Object.assign(fn, {__lluiVariants: ['X', …]})`.
|
|
57
|
+
*
|
|
58
|
+
* The runtime (in `@llui/dom` `elements.ts` / `el-split.ts`) reads
|
|
59
|
+
* `__lluiVariants` from event-handler bindings only — so tags placed
|
|
60
|
+
* on functions in non-handler positions (a const declared but never
|
|
61
|
+
* bound, an arrow passed to `Array.filter`, a view function whose
|
|
62
|
+
* body has nested handlers with dispatches) are runtime-inert. The
|
|
63
|
+
* compiler tags generously; the runtime registers selectively.
|
|
64
|
+
*
|
|
65
|
+
* Universal scope means three concrete patterns all surface their
|
|
66
|
+
* variants without the app author having to think about it:
|
|
67
|
+
*
|
|
68
|
+
* 1. **Inline event-handler arrows** —
|
|
69
|
+
* `onClick: () => send({type:'X'})` (the original Pass 1 case).
|
|
70
|
+
* 2. **Const-bound translator functions** —
|
|
71
|
+
* `const sendMenu = (m) => dispatch({type:'Y'})` paired with
|
|
72
|
+
* `*.connect(get, sendMenu, …)` (the original Pass 3 case). The
|
|
73
|
+
* tag travels with the function reference; library connect impls
|
|
74
|
+
* use `tagSend(send, libVariants, fn)` to propagate it onto
|
|
75
|
+
* returned handlers.
|
|
76
|
+
* 3. **Positional-arg handlers** —
|
|
77
|
+
* `helper(label, () => send({type:'Z'}))` where `helper` is an
|
|
78
|
+
* app-defined wrapper like `navButton(label, onClick)` that
|
|
79
|
+
* eventually binds the function as an event listener. The arrow
|
|
80
|
+
* is still tagged at its declaration site, and the runtime reads
|
|
81
|
+
* the tag when the wrapper binds it.
|
|
82
|
+
*
|
|
83
|
+
* False positives are deliberate. The alternative — proving that a
|
|
84
|
+
* tagged arrow actually reaches an event-handler binding — would
|
|
85
|
+
* require cross-function, cross-file flow analysis the compiler
|
|
86
|
+
* doesn't do. In practice the cost of an over-tagged arrow is bytes,
|
|
87
|
+
* not behavior: the runtime never reads the tag from non-handler
|
|
88
|
+
* bindings.
|
|
89
|
+
*
|
|
90
|
+
* Pass 2's `collectLocalFns` resolves identifiers to their original
|
|
91
|
+
* arrow/function initializers; this pass replaces those initializers
|
|
92
|
+
* with `Object.assign(arrow, {…})` wrappers. Run Pass 2 BEFORE Pass 1
|
|
93
|
+
* so the resolver still sees raw arrows.
|
|
94
|
+
*
|
|
95
|
+
* Already-wrapped functions (CallExpressions, including user-applied
|
|
96
|
+
* `tagSend(...)` or this pass's own prior output) are skipped — the
|
|
97
|
+
* pass only fires on bare arrow/function expressions.
|
|
98
|
+
*/
|
|
99
|
+
export function tagDispatchHandlers(node, f) {
|
|
100
|
+
const transformer = (ctx) => {
|
|
101
|
+
const visit = (n) => {
|
|
102
|
+
if (ts.isArrowFunction(n) || ts.isFunctionExpression(n)) {
|
|
103
|
+
// Visit children first so nested arrows are tagged before
|
|
104
|
+
// their parents see them. The parent's
|
|
105
|
+
// `collectLiteralSendVariants` walks the original (unwrapped)
|
|
106
|
+
// body — the recursive collect doesn't descend into nested
|
|
107
|
+
// function bodies (that would cause an outer arrow to inherit
|
|
108
|
+
// every dispatch from every closure within it, which is too
|
|
109
|
+
// permissive).
|
|
110
|
+
const inner = ts.visitEachChild(n, visit, ctx);
|
|
111
|
+
const variants = collectLiteralSendVariants(inner.body);
|
|
112
|
+
if (variants.length > 0) {
|
|
113
|
+
return wrapWithVariants(inner, variants, f);
|
|
114
|
+
}
|
|
115
|
+
return inner;
|
|
116
|
+
}
|
|
117
|
+
return ts.visitEachChild(n, visit, ctx);
|
|
118
|
+
};
|
|
119
|
+
return (sf) => ts.visitEachChild(sf, visit, ctx);
|
|
120
|
+
};
|
|
121
|
+
const result = ts.transform(node, [transformer]);
|
|
122
|
+
const out = result.transformed[0];
|
|
123
|
+
result.dispose();
|
|
124
|
+
return out;
|
|
125
|
+
}
|
|
126
|
+
function wrapWithVariants(arrow, variants, f) {
|
|
127
|
+
return f.createCallExpression(f.createPropertyAccessExpression(f.createIdentifier('Object'), 'assign'), undefined, [
|
|
128
|
+
arrow,
|
|
129
|
+
f.createObjectLiteralExpression([
|
|
130
|
+
f.createPropertyAssignment('__lluiVariants', f.createArrayLiteralExpression(variants.map((v) => f.createStringLiteral(v)), false)),
|
|
131
|
+
], false),
|
|
132
|
+
]);
|
|
133
|
+
}
|
|
134
|
+
export function injectScopeVariantRegistrations(node, f) {
|
|
135
|
+
let injected = false;
|
|
136
|
+
const transformer = (ctx) => {
|
|
137
|
+
/**
|
|
138
|
+
* Tracks whether we're inside a function body. Top-level
|
|
139
|
+
* (module-scope) connect calls are skipped — there's no render
|
|
140
|
+
* context active when module code runs, so the registration
|
|
141
|
+
* would silently no-op anyway, and emitting it adds noise. Apps
|
|
142
|
+
* with module-scope translators (a single shared `parts` re-used
|
|
143
|
+
* across views) declare `agentAffordances` instead.
|
|
144
|
+
*/
|
|
145
|
+
let inFunction = 0;
|
|
146
|
+
function rewriteBlock(block) {
|
|
147
|
+
const stmts = block.statements;
|
|
148
|
+
const localFns = collectLocalFns(stmts);
|
|
149
|
+
const out = [];
|
|
150
|
+
for (const stmt of stmts) {
|
|
151
|
+
const visited = ts.visitNode(stmt, (n) => visitNode(n, localFns));
|
|
152
|
+
out.push(visited);
|
|
153
|
+
}
|
|
154
|
+
if (ts.isSourceFile(block)) {
|
|
155
|
+
return f.updateSourceFile(block, out);
|
|
156
|
+
}
|
|
157
|
+
return f.updateBlock(block, out);
|
|
33
158
|
}
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
159
|
+
function visitNode(n, localFns) {
|
|
160
|
+
if (ts.isBlock(n)) {
|
|
161
|
+
return rewriteBlock(n);
|
|
162
|
+
}
|
|
163
|
+
// Track function-body nesting so we know whether the current
|
|
164
|
+
// call site can plausibly run within a render context. Arrow
|
|
165
|
+
// functions and function expressions/declarations both qualify.
|
|
166
|
+
if (ts.isArrowFunction(n) ||
|
|
167
|
+
ts.isFunctionExpression(n) ||
|
|
168
|
+
ts.isFunctionDeclaration(n) ||
|
|
169
|
+
ts.isMethodDeclaration(n)) {
|
|
170
|
+
inFunction++;
|
|
171
|
+
const r = ts.visitEachChild(n, (c) => visitNode(c, localFns), ctx);
|
|
172
|
+
inFunction--;
|
|
173
|
+
return r;
|
|
174
|
+
}
|
|
175
|
+
if (ts.isCallExpression(n) && inFunction > 0 && isConnectCallShape(n)) {
|
|
176
|
+
const sendArg = n.arguments[1];
|
|
177
|
+
const sendFn = resolveSendFn(sendArg, localFns);
|
|
178
|
+
if (sendFn) {
|
|
179
|
+
const variants = collectLiteralSendVariants(sendFn.body);
|
|
180
|
+
if (variants.length > 0) {
|
|
181
|
+
// Replace the call expression with a comma expression:
|
|
182
|
+
// (__registerScopeVariants([...]), originalCall)
|
|
183
|
+
// The comma keeps the call's value position intact (so
|
|
184
|
+
// `const parts = popover.connect(...)` still binds the
|
|
185
|
+
// original return), and ensures the registration fires
|
|
186
|
+
// *before* the call returns. This positions correctly
|
|
187
|
+
// regardless of the surrounding function context: view
|
|
188
|
+
// body, render callback inside `each(...)`, or any
|
|
189
|
+
// nested helper called from within a view.
|
|
190
|
+
injected = true;
|
|
191
|
+
const inner = ts.visitEachChild(n, (c) => visitNode(c, localFns), ctx);
|
|
192
|
+
return f.createParenthesizedExpression(f.createBinaryExpression(emitRegisterCall(variants, f), f.createToken(ts.SyntaxKind.CommaToken), inner));
|
|
193
|
+
}
|
|
43
194
|
}
|
|
44
195
|
}
|
|
196
|
+
return ts.visitEachChild(n, (c) => visitNode(c, localFns), ctx);
|
|
45
197
|
}
|
|
46
|
-
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
198
|
+
return (sf) => rewriteBlock(sf);
|
|
199
|
+
};
|
|
200
|
+
const result = ts.transform(node, [transformer]);
|
|
201
|
+
const out = result.transformed[0];
|
|
202
|
+
result.dispose();
|
|
203
|
+
return { sf: out, injected };
|
|
204
|
+
}
|
|
205
|
+
/**
|
|
206
|
+
* Collect `const fn = (m) => { … }` / `const fn = function(m){ … }`
|
|
207
|
+
* declarations in `stmts` so an identifier passed to a connect call
|
|
208
|
+
* later in the same scope can resolve to its body. Conservative —
|
|
209
|
+
* only direct function-valued initializers count; aliasing
|
|
210
|
+
* (`const a = b`) is not followed.
|
|
211
|
+
*/
|
|
212
|
+
function collectLocalFns(stmts) {
|
|
213
|
+
const out = new Map();
|
|
214
|
+
for (const stmt of stmts) {
|
|
215
|
+
if (!ts.isVariableStatement(stmt))
|
|
216
|
+
continue;
|
|
217
|
+
for (const decl of stmt.declarationList.declarations) {
|
|
218
|
+
if (!ts.isIdentifier(decl.name))
|
|
57
219
|
continue;
|
|
58
|
-
const init =
|
|
59
|
-
if (ts.
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
return init.text;
|
|
220
|
+
const init = decl.initializer;
|
|
221
|
+
if (init && (ts.isArrowFunction(init) || ts.isFunctionExpression(init))) {
|
|
222
|
+
out.set(decl.name.text, init);
|
|
223
|
+
}
|
|
63
224
|
}
|
|
64
|
-
return null;
|
|
65
225
|
}
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
226
|
+
return out;
|
|
227
|
+
}
|
|
228
|
+
function isConnectCallShape(node) {
|
|
229
|
+
if (!ts.isPropertyAccessExpression(node.expression))
|
|
230
|
+
return false;
|
|
231
|
+
if (node.expression.name.text !== 'connect')
|
|
232
|
+
return false;
|
|
233
|
+
return node.arguments.length >= 2;
|
|
234
|
+
}
|
|
235
|
+
function resolveSendFn(arg, localFns) {
|
|
236
|
+
if (ts.isArrowFunction(arg) || ts.isFunctionExpression(arg))
|
|
237
|
+
return arg;
|
|
238
|
+
if (ts.isIdentifier(arg))
|
|
239
|
+
return localFns.get(arg.text) ?? null;
|
|
240
|
+
return null;
|
|
241
|
+
}
|
|
242
|
+
function emitRegisterCall(variants, f) {
|
|
243
|
+
return f.createCallExpression(f.createIdentifier('__registerScopeVariants'), undefined, [
|
|
244
|
+
f.createArrayLiteralExpression(variants.map((v) => f.createStringLiteral(v)), false),
|
|
245
|
+
]);
|
|
246
|
+
}
|
|
247
|
+
// ── Shared: literal-send variant collection ──────────────────────────
|
|
248
|
+
/**
|
|
249
|
+
* Walk `node`, collecting every literal type string from
|
|
250
|
+
* `<id>({ type: 'literal', … })` call sites. De-dupes while preserving
|
|
251
|
+
* first-seen order so the emitted array reads naturally for anyone
|
|
252
|
+
* inspecting the compiled output.
|
|
253
|
+
*
|
|
254
|
+
* The walk stops at nested function/arrow boundaries — every function
|
|
255
|
+
* "owns" its own dispatches, and the universal tagger visits each
|
|
256
|
+
* function separately. Without this guard, an outer arrow whose body
|
|
257
|
+
* includes a nested onClick arrow would be tagged with the inner
|
|
258
|
+
* arrow's variants too: a parent view tagged with every dispatch in
|
|
259
|
+
* every child handler. The runtime only reads the tag from event-
|
|
260
|
+
* handler bindings, so functionally that's harmless — but it bloats
|
|
261
|
+
* the wrapped output and obscures intent.
|
|
262
|
+
*/
|
|
263
|
+
function collectLiteralSendVariants(body) {
|
|
264
|
+
const seen = new Set();
|
|
265
|
+
const out = [];
|
|
266
|
+
function visit(n) {
|
|
267
|
+
// Stop at every nested function/arrow boundary — those functions
|
|
268
|
+
// own their own variants and are tagged independently. An outer
|
|
269
|
+
// arrow whose body contains `setTimeout(() => send(...), 100)`
|
|
270
|
+
// doesn't dispatch directly when invoked; the inner arrow does
|
|
271
|
+
// when the timer fires. Counting the inner's variants on the
|
|
272
|
+
// outer would be too permissive.
|
|
273
|
+
if (ts.isArrowFunction(n) ||
|
|
274
|
+
ts.isFunctionExpression(n) ||
|
|
275
|
+
ts.isFunctionDeclaration(n) ||
|
|
276
|
+
ts.isMethodDeclaration(n)) {
|
|
277
|
+
return;
|
|
278
|
+
}
|
|
279
|
+
if (ts.isCallExpression(n)) {
|
|
280
|
+
const callee = n.expression;
|
|
281
|
+
const first = n.arguments[0];
|
|
282
|
+
if (callee &&
|
|
283
|
+
ts.isIdentifier(callee) &&
|
|
284
|
+
isDispatcherName(callee.text) &&
|
|
285
|
+
first &&
|
|
286
|
+
ts.isObjectLiteralExpression(first)) {
|
|
287
|
+
const variant = readTypeLiteral(first);
|
|
288
|
+
if (variant !== null && !seen.has(variant)) {
|
|
289
|
+
seen.add(variant);
|
|
290
|
+
out.push(variant);
|
|
74
291
|
}
|
|
75
292
|
}
|
|
76
293
|
}
|
|
77
|
-
ts.forEachChild(
|
|
294
|
+
ts.forEachChild(n, visit);
|
|
78
295
|
}
|
|
79
|
-
|
|
296
|
+
visit(body);
|
|
80
297
|
return out;
|
|
81
298
|
}
|
|
299
|
+
/**
|
|
300
|
+
* Recognize a callee as a dispatcher by name. The convention in LLui
|
|
301
|
+
* apps is `send` (the framework-provided dispatch) or `dispatch` (a
|
|
302
|
+
* common app-level alias), plus `send*` / `dispatch*` prefixes for
|
|
303
|
+
* named translators (`sendMenu`, `dispatchMenu`).
|
|
304
|
+
*
|
|
305
|
+
* Without this filter, the tagger would treat element-helper calls
|
|
306
|
+
* like `button({type: 'button'})` and `input({type: 'email'})` as
|
|
307
|
+
* dispatch sites — they syntactically match `<Identifier>({type:
|
|
308
|
+
* literal})`, but they construct DOM, not Msgs. Phantom variants
|
|
309
|
+
* like `button` and `email` would then leak into the binding-
|
|
310
|
+
* descriptor registry and surface as agent affordances. Filtering
|
|
311
|
+
* by name keeps the convention narrow enough that false positives
|
|
312
|
+
* are nearly zero while leaving room for translator naming.
|
|
313
|
+
*
|
|
314
|
+
* Apps that use unconventional dispatcher names (`forward`,
|
|
315
|
+
* `tellMenu`) need to either rename to the convention or wrap their
|
|
316
|
+
* handlers with the runtime `tagSend`/`tagVariants` helpers
|
|
317
|
+
* explicitly — the same escape hatch library code already uses.
|
|
318
|
+
*/
|
|
319
|
+
function isDispatcherName(name) {
|
|
320
|
+
return /^(send|dispatch)/i.test(name);
|
|
321
|
+
}
|
|
322
|
+
function readTypeLiteral(obj) {
|
|
323
|
+
for (const prop of obj.properties) {
|
|
324
|
+
if (!ts.isPropertyAssignment(prop))
|
|
325
|
+
continue;
|
|
326
|
+
if (!prop.name)
|
|
327
|
+
continue;
|
|
328
|
+
const nameOk = (ts.isIdentifier(prop.name) && prop.name.text === 'type') ||
|
|
329
|
+
(ts.isStringLiteral(prop.name) && prop.name.text === 'type');
|
|
330
|
+
if (!nameOk)
|
|
331
|
+
continue;
|
|
332
|
+
const init = prop.initializer;
|
|
333
|
+
if (ts.isStringLiteral(init))
|
|
334
|
+
return init.text;
|
|
335
|
+
if (ts.isNoSubstitutionTemplateLiteral(init))
|
|
336
|
+
return init.text;
|
|
337
|
+
}
|
|
338
|
+
return null;
|
|
339
|
+
}
|
|
82
340
|
//# sourceMappingURL=binding-descriptors.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"binding-descriptors.js","sourceRoot":"","sources":["../src/binding-descriptors.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,YAAY,CAAA;AAM3B;;;;;;;;;;;;;;;;;GAiBG;AACH,MAAM,UAAU,yBAAyB,CAAC,MAAc;IACtD,MAAM,EAAE,GAAG,EAAE,CAAC,gBAAgB,CAAC,SAAS,EAAE,MAAM,EAAE,EAAE,CAAC,YAAY,CAAC,MAAM,EAAE,IAAI,CAAC,CAAA;IAC/E,MAAM,GAAG,GAAwB,EAAE,CAAA;IAEnC,SAAS,oBAAoB,CAAC,MAAkC;QAC9D,KAAK,MAAM,IAAI,IAAI,MAAM,CAAC,UAAU,EAAE,CAAC;YACrC,IAAI,CAAC,EAAE,CAAC,oBAAoB,CAAC,IAAI,CAAC;gBAAE,SAAQ;YAC5C,IAAI,CAAC,IAAI,CAAC,IAAI,IAAI,CAAC,EAAE,CAAC,YAAY,CAAC,IAAI,CAAC,IAAI,CAAC,IAAI,IAAI,CAAC,IAAI,CAAC,IAAI,KAAK,MAAM;gBAAE,SAAQ;YACpF,MAAM,QAAQ,GAAG,IAAI,CAAC,WAAW,CAAA;YACjC,IAAI,CAAC,EAAE,CAAC,eAAe,CAAC,QAAQ,CAAC,IAAI,CAAC,EAAE,CAAC,oBAAoB,CAAC,QAAQ,CAA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AI,CAAC,WAAW,CAAA;YAC7B,IAAI,EAAE,CAAC,eAAe,CAAC,IAAI,CAAC;gBAAE,OAAO,IAAI,CAAC,IAAI,CAAA;YAC9C,IAAI,EAAE,CAAC,+BAA+B,CAAC,IAAI,CAAC;gBAAE,OAAO,IAAI,CAAC,IAAI,CAAA;QAChE,CAAC;QACD,OAAO,IAAI,CAAA;IACb,CAAC;IAED,SAAS,aAAa,CAAC,IAAa;QAClC,IAAI,EAAE,CAAC,gBAAgB,CAAC,IAAI,CAAC,EAAE,CAAC;YAC9B,MAAM,MAAM,GAAG,IAAI,CAAC,UAAU,CAAA;YAC9B,MAAM,UAAU,GAAG,EAAE,CAAC,YAAY,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,CAAC,IAAI,CAAA;YAC/D,IAAI,UAAU,KAAK,WAAW,IAAI,IAAI,CAAC,SAAS,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;gBAC5D,MAAM,QAAQ,GAAG,IAAI,CAAC,SAAS,CAAC,CAAC,CAAC,CAAA;gBAClC,IAAI,QAAQ,IAAI,EAAE,CAAC,yBAAyB,CAAC,QAAQ,CAAC,EAAE,CAAC;oBACvD,oBAAoB,CAAC,QAAQ,CAAC,CAAA;gBAChC,CAAC;YACH,CAAC;QACH,CAAC;QACD,EAAE,CAAC,YAAY,CAAC,IAAI,EAAE,aAAa,CAAC,CAAA;IACtC,CAAC;IAED,EAAE,CAAC,YAAY,CAAC,EAAE,EAAE,aAAa,CAAC,CAAA;IAClC,OAAO,GAAG,CAAA;AACZ,CAAC","sourcesContent":["import ts from 'typescript'\n\nexport type BindingDescriptor = {\n variant: string\n}\n\n/**\n * Walk the `view` arrow function of every top-level `component({...})` call\n * in the source and collect every `send({type: '...'})` call site's variant\n * literal. Returns them in encounter order.\n *\n * False positives: any call of the form `identifier({ type: 'x', ... })` —\n * we don't verify the callee resolves to the destructured `send` from the\n * view argument, because that level of scope tracking is beyond the budget\n * of this MVP extractor. Apps that call other identifiers with similarly\n * shaped literals would see those in the output. In practice, the pattern\n * is uncommon enough that false positives are rare.\n *\n * Missing: non-literal `type` values (e.g. `send({type: nextStep})`) are\n * skipped. This is the correct behavior — we can only record statically-\n * known variants.\n *\n * @see agent spec §5.2, §12.2\n */\nexport function extractBindingDescriptors(source: string): BindingDescriptor[] {\n const sf = ts.createSourceFile('view.ts', source, ts.ScriptTarget.Latest, true)\n const out: BindingDescriptor[] = []\n\n function visitComponentConfig(config: ts.ObjectLiteralExpression): void {\n for (const prop of config.properties) {\n if (!ts.isPropertyAssignment(prop)) continue\n if (!prop.name || !ts.isIdentifier(prop.name) || prop.name.text !== 'view') continue\n const viewExpr = prop.initializer\n if (!ts.isArrowFunction(viewExpr) && !ts.isFunctionExpression(viewExpr)) continue\n collectSendCalls(viewExpr.body)\n }\n }\n\n function collectSendCalls(node: ts.Node): void {\n if (ts.isCallExpression(node)) {\n const callee = node.expression\n const first = node.arguments[0]\n if (callee && ts.isIdentifier(callee) && first && ts.isObjectLiteralExpression(first)) {\n const variant = readTypeLiteral(first)\n if (variant !== null) {\n out.push({ variant })\n }\n }\n }\n ts.forEachChild(node, collectSendCalls)\n }\n\n function readTypeLiteral(obj: ts.ObjectLiteralExpression): string | null {\n for (const prop of obj.properties) {\n if (!ts.isPropertyAssignment(prop)) continue\n if (!prop.name) continue\n const nameOk =\n (ts.isIdentifier(prop.name) && prop.name.text === 'type') ||\n (ts.isStringLiteral(prop.name) && prop.name.text === 'type')\n if (!nameOk) continue\n const init = prop.initializer\n if (ts.isStringLiteral(init)) return init.text\n if (ts.isNoSubstitutionTemplateLiteral(init)) return init.text\n }\n return null\n }\n\n function visitTopLevel(node: ts.Node): void {\n if (ts.isCallExpression(node)) {\n const callee = node.expression\n const calleeName = ts.isIdentifier(callee) ? callee.text : null\n if (calleeName === 'component' && node.arguments.length > 0) {\n const firstArg = node.arguments[0]\n if (firstArg && ts.isObjectLiteralExpression(firstArg)) {\n visitComponentConfig(firstArg)\n }\n }\n }\n ts.forEachChild(node, visitTopLevel)\n }\n\n ts.forEachChild(sf, visitTopLevel)\n return out\n}\n"]}
|
|
1
|
+
{"version":3,"file":"binding-descriptors.js","sourceRoot":"","sources":["../src/binding-descriptors.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,YAAY,CAAA;AAE3B;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAiDG;AAEH,uEAAuE;AAEvE;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA6CG;AACH,MAAM,UAAU,mBAAmB,CAAC,IAAmB,EAAE,CAAiB;IACxE,MAAM,WAAW,GAAyC,CAAC,GAAG,EAAE,EAAE;QAChE,MAAM,KAAK,GAAe,CAAC,CAAC,EAAE,EAAE;YAC9B,IAAI,EAAE,CAAC,eAAe,CAAC,CAAC,CAAC,IAAI,EAAE,CAAC,oBAAoB,CAAC,CAAC,CAAC,EAAE,CAAC;gBACxD,0DAA0D;gBAC1D,uCAAuC;gBACvC,8DAA8D;gBAC9D,2DAA2D;gBAC3D,8DAA8D;gBAC9D,4DAA4D;gBAC5D,eAAe;gBACf,MAAM,KAAK,GAAG,EAAE,CAAC,cAAc,CAAC,CAAC,EAAE,KAAK,EAAE,GAAG,CAA6C,CAAA;gBAC1F,MAAM,QAAQ,GAAG,0BAA0B,CAAC,KAAK,CAAC,IAAI,CAAC,CAAA;gBACvD,IAAI,QAAQ,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;oBACxB,OAAO,gBAAgB,CAAC,KAAK,EAAE,QAAQ,EAAE,CAAC,CAAC,CAAA;gBAC7C,CAAC;gBACD,OAAO,KAAK,CAAA;YACd,CAAC;YACD,OAAO,EAAE,CAAC,cAAc,CAAC,CAAC,EAAE,KAAK,EAAE,GAAG,CAAC,CAAA;QACzC,CAAC,CAAA;QACD,OAAO,CAAC,EAAE,EAAE,EAAE,CAAC,EAAE,CAAC,cAAc,CAAC,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YAAE,OAAO,IAAI,CAAC,IAAI,CAAA;QAC9C,IAAI,EAAE,CAAC,+BAA+B,CAAC,IAAI,CAAC;YAAE,OAAO,IAAI,CAAC,IAAI,CAAA;IAChE,CAAC;IACD,OAAO,IAAI,CAAA;AACb,CAAC","sourcesContent":["import ts from 'typescript'\n\n/**\n * Compiler passes that surface the Msg variants currently dispatchable\n * from rendered UI to the agent layer's `list_actions`. Two passes\n * cover two distinct dispatch shapes:\n *\n * 1. `tagEventHandlerSends` — tags event-handler arrow functions\n * (`onClick`, `onInput`, …) whose body contains literal\n * `<id>({type: 'X', …})` call sites. Wraps with\n * `Object.assign(arrow, { __lluiVariants: ['X', …] })`. The\n * runtime in `@llui/dom` `elements.ts` / `el-split.ts` reads the\n * metadata at bind time and registers the variants on the active\n * component instance for the lifetime of the binding's scope.\n *\n * 2. `injectScopeVariantRegistrations` — handles the\n * dispatch-translation case that pass (1) can't follow:\n * `<bag>.connect(get, sendFn, …)` where `sendFn` is a user-\n * defined function that translates library Msgs into app Msgs via\n * `dispatch({type: 'X', …})`. Static analysis of the library's\n * internal onClick can't see across this hop. The pass detects the\n * `*.connect(get, sendFn, …)` syntactic pattern, follows `sendFn`\n * to its declaration, scans the body for literal dispatches, and\n * inserts a runtime `__registerScopeVariants(['X', …])` call\n * immediately before the connect call. Lifetime semantics fall\n * out of the render context: the call's active scope is whatever\n * `each(...)` / `branch(...)` / root scope happens to be live when\n * the view evaluates that statement.\n *\n * Both passes are gated on `devMode || emitAgentMetadata` in\n * `transform.ts`. Production bundles without agent integration get\n * neither the per-handler `Object.assign` cost nor the registration\n * statements.\n *\n * False positives are deliberate. The alternative — proving the\n * callee resolves to the destructured `send` from a `View` bag, or\n * tracing function calls across files — would require full scope\n * tracking the compiler doesn't do. In practice the patterns\n * `<id>({type:'X',…})` and `*.connect(get, fn, …)` are reliable\n * shape-level signals; an extra entry in the live descriptor\n * registry just means the agent sees one more \"affordable variant\"\n * than necessary — never a wrong dispatch, never a runtime error.\n *\n * False negatives stay where they were: non-literal `type` values\n * (`send({ type: nextStep })`), and dispatch translators bound via\n * patterns other than `*.connect(get, fn, …)`. Apps that hit those\n * cases declare `agentAffordances` on the component def — the\n * documented escape hatch.\n *\n * @see agent spec §5.2, §12.2\n * @see @llui/dom binding-descriptors.ts (runtime registry + helper)\n */\n\n// ── Pass 1: dispatch-handler tagger (universal) ─────────────────────\n\n/**\n * Walks every `ArrowFunction` and `FunctionExpression` in the source\n * and wraps any whose body contains literal `<id>({type:'X', …})`\n * dispatches with `Object.assign(fn, {__lluiVariants: ['X', …]})`.\n *\n * The runtime (in `@llui/dom` `elements.ts` / `el-split.ts`) reads\n * `__lluiVariants` from event-handler bindings only — so tags placed\n * on functions in non-handler positions (a const declared but never\n * bound, an arrow passed to `Array.filter`, a view function whose\n * body has nested handlers with dispatches) are runtime-inert. The\n * compiler tags generously; the runtime registers selectively.\n *\n * Universal scope means three concrete patterns all surface their\n * variants without the app author having to think about it:\n *\n * 1. **Inline event-handler arrows** —\n * `onClick: () => send({type:'X'})` (the original Pass 1 case).\n * 2. **Const-bound translator functions** —\n * `const sendMenu = (m) => dispatch({type:'Y'})` paired with\n * `*.connect(get, sendMenu, …)` (the original Pass 3 case). The\n * tag travels with the function reference; library connect impls\n * use `tagSend(send, libVariants, fn)` to propagate it onto\n * returned handlers.\n * 3. **Positional-arg handlers** —\n * `helper(label, () => send({type:'Z'}))` where `helper` is an\n * app-defined wrapper like `navButton(label, onClick)` that\n * eventually binds the function as an event listener. The arrow\n * is still tagged at its declaration site, and the runtime reads\n * the tag when the wrapper binds it.\n *\n * False positives are deliberate. The alternative — proving that a\n * tagged arrow actually reaches an event-handler binding — would\n * require cross-function, cross-file flow analysis the compiler\n * doesn't do. In practice the cost of an over-tagged arrow is bytes,\n * not behavior: the runtime never reads the tag from non-handler\n * bindings.\n *\n * Pass 2's `collectLocalFns` resolves identifiers to their original\n * arrow/function initializers; this pass replaces those initializers\n * with `Object.assign(arrow, {…})` wrappers. Run Pass 2 BEFORE Pass 1\n * so the resolver still sees raw arrows.\n *\n * Already-wrapped functions (CallExpressions, including user-applied\n * `tagSend(...)` or this pass's own prior output) are skipped — the\n * pass only fires on bare arrow/function expressions.\n */\nexport function tagDispatchHandlers(node: ts.SourceFile, f: ts.NodeFactory): ts.SourceFile {\n const transformer: ts.TransformerFactory<ts.SourceFile> = (ctx) => {\n const visit: ts.Visitor = (n) => {\n if (ts.isArrowFunction(n) || ts.isFunctionExpression(n)) {\n // Visit children first so nested arrows are tagged before\n // their parents see them. The parent's\n // `collectLiteralSendVariants` walks the original (unwrapped)\n // body — the recursive collect doesn't descend into nested\n // function bodies (that would cause an outer arrow to inherit\n // every dispatch from every closure within it, which is too\n // permissive).\n const inner = ts.visitEachChild(n, visit, ctx) as ts.ArrowFunction | ts.FunctionExpression\n const variants = collectLiteralSendVariants(inner.body)\n if (variants.length > 0) {\n return wrapWithVariants(inner, variants, f)\n }\n return inner\n }\n return ts.visitEachChild(n, visit, ctx)\n }\n return (sf) => ts.visitEachChild(sf, visit, ctx) as ts.SourceFile\n }\n const result = ts.transform(node, [transformer])\n const out = result.transformed[0] as ts.SourceFile\n result.dispose()\n return out\n}\n\nfunction wrapWithVariants(\n arrow: ts.ArrowFunction | ts.FunctionExpression,\n variants: readonly string[],\n f: ts.NodeFactory,\n): ts.CallExpression {\n return f.createCallExpression(\n f.createPropertyAccessExpression(f.createIdentifier('Object'), 'assign'),\n undefined,\n [\n arrow,\n f.createObjectLiteralExpression(\n [\n f.createPropertyAssignment(\n '__lluiVariants',\n f.createArrayLiteralExpression(\n variants.map((v) => f.createStringLiteral(v)),\n false,\n ),\n ),\n ],\n false,\n ),\n ],\n )\n}\n\n// ── Pass 2: connect-pattern registration injector ────────────────\n\nexport interface InjectResult {\n sf: ts.SourceFile\n /** True when at least one `__registerScopeVariants(...)` call was inserted. */\n injected: boolean\n}\n\nexport function injectScopeVariantRegistrations(\n node: ts.SourceFile,\n f: ts.NodeFactory,\n): InjectResult {\n let injected = false\n\n const transformer: ts.TransformerFactory<ts.SourceFile> = (ctx) => {\n /**\n * Tracks whether we're inside a function body. Top-level\n * (module-scope) connect calls are skipped — there's no render\n * context active when module code runs, so the registration\n * would silently no-op anyway, and emitting it adds noise. Apps\n * with module-scope translators (a single shared `parts` re-used\n * across views) declare `agentAffordances` instead.\n */\n let inFunction = 0\n\n function rewriteBlock<B extends ts.Block | ts.SourceFile>(block: B): B {\n const stmts = (block as ts.Block | ts.SourceFile).statements\n const localFns = collectLocalFns(stmts)\n const out: ts.Statement[] = []\n for (const stmt of stmts) {\n const visited = ts.visitNode(stmt, (n) => visitNode(n, localFns)) as ts.Statement\n out.push(visited)\n }\n if (ts.isSourceFile(block)) {\n return f.updateSourceFile(block, out) as B\n }\n return f.updateBlock(block, out) as B\n }\n\n function visitNode(\n n: ts.Node,\n localFns: Map<string, ts.ArrowFunction | ts.FunctionExpression>,\n ): ts.Node {\n if (ts.isBlock(n)) {\n return rewriteBlock(n)\n }\n // Track function-body nesting so we know whether the current\n // call site can plausibly run within a render context. Arrow\n // functions and function expressions/declarations both qualify.\n if (\n ts.isArrowFunction(n) ||\n ts.isFunctionExpression(n) ||\n ts.isFunctionDeclaration(n) ||\n ts.isMethodDeclaration(n)\n ) {\n inFunction++\n const r = ts.visitEachChild(n, (c) => visitNode(c, localFns), ctx)\n inFunction--\n return r\n }\n if (ts.isCallExpression(n) && inFunction > 0 && isConnectCallShape(n)) {\n const sendArg = n.arguments[1]!\n const sendFn = resolveSendFn(sendArg, localFns)\n if (sendFn) {\n const variants = collectLiteralSendVariants(sendFn.body)\n if (variants.length > 0) {\n // Replace the call expression with a comma expression:\n // (__registerScopeVariants([...]), originalCall)\n // The comma keeps the call's value position intact (so\n // `const parts = popover.connect(...)` still binds the\n // original return), and ensures the registration fires\n // *before* the call returns. This positions correctly\n // regardless of the surrounding function context: view\n // body, render callback inside `each(...)`, or any\n // nested helper called from within a view.\n injected = true\n const inner = ts.visitEachChild(\n n,\n (c) => visitNode(c, localFns),\n ctx,\n ) as ts.CallExpression\n return f.createParenthesizedExpression(\n f.createBinaryExpression(\n emitRegisterCall(variants, f),\n f.createToken(ts.SyntaxKind.CommaToken),\n inner,\n ),\n )\n }\n }\n }\n return ts.visitEachChild(n, (c) => visitNode(c, localFns), ctx)\n }\n\n return (sf) => rewriteBlock(sf)\n }\n\n const result = ts.transform(node, [transformer])\n const out = result.transformed[0] as ts.SourceFile\n result.dispose()\n return { sf: out, injected }\n}\n\n/**\n * Collect `const fn = (m) => { … }` / `const fn = function(m){ … }`\n * declarations in `stmts` so an identifier passed to a connect call\n * later in the same scope can resolve to its body. Conservative —\n * only direct function-valued initializers count; aliasing\n * (`const a = b`) is not followed.\n */\nfunction collectLocalFns(\n stmts: ts.NodeArray<ts.Statement>,\n): Map<string, ts.ArrowFunction | ts.FunctionExpression> {\n const out = new Map<string, ts.ArrowFunction | ts.FunctionExpression>()\n for (const stmt of stmts) {\n if (!ts.isVariableStatement(stmt)) continue\n for (const decl of stmt.declarationList.declarations) {\n if (!ts.isIdentifier(decl.name)) continue\n const init = decl.initializer\n if (init && (ts.isArrowFunction(init) || ts.isFunctionExpression(init))) {\n out.set(decl.name.text, init)\n }\n }\n }\n return out\n}\n\nfunction isConnectCallShape(node: ts.CallExpression): boolean {\n if (!ts.isPropertyAccessExpression(node.expression)) return false\n if (node.expression.name.text !== 'connect') return false\n return node.arguments.length >= 2\n}\n\nfunction resolveSendFn(\n arg: ts.Expression,\n localFns: Map<string, ts.ArrowFunction | ts.FunctionExpression>,\n): ts.ArrowFunction | ts.FunctionExpression | null {\n if (ts.isArrowFunction(arg) || ts.isFunctionExpression(arg)) return arg\n if (ts.isIdentifier(arg)) return localFns.get(arg.text) ?? null\n return null\n}\n\nfunction emitRegisterCall(variants: readonly string[], f: ts.NodeFactory): ts.CallExpression {\n return f.createCallExpression(f.createIdentifier('__registerScopeVariants'), undefined, [\n f.createArrayLiteralExpression(\n variants.map((v) => f.createStringLiteral(v)),\n false,\n ),\n ])\n}\n\n// ── Shared: literal-send variant collection ──────────────────────────\n\n/**\n * Walk `node`, collecting every literal type string from\n * `<id>({ type: 'literal', … })` call sites. De-dupes while preserving\n * first-seen order so the emitted array reads naturally for anyone\n * inspecting the compiled output.\n *\n * The walk stops at nested function/arrow boundaries — every function\n * \"owns\" its own dispatches, and the universal tagger visits each\n * function separately. Without this guard, an outer arrow whose body\n * includes a nested onClick arrow would be tagged with the inner\n * arrow's variants too: a parent view tagged with every dispatch in\n * every child handler. The runtime only reads the tag from event-\n * handler bindings, so functionally that's harmless — but it bloats\n * the wrapped output and obscures intent.\n */\nfunction collectLiteralSendVariants(body: ts.Node): string[] {\n const seen = new Set<string>()\n const out: string[] = []\n function visit(n: ts.Node): void {\n // Stop at every nested function/arrow boundary — those functions\n // own their own variants and are tagged independently. An outer\n // arrow whose body contains `setTimeout(() => send(...), 100)`\n // doesn't dispatch directly when invoked; the inner arrow does\n // when the timer fires. Counting the inner's variants on the\n // outer would be too permissive.\n if (\n ts.isArrowFunction(n) ||\n ts.isFunctionExpression(n) ||\n ts.isFunctionDeclaration(n) ||\n ts.isMethodDeclaration(n)\n ) {\n return\n }\n if (ts.isCallExpression(n)) {\n const callee = n.expression\n const first = n.arguments[0]\n if (\n callee &&\n ts.isIdentifier(callee) &&\n isDispatcherName(callee.text) &&\n first &&\n ts.isObjectLiteralExpression(first)\n ) {\n const variant = readTypeLiteral(first)\n if (variant !== null && !seen.has(variant)) {\n seen.add(variant)\n out.push(variant)\n }\n }\n }\n ts.forEachChild(n, visit)\n }\n visit(body)\n return out\n}\n\n/**\n * Recognize a callee as a dispatcher by name. The convention in LLui\n * apps is `send` (the framework-provided dispatch) or `dispatch` (a\n * common app-level alias), plus `send*` / `dispatch*` prefixes for\n * named translators (`sendMenu`, `dispatchMenu`).\n *\n * Without this filter, the tagger would treat element-helper calls\n * like `button({type: 'button'})` and `input({type: 'email'})` as\n * dispatch sites — they syntactically match `<Identifier>({type:\n * literal})`, but they construct DOM, not Msgs. Phantom variants\n * like `button` and `email` would then leak into the binding-\n * descriptor registry and surface as agent affordances. Filtering\n * by name keeps the convention narrow enough that false positives\n * are nearly zero while leaving room for translator naming.\n *\n * Apps that use unconventional dispatcher names (`forward`,\n * `tellMenu`) need to either rename to the convention or wrap their\n * handlers with the runtime `tagSend`/`tagVariants` helpers\n * explicitly — the same escape hatch library code already uses.\n */\nfunction isDispatcherName(name: string): boolean {\n return /^(send|dispatch)/i.test(name)\n}\n\nfunction readTypeLiteral(obj: ts.ObjectLiteralExpression): string | null {\n for (const prop of obj.properties) {\n if (!ts.isPropertyAssignment(prop)) continue\n if (!prop.name) continue\n const nameOk =\n (ts.isIdentifier(prop.name) && prop.name.text === 'type') ||\n (ts.isStringLiteral(prop.name) && prop.name.text === 'type')\n if (!nameOk) continue\n const init = prop.initializer\n if (ts.isStringLiteral(init)) return init.text\n if (ts.isNoSubstitutionTemplateLiteral(init)) return init.text\n }\n return null\n}\n"]}
|