@barefootjs/jsx 0.39.1 → 0.39.3
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/adapters/line-statement-text.d.ts +17 -0
- package/dist/adapters/line-statement-text.d.ts.map +1 -0
- package/dist/adapters/parsed-expr-emitter.d.ts +2 -2
- package/dist/adapters/parsed-expr-emitter.d.ts.map +1 -1
- package/dist/analyzer.d.ts +1 -0
- package/dist/analyzer.d.ts.map +1 -1
- package/dist/augment-inherited-props.d.ts +20 -0
- package/dist/augment-inherited-props.d.ts.map +1 -1
- package/dist/html-types.d.ts +15 -0
- package/dist/html-types.d.ts.map +1 -1
- package/dist/index.d.ts +6 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1527 -1169
- package/dist/ir-to-client-js/collect-elements.d.ts.map +1 -1
- package/dist/ir-to-client-js/safe-html.d.ts +1 -1
- package/dist/ir-to-client-js/safe-html.d.ts.map +1 -1
- package/dist/ir-to-client-js/utils.d.ts.map +1 -1
- package/dist/jsx-to-ir.d.ts.map +1 -1
- package/dist/literal-member-index.d.ts +6 -0
- package/dist/literal-member-index.d.ts.map +1 -0
- package/dist/nullable-signal-attr.d.ts +73 -0
- package/dist/nullable-signal-attr.d.ts.map +1 -0
- package/dist/scope/binding-scope.d.ts +11 -0
- package/dist/scope/binding-scope.d.ts.map +1 -1
- package/dist/scope/root-prop-alias.d.ts +43 -0
- package/dist/scope/root-prop-alias.d.ts.map +1 -0
- package/dist/signal-prop-name-collision.d.ts +20 -0
- package/dist/signal-prop-name-collision.d.ts.map +1 -0
- package/dist/ssr-defaults.d.ts.map +1 -1
- package/dist/ssr-seed-plan.d.ts +3 -2
- package/dist/ssr-seed-plan.d.ts.map +1 -1
- package/package.json +2 -2
- package/src/__tests__/__snapshots__/doc-examples.test.ts.snap +3 -3
- package/src/__tests__/attribute-fallback-wrap.test.ts +24 -0
- package/src/__tests__/binding-scope.test.ts +27 -0
- package/src/__tests__/client-template-escape-soundness.test.ts +17 -0
- package/src/__tests__/compiler-stress-1244.test.ts +10 -13
- package/src/__tests__/conditional-fallback-wrap.test.ts +20 -0
- package/src/__tests__/conditional-nested-loop-bindings.test.ts +41 -0
- package/src/__tests__/destructured-from-props-localconst-value.test.ts +3 -2
- package/src/__tests__/html-event-handler-names.test.ts +107 -0
- package/src/__tests__/line-statement-text.test.ts +25 -0
- package/src/__tests__/literal-member-index.test.ts +14 -0
- package/src/__tests__/lookup-literal-const.test.ts +46 -0
- package/src/__tests__/loop-param-accessor-key-member-safety.test.ts +66 -19
- package/src/__tests__/nullable-signal-attr.test.ts +124 -0
- package/src/__tests__/profile-binding-ids.test.ts +102 -0
- package/src/__tests__/root-prop-alias.test.ts +62 -0
- package/src/__tests__/signal-prop-name-collision.test.ts +171 -0
- package/src/__tests__/ssr-defaults.test.ts +6 -7
- package/src/__tests__/ssr-seed-plan.test.ts +23 -0
- package/src/__tests__/staged-ir/02-shadow-guards.test.ts +1 -1
- package/src/__tests__/staged-ir/08-relocate-unit.test.ts +6 -3
- package/src/adapters/line-statement-text.ts +24 -0
- package/src/adapters/parsed-expr-emitter.ts +3 -3
- package/src/analyzer.ts +60 -26
- package/src/augment-inherited-props.ts +49 -0
- package/src/html-types.ts +15 -0
- package/src/index.ts +14 -2
- package/src/ir-to-client-js/collect-elements.ts +15 -10
- package/src/ir-to-client-js/control-flow/plan/build-component-loop.ts +2 -2
- package/src/ir-to-client-js/safe-html.ts +19 -6
- package/src/ir-to-client-js/stringify/declaration-emit.ts +5 -5
- package/src/ir-to-client-js/utils.ts +88 -117
- package/src/jsx-to-ir.ts +21 -2
- package/src/literal-member-index.ts +12 -0
- package/src/nullable-signal-attr.ts +156 -0
- package/src/scope/binding-scope.ts +31 -1
- package/src/scope/root-prop-alias.ts +81 -0
- package/src/signal-prop-name-collision.ts +164 -0
- package/src/ssr-defaults.ts +3 -5
- package/src/ssr-seed-plan.ts +14 -2
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Nullish-attribute omission for signal reads (#3304).
|
|
3
|
+
*
|
|
4
|
+
* Hono omits an attribute whose value is `undefined` or `null`
|
|
5
|
+
* (`title={s()}` with `s()` undefined renders no `title`). A DSL adapter
|
|
6
|
+
* emits `name="<value>"` unconditionally, so a nil value renders
|
|
7
|
+
* `title=""` unless the adapter guards the attribute on a nil check. Each
|
|
8
|
+
* DSL adapter already guards a bare reference to a no-default optional prop
|
|
9
|
+
* that way; this module is the one decision for the signal half, which
|
|
10
|
+
* every adapter consults instead of re-deriving it.
|
|
11
|
+
*
|
|
12
|
+
* Only the SSR value matters here — a signal's SSR value is its initial
|
|
13
|
+
* value — and the guard is presence-only: a non-nil value renders exactly
|
|
14
|
+
* as it did unguarded, so over-including a signal never changes output for
|
|
15
|
+
* a non-nil value.
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
import type { ParsedExpr } from './expression-parser.ts'
|
|
19
|
+
import type { ComponentIR, MemoInfo, SignalInfo, TypeInfo } from './types.ts'
|
|
20
|
+
|
|
21
|
+
function isNullishType(type: TypeInfo): boolean {
|
|
22
|
+
return type.kind === 'primitive' && (type.primitive === 'undefined' || type.primitive === 'null')
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/** `T | undefined` / `T | null`, or bare `undefined` / `null`. */
|
|
26
|
+
function admitsNullish(type: TypeInfo): boolean {
|
|
27
|
+
if (isNullishType(type)) return true
|
|
28
|
+
return type.kind === 'union' && (type.unionTypes ?? []).some(isNullishType)
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/** `undefined` / `null` as a parsed initial value. */
|
|
32
|
+
export function isNullishLiteral(expr: ParsedExpr | undefined): boolean {
|
|
33
|
+
if (!expr) return false
|
|
34
|
+
if (expr.kind === 'identifier') return expr.name === 'undefined'
|
|
35
|
+
return expr.kind === 'literal' && expr.literalType === 'null'
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Whether the signal's SSR value can be nullish. The witness is its type
|
|
40
|
+
* admitting `undefined`/`null` (a zero-arg `createSignal<T>()` is typed
|
|
41
|
+
* `T | undefined`, #3215), or, for an untyped signal, a literal `undefined` /
|
|
42
|
+
* `null` initial value.
|
|
43
|
+
*/
|
|
44
|
+
export function isNullableSignal(signal: SignalInfo): boolean {
|
|
45
|
+
if (admitsNullish(signal.type)) return true
|
|
46
|
+
return signal.type.kind === 'unknown' && isNullishLiteral(signal.parsed)
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/** Getter names of the component's signals whose SSR value can be nullish. */
|
|
50
|
+
export function collectNullableSignalGetters(ir: ComponentIR): Set<string> {
|
|
51
|
+
return new Set((ir.metadata.signals ?? []).filter(isNullableSignal).map(s => s.getter))
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* The getter an attribute value reads when the whole value is a bare,
|
|
56
|
+
* zero-argument getter call (`s()`); `null` for any other shape. A member
|
|
57
|
+
* read, a ternary or a template literal around the call is not matched:
|
|
58
|
+
* the attribute's presence then depends on more than the signal.
|
|
59
|
+
*/
|
|
60
|
+
export function bareGetterCallName(parsed: ParsedExpr | undefined): string | null {
|
|
61
|
+
if (!parsed || parsed.kind !== 'call' || parsed.args.length !== 0) return null
|
|
62
|
+
return parsed.callee.kind === 'identifier' ? parsed.callee.name : null
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* The signal getter whose nil check should guard this attribute, or `null`
|
|
67
|
+
* when the attribute is not a bare read of a nullable signal. `nullable` is
|
|
68
|
+
* the set `collectNullableSignalGetters` built for the component.
|
|
69
|
+
*/
|
|
70
|
+
export function nullableSignalAttrGetter(
|
|
71
|
+
parsed: ParsedExpr | undefined,
|
|
72
|
+
nullable: ReadonlySet<string>,
|
|
73
|
+
): string | null {
|
|
74
|
+
const getter = bareGetterCallName(parsed)
|
|
75
|
+
return getter !== null && nullable.has(getter) ? getter : null
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* What `attrValueMayBeNullish` needs to know about the component: its
|
|
80
|
+
* nullable signal getters and its memos by name.
|
|
81
|
+
*/
|
|
82
|
+
export interface NullishAttrContext {
|
|
83
|
+
readonly nullableSignals: ReadonlySet<string>
|
|
84
|
+
readonly memos: ReadonlyMap<string, MemoInfo>
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
export function collectNullishAttrContext(ir: ComponentIR): NullishAttrContext {
|
|
88
|
+
return {
|
|
89
|
+
nullableSignals: collectNullableSignalGetters(ir),
|
|
90
|
+
memos: new Map((ir.metadata.memos ?? []).map(m => [m.name, m])),
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* Whether an attribute value's SSR value can be `undefined` / `null`, so a
|
|
96
|
+
* DSL adapter must guard the attribute on a nil check to omit it the way
|
|
97
|
+
* Hono does (#3322) — the derived-value half of the bare-signal guard
|
|
98
|
+
* (`nullableSignalAttrGetter`, #3304). Matches a value that reaches a
|
|
99
|
+
* nullish value through:
|
|
100
|
+
*
|
|
101
|
+
* - a read of a nullable signal or of a memo whose type or body can be
|
|
102
|
+
* nullish (`label()` with `label = createMemo(() => s())`);
|
|
103
|
+
* - an optional member read (`user()?.name`), or a member read of a value
|
|
104
|
+
* that can itself be nullish;
|
|
105
|
+
* - a ternary branch, or an `??` / `||` / `&&` operand the result can be;
|
|
106
|
+
* - a literal `undefined` / `null`.
|
|
107
|
+
*
|
|
108
|
+
* Like the bare-signal guard it is presence-only: a non-nil value renders
|
|
109
|
+
* exactly as unguarded, so over-including a value never changes output for
|
|
110
|
+
* a present value (`''`, `0` and `false` still render). `isShadowed` names
|
|
111
|
+
* a getter a loop binding hides at the attribute site, which is then not
|
|
112
|
+
* the signal / memo; a memo's own body is read in its declaration scope.
|
|
113
|
+
*/
|
|
114
|
+
export function attrValueMayBeNullish(
|
|
115
|
+
parsed: ParsedExpr | undefined,
|
|
116
|
+
ctx: NullishAttrContext,
|
|
117
|
+
isShadowed: (name: string) => boolean = () => false,
|
|
118
|
+
): boolean {
|
|
119
|
+
const visiting = new Set<string>()
|
|
120
|
+
// `shadowed` is the scope the expression is read in: the attribute site's
|
|
121
|
+
// loop bindings at the top level, none inside a memo body — its getters
|
|
122
|
+
// resolve where the memo is declared, not where it is read.
|
|
123
|
+
const walk = (expr: ParsedExpr, shadowed: (name: string) => boolean): boolean => {
|
|
124
|
+
switch (expr.kind) {
|
|
125
|
+
case 'identifier':
|
|
126
|
+
return expr.name === 'undefined'
|
|
127
|
+
case 'literal':
|
|
128
|
+
return expr.literalType === 'null'
|
|
129
|
+
case 'call': {
|
|
130
|
+
const getter = bareGetterCallName(expr)
|
|
131
|
+
if (getter === null || shadowed(getter)) return false
|
|
132
|
+
if (ctx.nullableSignals.has(getter)) return true
|
|
133
|
+
const memo = ctx.memos.get(getter)
|
|
134
|
+
if (!memo || visiting.has(getter)) return false
|
|
135
|
+
if (admitsNullish(memo.type)) return true
|
|
136
|
+
visiting.add(getter)
|
|
137
|
+
const result = memo.parsed !== undefined && walk(memo.parsed, () => false)
|
|
138
|
+
visiting.delete(getter)
|
|
139
|
+
return result
|
|
140
|
+
}
|
|
141
|
+
case 'member':
|
|
142
|
+
return expr.optional || walk(expr.object, shadowed)
|
|
143
|
+
case 'index-access':
|
|
144
|
+
return walk(expr.object, shadowed)
|
|
145
|
+
case 'conditional':
|
|
146
|
+
return walk(expr.consequent, shadowed) || walk(expr.alternate, shadowed)
|
|
147
|
+
case 'logical':
|
|
148
|
+
return expr.op === '&&'
|
|
149
|
+
? walk(expr.left, shadowed) || walk(expr.right, shadowed)
|
|
150
|
+
: walk(expr.right, shadowed)
|
|
151
|
+
default:
|
|
152
|
+
return false
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
return parsed !== undefined && walk(parsed, isShadowed)
|
|
156
|
+
}
|
|
@@ -39,6 +39,7 @@ export type ScopeBindingSource = 'item' | 'index' | 'destructure' | 'preamble' |
|
|
|
39
39
|
|
|
40
40
|
export interface ScopeBinding {
|
|
41
41
|
readonly source: ScopeBindingSource
|
|
42
|
+
readonly stringValue?: boolean
|
|
42
43
|
}
|
|
43
44
|
|
|
44
45
|
export interface ScopeFrame {
|
|
@@ -54,11 +55,22 @@ export interface ScopeFrame {
|
|
|
54
55
|
*/
|
|
55
56
|
export interface LoopBindingSource {
|
|
56
57
|
readonly param: string
|
|
58
|
+
readonly itemType?: LoopValueType | null
|
|
57
59
|
readonly index?: string | null
|
|
58
60
|
readonly paramBindings?: readonly { readonly name: string }[]
|
|
59
61
|
readonly preamble?: { readonly declaredNames: readonly string[] } | null
|
|
60
62
|
}
|
|
61
63
|
|
|
64
|
+
interface LoopValueType {
|
|
65
|
+
readonly primitive?: string
|
|
66
|
+
readonly unionTypes?: readonly LoopValueType[]
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
function isStringLoopValue(type: LoopValueType): boolean {
|
|
70
|
+
return type.primitive === 'string' ||
|
|
71
|
+
(type.unionTypes !== undefined && type.unionTypes.length > 0 && type.unionTypes.every(isStringLoopValue))
|
|
72
|
+
}
|
|
73
|
+
|
|
62
74
|
/**
|
|
63
75
|
* A stack of `ScopeFrame`s, innermost frame at index 0 of the internal
|
|
64
76
|
* array (i.e. `frames[0]` is what `enterLoopRow`/`enterCallback` most
|
|
@@ -112,7 +124,10 @@ export class BindingScope {
|
|
|
112
124
|
if (loop.paramBindings && loop.paramBindings.length > 0) {
|
|
113
125
|
for (const b of loop.paramBindings) bindings.set(b.name, { source: 'destructure' })
|
|
114
126
|
} else {
|
|
115
|
-
bindings.set(loop.param, {
|
|
127
|
+
bindings.set(loop.param, {
|
|
128
|
+
source: 'item',
|
|
129
|
+
...(loop.itemType ? { stringValue: isStringLoopValue(loop.itemType) } : {}),
|
|
130
|
+
})
|
|
116
131
|
}
|
|
117
132
|
if (loop.index != null) bindings.set(loop.index, { source: 'index' })
|
|
118
133
|
for (const name of loop.preamble?.declaredNames ?? []) bindings.set(name, { source: 'preamble' })
|
|
@@ -141,6 +156,21 @@ export class BindingScope {
|
|
|
141
156
|
return false
|
|
142
157
|
}
|
|
143
158
|
|
|
159
|
+
/** Resolve string typing at this position, respecting the nearest binding. */
|
|
160
|
+
isStringValueName(name: string, outer: (name: string) => boolean, componentProperty = false): boolean {
|
|
161
|
+
if (componentProperty) return outer(name)
|
|
162
|
+
const hit = this.lookup(name)
|
|
163
|
+
return hit ? hit.binding.stringValue === true : outer(name)
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
/** Names this frame shadows in an enclosing callback/row frame. */
|
|
167
|
+
shadowedNames(): readonly string[] {
|
|
168
|
+
if (!this.frames[0]) return []
|
|
169
|
+
return [...this.frames[0].bindings.keys()].filter(name =>
|
|
170
|
+
this.frames.slice(1).some(frame => frame.bindings.has(name)),
|
|
171
|
+
)
|
|
172
|
+
}
|
|
173
|
+
|
|
144
174
|
/**
|
|
145
175
|
* Resolves `name` against the frame stack innermost-first. `depth 0`
|
|
146
176
|
* means the innermost (most recently entered) frame; `null` when `name`
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import { collectLoopBoundNames } from '../adapters/loop-bound-names.ts'
|
|
2
|
+
import type { ComponentIR } from '../types.ts'
|
|
3
|
+
import type { BindingScope } from './binding-scope.ts'
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Root-prop aliases for template engines that flatten `props.X` to the bare
|
|
7
|
+
* template variable `X` (#3314). A loop that binds the same name (`.map(value
|
|
8
|
+
* => …)`) shadows that variable for its whole body, so an explicit
|
|
9
|
+
* `props.value` inside the row read the ROW's `value` instead of the prop.
|
|
10
|
+
*
|
|
11
|
+
* The adapter assigns each shadowed prop to an alias just before the loop
|
|
12
|
+
* header — where the bare name still means the prop — and an explicit
|
|
13
|
+
* `props.X` read inside the row uses the alias. Only the template syntax
|
|
14
|
+
* of the assignment is per-adapter; which names need one, and their alias
|
|
15
|
+
* name, are decided here once.
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* The template variable each prop in `propNames` is aliased to: `__bf_root_<name>`,
|
|
20
|
+
* extended with `_` until it names nothing else the component turns into a
|
|
21
|
+
* template variable — a prop, a local const or function, a signal or memo,
|
|
22
|
+
* any loop binding, or another prop's alias. The `__bf_` namespace is
|
|
23
|
+
* reserved only by convention, so an alias must not overwrite a user prop
|
|
24
|
+
* spelled `__bf_root_value` nor be shadowed by a binding of that name.
|
|
25
|
+
* Computed once per component so the assignment before a loop and every
|
|
26
|
+
* `props.X` read inside it agree on the name.
|
|
27
|
+
*/
|
|
28
|
+
export function rootPropAliasNames(ir: ComponentIR, propNames: Iterable<string>): ReadonlyMap<string, string> {
|
|
29
|
+
const { metadata } = ir
|
|
30
|
+
const props = [...propNames]
|
|
31
|
+
const taken = new Set<string>([
|
|
32
|
+
...props,
|
|
33
|
+
...metadata.localConstants.map(c => c.name),
|
|
34
|
+
...metadata.localFunctions.map(f => f.name),
|
|
35
|
+
...metadata.signals.flatMap(s => (s.setter ? [s.getter, s.setter] : [s.getter])),
|
|
36
|
+
...metadata.memos.map(m => m.name),
|
|
37
|
+
...collectLoopBoundNames(ir),
|
|
38
|
+
])
|
|
39
|
+
const aliases = new Map<string, string>()
|
|
40
|
+
for (const name of props) {
|
|
41
|
+
let alias = `__bf_root_${name}`
|
|
42
|
+
while (taken.has(alias)) alias += '_'
|
|
43
|
+
taken.add(alias)
|
|
44
|
+
aliases.set(name, alias)
|
|
45
|
+
}
|
|
46
|
+
return aliases
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Props the loop row newly shadows, with their aliases: names `row` binds
|
|
51
|
+
* that `outer` did not (an enclosing loop that already bound a name already
|
|
52
|
+
* aliased it, and a re-assignment here would capture that loop's row value)
|
|
53
|
+
* and that are component props (keys of `aliases`). In source order of
|
|
54
|
+
* `row`'s bindings.
|
|
55
|
+
*/
|
|
56
|
+
export function rootPropAliasesForLoop(
|
|
57
|
+
outer: BindingScope,
|
|
58
|
+
row: BindingScope,
|
|
59
|
+
aliases: ReadonlyMap<string, string>,
|
|
60
|
+
): { name: string; alias: string }[] {
|
|
61
|
+
const shadowed: { name: string; alias: string }[] = []
|
|
62
|
+
for (const name of row.boundNames()) {
|
|
63
|
+
const alias = aliases.get(name)
|
|
64
|
+
if (alias !== undefined && !outer.isBound(name)) shadowed.push({ name, alias })
|
|
65
|
+
}
|
|
66
|
+
return shadowed
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* The template variable an explicit `props.<name>` read resolves to at a
|
|
71
|
+
* position with `scope`: the alias (from `rootPropAliasNames`) while a loop
|
|
72
|
+
* binding shadows `name`, else `name` itself.
|
|
73
|
+
*/
|
|
74
|
+
export function rootPropReadName(
|
|
75
|
+
name: string,
|
|
76
|
+
scope: BindingScope,
|
|
77
|
+
aliases: ReadonlyMap<string, string>,
|
|
78
|
+
): string {
|
|
79
|
+
if (!scope.isBound(name)) return name
|
|
80
|
+
return aliases.get(name) ?? name
|
|
81
|
+
}
|
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
import ts from 'typescript'
|
|
2
|
+
import path from 'node:path'
|
|
3
|
+
import type { AnalyzerContext } from './analyzer-context.ts'
|
|
4
|
+
import type { ParsedExpr } from './expression-parser.ts'
|
|
5
|
+
|
|
6
|
+
// TypeScript exposes these resolution-cache readers on Program at runtime
|
|
7
|
+
// (its own incremental rebuild uses them), but omits them from the public
|
|
8
|
+
// declaration. Replaying the resolved graph preserves custom host hooks:
|
|
9
|
+
// renaming a local binding never changes any import or type directive.
|
|
10
|
+
type ResolvedProgram = ts.Program & {
|
|
11
|
+
getResolvedModule(file: ts.SourceFile, name: string, mode: ts.ResolutionMode): ts.ResolvedModuleWithFailedLookupLocations | undefined
|
|
12
|
+
getResolvedTypeReferenceDirective(file: ts.SourceFile, name: string, mode: ts.ResolutionMode): ts.ResolvedTypeReferenceDirectiveWithFailedLookupLocations | undefined
|
|
13
|
+
getAutomaticTypeDirectiveResolutions(): { get(name: string, mode: ts.ResolutionMode): ts.ResolvedTypeReferenceDirectiveWithFailedLookupLocations | undefined }
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/** Preserve a build-supplied Program's virtual files and resolution options
|
|
17
|
+
* when one root's source changes. Other SourceFiles remain untouched. */
|
|
18
|
+
export function programWithSeparatedSource(program: ts.Program, filePath: string, source: string): ts.Program {
|
|
19
|
+
const options = program.getCompilerOptions()
|
|
20
|
+
const host = ts.createCompilerHost(options)
|
|
21
|
+
const directory = program.getCurrentDirectory()
|
|
22
|
+
const target = path.resolve(directory, filePath)
|
|
23
|
+
const matches = (name: string) => path.resolve(directory, name) === target
|
|
24
|
+
const resolvedProgram = program as ResolvedProgram
|
|
25
|
+
const virtualDirectories = new Set<string>()
|
|
26
|
+
for (const file of program.getSourceFiles()) {
|
|
27
|
+
let dir = path.dirname(path.resolve(directory, file.fileName))
|
|
28
|
+
while (!virtualDirectories.has(dir)) {
|
|
29
|
+
virtualDirectories.add(dir)
|
|
30
|
+
const parent = path.dirname(dir)
|
|
31
|
+
if (parent === dir) break
|
|
32
|
+
dir = parent
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
return ts.createProgram({ rootNames: program.getRootFileNames(), options, projectReferences: program.getProjectReferences(), host: {
|
|
36
|
+
...host,
|
|
37
|
+
getCurrentDirectory: () => directory,
|
|
38
|
+
resolveModuleNameLiterals: (literals, containingFile, redirected, compilerOptions, containingSourceFile) => {
|
|
39
|
+
const original = program.getSourceFile(containingFile)
|
|
40
|
+
return literals.map(literal => {
|
|
41
|
+
const mode = program.getModeForUsageLocation(containingSourceFile, literal)
|
|
42
|
+
return (original && resolvedProgram.getResolvedModule(original, literal.text, mode))
|
|
43
|
+
?? ts.resolveModuleName(literal.text, containingFile, compilerOptions, host, undefined, redirected, mode)
|
|
44
|
+
})
|
|
45
|
+
},
|
|
46
|
+
resolveTypeReferenceDirectiveReferences: (references, containingFile, redirected, compilerOptions, containingSourceFile) => {
|
|
47
|
+
const original = program.getSourceFile(containingFile)
|
|
48
|
+
return references.map(reference => {
|
|
49
|
+
const name = typeof reference === 'string' ? reference : reference.fileName
|
|
50
|
+
const mode = ts.getModeForFileReference(reference, containingSourceFile?.impliedNodeFormat)
|
|
51
|
+
return (original ? resolvedProgram.getResolvedTypeReferenceDirective(original, name, mode)
|
|
52
|
+
: resolvedProgram.getAutomaticTypeDirectiveResolutions().get(name, mode))
|
|
53
|
+
?? ts.resolveTypeReferenceDirective(name, containingFile, compilerOptions, host, redirected, undefined, mode)
|
|
54
|
+
})
|
|
55
|
+
},
|
|
56
|
+
directoryExists: name => virtualDirectories.has(path.resolve(directory, name)) || host.directoryExists!(name),
|
|
57
|
+
fileExists: name => matches(name) || program.getSourceFile(name) !== undefined || host.fileExists(name),
|
|
58
|
+
readFile: name => matches(name) ? source : program.getSourceFile(name)?.text ?? host.readFile(name),
|
|
59
|
+
getSourceFile: (name, version, onError, shouldCreateNewSourceFile) => {
|
|
60
|
+
if (!matches(name)) return program.getSourceFile(name) ?? host.getSourceFile(name, version, onError, shouldCreateNewSourceFile)
|
|
61
|
+
const file = ts.createSourceFile(name, source, version, true, ts.ScriptKind.TSX)
|
|
62
|
+
file.impliedNodeFormat = program.getSourceFile(name)?.impliedNodeFormat
|
|
63
|
+
return file
|
|
64
|
+
},
|
|
65
|
+
} })
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
function isLiteralTree(expr: ParsedExpr): boolean {
|
|
69
|
+
switch (expr.kind) {
|
|
70
|
+
case 'literal': return true
|
|
71
|
+
case 'array-literal': return expr.elements.every(isLiteralTree)
|
|
72
|
+
case 'object-literal': return expr.properties.every(p => p.kind === 'prop' && isLiteralTree(p.value))
|
|
73
|
+
case 'unary': return ['-', '+'].includes(expr.op) && expr.argument.kind === 'literal' && expr.argument.literalType === 'number'
|
|
74
|
+
default: return false
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* A bare prop and a literal-seeded getter are different JS bindings, but
|
|
80
|
+
* scalar template backends flatten both to one variable. Give the local
|
|
81
|
+
* getter a hygienic name before building IR, so every backend (including
|
|
82
|
+
* the client emitter) receives one consistent binding identity.
|
|
83
|
+
*
|
|
84
|
+
* Source edits are AST/symbol-based and preserve untouched source bytes.
|
|
85
|
+
* Property keys, strings, and shadowing callback bindings never rename;
|
|
86
|
+
* shorthand values expand without changing their public keys. The rare
|
|
87
|
+
* collision-only path reuses the analyzer's checker when available. Its
|
|
88
|
+
* fallback binds the already-parsed SourceFile without parsing or loading
|
|
89
|
+
* dependencies: only lexical symbol identity is needed, not types.
|
|
90
|
+
*/
|
|
91
|
+
export function separateLiteralSignalPropBindings(
|
|
92
|
+
ctx: AnalyzerContext,
|
|
93
|
+
isNonReference: (id: ts.Identifier) => boolean,
|
|
94
|
+
): string | null {
|
|
95
|
+
if (!ctx.propsObjectName || !ctx.componentNode) return null
|
|
96
|
+
const propNames = new Set(ctx.propsParams.map(p => p.name))
|
|
97
|
+
const candidates = ctx.signals.filter(s =>
|
|
98
|
+
!s.isModule && !s.getterElided && propNames.has(s.getter) && s.parsed && isLiteralTree(s.parsed),
|
|
99
|
+
)
|
|
100
|
+
if (candidates.length === 0) return null
|
|
101
|
+
|
|
102
|
+
const sf = ctx.sourceFile
|
|
103
|
+
const checker = ctx.checker ?? ts.createProgram([sf.fileName], { noLib: true, noResolve: true }, {
|
|
104
|
+
...ts.createCompilerHost({ noLib: true, noResolve: true }),
|
|
105
|
+
getSourceFile: () => sf,
|
|
106
|
+
}).getTypeChecker()
|
|
107
|
+
const occupied = new Set<string>()
|
|
108
|
+
function collectNames(node: ts.Node): void {
|
|
109
|
+
// Also avoid case-fold collisions introduced by backend field naming.
|
|
110
|
+
if (ts.isIdentifier(node)) occupied.add(node.text.toLowerCase())
|
|
111
|
+
ts.forEachChild(node, collectNames)
|
|
112
|
+
}
|
|
113
|
+
collectNames(sf)
|
|
114
|
+
|
|
115
|
+
const renames = new Map<ts.Symbol, string>()
|
|
116
|
+
const declarations = new Map(candidates.map(s => [
|
|
117
|
+
sf.getPositionOfLineAndCharacter(s.loc.start.line - 1, s.loc.start.column), s,
|
|
118
|
+
]))
|
|
119
|
+
function collectBindings(node: ts.Node): void {
|
|
120
|
+
if (ts.isVariableDeclaration(node)) {
|
|
121
|
+
const signal = declarations.get(node.getStart(sf))
|
|
122
|
+
if (signal) {
|
|
123
|
+
const names = ts.isIdentifier(node.name) ? [node.name]
|
|
124
|
+
: ts.isArrayBindingPattern(node.name)
|
|
125
|
+
? node.name.elements.flatMap(e => ts.isBindingElement(e) && ts.isIdentifier(e.name) ? [e.name] : [])
|
|
126
|
+
: []
|
|
127
|
+
const id = names.find(n => n.text === signal.getter)
|
|
128
|
+
const symbol = id && checker.getSymbolAtLocation(id)
|
|
129
|
+
if (symbol) {
|
|
130
|
+
const base = `bfSignal_${signal.getter}`
|
|
131
|
+
let name = base
|
|
132
|
+
let suffix = 0
|
|
133
|
+
while (occupied.has(name.toLowerCase())) name = `${base}_${++suffix}`
|
|
134
|
+
occupied.add(name.toLowerCase())
|
|
135
|
+
renames.set(symbol, name)
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
ts.forEachChild(node, collectBindings)
|
|
140
|
+
}
|
|
141
|
+
collectBindings(ctx.componentNode)
|
|
142
|
+
|
|
143
|
+
const edits: { start: number; end: number; text: string }[] = []
|
|
144
|
+
function collectEdits(node: ts.Node): void {
|
|
145
|
+
if (ts.isIdentifier(node) && !isNonReference(node)) {
|
|
146
|
+
const parent = node.parent
|
|
147
|
+
const shorthand = ts.isShorthandPropertyAssignment(parent) && parent.name === node
|
|
148
|
+
const symbol = shorthand ? checker.getShorthandAssignmentValueSymbol(parent) : checker.getSymbolAtLocation(node)
|
|
149
|
+
const name = symbol && renames.get(symbol)
|
|
150
|
+
if (name) edits.push({
|
|
151
|
+
start: node.getStart(sf), end: node.getEnd(),
|
|
152
|
+
text: shorthand ? `${node.text}: ${name}` : name,
|
|
153
|
+
})
|
|
154
|
+
}
|
|
155
|
+
ts.forEachChild(node, collectEdits)
|
|
156
|
+
}
|
|
157
|
+
collectEdits(ctx.componentNode)
|
|
158
|
+
if (edits.length === 0) return null
|
|
159
|
+
let source = sf.text
|
|
160
|
+
for (const edit of edits.sort((a, b) => b.start - a.start)) {
|
|
161
|
+
source = source.slice(0, edit.start) + edit.text + source.slice(edit.end)
|
|
162
|
+
}
|
|
163
|
+
return source
|
|
164
|
+
}
|
package/src/ssr-defaults.ts
CHANGED
|
@@ -277,11 +277,9 @@ export function extractSsrDefaults(metadata: IRMetadata): Record<string, SsrDefa
|
|
|
277
277
|
//
|
|
278
278
|
// NOT self-derived (the initializer references a DIFFERENT prop, or no
|
|
279
279
|
// prop at all) keeps today's behavior: the signal's evaluated value
|
|
280
|
-
// wins
|
|
281
|
-
//
|
|
282
|
-
//
|
|
283
|
-
// both `label()` and `props.label` — that's a template-variable-
|
|
284
|
-
// aliasing defect, not this one, and must render byte-identically.
|
|
280
|
+
// wins its own entry. Literal-seeded getters that originally shared
|
|
281
|
+
// a bare prop's name have already been separated during analysis;
|
|
282
|
+
// their internal signal entry cannot overwrite the raw prop entry.
|
|
285
283
|
//
|
|
286
284
|
// Invariant this establishes (relied on by every template-stash
|
|
287
285
|
// conformance harness's seeding loops — see `test-render.ts`): a
|
package/src/ssr-seed-plan.ts
CHANGED
|
@@ -10,7 +10,8 @@
|
|
|
10
10
|
*
|
|
11
11
|
* Ordering / acyclicity guarantee: `steps` lists the component's signals
|
|
12
12
|
* first, then its memos, each group in declaration order (matching
|
|
13
|
-
* `IRMetadata.signals` / `IRMetadata.memos`)
|
|
13
|
+
* `IRMetadata.signals` / `IRMetadata.memos`), then canonical getter aliases.
|
|
14
|
+
* A binding's name only enters
|
|
14
15
|
* the scope set AFTER its own step is decided, so a `derived` step's `frees`
|
|
15
16
|
* can only name `baseScope` entries or EARLIER steps — self- and
|
|
16
17
|
* forward-references are rejected by construction, and a consumer emitting
|
|
@@ -31,6 +32,7 @@
|
|
|
31
32
|
*/
|
|
32
33
|
|
|
33
34
|
import { collectModuleStringConsts } from './augment-inherited-props.ts'
|
|
35
|
+
import { collectAliasableGetterNames, resolveGetterAliases } from './ir-to-client-js/csr-substitute.ts'
|
|
34
36
|
import { envSignalReaderFor, type EnvSignalReader } from './adapters/env-signal.ts'
|
|
35
37
|
import {
|
|
36
38
|
extractArrowBodyExpression,
|
|
@@ -44,7 +46,7 @@ import type { IRMetadata } from './types.ts'
|
|
|
44
46
|
import { isElidedFactoryGetter } from './types.ts'
|
|
45
47
|
|
|
46
48
|
/**
|
|
47
|
-
* One binding in
|
|
49
|
+
* One binding in dependency order (signals, memos, then getter aliases —
|
|
48
50
|
* matching `IRMetadata` order and the adapters' iteration).
|
|
49
51
|
*
|
|
50
52
|
* - `env-reader`: an env signal whose `envReader` key resolves in the shared
|
|
@@ -250,5 +252,15 @@ export function computeSsrSeedPlan(metadata: IRMetadata): SsrSeedPlan {
|
|
|
250
252
|
available.add(memo.name)
|
|
251
253
|
}
|
|
252
254
|
|
|
255
|
+
// A getter alias names the live binding, not its pre-recompute default.
|
|
256
|
+
// Seed aliases after every signal/memo so stash adapters do not freeze an
|
|
257
|
+
// unresolved prop-derived memo before its runtime value is available.
|
|
258
|
+
const getters = collectAliasableGetterNames(metadata.signals, metadata.memos)
|
|
259
|
+
for (const [alias, origin] of resolveGetterAliases(metadata.localConstants ?? [], name => getters.has(name))) {
|
|
260
|
+
if (metadata.propsParams.some(p => p.name === alias)) continue
|
|
261
|
+
const parsed: ParsedExpr = { kind: 'call', callee: { kind: 'identifier', name: origin }, args: [] }
|
|
262
|
+
steps.push(classify(alias, 'memo', `${origin}()`, parsed, available))
|
|
263
|
+
available.add(alias)
|
|
264
|
+
}
|
|
253
265
|
return { baseScope, steps }
|
|
254
266
|
}
|