@barefootjs/jsx 0.35.5 → 0.35.6
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/dist/analyzer-context.d.ts +5 -4
- package/dist/analyzer-context.d.ts.map +1 -1
- package/dist/analyzer.d.ts.map +1 -1
- package/dist/errors.d.ts +0 -1
- package/dist/errors.d.ts.map +1 -1
- package/dist/index.js +6711 -6471
- package/dist/ir-to-client-js/compute-prop-usage.d.ts +6 -0
- package/dist/ir-to-client-js/compute-prop-usage.d.ts.map +1 -1
- package/dist/ir-to-client-js/control-flow/plan/build-event-delegation.d.ts +3 -2
- package/dist/ir-to-client-js/control-flow/plan/build-event-delegation.d.ts.map +1 -1
- package/dist/ir-to-client-js/control-flow/plan/build-loop.d.ts +12 -2
- package/dist/ir-to-client-js/control-flow/plan/build-loop.d.ts.map +1 -1
- package/dist/ir-to-client-js/control-flow/plan/event-collision.d.ts +40 -0
- package/dist/ir-to-client-js/control-flow/plan/event-collision.d.ts.map +1 -0
- package/dist/ir-to-client-js/control-flow/plan/event-delegation.d.ts +13 -0
- package/dist/ir-to-client-js/control-flow/plan/event-delegation.d.ts.map +1 -1
- package/dist/ir-to-client-js/control-flow/plan/loop-delegation-index.d.ts +45 -0
- package/dist/ir-to-client-js/control-flow/plan/loop-delegation-index.d.ts.map +1 -0
- package/dist/ir-to-client-js/control-flow/plan/types.d.ts +2 -0
- package/dist/ir-to-client-js/control-flow/plan/types.d.ts.map +1 -1
- package/dist/ir-to-client-js/control-flow/stringify/event-delegation.d.ts +12 -0
- package/dist/ir-to-client-js/control-flow/stringify/event-delegation.d.ts.map +1 -1
- package/dist/ir-to-client-js/control-flow.d.ts +2 -1
- package/dist/ir-to-client-js/control-flow.d.ts.map +1 -1
- package/dist/ir-to-client-js/emit-reactive.d.ts +7 -3
- package/dist/ir-to-client-js/emit-reactive.d.ts.map +1 -1
- package/dist/ir-to-client-js/generate-init.d.ts.map +1 -1
- package/dist/ir-to-client-js/index.d.ts +1 -0
- package/dist/ir-to-client-js/index.d.ts.map +1 -1
- package/dist/ir-to-client-js/phases/event-handlers.d.ts +9 -1
- package/dist/ir-to-client-js/phases/event-handlers.d.ts.map +1 -1
- package/dist/ir-to-client-js/phases/props-event-handlers.d.ts +5 -0
- package/dist/ir-to-client-js/phases/props-event-handlers.d.ts.map +1 -1
- package/dist/ir-to-client-js/phases/props-extraction.d.ts +15 -13
- package/dist/ir-to-client-js/phases/props-extraction.d.ts.map +1 -1
- package/dist/ir-to-client-js/phases.d.ts +12 -1
- package/dist/ir-to-client-js/phases.d.ts.map +1 -1
- package/dist/ir-to-client-js/plan/build-declaration-emit.d.ts.map +1 -1
- package/dist/ir-to-client-js/prune-unused-prop-extractions.d.ts +50 -0
- package/dist/ir-to-client-js/prune-unused-prop-extractions.d.ts.map +1 -1
- package/dist/ir-to-client-js/rewrite-destructured-props.d.ts +29 -0
- package/dist/ir-to-client-js/rewrite-destructured-props.d.ts.map +1 -0
- package/dist/ir-to-client-js/utils.d.ts +9 -0
- package/dist/ir-to-client-js/utils.d.ts.map +1 -1
- package/dist/jsx-to-ir.d.ts.map +1 -1
- package/dist/prop-rewrite.d.ts.map +1 -1
- package/dist/props-binding.d.ts +112 -1
- package/dist/props-binding.d.ts.map +1 -1
- package/dist/ssr-defaults.d.ts.map +1 -1
- package/dist/static-literal.d.ts +20 -8
- package/dist/static-literal.d.ts.map +1 -1
- package/dist/types.d.ts +7 -0
- package/dist/types.d.ts.map +1 -1
- package/package.json +2 -2
- package/src/__tests__/__snapshots__/doc-examples.test.ts.snap +31 -35
- package/src/__tests__/aliased-destructured-prop-csr.test.ts +19 -7
- package/src/__tests__/body-destructured-prop-default-propsparams.test.ts +153 -0
- package/src/__tests__/body-destructured-props-live.test.ts +245 -0
- package/src/__tests__/client-js-generation.test.ts +11 -4
- package/src/__tests__/client-only-date-lowering.test.ts +5 -2
- package/src/__tests__/destructured-from-props-localconst-value.test.ts +17 -6
- package/src/__tests__/destructured-props-live.test.ts +167 -0
- package/src/__tests__/doc-examples.test.ts +2 -4
- package/src/__tests__/event-delegation-container-collision-2930.test.ts +197 -0
- package/src/__tests__/format-error.test.ts +5 -3
- package/src/__tests__/live-only-props.test.ts +160 -0
- package/src/__tests__/prop-references.test.ts +26 -17
- package/src/__tests__/props-destructuring.test.ts +6 -275
- package/src/__tests__/prune-unused-prop-extractions.test.ts +11 -5
- package/src/__tests__/signal-getter-not-called.test.ts +5 -0
- package/src/__tests__/ssr-defaults.test.ts +83 -0
- package/src/__tests__/staged-ir/02-shadow-guards.test.ts +12 -5
- package/src/__tests__/static-literal.test.ts +39 -4
- package/src/analyzer-context.ts +8 -7
- package/src/analyzer.ts +76 -84
- package/src/errors.ts +7 -8
- package/src/ir-to-client-js/compute-prop-usage.ts +19 -0
- package/src/ir-to-client-js/control-flow/plan/build-event-delegation.ts +5 -0
- package/src/ir-to-client-js/control-flow/plan/build-loop.ts +27 -11
- package/src/ir-to-client-js/control-flow/plan/event-collision.ts +55 -0
- package/src/ir-to-client-js/control-flow/plan/event-delegation.ts +13 -0
- package/src/ir-to-client-js/control-flow/plan/loop-delegation-index.ts +79 -0
- package/src/ir-to-client-js/control-flow/plan/types.ts +2 -0
- package/src/ir-to-client-js/control-flow/stringify/event-delegation.ts +50 -10
- package/src/ir-to-client-js/control-flow.ts +68 -31
- package/src/ir-to-client-js/emit-reactive.ts +12 -8
- package/src/ir-to-client-js/generate-init.ts +6 -0
- package/src/ir-to-client-js/index.ts +45 -3
- package/src/ir-to-client-js/phases/event-handlers.ts +10 -0
- package/src/ir-to-client-js/phases/props-event-handlers.ts +6 -0
- package/src/ir-to-client-js/phases/props-extraction.ts +29 -48
- package/src/ir-to-client-js/phases.ts +17 -3
- package/src/ir-to-client-js/plan/build-declaration-emit.ts +6 -5
- package/src/ir-to-client-js/prune-unused-prop-extractions.ts +31 -10
- package/src/ir-to-client-js/rewrite-destructured-props.ts +210 -0
- package/src/ir-to-client-js/utils.ts +14 -0
- package/src/jsx-to-ir.ts +96 -48
- package/src/prop-rewrite.ts +11 -0
- package/src/props-binding.ts +171 -6
- package/src/ssr-defaults.ts +10 -5
- package/src/static-literal.ts +21 -9
- package/src/types.ts +7 -0
|
@@ -0,0 +1,210 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Live-read rewrite for destructured props.
|
|
3
|
+
*
|
|
4
|
+
* A destructured name can appear bare in a dozen raw-captured text shapes
|
|
5
|
+
* (effect bodies, memo computations, signal initials, handler bodies,
|
|
6
|
+
* `mapArray` row bodies, conditional thunks, …), so rewriting it per
|
|
7
|
+
* emitter would mean N places to keep in sync; the single door is the
|
|
8
|
+
* joined init body, same precedent as `rewritePropsObjectRef`
|
|
9
|
+
* (`rewrite-props-object.ts`) one step earlier in the same pipeline.
|
|
10
|
+
*
|
|
11
|
+
* Two shapes reach this door, dispatched on `ctx.propsObjectName`:
|
|
12
|
+
*
|
|
13
|
+
* - PARAMETER destructuring (`function Child({ value })`,
|
|
14
|
+
* `propsObjectName === null`) — `rewriteParamDestructuredReads`, Move
|
|
15
|
+
* B's original mechanism (#2932).
|
|
16
|
+
* - a BODY destructure or bare-member alias off the whole props object
|
|
17
|
+
* (`function Child(props) { const { value } = props; ... }`,
|
|
18
|
+
* `propsObjectName` set) — `rewriteBodyAliasReads` (#2934). Move B left
|
|
19
|
+
* this branch a no-op: `rewritePropsObjectRef` (the pass immediately
|
|
20
|
+
* before this one) already turns `props.value` into `_p.value`
|
|
21
|
+
* wherever the SOURCE wrote it as a member access, but a body
|
|
22
|
+
* destructure's downstream USES are bare identifiers (`value`, not
|
|
23
|
+
* `props.value`), which that rename never touches — they stayed
|
|
24
|
+
* captured at the local's ONE declaration-time read forever.
|
|
25
|
+
*/
|
|
26
|
+
|
|
27
|
+
import ts from 'typescript'
|
|
28
|
+
import type { PropUsage } from '../types.ts'
|
|
29
|
+
import { computePropsUsedAsConditions } from './compute-prop-usage.ts'
|
|
30
|
+
import { boundPropLocalNames, livePropReadExpr, resolveBodyPropAliases, type LivePropReadInfo } from '../props-binding.ts'
|
|
31
|
+
import { parsePropReadInitializer } from './prune-unused-prop-extractions.ts'
|
|
32
|
+
import { rewriteScopedValueRefs } from '../prop-rewrite.ts'
|
|
33
|
+
import { identifierPattern } from '../identifier-pattern.ts'
|
|
34
|
+
import { PROPS_PARAM } from './utils.ts'
|
|
35
|
+
import type { ClientJsContext } from './types.ts'
|
|
36
|
+
|
|
37
|
+
export function rewriteDestructuredPropReads(
|
|
38
|
+
code: string,
|
|
39
|
+
ctx: ClientJsContext,
|
|
40
|
+
propUsage: ReadonlyMap<string, PropUsage>,
|
|
41
|
+
): string {
|
|
42
|
+
return ctx.propsObjectName === null
|
|
43
|
+
? rewriteParamDestructuredReads(code, ctx, propUsage)
|
|
44
|
+
: rewriteBodyAliasReads(code, ctx)
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Rewrite every bare value-position read of a PARAMETER-destructured prop in
|
|
49
|
+
* `code` (the joined `init*` body, already past `rewritePropsObjectRef`) to a
|
|
50
|
+
* live `_p.<key>` read.
|
|
51
|
+
*
|
|
52
|
+
* The walk is `rewriteScopedValueRefs` (`prop-rewrite.ts`), which carries a
|
|
53
|
+
* binding-scope stack, so `items.map((title) => title.a)` and a
|
|
54
|
+
* handler-local `const title = 'local'` keep their own binding even when
|
|
55
|
+
* `title` is also a prop name.
|
|
56
|
+
*
|
|
57
|
+
* Excluding `children`: reading `_p.children` live re-invokes a getter that
|
|
58
|
+
* instantiates child components (see `emitPropsExtraction`) — `children`
|
|
59
|
+
* must stay a captured-once read, by design.
|
|
60
|
+
*
|
|
61
|
+
* A null result can only mean `code` did not parse — never that a
|
|
62
|
+
* substitution broke it, since the walk parses the pristine input and every
|
|
63
|
+
* substituted string is a well-formed expression spliced where the walk
|
|
64
|
+
* already proved an identifier sat. The one shape that trips it is a
|
|
65
|
+
* tracked pre-existing hole (`map-body-no-silent-divergence.test.ts`'s
|
|
66
|
+
* `KNOWN_HOLES`): a `.map()` preamble leaking raw JSX, which is already a
|
|
67
|
+
* browser `SyntaxError` with or without this rewrite. Warning and passing
|
|
68
|
+
* the code through keeps that a tracked silent hole instead of promoting it
|
|
69
|
+
* to a compiler crash — same precedent as `pruneUnusedPropExtractions`.
|
|
70
|
+
*/
|
|
71
|
+
function rewriteParamDestructuredReads(
|
|
72
|
+
code: string,
|
|
73
|
+
ctx: ClientJsContext,
|
|
74
|
+
propUsage: ReadonlyMap<string, PropUsage>,
|
|
75
|
+
): string {
|
|
76
|
+
const names = new Set(boundPropLocalNames(ctx))
|
|
77
|
+
names.delete('children')
|
|
78
|
+
if (names.size === 0) return code
|
|
79
|
+
|
|
80
|
+
const propsUsedAsConditions = computePropsUsedAsConditions(ctx, names)
|
|
81
|
+
const propByName = new Map<string, LivePropReadInfo>()
|
|
82
|
+
for (const p of ctx.propsParams) propByName.set(p.name, p)
|
|
83
|
+
|
|
84
|
+
const replacementFor = (name: string): string => {
|
|
85
|
+
// `names` comes from `ctx.propsParams`, so the fallback is unreachable —
|
|
86
|
+
// kept over a non-null assertion.
|
|
87
|
+
const prop = propByName.get(name)
|
|
88
|
+
if (!prop) return `${PROPS_PARAM}.${name}`
|
|
89
|
+
return livePropReadExpr(prop, propUsage.get(name), propsUsedAsConditions.has(name))
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
const result = rewriteScopedValueRefs(code, names, replacementFor, { allowStatements: true })
|
|
93
|
+
if (result === null) {
|
|
94
|
+
console.warn(
|
|
95
|
+
`[barefootjs] rewriteDestructuredPropReads: the generated init body for ` +
|
|
96
|
+
`component "${ctx.componentName}" did not parse as JS/TSX (most likely a ` +
|
|
97
|
+
`pre-existing raw-JSX leak — see this function's docstring); skipping the ` +
|
|
98
|
+
`live-prop-read rewrite for it.`,
|
|
99
|
+
)
|
|
100
|
+
return code
|
|
101
|
+
}
|
|
102
|
+
return result
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* Rewrite every bare value-position read of a BODY-destructured (or
|
|
107
|
+
* bare-member-alias) prop local in `code` to a live `_p.<key>` read, for a
|
|
108
|
+
* bare-props-form component (`function Foo(props: Props)`).
|
|
109
|
+
*
|
|
110
|
+
* `resolveBodyPropAliases` can't distinguish `const { value } = props` from
|
|
111
|
+
* `const value = props.value` (the analyzer's IR is identical for both —
|
|
112
|
+
* see its docstring), so this rewrite treats them identically too. That's
|
|
113
|
+
* the correct call, not a shortcut: BOTH are pure single-prop passthroughs
|
|
114
|
+
* with no computation of their own, so both should read live, matching
|
|
115
|
+
* `props.value` used directly (which already reads live via
|
|
116
|
+
* `rewritePropsObjectRef`).
|
|
117
|
+
*
|
|
118
|
+
* A body alias's declaration is a REAL statement in `code` (unlike a
|
|
119
|
+
* parameter-destructured name, which never had one) — `const value =
|
|
120
|
+
* _p.value;`, physically sitting in the init body. Left in place, it would
|
|
121
|
+
* shadow every reference the rewrite is trying to make live (the walk sees
|
|
122
|
+
* it as a `Block`-scope declaration and treats every use as already
|
|
123
|
+
* correctly bound, exactly as it should for a genuine local). So this pass
|
|
124
|
+
* locates and DELETES each qualifying declaration BEFORE walking, rather
|
|
125
|
+
* than relying on `pruneUnusedPropExtractions` (which runs after this pass
|
|
126
|
+
* and only prunes an EXACT `const X = _p.X` shape — never a renamed alias's
|
|
127
|
+
* `const v = _p.value`, since its bound name doesn't match the read key).
|
|
128
|
+
*
|
|
129
|
+
* Only a declaration whose initializer is *exactly* `_p.<key>` [`?? <d>`] is
|
|
130
|
+
* touched — matched via `parsePropReadInitializer`, the same recognizer
|
|
131
|
+
* `pruneUnusedPropExtractions` uses for its narrower same-name case. A local
|
|
132
|
+
* this pass can't re-locate in that exact shape is left alone entirely
|
|
133
|
+
* (conservative false-keep: it stays captured, i.e. today's behavior, never
|
|
134
|
+
* a half-rewrite).
|
|
135
|
+
*
|
|
136
|
+
* Excluding `key === 'children'`: same hazard as the parameter form above,
|
|
137
|
+
* keyed by the CALLER-FACING name (`const { children: kids } = props`
|
|
138
|
+
* aliases `kids -> children` and must stay captured-once too).
|
|
139
|
+
*/
|
|
140
|
+
function rewriteBodyAliasReads(code: string, ctx: ClientJsContext): string {
|
|
141
|
+
const aliases = resolveBodyPropAliases(ctx.localConstants, ctx.propsObjectName)
|
|
142
|
+
for (const [local, alias] of aliases) {
|
|
143
|
+
if (alias.key === 'children') aliases.delete(local)
|
|
144
|
+
}
|
|
145
|
+
if (aliases.size === 0) return code
|
|
146
|
+
// Quick exit when none of the candidate local names appear at all.
|
|
147
|
+
if (![...aliases.keys()].some((name) => identifierPattern(name).test(code))) return code
|
|
148
|
+
|
|
149
|
+
const sourceFile = ts.createSourceFile('init-body.ts', code, ts.ScriptTarget.Latest, /*setParentNodes*/ true, ts.ScriptKind.TS)
|
|
150
|
+
|
|
151
|
+
const spans: Array<readonly [number, number]> = []
|
|
152
|
+
const located = new Map<string, { key: string; defaultValue?: string; defaultContainsArrow?: boolean }>()
|
|
153
|
+
for (const stmt of sourceFile.statements) {
|
|
154
|
+
if (!ts.isFunctionDeclaration(stmt) || !stmt.name?.text.startsWith('init') || !stmt.body) continue
|
|
155
|
+
for (const inner of stmt.body.statements) {
|
|
156
|
+
if (!ts.isVariableStatement(inner)) continue
|
|
157
|
+
if ((inner.declarationList.flags & ts.NodeFlags.Const) === 0) continue
|
|
158
|
+
const decls = inner.declarationList.declarations
|
|
159
|
+
if (decls.length !== 1) continue
|
|
160
|
+
const decl = decls[0]!
|
|
161
|
+
if (!ts.isIdentifier(decl.name) || !decl.initializer) continue
|
|
162
|
+
const alias = aliases.get(decl.name.text)
|
|
163
|
+
if (!alias) continue
|
|
164
|
+
const parsedInit = parsePropReadInitializer(decl.initializer)
|
|
165
|
+
if (parsedInit === null || parsedInit.key !== alias.key) continue
|
|
166
|
+
if ((parsedInit.fallback !== null) !== alias.hasDefault) continue
|
|
167
|
+
located.set(decl.name.text, {
|
|
168
|
+
key: alias.key,
|
|
169
|
+
defaultValue: parsedInit.fallback?.getText(sourceFile),
|
|
170
|
+
defaultContainsArrow: alias.defaultContainsArrow,
|
|
171
|
+
})
|
|
172
|
+
spans.push([inner.getFullStart(), inner.getEnd()])
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
if (located.size === 0) return code
|
|
176
|
+
|
|
177
|
+
// Delete the located declarations first (back-to-front) so the walk below
|
|
178
|
+
// never sees them as a shadowing local binding.
|
|
179
|
+
let stripped = code
|
|
180
|
+
for (const [start, end] of [...spans].sort((a, b) => b[0] - a[0])) {
|
|
181
|
+
stripped = stripped.slice(0, start) + stripped.slice(end)
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
const names = new Set(located.keys())
|
|
185
|
+
const replacementFor = (name: string): string => {
|
|
186
|
+
// `names` comes from `located`, so the fallback is unreachable — kept
|
|
187
|
+
// over a non-null assertion.
|
|
188
|
+
const info = located.get(name)
|
|
189
|
+
if (!info) return `${PROPS_PARAM}.${name}`
|
|
190
|
+
const prop: LivePropReadInfo = {
|
|
191
|
+
name,
|
|
192
|
+
sourceName: info.key,
|
|
193
|
+
defaultValue: info.defaultValue,
|
|
194
|
+
defaultContainsArrow: info.defaultContainsArrow,
|
|
195
|
+
}
|
|
196
|
+
return livePropReadExpr(prop, undefined, false)
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
const result = rewriteScopedValueRefs(stripped, names, replacementFor, { allowStatements: true })
|
|
200
|
+
if (result === null) {
|
|
201
|
+
console.warn(
|
|
202
|
+
`[barefootjs] rewriteDestructuredPropReads: the generated init body for ` +
|
|
203
|
+
`component "${ctx.componentName}" did not parse as JS/TSX after removing its ` +
|
|
204
|
+
`body-destructured prop-alias declarations; skipping the live-prop-read ` +
|
|
205
|
+
`rewrite for it.`,
|
|
206
|
+
)
|
|
207
|
+
return code
|
|
208
|
+
}
|
|
209
|
+
return result
|
|
210
|
+
}
|
|
@@ -278,6 +278,20 @@ export function toDomEventName(eventName: string): string {
|
|
|
278
278
|
return jsxToDomEventMap[eventName] ?? eventName
|
|
279
279
|
}
|
|
280
280
|
|
|
281
|
+
/**
|
|
282
|
+
* Non-bubbling events that require `addEventListener` with capture for
|
|
283
|
+
* delegation. Shared between the delegation stringifier and the own-handler
|
|
284
|
+
* collision check (#2930) so both agree on which events a container's own
|
|
285
|
+
* bubble-phase handler can never collide with in the first place — a
|
|
286
|
+
* container's own non-capturing `onFocus`, say, only fires when the
|
|
287
|
+
* container itself is the target, never for a descendant row.
|
|
288
|
+
*/
|
|
289
|
+
export const NON_BUBBLING_EVENTS = new Set([
|
|
290
|
+
'blur', 'focus', 'load', 'unload',
|
|
291
|
+
'mouseenter', 'mouseleave',
|
|
292
|
+
'pointerenter', 'pointerleave',
|
|
293
|
+
])
|
|
294
|
+
|
|
281
295
|
/**
|
|
282
296
|
* Quote a prop name if it is not a valid JS identifier.
|
|
283
297
|
* Returns the name as-is for valid identifiers (e.g., "checked"),
|
package/src/jsx-to-ir.ts
CHANGED
|
@@ -41,7 +41,7 @@ import {
|
|
|
41
41
|
} from './types.ts'
|
|
42
42
|
import { type AnalyzerContext, type MultiReturnJsxInfo, getSourceLocation, collectReactiveGetterNames } from './analyzer-context.ts'
|
|
43
43
|
import { parseExpression, isSupported, parseBlockBody, foldBlockToExpr, predicateTernaryToLogical, tsNodeToParsedExpr, sortComparatorFromArrow, stringifyParsedExpr, cssKebabCase, CALLBACK_METHODS, type ParsedExpr } from './expression-parser.ts'
|
|
44
|
-
import type { IRLoopSort, FunctionInfo, ConstantInfo } from './types.ts'
|
|
44
|
+
import type { IRLoopSort, FunctionInfo, ConstantInfo, ParamInfo } from './types.ts'
|
|
45
45
|
import { formatParamWithType } from './module-exports.ts'
|
|
46
46
|
import { createError, ErrorCodes, internalInvariant } from './errors.ts'
|
|
47
47
|
import { CLIENT_BUILTIN_SOURCE, isClientBuiltinName, type ClientBuiltinTag } from './builtins.ts'
|
|
@@ -51,7 +51,8 @@ import {
|
|
|
51
51
|
collectAstPropRefs,
|
|
52
52
|
rewriteScopedValueRefs,
|
|
53
53
|
} from './prop-rewrite.ts'
|
|
54
|
-
import { boundPropLocalNames, buildPropAliasMap, resolveAliasOrigin, resolveRestSpreadOriginCore } from './props-binding.ts'
|
|
54
|
+
import { boundPropLocalNames, buildPropAliasMap, resolveAliasOrigin, resolveRestSpreadOriginCore, livePropReadExpr } from './props-binding.ts'
|
|
55
|
+
import { PROPS_PARAM } from './ir-to-client-js/utils.ts'
|
|
55
56
|
import { resolveFreeRefs, isNameBound as isNameBoundInEnv, type BindingEnvironment } from './free-refs.ts'
|
|
56
57
|
import { computeFileScope } from './ir-to-client-js/component-scope.ts'
|
|
57
58
|
import { createTemplateAwareStringProtector } from './ir-to-client-js/html-template.ts'
|
|
@@ -129,6 +130,20 @@ interface TransformContext {
|
|
|
129
130
|
* `_p.<local>` (#2524 CSR half: `_p` is always caller-keyed).
|
|
130
131
|
*/
|
|
131
132
|
_destructuredPropAliases?: Map<string, string> | null
|
|
133
|
+
/**
|
|
134
|
+
* Cached local-name → `ParamInfo` map for the same eligible set, so
|
|
135
|
+
* `rewriteBarePropRefs` can apply a prop's destructure DEFAULT. Without
|
|
136
|
+
* it the CSR-only `template:` literal emitted a bare `_p.label` for
|
|
137
|
+
* `data-label={label}` with `label = 'none'`: correct once the caller
|
|
138
|
+
* sends a value, but the attribute was omitted entirely on the first
|
|
139
|
+
* render, where `_p.label` is `undefined`. Covers PARAMETER destructuring
|
|
140
|
+
* and an UNRENAMED BODY destructure default alike (`function Foo(props) {
|
|
141
|
+
* const { label = 'none' } = props }`) — both populate `defaultValue`
|
|
142
|
+
* directly on `propsParams` (the parameter form in `extractProps`, the
|
|
143
|
+
* body form via `collectConstant`'s overlay, #2943), so this map is a
|
|
144
|
+
* plain re-keying of `eligible`, no second default-resolution needed.
|
|
145
|
+
*/
|
|
146
|
+
_destructuredPropInfoByName?: ReadonlyMap<string, ParamInfo> | null
|
|
132
147
|
/**
|
|
133
148
|
* The active `.map()`/callback binding stack (#2482 Stage 1a) — replaces
|
|
134
149
|
* the former mutable per-name `Set<string>` mutated in lockstep with
|
|
@@ -588,7 +603,18 @@ function rewriteBarePropRefs(text: string, expr: ts.Node, ctx: TransformContext)
|
|
|
588
603
|
// for references to those locals and union the matching dep sets.
|
|
589
604
|
const extraPropRefs = collectBranchLocalPropRefsViaSubstitution(expr, ctx)
|
|
590
605
|
const propAliases = getDestructuredPropAliases(ctx)
|
|
591
|
-
|
|
606
|
+
const propInfoByName = getDestructuredPropInfoByName(ctx)
|
|
607
|
+
// No `usage`/`usedAsCondition`: the `?? []` / `?? {}` fallbacks need
|
|
608
|
+
// Phase-2 `PropUsage` data this Phase-1 rewrite doesn't have. Only the
|
|
609
|
+
// explicit destructure default applies here — see
|
|
610
|
+
// `_destructuredPropInfoByName`.
|
|
611
|
+
const replacementFor = propInfoByName
|
|
612
|
+
? (localName: string, callerKey: string): string => {
|
|
613
|
+
const info = propInfoByName.get(localName)
|
|
614
|
+
return info ? livePropReadExpr(info, undefined, false) : `${PROPS_PARAM}.${callerKey}`
|
|
615
|
+
}
|
|
616
|
+
: undefined
|
|
617
|
+
return rewriteBarePropRefsCore(dateLowered, expr, propNames, extraPropRefs, propAliases ?? undefined, replacementFor)
|
|
592
618
|
}
|
|
593
619
|
|
|
594
620
|
/**
|
|
@@ -676,10 +702,27 @@ function getDestructuredPropNames(ctx: TransformContext): Set<string> | null {
|
|
|
676
702
|
const names = eligible.map(p => p.name)
|
|
677
703
|
ctx._destructuredPropNames = names.length > 0 ? new Set(names) : null
|
|
678
704
|
ctx._destructuredPropAliases = buildPropAliasMap(eligible) ?? null
|
|
705
|
+
// #2943: `eligible`'s `ParamInfo`s already carry a body-destructured
|
|
706
|
+
// default when the alias is unrenamed — the analyzer overlays it onto
|
|
707
|
+
// `propsParams` directly at the binding's own declaration
|
|
708
|
+
// (`collectConstant`, `analyzer.ts`), so no separate overlay is needed
|
|
709
|
+
// here (there used to be one; see git history at #2934/#2943 for why).
|
|
710
|
+
const infoByName = new Map<string, ParamInfo>()
|
|
711
|
+
for (const p of eligible) infoByName.set(p.name, p)
|
|
712
|
+
ctx._destructuredPropInfoByName = infoByName.size > 0 ? infoByName : null
|
|
679
713
|
}
|
|
680
714
|
return ctx._destructuredPropNames ?? null
|
|
681
715
|
}
|
|
682
716
|
|
|
717
|
+
/**
|
|
718
|
+
* Companion to `getDestructuredPropNames`: local-name → `ParamInfo` for the
|
|
719
|
+
* same shadow-filtered eligible set. Both caches are written in one pass.
|
|
720
|
+
*/
|
|
721
|
+
function getDestructuredPropInfoByName(ctx: TransformContext): ReadonlyMap<string, ParamInfo> | null {
|
|
722
|
+
if (ctx._destructuredPropNames === undefined) getDestructuredPropNames(ctx)
|
|
723
|
+
return ctx._destructuredPropInfoByName ?? null
|
|
724
|
+
}
|
|
725
|
+
|
|
683
726
|
/**
|
|
684
727
|
* Companion to `getDestructuredPropNames`: the local-name → caller-key
|
|
685
728
|
* alias map for the SAME shadow-filtered eligible set, populated as a
|
|
@@ -2376,8 +2419,9 @@ function transformExpressionInner(
|
|
|
2376
2419
|
// Check for bare signal/memo identifier (BF044). A JSX text child is
|
|
2377
2420
|
// inherently a RENDERED position — unlike a component prop, `{expr}`
|
|
2378
2421
|
// here always becomes literal rendered output, never an opaque value
|
|
2379
|
-
// handed to a consumer to call later — so
|
|
2380
|
-
|
|
2422
|
+
// handed to a consumer to call later — so the check (top-level and
|
|
2423
|
+
// nested descent) always fires.
|
|
2424
|
+
checkBareSignalOrMemoIdentifier(expr, ctx, { renderedPosition: true })
|
|
2381
2425
|
|
|
2382
2426
|
// #547: Inline a JSX constant referenced by identifier. Unique to JSX-child
|
|
2383
2427
|
// position — conditional branches and return position don't resolve
|
|
@@ -6621,14 +6665,14 @@ function getAttributeValue(attr: ts.JsxAttribute, ctx: TransformContext): AttrVa
|
|
|
6621
6665
|
// `processComponentProps`), and only the element-attribute case is
|
|
6622
6666
|
// a RENDERED position — a component prop is an opaque value handed
|
|
6623
6667
|
// to the component, and this codebase's Context-Provider idiom
|
|
6624
|
-
// depends on that value being an uncalled accessor
|
|
6625
|
-
// open
|
|
6626
|
-
//
|
|
6627
|
-
//
|
|
6628
|
-
//
|
|
6629
|
-
//
|
|
6630
|
-
//
|
|
6631
|
-
checkBareSignalOrMemoIdentifier(expr, ctx, {
|
|
6668
|
+
// depends on that value being an uncalled accessor, whether passed
|
|
6669
|
+
// directly (`x={open}`, #2760) or nested in an object literal
|
|
6670
|
+
// (`value={{ open }}`, ui/components/ui/select/index.tsx) — calling
|
|
6671
|
+
// it eagerly there would freeze the value at provider-render time
|
|
6672
|
+
// and break every consumer's reactivity. So the entire check (top
|
|
6673
|
+
// level included) is gated on element-vs-component, derived from
|
|
6674
|
+
// the tag this attribute lives on.
|
|
6675
|
+
checkBareSignalOrMemoIdentifier(expr, ctx, { renderedPosition: isRenderedElementAttribute(attr, ctx) })
|
|
6632
6676
|
|
|
6633
6677
|
// Static style object: style={{ key: 'value', ... }} → CSS string at compile time
|
|
6634
6678
|
if (attr.name.getText(ctx.sourceFile) === 'style' && ts.isObjectLiteralExpression(expr)) {
|
|
@@ -7708,15 +7752,16 @@ function processComponentProps(
|
|
|
7708
7752
|
* hyphenated (a custom element) is a real DOM node, capitalised or
|
|
7709
7753
|
* dotted (`Select.Provider`) is a component.
|
|
7710
7754
|
*
|
|
7711
|
-
* Feeds `checkBareSignalOrMemoIdentifier`'s `
|
|
7755
|
+
* Feeds `checkBareSignalOrMemoIdentifier`'s `renderedPosition` gate: a
|
|
7712
7756
|
* DOM attribute value is a RENDERED position (this is genuinely what
|
|
7713
7757
|
* ends up in the DOM), so a bare getter buried inside it — `style={{
|
|
7714
7758
|
* color: val }}` — is exactly as much a forgotten `()` as a top-level
|
|
7715
7759
|
* one. A component prop is not rendered — it's an opaque value handed
|
|
7716
7760
|
* to the component — and this codebase's Context-Provider idiom
|
|
7717
|
-
* depends on that value being an UNCALLED accessor
|
|
7761
|
+
* depends on that value being an UNCALLED accessor, whether passed
|
|
7762
|
+
* directly (`x={open}`) or nested in an object literal
|
|
7718
7763
|
* (`ui/components/ui/select/index.tsx`'s `value={{ open, ... }}`), so
|
|
7719
|
-
*
|
|
7764
|
+
* the check must not fire there at all (#2760).
|
|
7720
7765
|
*/
|
|
7721
7766
|
function isRenderedElementAttribute(attr: ts.JsxAttribute, ctx: TransformContext): boolean {
|
|
7722
7767
|
const tagName = attr.parent.parent.tagName.getText(ctx.sourceFile)
|
|
@@ -7728,22 +7773,25 @@ function isRenderedElementAttribute(attr: ts.JsxAttribute, ctx: TransformContext
|
|
|
7728
7773
|
* Emits BF044 when a signal/memo getter is passed without calling it.
|
|
7729
7774
|
* e.g., value={count} instead of value={count()}
|
|
7730
7775
|
*
|
|
7731
|
-
*
|
|
7732
|
-
*
|
|
7733
|
-
*
|
|
7734
|
-
*
|
|
7735
|
-
*
|
|
7776
|
+
* `renderedPosition` gates the ENTIRE check, top-level included: only a
|
|
7777
|
+
* RENDERED position (a JSX text child; a DOM element's attribute value
|
|
7778
|
+
* via `isRenderedElementAttribute`) risks a forgotten `()` silently
|
|
7779
|
+
* producing wrong output, so only those positions fire at all. A
|
|
7780
|
+
* component prop is not rendered — it's an opaque value handed to the
|
|
7781
|
+
* component — and a bare getter there is this codebase's deliberate
|
|
7782
|
+
* Context-Provider idiom (measured: 66 such uses across
|
|
7783
|
+
* `ui/components/**`/`site/**`, all legitimate, none a forgotten `()`),
|
|
7784
|
+
* whether written directly (`<Foo x={val} />`) or nested inside an
|
|
7785
|
+
* object literal (`<Foo value={{ val }} />`, already silent before this
|
|
7786
|
+
* gate existed) — #2760 settled the two forms as one idiom, not an
|
|
7787
|
+
* inconsistency to fix by tightening the direct form. When
|
|
7788
|
+
* `renderedPosition` is true, nested descent additionally walks INSIDE
|
|
7789
|
+
* a composite expression — call arguments, template-literal spans,
|
|
7736
7790
|
* ternary branches (condition + both arms), array/object literal
|
|
7737
7791
|
* members, binary/logical operands, parenthesized wrappers — to catch
|
|
7738
|
-
* a bare reference buried anywhere in that reachable set (#2755,
|
|
7739
|
-
*
|
|
7740
|
-
*
|
|
7741
|
-
* RENDERED positions (a JSX text child; a DOM element's attribute value
|
|
7742
|
-
* via `isRenderedElementAttribute`) — never for a component prop, where
|
|
7743
|
-
* a bare getter is this codebase's deliberate Context-Provider idiom
|
|
7744
|
-
* (measured: 66 such uses across `ui/components/**`/`site/**`, all
|
|
7745
|
-
* legitimate, none a forgotten `()` — widening the gate to fire there
|
|
7746
|
-
* would be a correctness regression, not a fix).
|
|
7792
|
+
* a bare reference buried anywhere in that reachable set (#2755, #2751:
|
|
7793
|
+
* this is the single upstream gate whose shallow top-level-only check
|
|
7794
|
+
* let both downstream bugs through).
|
|
7747
7795
|
*
|
|
7748
7796
|
* Two structural exclusions apply whenever descending, both required
|
|
7749
7797
|
* so the walk doesn't false-positive on shapes that read the name
|
|
@@ -7781,8 +7829,10 @@ function isRenderedElementAttribute(attr: ts.JsxAttribute, ctx: TransformContext
|
|
|
7781
7829
|
function checkBareSignalOrMemoIdentifier(
|
|
7782
7830
|
expr: ts.Expression,
|
|
7783
7831
|
ctx: TransformContext,
|
|
7784
|
-
options?: {
|
|
7832
|
+
options?: { renderedPosition?: boolean }
|
|
7785
7833
|
): void {
|
|
7834
|
+
if (!options?.renderedPosition) return
|
|
7835
|
+
|
|
7786
7836
|
const reactiveNames = new Map<string, 'signal' | 'memo'>()
|
|
7787
7837
|
for (const signal of ctx.analyzer.signals) reactiveNames.set(signal.getter, 'signal')
|
|
7788
7838
|
for (const memo of ctx.analyzer.memos) reactiveNames.set(memo.name, 'memo')
|
|
@@ -7804,29 +7854,27 @@ function checkBareSignalOrMemoIdentifier(
|
|
|
7804
7854
|
)
|
|
7805
7855
|
}
|
|
7806
7856
|
|
|
7807
|
-
// Top-level check —
|
|
7808
|
-
//
|
|
7809
|
-
// respects the ambient loop/callback
|
|
7810
|
-
// needs — a pre-existing gap (the
|
|
7811
|
-
// `if (!ts.isIdentifier(expr)) return` never
|
|
7812
|
-
// that stayed invisible only because a bare
|
|
7813
|
-
// shadowed by a `.map(name => ...)` row param
|
|
7814
|
-
// exercised by any prior test. `ctx.scope` is
|
|
7815
|
-
// JSX node — already entered for the current
|
|
7816
|
-
// by the time an attribute/child expression
|
|
7817
|
-
// (`enterLoopRow`/`enterCallback`,
|
|
7818
|
-
// `isBound` here is a shadow-guard
|
|
7819
|
-
// `spec/compiler.md`'s `BindingScope`
|
|
7820
|
-
// getter of this name shadowed HERE,
|
|
7821
|
-
// its own reactive slot".
|
|
7857
|
+
// Top-level check — `renderedPosition` (checked above) has already
|
|
7858
|
+
// ruled out a component-prop position, so any call reaching here is a
|
|
7859
|
+
// genuinely rendered one. Also respects the ambient loop/callback
|
|
7860
|
+
// `BindingScope` a shadowed name needs — a pre-existing gap (the
|
|
7861
|
+
// original single-line `if (!ts.isIdentifier(expr)) return` never
|
|
7862
|
+
// consulted scope either) that stayed invisible only because a bare
|
|
7863
|
+
// top-level identifier shadowed by a `.map(name => ...)` row param
|
|
7864
|
+
// never happened to be exercised by any prior test. `ctx.scope` is
|
|
7865
|
+
// the LIVE scope at this JSX node — already entered for the current
|
|
7866
|
+
// loop row/callback frame by the time an attribute/child expression
|
|
7867
|
+
// under it is checked (`enterLoopRow`/`enterCallback`,
|
|
7868
|
+
// `scope/binding-scope.ts`) — so `isBound` here is a shadow-guard
|
|
7869
|
+
// read in exactly the sense `spec/compiler.md`'s `BindingScope`
|
|
7870
|
+
// section describes: is a signal getter of this name shadowed HERE,
|
|
7871
|
+
// not "does this expression need its own reactive slot".
|
|
7822
7872
|
if (ts.isIdentifier(expr)) {
|
|
7823
7873
|
const kind = reactiveNames.get(expr.text)
|
|
7824
7874
|
if (kind && !ctx.scope.isBound(expr.text)) report(expr, expr.text, kind)
|
|
7825
7875
|
return
|
|
7826
7876
|
}
|
|
7827
7877
|
|
|
7828
|
-
if (!options?.descendNested) return
|
|
7829
|
-
|
|
7830
7878
|
const boundStack: Array<Set<string>> = []
|
|
7831
7879
|
const isBound = (name: string): boolean => {
|
|
7832
7880
|
for (let i = boundStack.length - 1; i >= 0; i--) {
|
package/src/prop-rewrite.ts
CHANGED
|
@@ -99,6 +99,12 @@ function walkWithScope(
|
|
|
99
99
|
* binding (`renamed`) — and pullfrog review on #2889 caught that only the
|
|
100
100
|
* latter was excluded, so a substituted name colliding with the SOURCE key
|
|
101
101
|
* still corrupted to invalid JS (`const { (_p.tag): renamed } = obj`).
|
|
102
|
+
*
|
|
103
|
+
* The accessor/method/property NAME checks close the same gap for
|
|
104
|
+
* `{ get className() { return className } }` (the shape the
|
|
105
|
+
* reactive-child-props forwarding machinery emits): the name slot is walked
|
|
106
|
+
* like any other identifier, and substituting there yields
|
|
107
|
+
* `get (_p.className ?? '')() { ... }`.
|
|
102
108
|
*/
|
|
103
109
|
function isNonValuePosition(n: ts.Identifier, parent: ts.Node | undefined): boolean {
|
|
104
110
|
if (!parent) return false
|
|
@@ -108,6 +114,11 @@ function isNonValuePosition(n: ts.Identifier, parent: ts.Node | undefined): bool
|
|
|
108
114
|
if ((ts.isParameter(parent) || ts.isVariableDeclaration(parent) || ts.isBindingElement(parent)) && parent.name === n) return true
|
|
109
115
|
if (ts.isBindingElement(parent) && parent.propertyName === n) return true
|
|
110
116
|
if (ts.isTypeReferenceNode(parent)) return true
|
|
117
|
+
if (
|
|
118
|
+
(ts.isGetAccessorDeclaration(parent) || ts.isSetAccessorDeclaration(parent) || ts.isMethodDeclaration(parent)) &&
|
|
119
|
+
parent.name === n
|
|
120
|
+
) return true
|
|
121
|
+
if ((ts.isPropertySignature(parent) || ts.isPropertyDeclaration(parent)) && parent.name === n) return true
|
|
111
122
|
return false
|
|
112
123
|
}
|
|
113
124
|
|