@barefootjs/jsx 0.39.6 → 0.39.8
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 -1
- package/dist/adapters/attr-plan.d.ts.map +1 -1
- package/dist/adapters/callback-method.d.ts +44 -0
- package/dist/adapters/callback-method.d.ts.map +1 -1
- package/dist/adapters/component-props.d.ts +80 -13
- package/dist/adapters/component-props.d.ts.map +1 -1
- package/dist/adapters/computed-const.d.ts +52 -0
- package/dist/adapters/computed-const.d.ts.map +1 -0
- package/dist/adapters/dangerous-inner-html.d.ts +10 -0
- package/dist/adapters/dangerous-inner-html.d.ts.map +1 -1
- package/dist/adapters/dsl-template-adapter.d.ts +151 -0
- package/dist/adapters/dsl-template-adapter.d.ts.map +1 -0
- package/dist/adapters/element-shell.d.ts +44 -0
- package/dist/adapters/element-shell.d.ts.map +1 -0
- package/dist/adapters/interface.d.ts +2 -0
- package/dist/adapters/interface.d.ts.map +1 -1
- package/dist/adapters/jsx-adapter.d.ts +4 -0
- package/dist/adapters/jsx-adapter.d.ts.map +1 -1
- package/dist/adapters/literal-value.d.ts +2 -2
- package/dist/adapters/literal-value.d.ts.map +1 -1
- package/dist/adapters/parsed-expr-emitter.d.ts +27 -0
- package/dist/adapters/parsed-expr-emitter.d.ts.map +1 -1
- package/dist/adapters/style-object.d.ts +33 -0
- package/dist/adapters/style-object.d.ts.map +1 -0
- package/dist/analyzer.d.ts.map +1 -1
- package/dist/augment-inherited-props.d.ts +7 -0
- package/dist/augment-inherited-props.d.ts.map +1 -1
- package/dist/children-nullish.d.ts +22 -0
- package/dist/children-nullish.d.ts.map +1 -0
- package/dist/errors.d.ts +1 -0
- package/dist/errors.d.ts.map +1 -1
- package/dist/expression-parser.d.ts +9 -19
- package/dist/expression-parser.d.ts.map +1 -1
- package/dist/fold-render-reassignments.d.ts +43 -0
- package/dist/fold-render-reassignments.d.ts.map +1 -0
- package/dist/free-refs.d.ts +15 -0
- package/dist/free-refs.d.ts.map +1 -1
- package/dist/index.d.ts +21 -14
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +24620 -23233
- package/dist/ir-to-client-js/compute-inlinability.d.ts.map +1 -1
- package/dist/ir-to-client-js/control-flow/plan/event-collision.d.ts +2 -1
- package/dist/ir-to-client-js/control-flow/plan/event-collision.d.ts.map +1 -1
- package/dist/ir-to-client-js/control-flow/plan/insert.d.ts +2 -2
- package/dist/ir-to-client-js/control-flow/plan/insert.d.ts.map +1 -1
- package/dist/ir-to-client-js/control-flow/stringify/event-delegation.d.ts.map +1 -1
- package/dist/ir-to-client-js/control-flow/stringify/event-listener.d.ts +10 -9
- package/dist/ir-to-client-js/control-flow/stringify/event-listener.d.ts.map +1 -1
- package/dist/ir-to-client-js/csr-substitute.d.ts +0 -13
- package/dist/ir-to-client-js/csr-substitute.d.ts.map +1 -1
- package/dist/ir-to-client-js/emit-reactive.d.ts +1 -1
- package/dist/ir-to-client-js/emit-reactive.d.ts.map +1 -1
- package/dist/ir-to-client-js/emit-registration.d.ts +10 -8
- package/dist/ir-to-client-js/emit-registration.d.ts.map +1 -1
- package/dist/ir-to-client-js/generate-init.d.ts.map +1 -1
- package/dist/ir-to-client-js/html-template.d.ts +9 -19
- package/dist/ir-to-client-js/html-template.d.ts.map +1 -1
- package/dist/ir-to-client-js/identifiers.d.ts +6 -8
- package/dist/ir-to-client-js/identifiers.d.ts.map +1 -1
- package/dist/ir-to-client-js/imports.d.ts +23 -2
- package/dist/ir-to-client-js/imports.d.ts.map +1 -1
- package/dist/ir-to-client-js/index.d.ts.map +1 -1
- package/dist/ir-to-client-js/phases/props-extraction.d.ts +3 -2
- package/dist/ir-to-client-js/phases/props-extraction.d.ts.map +1 -1
- package/dist/ir-to-client-js/plan/declaration-emit.d.ts +5 -0
- package/dist/ir-to-client-js/plan/declaration-emit.d.ts.map +1 -1
- package/dist/ir-to-client-js/prop-handling.d.ts +11 -12
- package/dist/ir-to-client-js/prop-handling.d.ts.map +1 -1
- package/dist/ir-to-client-js/prune-unused-prop-extractions.d.ts +1 -1
- package/dist/ir-to-client-js/prune-unused-prop-extractions.d.ts.map +1 -1
- package/dist/ir-to-client-js/reactivity.d.ts.map +1 -1
- package/dist/ir-to-client-js/rewrite-destructured-props.d.ts +1 -1
- package/dist/ir-to-client-js/rewrite-destructured-props.d.ts.map +1 -1
- package/dist/ir-to-client-js/safe-html.d.ts +20 -2
- package/dist/ir-to-client-js/safe-html.d.ts.map +1 -1
- package/dist/ir-to-client-js/types.d.ts +3 -3
- package/dist/ir-to-client-js/types.d.ts.map +1 -1
- package/dist/ir-to-client-js/utils.d.ts +15 -11
- package/dist/ir-to-client-js/utils.d.ts.map +1 -1
- package/dist/jsx-to-ir.d.ts.map +1 -1
- package/dist/nullable-signal-attr.d.ts +43 -12
- package/dist/nullable-signal-attr.d.ts.map +1 -1
- package/dist/prop-rewrite.d.ts +18 -0
- package/dist/prop-rewrite.d.ts.map +1 -1
- package/dist/props-binding.d.ts +43 -6
- package/dist/props-binding.d.ts.map +1 -1
- package/dist/relocate.d.ts +16 -0
- package/dist/relocate.d.ts.map +1 -1
- package/dist/scanner/js-scanner.d.ts.map +1 -1
- package/dist/scanner/js-scanner.js +30 -2
- package/dist/signal-prop-name-collision.d.ts +6 -5
- package/dist/signal-prop-name-collision.d.ts.map +1 -1
- package/dist/ssr-defaults.d.ts +28 -0
- package/dist/ssr-defaults.d.ts.map +1 -1
- package/dist/ssr-seed-plan.d.ts +37 -18
- package/dist/ssr-seed-plan.d.ts.map +1 -1
- package/dist/types.d.ts +162 -20
- package/dist/types.d.ts.map +1 -1
- package/dist/value-references.d.ts +21 -0
- package/dist/value-references.d.ts.map +1 -1
- package/package.json +2 -2
- package/src/__tests__/__snapshots__/doc-examples.test.ts.snap +18 -18
- package/src/__tests__/adapter-output.test.ts +5 -3
- package/src/__tests__/analyzer.test.ts +33 -0
- package/src/__tests__/arithmetic-lowering.test.ts +44 -0
- package/src/__tests__/block-memo-type.test.ts +51 -0
- package/src/__tests__/callback-method.test.ts +75 -0
- package/src/__tests__/children-read.test.ts +226 -0
- package/src/__tests__/client-only-date-lowering.test.ts +2 -2
- package/src/__tests__/client-template-escape-soundness.test.ts +31 -1
- package/src/__tests__/component-props.test.ts +68 -1
- package/src/__tests__/computed-const.test.ts +101 -0
- package/src/__tests__/constant-inlining-tree.test.ts +136 -0
- package/src/__tests__/destructure-default-reads-earlier-param.test.ts +138 -0
- package/src/__tests__/dsl-template-adapter.test.ts +173 -0
- package/src/__tests__/element-shell.test.ts +78 -0
- package/src/__tests__/event-handler-verbatim.test.ts +73 -0
- package/src/__tests__/expression-parser.test.ts +1 -44
- package/src/__tests__/fold-render-reassignments.test.ts +96 -0
- package/src/__tests__/inlinable-local-method-call.test.ts +81 -0
- package/src/__tests__/memo-body-expr.test.ts +120 -0
- package/src/__tests__/nullable-signal-attr.test.ts +67 -3
- package/src/__tests__/prop-handling.test.ts +35 -16
- package/src/__tests__/props-alias-const.test.ts +65 -0
- package/src/__tests__/reference-graph-identifiers.test.ts +161 -0
- package/src/__tests__/runtime-import-detection.test.ts +134 -0
- package/src/__tests__/signal-prop-name-collision.test.ts +30 -0
- package/src/__tests__/signal-prop-seed.test.ts +90 -0
- package/src/__tests__/ssr-defaults.test.ts +19 -1
- package/src/__tests__/ssr-seed-plan.test.ts +173 -1
- package/src/__tests__/static-template-reachability.test.ts +137 -0
- package/src/__tests__/style-object-plan.test.ts +54 -0
- package/src/__tests__/zero-param-helper-inline.test.ts +57 -0
- package/src/adapters/attr-plan.ts +29 -1
- package/src/adapters/callback-method.ts +85 -0
- package/src/adapters/component-props.ts +130 -12
- package/src/adapters/computed-const.ts +185 -0
- package/src/adapters/dangerous-inner-html.ts +33 -0
- package/src/adapters/dsl-template-adapter.ts +248 -0
- package/src/adapters/element-shell.ts +160 -0
- package/src/adapters/interface.ts +8 -0
- package/src/adapters/jsx-adapter.ts +12 -6
- package/src/adapters/literal-value.ts +2 -2
- package/src/adapters/parsed-expr-emitter.ts +34 -0
- package/src/adapters/style-object.ts +61 -0
- package/src/analyzer.ts +382 -94
- package/src/augment-inherited-props.ts +12 -0
- package/src/children-nullish.ts +33 -0
- package/src/debug.ts +2 -2
- package/src/errors.ts +12 -0
- package/src/expression-parser.ts +56 -53
- package/src/fold-render-reassignments.ts +314 -0
- package/src/free-refs.ts +77 -17
- package/src/index.ts +27 -10
- package/src/ir-to-client-js/compute-inlinability.ts +122 -19
- package/src/ir-to-client-js/control-flow/plan/event-collision.ts +2 -1
- package/src/ir-to-client-js/control-flow/plan/insert.ts +2 -2
- package/src/ir-to-client-js/control-flow/stringify/event-delegation.ts +3 -2
- package/src/ir-to-client-js/control-flow/stringify/event-listener.ts +13 -12
- package/src/ir-to-client-js/csr-substitute.ts +25 -37
- package/src/ir-to-client-js/emit-reactive.ts +2 -2
- package/src/ir-to-client-js/emit-registration.ts +31 -59
- package/src/ir-to-client-js/generate-init.ts +17 -1
- package/src/ir-to-client-js/html-template.ts +56 -120
- package/src/ir-to-client-js/identifiers.ts +75 -24
- package/src/ir-to-client-js/imports.ts +56 -19
- package/src/ir-to-client-js/index.ts +5 -3
- package/src/ir-to-client-js/init-declarations.ts +1 -1
- package/src/ir-to-client-js/phases/event-handlers.ts +2 -2
- package/src/ir-to-client-js/phases/props-extraction.ts +16 -15
- package/src/ir-to-client-js/plan/build-declaration-emit.ts +26 -52
- package/src/ir-to-client-js/plan/declaration-emit.ts +5 -0
- package/src/ir-to-client-js/prop-handling.ts +13 -54
- package/src/ir-to-client-js/prune-unused-prop-extractions.ts +7 -4
- package/src/ir-to-client-js/reactivity.ts +2 -2
- package/src/ir-to-client-js/rewrite-destructured-props.ts +27 -9
- package/src/ir-to-client-js/safe-html.ts +29 -35
- package/src/ir-to-client-js/stringify/declaration-emit.ts +1 -1
- package/src/ir-to-client-js/types.ts +3 -2
- package/src/ir-to-client-js/utils.ts +13 -26
- package/src/jsx-to-ir.ts +318 -130
- package/src/nullable-signal-attr.ts +156 -37
- package/src/prop-rewrite.ts +41 -0
- package/src/props-binding.ts +117 -7
- package/src/relocate.ts +40 -8
- package/src/scanner/__tests__/js-scanner.test.ts +37 -0
- package/src/scanner/js-scanner.ts +64 -4
- package/src/signal-prop-name-collision.ts +50 -24
- package/src/ssr-defaults.ts +47 -20
- package/src/ssr-seed-plan.ts +121 -29
- package/src/types.ts +165 -21
- package/src/value-references.ts +132 -0
package/src/analyzer.ts
CHANGED
|
@@ -7,12 +7,13 @@
|
|
|
7
7
|
*/
|
|
8
8
|
|
|
9
9
|
import ts from 'typescript'
|
|
10
|
+
import { foldRenderReassignmentsInSource } from './fold-render-reassignments.ts'
|
|
10
11
|
import { CLIENT_DIRECTIVE_INTERIOR_RE, USE_CLIENT_DIRECTIVE, isUseClientDirectiveText } from './directives.ts'
|
|
11
|
-
import type { ImportSpecifier, TypeInfo, ParamInfo, ReactiveFactoryInfo, DeclinedReactiveFactory, RequiredFactoryImport, FactoryRenameSite, SourceLocation, SignalFactoryCall } from './types.ts'
|
|
12
|
-
import { parseExpression, parseBlockBodyTolerant, foldBlockToExpr, tsNodeToParsedExpr } from './expression-parser.ts'
|
|
12
|
+
import type { ImportSpecifier, TypeInfo, ParamInfo, PropDefaultPart, PropDefaultRead, ReactiveFactoryInfo, DeclinedReactiveFactory, RequiredFactoryImport, FactoryRenameSite, SourceLocation, SignalFactoryCall, MemoInfo } from './types.ts'
|
|
13
|
+
import { parseExpression, parseBlockBodyTolerant, foldBlockToExpr, tsNodeToParsedExpr, stringifyParsedExpr, type ParsedExpr } from './expression-parser.ts'
|
|
13
14
|
import type { CallbackBodyAcceptor } from './adapters/interface.ts'
|
|
14
|
-
import { rewriteBarePropRefs } from './prop-rewrite.ts'
|
|
15
|
-
import { buildPropAliasMap, coalesceDefaultText } from './props-binding.ts'
|
|
15
|
+
import { rewriteBarePropRefs, splitScopedValueRefs } from './prop-rewrite.ts'
|
|
16
|
+
import { buildPropAliasMap, coalesceDefaultText, livePropReadExpr, signalPropSeed } from './props-binding.ts'
|
|
16
17
|
import { incrementCounter } from './instrumentation.ts'
|
|
17
18
|
import {
|
|
18
19
|
type AnalyzerContext,
|
|
@@ -32,7 +33,7 @@ import { CATALOGUED_RICH_TYPE_NAMES } from './date-lowering.ts'
|
|
|
32
33
|
import path from 'node:path'
|
|
33
34
|
import fs from 'node:fs'
|
|
34
35
|
import { signalSecondBinding } from './signal-initializer.ts'
|
|
35
|
-
import {
|
|
36
|
+
import { separateSignalPropBindings, programWithSeparatedSource } from './signal-prop-name-collision.ts'
|
|
36
37
|
|
|
37
38
|
// =============================================================================
|
|
38
39
|
// TypeScript Program Creation
|
|
@@ -200,6 +201,22 @@ export function createProgramForFile(
|
|
|
200
201
|
}
|
|
201
202
|
}
|
|
202
203
|
|
|
204
|
+
/**
|
|
205
|
+
* The source the analyzer reads after its pre-passes, or `null` when none
|
|
206
|
+
* applies: same-file reactive factory calls inlined (#931), then each
|
|
207
|
+
* `let`'s render-time reassignments folded into its initializer so every
|
|
208
|
+
* stage reads one value for it (#3501).
|
|
209
|
+
*/
|
|
210
|
+
function rewriteSourceForAnalysis(
|
|
211
|
+
source: string,
|
|
212
|
+
filePath: string,
|
|
213
|
+
prescan: ReturnType<typeof prescanReactiveFactoriesInSource>,
|
|
214
|
+
): string | null {
|
|
215
|
+
const inlined = prescan.factories.size > 0 ? rewriteFactoryCallsInSource(source, prescan) : null
|
|
216
|
+
const base = inlined ?? source
|
|
217
|
+
return foldRenderReassignmentsInSource(base, filePath) ?? inlined
|
|
218
|
+
}
|
|
219
|
+
|
|
203
220
|
/**
|
|
204
221
|
* The Program to pass as `compileJSX`'s `program` when a test harness
|
|
205
222
|
* compiles `source` under the relative virtual `filename`, with type
|
|
@@ -220,6 +237,18 @@ export function createAnchoredProgram(source: string, filename: string, anchorDi
|
|
|
220
237
|
return createProgramForFile(source, filename, { currentDirectory: anchorDir })?.program
|
|
221
238
|
}
|
|
222
239
|
|
|
240
|
+
/**
|
|
241
|
+
* Parse a signal's initializer into the IR, and record which prop it seeds
|
|
242
|
+
* from and with what fallback (#3508).
|
|
243
|
+
*/
|
|
244
|
+
function parseSignalInitializer(signal: AnalyzerContext['signals'][number], ctx: AnalyzerContext): void {
|
|
245
|
+
if (!signal.initialValue) return
|
|
246
|
+
const parsed = parseExpression(`(${signal.initialValue})`)
|
|
247
|
+
if (parsed.kind !== 'unsupported') signal.parsed = parsed
|
|
248
|
+
const seed = signal.isModule ? null : signalPropSeed(signal.parsed, ctx.propsParams, ctx.propsObjectName)
|
|
249
|
+
if (seed) signal.propSeed = seed
|
|
250
|
+
}
|
|
251
|
+
|
|
223
252
|
// =============================================================================
|
|
224
253
|
// Main Entry Point
|
|
225
254
|
// =============================================================================
|
|
@@ -255,9 +284,7 @@ export function analyzeComponent(
|
|
|
255
284
|
// no recognisable factories are present, so it is a no-op for the
|
|
256
285
|
// common case.
|
|
257
286
|
const prescan = prescanReactiveFactoriesInSource(source, filePath)
|
|
258
|
-
const rewritten =
|
|
259
|
-
? rewriteFactoryCallsInSource(source, prescan)
|
|
260
|
-
: null
|
|
287
|
+
const rewritten = rewriteSourceForAnalysis(source, filePath, prescan)
|
|
261
288
|
if (rewritten) {
|
|
262
289
|
source = rewritten
|
|
263
290
|
// The pre-built ts.Program (if any) references the original source,
|
|
@@ -370,13 +397,9 @@ export function analyzeComponent(
|
|
|
370
397
|
// scalars / prop refs are unchanged. An unsupported shape leaves `parsed`
|
|
371
398
|
// undefined and the adapter falls back. Runs after `scanImportedClientSignals`
|
|
372
399
|
// so imported signals are covered too.
|
|
373
|
-
for (const signal of ctx.signals)
|
|
374
|
-
if (!signal.initialValue) continue
|
|
375
|
-
const parsed = parseExpression(`(${signal.initialValue})`)
|
|
376
|
-
if (parsed.kind !== 'unsupported') signal.parsed = parsed
|
|
377
|
-
}
|
|
400
|
+
for (const signal of ctx.signals) parseSignalInitializer(signal, ctx)
|
|
378
401
|
|
|
379
|
-
const separatedSource =
|
|
402
|
+
const separatedSource = separateSignalPropBindings(ctx, isNonReferenceIdentifierPosition)
|
|
380
403
|
if (separatedSource !== null) {
|
|
381
404
|
// The checker supplied by the caller names the old AST. Re-analyze the
|
|
382
405
|
// structurally renamed source, preserving the shared-program contract
|
|
@@ -394,14 +417,20 @@ export function analyzeComponent(
|
|
|
394
417
|
// parser dropped a statement it couldn't represent) or one that doesn't fold
|
|
395
418
|
// (imperative residue) leaves `parsed` undefined and consumers keep their
|
|
396
419
|
// existing `parsedBlock` fallback.
|
|
420
|
+
foldBlockMemos(ctx)
|
|
421
|
+
|
|
422
|
+
return ctx
|
|
423
|
+
}
|
|
424
|
+
|
|
425
|
+
function foldBlockMemos(ctx: AnalyzerContext): void {
|
|
397
426
|
const reactiveGetterNames = collectReactiveGetterNames(ctx.signals, ctx.memos)
|
|
398
427
|
for (const memo of ctx.memos) {
|
|
399
|
-
if (memo.parsed || !memo.parsedBlock || !memo.parsedBlockComplete) continue
|
|
428
|
+
if (memo.parsed || !memo.parsedBlock || !memo.parsedBlockComplete || clientDirectiveMemos.has(memo)) continue
|
|
400
429
|
const folded = foldBlockToExpr(memo.parsedBlock, { pureCallNames: reactiveGetterNames })
|
|
401
|
-
if (folded.ok)
|
|
430
|
+
if (!folded.ok) continue
|
|
431
|
+
memo.parsed = folded.expr
|
|
432
|
+
if (blockTextTypedMemos.has(memo)) memo.type = foldType(folded.expr)
|
|
402
433
|
}
|
|
403
|
-
|
|
404
|
-
return ctx
|
|
405
434
|
}
|
|
406
435
|
|
|
407
436
|
/**
|
|
@@ -473,10 +502,13 @@ function collectNamedExports(sourceFile: ts.SourceFile): Set<string> {
|
|
|
473
502
|
* declarations of a module-level `const f = (…) => { … }` were collected
|
|
474
503
|
* as module-level declarations of the FILE — hoisted into the init body
|
|
475
504
|
* and into the marked template, where their references to the helper's
|
|
476
|
-
* own parameters are unbound (#2986).
|
|
505
|
+
* own parameters are unbound (#2986). A class static block is its own scope
|
|
506
|
+
* too: `class { static { var $ } }` declares `$` in the block, not in the
|
|
507
|
+
* component body.
|
|
477
508
|
*/
|
|
478
509
|
function isFunctionScope(node: ts.Node): boolean {
|
|
479
|
-
return ts.
|
|
510
|
+
return ts.isClassStaticBlockDeclaration(node)
|
|
511
|
+
|| ts.isArrowFunction(node)
|
|
480
512
|
|| ts.isFunctionExpression(node)
|
|
481
513
|
|| ts.isFunctionDeclaration(node)
|
|
482
514
|
|| ts.isMethodDeclaration(node)
|
|
@@ -1506,7 +1538,7 @@ function collectSignal(node: ts.VariableDeclaration, ctx: AnalyzerContext): void
|
|
|
1506
1538
|
const propNames = new Set(ctx.propsParams.map(p => p.name))
|
|
1507
1539
|
if (propNames.size > 0) {
|
|
1508
1540
|
const propAliases = buildPropAliasMap(ctx.propsParams)
|
|
1509
|
-
templateInitialValue = rewriteBarePropRefs(initialValue, callExpr.arguments[0], propNames, undefined, propAliases)
|
|
1541
|
+
templateInitialValue = rewriteBarePropRefs(initialValue, callExpr.arguments[0], propNames, undefined, propAliases, destructuredPropTemplateRead(ctx))
|
|
1510
1542
|
}
|
|
1511
1543
|
}
|
|
1512
1544
|
|
|
@@ -1583,6 +1615,7 @@ function collectFactorySignal(
|
|
|
1583
1615
|
: new Set<string>()
|
|
1584
1616
|
|
|
1585
1617
|
let type: TypeInfo = { kind: 'unknown', raw: 'unknown' }
|
|
1618
|
+
let blockTyped = false
|
|
1586
1619
|
if (callExpr.typeArguments && callExpr.typeArguments.length > 0) {
|
|
1587
1620
|
type = typeNodeToTypeInfo(callExpr.typeArguments[0], ctx.sourceFile) ?? type
|
|
1588
1621
|
} else if (initialNode.kind !== 'absent') {
|
|
@@ -1597,7 +1630,7 @@ function collectFactorySignal(
|
|
|
1597
1630
|
const propNames = new Set(ctx.propsParams.map(p => p.name))
|
|
1598
1631
|
if (propNames.size > 0) {
|
|
1599
1632
|
const propAliases = buildPropAliasMap(ctx.propsParams)
|
|
1600
|
-
templateInitialValue = rewriteBarePropRefs(initialValue, initialNode.node, propNames, undefined, propAliases)
|
|
1633
|
+
templateInitialValue = rewriteBarePropRefs(initialValue, initialNode.node, propNames, undefined, propAliases, destructuredPropTemplateRead(ctx))
|
|
1601
1634
|
}
|
|
1602
1635
|
}
|
|
1603
1636
|
|
|
@@ -1890,7 +1923,7 @@ function isMemoDeclaration(node: ts.VariableDeclaration, ctx: AnalyzerContext):
|
|
|
1890
1923
|
function memoBodyIsTemplateLiteral(memoArrow: ts.Expression | undefined): boolean {
|
|
1891
1924
|
let node: ts.Node | undefined = memoArrow
|
|
1892
1925
|
while (node && ts.isParenthesizedExpression(node)) node = node.expression
|
|
1893
|
-
if (!node || !ts.isArrowFunction(node)) return false
|
|
1926
|
+
if (!node || (!ts.isArrowFunction(node) && !ts.isFunctionExpression(node))) return false
|
|
1894
1927
|
let body: ts.Node = node.body
|
|
1895
1928
|
while (ts.isParenthesizedExpression(body)) body = body.expression
|
|
1896
1929
|
if (ts.isBlock(body)) {
|
|
@@ -1902,68 +1935,106 @@ function memoBodyIsTemplateLiteral(memoArrow: ts.Expression | undefined): boolea
|
|
|
1902
1935
|
return ts.isTemplateExpression(body) || ts.isNoSubstitutionTemplateLiteral(body)
|
|
1903
1936
|
}
|
|
1904
1937
|
|
|
1905
|
-
|
|
1906
|
-
|
|
1907
|
-
|
|
1908
|
-
|
|
1909
|
-
|
|
1910
|
-
|
|
1911
|
-
|
|
1912
|
-
|
|
1913
|
-
|
|
1914
|
-
|
|
1915
|
-
|
|
1916
|
-
|
|
1917
|
-
|
|
1918
|
-
|
|
1919
|
-
|
|
1920
|
-
|
|
1921
|
-
|
|
1922
|
-
|
|
1923
|
-
|
|
1924
|
-
|
|
1925
|
-
//
|
|
1926
|
-
|
|
1927
|
-
|
|
1928
|
-
|
|
1929
|
-
|
|
1930
|
-
|
|
1938
|
+
/**
|
|
1939
|
+
* Whether a callback's body means the same outside the callback. An arrow
|
|
1940
|
+
* binds nothing of its own. A `function` expression binds its name (when it
|
|
1941
|
+
* has one), `arguments` and `this`, so its body only means the same when it
|
|
1942
|
+
* is anonymous and reads neither; the bodies of nested non-arrow functions,
|
|
1943
|
+
* methods and class members bind their own and are not searched, but their
|
|
1944
|
+
* computed names and a class's `extends` clause are.
|
|
1945
|
+
*/
|
|
1946
|
+
function isFunctionScopeFree(fn: ts.ArrowFunction | ts.FunctionExpression): boolean {
|
|
1947
|
+
if (ts.isArrowFunction(fn)) return true
|
|
1948
|
+
if (fn.name) return false
|
|
1949
|
+
let bound = false
|
|
1950
|
+
const visit = (node: ts.Node): void => {
|
|
1951
|
+
if (bound) return
|
|
1952
|
+
if (node.kind === ts.SyntaxKind.ThisKeyword || (ts.isIdentifier(node) && node.text === 'arguments')) {
|
|
1953
|
+
bound = true
|
|
1954
|
+
return
|
|
1955
|
+
}
|
|
1956
|
+
// A nested function or class body binds its own `this` / `arguments`,
|
|
1957
|
+
// but a computed member name and a class's `extends` clause are
|
|
1958
|
+
// evaluated in the enclosing scope, so those are still searched.
|
|
1959
|
+
if (ts.isFunctionDeclaration(node) || ts.isFunctionExpression(node)) return
|
|
1960
|
+
if (ts.isMethodDeclaration(node) || ts.isGetAccessor(node) || ts.isSetAccessor(node) ||
|
|
1961
|
+
ts.isPropertyDeclaration(node)) {
|
|
1962
|
+
if (ts.isComputedPropertyName(node.name)) visit(node.name.expression)
|
|
1963
|
+
return
|
|
1964
|
+
}
|
|
1965
|
+
if (ts.isClassLike(node)) {
|
|
1966
|
+
node.heritageClauses?.forEach(visit)
|
|
1967
|
+
node.members.forEach(visit)
|
|
1968
|
+
return
|
|
1931
1969
|
}
|
|
1970
|
+
ts.forEachChild(node, visit)
|
|
1932
1971
|
}
|
|
1972
|
+
ts.forEachChild(fn.body, visit)
|
|
1973
|
+
return !bound
|
|
1974
|
+
}
|
|
1933
1975
|
|
|
1934
|
-
|
|
1935
|
-
|
|
1936
|
-
|
|
1937
|
-
|
|
1938
|
-
|
|
1939
|
-
|
|
1940
|
-
|
|
1941
|
-
if (
|
|
1942
|
-
|
|
1943
|
-
|
|
1944
|
-
|
|
1945
|
-
|
|
1946
|
-
|
|
1947
|
-
|
|
1948
|
-
|
|
1949
|
-
|
|
1950
|
-
|
|
1951
|
-
|
|
1952
|
-
|
|
1976
|
+
/**
|
|
1977
|
+
* Whether calling the callback yields its body's value in the callback's
|
|
1978
|
+
* absence: not async (a Promise), not a generator (an iterator), and not
|
|
1979
|
+
* bound to its own scope (`isFunctionScopeFree`).
|
|
1980
|
+
*/
|
|
1981
|
+
function isValueReturningCallback(fn: ts.ArrowFunction | ts.FunctionExpression): boolean {
|
|
1982
|
+
if (ts.isFunctionExpression(fn) && fn.asteriskToken) return false
|
|
1983
|
+
if (fn.modifiers?.some(m => m.kind === ts.SyntaxKind.AsyncKeyword)) return false
|
|
1984
|
+
return isFunctionScopeFree(fn)
|
|
1985
|
+
}
|
|
1986
|
+
|
|
1987
|
+
/**
|
|
1988
|
+
* The expression a memo's callback evaluates to, as the analyzer reads it from
|
|
1989
|
+
* the callback node (#3510). A zero-parameter arrow or `function` expression
|
|
1990
|
+
* yields its body: the expression body, or the operand of a block that is a
|
|
1991
|
+
* single `return`. Any other block (and any other callback: async, generator,
|
|
1992
|
+
* a parameter, a function reference, a `function` expression whose body needs
|
|
1993
|
+
* its own scope) yields a call of the whole callback, so a consumer that
|
|
1994
|
+
* substitutes the memo's value never sees an uncalled function or a body
|
|
1995
|
+
* detached from its bindings.
|
|
1996
|
+
*/
|
|
1997
|
+
function memoBodyNode(callback: ts.Expression): { node: ts.Node; call: boolean } {
|
|
1998
|
+
let fn: ts.Expression = callback
|
|
1999
|
+
while (ts.isParenthesizedExpression(fn)) fn = fn.expression
|
|
2000
|
+
if (!ts.isArrowFunction(fn) && !ts.isFunctionExpression(fn)) return { node: callback, call: true }
|
|
2001
|
+
if (fn.parameters.length > 0 || !isValueReturningCallback(fn)) return { node: callback, call: true }
|
|
2002
|
+
if (!ts.isBlock(fn.body)) return { node: fn.body, call: false }
|
|
2003
|
+
const [only] = fn.body.statements
|
|
2004
|
+
if (fn.body.statements.length === 1 && ts.isReturnStatement(only) && only.expression) {
|
|
2005
|
+
return { node: only.expression, call: false }
|
|
1953
2006
|
}
|
|
2007
|
+
return { node: callback, call: true }
|
|
2008
|
+
}
|
|
1954
2009
|
|
|
2010
|
+
/**
|
|
2011
|
+
* The memo body as structured trees (#2040, #3510): `parsed` for an
|
|
2012
|
+
* expression body, `parsedBlock` for a block body. See the field docs on
|
|
2013
|
+
* `MemoInfo`.
|
|
2014
|
+
*/
|
|
2015
|
+
function memoStructuredBody(
|
|
2016
|
+
callback: ts.Expression | undefined,
|
|
2017
|
+
ctx: AnalyzerContext,
|
|
2018
|
+
): Pick<MemoInfo, 'parsed' | 'parsedBlock' | 'parsedBlockComplete'> {
|
|
1955
2019
|
// Structured parse of the arrow BODY, so adapters can shape-match the memo
|
|
1956
2020
|
// on a tree instead of re-parsing `computation`. Parse from the type-STRIPPED
|
|
1957
2021
|
// body (`ctx.getJS`, same source as `computation`) — `getText` would keep
|
|
1958
2022
|
// TypeScript-only syntax (`as T`, `!`, `satisfies`) that `parseExpression`
|
|
1959
2023
|
// rejects, leaving `parsed` undefined for typed bodies that the stripped
|
|
1960
|
-
// `computation` would match. Expression
|
|
1961
|
-
// (`() => { … }`) and unsupported shapes leave
|
|
1962
|
-
//
|
|
1963
|
-
|
|
2024
|
+
// `computation` would match. Expression bodies only — block bodies
|
|
2025
|
+
// (`() => { … }`, `function () { … }`) and unsupported shapes leave
|
|
2026
|
+
// `parsed` undefined here (the block fold sets it later). Parens around the
|
|
2027
|
+
// callback are unwrapped (#3510).
|
|
2028
|
+
let memoFn: ts.Node | undefined = callback
|
|
2029
|
+
while (memoFn && ts.isParenthesizedExpression(memoFn)) memoFn = memoFn.expression
|
|
2030
|
+
// A parameter binds names the body reads (`(label = 'inner') => label`),
|
|
2031
|
+
// which a tree of the body alone would read as component scope (#3523).
|
|
2032
|
+
if (memoFn && ((!ts.isArrowFunction(memoFn) && !ts.isFunctionExpression(memoFn)) || memoFn.parameters.length > 0 || !isValueReturningCallback(memoFn))) {
|
|
2033
|
+
memoFn = undefined
|
|
2034
|
+
}
|
|
1964
2035
|
const parsedBody =
|
|
1965
|
-
|
|
1966
|
-
? parseExpression(ctx.getJS(
|
|
2036
|
+
memoFn && !ts.isBlock(memoFn.body)
|
|
2037
|
+
? parseExpression(ctx.getJS(memoFn.body))
|
|
1967
2038
|
: undefined
|
|
1968
2039
|
// `object-literal` is excluded alongside `unsupported`: an object-returning
|
|
1969
2040
|
// memo (`() => ({ … })`) isn't lowered from the parsed tree yet, so leaving
|
|
@@ -1976,14 +2047,8 @@ function collectMemo(node: ts.VariableDeclaration, ctx: AnalyzerContext): void {
|
|
|
1976
2047
|
|
|
1977
2048
|
// Block-bodied memos: carry the statements (tolerant — unparseable ones are
|
|
1978
2049
|
// omitted) so adapters can pattern-match block shapes (e.g. a guard-and-
|
|
1979
|
-
// return-const memo) without re-parsing `computation`.
|
|
1980
|
-
|
|
1981
|
-
let arrowNode: ts.Node | undefined = memoArrow
|
|
1982
|
-
while (arrowNode && ts.isParenthesizedExpression(arrowNode)) arrowNode = arrowNode.expression
|
|
1983
|
-
const blockBody =
|
|
1984
|
-
arrowNode && ts.isArrowFunction(arrowNode) && ts.isBlock(arrowNode.body)
|
|
1985
|
-
? arrowNode.body
|
|
1986
|
-
: undefined
|
|
2050
|
+
// return-const memo) without re-parsing `computation`.
|
|
2051
|
+
const blockBody = memoFn && ts.isBlock(memoFn.body) ? memoFn.body : undefined
|
|
1987
2052
|
const parsedBlock = blockBody
|
|
1988
2053
|
? parseBlockBodyTolerant(blockBody, ctx.sourceFile, node => ctx.getJS(node))
|
|
1989
2054
|
: undefined
|
|
@@ -1992,7 +2057,17 @@ function collectMemo(node: ts.VariableDeclaration, ctx: AnalyzerContext): void {
|
|
|
1992
2057
|
// represented — nothing silently omitted (see `MemoInfo.parsedBlockComplete`).
|
|
1993
2058
|
const parsedBlockComplete =
|
|
1994
2059
|
parsedBlock && blockBody ? parsedBlock.length === blockBody.statements.length : undefined
|
|
2060
|
+
return { parsed, parsedBlock, parsedBlockComplete }
|
|
2061
|
+
}
|
|
1995
2062
|
|
|
2063
|
+
/**
|
|
2064
|
+
* `MemoInfo.bodyExpr` / `bodyExprIsCall` / `templateBodyExpr` from the
|
|
2065
|
+
* callback's body node (#3510).
|
|
2066
|
+
*/
|
|
2067
|
+
function memoBodyFields(
|
|
2068
|
+
body: { node: ts.Node; call: boolean },
|
|
2069
|
+
ctx: AnalyzerContext,
|
|
2070
|
+
): Pick<MemoInfo, 'bodyExpr' | 'bodyExprIsCall' | 'templateBodyExpr'> {
|
|
1996
2071
|
// Destructured-arg components only — the memo twin of the signal
|
|
1997
2072
|
// `templateInitialValue` rewrite (#2265) a few hundred lines up: a memo
|
|
1998
2073
|
// body referencing a bare destructured prop (`createMemo(() => value *
|
|
@@ -2001,33 +2076,149 @@ function collectMemo(node: ts.VariableDeclaration, ctx: AnalyzerContext): void {
|
|
|
2001
2076
|
// module scope — it is not a closure over `initXxx`'s `const value =
|
|
2002
2077
|
// _p.value` extraction, so the bare ref must become `_p.value`
|
|
2003
2078
|
// (adapter-tests scope-gate, #2468 sweep).
|
|
2004
|
-
|
|
2005
|
-
|
|
2079
|
+
const asBodyExpr = (text: string): string => (body.call ? `(${text})()` : text)
|
|
2080
|
+
const bodyExpr = asBodyExpr(ctx.getJS(body.node))
|
|
2081
|
+
let templateBodyExpr: string | undefined
|
|
2082
|
+
if (!ctx.propsObjectName) {
|
|
2006
2083
|
const propNames = new Set(ctx.propsParams.map(p => p.name))
|
|
2007
2084
|
if (propNames.size > 0) {
|
|
2008
2085
|
const propAliases = buildPropAliasMap(ctx.propsParams)
|
|
2009
|
-
|
|
2086
|
+
const rewritten = rewriteBarePropRefs(ctx.getJS(body.node), body.node, propNames, undefined, propAliases, destructuredPropTemplateRead(ctx))
|
|
2087
|
+
templateBodyExpr = rewritten === undefined ? undefined : asBodyExpr(rewritten)
|
|
2088
|
+
}
|
|
2089
|
+
}
|
|
2090
|
+
return { bodyExpr, bodyExprIsCall: body.call || undefined, templateBodyExpr }
|
|
2091
|
+
}
|
|
2092
|
+
|
|
2093
|
+
function collectMemo(node: ts.VariableDeclaration, ctx: AnalyzerContext): void {
|
|
2094
|
+
const name = (node.name as ts.Identifier).text
|
|
2095
|
+
const callExpr = node.initializer as ts.CallExpression
|
|
2096
|
+
const computation = callExpr.arguments[0] ? ctx.getJS(callExpr.arguments[0]) : ''
|
|
2097
|
+
const typedComputation = callExpr.arguments[0] ? callExpr.arguments[0].getText(ctx.sourceFile) : undefined
|
|
2098
|
+
const body = callExpr.arguments[0] ? memoBodyNode(callExpr.arguments[0]) : undefined
|
|
2099
|
+
|
|
2100
|
+
// Extract dependencies from computation
|
|
2101
|
+
const deps = extractDependencies(computation, ctx)
|
|
2102
|
+
|
|
2103
|
+
// Try to infer type. Prefer the explicit `<T>` type argument when
|
|
2104
|
+
// present; otherwise fall back to the same value-shape inference
|
|
2105
|
+
// signals use (`inferTypeFromValue` handles `.length`, `.some(...)`,
|
|
2106
|
+
// `.every(...)`, etc.). Without this, `createMemo(() =>
|
|
2107
|
+
// todos().filter(...).length)` ends up as `unknown` and the Go
|
|
2108
|
+
// adapter renders the field as `interface{}`, even though the
|
|
2109
|
+
// expression is clearly a number.
|
|
2110
|
+
let type: TypeInfo = { kind: 'unknown', raw: 'unknown' }
|
|
2111
|
+
let blockTyped = false
|
|
2112
|
+
if (callExpr.typeArguments && callExpr.typeArguments.length > 0) {
|
|
2113
|
+
type = typeNodeToTypeInfo(callExpr.typeArguments[0], ctx.sourceFile) ?? type
|
|
2114
|
+
} else {
|
|
2115
|
+
// The memo computation is a function; the type we want is the body's,
|
|
2116
|
+
// not the function itself, so `inferTypeFromValue` sees the body text
|
|
2117
|
+
// (a block body reads as `object`, which the checker upgrade below
|
|
2118
|
+
// refines).
|
|
2119
|
+
const fn = callExpr.arguments[0]
|
|
2120
|
+
if (fn && (ts.isArrowFunction(fn) || ts.isFunctionExpression(fn))) {
|
|
2121
|
+
type = inferTypeFromValue(ctx.getJS(fn.body).trim())
|
|
2122
|
+
blockTyped = ts.isBlock(fn.body)
|
|
2010
2123
|
}
|
|
2011
2124
|
}
|
|
2012
2125
|
|
|
2013
|
-
|
|
2126
|
+
// When the syntactic heuristic above can't resolve a precise type
|
|
2127
|
+
// (`object`/`unknown` — e.g. a local-function call or a ternary of typed
|
|
2128
|
+
// arrays), ask the type checker for the memo body's actual type. This is
|
|
2129
|
+
// what lets the Go adapter emit `[][]CalendarDay` / `[]string` / `bool`
|
|
2130
|
+
// instead of `map[string]interface{}` / `bool` placeholders (#1968), so a
|
|
2131
|
+
// typed backend can populate the SSR data. Only upgrades imprecise results —
|
|
2132
|
+
// already-precise syntactic types are left untouched.
|
|
2133
|
+
if (
|
|
2134
|
+
ctx.checker &&
|
|
2135
|
+
(type.kind === 'unknown' || type.kind === 'object') &&
|
|
2136
|
+
callExpr.arguments[0] &&
|
|
2137
|
+
(ts.isArrowFunction(callExpr.arguments[0]) || ts.isFunctionExpression(callExpr.arguments[0]))
|
|
2138
|
+
) {
|
|
2139
|
+
const fnType = ctx.checker.getTypeAtLocation(callExpr.arguments[0])
|
|
2140
|
+
const sig = fnType.getCallSignatures()[0]
|
|
2141
|
+
if (sig) {
|
|
2142
|
+
const inferred = tsTypeToTypeInfo(ctx.checker.getReturnTypeOfSignature(sig), ctx.checker)
|
|
2143
|
+
if (inferred && inferred.kind !== 'unknown') {
|
|
2144
|
+
type = inferred
|
|
2145
|
+
blockTyped = false
|
|
2146
|
+
}
|
|
2147
|
+
}
|
|
2148
|
+
}
|
|
2149
|
+
|
|
2150
|
+
const { parsed, parsedBlock, parsedBlockComplete } = memoStructuredBody(callExpr.arguments[0], ctx)
|
|
2151
|
+
|
|
2152
|
+
const bodyFields = body ? memoBodyFields(body, ctx) : {}
|
|
2153
|
+
|
|
2154
|
+
const memo: MemoInfo = {
|
|
2014
2155
|
name,
|
|
2015
2156
|
computation,
|
|
2016
|
-
|
|
2157
|
+
...bodyFields,
|
|
2017
2158
|
parsedBlock,
|
|
2018
2159
|
parsedBlockComplete,
|
|
2019
2160
|
typedComputation: typedComputation !== computation ? typedComputation : undefined,
|
|
2020
2161
|
parsed,
|
|
2021
|
-
bodyIsTemplateLiteral: memoBodyIsTemplateLiteral(
|
|
2162
|
+
bodyIsTemplateLiteral: memoBodyIsTemplateLiteral(callExpr.arguments[0]),
|
|
2022
2163
|
type,
|
|
2023
2164
|
deps,
|
|
2024
2165
|
loc: getSourceLocation(node, ctx.sourceFile, ctx.filePath),
|
|
2025
2166
|
computationFreeIdentifiers: callExpr.arguments[0]
|
|
2026
2167
|
? extractFreeIdentifiersFromNode(callExpr.arguments[0])
|
|
2027
2168
|
: new Set(),
|
|
2028
|
-
}
|
|
2169
|
+
}
|
|
2170
|
+
ctx.memos.push(memo)
|
|
2171
|
+
markBlockMemo(memo, blockTyped, body?.node, ctx)
|
|
2172
|
+
}
|
|
2173
|
+
|
|
2174
|
+
/** Record what `foldBlockMemos` needs to know about a block memo (#3523). */
|
|
2175
|
+
function markBlockMemo(memo: MemoInfo, blockTyped: boolean, bodyNode: ts.Node | undefined, ctx: AnalyzerContext): void {
|
|
2176
|
+
if (blockTyped) blockTextTypedMemos.add(memo)
|
|
2177
|
+
if (bodyNode && containsClientDirective(bodyNode, ctx.sourceFile)) clientDirectiveMemos.add(memo)
|
|
2029
2178
|
}
|
|
2030
2179
|
|
|
2180
|
+
/**
|
|
2181
|
+
* Whether a `/* @client */` directive comment precedes any node inside
|
|
2182
|
+
* `root` (`return /* @client */ d.toLocaleDateString(…)`). A block memo
|
|
2183
|
+
* carrying one is left unfolded, so it stays client-only (#3523).
|
|
2184
|
+
*/
|
|
2185
|
+
function containsClientDirective(root: ts.Node, sourceFile: ts.SourceFile): boolean {
|
|
2186
|
+
let found = false
|
|
2187
|
+
const visit = (node: ts.Node): void => {
|
|
2188
|
+
if (found) return
|
|
2189
|
+
const trivia = sourceFile.text.slice(node.pos, node.getStart(sourceFile))
|
|
2190
|
+
BLOCK_COMMENT_RE.lastIndex = 0
|
|
2191
|
+
let m: RegExpExecArray | null
|
|
2192
|
+
while ((m = BLOCK_COMMENT_RE.exec(trivia)) !== null) {
|
|
2193
|
+
if (CLIENT_DIRECTIVE_INTERIOR_RE.test(m[1])) found = true
|
|
2194
|
+
}
|
|
2195
|
+
ts.forEachChild(node, visit)
|
|
2196
|
+
}
|
|
2197
|
+
ts.forEachChild(root, visit)
|
|
2198
|
+
return found
|
|
2199
|
+
}
|
|
2200
|
+
|
|
2201
|
+
/** Block memos that carry a `/* @client */` directive and so are not folded. */
|
|
2202
|
+
const clientDirectiveMemos = new WeakSet<MemoInfo>()
|
|
2203
|
+
|
|
2204
|
+
/**
|
|
2205
|
+
* A block memo's type from its fold (#3523): the block's text read as
|
|
2206
|
+
* `object`, but its value is the fold. A returned object literal stays
|
|
2207
|
+
* `object`. A logical fold (`k ?? false`) is `unknown`: its value can be
|
|
2208
|
+
* either side, while the text heuristic would read the fallback's type.
|
|
2209
|
+
*/
|
|
2210
|
+
function foldType(expr: ParsedExpr): TypeInfo {
|
|
2211
|
+
if (expr.kind === 'object-literal') return { kind: 'object', raw: 'object' }
|
|
2212
|
+
if (expr.kind === 'logical') return { kind: 'unknown', raw: 'unknown' }
|
|
2213
|
+
return inferTypeFromValue(stringifyParsedExpr(expr))
|
|
2214
|
+
}
|
|
2215
|
+
|
|
2216
|
+
/**
|
|
2217
|
+
* Memos whose type was read off a block body's text, where `{` reads as an
|
|
2218
|
+
* object. Their type is re-inferred from the block's fold (#3523).
|
|
2219
|
+
*/
|
|
2220
|
+
const blockTextTypedMemos = new WeakSet<MemoInfo>()
|
|
2221
|
+
|
|
2031
2222
|
// =============================================================================
|
|
2032
2223
|
// Effect Detection & Collection
|
|
2033
2224
|
// =============================================================================
|
|
@@ -2592,15 +2783,34 @@ function collectTypeAliasDefinition(
|
|
|
2592
2783
|
const properties = ts.isTypeLiteralNode(node.type)
|
|
2593
2784
|
? membersToProperties(node.type.members, ctx.sourceFile)
|
|
2594
2785
|
: undefined
|
|
2786
|
+
const stringLiteralUnion = stringLiteralMembers(node.type)
|
|
2595
2787
|
ctx.typeDefinitions.push({
|
|
2596
2788
|
kind: 'type',
|
|
2597
2789
|
name: node.name.text,
|
|
2598
2790
|
definition: node.getText(ctx.sourceFile),
|
|
2599
2791
|
properties,
|
|
2792
|
+
...(stringLiteralUnion ? { stringLiteralUnion } : {}),
|
|
2600
2793
|
loc: getSourceLocation(node, ctx.sourceFile, ctx.filePath),
|
|
2601
2794
|
})
|
|
2602
2795
|
}
|
|
2603
2796
|
|
|
2797
|
+
/**
|
|
2798
|
+
* The values of a type that is a string literal or a union of string
|
|
2799
|
+
* literals only, or `undefined` for any other type.
|
|
2800
|
+
*/
|
|
2801
|
+
function stringLiteralMembers(type: ts.TypeNode): string[] | undefined {
|
|
2802
|
+
if (ts.isParenthesizedTypeNode(type)) return stringLiteralMembers(type.type)
|
|
2803
|
+
if (ts.isLiteralTypeNode(type)) return ts.isStringLiteral(type.literal) ? [type.literal.text] : undefined
|
|
2804
|
+
if (!ts.isUnionTypeNode(type)) return undefined
|
|
2805
|
+
const values: string[] = []
|
|
2806
|
+
for (const member of type.types) {
|
|
2807
|
+
const nested = stringLiteralMembers(member)
|
|
2808
|
+
if (!nested) return undefined
|
|
2809
|
+
values.push(...nested)
|
|
2810
|
+
}
|
|
2811
|
+
return values
|
|
2812
|
+
}
|
|
2813
|
+
|
|
2604
2814
|
// =============================================================================
|
|
2605
2815
|
// Function Collection
|
|
2606
2816
|
// =============================================================================
|
|
@@ -3453,6 +3663,18 @@ function getSystemConstructKind(node: ts.Node): 'createContext' | 'weakMap' | un
|
|
|
3453
3663
|
return undefined
|
|
3454
3664
|
}
|
|
3455
3665
|
|
|
3666
|
+
/**
|
|
3667
|
+
* The CSR `template:` read of a destructured prop a signal seed or memo body
|
|
3668
|
+
* names (#2265): the live read every client site shares
|
|
3669
|
+
* (`livePropReadExpr`), so the module-scope arrow applies the prop's
|
|
3670
|
+
* destructure default too, including one that reads an earlier prop
|
|
3671
|
+
* (`{ off = false, on = off }` → `(_p.on ?? (_p.off ?? false))`, #3531).
|
|
3672
|
+
*/
|
|
3673
|
+
function destructuredPropTemplateRead(ctx: AnalyzerContext): (localName: string, callerKey: string) => string {
|
|
3674
|
+
const byName = new Map(ctx.propsParams.filter(p => !p.isRest).map(p => [p.name, p]))
|
|
3675
|
+
return (localName, callerKey) => livePropReadExpr(byName.get(localName) ?? { name: localName, sourceName: callerKey }, undefined, false)
|
|
3676
|
+
}
|
|
3677
|
+
|
|
3456
3678
|
/**
|
|
3457
3679
|
* The default-value-shaped fields a binding element's own `initializer`
|
|
3458
3680
|
* contributes to a `ParamInfo` — `defaultValue`'s source text, `parsed`'s
|
|
@@ -3466,14 +3688,70 @@ function getSystemConstructKind(node: ts.Node): 'createContext' | 'weakMap' | un
|
|
|
3466
3688
|
function bindingElementDefaultFields(
|
|
3467
3689
|
initializer: ts.Expression | undefined,
|
|
3468
3690
|
ctx: AnalyzerContext,
|
|
3469
|
-
|
|
3691
|
+
earlier: ReadonlyMap<string, PropDefaultRead> = new Map(),
|
|
3692
|
+
): Pick<ParamInfo, 'defaultValue' | 'parsed' | 'defaultContainsArrow' | 'defaultParts'> {
|
|
3470
3693
|
const defaultValue = initializer ? ctx.getJS(initializer) : undefined
|
|
3471
3694
|
const defaultContainsArrow = initializer ? nodeContainsArrow(initializer) : false
|
|
3472
3695
|
const parsedDefault = initializer ? tsNodeToParsedExpr(initializer) : undefined
|
|
3696
|
+
const defaultParts = initializer && defaultValue !== undefined
|
|
3697
|
+
? defaultPartsReadingEarlierBindings(initializer, defaultValue, earlier)
|
|
3698
|
+
: undefined
|
|
3473
3699
|
return {
|
|
3474
3700
|
defaultValue,
|
|
3475
3701
|
...(parsedDefault && parsedDefault.kind !== 'unsupported' && { parsed: parsedDefault }),
|
|
3476
3702
|
defaultContainsArrow: defaultContainsArrow || undefined,
|
|
3703
|
+
...(defaultParts && { defaultParts }),
|
|
3704
|
+
}
|
|
3705
|
+
}
|
|
3706
|
+
|
|
3707
|
+
/**
|
|
3708
|
+
* A destructure default split at its reads of EARLIER bindings of the same
|
|
3709
|
+
* pattern (`ParamInfo.defaultParts`, #3531), or undefined when it reads none.
|
|
3710
|
+
* JS puts each earlier binding in scope for a later default
|
|
3711
|
+
* (`{ off = false, on = off }`), so a free `off` there is that binding, not an
|
|
3712
|
+
* outer name; a later binding is still in its TDZ and is left alone. The split
|
|
3713
|
+
* runs on the default's JS text with the same scope-aware walk the client
|
|
3714
|
+
* prop rewrite uses, so a nested callback parameter of the same name is not a
|
|
3715
|
+
* read.
|
|
3716
|
+
*/
|
|
3717
|
+
function defaultPartsReadingEarlierBindings(
|
|
3718
|
+
initializer: ts.Expression,
|
|
3719
|
+
defaultValue: string,
|
|
3720
|
+
earlier: ReadonlyMap<string, PropDefaultRead>,
|
|
3721
|
+
): PropDefaultPart[] | undefined {
|
|
3722
|
+
if (earlier.size === 0) return undefined
|
|
3723
|
+
const names = new Set([...extractFreeIdentifiersFromNode(initializer)].filter(n => earlier.has(n)))
|
|
3724
|
+
if (names.size === 0) return undefined
|
|
3725
|
+
const pieces = splitScopedValueRefs(defaultValue, names)
|
|
3726
|
+
if (pieces === null || !pieces.some(p => p.kind === 'ref')) return undefined
|
|
3727
|
+
return pieces.map((piece): PropDefaultPart =>
|
|
3728
|
+
piece.kind === 'text'
|
|
3729
|
+
? { kind: 'text', text: piece.text }
|
|
3730
|
+
: { kind: 'param', param: earlier.get(piece.name)! },
|
|
3731
|
+
)
|
|
3732
|
+
}
|
|
3733
|
+
|
|
3734
|
+
/** {@link propDefaultReadOf} for a body destructure's binding (`const { label: text = 'x' } = props`). */
|
|
3735
|
+
function bodyBindingRead(
|
|
3736
|
+
localName: string,
|
|
3737
|
+
sourceKey: string,
|
|
3738
|
+
defaultFields: Pick<ParamInfo, 'defaultValue' | 'defaultContainsArrow' | 'defaultParts'> | undefined,
|
|
3739
|
+
): PropDefaultRead {
|
|
3740
|
+
return propDefaultReadOf({
|
|
3741
|
+
name: localName,
|
|
3742
|
+
...(sourceKey !== localName && { sourceName: sourceKey }),
|
|
3743
|
+
...defaultFields,
|
|
3744
|
+
})
|
|
3745
|
+
}
|
|
3746
|
+
|
|
3747
|
+
/** The fields a later default's `param` part needs to read `p` (`PropDefaultRead`). */
|
|
3748
|
+
function propDefaultReadOf(p: PropDefaultRead): PropDefaultRead {
|
|
3749
|
+
return {
|
|
3750
|
+
name: p.name,
|
|
3751
|
+
...(p.sourceName !== undefined && { sourceName: p.sourceName }),
|
|
3752
|
+
...(p.defaultValue !== undefined && { defaultValue: p.defaultValue }),
|
|
3753
|
+
...(p.defaultContainsArrow && { defaultContainsArrow: p.defaultContainsArrow }),
|
|
3754
|
+
...(p.defaultParts && { defaultParts: p.defaultParts }),
|
|
3477
3755
|
}
|
|
3478
3756
|
}
|
|
3479
3757
|
|
|
@@ -3500,6 +3778,9 @@ function collectConstant(
|
|
|
3500
3778
|
ctx.propsObjectName === node.initializer.text
|
|
3501
3779
|
) {
|
|
3502
3780
|
const propsName = node.initializer.text
|
|
3781
|
+
// Bindings already destructured by this pattern, by local name: a later
|
|
3782
|
+
// default reads them (`const { off = false, on = off } = props`, #3531).
|
|
3783
|
+
const earlier = new Map<string, PropDefaultRead>()
|
|
3503
3784
|
for (const el of node.name.elements) {
|
|
3504
3785
|
if (!ts.isBindingElement(el) || !ts.isIdentifier(el.name) || el.dotDotDotToken) continue
|
|
3505
3786
|
const localName = el.name.text
|
|
@@ -3507,7 +3788,8 @@ function collectConstant(
|
|
|
3507
3788
|
// Shared with the #2943 `ParamInfo` overlay below so "what does this
|
|
3508
3789
|
// default look like" (source text, `defaultContainsArrow`) has one
|
|
3509
3790
|
// computation, not two independently-drifting ones.
|
|
3510
|
-
const defaultFields = el.initializer ? bindingElementDefaultFields(el.initializer, ctx) : undefined
|
|
3791
|
+
const defaultFields = el.initializer ? bindingElementDefaultFields(el.initializer, ctx, earlier) : undefined
|
|
3792
|
+
earlier.set(localName, bodyBindingRead(localName, sourceKey, defaultFields))
|
|
3511
3793
|
const baseValue = `${propsName}.${sourceKey}`
|
|
3512
3794
|
// Parenthesize an arrow/function-expression default (#2940) — `??`
|
|
3513
3795
|
// binds tighter than an arrow head, so a bare `props.fmt ?? (v) =>
|
|
@@ -3545,6 +3827,7 @@ function collectConstant(
|
|
|
3545
3827
|
// Body-level destructure-from-props is collected only when
|
|
3546
3828
|
// _isModule === false; binding lives in init scope.
|
|
3547
3829
|
origin: { phase: 'hydrate', scope: 'init', effect: 'pure' },
|
|
3830
|
+
propsDestructure: { sourceKey },
|
|
3548
3831
|
})
|
|
3549
3832
|
|
|
3550
3833
|
// #2943: a BODY destructure's default (`const { label = 'none' } =
|
|
@@ -3856,6 +4139,9 @@ function extractProps(param: ts.ParameterDeclaration, ctx: AnalyzerContext): voi
|
|
|
3856
4139
|
// `props: Props` path instead of degrading to `unknown` (which typed
|
|
3857
4140
|
// adapters emit as `interface{}` + an unchecked assertion). See issue #2150.
|
|
3858
4141
|
const memberTypes = param.type ? collectMemberTypes(param.type, ctx) : null
|
|
4142
|
+
// Bindings already destructured, by local name: a later default reads
|
|
4143
|
+
// them (`{ off = false, on = off }`, #3531).
|
|
4144
|
+
const earlier = new Map<string, PropDefaultRead>()
|
|
3859
4145
|
|
|
3860
4146
|
for (const element of param.name.elements) {
|
|
3861
4147
|
if (ts.isBindingElement(element) && ts.isIdentifier(element.name)) {
|
|
@@ -3876,16 +4162,18 @@ function extractProps(param: ts.ParameterDeclaration, ctx: AnalyzerContext): voi
|
|
|
3876
4162
|
const member = memberTypes?.get(sourcePropName)
|
|
3877
4163
|
const resolvedType: TypeInfo = member?.type ?? { kind: 'unknown', raw: 'unknown' }
|
|
3878
4164
|
|
|
3879
|
-
|
|
4165
|
+
const info: ParamInfo = {
|
|
3880
4166
|
name: localName,
|
|
3881
4167
|
type: resolvedType,
|
|
3882
4168
|
// The type's `?` and a destructure default (`{ x = 1 }`) both mean
|
|
3883
4169
|
// the caller may omit the prop.
|
|
3884
4170
|
optional: !!member?.optional || !!element.initializer,
|
|
3885
|
-
...bindingElementDefaultFields(element.initializer, ctx),
|
|
4171
|
+
...bindingElementDefaultFields(element.initializer, ctx, earlier),
|
|
3886
4172
|
// Only aliased bindings carry the source key — see ParamInfo.sourceName.
|
|
3887
4173
|
...(sourcePropName !== localName && { sourceName: sourcePropName }),
|
|
3888
|
-
}
|
|
4174
|
+
}
|
|
4175
|
+
ctx.propsParams.push(info)
|
|
4176
|
+
earlier.set(localName, propDefaultReadOf(info))
|
|
3889
4177
|
}
|
|
3890
4178
|
}
|
|
3891
4179
|
|