@barefootjs/jsx 0.39.1 → 0.39.2
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/parsed-expr-emitter.d.ts +2 -2
- package/dist/adapters/parsed-expr-emitter.d.ts.map +1 -1
- package/dist/analyzer.d.ts.map +1 -1
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1267 -1078
- 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/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/scope/binding-scope.d.ts +11 -0
- package/dist/scope/binding-scope.d.ts.map +1 -1
- 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__/binding-scope.test.ts +27 -0
- package/src/__tests__/client-template-escape-soundness.test.ts +17 -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__/literal-member-index.test.ts +14 -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/parsed-expr-emitter.ts +3 -3
- package/src/analyzer.ts +10 -0
- package/src/index.ts +1 -0
- package/src/ir-to-client-js/collect-elements.ts +8 -9
- package/src/ir-to-client-js/safe-html.ts +19 -6
- package/src/jsx-to-ir.ts +14 -2
- package/src/literal-member-index.ts +12 -0
- package/src/scope/binding-scope.ts +31 -1
- package/src/signal-prop-name-collision.ts +164 -0
- package/src/ssr-defaults.ts +3 -5
- package/src/ssr-seed-plan.ts +14 -2
|
@@ -469,14 +469,15 @@ export function collectInnerLoops(
|
|
|
469
469
|
*
|
|
470
470
|
* Used by both the top-level `case 'loop'` in `collectElements` and the
|
|
471
471
|
* branch-loop collector in `collectBranchLoops`. Each call site applies
|
|
472
|
-
* its own final-emission rule over `innerLoops
|
|
473
|
-
*
|
|
474
|
-
*
|
|
472
|
+
* its own final-emission rule over `innerLoops`. Static top-level loops
|
|
473
|
+
* hydrate existing rows; branch-scoped loops materialize rows via mapArray
|
|
474
|
+
* and therefore use composite rendering for static nested loops too.
|
|
475
475
|
*/
|
|
476
476
|
function decideLoopRendering(
|
|
477
477
|
loop: IRLoop,
|
|
478
478
|
siblingOffsets: Map<IRLoop, IRNode[]>,
|
|
479
479
|
ctx: ClientJsContext | undefined,
|
|
480
|
+
branchScope = false,
|
|
480
481
|
): { useElementReconciliation: boolean; innerLoops: NestedLoop[] | undefined } {
|
|
481
482
|
const hasNestedComps = (loop.nestedComponents?.length ?? 0) > 0
|
|
482
483
|
// Collect inner loops even when the outer item is a child component
|
|
@@ -495,7 +496,7 @@ function decideLoopRendering(
|
|
|
495
496
|
)
|
|
496
497
|
const hasInnerLoops = (innerLoops?.length ?? 0) > 0
|
|
497
498
|
const useElementReconciliation =
|
|
498
|
-
!loop.childComponent && !loop.isStaticArray && (hasNestedComps || hasInnerLoops)
|
|
499
|
+
!loop.childComponent && (!loop.isStaticArray || (branchScope && hasInnerLoops)) && (hasNestedComps || hasInnerLoops)
|
|
499
500
|
return { useElementReconciliation, innerLoops }
|
|
500
501
|
}
|
|
501
502
|
|
|
@@ -1342,10 +1343,8 @@ function collectBranchLoops(
|
|
|
1342
1343
|
// the top-level `useElementReconciliation` rule so a `.map()` directly
|
|
1343
1344
|
// inside an outer `.map()` gets its own reactive mapArray even when
|
|
1344
1345
|
// the outer loop lives inside a conditional branch.
|
|
1345
|
-
//
|
|
1346
|
-
//
|
|
1347
|
-
// is handled at the enclosing branch-loop level below (`if (ctx)` block
|
|
1348
|
-
// around `collectLoopChildReactiveTexts`), not per inner loop.
|
|
1346
|
+
// Branch rows are materialized by mapArray even for static arrays.
|
|
1347
|
+
// Collect each inner row's bindings and reconcile nested rows there too.
|
|
1349
1348
|
// flatMap PROJECTION loop — see the top-level handler's note: children
|
|
1350
1349
|
// are SSR-only; the client rides the descriptor mapArray path.
|
|
1351
1350
|
const projectionInner =
|
|
@@ -1355,7 +1354,7 @@ function collectBranchLoops(
|
|
|
1355
1354
|
|
|
1356
1355
|
const { useElementReconciliation, innerLoops: innerLoopsCollected } = projectionInner
|
|
1357
1356
|
? { useElementReconciliation: false, innerLoops: undefined }
|
|
1358
|
-
: decideLoopRendering(n, siblingOffsets,
|
|
1357
|
+
: decideLoopRendering(n, siblingOffsets, ctx, true)
|
|
1359
1358
|
|
|
1360
1359
|
// Build the item template from loop children.
|
|
1361
1360
|
// Use loopDepth=0: this loop gets its own mapArray/mapArrayAnchored call (independent
|
|
@@ -37,6 +37,7 @@
|
|
|
37
37
|
*/
|
|
38
38
|
|
|
39
39
|
import type { IRExpression } from '../types.ts'
|
|
40
|
+
import { parseExpression, type ParsedExpr } from '../expression-parser.ts'
|
|
40
41
|
|
|
41
42
|
/**
|
|
42
43
|
* A JS expression string that evaluates, in the client template, to HTML
|
|
@@ -170,20 +171,33 @@ export const EMPTY_MARKUP: SafeHtml = safeHtml("''")
|
|
|
170
171
|
|
|
171
172
|
/**
|
|
172
173
|
* Recognizes a JSX child-position expression that is exactly a reference to
|
|
173
|
-
* the reserved `children` prop
|
|
174
|
+
* the reserved `children` prop, optionally with `?? ''`: bare `children` (destructured) or
|
|
174
175
|
* `<receiver>.children` for any single-identifier receiver (`props.children`,
|
|
175
176
|
* a custom props-param name, a loop-scoped alias closing over props, ...).
|
|
176
177
|
*
|
|
177
178
|
* Deliberately LOOSER than `isTransparentFragment` (`jsx-to-ir.ts`), which
|
|
178
179
|
* runs on the TS AST with the analyzer-resolved props name in hand. This
|
|
179
|
-
* layer
|
|
180
|
+
* layer parses IR expressions without analyzer context, so any single-identifier receiver
|
|
180
181
|
* is the available approximation. The looseness costs nothing measurable: an
|
|
181
182
|
* unrelated `.children` member (a tree node's own `children` array, say) is a
|
|
182
183
|
* reactive member expression that gets a `slotId` and takes the slotted arm
|
|
183
184
|
* of `spliceChildValue` before this predicate is consulted.
|
|
184
185
|
*/
|
|
185
186
|
function isChildrenPassthroughExpr(expr: string): boolean {
|
|
186
|
-
return
|
|
187
|
+
return isChildrenPassthrough(parseExpression(expr))
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
function isChildrenPassthrough(expr: ParsedExpr): boolean {
|
|
191
|
+
if (expr.kind === 'identifier') return expr.name === 'children'
|
|
192
|
+
if (expr.kind === 'member') {
|
|
193
|
+
return expr.object.kind === 'identifier' && expr.property === 'children'
|
|
194
|
+
&& !expr.computed && !expr.optional
|
|
195
|
+
}
|
|
196
|
+
// An empty fallback adds no untrusted text to the compiler-built markup.
|
|
197
|
+
// Other fallbacks still take the escape-by-default path.
|
|
198
|
+
return expr.kind === 'logical' && expr.op === '??'
|
|
199
|
+
&& expr.right.kind === 'literal' && expr.right.value === ''
|
|
200
|
+
&& isChildrenPassthrough(expr.left)
|
|
187
201
|
}
|
|
188
202
|
|
|
189
203
|
export interface ChildSpliceContext {
|
|
@@ -220,7 +234,7 @@ export interface ChildSpliceContext {
|
|
|
220
234
|
* 6. otherwise → `escapedText`
|
|
221
235
|
*
|
|
222
236
|
* Arm 5 tests BOTH `node.expr` (the original source text, stable across
|
|
223
|
-
* builders) and the caller's resolved form (
|
|
237
|
+
* builders) and the caller's resolved form (with parentheses handled structurally): a
|
|
224
238
|
* destructured-and-renamed children (`const { children: kids } = props`)
|
|
225
239
|
* reads `kids` in the source and `(_p.children)` after substitution, and
|
|
226
240
|
* either alone misses one of the two shapes (#2786).
|
|
@@ -235,8 +249,7 @@ export function spliceChildValue(
|
|
|
235
249
|
if (node.slotId) {
|
|
236
250
|
return cx.markupSlotIds?.has(node.slotId) ? escapedTextOrMarkup(valueExpr) : escapedText(valueExpr)
|
|
237
251
|
}
|
|
238
|
-
|
|
239
|
-
if (isChildrenPassthroughExpr(node.expr) || isChildrenPassthroughExpr(resolved)) {
|
|
252
|
+
if (isChildrenPassthroughExpr(node.expr) || isChildrenPassthroughExpr(valueExpr)) {
|
|
240
253
|
return childrenMarkup(valueExpr)
|
|
241
254
|
}
|
|
242
255
|
return escapedText(valueExpr)
|
package/src/jsx-to-ir.ts
CHANGED
|
@@ -40,7 +40,7 @@ import {
|
|
|
40
40
|
isReactiveOrigin,
|
|
41
41
|
AttrValueOf,
|
|
42
42
|
} from './types.ts'
|
|
43
|
-
import { type AnalyzerContext, type MultiReturnJsxInfo, getSourceLocation, collectReactiveGetterNames } from './analyzer-context.ts'
|
|
43
|
+
import { type AnalyzerContext, type MultiReturnJsxInfo, getSourceLocation, collectReactiveGetterNames, tsTypeToTypeInfo } from './analyzer-context.ts'
|
|
44
44
|
import { parseExpression, isSupported, parseBlockBody, foldBlockToExpr, predicateTernaryToLogical, tsNodeToParsedExpr, sortComparatorFromArrow, stringifyParsedExpr, cssKebabCase, CALLBACK_METHODS, type ParsedExpr } from './expression-parser.ts'
|
|
45
45
|
import type { IRLoopSort, FunctionInfo, ConstantInfo, ParamInfo } from './types.ts'
|
|
46
46
|
import { formatParamWithType } from './module-exports.ts'
|
|
@@ -4831,11 +4831,13 @@ function transformMapCall(
|
|
|
4831
4831
|
let children: IRNode[] = []
|
|
4832
4832
|
let paramBindings: LoopParamBinding[] | undefined
|
|
4833
4833
|
let flatMapCallback: FlatMapCallback | undefined
|
|
4834
|
+
let itemTypeNode: ts.Node | undefined
|
|
4834
4835
|
|
|
4835
4836
|
if (ts.isArrowFunction(callback)) {
|
|
4836
4837
|
// Extract parameter names and type annotations
|
|
4837
4838
|
if (callback.parameters.length > 0) {
|
|
4838
4839
|
const firstParam = callback.parameters[0]
|
|
4840
|
+
itemTypeNode = firstParam
|
|
4839
4841
|
param = firstParam.name.getText(ctx.sourceFile)
|
|
4840
4842
|
if (firstParam.type) {
|
|
4841
4843
|
paramType = firstParam.type.getText(ctx.sourceFile)
|
|
@@ -4864,6 +4866,7 @@ function transformMapCall(
|
|
|
4864
4866
|
ts.isBindingElement(elements[1]) && ts.isIdentifier(elements[1].name)) {
|
|
4865
4867
|
index = elements[0].name.text
|
|
4866
4868
|
param = elements[1].name.text
|
|
4869
|
+
itemTypeNode = elements[1].name
|
|
4867
4870
|
// Don't populate paramBindings — the destructure is fully
|
|
4868
4871
|
// resolved into index + param by the iteration shape.
|
|
4869
4872
|
} else {
|
|
@@ -5587,13 +5590,22 @@ function transformMapCall(
|
|
|
5587
5590
|
// createComponent replacement).
|
|
5588
5591
|
const nestedComponents = collectNestedComponents(children).filter(c => c.name !== childComponent?.name)
|
|
5589
5592
|
|
|
5593
|
+
// Scalar operand evidence only. Inferring object rows here would bypass
|
|
5594
|
+
// adapters' existing row-structure capability checks and their diagnostics.
|
|
5595
|
+
const callbackType = itemTypeNode && ctx.analyzer.checker
|
|
5596
|
+
? tsTypeToTypeInfo(ctx.analyzer.checker.getTypeAtLocation(itemTypeNode), ctx.analyzer.checker)
|
|
5597
|
+
: null
|
|
5598
|
+
const scalarItemType = callbackType?.kind === 'primitive' ||
|
|
5599
|
+
(callbackType?.kind === 'union' && callbackType.unionTypes?.every(t => t.kind === 'primitive'))
|
|
5600
|
+
? callbackType : null
|
|
5601
|
+
|
|
5590
5602
|
return {
|
|
5591
5603
|
type: 'loop',
|
|
5592
5604
|
method: method === 'flatMap' ? 'flatMap' : undefined,
|
|
5593
5605
|
array,
|
|
5594
5606
|
templateArray,
|
|
5595
5607
|
arrayType: null,
|
|
5596
|
-
itemType:
|
|
5608
|
+
itemType: scalarItemType,
|
|
5597
5609
|
param,
|
|
5598
5610
|
index,
|
|
5599
5611
|
key,
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { ParsedExpr } from './expression-parser.ts'
|
|
2
|
+
|
|
3
|
+
/** Recover an index operand from a computed member's property key.
|
|
4
|
+
* Only canonical array indices become numbers: keys such as "01" must
|
|
5
|
+
* remain strings. Both `obj[0]` and `obj['0']` use the same JS key. */
|
|
6
|
+
export function literalMemberIndex(property: string): ParsedExpr {
|
|
7
|
+
const index = Number(property)
|
|
8
|
+
if (Number.isInteger(index) && index >= 0 && index < 0xffffffff && String(index) === property) {
|
|
9
|
+
return { kind: 'literal', value: index, literalType: 'number' }
|
|
10
|
+
}
|
|
11
|
+
return { kind: 'literal', value: property, literalType: 'string' }
|
|
12
|
+
}
|
|
@@ -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,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
|
}
|