@barefootjs/jsx 0.41.1 → 0.41.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/attr-plan.d.ts +8 -0
- package/dist/adapters/attr-plan.d.ts.map +1 -1
- package/dist/adapters/component-props.d.ts.map +1 -1
- package/dist/adapters/computed-const.d.ts +19 -0
- package/dist/adapters/computed-const.d.ts.map +1 -1
- package/dist/adapters/dsl-template-adapter.d.ts +2 -1
- package/dist/adapters/dsl-template-adapter.d.ts.map +1 -1
- package/dist/adapters/inherited-const-server-read.d.ts +37 -0
- package/dist/adapters/inherited-const-server-read.d.ts.map +1 -0
- package/dist/adapters/loop-row-type.d.ts +18 -0
- package/dist/adapters/loop-row-type.d.ts.map +1 -1
- package/dist/adapters/non-primitive-equality.d.ts +8 -2
- package/dist/adapters/non-primitive-equality.d.ts.map +1 -1
- package/dist/adapters/unapplied-prop-default.d.ts +34 -0
- package/dist/adapters/unapplied-prop-default.d.ts.map +1 -0
- package/dist/adapters/uninlinable-seed-call.d.ts +29 -0
- package/dist/adapters/uninlinable-seed-call.d.ts.map +1 -0
- package/dist/analyzer.d.ts.map +1 -1
- package/dist/augment-inherited-props.d.ts +4 -22
- package/dist/augment-inherited-props.d.ts.map +1 -1
- package/dist/const-identity.d.ts +38 -0
- package/dist/const-identity.d.ts.map +1 -0
- package/dist/index.d.ts +9 -4
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2328 -1340
- package/dist/ir-to-client-js/child-props-object.d.ts +39 -41
- package/dist/ir-to-client-js/child-props-object.d.ts.map +1 -1
- package/dist/ir-to-client-js/collect-elements.d.ts.map +1 -1
- package/dist/ir-to-client-js/component-scope.d.ts +13 -2
- package/dist/ir-to-client-js/component-scope.d.ts.map +1 -1
- package/dist/ir-to-client-js/control-flow/plan/build-inner-loop.d.ts.map +1 -1
- package/dist/ir-to-client-js/control-flow/plan/build-loop-child-arm.d.ts.map +1 -1
- package/dist/ir-to-client-js/html-template.d.ts.map +1 -1
- package/dist/ir-to-client-js/prop-handling.d.ts.map +1 -1
- package/dist/jsx-to-ir.d.ts.map +1 -1
- package/dist/local-call-body.d.ts +112 -0
- package/dist/local-call-body.d.ts.map +1 -0
- package/dist/nullable-signal-attr.d.ts +10 -0
- package/dist/nullable-signal-attr.d.ts.map +1 -1
- package/dist/prop-default-resolution.d.ts +52 -0
- package/dist/prop-default-resolution.d.ts.map +1 -0
- package/dist/props-binding.d.ts +15 -0
- package/dist/props-binding.d.ts.map +1 -1
- package/dist/server-read-walk.d.ts +66 -0
- package/dist/server-read-walk.d.ts.map +1 -0
- package/dist/ssr-defaults.d.ts.map +1 -1
- package/dist/ssr-seed-plan.d.ts +24 -1
- package/dist/ssr-seed-plan.d.ts.map +1 -1
- package/dist/static-const-member.d.ts +92 -0
- package/dist/static-const-member.d.ts.map +1 -0
- package/dist/types.d.ts +45 -0
- package/dist/types.d.ts.map +1 -1
- package/package.json +2 -2
- package/src/__tests__/computed-const.test.ts +7 -1
- package/src/__tests__/const-identity-child-prop.test.ts +127 -0
- package/src/__tests__/inherited-const-server-read.test.ts +116 -0
- package/src/__tests__/issue-3688-props-spread-child-csr.test.ts +8 -3
- package/src/__tests__/issue-3714-open-rest-spread-child.test.ts +188 -0
- package/src/__tests__/local-call-body.test.ts +101 -0
- package/src/__tests__/prop-default-resolution.test.ts +185 -0
- package/src/__tests__/props-spread-shadowed.test.ts +113 -0
- package/src/__tests__/server-read-walk.test.ts +125 -0
- package/src/__tests__/ssr-defaults.test.ts +67 -7
- package/src/__tests__/static-record-literal-array.test.ts +82 -11
- package/src/adapters/attr-plan.ts +12 -1
- package/src/adapters/component-props.ts +2 -1
- package/src/adapters/computed-const.ts +108 -12
- package/src/adapters/dsl-template-adapter.ts +17 -4
- package/src/adapters/inherited-const-server-read.ts +174 -0
- package/src/adapters/loop-row-type.ts +69 -2
- package/src/adapters/non-primitive-equality.ts +17 -2
- package/src/adapters/unapplied-prop-default.ts +83 -0
- package/src/adapters/uninlinable-seed-call.ts +62 -0
- package/src/analyzer.ts +43 -3
- package/src/augment-inherited-props.ts +4 -49
- package/src/compiler.ts +4 -4
- package/src/const-identity.ts +189 -0
- package/src/index.ts +9 -4
- package/src/ir-to-client-js/child-props-object.ts +94 -62
- package/src/ir-to-client-js/collect-elements.ts +28 -22
- package/src/ir-to-client-js/component-scope.ts +16 -2
- package/src/ir-to-client-js/control-flow/plan/build-inner-loop.ts +1 -0
- package/src/ir-to-client-js/control-flow/plan/build-loop-child-arm.ts +1 -0
- package/src/ir-to-client-js/html-template.ts +24 -4
- package/src/ir-to-client-js/index.ts +2 -2
- package/src/ir-to-client-js/prop-handling.ts +4 -0
- package/src/jsx-to-ir.ts +23 -2
- package/src/local-call-body.ts +191 -0
- package/src/nullable-signal-attr.ts +39 -17
- package/src/prop-default-resolution.ts +195 -0
- package/src/props-binding.ts +16 -0
- package/src/server-read-walk.ts +174 -0
- package/src/ssr-defaults.ts +93 -10
- package/src/ssr-seed-plan.ts +104 -18
- package/src/static-const-member.ts +243 -0
- package/src/types.ts +45 -0
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* BF101 for a computed destructure default no template can apply (#3725).
|
|
3
|
+
*
|
|
4
|
+
* A backend applies a computed default when it renders, from the expression
|
|
5
|
+
* the default stands for once the consts and functions it reads are
|
|
6
|
+
* resolved (`appliedPropDefaults`). A default reading a component
|
|
7
|
+
* declaration with no such value (a signal, a memo, a `let`, a reassigned
|
|
8
|
+
* const, a function with parameters or statements, a const declared after
|
|
9
|
+
* the destructure) leaves nothing to apply, so an omitted prop would render
|
|
10
|
+
* empty where JS renders the default.
|
|
11
|
+
*
|
|
12
|
+
* Only a prop the server render reads is refused (`walkServerReads`): in a
|
|
13
|
+
* position, through a signal seed, a memo body or a const, or through
|
|
14
|
+
* another prop's default. A prop only an event handler reads (`{ onDone =
|
|
15
|
+
* handleDone }`) never renders its default on the server.
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
import { freeIdentifiers } from '../expression-parser.ts'
|
|
19
|
+
import { walkServerReads, type ServerReadWalkOptions } from '../server-read-walk.ts'
|
|
20
|
+
import { appliedPropDefaults, type PropDefaultApplication } from '../ssr-seed-plan.ts'
|
|
21
|
+
import type { CompilerError, ComponentIR } from '../types.ts'
|
|
22
|
+
|
|
23
|
+
/** Where {@link refuseUnappliedPropDefaults} records its errors. */
|
|
24
|
+
export interface UnappliedPropDefaultSink {
|
|
25
|
+
readonly errors: CompilerError[]
|
|
26
|
+
readonly componentName: string
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/** Record BF101 for each prop the server render reads whose computed default no template can apply. */
|
|
30
|
+
export function refuseUnappliedPropDefaults(ir: ComponentIR, sink: UnappliedPropDefaultSink): void {
|
|
31
|
+
const { defaults } = appliedPropDefaults(ir.metadata)
|
|
32
|
+
if (![...defaults.values()].some(d => d.blocked.length > 0)) return
|
|
33
|
+
const read = propsReadFrom(ir, defaults)
|
|
34
|
+
// A component renders on the server whatever directive its prop carries.
|
|
35
|
+
const readByChild = propsReadFrom(ir, defaults, position => position.role === 'prop')
|
|
36
|
+
for (const param of ir.metadata.propsParams) {
|
|
37
|
+
const blocked = read.has(param.name) ? defaults.get(param.name)!.blocked : []
|
|
38
|
+
if (blocked.length === 0) continue
|
|
39
|
+
const names = blocked.map(name => `'${name}'`).join(', ')
|
|
40
|
+
const deferrable = !readByChild.has(param.name)
|
|
41
|
+
sink.errors.push({
|
|
42
|
+
code: 'BF101',
|
|
43
|
+
severity: 'error',
|
|
44
|
+
message:
|
|
45
|
+
`The default of prop '${param.name}' reads ${names}, which no template can compute when it applies the default: ` +
|
|
46
|
+
`only a prop, or a 'const' or a function returning one expression that reads no signal, memo, 'let' or reassigned ` +
|
|
47
|
+
`value, is read there. The server render would show '${param.name}' empty where JS shows the default.`,
|
|
48
|
+
// A body destructure's own binding, or the component for a parameter default.
|
|
49
|
+
loc: defaults.get(param.name)!.loc ?? ir.root.loc,
|
|
50
|
+
suggestion: deferrable
|
|
51
|
+
? {
|
|
52
|
+
message: `Compute the default from props and consts only, or defer the reads of '${param.name}' to the client with /* @client */.`,
|
|
53
|
+
escape: [{ kind: 'client-directive' }],
|
|
54
|
+
}
|
|
55
|
+
: { message: `Compute the default from props and consts only.` },
|
|
56
|
+
})
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* The props with a computed default that the server render reads, starting
|
|
62
|
+
* from the positions `startsAt` keeps (every position when unset): in a
|
|
63
|
+
* position, through a declaration it reads, or through another such prop's
|
|
64
|
+
* default.
|
|
65
|
+
*/
|
|
66
|
+
export function propsReadFrom(
|
|
67
|
+
ir: ComponentIR,
|
|
68
|
+
defaults: PropDefaultApplication['defaults'],
|
|
69
|
+
startsAt?: ServerReadWalkOptions['startsAt'],
|
|
70
|
+
): ReadonlySet<string> {
|
|
71
|
+
const read = new Set<string>()
|
|
72
|
+
const pending: string[] = []
|
|
73
|
+
const visitProp = (name: string): void => {
|
|
74
|
+
if (read.has(name) || !defaults.has(name)) return
|
|
75
|
+
read.add(name)
|
|
76
|
+
pending.push(name)
|
|
77
|
+
}
|
|
78
|
+
walkServerReads(ir, () => {}, { visitProp, startsAt })
|
|
79
|
+
for (let name = pending.pop(); name !== undefined; name = pending.pop()) {
|
|
80
|
+
for (const free of freeIdentifiers(defaults.get(name)!.expr) ?? []) visitProp(free)
|
|
81
|
+
}
|
|
82
|
+
return read
|
|
83
|
+
}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* BF101 for a value the server render reads that calls a function a
|
|
3
|
+
* template cannot inline (#3721).
|
|
4
|
+
*
|
|
5
|
+
* A template computes a signal's or memo's SSR value from its seed or body
|
|
6
|
+
* (the SSR seed plan, the static SSR defaults, Go's constructor), and a
|
|
7
|
+
* const's from its initializer. A zero-argument call of a local function is
|
|
8
|
+
* read there as the expression the function returns (`localCall`'s
|
|
9
|
+
* `inline`); a call of any other local function (`refuse`: parameters, a
|
|
10
|
+
* body other than a single `return`) has no value a template can compute,
|
|
11
|
+
* so the value would render as its empty static default where JS renders
|
|
12
|
+
* the call's result. The same call written in markup is refused by the
|
|
13
|
+
* shared inlining (`inlineComputedConsts`); this is the refusal for the
|
|
14
|
+
* value positions that inlining never sees.
|
|
15
|
+
*
|
|
16
|
+
* Only a value the server render reads is refused, however it is reached
|
|
17
|
+
* (`walkServerReads`): through a signal, a memo, a const initializer or an
|
|
18
|
+
* inlined function, each name resolved lexically. A `/* @client *\/` read
|
|
19
|
+
* and a loop row named like a component value read none.
|
|
20
|
+
*/
|
|
21
|
+
|
|
22
|
+
import { walkServerReads, type ServerRead } from '../server-read-walk.ts'
|
|
23
|
+
import type { CompilerError, ComponentIR, SourceLocation } from '../types.ts'
|
|
24
|
+
|
|
25
|
+
/** Where {@link refuseUninlinableSeedCalls} records its errors. */
|
|
26
|
+
export interface UninlinableSeedCallSink {
|
|
27
|
+
readonly errors: CompilerError[]
|
|
28
|
+
readonly componentName: string
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/** Record BF101 for each declaration the server render reads that calls an uninlinable local function. */
|
|
32
|
+
export function refuseUninlinableSeedCalls(ir: ComponentIR, sink: UninlinableSeedCallSink): void {
|
|
33
|
+
walkServerReads(ir, (read) => {
|
|
34
|
+
for (const fn of read.refusedCalls) record(read.kind, read.name, fn, read.loc, sink)
|
|
35
|
+
})
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
const SUBJECT: Record<ServerRead['kind'], (name: string) => string> = {
|
|
39
|
+
signal: name => `Signal '${name}' is seeded from`,
|
|
40
|
+
memo: name => `Memo '${name}' computes`,
|
|
41
|
+
const: name => `'${name}' is initialized from`,
|
|
42
|
+
function: name => `Function '${name}' returns`,
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
function record(kind: ServerRead['kind'], name: string, fn: string, loc: SourceLocation, sink: UninlinableSeedCallSink): void {
|
|
46
|
+
const what = SUBJECT[kind](name)
|
|
47
|
+
const message =
|
|
48
|
+
`${what} '${fn}()', a call of a function a template cannot compute: only a function that takes no parameters and ` +
|
|
49
|
+
`whose body is a single 'return <expr>' is read on the server, as the expression it returns. The server render ` +
|
|
50
|
+
`would show '${name}' empty where JS shows the call's result.`
|
|
51
|
+
if (sink.errors.some(e => e.code === 'BF101' && e.message === message)) return
|
|
52
|
+
sink.errors.push({
|
|
53
|
+
code: 'BF101',
|
|
54
|
+
severity: 'error',
|
|
55
|
+
message,
|
|
56
|
+
loc,
|
|
57
|
+
suggestion: {
|
|
58
|
+
message: `Make '${fn}' a single 'return <expr>' with no parameters, or defer the reads of '${name}' to the client with /* @client */.`,
|
|
59
|
+
escape: [{ kind: 'client-directive' }],
|
|
60
|
+
},
|
|
61
|
+
})
|
|
62
|
+
}
|
package/src/analyzer.ts
CHANGED
|
@@ -9,7 +9,7 @@
|
|
|
9
9
|
import ts from 'typescript'
|
|
10
10
|
import { foldRenderReassignmentsInSource } from './fold-render-reassignments.ts'
|
|
11
11
|
import { CLIENT_DIRECTIVE_INTERIOR_RE, USE_CLIENT_DIRECTIVE, isUseClientDirectiveText } from './directives.ts'
|
|
12
|
-
import type { ImportSpecifier, TypeInfo, ParamInfo, PropDefaultPart, PropDefaultRead, ReactiveFactoryInfo, DeclinedReactiveFactory, RequiredFactoryImport, FactoryRenameSite, SourceLocation, SignalFactoryCall, MemoInfo, SignalInfo } from './types.ts'
|
|
12
|
+
import type { ImportSpecifier, TypeInfo, ParamInfo, PropDefaultPart, PropDefaultRead, ReactiveFactoryInfo, DeclinedReactiveFactory, RequiredFactoryImport, FactoryRenameSite, SourceLocation, SignalFactoryCall, MemoInfo, SignalInfo, ConstantInfo } from './types.ts'
|
|
13
13
|
import { CALLBACK_METHODS, parseExpression, parseBlockBodyTolerant, foldBlockToExpr, tsNodeToParsedExpr, stringifyParsedExpr, type ParsedExpr } from './expression-parser.ts'
|
|
14
14
|
import type { CallbackBodyAcceptor } from './adapters/interface.ts'
|
|
15
15
|
import { collectAssignmentTargets, rewriteBarePropRefs, splitScopedValueRefs } from './prop-rewrite.ts'
|
|
@@ -39,6 +39,7 @@ import { separateSignalPropBindings, programWithSeparatedSource } from './signal
|
|
|
39
39
|
import { isAssignmentTargetIdentifier } from './assignment-target.ts'
|
|
40
40
|
import { addSourceReplacement } from './strip-types.ts'
|
|
41
41
|
import { walkWithScope } from './scope/lexical-scope-walk.ts'
|
|
42
|
+
import { markIdentityBoundConstants, recordIdentityCandidate } from './const-identity.ts'
|
|
42
43
|
|
|
43
44
|
// =============================================================================
|
|
44
45
|
// TypeScript Program Creation
|
|
@@ -3429,6 +3430,8 @@ function collectFunction(
|
|
|
3429
3430
|
// rewrite (the failure mode behind #1130) preserves the modifier from IR
|
|
3430
3431
|
// rather than reconstructing it from text.
|
|
3431
3432
|
const isGenerator = !!node.asteriskToken
|
|
3433
|
+
// A body rewritten for branch-local names above no longer matches its tree.
|
|
3434
|
+
const returnParsed = plainReturnParsed(node, body, isJsxFunction, ctx)
|
|
3432
3435
|
|
|
3433
3436
|
ctx.localFunctions.push({
|
|
3434
3437
|
name,
|
|
@@ -3444,12 +3447,44 @@ function collectFunction(
|
|
|
3444
3447
|
isGenerator: isGenerator || undefined,
|
|
3445
3448
|
declarationKind: 'function',
|
|
3446
3449
|
isModule: _isModule || undefined,
|
|
3450
|
+
inComponentBody: inComponentBody(node, ctx),
|
|
3451
|
+
returnParsed,
|
|
3447
3452
|
isJsxFunction: isJsxFunction || undefined,
|
|
3448
3453
|
freeIdentifiers: extractFreeIdentifiersFromNode(node),
|
|
3449
3454
|
loc: getSourceLocation(node, ctx.sourceFile, ctx.filePath),
|
|
3450
3455
|
})
|
|
3451
3456
|
}
|
|
3452
3457
|
|
|
3458
|
+
/**
|
|
3459
|
+
* The tree of the expression a parameterless, non-async, non-generator
|
|
3460
|
+
* function whose body is one `return <expr>` statement returns; `undefined`
|
|
3461
|
+
* for any other function, a body rewritten from its source (`body`), or an
|
|
3462
|
+
* expression the parser does not support.
|
|
3463
|
+
*/
|
|
3464
|
+
function plainReturnParsed(
|
|
3465
|
+
node: ts.FunctionDeclaration,
|
|
3466
|
+
body: string,
|
|
3467
|
+
isJsxFunction: boolean,
|
|
3468
|
+
ctx: AnalyzerContext,
|
|
3469
|
+
): ParsedExpr | undefined {
|
|
3470
|
+
if (node.parameters.length > 0 || !node.body || isJsxFunction || node.asteriskToken) return undefined
|
|
3471
|
+
if (node.modifiers?.some(m => m.kind === ts.SyntaxKind.AsyncKeyword) || body !== ctx.getJS(node.body)) return undefined
|
|
3472
|
+
const [statement, ...rest] = node.body.statements
|
|
3473
|
+
if (!statement || rest.length > 0 || !ts.isReturnStatement(statement) || !statement.expression) return undefined
|
|
3474
|
+
const parsed = parseExpression(ctx.getJS(statement.expression))
|
|
3475
|
+
return parsed && parsed.kind !== 'unsupported' ? parsed : undefined
|
|
3476
|
+
}
|
|
3477
|
+
|
|
3478
|
+
/** `FunctionInfo.inComponentBody`: `true` when `node` is nested inside the component function. */
|
|
3479
|
+
function inComponentBody(node: ts.Node, ctx: AnalyzerContext): true | undefined {
|
|
3480
|
+
const component = ctx.componentNode
|
|
3481
|
+
if (!component) return undefined
|
|
3482
|
+
for (let current = node.parent; current; current = current.parent) {
|
|
3483
|
+
if (current === component) return true
|
|
3484
|
+
}
|
|
3485
|
+
return undefined
|
|
3486
|
+
}
|
|
3487
|
+
|
|
3453
3488
|
// =============================================================================
|
|
3454
3489
|
// Constant Collection
|
|
3455
3490
|
// =============================================================================
|
|
@@ -4609,7 +4644,7 @@ function collectConstant(
|
|
|
4609
4644
|
? parseExpression(`(${value.trim()})`)
|
|
4610
4645
|
: undefined
|
|
4611
4646
|
|
|
4612
|
-
|
|
4647
|
+
const constant: ConstantInfo = {
|
|
4613
4648
|
name,
|
|
4614
4649
|
value,
|
|
4615
4650
|
parsed,
|
|
@@ -4633,7 +4668,9 @@ function collectConstant(
|
|
|
4633
4668
|
scope: isModule ? 'module' : 'init',
|
|
4634
4669
|
effect: 'pure',
|
|
4635
4670
|
},
|
|
4636
|
-
}
|
|
4671
|
+
}
|
|
4672
|
+
recordIdentityCandidate(constant, node.initializer, ctx.checker)
|
|
4673
|
+
ctx.localConstants.push(constant)
|
|
4637
4674
|
}
|
|
4638
4675
|
|
|
4639
4676
|
// =============================================================================
|
|
@@ -5333,6 +5370,9 @@ function validateContext(ctx: AnalyzerContext): void {
|
|
|
5333
5370
|
// downstream inlining never re-embeds their stale initializer text (#2910).
|
|
5334
5371
|
markMutatedConstants(ctx)
|
|
5335
5372
|
markWrittenBindings(ctx)
|
|
5373
|
+
// A const whose initializer allocates and reads nothing reactive is read
|
|
5374
|
+
// as the binding, never re-evaluated at a prop read (#3868).
|
|
5375
|
+
markIdentityBoundConstants(ctx)
|
|
5336
5376
|
writeTemplatePropReads(ctx)
|
|
5337
5377
|
|
|
5338
5378
|
// BF052: init statements that write to an identifier with no visible
|
|
@@ -13,9 +13,7 @@
|
|
|
13
13
|
import ts from 'typescript'
|
|
14
14
|
|
|
15
15
|
import type { ComponentIR, IRMetadata, IRNode, IRElement, IRTemplatePart, TypeInfo } from './types.ts'
|
|
16
|
-
import type { ParsedExpr } from './expression-parser.ts'
|
|
17
16
|
import { isBooleanAttr } from './html-constants.ts'
|
|
18
|
-
import { literalMemberIndex } from './literal-member-index.ts'
|
|
19
17
|
|
|
20
18
|
/**
|
|
21
19
|
* A `const x = useContext(SomeContext)` consumer in a component body. SSR
|
|
@@ -474,7 +472,7 @@ type StaticConstants = IRMetadata['localConstants'] | undefined
|
|
|
474
472
|
* The const declaration a read of `name` reaches, when its declared value
|
|
475
473
|
* is what the read sees — the ONE decision behind every compile-time lookup
|
|
476
474
|
* of a literal const's member (`lookupStaticRecordLiteral`,
|
|
477
|
-
* `
|
|
475
|
+
* `lookupStaticConstMember`, the Go adapter's record lookup). Returns
|
|
478
476
|
* `null` when the read must fall back to the generic lowering:
|
|
479
477
|
*
|
|
480
478
|
* - `isShadowed(name)`: an enclosing loop callback's param, index,
|
|
@@ -515,8 +513,9 @@ export function resolveStaticConstBinding(
|
|
|
515
513
|
* compile-time lookup (the icon registry's `strokePaths['chevron-down']`,
|
|
516
514
|
* pagination's `variantClasses.ghost`; #1896 / #1897). Returns the
|
|
517
515
|
* looked-up scalar, or `null` for any other shape so callers fall back
|
|
518
|
-
* to their generic lowering. A
|
|
519
|
-
* literal
|
|
516
|
+
* to their generic lowering. A read the literal const answers structurally
|
|
517
|
+
* (an array element, a key the literal lacks) goes through
|
|
518
|
+
* `lookupStaticConstMember` (`static-const-member.ts`) first.
|
|
520
519
|
* Shared by all seven template-string adapters;
|
|
521
520
|
* the prop-KEYED variant of the pattern lives in `parseRecordIndexAccess`.
|
|
522
521
|
*
|
|
@@ -566,50 +565,6 @@ export function lookupStaticRecordLiteral(
|
|
|
566
565
|
return null
|
|
567
566
|
}
|
|
568
567
|
|
|
569
|
-
/**
|
|
570
|
-
* The element at `key` of a module or component-local array-literal const
|
|
571
|
-
* (`QS[0]`, `QS['1']`)
|
|
572
|
-
* or its `length`, as the element's own typed `ParsedExpr`: a string or
|
|
573
|
-
* number `literal`, or a unary `-` over a number literal. The caller emits
|
|
574
|
-
* it through its own expression emitter, exactly as if the source had
|
|
575
|
-
* written the element in place, so a value such as `-0` keeps its sign in
|
|
576
|
-
* arithmetic and a comparison (`1 / QS[0] < 0`) and is spelled `0` only
|
|
577
|
-
* where JS stringifies it. An index key must be canonical (`'01'` names no
|
|
578
|
-
* element). A hole or spread at or before the element (either also makes
|
|
579
|
-
* `length` unknown), an out-of-range index or any other element shape is
|
|
580
|
-
* `null`, so the caller falls back to its generic lowering.
|
|
581
|
-
*
|
|
582
|
-
* `isShadowed` is REQUIRED for the same reason as in
|
|
583
|
-
* `lookupStaticRecordLiteral`; which declaration the name reaches is
|
|
584
|
-
* `resolveStaticConstBinding`'s decision.
|
|
585
|
-
*/
|
|
586
|
-
export function lookupStaticArrayElement(
|
|
587
|
-
objectName: string,
|
|
588
|
-
key: string,
|
|
589
|
-
constants: StaticConstants,
|
|
590
|
-
isShadowed: (name: string) => boolean,
|
|
591
|
-
): ParsedExpr | null {
|
|
592
|
-
const array = resolveStaticConstBinding(objectName, constants, isShadowed)?.parsed
|
|
593
|
-
if (array?.kind !== 'array-literal') return null
|
|
594
|
-
if (key === 'length') {
|
|
595
|
-
if (array.elements.some(e => e.kind === 'unsupported')) return null
|
|
596
|
-
return { kind: 'literal', value: array.elements.length, literalType: 'number', raw: String(array.elements.length) }
|
|
597
|
-
}
|
|
598
|
-
const index = literalMemberIndex(key)
|
|
599
|
-
if (index.kind !== 'literal' || typeof index.value !== 'number') return null
|
|
600
|
-
const upTo = array.elements.slice(0, index.value + 1)
|
|
601
|
-
if (upTo.length <= index.value || upTo.some(e => e.kind === 'unsupported')) return null
|
|
602
|
-
const element = upTo[index.value]
|
|
603
|
-
if (element.kind === 'literal' && (element.literalType === 'string' || element.literalType === 'number')) return element
|
|
604
|
-
if (
|
|
605
|
-
element.kind === 'unary' && element.op === '-' &&
|
|
606
|
-
element.argument.kind === 'literal' && element.argument.literalType === 'number'
|
|
607
|
-
) {
|
|
608
|
-
return element
|
|
609
|
-
}
|
|
610
|
-
return null
|
|
611
|
-
}
|
|
612
|
-
|
|
613
568
|
/**
|
|
614
569
|
* A literal-initialized const's value. A number carries its `value` (a
|
|
615
570
|
* `const NZ = -0` is negative zero, which `text` cannot say), which each
|
package/src/compiler.ts
CHANGED
|
@@ -410,7 +410,7 @@ function compileMultipleComponents(
|
|
|
410
410
|
nonExportedSiblings.add(componentIR.metadata.componentName)
|
|
411
411
|
}
|
|
412
412
|
}
|
|
413
|
-
setActiveComponentScope({ fileScope, nonExportedSiblings })
|
|
413
|
+
const outerScope = setActiveComponentScope({ fileScope, nonExportedSiblings })
|
|
414
414
|
// Same adapter capabilities for every component compiled in this file
|
|
415
415
|
// (#1187 phase 3).
|
|
416
416
|
const multiAdapterCaps = {
|
|
@@ -482,7 +482,7 @@ function compileMultipleComponents(
|
|
|
482
482
|
errors.push(...componentIR.errors)
|
|
483
483
|
}
|
|
484
484
|
} finally {
|
|
485
|
-
setActiveComponentScope(
|
|
485
|
+
setActiveComponentScope(outerScope)
|
|
486
486
|
}
|
|
487
487
|
|
|
488
488
|
if (allOutputs.length === 0) {
|
|
@@ -956,7 +956,7 @@ export function compileJSX(
|
|
|
956
956
|
? new Set<string>()
|
|
957
957
|
: new Set([componentIR.metadata.componentName]),
|
|
958
958
|
}
|
|
959
|
-
setActiveComponentScope(singleScope)
|
|
959
|
+
const outerScope = setActiveComponentScope(singleScope)
|
|
960
960
|
// Adapter capabilities thread through to relocate's inline-safety
|
|
961
961
|
// check so a registered template primitive escapes the bridged-arg /
|
|
962
962
|
// zero-arg rejection (#1187 phase 3).
|
|
@@ -1000,7 +1000,7 @@ export function compileJSX(
|
|
|
1000
1000
|
}
|
|
1001
1001
|
}
|
|
1002
1002
|
} finally {
|
|
1003
|
-
setActiveComponentScope(
|
|
1003
|
+
setActiveComponentScope(outerScope)
|
|
1004
1004
|
}
|
|
1005
1005
|
|
|
1006
1006
|
return { files, errors }
|
|
@@ -0,0 +1,189 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Which consts a read site must reference as the binding itself rather than
|
|
3
|
+
* re-evaluate from the initializer (#3868).
|
|
4
|
+
*
|
|
5
|
+
* The client emitter passes a child component's props as getters, and for a
|
|
6
|
+
* prop whose value is a bare const it used to splice the const's initializer
|
|
7
|
+
* text into the getter (`get token() { return { n: 1 } }`). Each read then
|
|
8
|
+
* evaluated the initializer again. For a value that is a primitive this is
|
|
9
|
+
* invisible, and for one that reads a signal, a memo or a prop it is what
|
|
10
|
+
* keeps the prop live. For an initializer that allocates (an object or array
|
|
11
|
+
* literal, `new X()`, a call, a function expression) and reads nothing
|
|
12
|
+
* reactive it only breaks identity: two reads of the prop see two objects,
|
|
13
|
+
* where the source and the SSR render pass the one object the declaration
|
|
14
|
+
* created.
|
|
15
|
+
*
|
|
16
|
+
* The decision is made here, in Phase 1, from the initializer's AST and the
|
|
17
|
+
* component's bindings, and carried as `ConstantInfo.identityBound`. The
|
|
18
|
+
* emitter only reads the flag.
|
|
19
|
+
*/
|
|
20
|
+
|
|
21
|
+
import ts from 'typescript'
|
|
22
|
+
import type { AnalyzerContext } from './analyzer-context.ts'
|
|
23
|
+
import type { ConstantInfo } from './types.ts'
|
|
24
|
+
import { containsReactiveExpression } from './reactivity-checker.ts'
|
|
25
|
+
import { signalSecondBinding } from './signal-initializer.ts'
|
|
26
|
+
|
|
27
|
+
function unwrapTransparent(node: ts.Expression): ts.Expression {
|
|
28
|
+
let current = node
|
|
29
|
+
while (
|
|
30
|
+
ts.isParenthesizedExpression(current) ||
|
|
31
|
+
ts.isAsExpression(current) ||
|
|
32
|
+
ts.isSatisfiesExpression(current) ||
|
|
33
|
+
ts.isNonNullExpression(current) ||
|
|
34
|
+
ts.isTypeAssertionExpression(current)
|
|
35
|
+
) {
|
|
36
|
+
current = current.expression
|
|
37
|
+
}
|
|
38
|
+
return current
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
function isAllocatingNode(node: ts.Expression): boolean {
|
|
42
|
+
return (
|
|
43
|
+
ts.isObjectLiteralExpression(node) ||
|
|
44
|
+
ts.isArrayLiteralExpression(node) ||
|
|
45
|
+
ts.isNewExpression(node) ||
|
|
46
|
+
ts.isCallExpression(node) ||
|
|
47
|
+
ts.isTaggedTemplateExpression(node) ||
|
|
48
|
+
ts.isArrowFunction(node) ||
|
|
49
|
+
ts.isFunctionExpression(node) ||
|
|
50
|
+
ts.isClassExpression(node) ||
|
|
51
|
+
ts.isRegularExpressionLiteral(node)
|
|
52
|
+
)
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
const VALUE_SELECTING_OPERATORS: ReadonlySet<ts.SyntaxKind> = new Set([
|
|
56
|
+
ts.SyntaxKind.QuestionQuestionToken,
|
|
57
|
+
ts.SyntaxKind.BarBarToken,
|
|
58
|
+
ts.SyntaxKind.AmpersandAmpersandToken,
|
|
59
|
+
ts.SyntaxKind.CommaToken,
|
|
60
|
+
])
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Can evaluating `initializer` a second time yield a different object than
|
|
64
|
+
* the first evaluation did? True when the value it produces may be one it
|
|
65
|
+
* creates: an allocating expression itself, or a ternary / `??` / `||` /
|
|
66
|
+
* `&&` / comma whose result may be such an operand.
|
|
67
|
+
*/
|
|
68
|
+
export function initializerAllocates(initializer: ts.Expression): boolean {
|
|
69
|
+
const node = unwrapTransparent(initializer)
|
|
70
|
+
if (isAllocatingNode(node)) return true
|
|
71
|
+
if (ts.isConditionalExpression(node)) {
|
|
72
|
+
return initializerAllocates(node.whenTrue) || initializerAllocates(node.whenFalse)
|
|
73
|
+
}
|
|
74
|
+
if (ts.isBinaryExpression(node) && VALUE_SELECTING_OPERATORS.has(node.operatorToken.kind)) {
|
|
75
|
+
return initializerAllocates(node.left) || initializerAllocates(node.right)
|
|
76
|
+
}
|
|
77
|
+
return false
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/** Is `initializer` an arrow or function expression (evaluating it reads nothing)? */
|
|
81
|
+
function isFunctionExpressionNode(initializer: ts.Expression): boolean {
|
|
82
|
+
const node = unwrapTransparent(initializer)
|
|
83
|
+
return ts.isArrowFunction(node) || ts.isFunctionExpression(node)
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
interface IdentityCandidate {
|
|
87
|
+
readsReactiveBrand: boolean
|
|
88
|
+
isFunctionExpression: boolean
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* The per-declaration half of the decision, recorded while the declaration's
|
|
93
|
+
* AST is at hand: only consts whose initializer allocates are candidates, and
|
|
94
|
+
* whether the initializer reads a `Reactive<T>`-branded value needs the type
|
|
95
|
+
* checker (a component compiled without one reads none). Kept off
|
|
96
|
+
* `ConstantInfo`: `markIdentityBoundConstants` turns it into
|
|
97
|
+
* `identityBound` once the whole body has been walked.
|
|
98
|
+
*/
|
|
99
|
+
const candidates: WeakMap<ConstantInfo, IdentityCandidate> = new WeakMap()
|
|
100
|
+
|
|
101
|
+
export function recordIdentityCandidate(
|
|
102
|
+
constant: ConstantInfo,
|
|
103
|
+
initializer: ts.Expression | undefined,
|
|
104
|
+
checker: ts.TypeChecker | null,
|
|
105
|
+
): void {
|
|
106
|
+
if (!initializer || !initializerAllocates(initializer)) return
|
|
107
|
+
if (isFunctionExpressionNode(initializer)) {
|
|
108
|
+
// Evaluating a function expression reads none of the names its body
|
|
109
|
+
// reads, so re-evaluating it never made the prop reactive.
|
|
110
|
+
candidates.set(constant, { readsReactiveBrand: false, isFunctionExpression: true })
|
|
111
|
+
return
|
|
112
|
+
}
|
|
113
|
+
const readsReactiveBrand = checker ? containsReactiveExpression(initializer, checker) : false
|
|
114
|
+
candidates.set(constant, { readsReactiveBrand, isFunctionExpression: false })
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* The component's bindings whose read, re-evaluated at each prop read, can
|
|
119
|
+
* see a new value: signal accessors (and their setters / actions), memos,
|
|
120
|
+
* props, the body-destructure aliases of props (rewritten to live prop
|
|
121
|
+
* reads), and component-scope functions, whose call may read any of those.
|
|
122
|
+
*/
|
|
123
|
+
function reactiveSourceNames(ctx: AnalyzerContext): Set<string> {
|
|
124
|
+
const names = new Set<string>()
|
|
125
|
+
for (const s of ctx.signals) {
|
|
126
|
+
names.add(s.getter)
|
|
127
|
+
const second = signalSecondBinding(s)
|
|
128
|
+
if (second) names.add(second)
|
|
129
|
+
}
|
|
130
|
+
for (const m of ctx.memos) names.add(m.name)
|
|
131
|
+
for (const p of ctx.propsParams) names.add(p.name)
|
|
132
|
+
if (ctx.propsObjectName) names.add(ctx.propsObjectName)
|
|
133
|
+
if (ctx.restPropsName) names.add(ctx.restPropsName)
|
|
134
|
+
for (const f of ctx.localFunctions) {
|
|
135
|
+
if (!f.isModule) names.add(f.name)
|
|
136
|
+
}
|
|
137
|
+
for (const c of ctx.localConstants) {
|
|
138
|
+
if (c.isModule) continue
|
|
139
|
+
if (c.propsDestructure || c.isFunctionValue) names.add(c.name)
|
|
140
|
+
}
|
|
141
|
+
addReactiveAliases(ctx, names)
|
|
142
|
+
return names
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/**
|
|
146
|
+
* A component const whose initializer reads a reactive source is one too:
|
|
147
|
+
* `const p = props` or `const get = count` aliases it, and
|
|
148
|
+
* `const half = count() / 2` derives from it. The client inlines such a
|
|
149
|
+
* const at each read, so a const that reads IT stays live as well. Runs
|
|
150
|
+
* to a fixpoint, since an alias may be declared through another alias.
|
|
151
|
+
*/
|
|
152
|
+
function addReactiveAliases(ctx: AnalyzerContext, names: Set<string>): void {
|
|
153
|
+
const consts = ctx.localConstants.filter(c => !c.isModule)
|
|
154
|
+
let grew = true
|
|
155
|
+
while (grew) {
|
|
156
|
+
grew = false
|
|
157
|
+
for (const c of consts) {
|
|
158
|
+
if (names.has(c.name) || !readsAny(c.freeIdentifiers, names)) continue
|
|
159
|
+
names.add(c.name)
|
|
160
|
+
grew = true
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
/**
|
|
166
|
+
* Set `identityBound` on every const whose initializer allocates and reads
|
|
167
|
+
* nothing reactive (see the module comment). A const written after its
|
|
168
|
+
* declaration is left alone: `mutatedAfterDeclaration` already keeps it a
|
|
169
|
+
* reference.
|
|
170
|
+
*/
|
|
171
|
+
export function markIdentityBoundConstants(ctx: AnalyzerContext): void {
|
|
172
|
+
const pending = ctx.localConstants.filter(c => candidates.has(c) && !c.isJsx && !c.isJsxFunction)
|
|
173
|
+
if (pending.length === 0) return
|
|
174
|
+
const reactive = reactiveSourceNames(ctx)
|
|
175
|
+
for (const constant of pending) {
|
|
176
|
+
const candidate = candidates.get(constant)!
|
|
177
|
+
if (candidate.readsReactiveBrand) continue
|
|
178
|
+
if (!candidate.isFunctionExpression && readsAny(constant.freeIdentifiers, reactive)) continue
|
|
179
|
+
constant.identityBound = true
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
function readsAny(names: ReadonlySet<string> | undefined, of: ReadonlySet<string>): boolean {
|
|
184
|
+
if (!names) return false
|
|
185
|
+
for (const name of names) {
|
|
186
|
+
if (of.has(name)) return true
|
|
187
|
+
}
|
|
188
|
+
return false
|
|
189
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -39,7 +39,8 @@ export { resolveGetterAliases, collectAliasableGetterNames } from './ir-to-clien
|
|
|
39
39
|
export { buildImportAliasMap } from './ir-to-client-js/component-scope.ts'
|
|
40
40
|
|
|
41
41
|
// Backend-neutral SSR seed plan (in-template derived signal/memo seeding)
|
|
42
|
-
export { computeSsrSeedPlan, hasComputedDefault, inDefaultReadOrder } from './ssr-seed-plan.ts'
|
|
42
|
+
export { appliedPropDefaults, computeSsrSeedPlan, hasComputedDefault, inDefaultReadOrder, type PropDefaultApplication } from './ssr-seed-plan.ts'
|
|
43
|
+
export type { ResolvedPropDefault } from './prop-default-resolution.ts'
|
|
43
44
|
export type { SsrSeedPlan, SsrSeedStep } from './ssr-seed-plan.ts'
|
|
44
45
|
|
|
45
46
|
// Pure IR types
|
|
@@ -132,10 +133,13 @@ export type { JsxAdapterConfig } from './adapters/jsx-adapter.ts'
|
|
|
132
133
|
export { rewriteImportsForTemplate, rewriteDynamicImportsInSource } from './adapters/template-imports.ts'
|
|
133
134
|
export { emitParsedExpr, emitParsedValue, primitivePlanFor, arithmeticHelper, groupBinaryOperand, groupUnaryOperand, groupObjectLiteralSegments, isStringTypedOperand, isStringConcatBinary, isNumericArithmeticResult } from './adapters/parsed-expr-emitter.ts'
|
|
134
135
|
export type { StringNameTest } from './adapters/parsed-expr-emitter.ts'
|
|
135
|
-
export { concatType, declaredValueTypes, loopRowType, type DeclaredValueTypes } from './adapters/loop-row-type.ts'
|
|
136
|
+
export { concatType, declaredValueMayBeNil, declaredValueTypes, loopRowType, type DeclaredValueTypes } from './adapters/loop-row-type.ts'
|
|
136
137
|
export { collectNumberValueNames, isNumberValuedExpr, numberValuedSeedNames } from './adapters/number-value.ts'
|
|
137
138
|
export { operandFacts, planPrimitive, planTest, spellTest, primitivesAt, isNativePlan, relationalHelper, isNativeToNumber, booleanValueSites, planTextDisplay, foldNegatedNumberLiteral, numberLiteralSpelling, isRuntimeNumberLiteral, plusSpelling, planToString, needsToString, strictEqSpelling, mayHoldObject, isNullishTest, isNilLiteralOperand, nilTestOperand, refuseUnspelledNilTest, NON_PRIMITIVE_EQUALITY_REASON } from './adapters/primitive-plan.ts'
|
|
138
139
|
export { refuseNonPrimitiveStrictEquality, type NonPrimitiveEqualitySink } from './adapters/non-primitive-equality.ts'
|
|
140
|
+
export { refuseInheritedConstServerReads } from './adapters/inherited-const-server-read.ts'
|
|
141
|
+
export { refuseUninlinableSeedCalls, type UninlinableSeedCallSink } from './adapters/uninlinable-seed-call.ts'
|
|
142
|
+
export { refuseUnappliedPropDefaults, type UnappliedPropDefaultSink } from './adapters/unapplied-prop-default.ts'
|
|
139
143
|
export { rewriteNilReadings, rewriteNilReadingsDeep, isNilReadingFallback, NAN_LITERAL } from './adapters/nil-reading.ts'
|
|
140
144
|
export type { PrimitiveOp, Proof, NativeSafeClaims, NativeSafeTable, PrimitiveHelper, NilTestHelper, PrimitivePlan, PrimitiveInputs, TextDisplay, NumberLiteralSpelling, PlusSpelling, StrictEqSpelling } from './adapters/primitive-plan.ts'
|
|
141
145
|
export {
|
|
@@ -214,7 +218,7 @@ export type {
|
|
|
214
218
|
export { derivesScopeFromSlot } from './adapters/child-scope.ts'
|
|
215
219
|
export { childTemplateName, componentPropKey, isEmptyComponentProps, planComponentProps, propsSpreadParam, resolveComponentPropKeys, spellComponentProps, templatePropValue } from './adapters/component-props.ts'
|
|
216
220
|
export type { ComponentPropEntry, ComponentPropSegment, ComponentPropSpelling, ComponentPropsOptions, ComponentPropsParam, EarlierComponentProps, ResolvedComponentProp, SpelledComponentPropSegment } from './adapters/component-props.ts'
|
|
217
|
-
export { parseUndefinedAlternateTernary, planAttrExpression, planSpreadAttr, } from './adapters/attr-plan.ts'
|
|
221
|
+
export { parseUndefinedAlternateTernary, planAttrExpression, planSpreadAttr, spreadsPropsObject, } from './adapters/attr-plan.ts'
|
|
218
222
|
export type { AttrExpressionPlan, AttrPlanContext, SpreadAttrPlan, SpreadPlanContext, UndefinedAlternateTernary } from './adapters/attr-plan.ts'
|
|
219
223
|
export { loopHeaderTargets, planLoop, savedLoopBindings, spellDestructureLocals, spellFilteredRow, spellSortedSource } from './adapters/loop-plan.ts'
|
|
220
224
|
export { computedConstContext, inlineComputedConstAttr, inlineComputedConsts } from './adapters/computed-const.ts'
|
|
@@ -496,8 +500,9 @@ export { BOOLEAN_ATTRS, isBooleanAttr } from './html-constants.ts'
|
|
|
496
500
|
export { USE_CLIENT_DIRECTIVE, CLIENT_EXPRESSION_DIRECTIVE } from './directives.ts'
|
|
497
501
|
|
|
498
502
|
// Shared props-object-pattern helpers for the Go / Mojo template adapters
|
|
499
|
-
export { augmentInheritedPropAccesses, parseRecordIndexAccess, evalStringArrayJoin, collectModuleStringConsts, lookupStaticRecordLiteral,
|
|
503
|
+
export { augmentInheritedPropAccesses, parseRecordIndexAccess, evalStringArrayJoin, collectModuleStringConsts, lookupStaticRecordLiteral, resolveStaticConstBinding, lookupLiteralConst, collectContextConsumers, contextConsumerSeed, type LiteralConst } from './augment-inherited-props.ts'
|
|
500
504
|
export type { RecordIndexAccess, RecordIndexEntry, ContextConsumer } from './augment-inherited-props.ts'
|
|
505
|
+
export { lookupStaticConstMember, readStaticConstMember, spellStaticConstMember, staticIndexKey, type StaticConstMemberRead, type StaticConstMemberSink } from './static-const-member.ts'
|
|
501
506
|
|
|
502
507
|
// HTML element attribute types
|
|
503
508
|
export type {
|