@pyreon/compiler 0.18.0 → 0.20.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.
- package/lib/analysis/index.js.html +1 -1
- package/lib/index.js +2081 -1262
- package/lib/types/index.d.ts +310 -125
- package/package.json +14 -12
- package/src/defer-inline.ts +397 -157
- package/src/index.ts +14 -2
- package/src/jsx.ts +784 -19
- package/src/load-native.ts +1 -0
- package/src/manifest.ts +280 -0
- package/src/pyreon-intercept.ts +164 -0
- package/src/react-intercept.ts +59 -0
- package/src/reactivity-lens.ts +190 -0
- package/src/tests/backend-parity-r7-r9.test.ts +91 -0
- package/src/tests/backend-prop-derived-callback-divergence.test.ts +74 -0
- package/src/tests/collapse-bail-census.test.ts +245 -0
- package/src/tests/collapse-key-source-hygiene.test.ts +88 -0
- package/src/tests/defer-inline.test.ts +209 -21
- package/src/tests/detector-tag-consistency.test.ts +2 -0
- package/src/tests/element-valued-const-child.test.ts +61 -0
- package/src/tests/falsy-child-characterization.test.ts +48 -0
- package/src/tests/malformed-input-resilience.test.ts +50 -0
- package/src/tests/manifest-snapshot.test.ts +55 -0
- package/src/tests/native-equivalence.test.ts +104 -3
- package/src/tests/partial-collapse-detector.test.ts +121 -0
- package/src/tests/partial-collapse-emit.test.ts +104 -0
- package/src/tests/partial-collapse-robustness.test.ts +53 -0
- package/src/tests/prop-derived-shadow.test.ts +96 -0
- package/src/tests/pure-call-reactive-args.test.ts +50 -0
- package/src/tests/pyreon-intercept.test.ts +189 -0
- package/src/tests/r13-callback-stmt-equivalence.test.ts +58 -0
- package/src/tests/r14-ssr-mode-parity.test.ts +51 -0
- package/src/tests/r15-elemconst-propderived.test.ts +47 -0
- package/src/tests/r19-defer-inline-robust.test.ts +54 -0
- package/src/tests/r20-backend-equivalence-sweep.test.ts +50 -0
- package/src/tests/react-intercept.test.ts +50 -2
- package/src/tests/reactivity-lens.test.ts +170 -0
- package/src/tests/rocketstyle-collapse.test.ts +208 -0
- package/src/tests/signal-autocall-shadow.test.ts +86 -0
- package/src/tests/sourcemap-fidelity.test.ts +77 -0
- package/src/tests/static-text-baking.test.ts +64 -0
- package/src/tests/transform-state-isolation.test.ts +49 -0
|
@@ -5,11 +5,23 @@
|
|
|
5
5
|
* between the two backends.
|
|
6
6
|
*/
|
|
7
7
|
import { transformJSX_JS } from '../jsx'
|
|
8
|
+
import type { ReactivitySpan } from '../jsx'
|
|
8
9
|
|
|
9
10
|
// Load native if available
|
|
10
|
-
let nativeTransform:
|
|
11
|
-
|
|
12
|
-
|
|
11
|
+
let nativeTransform:
|
|
12
|
+
| ((
|
|
13
|
+
code: string,
|
|
14
|
+
filename: string,
|
|
15
|
+
ssr: boolean,
|
|
16
|
+
knownSignals: string[] | null,
|
|
17
|
+
reactivityLens?: boolean,
|
|
18
|
+
) => {
|
|
19
|
+
code: string
|
|
20
|
+
usesTemplates?: boolean | null
|
|
21
|
+
warnings: Array<{ message: string; line: number; column: number; code: string }>
|
|
22
|
+
reactivityLens?: ReactivitySpan[] | null
|
|
23
|
+
})
|
|
24
|
+
| null = null
|
|
13
25
|
|
|
14
26
|
try {
|
|
15
27
|
const path = require('node:path')
|
|
@@ -39,6 +51,50 @@ function compareSsr(input: string) {
|
|
|
39
51
|
expect(rs.code).toBe(js.code)
|
|
40
52
|
}
|
|
41
53
|
|
|
54
|
+
// Reactivity-lens cross-backend gate (Phase 3). Asserts the Rust binary
|
|
55
|
+
// emits the SAME sidecar the JS oracle does. Two contracts:
|
|
56
|
+
// 1. ADDITIVE — `code` is byte-identical with the lens collected vs
|
|
57
|
+
// not, on BOTH backends (the option never affects codegen).
|
|
58
|
+
// 2. PARITY — the SET of recorded spans is identical JS↔Rust.
|
|
59
|
+
// Spans are compared order-independently: the LSP consumer sorts before
|
|
60
|
+
// rendering, so traversal order is NOT part of the contract — the SET of
|
|
61
|
+
// codegen decisions is. Sorting by (start,end,kind,detail) makes a
|
|
62
|
+
// missing/extra/wrong span fail loudly while ignoring walk order.
|
|
63
|
+
function canon(spans: ReactivitySpan[] | null | undefined): string[] {
|
|
64
|
+
return (spans ?? [])
|
|
65
|
+
.map(
|
|
66
|
+
(s) =>
|
|
67
|
+
`${s.start}|${s.end}|${s.line}|${s.column}|${s.endLine}|${s.endColumn}|${s.kind}|${s.detail}`,
|
|
68
|
+
)
|
|
69
|
+
.sort()
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
function compareLens(input: string, filename = 'test.tsx') {
|
|
73
|
+
const jsOff = transformJSX_JS(input, filename)
|
|
74
|
+
const jsOn = transformJSX_JS(input, filename, { reactivityLens: true })
|
|
75
|
+
const rsOff = nativeTransform!(input, filename, false, null, false)
|
|
76
|
+
const rsOn = nativeTransform!(input, filename, false, null, true)
|
|
77
|
+
|
|
78
|
+
// (1) additive — collecting the lens never changes emitted code, on
|
|
79
|
+
// either backend, and both backends still agree on that code.
|
|
80
|
+
expect(jsOn.code).toBe(jsOff.code)
|
|
81
|
+
expect(rsOn.code).toBe(rsOff.code)
|
|
82
|
+
expect(rsOn.code).toBe(jsOn.code)
|
|
83
|
+
|
|
84
|
+
// The opt-out path must NOT carry the sidecar (parity with JS, which
|
|
85
|
+
// omits the field entirely when not collecting).
|
|
86
|
+
expect(rsOff.reactivityLens == null).toBe(true)
|
|
87
|
+
expect(jsOff.reactivityLens == null).toBe(true)
|
|
88
|
+
|
|
89
|
+
// (2) parity — identical SET of spans.
|
|
90
|
+
const j = canon(jsOn.reactivityLens)
|
|
91
|
+
const r = canon(rsOn.reactivityLens)
|
|
92
|
+
expect(r).toEqual(j)
|
|
93
|
+
// Guard against the degenerate "both empty → trivially equal" pass:
|
|
94
|
+
// every fixture below is chosen to produce ≥1 span.
|
|
95
|
+
expect(j.length).toBeGreaterThan(0)
|
|
96
|
+
}
|
|
97
|
+
|
|
42
98
|
// ─── Cross-backend equivalence ──────────────────────────────────────────────
|
|
43
99
|
|
|
44
100
|
describeNative('Native vs JS equivalence — basic', () => {
|
|
@@ -729,3 +785,48 @@ describeNative('Native vs JS equivalence — DOM properties', () => {
|
|
|
729
785
|
compare('<div><input title={x()} /></div>')
|
|
730
786
|
})
|
|
731
787
|
})
|
|
788
|
+
|
|
789
|
+
// ─── Reactivity-lens parity (Phase 3) ───────────────────────────────────────
|
|
790
|
+
// The Rust binary must emit the SAME sidecar as the JS oracle so the
|
|
791
|
+
// ~80% of users on the native path get the Lens too. Each fixture is
|
|
792
|
+
// chosen to exercise one of the five structural kinds; `compareLens`
|
|
793
|
+
// also asserts the additive guarantee (codegen byte-identical with the
|
|
794
|
+
// option on vs off, both backends) so this block doubles as the native
|
|
795
|
+
// regression guard for "the lens option must never affect output".
|
|
796
|
+
//
|
|
797
|
+
// Bisect-verified: removing ANY of the 6 `ctx.lens(...)` calls in
|
|
798
|
+
// native/src/lib.rs fails the matching fixture below with an
|
|
799
|
+
// array-length / element mismatch in `compareLens`'s parity assertion
|
|
800
|
+
// (e.g. dropping the `reactive-prop` call → `<Comp value={x()} />` fails
|
|
801
|
+
// `expect(r).toEqual(j)` because the Rust set is missing that span);
|
|
802
|
+
// restored → 9/9 pass.
|
|
803
|
+
describeNative('Reactivity-lens — JS↔Rust span parity', () => {
|
|
804
|
+
test('reactive text child (_bindText)', () =>
|
|
805
|
+
compareLens('<div>{count()}</div>'))
|
|
806
|
+
|
|
807
|
+
test('reactive accessor text child (() => …)', () =>
|
|
808
|
+
compareLens('<div>{() => count()}</div>'))
|
|
809
|
+
|
|
810
|
+
test('static-text child (baked once — the high-precision negative)', () =>
|
|
811
|
+
compareLens('<div>{someConst}</div>'))
|
|
812
|
+
|
|
813
|
+
test('reactive-prop on a component (_rp(() => …))', () =>
|
|
814
|
+
compareLens('<Comp value={count()} />'))
|
|
815
|
+
|
|
816
|
+
test('reactive-attr on a DOM element (live binding)', () =>
|
|
817
|
+
compareLens('<div><span title={count()}>hi</span></div>'))
|
|
818
|
+
|
|
819
|
+
test('hoisted-static (module-scope hoist)', () =>
|
|
820
|
+
compareLens('<Comp>{<b class="x">hi</b>}</Comp>'))
|
|
821
|
+
|
|
822
|
+
test('mixed: reactive + static + prop in one tree', () =>
|
|
823
|
+
compareLens(
|
|
824
|
+
'<section><Comp value={count()} /><p>{count()}</p><p>{label}</p></section>',
|
|
825
|
+
))
|
|
826
|
+
|
|
827
|
+
test('multi-line source — line/column parity across newlines', () =>
|
|
828
|
+
compareLens('<div>\n {count()}\n <span title={other()}>\n z</span>\n</div>'))
|
|
829
|
+
|
|
830
|
+
test('signal auto-call shape — declared signal, bare {count} → reactive', () =>
|
|
831
|
+
compareLens('const count = signal(0); const App = () => <div>{count}</div>', 'auto.tsx'))
|
|
832
|
+
})
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* PR 1 of the partial-collapse spec (`.claude/plans/open-work-2026-q3.md`
|
|
3
|
+
* → #1). The shared `on*`-handler-only detector — the build STARTED, not
|
|
4
|
+
* just measured. The bail-reason census (`collapse-bail-census.test.ts`)
|
|
5
|
+
* proved 7.8% of all `@pyreon/ui-components` call sites bail SOLELY on
|
|
6
|
+
* `on*` handlers while every dimension/style prop is a string literal and
|
|
7
|
+
* children are static text; `detectPartialCollapsibleShape` is the exact
|
|
8
|
+
* detector that claims that subset.
|
|
9
|
+
*
|
|
10
|
+
* Contract under test (mirrors the conservative discipline of the full
|
|
11
|
+
* `detectCollapsibleShape` — every uncertain signal bails):
|
|
12
|
+
*
|
|
13
|
+
* - literal-prop + ≥1 `on[A-Z]…` handler + static-text children
|
|
14
|
+
* → { props (literals only), childrenText, handlers[] }
|
|
15
|
+
* - ZERO handlers → null (that IS the full-collapse shape; the partial
|
|
16
|
+
* detector defers so the existing path stays byte-unchanged and the
|
|
17
|
+
* two detectors NEVER both claim a site — the load-bearing separation)
|
|
18
|
+
* - spread / non-handler `{expr}` prop / boolean attr / element child /
|
|
19
|
+
* expression child → null (hard bail, same catalogue as full)
|
|
20
|
+
* - handler expr span (`exprStart`/`exprEnd`) slices the EXACT source
|
|
21
|
+
* of the `{...}` contents (load-bearing for PR 3's emit, which
|
|
22
|
+
* re-emits `code.slice(exprStart, exprEnd)` into `_rsCollapseH`)
|
|
23
|
+
*
|
|
24
|
+
* Bisect-verify (documented in the PR body): replace the body of
|
|
25
|
+
* `detectPartialCollapsibleShape` with `return null` → the 4 POSITIVE
|
|
26
|
+
* specs fail with `expected null to be …`; the 6 NEGATIVE specs still
|
|
27
|
+
* pass (they assert null). Restore → 10/10. That asymmetry proves the
|
|
28
|
+
* positive assertions are load-bearing on the handler-relaxation logic,
|
|
29
|
+
* not passing for the wrong reason.
|
|
30
|
+
*/
|
|
31
|
+
import { describe, expect, it } from 'vitest'
|
|
32
|
+
import { parseSync } from 'oxc-parser'
|
|
33
|
+
import { detectPartialCollapsibleShape } from '../jsx'
|
|
34
|
+
|
|
35
|
+
/** Parse a JSX snippet and return its first JSXElement node (the shape
|
|
36
|
+
* `tryRocketstyleCollapse` receives in production). */
|
|
37
|
+
function firstJsxElement(code: string): any {
|
|
38
|
+
const { program } = parseSync('input.tsx', code, { sourceType: 'module', lang: 'tsx' })
|
|
39
|
+
let found: any = null
|
|
40
|
+
const visit = (node: any): void => {
|
|
41
|
+
if (found || !node || typeof node !== 'object') return
|
|
42
|
+
if (node.type === 'JSXElement') {
|
|
43
|
+
found = node
|
|
44
|
+
return
|
|
45
|
+
}
|
|
46
|
+
for (const k in node) {
|
|
47
|
+
const v = node[k]
|
|
48
|
+
if (Array.isArray(v)) for (const c of v) visit(c)
|
|
49
|
+
else if (v && typeof v === 'object' && typeof v.type === 'string') visit(v)
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
visit(program)
|
|
53
|
+
return found
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
const detect = (code: string) => detectPartialCollapsibleShape(firstJsxElement(code), 'Button')
|
|
57
|
+
|
|
58
|
+
describe('detectPartialCollapsibleShape — PR 1 (on*-handler-only subset)', () => {
|
|
59
|
+
// ── POSITIVE: the partial-collapsible subset ────────────────────────────
|
|
60
|
+
it('claims a literal-prop site with one handler', () => {
|
|
61
|
+
const code =
|
|
62
|
+
'const x = <Button state="primary" size="medium" onClick={handleClick}>Save</Button>'
|
|
63
|
+
const r = detect(code)
|
|
64
|
+
expect(r).not.toBeNull()
|
|
65
|
+
expect(r!.props).toEqual({ state: 'primary', size: 'medium' })
|
|
66
|
+
expect(r!.childrenText).toBe('Save')
|
|
67
|
+
expect(r!.handlers).toHaveLength(1)
|
|
68
|
+
expect(r!.handlers[0]!.name).toBe('onClick')
|
|
69
|
+
expect(code.slice(r!.handlers[0]!.exprStart, r!.handlers[0]!.exprEnd)).toBe('handleClick')
|
|
70
|
+
})
|
|
71
|
+
|
|
72
|
+
it('peels multiple handlers, keeps literal props out of handlers[]', () => {
|
|
73
|
+
const code = 'const x = <Button state="primary" onClick={a} onPointerEnter={b}>Go</Button>'
|
|
74
|
+
const r = detect(code)
|
|
75
|
+
expect(r).not.toBeNull()
|
|
76
|
+
expect(r!.props).toEqual({ state: 'primary' })
|
|
77
|
+
expect(r!.handlers.map((h) => h.name)).toEqual(['onClick', 'onPointerEnter'])
|
|
78
|
+
})
|
|
79
|
+
|
|
80
|
+
it('captures the EXACT expression span for an inline arrow handler', () => {
|
|
81
|
+
const code = 'const x = <Button state="primary" onClick={() => doThing(1)}>Y</Button>'
|
|
82
|
+
const r = detect(code)
|
|
83
|
+
expect(r).not.toBeNull()
|
|
84
|
+
expect(code.slice(r!.handlers[0]!.exprStart, r!.handlers[0]!.exprEnd)).toBe('() => doThing(1)')
|
|
85
|
+
})
|
|
86
|
+
|
|
87
|
+
it('trims static-text children (parity with the full detector)', () => {
|
|
88
|
+
const code = 'const x = <Button state="primary" onClick={h}>\n Save\n</Button>'
|
|
89
|
+
const r = detect(code)
|
|
90
|
+
expect(r).not.toBeNull()
|
|
91
|
+
expect(r!.childrenText).toBe('Save')
|
|
92
|
+
})
|
|
93
|
+
|
|
94
|
+
// ── NEGATIVE: every uncertain shape bails (null) ────────────────────────
|
|
95
|
+
it('returns null for ZERO handlers (defers to the full-collapse path)', () => {
|
|
96
|
+
// The load-bearing separation: a fully-literal site with no handler is
|
|
97
|
+
// the EXISTING full-collapse shape — the partial detector must NOT
|
|
98
|
+
// claim it, so the two never both fire on one site.
|
|
99
|
+
expect(detect('const x = <Button state="primary">Save</Button>')).toBeNull()
|
|
100
|
+
})
|
|
101
|
+
|
|
102
|
+
it('returns null for a spread attribute', () => {
|
|
103
|
+
expect(detect('const x = <Button {...rest} onClick={h}>X</Button>')).toBeNull()
|
|
104
|
+
})
|
|
105
|
+
|
|
106
|
+
it('returns null for a non-handler dynamic prop alongside a handler', () => {
|
|
107
|
+
expect(detect('const x = <Button state={dyn} onClick={h}>X</Button>')).toBeNull()
|
|
108
|
+
})
|
|
109
|
+
|
|
110
|
+
it('returns null for a boolean attribute', () => {
|
|
111
|
+
expect(detect('const x = <Button disabled onClick={h}>X</Button>')).toBeNull()
|
|
112
|
+
})
|
|
113
|
+
|
|
114
|
+
it('returns null for an element child', () => {
|
|
115
|
+
expect(detect('const x = <Button onClick={h}><span /></Button>')).toBeNull()
|
|
116
|
+
})
|
|
117
|
+
|
|
118
|
+
it('returns null for an expression child', () => {
|
|
119
|
+
expect(detect('const x = <Button onClick={h}>{label}</Button>')).toBeNull()
|
|
120
|
+
})
|
|
121
|
+
})
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* PR 3/4 of the partial-collapse build (open-work #1) — the compiler
|
|
3
|
+
* EMIT half: `tryRocketstyleCollapse` falls back to
|
|
4
|
+
* `tryPartialCollapse` (PR 1's `detectPartialCollapsibleShape`) when the
|
|
5
|
+
* FULL `detectCollapsibleShape` bails, emitting `__rsCollapseH(...)` +
|
|
6
|
+
* the residual handlers object (consumed by PR 2's `_rsCollapseH`,
|
|
7
|
+
* #681) instead of bailing to the 5-layer mount.
|
|
8
|
+
*
|
|
9
|
+
* Mirrors the existing `rocketstyle-collapse.test.ts` harness exactly
|
|
10
|
+
* (stubbed resolved-`sites` map — the resolver/plugin scan is the
|
|
11
|
+
* CI-exercised half; this proves the emit contract in isolation, same
|
|
12
|
+
* as the shipped full-collapse emission specs do).
|
|
13
|
+
*
|
|
14
|
+
* Bisect-verify (PR body): revert the one fallback line in
|
|
15
|
+
* `tryRocketstyleCollapse` (`if (!shape) return tryPartialCollapse(...)`
|
|
16
|
+
* → `if (!shape) return false`) → the partial-emit specs fail
|
|
17
|
+
* (`__rsCollapseH(` absent) while the FULL-collapse regression spec
|
|
18
|
+
* still passes (proving the full path is byte-unchanged — the fallback
|
|
19
|
+
* is the only delta). Restore → all pass. Locally bisect-verifiable
|
|
20
|
+
* (minimal `../jsx` graph, like PR 1 #679 — no resolver, no built lib).
|
|
21
|
+
*/
|
|
22
|
+
import { describe, expect, it } from 'vitest'
|
|
23
|
+
import { rocketstyleCollapseKey, transformJSX } from '../jsx'
|
|
24
|
+
|
|
25
|
+
const SITE = {
|
|
26
|
+
templateHtml: '<button data-x="1"><span class="inner">Save</span></button>',
|
|
27
|
+
lightClass: 'pyr-L1 pyr-L2',
|
|
28
|
+
darkClass: 'pyr-D1 pyr-D2',
|
|
29
|
+
rules: ['.pyr-L1{color:red}', '.pyr-D1{color:blue}'],
|
|
30
|
+
ruleKey: 'bundleA',
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
function collapseOpt(candidates: string[], sites: Record<string, typeof SITE>) {
|
|
34
|
+
return {
|
|
35
|
+
collapseRocketstyle: {
|
|
36
|
+
candidates: new Set(candidates),
|
|
37
|
+
sites: new Map(Object.entries(sites)),
|
|
38
|
+
mode: { name: 'useMode', source: '@pyreon/ui-core' },
|
|
39
|
+
},
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
describe('compiler — partial-collapse emission (on*-handler-only)', () => {
|
|
44
|
+
it('emits __rsCollapseH + handlers object + the _rsCollapseH import', () => {
|
|
45
|
+
const key = rocketstyleCollapseKey('Button', { state: 'primary', size: 'medium' }, 'Save')
|
|
46
|
+
const src =
|
|
47
|
+
'const x = <Button state="primary" size="medium" onClick={handleClick}>Save</Button>'
|
|
48
|
+
const { code } = transformJSX(src, 'App.tsx', collapseOpt(['Button'], { [key]: SITE }))
|
|
49
|
+
|
|
50
|
+
expect(code).toContain(
|
|
51
|
+
'__rsCollapseH("<button data-x=\\"1\\"><span class=\\"inner\\">Save</span></button>", ' +
|
|
52
|
+
'"pyr-L1 pyr-L2", "pyr-D1 pyr-D2", () => __pyrMode() === "dark", ' +
|
|
53
|
+
'{ "onClick": (handleClick) })',
|
|
54
|
+
)
|
|
55
|
+
// The runtime helper is imported alongside `_rsCollapse`.
|
|
56
|
+
expect(code).toContain(
|
|
57
|
+
'import { _rsCollapse as __rsCollapse, _rsCollapseH as __rsCollapseH } from "@pyreon/runtime-dom";',
|
|
58
|
+
)
|
|
59
|
+
// Idempotent rule injection still emitted (same as the full path).
|
|
60
|
+
expect(code).toContain('__rsSheet.injectRules(')
|
|
61
|
+
expect(code).toContain('"bundleA"')
|
|
62
|
+
})
|
|
63
|
+
|
|
64
|
+
it('peels multiple handlers verbatim (arrow stays one arg via parens)', () => {
|
|
65
|
+
const key = rocketstyleCollapseKey('Button', { state: 'primary' }, 'Go')
|
|
66
|
+
const src = 'const x = <Button state="primary" onClick={a} onPointerEnter={() => b(1)}>Go</Button>'
|
|
67
|
+
const { code } = transformJSX(src, 'M.tsx', collapseOpt(['Button'], { [key]: SITE }))
|
|
68
|
+
expect(code).toContain('{ "onClick": (a), "onPointerEnter": (() => b(1)) }')
|
|
69
|
+
expect(code).toContain('__rsCollapseH(')
|
|
70
|
+
})
|
|
71
|
+
|
|
72
|
+
it('FULL-collapse path is byte-unchanged (regression): no-handler site still emits plain __rsCollapse', () => {
|
|
73
|
+
const key = rocketstyleCollapseKey('Button', { state: 'primary' }, 'Save')
|
|
74
|
+
const src = 'const x = <Button state="primary">Save</Button>'
|
|
75
|
+
const { code } = transformJSX(src, 'F.tsx', collapseOpt(['Button'], { [key]: SITE }))
|
|
76
|
+
// Plain `__rsCollapse(` call — NOT the H variant — and the import
|
|
77
|
+
// must NOT pull `_rsCollapseH` (the conditional stays off).
|
|
78
|
+
expect(code).toContain('__rsCollapse("<button data-x=\\"1\\"')
|
|
79
|
+
expect(code).not.toContain('__rsCollapseH(')
|
|
80
|
+
expect(code).toContain('import { _rsCollapse as __rsCollapse } from "@pyreon/runtime-dom";')
|
|
81
|
+
expect(code).not.toContain('_rsCollapseH as __rsCollapseH')
|
|
82
|
+
})
|
|
83
|
+
|
|
84
|
+
it('bails (no collapse) on a non-handler dynamic prop alongside a handler', () => {
|
|
85
|
+
const key = rocketstyleCollapseKey('Button', { state: 'primary' }, 'X')
|
|
86
|
+
const src = 'const x = <Button state="primary" foo={dyn} onClick={h}>X</Button>'
|
|
87
|
+
const { code } = transformJSX(src, 'B1.tsx', collapseOpt(['Button'], { [key]: SITE }))
|
|
88
|
+
expect(code).not.toContain('__rsCollapseH')
|
|
89
|
+
})
|
|
90
|
+
|
|
91
|
+
it('bails when the handler-site key has no resolved entry (resolver bailed)', () => {
|
|
92
|
+
const otherKey = rocketstyleCollapseKey('Button', { state: 'secondary' }, 'X')
|
|
93
|
+
const src = 'const x = <Button state="primary" onClick={h}>X</Button>'
|
|
94
|
+
// Only `secondary` is resolved; the `primary` partial site is not.
|
|
95
|
+
const { code } = transformJSX(src, 'B2.tsx', collapseOpt(['Button'], { [otherKey]: SITE }))
|
|
96
|
+
expect(code).not.toContain('__rsCollapseH')
|
|
97
|
+
})
|
|
98
|
+
|
|
99
|
+
it('does nothing when collapseRocketstyle is absent (default OFF)', () => {
|
|
100
|
+
const src = 'const x = <Button state="primary" onClick={h}>X</Button>'
|
|
101
|
+
const { code } = transformJSX(src, 'Off.tsx', {})
|
|
102
|
+
expect(code).not.toContain('__rsCollapseH')
|
|
103
|
+
})
|
|
104
|
+
})
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Compiler hardening — Round 8 (robustness gate; no bug found).
|
|
3
|
+
*
|
|
4
|
+
* The partial-collapse emit path (#683 `tryPartialCollapse` →
|
|
5
|
+
* `__rsCollapseH(...)`, runtime #681, e2e #684) shipped with a happy-path
|
|
6
|
+
* emission test only. This adversarial gate locks its SAFETY contract under
|
|
7
|
+
* inputs that must BAIL or emit cleanly — never throw, never emit
|
|
8
|
+
* un-parseable JS: handlers with commas/ternaries/nested-braces/JSX-in-body/
|
|
9
|
+
* template-literals/signal-closures, multi-handler, dynamic non-handler prop,
|
|
10
|
+
* spread, `onClick={undefined}`, and key-miss. All currently pass (the path
|
|
11
|
+
* is robust); this prevents a future regression in the new code from silently
|
|
12
|
+
* shipping broken collapsed output.
|
|
13
|
+
*/
|
|
14
|
+
import { parseSync } from 'oxc-parser'
|
|
15
|
+
import { describe, expect, it } from 'vitest'
|
|
16
|
+
import { rocketstyleCollapseKey, transformJSX } from '../jsx'
|
|
17
|
+
|
|
18
|
+
const SITE = { templateHtml: '<button><span>Save</span></button>', lightClass: 'L', darkClass: 'D', rules: ['.L{}'], ruleKey: 'b' }
|
|
19
|
+
const opt = (sites: Record<string, typeof SITE>) => ({
|
|
20
|
+
collapseRocketstyle: { candidates: new Set(['Button']), sites: new Map(Object.entries(sites)), mode: { name: 'useMode', source: '@pyreon/ui-core' } },
|
|
21
|
+
})
|
|
22
|
+
const reparses = (c: string): boolean => { try { return !(parseSync('o.tsx', c).errors?.length) } catch { return false } }
|
|
23
|
+
|
|
24
|
+
const CASES: Array<[string, string, Record<string, string>]> = [
|
|
25
|
+
['multi-handler', `const x = <Button state="primary" onClick={a} onPointerEnter={b}>Save</Button>`, { state: 'primary' }],
|
|
26
|
+
['arrow-with-commas', `const x = <Button state="primary" onClick={() => f(a, b, c)}>Save</Button>`, { state: 'primary' }],
|
|
27
|
+
['ternary-handler', `const x = <Button state="primary" onClick={cond ? h1 : h2}>Save</Button>`, { state: 'primary' }],
|
|
28
|
+
['nested-braces-handler', `const x = <Button state="primary" onClick={() => { const o = {a:1}; g(o) }}>Save</Button>`, { state: 'primary' }],
|
|
29
|
+
['signal-closure-handler', `const x = <Button state="primary" onClick={() => s.set(s() + 1)}>Save</Button>`, { state: 'primary' }],
|
|
30
|
+
['jsx-in-handler-body', `const x = <Button state="primary" onClick={() => render(<i/>)}>Save</Button>`, { state: 'primary' }],
|
|
31
|
+
['template-literal-in-handler', `const x = <Button state="primary" onClick={() => log(\`v=\${y}\`)}>Save</Button>`, { state: 'primary' }],
|
|
32
|
+
['dynamic-non-handler-prop-bails', `const x = <Button state={dyn} onClick={h}>Save</Button>`, { state: 'primary' }],
|
|
33
|
+
['spread-bails', `const x = <Button state="primary" {...rest} onClick={h}>Save</Button>`, { state: 'primary' }],
|
|
34
|
+
['onClick-undefined', `const x = <Button state="primary" onClick={undefined}>Save</Button>`, { state: 'primary' }],
|
|
35
|
+
['key-miss-no-collapse', `const x = <Button state="primary" onClick={h}>Save</Button>`, { size: 'x' }],
|
|
36
|
+
]
|
|
37
|
+
|
|
38
|
+
describe('Round 8 — partial-collapse emit is robust (never throws / never emits broken JS)', () => {
|
|
39
|
+
for (const [name, src, props] of CASES) {
|
|
40
|
+
it(name, () => {
|
|
41
|
+
const key = rocketstyleCollapseKey('Button', props, 'Save')
|
|
42
|
+
let code = ''
|
|
43
|
+
let threw: unknown = null
|
|
44
|
+
try {
|
|
45
|
+
code = transformJSX(src, 'App.tsx', opt({ [key]: SITE })).code ?? ''
|
|
46
|
+
} catch (e) {
|
|
47
|
+
threw = e
|
|
48
|
+
}
|
|
49
|
+
expect(threw, `partial-collapse must not throw on: ${src}`).toBeNull()
|
|
50
|
+
expect(reparses(code), `partial-collapse must emit parseable JS for: ${src}`).toBe(true)
|
|
51
|
+
})
|
|
52
|
+
}
|
|
53
|
+
})
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Compiler hardening — Round 2.
|
|
3
|
+
*
|
|
4
|
+
* The prop-derived reactive-props inlining pass (`resolveIdentifiersInText`
|
|
5
|
+
* in jsx.ts) used to substitute every identifier matching a prop-derived
|
|
6
|
+
* const NAME, with zero lexical-scope analysis. Idiomatic code that reuses a
|
|
7
|
+
* short name (`a` / `x` / `i` / `item`) as a callback param or nested local
|
|
8
|
+
* while a prop-derived const of the same name exists was MISCOMPILED:
|
|
9
|
+
*
|
|
10
|
+
* const a = props.x
|
|
11
|
+
* items.map(a => <li>{a}</li>)
|
|
12
|
+
* → items.map((props.x) => <li>{(props.x)}</li>) // un-parseable JS
|
|
13
|
+
*
|
|
14
|
+
* Two cases emitted literally un-parseable JavaScript (arrow-param,
|
|
15
|
+
* catch-param); the rest silently rebound the wrong identifier. The
|
|
16
|
+
* signal-auto-call pass was already scope-aware (`shadowedSignals`); the fix
|
|
17
|
+
* gives the prop-derived pass the same block-accurate shadow discipline.
|
|
18
|
+
*
|
|
19
|
+
* Bisect: revert `scopeBoundPropDerived` / the `!shadowed.has(...)` guard in
|
|
20
|
+
* jsx.ts → the SHADOW specs fail (un-parseable emit / wrong substitution);
|
|
21
|
+
* the no-shadow + transitive specs keep passing (they are the over-suppression
|
|
22
|
+
* guard — the fix must NOT stop inlining where there is no shadow). Restore →
|
|
23
|
+
* all pass.
|
|
24
|
+
*/
|
|
25
|
+
import { parseSync } from 'oxc-parser'
|
|
26
|
+
import { describe, expect, it } from 'vitest'
|
|
27
|
+
import { transformJSX_JS } from '../jsx'
|
|
28
|
+
|
|
29
|
+
const emit = (code: string): string => transformJSX_JS(code, 'c.tsx').code ?? ''
|
|
30
|
+
const parses = (out: string): boolean => {
|
|
31
|
+
try {
|
|
32
|
+
return (parseSync('o.tsx', out).errors?.length ?? 0) === 0
|
|
33
|
+
} catch {
|
|
34
|
+
return false
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
describe('prop-derived inlining — lexical shadowing is respected', () => {
|
|
39
|
+
it('arrow-function parameter shadowing a prop-derived const is NOT rewritten (was un-parseable)', () => {
|
|
40
|
+
const out = emit(`function C(props){ const a = props.x; return <ul>{props.items.map(a => <li>{a}</li>)}</ul> }`)
|
|
41
|
+
expect(parses(out)).toBe(true)
|
|
42
|
+
expect(out).not.toContain('(props.x) =>')
|
|
43
|
+
expect(out).toContain('.map(a => <li>{a}</li>)')
|
|
44
|
+
})
|
|
45
|
+
|
|
46
|
+
it('catch-clause parameter shadowing a prop-derived const is NOT rewritten (was un-parseable)', () => {
|
|
47
|
+
const out = emit(`function C(props){ const e = props.err; const h = () => { try {} catch (e) { return e } }; return <i>{h()}</i> }`)
|
|
48
|
+
expect(parses(out)).toBe(true)
|
|
49
|
+
expect(out).toContain('catch (e)')
|
|
50
|
+
expect(out).toContain('return e')
|
|
51
|
+
})
|
|
52
|
+
|
|
53
|
+
it('named-function parameter shadowing a prop-derived const keeps the parameter binding', () => {
|
|
54
|
+
const out = emit(`function C(props){ const x = props.x; function row(x){ return <td>{x}</td> } return <table>{props.rows.map(row)}</table> }`)
|
|
55
|
+
expect(parses(out)).toBe(true)
|
|
56
|
+
expect(out).toContain('function row(x)')
|
|
57
|
+
expect(out).toMatch(/__t0\.data = x\b/) // the row PARAM, not (props.x)
|
|
58
|
+
})
|
|
59
|
+
|
|
60
|
+
it('nested const shadowing a prop-derived const is not clobbered', () => {
|
|
61
|
+
const out = emit(`function C(props){ const a = props.x; const g = () => { const a = 7; return a }; return <b>{g()}</b> }`)
|
|
62
|
+
expect(parses(out)).toBe(true)
|
|
63
|
+
expect(out).toContain('const a = 7; return a')
|
|
64
|
+
expect(out).not.toContain('const a = 7; return (props.x)')
|
|
65
|
+
})
|
|
66
|
+
|
|
67
|
+
it('block-scoped shadow does NOT over-suppress an outer-scope reference (block accuracy)', () => {
|
|
68
|
+
const out = emit(`function C(props){ const a = props.x; { const a = 'inner'; } return <div>{a}</div> }`)
|
|
69
|
+
expect(out).toContain('(props.x)') // {a} is OUTSIDE the shadowing block → still inlined
|
|
70
|
+
})
|
|
71
|
+
})
|
|
72
|
+
|
|
73
|
+
describe('prop-derived inlining — no-shadow paths still inline (over-suppression guard)', () => {
|
|
74
|
+
it('non-shadowing callback still inlines the prop-derived const', () => {
|
|
75
|
+
const out = emit(`function C(props){ const a = props.x; return <ul>{props.items.map(it => <li>{a}{it}</li>)}</ul> }`)
|
|
76
|
+
expect(out).toContain('(props.x)')
|
|
77
|
+
expect(out).toContain('it') // the genuine callback param is untouched
|
|
78
|
+
})
|
|
79
|
+
|
|
80
|
+
it('transitive chain still resolves through to props', () => {
|
|
81
|
+
const out = emit(`function C(props){ const a = props.x; const b = a + 1; const c = b * 2; return <div>{c}</div> }`)
|
|
82
|
+
expect(out).toContain('(((props.x) + 1) * 2)')
|
|
83
|
+
})
|
|
84
|
+
|
|
85
|
+
// Scope PRECISION: a `for (let i=…)` head block-scopes `i` to the loop. A
|
|
86
|
+
// `return i` AFTER the loop is out of the loop's scope and resolves to the
|
|
87
|
+
// OUTER prop-derived `const i` — so it MUST still inline. (Proven: in plain
|
|
88
|
+
// JS, `const i='OUTER'; (() => { for(let i=0;i<3;i++){} return i })()` ===
|
|
89
|
+
// 'OUTER'.) This guards against an over-broad shadow heuristic that would
|
|
90
|
+
// wrongly treat the non-escaping loop var as a shadow at the post-loop use.
|
|
91
|
+
it('does NOT over-suppress: post-loop reference is the outer prop-derived const', () => {
|
|
92
|
+
const out = emit(`function C(props){ const i = props.start; const f = () => { for (let i=0;i<3;i++){} return i }; return <s>{f()}</s> }`)
|
|
93
|
+
expect(parses(out)).toBe(true)
|
|
94
|
+
expect(out).toContain('return (props.start)')
|
|
95
|
+
})
|
|
96
|
+
})
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Compiler hardening — Round 4 (contract lock; hypothesis disproven = no bug).
|
|
3
|
+
*
|
|
4
|
+
* Hypothesis probed: does a "pure" call (Math.* / JSON.* / Object.* / String /
|
|
5
|
+
* Number — the ~40 fns `isPureStaticCall` treats as side-effect-free) get
|
|
6
|
+
* mis-classified as STATIC when its ARGUMENTS are reactive (signal / prop)?
|
|
7
|
+
* That would silently drop reactivity (the value renders once, never updates).
|
|
8
|
+
*
|
|
9
|
+
* Result: NOT a bug — `shouldWrap`/`isDynamic` correctly inspect arguments, so
|
|
10
|
+
* a pure callee with a dynamic arg is still reactively wrapped. This file is
|
|
11
|
+
* the self-discriminating regression gate for that contract: if someone later
|
|
12
|
+
* "optimizes" pure-call detection to skip wrapping by callee name alone, these
|
|
13
|
+
* specs fail (the exact stale-render bug that optimization would introduce).
|
|
14
|
+
*
|
|
15
|
+
* Bisect: make `isPureStaticCall` short-circuit `shouldWrap` regardless of
|
|
16
|
+
* args → every spec here fails (emit becomes `textContent = Math.max(n(),0)`,
|
|
17
|
+
* no `_bind`). Restore → all pass. The fully-static control proves the gate
|
|
18
|
+
* does not just assert "always reactive".
|
|
19
|
+
*/
|
|
20
|
+
import { describe, expect, it } from 'vitest'
|
|
21
|
+
import { transformJSX_JS } from '../jsx'
|
|
22
|
+
|
|
23
|
+
const emit = (c: string): string => transformJSX_JS(c, 'c.tsx').code ?? ''
|
|
24
|
+
const isReactive = (o: string): boolean => /_bind\(|_bindText\(/.test(o)
|
|
25
|
+
|
|
26
|
+
describe('Round 4 — pure call with a DYNAMIC argument must stay reactive', () => {
|
|
27
|
+
it('Math.max(signal(), 0) is reactively wrapped', () => {
|
|
28
|
+
expect(isReactive(emit(`function C(){ const n=signal(0); return <div>{Math.max(n(),0)}</div> }`))).toBe(true)
|
|
29
|
+
})
|
|
30
|
+
it('Math.round(props.x) is reactively wrapped', () => {
|
|
31
|
+
expect(isReactive(emit(`function C(p){ return <div>{Math.round(p.x)}</div> }`))).toBe(true)
|
|
32
|
+
})
|
|
33
|
+
it('JSON.stringify(props.o) is reactively wrapped', () => {
|
|
34
|
+
expect(isReactive(emit(`function C(p){ return <div>{JSON.stringify(p.o)}</div> }`))).toBe(true)
|
|
35
|
+
})
|
|
36
|
+
it('Object.keys(props.o).length is reactively wrapped', () => {
|
|
37
|
+
expect(isReactive(emit(`function C(p){ return <div>{Object.keys(p.o).length}</div> }`))).toBe(true)
|
|
38
|
+
})
|
|
39
|
+
it('String(props.x) is reactively wrapped', () => {
|
|
40
|
+
expect(isReactive(emit(`function C(p){ return <div>{String(p.x)}</div> }`))).toBe(true)
|
|
41
|
+
})
|
|
42
|
+
|
|
43
|
+
// Control: a pure call with ONLY static args is correctly STATIC (proves
|
|
44
|
+
// the gate is not vacuously "everything is reactive").
|
|
45
|
+
it('Math.max(1,2,3) with only static args is NOT wrapped', () => {
|
|
46
|
+
const o = emit(`function C(){ return <div>{Math.max(1,2,3)}</div> }`)
|
|
47
|
+
expect(isReactive(o)).toBe(false)
|
|
48
|
+
expect(o).toContain('textContent = Math.max(1,2,3)')
|
|
49
|
+
})
|
|
50
|
+
})
|