@barefootjs/jsx 0.31.9 → 0.32.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.
@@ -1,165 +0,0 @@
1
- /**
2
- * TEST-HARNESS ONLY. Evaluate a signal initializer / prop-default source
3
- * expression against a mock `props` object by EXECUTING it (`new
4
- * Function`, with `undefined` shadowing the globals listed below), the
5
- * same way the Hono/CSR conformance reference produces its value — by
6
- * actually running the component. Never call this from a build path (`bf
7
- * build`, `compileJSX`, any adapter's `generate`); it exists only so each
8
- * adapter's `test-render.ts` conformance harness can seed a signal/
9
- * prop-default for a non-JS-runtime SSR render (PHP/Ruby/Python/Perl/Go)
10
- * that matches what Hono would compute. (#2209)
11
- *
12
- * NOT a security sandbox — the global shadowing below is a determinism
13
- * tripwire (catches the common accidental-nondeterminism case, e.g.
14
- * `Date.now()`), not containment: a sufficiently creative expression can
15
- * still reach the real global scope (`new Function` bodies execute
16
- * unlexically-scoped, so e.g. `[].constructor.constructor('return
17
- * Date')()` recovers `Date` even though the bare name is shadowed, and
18
- * `eval` can't be shadowed at all — JS forbids declaring or binding a
19
- * parameter literally named `eval` in strict-mode code). Do not widen this
20
- * module's use to anything but first-party fixture source. The actual
21
- * trust boundary is that the evaluated text is first-party fixture
22
- * source, already compiled by this same process — the conformance harness
23
- * already executes fixture-derived code far more invasively (spawning
24
- * `ruby`/`php`/`perl`/`go run` on generated programs). This module is
25
- * never reachable from anything a real end user's untrusted input could
26
- * influence.
27
- *
28
- * Why real execution instead of a hand-rolled evaluator: the initializer
29
- * source is an arbitrary JS-subset expression over `props` (e.g. `(props.x
30
- * ?? []).map(t => ({ ...t, editing: false }))`, #2209's actual repro) —
31
- * every previous approach here was a regex/pattern-match over a small
32
- * catalogue of recognized shapes (`props.x`, `props.x ?? default`, a bare
33
- * literal), and #2209 is literally "the catalogue missed a shape" (the
34
- * THIRD such miss on this codebase, per the superseded
35
- * `evaluate-signal-init.test.ts`'s own #1672 pins). A hand-written
36
- * evaluator over `ParsedExpr` would face the same drift, and can't
37
- * represent object spread (`{ ...t, editing: false }`) without extending
38
- * `ParsedExpr` — a production-compiler change that ripples into every
39
- * adapter's exhaustive switch, disproportionate for a test-only need.
40
- * `new Function` delegates parsing to the JS engine itself — CLAUDE.md's
41
- * regex-parsing ban exists precisely to avoid the false-match/missed-shape
42
- * failure mode this replaces.
43
- *
44
- * Shadowed as `undefined` (best-effort determinism, not containment — see
45
- * above): `globalThis`, `window`, `document`, `Date`, `Math`, `crypto`,
46
- * `performance`, `fetch`, `setTimeout`, `setInterval`, `require`,
47
- * `process`, `Function`. (`eval` is deliberately absent from this list —
48
- * it cannot be shadowed as a parameter name in strict-mode code; see
49
- * above.)
50
- *
51
- * `props` bare-identifier destructured params (`createSignal(count)` where
52
- * `count` is a destructured prop, not a `props.x` member) are NOT bound —
53
- * the evaluator only exposes `props` — so such an initializer throws
54
- * `ReferenceError` and falls back to "unset", matching every prior
55
- * evaluator's behavior for that shape. Extending the environment with
56
- * `ir.metadata.propsParams` bindings is a natural follow-up if a fixture
57
- * ever needs it.
58
- */
59
-
60
- const BLOCKED_GLOBALS = [
61
- 'globalThis',
62
- 'window',
63
- 'document',
64
- 'Date',
65
- 'Math',
66
- 'crypto',
67
- 'performance',
68
- 'fetch',
69
- 'setTimeout',
70
- 'setInterval',
71
- 'require',
72
- 'process',
73
- 'Function',
74
- ] as const
75
-
76
- export type SignalInitEvalResult = { ok: true; value: unknown } | { ok: false }
77
-
78
- /**
79
- * A value the harness's downstream language serializers (JSON/Python/PHP/
80
- * Perl/Ruby literal builders) can actually marshal: `null`, a boolean, a
81
- * number (including non-finite — several serializers already special-case
82
- * NaN/Infinity), a string, a dense array with no holes and no `undefined`
83
- * elements, or a plain object (`Object.prototype` or null prototype only —
84
- * rejects class instances, functions, Maps/Sets, etc). Rejects genuine
85
- * cycles; a shared (non-cyclic) reference appearing more than once — e.g.
86
- * the same object at two array indices — is fine (JSON-equivalent
87
- * behavior just duplicates it), so `ancestors` tracks only the current
88
- * recursion path, not every value ever visited.
89
- */
90
- function isTransportable(value: unknown, ancestors: Set<unknown> = new Set()): boolean {
91
- if (value === null) return true
92
- const t = typeof value
93
- if (t === 'boolean' || t === 'number' || t === 'string') return true
94
- if (t !== 'object') return false
95
- if (ancestors.has(value)) return false // a real cycle (value is its own ancestor)
96
- ancestors.add(value)
97
- try {
98
- if (Array.isArray(value)) {
99
- // `Array.prototype.every` silently skips holes (`[1, , 3]`), so a
100
- // sparse array would otherwise pass — compare against the own
101
- // enumerable key count (holes aren't own keys) to catch that.
102
- if (Object.keys(value).length !== value.length) return false
103
- return value.every(el => el !== undefined && isTransportable(el, ancestors))
104
- }
105
- const proto = Object.getPrototypeOf(value)
106
- if (proto !== Object.prototype && proto !== null) return false
107
- return Object.values(value as Record<string, unknown>).every(v => isTransportable(v, ancestors))
108
- } finally {
109
- ancestors.delete(value)
110
- }
111
- }
112
-
113
- /**
114
- * Evaluate `expr` (a JS-subset source expression) against `props`. Returns
115
- * `{ ok: false }` when the expression fails to parse, throws at evaluation
116
- * time (e.g. a `ReferenceError` for an unbound identifier), or evaluates to
117
- * something the downstream serializers can't marshal (see
118
- * {@link isTransportable}).
119
- */
120
- export function tryEvaluateSignalInit(
121
- expr: string,
122
- props?: Record<string, unknown>,
123
- ): SignalInitEvalResult {
124
- const src = expr.trim()
125
- if (src === '') return { ok: false }
126
- let fn: (props: Record<string, unknown>, ...blocked: undefined[]) => unknown
127
- try {
128
- // `props` plus the blocked-globals shadows are the only bindings
129
- // passed in — best-effort determinism, not containment; see the file
130
- // docstring for the trust-boundary rationale (test-harness-only,
131
- // never a build path).
132
- fn = new Function(
133
- 'props',
134
- ...BLOCKED_GLOBALS,
135
- `'use strict'; return (\n${src}\n);`,
136
- ) as typeof fn
137
- } catch {
138
- return { ok: false }
139
- }
140
- try {
141
- const value = fn(props ?? {})
142
- // `undefined` at the TOP level is a genuine, distinguishable result
143
- // (e.g. an explicit `undefined` initializer, or `props.x` with no `x`
144
- // and no `??` fallback) — not a marshal failure. `evaluateSignalInit`'s
145
- // wrapper still collapses it to the "skip" outcome; callers that need
146
- // the distinction use this function directly.
147
- if (value === undefined) return { ok: true, value: undefined }
148
- return isTransportable(value) ? { ok: true, value } : { ok: false }
149
- } catch {
150
- return { ok: false }
151
- }
152
- }
153
-
154
- /**
155
- * Drop-in replacement for the harnesses' former per-adapter regex
156
- * evaluator: `null` means "could not evaluate, or evaluated to
157
- * `undefined` — leave the signal/default unseeded", matching every prior
158
- * evaluator's convention (an explicit JS `null` initializer also maps to
159
- * `null` — the same "skip" outcome, since none of these harnesses
160
- * distinguish "explicitly null" from "unset" downstream).
161
- */
162
- export function evaluateSignalInit(expr: string, props?: Record<string, unknown>): unknown {
163
- const result = tryEvaluateSignalInit(expr, props)
164
- return result.ok && result.value !== undefined ? result.value : null
165
- }