@barefootjs/jsx 0.35.6 → 0.35.8
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.d.ts.map +1 -1
- package/dist/index.js +59 -20
- package/dist/ir-to-client-js/csr-substitute.d.ts +25 -5
- package/dist/ir-to-client-js/csr-substitute.d.ts.map +1 -1
- package/dist/ir-to-client-js/html-template.d.ts.map +1 -1
- package/dist/ir-to-client-js/imports.d.ts +2 -2
- package/dist/ir-to-client-js/imports.d.ts.map +1 -1
- package/dist/props-binding.d.ts +11 -0
- package/dist/props-binding.d.ts.map +1 -1
- package/dist/ssr-defaults.d.ts.map +1 -1
- package/package.json +2 -2
- package/src/__tests__/body-destructured-prop-default-arrow.test.ts +142 -0
- package/src/__tests__/csr-substitute-bare-getter.test.ts +117 -0
- package/src/__tests__/csr-template-scope-soundness.test.ts +125 -0
- package/src/__tests__/issue-2960-multi-root-cond-same-tag.test.ts +141 -0
- package/src/__tests__/module-helper-scope-2986.test.ts +286 -0
- package/src/__tests__/reactive-factory-cross-file.test.ts +37 -0
- package/src/__tests__/ssr-defaults.test.ts +24 -2
- package/src/analyzer.ts +57 -18
- package/src/ir-to-client-js/csr-substitute.ts +61 -11
- package/src/ir-to-client-js/html-template.ts +65 -9
- package/src/ir-to-client-js/imports.ts +5 -0
- package/src/ir-to-client-js/rewrite-destructured-props.ts +13 -1
- package/src/props-binding.ts +15 -1
- package/src/ssr-defaults.ts +32 -0
|
@@ -878,6 +878,43 @@ export function Ticker() {
|
|
|
878
878
|
expect(clientJs!.content).toMatch(/import\s*\{[^}]*onMount[^}]*\}\s*from\s*'@barefootjs\/client\/runtime'/)
|
|
879
879
|
})
|
|
880
880
|
|
|
881
|
+
// This is a COMPILE-TIME assertion only (the generated import list), not
|
|
882
|
+
// a runtime behavior test — `batch` around a single `setCount` call has
|
|
883
|
+
// no observable effect at runtime (batching only matters for coalescing
|
|
884
|
+
// MULTIPLE signal writes into one effect flush; see `batch`'s doc comment
|
|
885
|
+
// in `packages/client/src/reactive.ts`). It's used here purely as the
|
|
886
|
+
// smallest fixture that calls `batch` from a cross-file factory, to pin
|
|
887
|
+
// #2985: `batch` was missing from `RUNTIME_IMPORT_CANDIDATES`
|
|
888
|
+
// (`ir-to-client-js/imports.ts`), so it compiled clean but was silently
|
|
889
|
+
// dropped from the emitted `@barefootjs/client/runtime` import, throwing
|
|
890
|
+
// `ReferenceError: batch is not defined` at hydration.
|
|
891
|
+
test('M17b: batch is provisioned from usage for a cross-file factory (#2985)', () => {
|
|
892
|
+
writeFixture('matrix17b/useCounter.tsx', `'use client'
|
|
893
|
+
import { batch, createSignal } from '@barefootjs/client'
|
|
894
|
+
|
|
895
|
+
export function useCounter() {
|
|
896
|
+
const [count, setCount] = createSignal(0)
|
|
897
|
+
const increment = () => batch(() => setCount(count() + 1))
|
|
898
|
+
return { count, increment }
|
|
899
|
+
}
|
|
900
|
+
`)
|
|
901
|
+
const consumerSource = `'use client'
|
|
902
|
+
import { useCounter } from './useCounter'
|
|
903
|
+
|
|
904
|
+
export function Counter() {
|
|
905
|
+
const { count, increment } = useCounter()
|
|
906
|
+
return <button onClick={increment}>{count()}</button>
|
|
907
|
+
}
|
|
908
|
+
`
|
|
909
|
+
const consumerPath = writeFixture('matrix17b/Counter.tsx', consumerSource)
|
|
910
|
+
|
|
911
|
+
const result = compileJSX(consumerSource, consumerPath, { adapter })
|
|
912
|
+
expect(result.errors.filter(e => e.severity === 'error')).toHaveLength(0)
|
|
913
|
+
const clientJs = result.files.find(f => f.type === 'clientJs')
|
|
914
|
+
expect(clientJs).toBeDefined()
|
|
915
|
+
expect(clientJs!.content).toMatch(/import\s*\{[^}]*\bbatch\b[^}]*\}\s*from\s*'@barefootjs\/client\/runtime'/)
|
|
916
|
+
})
|
|
917
|
+
|
|
881
918
|
test('M18: a type-only helper import does not trigger BF112 and is re-provisioned as `import type` (#2350)', () => {
|
|
882
919
|
writeFixture('matrix18/todo-types.ts', `export interface Todo {
|
|
883
920
|
id: number
|
|
@@ -641,6 +641,26 @@ describe('extractSsrDefaults', () => {
|
|
|
641
641
|
expect(defaults?.label).toEqual({ propName: 'label', value: null })
|
|
642
642
|
expect(defaults?.labelAlias).toBe(defaults?.label)
|
|
643
643
|
})
|
|
644
|
+
|
|
645
|
+
// #2924 review follow-up (pullfrog nitpick on the PR fixing the setter
|
|
646
|
+
// case): the setter-symmetric shape of the #2813 getter-alias test above
|
|
647
|
+
// — a bare local-const alias of a signal SETTER must be seeded under its
|
|
648
|
+
// own name too, not just the setter's own name.
|
|
649
|
+
test('a bare local-const alias of a signal setter is seeded under the alias name too', () => {
|
|
650
|
+
const metadata = metadataFor(`
|
|
651
|
+
'use client'
|
|
652
|
+
import { createSignal } from '@barefootjs/client'
|
|
653
|
+
function C() {
|
|
654
|
+
const [count, setCount] = createSignal(5)
|
|
655
|
+
const setCountAlias = setCount
|
|
656
|
+
return <button onClick={() => setCountAlias(6)}>{count()}</button>
|
|
657
|
+
}
|
|
658
|
+
`)
|
|
659
|
+
|
|
660
|
+
const defaults = extractSsrDefaults(metadata)
|
|
661
|
+
expect(defaults?.setCount).toEqual({ value: null })
|
|
662
|
+
expect(defaults?.setCountAlias).toEqual({ value: null })
|
|
663
|
+
})
|
|
644
664
|
})
|
|
645
665
|
|
|
646
666
|
// TS twin of the Ruby/Python/PHP/Perl/Rust `derive*FromDefaults` runtime
|
|
@@ -711,8 +731,10 @@ describe('deriveStashFromDefaults', () => {
|
|
|
711
731
|
`)
|
|
712
732
|
const defaults = extractSsrDefaults(metadata)!
|
|
713
733
|
|
|
714
|
-
|
|
715
|
-
|
|
734
|
+
// `setLabel` also gets a stash entry (#2924's SSR-half fix) — every
|
|
735
|
+
// signal setter is seeded unconditionally, same as every getter.
|
|
736
|
+
expect(deriveStashFromDefaults(defaults, { label: 'Hello' })).toEqual({ label: 'Hello', setLabel: null })
|
|
737
|
+
expect(deriveStashFromDefaults(defaults, {})).toEqual({ label: null, setLabel: null })
|
|
716
738
|
})
|
|
717
739
|
|
|
718
740
|
test('propName-less entry (signal / memo local): always uses the static value', () => {
|
package/src/analyzer.ts
CHANGED
|
@@ -11,7 +11,7 @@ import type { ImportSpecifier, TypeInfo, ParamInfo, ReactiveFactoryInfo, Decline
|
|
|
11
11
|
import { parseExpression, parseBlockBodyTolerant, foldBlockToExpr, tsNodeToParsedExpr } from './expression-parser.ts'
|
|
12
12
|
import type { CallbackBodyAcceptor } from './adapters/interface.ts'
|
|
13
13
|
import { rewriteBarePropRefs } from './prop-rewrite.ts'
|
|
14
|
-
import { buildPropAliasMap } from './props-binding.ts'
|
|
14
|
+
import { buildPropAliasMap, coalesceDefaultText } from './props-binding.ts'
|
|
15
15
|
import { incrementCounter } from './instrumentation.ts'
|
|
16
16
|
import {
|
|
17
17
|
type AnalyzerContext,
|
|
@@ -386,6 +386,28 @@ function collectNamedExports(sourceFile: ts.SourceFile): Set<string> {
|
|
|
386
386
|
// Single Pass Visitor
|
|
387
387
|
// =============================================================================
|
|
388
388
|
|
|
389
|
+
/**
|
|
390
|
+
* Does this node open a new function scope?
|
|
391
|
+
*
|
|
392
|
+
* The single answer to the one question every declaration collector in
|
|
393
|
+
* this file asks: bindings declared inside a function belong to that
|
|
394
|
+
* function, never to the scope being collected. `visitComponentBody` has
|
|
395
|
+
* always stopped here; `visit` did not, so the inner `const`/`function`
|
|
396
|
+
* declarations of a module-level `const f = (…) => { … }` were collected
|
|
397
|
+
* as module-level declarations of the FILE — hoisted into the init body
|
|
398
|
+
* and into the marked template, where their references to the helper's
|
|
399
|
+
* own parameters are unbound (#2986).
|
|
400
|
+
*/
|
|
401
|
+
function isFunctionScope(node: ts.Node): boolean {
|
|
402
|
+
return ts.isArrowFunction(node)
|
|
403
|
+
|| ts.isFunctionExpression(node)
|
|
404
|
+
|| ts.isFunctionDeclaration(node)
|
|
405
|
+
|| ts.isMethodDeclaration(node)
|
|
406
|
+
|| ts.isGetAccessorDeclaration(node)
|
|
407
|
+
|| ts.isSetAccessorDeclaration(node)
|
|
408
|
+
|| ts.isConstructorDeclaration(node)
|
|
409
|
+
}
|
|
410
|
+
|
|
389
411
|
function visit(
|
|
390
412
|
node: ts.Node,
|
|
391
413
|
ctx: AnalyzerContext,
|
|
@@ -607,7 +629,23 @@ function visit(
|
|
|
607
629
|
}
|
|
608
630
|
}
|
|
609
631
|
|
|
610
|
-
ts.forEachChild(node, (child) =>
|
|
632
|
+
ts.forEachChild(node, (child) => {
|
|
633
|
+
// Stop at a nested function scope (#2986). Two carve-outs at module level:
|
|
634
|
+
//
|
|
635
|
+
// • `function` declarations must still be VISITED, because the
|
|
636
|
+
// module-level branch above is what collects them — and it `return`s
|
|
637
|
+
// immediately afterwards, so the descent-stop is already encoded
|
|
638
|
+
// there. This walk is also the ONLY one that reaches a `function`
|
|
639
|
+
// declaration inside a component body (`visitComponentBody` skips
|
|
640
|
+
// them); `compute-scope`'s fixpoint then demotes it to init scope.
|
|
641
|
+
// Skipping them here silently produces zero-output components.
|
|
642
|
+
//
|
|
643
|
+
// • The target component's own arrow (`const App = () => { … }`):
|
|
644
|
+
// `analyzeComponentBody` handles its body, but `visit` must still
|
|
645
|
+
// descend to reach the `function` declarations inside it.
|
|
646
|
+
if (isFunctionScope(child) && !ts.isFunctionDeclaration(child) && child !== ctx.componentNode) return
|
|
647
|
+
visit(child, ctx, targetComponentName, namedExports)
|
|
648
|
+
})
|
|
611
649
|
}
|
|
612
650
|
|
|
613
651
|
// =============================================================================
|
|
@@ -846,17 +884,11 @@ function visitComponentBody(node: ts.Node, ctx: AnalyzerContext): void {
|
|
|
846
884
|
ctx.jsxReturn = unwrapJsxTransparent(node.expression)
|
|
847
885
|
}
|
|
848
886
|
|
|
849
|
-
// Skip recursion into
|
|
850
|
-
//
|
|
887
|
+
// Skip recursion into a nested function scope — its bindings are
|
|
888
|
+
// function-scoped, not component init-scope declarations. Same predicate
|
|
889
|
+
// the module-level walk (`visit`) uses.
|
|
851
890
|
ts.forEachChild(node, (child) => {
|
|
852
|
-
|
|
853
|
-
if (
|
|
854
|
-
ts.isArrowFunction(child) ||
|
|
855
|
-
ts.isFunctionExpression(child) ||
|
|
856
|
-
ts.isFunctionDeclaration(child)
|
|
857
|
-
) {
|
|
858
|
-
return
|
|
859
|
-
}
|
|
891
|
+
if (isFunctionScope(child)) return
|
|
860
892
|
visitComponentBody(child, ctx)
|
|
861
893
|
})
|
|
862
894
|
}
|
|
@@ -3181,10 +3213,18 @@ function collectConstant(
|
|
|
3181
3213
|
if (!ts.isBindingElement(el) || !ts.isIdentifier(el.name) || el.dotDotDotToken) continue
|
|
3182
3214
|
const localName = el.name.text
|
|
3183
3215
|
const sourceKey = el.propertyName && ts.isIdentifier(el.propertyName) ? el.propertyName.text : localName
|
|
3184
|
-
|
|
3216
|
+
// Shared with the #2943 `ParamInfo` overlay below so "what does this
|
|
3217
|
+
// default look like" (source text, `defaultContainsArrow`) has one
|
|
3218
|
+
// computation, not two independently-drifting ones.
|
|
3219
|
+
const defaultFields = el.initializer ? bindingElementDefaultFields(el.initializer, ctx) : undefined
|
|
3185
3220
|
const baseValue = `${propsName}.${sourceKey}`
|
|
3186
|
-
|
|
3187
|
-
|
|
3221
|
+
// Parenthesize an arrow/function-expression default (#2940) — `??`
|
|
3222
|
+
// binds tighter than an arrow head, so a bare `props.fmt ?? (v) =>
|
|
3223
|
+
// ...` is a SyntaxError in the emitted JS. Same rule
|
|
3224
|
+
// `propReadFallback` already applies to every other live prop read.
|
|
3225
|
+
const value = defaultFields?.defaultValue !== undefined
|
|
3226
|
+
? `${baseValue} ?? ${coalesceDefaultText(defaultFields.defaultValue, defaultFields.defaultContainsArrow)}`
|
|
3227
|
+
: baseValue
|
|
3188
3228
|
const freeIdentifiers = el.initializer ? extractFreeIdentifiersFromNode(el.initializer) : new Set([propsName])
|
|
3189
3229
|
// Structured parse of `value`, same as the plain-identifier branch
|
|
3190
3230
|
// below (#2724 review finding): without this, a RENAMED destructure
|
|
@@ -3210,7 +3250,7 @@ function collectConstant(
|
|
|
3210
3250
|
type: null,
|
|
3211
3251
|
loc: getSourceLocation(node, ctx.sourceFile, ctx.filePath),
|
|
3212
3252
|
freeIdentifiers,
|
|
3213
|
-
containsArrow:
|
|
3253
|
+
containsArrow: defaultFields?.defaultContainsArrow,
|
|
3214
3254
|
// Body-level destructure-from-props is collected only when
|
|
3215
3255
|
// _isModule === false; binding lives in init scope.
|
|
3216
3256
|
origin: { phase: 'hydrate', scope: 'init', effect: 'pure' },
|
|
@@ -3233,8 +3273,7 @@ function collectConstant(
|
|
|
3233
3273
|
// this is a static, compile-time fact about the source, not a
|
|
3234
3274
|
// liveness question, so (unlike `resolveBodyPropAliases`'s
|
|
3235
3275
|
// `requireLiveRewriteSafe` gate) nothing here excludes them.
|
|
3236
|
-
if (el.initializer) {
|
|
3237
|
-
const defaultFields = bindingElementDefaultFields(el.initializer, ctx)
|
|
3276
|
+
if (el.initializer && defaultFields) {
|
|
3238
3277
|
const existing = ctx.propsParams.find(p => !p.isRest && p.sourceName === undefined && p.name === sourceKey)
|
|
3239
3278
|
if (localName === sourceKey) {
|
|
3240
3279
|
// Unrenamed: overlay onto the type-member entry `sourceKey`
|
|
@@ -6,6 +6,7 @@
|
|
|
6
6
|
*
|
|
7
7
|
* - signal getter calls (`count()`) → `(initialValue)`
|
|
8
8
|
* - memo getter calls (`bars()`) → `(computationBody)`
|
|
9
|
+
* - bare signal/memo getter refs (`count`) → `(() => (initialValue))`
|
|
9
10
|
* - bare inlinable-const refs (`label`) → `(csrInlinable.rewrittenValue)`
|
|
10
11
|
* - bare source-level props refs (`props.x`) → `_p.x`
|
|
11
12
|
*
|
|
@@ -59,7 +60,25 @@ export type CsrInlinabilityMap = Map<string, CsrInlinableEntry | null>
|
|
|
59
60
|
/**
|
|
60
61
|
* A single substitution: when the source expression mentions `name`
|
|
61
62
|
* (either bare or as a zero-arg call, depending on `kind`), the AST
|
|
62
|
-
* walker splices
|
|
63
|
+
* walker splices a replacement in place.
|
|
64
|
+
*
|
|
65
|
+
* `kind` says what `replacement` IS, not just where it matches:
|
|
66
|
+
*
|
|
67
|
+
* - `'call'`: `replacement` is the VALUE the zero-arg accessor `name()`
|
|
68
|
+
* produces (a signal's initial value, a memo's computation body). The
|
|
69
|
+
* call site `name()` substitutes to `(replacement)` directly. A BARE
|
|
70
|
+
* reference to `name` (uncalled — e.g. a getter passed as a component
|
|
71
|
+
* prop, `<Display value={count} />`, #2924) substitutes to
|
|
72
|
+
* `(() => (replacement))` instead: a thunk over the same value, which
|
|
73
|
+
* is the CSR-template mirror of the reference adapter's SSR shim
|
|
74
|
+
* (`const count = () => 5`, `packages/adapter-hono`) — the only
|
|
75
|
+
* reading that lets a bare accessor evaluate identically on SSR and on
|
|
76
|
+
* CSR-fresh-mount. Registered only by `buildSignalMemoEnv` below.
|
|
77
|
+
* - `'identifier'`: `replacement` is already the value a BARE reference
|
|
78
|
+
* to `name` should read as (e.g. an inlinable const's resolved value);
|
|
79
|
+
* there is no corresponding call form. Substitutes to `(replacement)`
|
|
80
|
+
* at both a bare reference and the (non-existent, for this kind) call
|
|
81
|
+
* position.
|
|
63
82
|
*/
|
|
64
83
|
export interface CsrSubstitution {
|
|
65
84
|
kind: 'call' | 'identifier'
|
|
@@ -71,10 +90,11 @@ export interface CsrSubstitution {
|
|
|
71
90
|
|
|
72
91
|
export interface CsrEnv {
|
|
73
92
|
/**
|
|
74
|
-
* Map of name → substitution. Call-kind entries match `name()`
|
|
75
|
-
* (zero-arg call with bare-ident callee
|
|
76
|
-
*
|
|
77
|
-
*
|
|
93
|
+
* Map of name → substitution. Call-kind entries match both `name()`
|
|
94
|
+
* (zero-arg call with bare-ident callee — replaced with the accessor's
|
|
95
|
+
* value) and bare uses of `name` (replaced with a thunk over that same
|
|
96
|
+
* value, #2924); identifier-kind entries match bare uses of `name`
|
|
97
|
+
* outside member-access tails.
|
|
78
98
|
*/
|
|
79
99
|
substitutions: Map<string, CsrSubstitution>
|
|
80
100
|
/** Source-level props object name (`props`); null for destructured-args. */
|
|
@@ -181,6 +201,13 @@ function csrSubstituteOnce(
|
|
|
181
201
|
splices.push({ start: start - OFFSET, end: end - OFFSET, text: `(${sub.replacement})` })
|
|
182
202
|
}
|
|
183
203
|
|
|
204
|
+
// Text spliced in for a BARE (uncalled) reference to `sub`'s name — see
|
|
205
|
+
// `CsrSubstitution`'s docstring for why `call`-kind entries need a
|
|
206
|
+
// thunk here instead of the raw value substituted at the call site
|
|
207
|
+
// (#2924).
|
|
208
|
+
const bareReplacementText = (sub: CsrSubstitution): string =>
|
|
209
|
+
sub.kind === 'call' ? `(() => (${sub.replacement}))` : `(${sub.replacement})`
|
|
210
|
+
|
|
184
211
|
const collectBindingNames = (name: ts.BindingName, out: Set<string>): void => {
|
|
185
212
|
if (ts.isIdentifier(name)) out.add(name.text)
|
|
186
213
|
else if (ts.isObjectBindingPattern(name)) {
|
|
@@ -243,18 +270,21 @@ function csrSubstituteOnce(
|
|
|
243
270
|
}
|
|
244
271
|
|
|
245
272
|
// Shorthand property: `{ X }` — `X` IS both a key and a value
|
|
246
|
-
// reference. Treat the value side as a free ref.
|
|
273
|
+
// reference. Treat the value side as a free ref. `X` is necessarily a
|
|
274
|
+
// BARE reference here (there's no call-syntax shorthand), so a
|
|
275
|
+
// `call`-kind entry needs the thunk form too (#2924) — same as the
|
|
276
|
+
// bare-identifier branch below.
|
|
247
277
|
if (ts.isShorthandPropertyAssignment(node)) {
|
|
248
278
|
if (ts.isIdentifier(node.name) && !isBound(node.name.text)) {
|
|
249
279
|
const sub = env.substitutions.get(node.name.text)
|
|
250
|
-
if (sub
|
|
280
|
+
if (sub) {
|
|
251
281
|
// The shorthand expands to a key-value pair when we substitute,
|
|
252
282
|
// so emit `name: (replacement)` to keep the object literal
|
|
253
283
|
// grammatical. Position spans the whole shorthand.
|
|
254
284
|
splices.push({
|
|
255
285
|
start: node.getStart(sourceFile) - OFFSET,
|
|
256
286
|
end: node.getEnd() - OFFSET,
|
|
257
|
-
text: `${node.name.text}:
|
|
287
|
+
text: `${node.name.text}: ${bareReplacementText(sub)}`,
|
|
258
288
|
})
|
|
259
289
|
}
|
|
260
290
|
}
|
|
@@ -295,12 +325,18 @@ function csrSubstituteOnce(
|
|
|
295
325
|
return
|
|
296
326
|
}
|
|
297
327
|
|
|
298
|
-
// Bare identifier reference.
|
|
328
|
+
// Bare identifier reference. A `call`-kind entry (a signal/memo
|
|
329
|
+
// getter) substitutes to a thunk over its value here, not the value
|
|
330
|
+
// itself — the bare form is the accessor, not a read of it (#2924).
|
|
299
331
|
if (ts.isIdentifier(node)) {
|
|
300
332
|
if (isBound(node.text)) return
|
|
301
333
|
const sub = env.substitutions.get(node.text)
|
|
302
|
-
if (sub
|
|
303
|
-
|
|
334
|
+
if (sub) {
|
|
335
|
+
splices.push({
|
|
336
|
+
start: node.getStart(sourceFile) - OFFSET,
|
|
337
|
+
end: node.getEnd() - OFFSET,
|
|
338
|
+
text: bareReplacementText(sub),
|
|
339
|
+
})
|
|
304
340
|
}
|
|
305
341
|
return
|
|
306
342
|
}
|
|
@@ -553,6 +589,20 @@ export function buildSignalMemoEnv(
|
|
|
553
589
|
replacement: normalizeSignalInitial(s, propsObjectName),
|
|
554
590
|
freeIdentifiers: s.initialFreeIdentifiers ?? new Set(),
|
|
555
591
|
})
|
|
592
|
+
// A bare reference to the SETTER (`<Display update={setCount} />`,
|
|
593
|
+
// any non-`on*`-prefixed prop name — #2924's setter-symmetric case)
|
|
594
|
+
// hits the identical template-scope gap as a bare getter reference:
|
|
595
|
+
// `setCount` only exists as a real closure inside `initCounter`. The
|
|
596
|
+
// reference (Hono) adapter's own SSR shim answers this the same way
|
|
597
|
+
// it answers a bare getter — a module-scope noop (`const setCount =
|
|
598
|
+
// () => {}`) — so an `identifier`-kind entry (not `call`: a setter is
|
|
599
|
+
// never itself CALLED as a zero-arg form the way a getter is) mirrors
|
|
600
|
+
// that shim directly, with no thunk-wrapping needed. `s.setter` is
|
|
601
|
+
// null for the setter-elided declaration form (`const [count] =
|
|
602
|
+
// createSignal(...)`), which has no name to register.
|
|
603
|
+
if (s.setter) {
|
|
604
|
+
substitutions.set(s.setter, { kind: 'identifier', replacement: '() => {}', freeIdentifiers: new Set() })
|
|
605
|
+
}
|
|
556
606
|
}
|
|
557
607
|
for (const m of memos) {
|
|
558
608
|
substitutions.set(m.name, {
|
|
@@ -1621,17 +1621,73 @@ export function addCondAttrToTemplate(html: string, condId: string): string {
|
|
|
1621
1621
|
return `<!--bf-cond-start:${condId}-->${html}<!--bf-cond-end:${condId}-->`
|
|
1622
1622
|
}
|
|
1623
1623
|
|
|
1624
|
-
/**
|
|
1624
|
+
/**
|
|
1625
|
+
* Check if HTML string has a single root element (not multiple siblings).
|
|
1626
|
+
*
|
|
1627
|
+
* Walks top-level tags tracking nesting depth rather than checking only
|
|
1628
|
+
* "does the string end with a closing tag of the root's tag name" — that
|
|
1629
|
+
* lexical shortcut matches `<div id="before">before</div><div id="after">after</div>`
|
|
1630
|
+
* (two sibling `<div>`s) as a false single root, because the STRING happens
|
|
1631
|
+
* to end in `</div>`, the same tag name the root opened with, even though
|
|
1632
|
+
* that closing tag belongs to the second, unrelated div (#2960). A
|
|
1633
|
+
* multi-root branch wrongly classified here gets `bf-c="<id>"` stamped
|
|
1634
|
+
* onto only its first element instead of being comment-wrapped, and
|
|
1635
|
+
* `insert()`'s runtime then drops every sibling but the first on a branch
|
|
1636
|
+
* swap (`updateElementConditional`'s `fragment.firstChild`).
|
|
1637
|
+
*/
|
|
1625
1638
|
function isSingleRootElement(html: string): boolean {
|
|
1626
|
-
|
|
1627
|
-
|
|
1639
|
+
const trimmed = html.trim()
|
|
1640
|
+
// Same gate as before this fix: only a tag name matching `\w+`,
|
|
1641
|
+
// followed immediately by whitespace or `>`, is recognized here — that
|
|
1642
|
+
// is exactly the shape `addCondAttrToTemplate`'s splice regex
|
|
1643
|
+
// (`html.replace(/^(<\w+)(\s|>)/, ...)`) can stamp `bf-c` onto. A root
|
|
1644
|
+
// this gate doesn't recognize (a self-closing tag's own `/`, a
|
|
1645
|
+
// hyphenated custom-element tag) falls through to `false`
|
|
1646
|
+
// (comment-wrap) unchanged from before — widening this gate without
|
|
1647
|
+
// also teaching the splice regex the same shapes would silently drop
|
|
1648
|
+
// BOTH the `bf-c` stamp and the comment-wrap fallback for those roots.
|
|
1649
|
+
const match = trimmed.match(/^<(\w+)[\s>]/)
|
|
1628
1650
|
if (!match) return false
|
|
1629
|
-
|
|
1630
|
-
|
|
1631
|
-
|
|
1632
|
-
//
|
|
1633
|
-
|
|
1634
|
-
|
|
1651
|
+
// Self-closing tags like <br/>, <input/> with nothing else.
|
|
1652
|
+
if (/^<\w+[^>]*\/>$/.test(trimmed)) return true
|
|
1653
|
+
|
|
1654
|
+
// Walk top-level tags tracking nesting depth — see the docstring above
|
|
1655
|
+
// for why (#2960). The comment alternative must come first so it wins at a `<!--`
|
|
1656
|
+
// position — without it, a BarefootJS marker comment like
|
|
1657
|
+
// `<!--bf:s1-->`/`<!--/-->` (a reactive text slot nested inside a
|
|
1658
|
+
// genuine single root) matches the generic tag alternative too (its
|
|
1659
|
+
// body has no `\s`, `/` or `>` before the closing `-->`) and gets
|
|
1660
|
+
// miscounted as an opening tag, throwing off the depth count for
|
|
1661
|
+
// perfectly ordinary single-root branches.
|
|
1662
|
+
const tagPattern = /<!--[\s\S]*?-->|<\/?[^\s/>]+[^>]*>/g
|
|
1663
|
+
let depth = 0
|
|
1664
|
+
let tagMatch: RegExpExecArray | null
|
|
1665
|
+
while ((tagMatch = tagPattern.exec(trimmed))) {
|
|
1666
|
+
const raw = tagMatch[0]
|
|
1667
|
+
if (raw.startsWith('<!--')) continue // HTML comment, not a tag: depth unchanged
|
|
1668
|
+
if (raw.endsWith('/>')) {
|
|
1669
|
+
// Self-closing/void tag (e.g. `<input .../>`). The whole-string
|
|
1670
|
+
// check above only catches a self-closing tag with NOTHING else in
|
|
1671
|
+
// the string — a self-closing root FOLLOWED by more content (e.g.
|
|
1672
|
+
// `<input .../><label>...</label>`, a genuine two-root branch) still
|
|
1673
|
+
// reaches here with the guard above already satisfied (it only
|
|
1674
|
+
// looks at the tag name and the very next character, not whether
|
|
1675
|
+
// the tag turns out to self-close). At depth 0 this tag completes a
|
|
1676
|
+
// whole top-level unit by itself, so treat it exactly like any
|
|
1677
|
+
// other tag's closing point: single root only if nothing follows.
|
|
1678
|
+
// Nested inside another element (depth > 0) it's just an ordinary
|
|
1679
|
+
// child and doesn't affect depth.
|
|
1680
|
+
if (depth === 0) return tagPattern.lastIndex === trimmed.length
|
|
1681
|
+
continue
|
|
1682
|
+
}
|
|
1683
|
+
depth += raw.startsWith('</') ? -1 : 1
|
|
1684
|
+
if (depth === 0) {
|
|
1685
|
+
// Back to depth 0: the root element's own closing tag. Single root
|
|
1686
|
+
// only if nothing else follows it.
|
|
1687
|
+
return tagPattern.lastIndex === trimmed.length
|
|
1688
|
+
}
|
|
1689
|
+
}
|
|
1690
|
+
return false
|
|
1635
1691
|
}
|
|
1636
1692
|
|
|
1637
1693
|
/**
|
|
@@ -11,6 +11,11 @@ import { identifierCallPattern } from '../identifier-pattern.ts'
|
|
|
11
11
|
// All exports from @barefootjs/client/runtime that may be used in generated code
|
|
12
12
|
export const RUNTIME_IMPORT_CANDIDATES = [
|
|
13
13
|
'createSignal', 'createMemo', 'createEffect', 'onCleanup', 'onMount',
|
|
14
|
+
// Coalesces multiple signal writes into one flush (#2985) — omitted here
|
|
15
|
+
// previously even though it's a real `@barefootjs/client` export, so a
|
|
16
|
+
// cross-file reactive factory that called it compiled clean but left
|
|
17
|
+
// `batch` unimported in the generated client bundle.
|
|
18
|
+
'batch',
|
|
14
19
|
'hydrate', 'insert', 'getLoopChildren', 'getLoopNodes', 'mapArray', 'mapArrayAnchored', 'mapArrayLazy', 'patchLeaf', 'createDisposableEffect',
|
|
15
20
|
// Resolves the real DOM container for a loop nested inside a loop-row
|
|
16
21
|
// conditional's branch when the conditional's wrapper element carries no
|
|
@@ -167,7 +167,19 @@ function rewriteBodyAliasReads(code: string, ctx: ClientJsContext): string {
|
|
|
167
167
|
located.set(decl.name.text, {
|
|
168
168
|
key: alias.key,
|
|
169
169
|
defaultValue: parsedInit.fallback?.getText(sourceFile),
|
|
170
|
-
|
|
170
|
+
// `alias.defaultContainsArrow` mirrors `c.containsArrow`, computed
|
|
171
|
+
// against the ORIGINAL initializer before #2940's fix wrapped an
|
|
172
|
+
// arrow/function-expression default in its own protective parens
|
|
173
|
+
// (`coalesceDefaultText`, `analyzer.ts`). `parsedInit.fallback`
|
|
174
|
+
// here re-parses that ALREADY-wrapped `ConstantInfo.value` text, so
|
|
175
|
+
// when the fallback is itself a `ParenthesizedExpression` the wrap
|
|
176
|
+
// has already happened — `livePropReadExpr` below must not apply
|
|
177
|
+
// `coalesceDefaultText` a second time, or a live handler-scoped
|
|
178
|
+
// read doubles up (`(_p.fmt ?? (((v) => ...)))(1)` instead of the
|
|
179
|
+
// minimal `(_p.fmt ?? ((v) => ...))(1)`).
|
|
180
|
+
defaultContainsArrow: alias.defaultContainsArrow && !(
|
|
181
|
+
parsedInit.fallback && ts.isParenthesizedExpression(parsedInit.fallback)
|
|
182
|
+
),
|
|
171
183
|
})
|
|
172
184
|
spans.push([inner.getFullStart(), inner.getEnd()])
|
|
173
185
|
}
|
package/src/props-binding.ts
CHANGED
|
@@ -244,6 +244,20 @@ export function propReadBase(p: LivePropReadInfo): string {
|
|
|
244
244
|
return `${PROPS_PARAM}.${p.sourceName ?? p.name}`
|
|
245
245
|
}
|
|
246
246
|
|
|
247
|
+
/**
|
|
248
|
+
* The text a destructure default contributes as the right operand of a `??`
|
|
249
|
+
* prop read. An arrow (or function-expression) default needs its own
|
|
250
|
+
* parens — `??` binds tighter than an arrow head, so `_p.onInput ?? () =>
|
|
251
|
+
* {}` is a SyntaxError. The one implementation of that decision, shared by
|
|
252
|
+
* `propReadFallback` below (every live `_p.X` read) and `collectConstant`'s
|
|
253
|
+
* body-destructure expansion (`analyzer.ts`, which builds
|
|
254
|
+
* `ConstantInfo.value`) — see #2940, where the analyzer built that value
|
|
255
|
+
* without this check and emitted invalid JS.
|
|
256
|
+
*/
|
|
257
|
+
export function coalesceDefaultText(defaultValue: string, defaultContainsArrow: boolean | undefined): string {
|
|
258
|
+
return defaultContainsArrow ? `(${defaultValue})` : defaultValue
|
|
259
|
+
}
|
|
260
|
+
|
|
247
261
|
/**
|
|
248
262
|
* The `??` right-hand side for a prop read, or null when the bare read is
|
|
249
263
|
* correct. Precedence:
|
|
@@ -263,7 +277,7 @@ export function propReadFallback(
|
|
|
263
277
|
usage: PropUsage | undefined,
|
|
264
278
|
usedAsCondition: boolean,
|
|
265
279
|
): string | null {
|
|
266
|
-
if (p.defaultValue) return
|
|
280
|
+
if (p.defaultValue) return coalesceDefaultText(p.defaultValue, p.defaultContainsArrow)
|
|
267
281
|
if (usage?.usedAsLoopArray) return '[]'
|
|
268
282
|
if (propHasPropertyAccess(usage) && !usedAsCondition) return '{}'
|
|
269
283
|
return null
|
package/src/ssr-defaults.ts
CHANGED
|
@@ -298,6 +298,23 @@ export function extractSsrDefaults(metadata: IRMetadata): Record<string, SsrDefa
|
|
|
298
298
|
// through the chain exactly as before; only the manifest's own seed
|
|
299
299
|
// choice changes, not the static-eval semantics.
|
|
300
300
|
bindings[sig.getter] = value
|
|
301
|
+
|
|
302
|
+
// #2924 (SSR half): a bare reference to the SETTER (`<Display
|
|
303
|
+
// update={setCount} />`) can reach a template-stash adapter's own
|
|
304
|
+
// rendered template text the same way a bare getter reference does
|
|
305
|
+
// (`bf->render_child('display', update => $setCount, ...)`) — Mojo's
|
|
306
|
+
// `Mojo::Template->new(vars => 1)` only declares `my $x` for stash
|
|
307
|
+
// keys it's actually given, so an unseeded setter aborts the render
|
|
308
|
+
// with Perl's strict-mode "Global symbol requires explicit package
|
|
309
|
+
// name". Mirrors Hono's own unconditional SSR shim for every signal's
|
|
310
|
+
// setter (`const setCount: (...) => void = () => {}`, emitted next to
|
|
311
|
+
// the getter's `const count = () => 5` regardless of where either is
|
|
312
|
+
// referenced) — seeded here the same unconditional way, since the
|
|
313
|
+
// value is never read or invoked during SSR (only forwarded through
|
|
314
|
+
// `render_child`), just needs to EXIST in the stash.
|
|
315
|
+
if (sig.setter && !(sig.setter in out)) {
|
|
316
|
+
out[sig.setter] = { value: null }
|
|
317
|
+
}
|
|
301
318
|
}
|
|
302
319
|
|
|
303
320
|
for (const memo of metadata.memos) {
|
|
@@ -330,8 +347,23 @@ export function extractSsrDefaults(metadata: IRMetadata): Record<string, SsrDefa
|
|
|
330
347
|
// pointing at one value. Env-signal getters are excluded from the
|
|
331
348
|
// getter-name set (they're never given an `out` entry above — an
|
|
332
349
|
// adapter's own env-signal binding seeds them instead).
|
|
350
|
+
//
|
|
351
|
+
// #2924 review follow-up: a local-const alias of a signal SETTER
|
|
352
|
+
// (`const alias = setCount`) needs the identical treatment — the setter
|
|
353
|
+
// loop above now seeds `out[sig.setter]` unconditionally, but
|
|
354
|
+
// `collectAliasableGetterNames` only ever returns GETTER/memo names
|
|
355
|
+
// (it's also the Go adapter's own `rootFieldRef` eligibility set, which
|
|
356
|
+
// has no notion of a setter alias, so widening it there would be the
|
|
357
|
+
// wrong fix). Union in the setter names locally instead, mirroring how
|
|
358
|
+
// the CSR side's own `substitutions.has(n)` predicate already covers
|
|
359
|
+
// both without any special-casing (`csr-substitute.ts`'s
|
|
360
|
+
// `buildSignalMemoEnv`, since this PR's setter entries land in the same
|
|
361
|
+
// `substitutions` map as getters).
|
|
333
362
|
{
|
|
334
363
|
const getterNames = collectAliasableGetterNames(metadata.signals, metadata.memos)
|
|
364
|
+
for (const sig of metadata.signals) {
|
|
365
|
+
if (sig.setter && !sig.isModule && !sig.envReader) getterNames.add(sig.setter)
|
|
366
|
+
}
|
|
335
367
|
for (const [alias, origin] of resolveGetterAliases(metadata.localConstants ?? [], (n) => getterNames.has(n))) {
|
|
336
368
|
if (alias in out) continue
|
|
337
369
|
out[alias] = out[origin]
|