@barefootjs/jsx 0.39.5 → 0.39.7
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/array-method.d.ts +93 -0
- package/dist/adapters/array-method.d.ts.map +1 -0
- package/dist/adapters/attr-plan.d.ts +142 -0
- package/dist/adapters/attr-plan.d.ts.map +1 -0
- package/dist/adapters/boolean-result.d.ts +84 -9
- package/dist/adapters/boolean-result.d.ts.map +1 -1
- package/dist/adapters/callback-method.d.ts +153 -0
- package/dist/adapters/callback-method.d.ts.map +1 -0
- package/dist/adapters/component-props.d.ts +99 -0
- package/dist/adapters/component-props.d.ts.map +1 -0
- package/dist/adapters/component-tree.d.ts +25 -0
- package/dist/adapters/component-tree.d.ts.map +1 -0
- 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/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/literal-value.d.ts +22 -0
- package/dist/adapters/literal-value.d.ts.map +1 -0
- package/dist/adapters/loop-plan.d.ts +53 -0
- package/dist/adapters/loop-plan.d.ts.map +1 -0
- package/dist/adapters/prop-classes.d.ts +37 -0
- package/dist/adapters/prop-classes.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/expression-parser.d.ts +38 -0
- 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 +9 -0
- package/dist/free-refs.d.ts.map +1 -1
- package/dist/index.d.ts +23 -5
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +3954 -2401
- package/dist/ir-to-client-js/compute-inlinability.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-registration.d.ts.map +1 -1
- package/dist/ir-to-client-js/html-template.d.ts +9 -10
- package/dist/ir-to-client-js/html-template.d.ts.map +1 -1
- package/dist/ir-to-client-js/index.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/utils.d.ts +13 -0
- package/dist/ir-to-client-js/utils.d.ts.map +1 -1
- package/dist/jsx-to-ir.d.ts +465 -1
- package/dist/jsx-to-ir.d.ts.map +1 -1
- package/dist/nullable-signal-attr.d.ts +22 -2
- package/dist/nullable-signal-attr.d.ts.map +1 -1
- package/dist/props-binding.d.ts +34 -5
- 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/ssr-seed-plan.d.ts +0 -6
- package/dist/ssr-seed-plan.d.ts.map +1 -1
- package/dist/types.d.ts +54 -2
- package/dist/types.d.ts.map +1 -1
- package/package.json +2 -2
- package/src/__tests__/__snapshots__/doc-examples.test.ts.snap +1 -1
- package/src/__tests__/action-accessor.test.ts +6 -24
- package/src/__tests__/array-method-catalogue.test.ts +99 -0
- package/src/__tests__/attr-plan.test.ts +121 -0
- package/src/__tests__/binding-scope-ratchet.test.ts +12 -12
- package/src/__tests__/block-memo-type.test.ts +51 -0
- package/src/__tests__/boolean-result-expr.test.ts +106 -0
- package/src/__tests__/boolean-result.test.ts +34 -1
- package/src/__tests__/callback-method.test.ts +264 -0
- package/src/__tests__/client-only-date-lowering.test.ts +2 -2
- package/src/__tests__/component-props.test.ts +148 -0
- package/src/__tests__/computed-const.test.ts +101 -0
- package/src/__tests__/delegated-handler-preamble.test.ts +4 -4
- package/src/__tests__/dsl-shared-helpers.test.ts +166 -0
- package/src/__tests__/element-shell.test.ts +78 -0
- package/src/__tests__/fold-render-reassignments.test.ts +96 -0
- package/src/__tests__/inlinable-local-method-call.test.ts +81 -0
- package/src/__tests__/issue-3240-nested-static-map-event-delegation.test.ts +1 -1
- package/src/__tests__/loop-plan.test.ts +104 -0
- package/src/__tests__/map-call-decisions.test.ts +521 -0
- package/src/__tests__/memo-body-expr.test.ts +120 -0
- package/src/__tests__/nullable-signal-attr.test.ts +45 -0
- package/src/__tests__/prop-handling.test.ts +35 -16
- package/src/__tests__/props-alias-const.test.ts +65 -0
- package/src/__tests__/signal-prop-seed.test.ts +90 -0
- package/src/__tests__/ssr-seed-plan.test.ts +54 -1
- package/src/__tests__/static-template-reachability.test.ts +137 -0
- package/src/adapters/array-method.ts +193 -0
- package/src/adapters/attr-plan.ts +228 -0
- package/src/adapters/boolean-result.ts +208 -12
- package/src/adapters/callback-method.ts +363 -0
- package/src/adapters/component-props.ts +144 -0
- package/src/adapters/component-tree.ts +118 -0
- package/src/adapters/computed-const.ts +185 -0
- package/src/adapters/dangerous-inner-html.ts +33 -0
- package/src/adapters/element-shell.ts +160 -0
- package/src/adapters/interface.ts +8 -0
- package/src/adapters/literal-value.ts +36 -0
- package/src/adapters/loop-plan.ts +88 -0
- package/src/adapters/prop-classes.ts +73 -0
- package/src/analyzer.ts +271 -82
- package/src/augment-inherited-props.ts +12 -0
- package/src/expression-parser.ts +537 -374
- package/src/fold-render-reassignments.ts +314 -0
- package/src/free-refs.ts +49 -0
- package/src/index.ts +61 -3
- package/src/ir-to-client-js/compute-inlinability.ts +93 -17
- package/src/ir-to-client-js/control-flow/stringify/event-delegation.ts +9 -7
- package/src/ir-to-client-js/csr-substitute.ts +13 -35
- package/src/ir-to-client-js/emit-registration.ts +3 -2
- package/src/ir-to-client-js/html-template.ts +26 -70
- package/src/ir-to-client-js/index.ts +3 -2
- package/src/ir-to-client-js/init-declarations.ts +1 -1
- package/src/ir-to-client-js/plan/build-declaration-emit.ts +25 -51
- 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/rewrite-destructured-props.ts +6 -5
- package/src/ir-to-client-js/stringify/declaration-emit.ts +1 -1
- package/src/ir-to-client-js/utils.ts +11 -0
- package/src/jsx-to-ir.ts +1346 -1022
- package/src/nullable-signal-attr.ts +85 -4
- package/src/props-binding.ts +100 -5
- package/src/relocate.ts +40 -8
- package/src/ssr-seed-plan.ts +18 -6
- package/src/types.ts +50 -2
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, 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
15
|
import { rewriteBarePropRefs } from './prop-rewrite.ts'
|
|
15
|
-
import { buildPropAliasMap, coalesceDefaultText } from './props-binding.ts'
|
|
16
|
+
import { buildPropAliasMap, coalesceDefaultText, signalPropSeed } from './props-binding.ts'
|
|
16
17
|
import { incrementCounter } from './instrumentation.ts'
|
|
17
18
|
import {
|
|
18
19
|
type AnalyzerContext,
|
|
@@ -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,11 +397,7 @@ 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
402
|
const separatedSource = separateLiteralSignalPropBindings(ctx, isNonReferenceIdentifierPosition)
|
|
380
403
|
if (separatedSource !== null) {
|
|
@@ -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
|
/**
|
|
@@ -1583,6 +1612,7 @@ function collectFactorySignal(
|
|
|
1583
1612
|
: new Set<string>()
|
|
1584
1613
|
|
|
1585
1614
|
let type: TypeInfo = { kind: 'unknown', raw: 'unknown' }
|
|
1615
|
+
let blockTyped = false
|
|
1586
1616
|
if (callExpr.typeArguments && callExpr.typeArguments.length > 0) {
|
|
1587
1617
|
type = typeNodeToTypeInfo(callExpr.typeArguments[0], ctx.sourceFile) ?? type
|
|
1588
1618
|
} else if (initialNode.kind !== 'absent') {
|
|
@@ -1890,7 +1920,7 @@ function isMemoDeclaration(node: ts.VariableDeclaration, ctx: AnalyzerContext):
|
|
|
1890
1920
|
function memoBodyIsTemplateLiteral(memoArrow: ts.Expression | undefined): boolean {
|
|
1891
1921
|
let node: ts.Node | undefined = memoArrow
|
|
1892
1922
|
while (node && ts.isParenthesizedExpression(node)) node = node.expression
|
|
1893
|
-
if (!node || !ts.isArrowFunction(node)) return false
|
|
1923
|
+
if (!node || (!ts.isArrowFunction(node) && !ts.isFunctionExpression(node))) return false
|
|
1894
1924
|
let body: ts.Node = node.body
|
|
1895
1925
|
while (ts.isParenthesizedExpression(body)) body = body.expression
|
|
1896
1926
|
if (ts.isBlock(body)) {
|
|
@@ -1902,68 +1932,106 @@ function memoBodyIsTemplateLiteral(memoArrow: ts.Expression | undefined): boolea
|
|
|
1902
1932
|
return ts.isTemplateExpression(body) || ts.isNoSubstitutionTemplateLiteral(body)
|
|
1903
1933
|
}
|
|
1904
1934
|
|
|
1905
|
-
|
|
1906
|
-
|
|
1907
|
-
|
|
1908
|
-
|
|
1909
|
-
|
|
1910
|
-
|
|
1911
|
-
|
|
1912
|
-
|
|
1913
|
-
|
|
1914
|
-
|
|
1915
|
-
|
|
1916
|
-
|
|
1917
|
-
|
|
1918
|
-
|
|
1919
|
-
|
|
1920
|
-
|
|
1921
|
-
|
|
1922
|
-
if (callExpr.typeArguments && callExpr.typeArguments.length > 0) {
|
|
1923
|
-
type = typeNodeToTypeInfo(callExpr.typeArguments[0], ctx.sourceFile) ?? type
|
|
1924
|
-
} else {
|
|
1925
|
-
// The memo computation is an arrow function; the type we want is
|
|
1926
|
-
// the body's, not the arrow itself. Pull `() => <body>` apart so
|
|
1927
|
-
// `inferTypeFromValue` sees the actual expression.
|
|
1928
|
-
const arrowBody = computation.replace(/^\s*\([^)]*\)\s*=>\s*/, '').trim()
|
|
1929
|
-
if (arrowBody && arrowBody !== computation) {
|
|
1930
|
-
type = inferTypeFromValue(arrowBody)
|
|
1935
|
+
/**
|
|
1936
|
+
* Whether a callback's body means the same outside the callback. An arrow
|
|
1937
|
+
* binds nothing of its own. A `function` expression binds its name (when it
|
|
1938
|
+
* has one), `arguments` and `this`, so its body only means the same when it
|
|
1939
|
+
* is anonymous and reads neither; the bodies of nested non-arrow functions,
|
|
1940
|
+
* methods and class members bind their own and are not searched, but their
|
|
1941
|
+
* computed names and a class's `extends` clause are.
|
|
1942
|
+
*/
|
|
1943
|
+
function isFunctionScopeFree(fn: ts.ArrowFunction | ts.FunctionExpression): boolean {
|
|
1944
|
+
if (ts.isArrowFunction(fn)) return true
|
|
1945
|
+
if (fn.name) return false
|
|
1946
|
+
let bound = false
|
|
1947
|
+
const visit = (node: ts.Node): void => {
|
|
1948
|
+
if (bound) return
|
|
1949
|
+
if (node.kind === ts.SyntaxKind.ThisKeyword || (ts.isIdentifier(node) && node.text === 'arguments')) {
|
|
1950
|
+
bound = true
|
|
1951
|
+
return
|
|
1931
1952
|
}
|
|
1953
|
+
// A nested function or class body binds its own `this` / `arguments`,
|
|
1954
|
+
// but a computed member name and a class's `extends` clause are
|
|
1955
|
+
// evaluated in the enclosing scope, so those are still searched.
|
|
1956
|
+
if (ts.isFunctionDeclaration(node) || ts.isFunctionExpression(node)) return
|
|
1957
|
+
if (ts.isMethodDeclaration(node) || ts.isGetAccessor(node) || ts.isSetAccessor(node) ||
|
|
1958
|
+
ts.isPropertyDeclaration(node)) {
|
|
1959
|
+
if (ts.isComputedPropertyName(node.name)) visit(node.name.expression)
|
|
1960
|
+
return
|
|
1961
|
+
}
|
|
1962
|
+
if (ts.isClassLike(node)) {
|
|
1963
|
+
node.heritageClauses?.forEach(visit)
|
|
1964
|
+
node.members.forEach(visit)
|
|
1965
|
+
return
|
|
1966
|
+
}
|
|
1967
|
+
ts.forEachChild(node, visit)
|
|
1932
1968
|
}
|
|
1969
|
+
ts.forEachChild(fn.body, visit)
|
|
1970
|
+
return !bound
|
|
1971
|
+
}
|
|
1933
1972
|
|
|
1934
|
-
|
|
1935
|
-
|
|
1936
|
-
|
|
1937
|
-
|
|
1938
|
-
|
|
1939
|
-
|
|
1940
|
-
|
|
1941
|
-
if (
|
|
1942
|
-
|
|
1943
|
-
|
|
1944
|
-
|
|
1945
|
-
|
|
1946
|
-
|
|
1947
|
-
|
|
1948
|
-
|
|
1949
|
-
|
|
1950
|
-
|
|
1951
|
-
|
|
1952
|
-
|
|
1973
|
+
/**
|
|
1974
|
+
* Whether calling the callback yields its body's value in the callback's
|
|
1975
|
+
* absence: not async (a Promise), not a generator (an iterator), and not
|
|
1976
|
+
* bound to its own scope (`isFunctionScopeFree`).
|
|
1977
|
+
*/
|
|
1978
|
+
function isValueReturningCallback(fn: ts.ArrowFunction | ts.FunctionExpression): boolean {
|
|
1979
|
+
if (ts.isFunctionExpression(fn) && fn.asteriskToken) return false
|
|
1980
|
+
if (fn.modifiers?.some(m => m.kind === ts.SyntaxKind.AsyncKeyword)) return false
|
|
1981
|
+
return isFunctionScopeFree(fn)
|
|
1982
|
+
}
|
|
1983
|
+
|
|
1984
|
+
/**
|
|
1985
|
+
* The expression a memo's callback evaluates to, as the analyzer reads it from
|
|
1986
|
+
* the callback node (#3510). A zero-parameter arrow or `function` expression
|
|
1987
|
+
* yields its body: the expression body, or the operand of a block that is a
|
|
1988
|
+
* single `return`. Any other block (and any other callback: async, generator,
|
|
1989
|
+
* a parameter, a function reference, a `function` expression whose body needs
|
|
1990
|
+
* its own scope) yields a call of the whole callback, so a consumer that
|
|
1991
|
+
* substitutes the memo's value never sees an uncalled function or a body
|
|
1992
|
+
* detached from its bindings.
|
|
1993
|
+
*/
|
|
1994
|
+
function memoBodyNode(callback: ts.Expression): { node: ts.Node; call: boolean } {
|
|
1995
|
+
let fn: ts.Expression = callback
|
|
1996
|
+
while (ts.isParenthesizedExpression(fn)) fn = fn.expression
|
|
1997
|
+
if (!ts.isArrowFunction(fn) && !ts.isFunctionExpression(fn)) return { node: callback, call: true }
|
|
1998
|
+
if (fn.parameters.length > 0 || !isValueReturningCallback(fn)) return { node: callback, call: true }
|
|
1999
|
+
if (!ts.isBlock(fn.body)) return { node: fn.body, call: false }
|
|
2000
|
+
const [only] = fn.body.statements
|
|
2001
|
+
if (fn.body.statements.length === 1 && ts.isReturnStatement(only) && only.expression) {
|
|
2002
|
+
return { node: only.expression, call: false }
|
|
1953
2003
|
}
|
|
2004
|
+
return { node: callback, call: true }
|
|
2005
|
+
}
|
|
1954
2006
|
|
|
2007
|
+
/**
|
|
2008
|
+
* The memo body as structured trees (#2040, #3510): `parsed` for an
|
|
2009
|
+
* expression body, `parsedBlock` for a block body. See the field docs on
|
|
2010
|
+
* `MemoInfo`.
|
|
2011
|
+
*/
|
|
2012
|
+
function memoStructuredBody(
|
|
2013
|
+
callback: ts.Expression | undefined,
|
|
2014
|
+
ctx: AnalyzerContext,
|
|
2015
|
+
): Pick<MemoInfo, 'parsed' | 'parsedBlock' | 'parsedBlockComplete'> {
|
|
1955
2016
|
// Structured parse of the arrow BODY, so adapters can shape-match the memo
|
|
1956
2017
|
// on a tree instead of re-parsing `computation`. Parse from the type-STRIPPED
|
|
1957
2018
|
// body (`ctx.getJS`, same source as `computation`) — `getText` would keep
|
|
1958
2019
|
// TypeScript-only syntax (`as T`, `!`, `satisfies`) that `parseExpression`
|
|
1959
2020
|
// rejects, leaving `parsed` undefined for typed bodies that the stripped
|
|
1960
|
-
// `computation` would match. Expression
|
|
1961
|
-
// (`() => { … }`) and unsupported shapes leave
|
|
1962
|
-
//
|
|
1963
|
-
|
|
2021
|
+
// `computation` would match. Expression bodies only — block bodies
|
|
2022
|
+
// (`() => { … }`, `function () { … }`) and unsupported shapes leave
|
|
2023
|
+
// `parsed` undefined here (the block fold sets it later). Parens around the
|
|
2024
|
+
// callback are unwrapped (#3510).
|
|
2025
|
+
let memoFn: ts.Node | undefined = callback
|
|
2026
|
+
while (memoFn && ts.isParenthesizedExpression(memoFn)) memoFn = memoFn.expression
|
|
2027
|
+
// A parameter binds names the body reads (`(label = 'inner') => label`),
|
|
2028
|
+
// which a tree of the body alone would read as component scope (#3523).
|
|
2029
|
+
if (memoFn && ((!ts.isArrowFunction(memoFn) && !ts.isFunctionExpression(memoFn)) || memoFn.parameters.length > 0 || !isValueReturningCallback(memoFn))) {
|
|
2030
|
+
memoFn = undefined
|
|
2031
|
+
}
|
|
1964
2032
|
const parsedBody =
|
|
1965
|
-
|
|
1966
|
-
? parseExpression(ctx.getJS(
|
|
2033
|
+
memoFn && !ts.isBlock(memoFn.body)
|
|
2034
|
+
? parseExpression(ctx.getJS(memoFn.body))
|
|
1967
2035
|
: undefined
|
|
1968
2036
|
// `object-literal` is excluded alongside `unsupported`: an object-returning
|
|
1969
2037
|
// memo (`() => ({ … })`) isn't lowered from the parsed tree yet, so leaving
|
|
@@ -1976,14 +2044,8 @@ function collectMemo(node: ts.VariableDeclaration, ctx: AnalyzerContext): void {
|
|
|
1976
2044
|
|
|
1977
2045
|
// Block-bodied memos: carry the statements (tolerant — unparseable ones are
|
|
1978
2046
|
// 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
|
|
2047
|
+
// return-const memo) without re-parsing `computation`.
|
|
2048
|
+
const blockBody = memoFn && ts.isBlock(memoFn.body) ? memoFn.body : undefined
|
|
1987
2049
|
const parsedBlock = blockBody
|
|
1988
2050
|
? parseBlockBodyTolerant(blockBody, ctx.sourceFile, node => ctx.getJS(node))
|
|
1989
2051
|
: undefined
|
|
@@ -1992,7 +2054,17 @@ function collectMemo(node: ts.VariableDeclaration, ctx: AnalyzerContext): void {
|
|
|
1992
2054
|
// represented — nothing silently omitted (see `MemoInfo.parsedBlockComplete`).
|
|
1993
2055
|
const parsedBlockComplete =
|
|
1994
2056
|
parsedBlock && blockBody ? parsedBlock.length === blockBody.statements.length : undefined
|
|
2057
|
+
return { parsed, parsedBlock, parsedBlockComplete }
|
|
2058
|
+
}
|
|
1995
2059
|
|
|
2060
|
+
/**
|
|
2061
|
+
* `MemoInfo.bodyExpr` / `bodyExprIsCall` / `templateBodyExpr` from the
|
|
2062
|
+
* callback's body node (#3510).
|
|
2063
|
+
*/
|
|
2064
|
+
function memoBodyFields(
|
|
2065
|
+
body: { node: ts.Node; call: boolean },
|
|
2066
|
+
ctx: AnalyzerContext,
|
|
2067
|
+
): Pick<MemoInfo, 'bodyExpr' | 'bodyExprIsCall' | 'templateBodyExpr'> {
|
|
1996
2068
|
// Destructured-arg components only — the memo twin of the signal
|
|
1997
2069
|
// `templateInitialValue` rewrite (#2265) a few hundred lines up: a memo
|
|
1998
2070
|
// body referencing a bare destructured prop (`createMemo(() => value *
|
|
@@ -2001,33 +2073,149 @@ function collectMemo(node: ts.VariableDeclaration, ctx: AnalyzerContext): void {
|
|
|
2001
2073
|
// module scope — it is not a closure over `initXxx`'s `const value =
|
|
2002
2074
|
// _p.value` extraction, so the bare ref must become `_p.value`
|
|
2003
2075
|
// (adapter-tests scope-gate, #2468 sweep).
|
|
2004
|
-
|
|
2005
|
-
|
|
2076
|
+
const asBodyExpr = (text: string): string => (body.call ? `(${text})()` : text)
|
|
2077
|
+
const bodyExpr = asBodyExpr(ctx.getJS(body.node))
|
|
2078
|
+
let templateBodyExpr: string | undefined
|
|
2079
|
+
if (!ctx.propsObjectName) {
|
|
2006
2080
|
const propNames = new Set(ctx.propsParams.map(p => p.name))
|
|
2007
2081
|
if (propNames.size > 0) {
|
|
2008
2082
|
const propAliases = buildPropAliasMap(ctx.propsParams)
|
|
2009
|
-
|
|
2083
|
+
const rewritten = rewriteBarePropRefs(ctx.getJS(body.node), body.node, propNames, undefined, propAliases)
|
|
2084
|
+
templateBodyExpr = rewritten === undefined ? undefined : asBodyExpr(rewritten)
|
|
2085
|
+
}
|
|
2086
|
+
}
|
|
2087
|
+
return { bodyExpr, bodyExprIsCall: body.call || undefined, templateBodyExpr }
|
|
2088
|
+
}
|
|
2089
|
+
|
|
2090
|
+
function collectMemo(node: ts.VariableDeclaration, ctx: AnalyzerContext): void {
|
|
2091
|
+
const name = (node.name as ts.Identifier).text
|
|
2092
|
+
const callExpr = node.initializer as ts.CallExpression
|
|
2093
|
+
const computation = callExpr.arguments[0] ? ctx.getJS(callExpr.arguments[0]) : ''
|
|
2094
|
+
const typedComputation = callExpr.arguments[0] ? callExpr.arguments[0].getText(ctx.sourceFile) : undefined
|
|
2095
|
+
const body = callExpr.arguments[0] ? memoBodyNode(callExpr.arguments[0]) : undefined
|
|
2096
|
+
|
|
2097
|
+
// Extract dependencies from computation
|
|
2098
|
+
const deps = extractDependencies(computation, ctx)
|
|
2099
|
+
|
|
2100
|
+
// Try to infer type. Prefer the explicit `<T>` type argument when
|
|
2101
|
+
// present; otherwise fall back to the same value-shape inference
|
|
2102
|
+
// signals use (`inferTypeFromValue` handles `.length`, `.some(...)`,
|
|
2103
|
+
// `.every(...)`, etc.). Without this, `createMemo(() =>
|
|
2104
|
+
// todos().filter(...).length)` ends up as `unknown` and the Go
|
|
2105
|
+
// adapter renders the field as `interface{}`, even though the
|
|
2106
|
+
// expression is clearly a number.
|
|
2107
|
+
let type: TypeInfo = { kind: 'unknown', raw: 'unknown' }
|
|
2108
|
+
let blockTyped = false
|
|
2109
|
+
if (callExpr.typeArguments && callExpr.typeArguments.length > 0) {
|
|
2110
|
+
type = typeNodeToTypeInfo(callExpr.typeArguments[0], ctx.sourceFile) ?? type
|
|
2111
|
+
} else {
|
|
2112
|
+
// The memo computation is a function; the type we want is the body's,
|
|
2113
|
+
// not the function itself, so `inferTypeFromValue` sees the body text
|
|
2114
|
+
// (a block body reads as `object`, which the checker upgrade below
|
|
2115
|
+
// refines).
|
|
2116
|
+
const fn = callExpr.arguments[0]
|
|
2117
|
+
if (fn && (ts.isArrowFunction(fn) || ts.isFunctionExpression(fn))) {
|
|
2118
|
+
type = inferTypeFromValue(ctx.getJS(fn.body).trim())
|
|
2119
|
+
blockTyped = ts.isBlock(fn.body)
|
|
2120
|
+
}
|
|
2121
|
+
}
|
|
2122
|
+
|
|
2123
|
+
// When the syntactic heuristic above can't resolve a precise type
|
|
2124
|
+
// (`object`/`unknown` — e.g. a local-function call or a ternary of typed
|
|
2125
|
+
// arrays), ask the type checker for the memo body's actual type. This is
|
|
2126
|
+
// what lets the Go adapter emit `[][]CalendarDay` / `[]string` / `bool`
|
|
2127
|
+
// instead of `map[string]interface{}` / `bool` placeholders (#1968), so a
|
|
2128
|
+
// typed backend can populate the SSR data. Only upgrades imprecise results —
|
|
2129
|
+
// already-precise syntactic types are left untouched.
|
|
2130
|
+
if (
|
|
2131
|
+
ctx.checker &&
|
|
2132
|
+
(type.kind === 'unknown' || type.kind === 'object') &&
|
|
2133
|
+
callExpr.arguments[0] &&
|
|
2134
|
+
(ts.isArrowFunction(callExpr.arguments[0]) || ts.isFunctionExpression(callExpr.arguments[0]))
|
|
2135
|
+
) {
|
|
2136
|
+
const fnType = ctx.checker.getTypeAtLocation(callExpr.arguments[0])
|
|
2137
|
+
const sig = fnType.getCallSignatures()[0]
|
|
2138
|
+
if (sig) {
|
|
2139
|
+
const inferred = tsTypeToTypeInfo(ctx.checker.getReturnTypeOfSignature(sig), ctx.checker)
|
|
2140
|
+
if (inferred && inferred.kind !== 'unknown') {
|
|
2141
|
+
type = inferred
|
|
2142
|
+
blockTyped = false
|
|
2143
|
+
}
|
|
2010
2144
|
}
|
|
2011
2145
|
}
|
|
2012
2146
|
|
|
2013
|
-
ctx
|
|
2147
|
+
const { parsed, parsedBlock, parsedBlockComplete } = memoStructuredBody(callExpr.arguments[0], ctx)
|
|
2148
|
+
|
|
2149
|
+
const bodyFields = body ? memoBodyFields(body, ctx) : {}
|
|
2150
|
+
|
|
2151
|
+
const memo: MemoInfo = {
|
|
2014
2152
|
name,
|
|
2015
2153
|
computation,
|
|
2016
|
-
|
|
2154
|
+
...bodyFields,
|
|
2017
2155
|
parsedBlock,
|
|
2018
2156
|
parsedBlockComplete,
|
|
2019
2157
|
typedComputation: typedComputation !== computation ? typedComputation : undefined,
|
|
2020
2158
|
parsed,
|
|
2021
|
-
bodyIsTemplateLiteral: memoBodyIsTemplateLiteral(
|
|
2159
|
+
bodyIsTemplateLiteral: memoBodyIsTemplateLiteral(callExpr.arguments[0]),
|
|
2022
2160
|
type,
|
|
2023
2161
|
deps,
|
|
2024
2162
|
loc: getSourceLocation(node, ctx.sourceFile, ctx.filePath),
|
|
2025
2163
|
computationFreeIdentifiers: callExpr.arguments[0]
|
|
2026
2164
|
? extractFreeIdentifiersFromNode(callExpr.arguments[0])
|
|
2027
2165
|
: new Set(),
|
|
2028
|
-
}
|
|
2166
|
+
}
|
|
2167
|
+
ctx.memos.push(memo)
|
|
2168
|
+
markBlockMemo(memo, blockTyped, body?.node, ctx)
|
|
2029
2169
|
}
|
|
2030
2170
|
|
|
2171
|
+
/** Record what `foldBlockMemos` needs to know about a block memo (#3523). */
|
|
2172
|
+
function markBlockMemo(memo: MemoInfo, blockTyped: boolean, bodyNode: ts.Node | undefined, ctx: AnalyzerContext): void {
|
|
2173
|
+
if (blockTyped) blockTextTypedMemos.add(memo)
|
|
2174
|
+
if (bodyNode && containsClientDirective(bodyNode, ctx.sourceFile)) clientDirectiveMemos.add(memo)
|
|
2175
|
+
}
|
|
2176
|
+
|
|
2177
|
+
/**
|
|
2178
|
+
* Whether a `/* @client */` directive comment precedes any node inside
|
|
2179
|
+
* `root` (`return /* @client */ d.toLocaleDateString(…)`). A block memo
|
|
2180
|
+
* carrying one is left unfolded, so it stays client-only (#3523).
|
|
2181
|
+
*/
|
|
2182
|
+
function containsClientDirective(root: ts.Node, sourceFile: ts.SourceFile): boolean {
|
|
2183
|
+
let found = false
|
|
2184
|
+
const visit = (node: ts.Node): void => {
|
|
2185
|
+
if (found) return
|
|
2186
|
+
const trivia = sourceFile.text.slice(node.pos, node.getStart(sourceFile))
|
|
2187
|
+
BLOCK_COMMENT_RE.lastIndex = 0
|
|
2188
|
+
let m: RegExpExecArray | null
|
|
2189
|
+
while ((m = BLOCK_COMMENT_RE.exec(trivia)) !== null) {
|
|
2190
|
+
if (CLIENT_DIRECTIVE_INTERIOR_RE.test(m[1])) found = true
|
|
2191
|
+
}
|
|
2192
|
+
ts.forEachChild(node, visit)
|
|
2193
|
+
}
|
|
2194
|
+
ts.forEachChild(root, visit)
|
|
2195
|
+
return found
|
|
2196
|
+
}
|
|
2197
|
+
|
|
2198
|
+
/** Block memos that carry a `/* @client */` directive and so are not folded. */
|
|
2199
|
+
const clientDirectiveMemos = new WeakSet<MemoInfo>()
|
|
2200
|
+
|
|
2201
|
+
/**
|
|
2202
|
+
* A block memo's type from its fold (#3523): the block's text read as
|
|
2203
|
+
* `object`, but its value is the fold. A returned object literal stays
|
|
2204
|
+
* `object`. A logical fold (`k ?? false`) is `unknown`: its value can be
|
|
2205
|
+
* either side, while the text heuristic would read the fallback's type.
|
|
2206
|
+
*/
|
|
2207
|
+
function foldType(expr: ParsedExpr): TypeInfo {
|
|
2208
|
+
if (expr.kind === 'object-literal') return { kind: 'object', raw: 'object' }
|
|
2209
|
+
if (expr.kind === 'logical') return { kind: 'unknown', raw: 'unknown' }
|
|
2210
|
+
return inferTypeFromValue(stringifyParsedExpr(expr))
|
|
2211
|
+
}
|
|
2212
|
+
|
|
2213
|
+
/**
|
|
2214
|
+
* Memos whose type was read off a block body's text, where `{` reads as an
|
|
2215
|
+
* object. Their type is re-inferred from the block's fold (#3523).
|
|
2216
|
+
*/
|
|
2217
|
+
const blockTextTypedMemos = new WeakSet<MemoInfo>()
|
|
2218
|
+
|
|
2031
2219
|
// =============================================================================
|
|
2032
2220
|
// Effect Detection & Collection
|
|
2033
2221
|
// =============================================================================
|
|
@@ -3545,6 +3733,7 @@ function collectConstant(
|
|
|
3545
3733
|
// Body-level destructure-from-props is collected only when
|
|
3546
3734
|
// _isModule === false; binding lives in init scope.
|
|
3547
3735
|
origin: { phase: 'hydrate', scope: 'init', effect: 'pure' },
|
|
3736
|
+
propsDestructure: { sourceKey },
|
|
3548
3737
|
})
|
|
3549
3738
|
|
|
3550
3739
|
// #2943: a BODY destructure's default (`const { label = 'none' } =
|
|
@@ -90,6 +90,18 @@ export function collectContextConsumers(metadata: IRMetadata): ContextConsumer[]
|
|
|
90
90
|
return consumers
|
|
91
91
|
}
|
|
92
92
|
|
|
93
|
+
/**
|
|
94
|
+
* The SSR seed of a component's context consumers: one `line` per consumer,
|
|
95
|
+
* each binding the local to the active provider value (or the
|
|
96
|
+
* `createContext` default), newline-terminated; `''` when it consumes no
|
|
97
|
+
* context (#1297).
|
|
98
|
+
*/
|
|
99
|
+
export function contextConsumerSeed(metadata: IRMetadata, line: (consumer: ContextConsumer) => string): string {
|
|
100
|
+
const consumers = collectContextConsumers(metadata)
|
|
101
|
+
if (consumers.length === 0) return ''
|
|
102
|
+
return consumers.map(line).join('\n') + '\n'
|
|
103
|
+
}
|
|
104
|
+
|
|
93
105
|
/** Parse `useContext(Ident)` → `Ident`, else `null`. */
|
|
94
106
|
function parseUseContextArg(source: string): string | null {
|
|
95
107
|
const expr = parseSingleExpression(source)
|