@barefootjs/jsx 0.41.0 → 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 +11 -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/number-value.d.ts +2 -1
- package/dist/adapters/number-value.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 +10 -5
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2500 -1475
- 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 +59 -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-const-child-prop.test.ts +85 -0
- 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 +19 -3
- 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/number-value.ts +26 -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 +10 -5
- 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 +121 -60
- 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 +56 -0
package/src/jsx-to-ir.ts
CHANGED
|
@@ -47,6 +47,7 @@ import { parseExpression, freeIdentifiers, isSupported, parseBlockBody, foldBloc
|
|
|
47
47
|
import { fallbackChainParts, nullishChainParts } from './children-nullish.ts'
|
|
48
48
|
import type { IRLoopSort, FunctionInfo, ConstantInfo, ParamInfo, FreeReference, SpreadAttr, JsSourceText } from './types.ts'
|
|
49
49
|
import { resolveStaticConstBinding } from './augment-inherited-props.ts'
|
|
50
|
+
import { isFullyStaticLiteral } from './static-literal.ts'
|
|
50
51
|
import { componentScopeNames } from './adapters/computed-const.ts'
|
|
51
52
|
import { formatParamWithType } from './module-exports.ts'
|
|
52
53
|
import { createError, ErrorCodes, internalInvariant } from './errors.ts'
|
|
@@ -58,7 +59,7 @@ import {
|
|
|
58
59
|
collectAstPropRefs,
|
|
59
60
|
rewriteScopedValueRefs,
|
|
60
61
|
} from './prop-rewrite.ts'
|
|
61
|
-
import { boundPropLocalNames, buildPropAliasMap, templatePropReadParams, resolveAliasOrigin, resolveRestSpreadOriginCore, livePropReadExpr } from './props-binding.ts'
|
|
62
|
+
import { boundPropLocalNames, buildPropAliasMap, templatePropReadParams, resolveAliasOrigin, resolveRestSpreadOriginCore, restBagOmittedKeys, livePropReadExpr } from './props-binding.ts'
|
|
62
63
|
import { PROPS_PARAM } from './ir-to-client-js/utils.ts'
|
|
63
64
|
import { walkIR } from './ir-to-client-js/walker.ts'
|
|
64
65
|
import { resolveFreeRefs, rebindHelperParamRefs, isNameBound as isNameBoundInEnv, type BindingEnvironment } from './free-refs.ts'
|
|
@@ -7367,9 +7368,17 @@ function expandSpreadAttribute(
|
|
|
7367
7368
|
const slotId = generateSpreadSlotId(ctx)
|
|
7368
7369
|
const value = AttrValueOf.spread(spreadExpr, ctx.getTemplateJS(attr.expression), slotId)
|
|
7369
7370
|
const callerBag = callerBagOfSpread(value, ctx)
|
|
7371
|
+
const shadowsPropsObject = spreadShadowsPropsObject(attr, ctx)
|
|
7370
7372
|
return [{
|
|
7371
7373
|
name: '...',
|
|
7372
|
-
value: callerBag
|
|
7374
|
+
value: callerBag || shadowsPropsObject
|
|
7375
|
+
? {
|
|
7376
|
+
...value,
|
|
7377
|
+
...(callerBag && { callerBag }),
|
|
7378
|
+
...(callerBag === 'rest' && { restOmittedKeys: restBagOmittedKeys(ctx.analyzer) }),
|
|
7379
|
+
...(shadowsPropsObject && { shadowsPropsObject: true as const }),
|
|
7380
|
+
}
|
|
7381
|
+
: value,
|
|
7373
7382
|
loc,
|
|
7374
7383
|
freeIdentifiers: spreadFreeIdentifiers,
|
|
7375
7384
|
}]
|
|
@@ -7668,16 +7677,46 @@ function isEffectFreeExpression(expr: ts.Expression): boolean {
|
|
|
7668
7677
|
* handler name. `null` otherwise, and the spread stays a spread.
|
|
7669
7678
|
*/
|
|
7670
7679
|
function expandStaticConstSpread(attr: ts.JsxSpreadAttribute, ctx: TransformContext): Array<{ key: string; text: string }> | null {
|
|
7671
|
-
|
|
7680
|
+
const init = staticConstInitializerOf(attr.expression, ctx)
|
|
7681
|
+
if (!init || !ts.isObjectLiteralExpression(init)) return null
|
|
7682
|
+
return staticObjectSpreadEntries(init)
|
|
7683
|
+
}
|
|
7684
|
+
|
|
7685
|
+
/**
|
|
7686
|
+
* The initializer of the const a bare-name read `expr` reaches, when that
|
|
7687
|
+
* const holds an array or object literal (`const QS = ['a', 'b']`,
|
|
7688
|
+
* `const base = { label: 'x' }`): the read is the const's value, so the IR
|
|
7689
|
+
* can carry the literal in its place. Which declaration the name reaches is
|
|
7690
|
+
* `resolveStaticConstBinding`'s decision; a module const a component
|
|
7691
|
+
* binding of the same name hides (`function C({ QS })`) is not reached.
|
|
7692
|
+
* `null` for any other read or initializer.
|
|
7693
|
+
*/
|
|
7694
|
+
function staticConstInitializerOf(expr: ts.Expression, ctx: TransformContext): ts.ArrayLiteralExpression | ts.ObjectLiteralExpression | null {
|
|
7672
7695
|
while (ts.isParenthesizedExpression(expr)) expr = expr.expression
|
|
7673
7696
|
if (!ts.isIdentifier(expr)) return null
|
|
7674
7697
|
const name = expr.text
|
|
7675
7698
|
const constant = resolveStaticConstBinding(name, ctx.analyzer.localConstants, n => ctx.scope.isBound(n))
|
|
7676
|
-
|
|
7699
|
+
const kind = constant?.parsed?.kind
|
|
7700
|
+
if (!constant || (kind !== 'object-literal' && kind !== 'array-literal')) return null
|
|
7677
7701
|
if (constant.isModule && componentScopeNames(ctx.analyzer).has(name)) return null
|
|
7678
7702
|
const init = parseConstInitializer(constant)
|
|
7679
|
-
if (!init || !ts.isObjectLiteralExpression(init)) return null
|
|
7680
|
-
return
|
|
7703
|
+
if (!init || !(ts.isObjectLiteralExpression(init) || ts.isArrayLiteralExpression(init))) return null
|
|
7704
|
+
return init
|
|
7705
|
+
}
|
|
7706
|
+
|
|
7707
|
+
/**
|
|
7708
|
+
* `value` with `staticConstLiteral` set when the prop's value is a bare read
|
|
7709
|
+
* of a const holding a fully static array or object literal (`<Kid xs={QS} />`
|
|
7710
|
+
* beside `const QS = ['a', 'b']`): only a literal that reads no name and
|
|
7711
|
+
* calls nothing (`isFullyStaticLiteral`). `value` unchanged otherwise.
|
|
7712
|
+
*/
|
|
7713
|
+
function withStaticConstLiteral(value: AttrValue, attr: ts.JsxAttribute, ctx: TransformContext): AttrValue {
|
|
7714
|
+
if (value.kind !== 'expression' || !attr.initializer || !ts.isJsxExpression(attr.initializer) || !attr.initializer.expression) return value
|
|
7715
|
+
const init = staticConstInitializerOf(attr.initializer.expression, ctx)
|
|
7716
|
+
if (!init) return value
|
|
7717
|
+
const parsed = tsNodeToParsedExpr(init)
|
|
7718
|
+
if (!isFullyStaticLiteral(parsed)) return value
|
|
7719
|
+
return { ...value, staticConstLiteral: { expr: astText(init), parsed } }
|
|
7681
7720
|
}
|
|
7682
7721
|
|
|
7683
7722
|
/**
|
|
@@ -7696,6 +7735,19 @@ function staticObjectSpreadEntries(init: ts.ObjectLiteralExpression): Array<{ ke
|
|
|
7696
7735
|
return [...entries].map(([key, text]) => ({ key, text }))
|
|
7697
7736
|
}
|
|
7698
7737
|
|
|
7738
|
+
/**
|
|
7739
|
+
* Whether `{...name}` spreads a binding spelled like the component's props
|
|
7740
|
+
* object (`SpreadAttr.shadowsPropsObject`): a loop row or callback parameter
|
|
7741
|
+
* of that name hides the props object at the spread (#3727).
|
|
7742
|
+
*/
|
|
7743
|
+
function spreadShadowsPropsObject(attr: ts.JsxSpreadAttribute, ctx: TransformContext): boolean {
|
|
7744
|
+
const propsName = ctx.analyzer.propsObjectName
|
|
7745
|
+
if (propsName === null) return false
|
|
7746
|
+
let expr: ts.Expression = attr.expression
|
|
7747
|
+
while (ts.isParenthesizedExpression(expr)) expr = expr.expression
|
|
7748
|
+
return ts.isIdentifier(expr) && expr.text === propsName && ctx.scope.isBound(propsName)
|
|
7749
|
+
}
|
|
7750
|
+
|
|
7699
7751
|
/**
|
|
7700
7752
|
* Which caller prop bag a spread forwards whole (`SpreadAttr.callerBag`):
|
|
7701
7753
|
* its `expr` / `templateExpr` resolved through any alias hop
|
|
@@ -9527,65 +9579,74 @@ function processComponentProps(
|
|
|
9527
9579
|
}
|
|
9528
9580
|
}
|
|
9529
9581
|
|
|
9530
|
-
|
|
9531
|
-
|
|
9532
|
-
// template literal back into a JS expression — but keep the parsed
|
|
9533
|
-
// parts in `parts` so template-based SSR adapters (Mojo, Go) can still
|
|
9534
|
-
// emit structured lookups / ternaries instead of round-tripping a raw
|
|
9535
|
-
// JS source through their expression pipelines. JS-runtime adapters
|
|
9536
|
-
// (Hono) keep using `expr`. Boolean-attr is also promoted to
|
|
9537
|
-
// shorthand (`<X disabled />` → `disabled={true}`).
|
|
9538
|
-
if (value.kind === 'template') {
|
|
9539
|
-
// Collapse using the raw part values, but also carry the
|
|
9540
|
-
// template-variant projection (parts' `templateExpr` /
|
|
9541
|
-
// `templateCondition` / `templateKey`, i.e. destructured-prop refs
|
|
9542
|
-
// rewritten to `_p.X`) as `templateExpr`. Without it, the
|
|
9543
|
-
// `templateExpr` derivation below walks the ORIGINAL initializer AST
|
|
9544
|
-
// — for a const-resolved template (`className={classes}`) that AST is
|
|
9545
|
-
// the bare `classes` identifier with no prop refs, so the rewrite
|
|
9546
|
-
// no-ops and the module-scope registration template leaks bare
|
|
9547
|
-
// destructured props into `renderChild(...)` (#2468).
|
|
9548
|
-
const collapsed = templatePartsToJsExpr(value.parts)
|
|
9549
|
-
const collapsedTemplate = templatePartsToJsExpr(value.parts, { useTemplate: true })
|
|
9550
|
-
value = AttrValueOf.expression(collapsed, {
|
|
9551
|
-
parts: value.parts,
|
|
9552
|
-
...(collapsedTemplate !== collapsed && { templateExpr: collapsedTemplate }),
|
|
9553
|
-
})
|
|
9554
|
-
} else if (value.kind === 'boolean-attr') {
|
|
9555
|
-
value = AttrValueOf.booleanShorthand()
|
|
9556
|
-
}
|
|
9582
|
+
props.push(valueComponentProp(attr, name, ctx))
|
|
9583
|
+
}
|
|
9557
9584
|
|
|
9558
|
-
|
|
9559
|
-
|
|
9560
|
-
if (value.kind === 'expression' && value.templateExpr === undefined) {
|
|
9561
|
-
const rewritten = rewriteBarePropRefs(value.expr, attr.initializer.expression, ctx)
|
|
9562
|
-
if (rewritten !== value.expr) {
|
|
9563
|
-
value = { ...value, templateExpr: rewritten }
|
|
9564
|
-
}
|
|
9565
|
-
}
|
|
9566
|
-
// `/* @client */` in component-prop initializer position: defer
|
|
9567
|
-
// to hydrate. Detection routed through the shared helper so it
|
|
9568
|
-
// agrees with JSX-child / element-attr sites. Downstream:
|
|
9569
|
-
// html-template strips the prop from `renderChild`; `initChild`'s
|
|
9570
|
-
// `propsExpr` already runs in init scope so the value reaches
|
|
9571
|
-
// the child component once init runs.
|
|
9572
|
-
if (hasLeadingClientDirective(attr.initializer.expression, ctx.sourceFile)) {
|
|
9573
|
-
clientOnly = true
|
|
9574
|
-
}
|
|
9575
|
-
}
|
|
9585
|
+
return props
|
|
9586
|
+
}
|
|
9576
9587
|
|
|
9577
|
-
|
|
9578
|
-
|
|
9579
|
-
|
|
9580
|
-
|
|
9581
|
-
|
|
9582
|
-
|
|
9583
|
-
|
|
9584
|
-
|
|
9588
|
+
/**
|
|
9589
|
+
* The `IRProp` for one JSX attribute of a component whose value is not a
|
|
9590
|
+
* spread, a JSX element or a refused JSX-wrapping shape (those are
|
|
9591
|
+
* `processComponentProps`'s own cases).
|
|
9592
|
+
*/
|
|
9593
|
+
function valueComponentProp(attr: ts.JsxAttribute, name: string, ctx: TransformContext): IRProp {
|
|
9594
|
+
let value = getAttributeValue(attr, ctx)
|
|
9595
|
+
// Components receive props as runtime values, so collapse a structured
|
|
9596
|
+
// template literal back into a JS expression — but keep the parsed
|
|
9597
|
+
// parts in `parts` so template-based SSR adapters (Mojo, Go) can still
|
|
9598
|
+
// emit structured lookups / ternaries instead of round-tripping a raw
|
|
9599
|
+
// JS source through their expression pipelines. JS-runtime adapters
|
|
9600
|
+
// (Hono) keep using `expr`. Boolean-attr is also promoted to
|
|
9601
|
+
// shorthand (`<X disabled />` → `disabled={true}`).
|
|
9602
|
+
if (value.kind === 'template') {
|
|
9603
|
+
// Collapse using the raw part values, but also carry the
|
|
9604
|
+
// template-variant projection (parts' `templateExpr` /
|
|
9605
|
+
// `templateCondition` / `templateKey`, i.e. destructured-prop refs
|
|
9606
|
+
// rewritten to `_p.X`) as `templateExpr`. Without it, the
|
|
9607
|
+
// `templateExpr` derivation below walks the ORIGINAL initializer AST
|
|
9608
|
+
// — for a const-resolved template (`className={classes}`) that AST is
|
|
9609
|
+
// the bare `classes` identifier with no prop refs, so the rewrite
|
|
9610
|
+
// no-ops and the module-scope registration template leaks bare
|
|
9611
|
+
// destructured props into `renderChild(...)` (#2468).
|
|
9612
|
+
const collapsed = templatePartsToJsExpr(value.parts)
|
|
9613
|
+
const collapsedTemplate = templatePartsToJsExpr(value.parts, { useTemplate: true })
|
|
9614
|
+
value = AttrValueOf.expression(collapsed, {
|
|
9615
|
+
parts: value.parts,
|
|
9616
|
+
...(collapsedTemplate !== collapsed && { templateExpr: collapsedTemplate }),
|
|
9585
9617
|
})
|
|
9618
|
+
} else if (value.kind === 'boolean-attr') {
|
|
9619
|
+
value = AttrValueOf.booleanShorthand()
|
|
9586
9620
|
}
|
|
9587
9621
|
|
|
9588
|
-
|
|
9622
|
+
let clientOnly: boolean | undefined
|
|
9623
|
+
if (attr.initializer && ts.isJsxExpression(attr.initializer) && attr.initializer.expression) {
|
|
9624
|
+
if (value.kind === 'expression' && value.templateExpr === undefined) {
|
|
9625
|
+
const rewritten = rewriteBarePropRefs(value.expr, attr.initializer.expression, ctx)
|
|
9626
|
+
if (rewritten !== value.expr) {
|
|
9627
|
+
value = { ...value, templateExpr: rewritten }
|
|
9628
|
+
}
|
|
9629
|
+
}
|
|
9630
|
+
// `/* @client */` in component-prop initializer position: defer
|
|
9631
|
+
// to hydrate. Detection routed through the shared helper so it
|
|
9632
|
+
// agrees with JSX-child / element-attr sites. Downstream:
|
|
9633
|
+
// html-template strips the prop from `renderChild`; `initChild`'s
|
|
9634
|
+
// `propsExpr` already runs in init scope so the value reaches
|
|
9635
|
+
// the child component once init runs.
|
|
9636
|
+
if (hasLeadingClientDirective(attr.initializer.expression, ctx.sourceFile)) {
|
|
9637
|
+
clientOnly = true
|
|
9638
|
+
}
|
|
9639
|
+
}
|
|
9640
|
+
|
|
9641
|
+
const freeIdentifiers = attrFreeIdentifiers(attr)
|
|
9642
|
+
return {
|
|
9643
|
+
name,
|
|
9644
|
+
value: clientOnly ? value : withStaticConstLiteral(value, attr, ctx),
|
|
9645
|
+
clientOnly,
|
|
9646
|
+
loc: getSourceLocation(attr, ctx.sourceFile, ctx.filePath),
|
|
9647
|
+
...computeReactivityFlags(attr, ctx),
|
|
9648
|
+
...(freeIdentifiers !== undefined && { freeIdentifiers }),
|
|
9649
|
+
}
|
|
9589
9650
|
}
|
|
9590
9651
|
|
|
9591
9652
|
// =============================================================================
|
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Zero-argument calls a template resolves to what the called function
|
|
3
|
+
* returns (#3721).
|
|
4
|
+
*
|
|
5
|
+
* A template has no binding for a component's or module's functions: the
|
|
6
|
+
* backend receives the props, not the JS scope. Every template adapter reads
|
|
7
|
+
* a zero-argument identifier call as a signal or memo getter (`count()` →
|
|
8
|
+
* `count`), so a call to a function (`function value() { return 1 }` called
|
|
9
|
+
* as `value()`) read a template variable of the function's name — nothing,
|
|
10
|
+
* or a prop of the same name, which a `(props)`-arg component flattens into
|
|
11
|
+
* the same template namespace.
|
|
12
|
+
*
|
|
13
|
+
* This is the one decision of what such a call reads, made in two steps.
|
|
14
|
+
* First the declaration the name denotes where the call is written
|
|
15
|
+
* ({@link localBindings}): a component-scope declaration of a name hides
|
|
16
|
+
* the module one, whatever either is, and a function body's names resolve
|
|
17
|
+
* in the scope the function is declared in. A prop counts as a component
|
|
18
|
+
* binding only where its bare name is in scope (`templatePropReadParams`):
|
|
19
|
+
* a destructured parameter, or a body binding that holds the prop. A typed
|
|
20
|
+
* prop the body never binds does not hide a function of its name, as in JS.
|
|
21
|
+
* Then what a call of that declaration is ({@link localCall}): inlined, for
|
|
22
|
+
* a function taking no parameters whose body is a single `return <expr>` (a
|
|
23
|
+
* `function` declaration's `FunctionInfo.returnParsed`, or a zero-param
|
|
24
|
+
* arrow `const`); refused, for any other function; neither, for a binding
|
|
25
|
+
* that is not a function. The consumers resolve the call through this
|
|
26
|
+
* table: the SSR seed plan (signal seeds, memo bodies), the static SSR
|
|
27
|
+
* defaults, the template adapters' shared inlining of markup expressions
|
|
28
|
+
* (`inlineComputedConsts`), and the walk of what a server-rendered position
|
|
29
|
+
* reads (`walkServerReads`).
|
|
30
|
+
*/
|
|
31
|
+
|
|
32
|
+
import { freeIdentifiers, mapParsedExpr, zeroArgCallBody, type ParsedExpr } from './expression-parser.ts'
|
|
33
|
+
import { templatePropReadParams, type TemplatePropReadBindings } from './props-binding.ts'
|
|
34
|
+
import type { ConstantInfo, FunctionInfo, MemoInfo, SignalInfo } from './types.ts'
|
|
35
|
+
|
|
36
|
+
/** A scope a name is declared in, and a function body's names are read in. */
|
|
37
|
+
export type LocalScope = 'component' | 'module'
|
|
38
|
+
|
|
39
|
+
/** What a zero-argument call of a function returns, and where its body's names resolve. */
|
|
40
|
+
export interface LocalCallBody {
|
|
41
|
+
/** The returned expression. */
|
|
42
|
+
body: ParsedExpr
|
|
43
|
+
/** The scope the function is declared in, which the body's free names are read in. */
|
|
44
|
+
scope: LocalScope
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/** The component bindings {@link localBindings} reads. */
|
|
48
|
+
export interface LocalCallBindings extends TemplatePropReadBindings {
|
|
49
|
+
localFunctions: readonly FunctionInfo[]
|
|
50
|
+
restPropsName?: string | null
|
|
51
|
+
signals: readonly SignalInfo[]
|
|
52
|
+
memos: readonly MemoInfo[]
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* What a name denotes in the scope it is declared in. `opaque` is a binding
|
|
57
|
+
* with no declaration to read: a prop in scope by its bare name, the props
|
|
58
|
+
* object, the rest props, a signal setter.
|
|
59
|
+
*/
|
|
60
|
+
export type LocalBinding =
|
|
61
|
+
| { kind: 'signal'; scope: LocalScope; info: SignalInfo }
|
|
62
|
+
| { kind: 'memo'; scope: LocalScope; info: MemoInfo }
|
|
63
|
+
| { kind: 'const'; scope: LocalScope; info: ConstantInfo }
|
|
64
|
+
| { kind: 'function'; scope: LocalScope; info: FunctionInfo }
|
|
65
|
+
| { kind: 'opaque'; scope: LocalScope }
|
|
66
|
+
|
|
67
|
+
/** The declaration `name` denotes, read from `from`: `null` for a name neither scope declares. */
|
|
68
|
+
export type LocalBindingResolver = (name: string, from: LocalScope) => LocalBinding | null
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* The lexical resolution of a component's and its module's names. Read from
|
|
72
|
+
* the component scope, a component declaration of the name is the binding,
|
|
73
|
+
* and only a name the component does not declare reaches the module's; read
|
|
74
|
+
* from the module scope (a module function's body), only the module's is.
|
|
75
|
+
*/
|
|
76
|
+
export function localBindings(b: LocalCallBindings): LocalBindingResolver {
|
|
77
|
+
const scopes = { component: new Map<string, LocalBinding>(), module: new Map<string, LocalBinding>() }
|
|
78
|
+
const bind = (name: string, binding: LocalBinding): void => {
|
|
79
|
+
scopes[binding.scope].set(name, binding)
|
|
80
|
+
}
|
|
81
|
+
for (const info of b.localFunctions) {
|
|
82
|
+
bind(info.name, { kind: 'function', scope: info.inComponentBody ? 'component' : 'module', info })
|
|
83
|
+
}
|
|
84
|
+
for (const info of b.localConstants) bind(info.name, { kind: 'const', scope: info.isModule ? 'module' : 'component', info })
|
|
85
|
+
for (const info of b.memos) bind(info.name, { kind: 'memo', scope: info.isModule ? 'module' : 'component', info })
|
|
86
|
+
for (const info of b.signals) {
|
|
87
|
+
const scope = info.isModule ? 'module' : 'component'
|
|
88
|
+
bind(info.getter, { kind: 'signal', scope, info })
|
|
89
|
+
if (info.setter) bind(info.setter, { kind: 'opaque', scope })
|
|
90
|
+
}
|
|
91
|
+
const props = [b.propsObjectName, b.restPropsName, ...templatePropReadParams(b).map(p => p.name)]
|
|
92
|
+
for (const name of props) if (name) bind(name, { kind: 'opaque', scope: 'component' })
|
|
93
|
+
return (name, from) => (from === 'component' ? scopes.component.get(name) : undefined) ?? scopes.module.get(name) ?? null
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* What a zero-argument call of `binding` is to a template: `inline` with the
|
|
98
|
+
* expression the function returns, `refuse` for a function a template
|
|
99
|
+
* cannot inline (one with parameters, a body other than a single `return`,
|
|
100
|
+
* an `async` / generator one, a reassigned arrow `const`), or `null` for a
|
|
101
|
+
* binding that is not such a function. A function returning JSX is inlined
|
|
102
|
+
* into the markup before any template reads it, and is `null` too.
|
|
103
|
+
*/
|
|
104
|
+
export function localCall(binding: LocalBinding | null): { kind: 'inline'; body: ParsedExpr } | { kind: 'refuse' } | null {
|
|
105
|
+
if (binding?.kind === 'function') {
|
|
106
|
+
const f = binding.info
|
|
107
|
+
if (f.isJsxFunction || f.containsJsx || f.isMultiReturnJsxHelper) return null
|
|
108
|
+
return f.returnParsed ? { kind: 'inline', body: f.returnParsed } : { kind: 'refuse' }
|
|
109
|
+
}
|
|
110
|
+
if (binding?.kind !== 'const') return null
|
|
111
|
+
const c = binding.info
|
|
112
|
+
const body = arrowConstBody(c)
|
|
113
|
+
if (body) return { kind: 'inline', body }
|
|
114
|
+
if (c.declarationKind !== 'const' || c.parsed?.kind !== 'arrow' || c.isJsxFunction || c.isJsx) return null
|
|
115
|
+
return { kind: 'refuse' }
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* Each function name a zero-argument call written in `from` reads as a
|
|
120
|
+
* function whose returned expression a template can inline, with that
|
|
121
|
+
* expression ({@link inlinedBody}).
|
|
122
|
+
*/
|
|
123
|
+
export function localCallBodies(b: LocalCallBindings, from: LocalScope = 'component'): ReadonlyMap<string, LocalCallBody> {
|
|
124
|
+
const resolve = localBindings(b)
|
|
125
|
+
const out = new Map<string, LocalCallBody>()
|
|
126
|
+
for (const name of callableNames(b)) {
|
|
127
|
+
const binding = resolve(name, from)
|
|
128
|
+
const body = binding && inlinedBody(binding, resolve, new Set())
|
|
129
|
+
if (binding && body) out.set(name, { body, scope: binding.scope })
|
|
130
|
+
}
|
|
131
|
+
return out
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/**
|
|
135
|
+
* Each function name a zero-argument call written in `from` reads as a
|
|
136
|
+
* function a template cannot inline: {@link localCall}'s `refuse`, or a
|
|
137
|
+
* module function whose returned expression calls one. Read as a getter,
|
|
138
|
+
* such a call would render an unbound name or a prop of that name, so a
|
|
139
|
+
* template refuses it.
|
|
140
|
+
*/
|
|
141
|
+
export function uninlinableLocalCalls(b: LocalCallBindings, from: LocalScope = 'component'): ReadonlySet<string> {
|
|
142
|
+
const resolve = localBindings(b)
|
|
143
|
+
const out = new Set<string>()
|
|
144
|
+
for (const name of callableNames(b)) {
|
|
145
|
+
const binding = resolve(name, from)
|
|
146
|
+
if (binding && localCall(binding) && !inlinedBody(binding, resolve, new Set())) out.add(name)
|
|
147
|
+
}
|
|
148
|
+
return out
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
/**
|
|
152
|
+
* The expression a zero-argument call of `binding` is inlined as, or `null`
|
|
153
|
+
* when a template cannot inline it.
|
|
154
|
+
*
|
|
155
|
+
* A component function's returned expression is read where the call is
|
|
156
|
+
* written (these tables serve the component scope), so it stands as it is.
|
|
157
|
+
* A module function's is not: each local function it calls is the module's,
|
|
158
|
+
* which a component declaration of the same name would replace once the
|
|
159
|
+
* expression sits in the component scope. Those calls are inlined here, in
|
|
160
|
+
* module scope, and one a template cannot inline (or a function calling
|
|
161
|
+
* itself) makes the caller uninlinable too.
|
|
162
|
+
*/
|
|
163
|
+
function inlinedBody(binding: LocalBinding, resolve: LocalBindingResolver, running: ReadonlySet<LocalBinding>): ParsedExpr | null {
|
|
164
|
+
const call = localCall(binding)
|
|
165
|
+
if (call?.kind !== 'inline' || running.has(binding)) return null
|
|
166
|
+
if (binding.scope === 'component') return call.body
|
|
167
|
+
const inner = new Set(running).add(binding)
|
|
168
|
+
let inlinable = true
|
|
169
|
+
const body = mapParsedExpr(call.body, (e, enclosing) => {
|
|
170
|
+
if (e.kind !== 'call' || e.args.length > 0 || e.callee.kind !== 'identifier' || enclosing.has(e.callee.name)) return null
|
|
171
|
+
const callee = resolve(e.callee.name, 'module')
|
|
172
|
+
if (!callee || !localCall(callee)) return null
|
|
173
|
+
const inlined = inlinedBody(callee, resolve, inner)
|
|
174
|
+
// Inlined under a callback param of one of its names, the expression would read the param.
|
|
175
|
+
const free = inlined && freeIdentifiers(inlined)
|
|
176
|
+
if (!inlined || !free || [...free].some(name => enclosing.has(name))) inlinable = false
|
|
177
|
+
return inlined
|
|
178
|
+
})
|
|
179
|
+
return inlinable ? body : null
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
/** Every name a function declaration or a const binds, in either scope. */
|
|
183
|
+
function callableNames(b: LocalCallBindings): Set<string> {
|
|
184
|
+
return new Set([...b.localFunctions.map(f => f.name), ...b.localConstants.map(c => c.name)])
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
/** The body of a zero-param arrow `const` that is never reassigned, or `null`. */
|
|
188
|
+
function arrowConstBody(c: ConstantInfo): ParsedExpr | null {
|
|
189
|
+
if (c.declarationKind !== 'const' || c.mutatedAfterDeclaration || !c.parsed) return null
|
|
190
|
+
return zeroArgCallBody(c.parsed)
|
|
191
|
+
}
|
|
@@ -242,6 +242,16 @@ export interface NullishAttrContext {
|
|
|
242
242
|
* ToNumber unless both sides are strings, so its nil's reading decides the
|
|
243
243
|
* comparison (`undefined < 1` is false, `null < 1` is true). */
|
|
244
244
|
readonly nullableScalarProps?: ReadonlyMap<string, NullableScalarProp>
|
|
245
|
+
/**
|
|
246
|
+
* Whether the value a read names at the attribute site is declared to
|
|
247
|
+
* admit `undefined` / `null` (#3728): a loop row's optional member
|
|
248
|
+
* (`r.tone` where the element type has `tone?: string`, a destructured
|
|
249
|
+
* `tone`, `r.meta.tone`). The adapter answers it from the declared types
|
|
250
|
+
* through its live `BindingScope` (`declaredValueMayBeNil`). Consulted for
|
|
251
|
+
* the attribute's own reads only, never inside a memo body, whose names
|
|
252
|
+
* resolve where the memo is declared.
|
|
253
|
+
*/
|
|
254
|
+
readonly declaredNil?: (expr: ParsedExpr) => boolean
|
|
245
255
|
}
|
|
246
256
|
|
|
247
257
|
/** A string or boolean prop's nil reading ({@link NullishAttrContext.nullableScalarProps}). */
|
|
@@ -360,44 +370,56 @@ export function attrValueMayBeNullish(
|
|
|
360
370
|
// `shadowed` is the scope the expression is read in: the attribute site's
|
|
361
371
|
// loop bindings at the top level, none inside a memo body — its getters
|
|
362
372
|
// resolve where the memo is declared, not where it is read.
|
|
363
|
-
|
|
373
|
+
// `atSite`: the expression is the attribute's own, read in the attribute
|
|
374
|
+
// site's scope, where the declared-type answer (`declaredNil`) applies.
|
|
375
|
+
const walk = (expr: ParsedExpr, shadowed: (name: string) => boolean, atSite = true): boolean => {
|
|
364
376
|
if (readsUndefinedLeaf(expr, ctx, shadowed)) return true
|
|
377
|
+
if (atSite && ctx.declaredNil !== undefined && isPlainRead(expr) && ctx.declaredNil(expr)) return true
|
|
365
378
|
switch (expr.kind) {
|
|
366
379
|
case 'identifier':
|
|
367
380
|
return expr.name === 'undefined'
|
|
368
381
|
case 'literal':
|
|
369
382
|
return expr.literalType === 'null'
|
|
370
|
-
case 'call':
|
|
371
|
-
|
|
372
|
-
if (getter === null || shadowed(getter)) return false
|
|
373
|
-
if (ctx.nullableSignals.has(getter)) return true
|
|
374
|
-
const memo = ctx.memos.get(getter)
|
|
375
|
-
if (!memo || visiting.has(getter)) return false
|
|
376
|
-
if (admitsNullish(memo.type)) return true
|
|
377
|
-
visiting.add(getter)
|
|
378
|
-
const result = memo.parsed !== undefined && walk(memo.parsed, () => false)
|
|
379
|
-
visiting.delete(getter)
|
|
380
|
-
return result
|
|
381
|
-
}
|
|
383
|
+
case 'call':
|
|
384
|
+
return getterMayBeNullish(expr, shadowed)
|
|
382
385
|
// A computed key or an index (`pair[1]`, `row[key]`) reads `undefined`
|
|
383
386
|
// when it is out of range or absent (#3497).
|
|
384
387
|
case 'member':
|
|
385
|
-
return expr.optional || expr.computed || walk(expr.object, shadowed)
|
|
388
|
+
return expr.optional || expr.computed || walk(expr.object, shadowed, atSite)
|
|
386
389
|
case 'index-access':
|
|
387
390
|
return true
|
|
388
391
|
case 'conditional':
|
|
389
|
-
return walk(expr.consequent, shadowed) || walk(expr.alternate, shadowed)
|
|
392
|
+
return walk(expr.consequent, shadowed, atSite) || walk(expr.alternate, shadowed, atSite)
|
|
390
393
|
case 'logical':
|
|
391
394
|
return expr.op === '&&'
|
|
392
|
-
? walk(expr.left, shadowed) || walk(expr.right, shadowed)
|
|
393
|
-
: walk(expr.right, shadowed)
|
|
395
|
+
? walk(expr.left, shadowed, atSite) || walk(expr.right, shadowed, atSite)
|
|
396
|
+
: walk(expr.right, shadowed, atSite)
|
|
394
397
|
default:
|
|
395
398
|
return false
|
|
396
399
|
}
|
|
397
400
|
}
|
|
401
|
+
// A nullable signal's getter, or a memo whose type or body can be nullish.
|
|
402
|
+
const getterMayBeNullish = (expr: ParsedExpr, shadowed: (name: string) => boolean): boolean => {
|
|
403
|
+
const getter = bareGetterCallName(expr)
|
|
404
|
+
if (getter === null || shadowed(getter)) return false
|
|
405
|
+
if (ctx.nullableSignals.has(getter)) return true
|
|
406
|
+
const memo = ctx.memos.get(getter)
|
|
407
|
+
if (!memo || visiting.has(getter)) return false
|
|
408
|
+
if (admitsNullish(memo.type)) return true
|
|
409
|
+
visiting.add(getter)
|
|
410
|
+
const result = memo.parsed !== undefined && walk(memo.parsed, () => false, false)
|
|
411
|
+
visiting.delete(getter)
|
|
412
|
+
return result
|
|
413
|
+
}
|
|
398
414
|
return parsed !== undefined && walk(parsed, isShadowed)
|
|
399
415
|
}
|
|
400
416
|
|
|
417
|
+
/** A bare name or a non-computed member path over one (`tone`, `r.tone`, `r.meta.tone`). */
|
|
418
|
+
function isPlainRead(expr: ParsedExpr): boolean {
|
|
419
|
+
if (expr.kind === 'identifier') return true
|
|
420
|
+
return expr.kind === 'member' && !expr.computed && isPlainRead(expr.object)
|
|
421
|
+
}
|
|
422
|
+
|
|
401
423
|
/** A method call that can miss (`methodMayReturnUndefined`), or a read of a prop that can be absent. */
|
|
402
424
|
function readsUndefinedLeaf(expr: ParsedExpr, ctx: NullishAttrContext, shadowed: (name: string) => boolean): boolean {
|
|
403
425
|
return methodMayReturnUndefined(expr) || readsNullableProp(expr, ctx, shadowed)
|