@barefootjs/jsx 0.35.7 → 0.36.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (36) hide show
  1. package/dist/analyzer.d.ts.map +1 -1
  2. package/dist/directives.d.ts +70 -0
  3. package/dist/directives.d.ts.map +1 -0
  4. package/dist/index.d.ts +1 -0
  5. package/dist/index.d.ts.map +1 -1
  6. package/dist/index.js +174 -120
  7. package/dist/ir-to-client-js/compute-inlinability.d.ts +10 -0
  8. package/dist/ir-to-client-js/compute-inlinability.d.ts.map +1 -1
  9. package/dist/ir-to-client-js/compute-scope.d.ts.map +1 -1
  10. package/dist/ir-to-client-js/csr-substitute.d.ts +25 -5
  11. package/dist/ir-to-client-js/csr-substitute.d.ts.map +1 -1
  12. package/dist/ir-to-client-js/emit-module-level.d.ts.map +1 -1
  13. package/dist/ir-to-client-js/imports.d.ts +2 -2
  14. package/dist/ir-to-client-js/imports.d.ts.map +1 -1
  15. package/dist/jsx-to-ir.d.ts.map +1 -1
  16. package/dist/preprocess-inline-jsx-callbacks.d.ts.map +1 -1
  17. package/dist/ssr-defaults.d.ts.map +1 -1
  18. package/package.json +2 -2
  19. package/src/__tests__/csr-substitute-bare-getter.test.ts +117 -0
  20. package/src/__tests__/csr-template-scope-soundness.test.ts +125 -0
  21. package/src/__tests__/module-function-helper-chain-3000.test.ts +200 -0
  22. package/src/__tests__/module-helper-scope-2986.test.ts +296 -0
  23. package/src/__tests__/module-scope-arrow-const-2988.test.ts +180 -0
  24. package/src/__tests__/reactive-factory-cross-file.test.ts +37 -0
  25. package/src/__tests__/ssr-defaults.test.ts +24 -2
  26. package/src/analyzer.ts +48 -19
  27. package/src/directives.ts +75 -0
  28. package/src/index.ts +3 -0
  29. package/src/ir-to-client-js/compute-inlinability.ts +92 -16
  30. package/src/ir-to-client-js/compute-scope.ts +72 -18
  31. package/src/ir-to-client-js/csr-substitute.ts +61 -11
  32. package/src/ir-to-client-js/emit-module-level.ts +10 -1
  33. package/src/ir-to-client-js/imports.ts +5 -0
  34. package/src/jsx-to-ir.ts +1 -1
  35. package/src/preprocess-inline-jsx-callbacks.ts +2 -1
  36. package/src/ssr-defaults.ts +32 -0
@@ -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 `replacement` in place.
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) and replace the entire
76
- * call expression; identifier-kind entries match bare uses of
77
- * `name` outside member-access tails.
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 && sub.kind === 'identifier') {
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}: (${sub.replacement})`,
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 && sub.kind === 'identifier') {
303
- recordSubstitution(node.getStart(sourceFile), node.getEnd(), sub)
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, {
@@ -47,7 +47,16 @@ export function emitModuleLevelDeclarations(
47
47
  const lines: string[] = []
48
48
  for (const constant of moduleLevelConstants) {
49
49
  if (!constant.value) continue
50
- lines.push(`var ${constant.name} = ${constant.name} ?? ${constant.value}`)
50
+ // An arrow-valued initializer needs parenthesising on the right of
51
+ // `??` — `x ?? (s) => …` is a SyntaxError because `??`'s operand
52
+ // grammar (BitwiseORExpression) is higher-precedence than an arrow
53
+ // function (AssignmentExpression); only an already-parenthesised
54
+ // value (an object literal, `(a, b) => …`) survives unwrapped, and
55
+ // wrapping those again is a harmless no-op (#2988). Gated on
56
+ // `containsArrow` specifically so existing fixtures with
57
+ // parenthesised object-literal module consts don't churn.
58
+ const value = constant.containsArrow ? `(${constant.value})` : constant.value
59
+ lines.push(`var ${constant.name} = ${constant.name} ?? ${value}`)
51
60
  }
52
61
  for (const fn of moduleLevelFunctions) {
53
62
  const paramStr = fn.params.map(p => {
@@ -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
package/src/jsx-to-ir.ts CHANGED
@@ -5,6 +5,7 @@
5
5
  */
6
6
 
7
7
  import ts from 'typescript'
8
+ import { CLIENT_DIRECTIVE_INTERIOR_RE } from './directives.ts'
8
9
  import {
9
10
  type IRNode,
10
11
  type IRElement,
@@ -272,7 +273,6 @@ interface TransformContext {
272
273
  * specially), so we read the trivia text directly and parse block
273
274
  * comments out of it.
274
275
  */
275
- const CLIENT_DIRECTIVE_INTERIOR_RE = /^\s*@client\s*$/
276
276
  const BLOCK_COMMENT_RE = /\/\*([\s\S]*?)\*\//g
277
277
  function hasLeadingClientDirective(expr: ts.Expression, sourceFile: ts.SourceFile): boolean {
278
278
  const trivia = sourceFile.text.slice(expr.pos, expr.getStart(sourceFile))
@@ -31,6 +31,7 @@
31
31
  */
32
32
 
33
33
  import ts from 'typescript'
34
+ import { isUseClientDirectiveText } from './directives.ts'
34
35
  import type { CompilerError, SourceLocation } from './types.ts'
35
36
  import { ErrorCodes, createError } from './errors.ts'
36
37
 
@@ -97,7 +98,7 @@ function runSinglePass(
97
98
  const hasUseClient = sourceFile.statements.some(stmt =>
98
99
  ts.isExpressionStatement(stmt) &&
99
100
  ts.isStringLiteral(stmt.expression) &&
100
- (stmt.expression.text === 'use client' || stmt.expression.text === "'use client'")
101
+ isUseClientDirectiveText(stmt.expression.text)
101
102
  )
102
103
 
103
104
  // Without `'use client'` the synthesized component has nowhere to
@@ -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]