@barefootjs/jsx 0.39.2 → 0.39.3
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/line-statement-text.d.ts +17 -0
- package/dist/adapters/line-statement-text.d.ts.map +1 -0
- package/dist/analyzer.d.ts +1 -0
- package/dist/analyzer.d.ts.map +1 -1
- package/dist/augment-inherited-props.d.ts +20 -0
- package/dist/augment-inherited-props.d.ts.map +1 -1
- package/dist/html-types.d.ts +15 -0
- package/dist/html-types.d.ts.map +1 -1
- package/dist/index.d.ts +5 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +260 -91
- package/dist/ir-to-client-js/collect-elements.d.ts.map +1 -1
- package/dist/ir-to-client-js/utils.d.ts.map +1 -1
- package/dist/nullable-signal-attr.d.ts +73 -0
- package/dist/nullable-signal-attr.d.ts.map +1 -0
- package/dist/scope/root-prop-alias.d.ts +43 -0
- package/dist/scope/root-prop-alias.d.ts.map +1 -0
- package/package.json +2 -2
- package/src/__tests__/__snapshots__/doc-examples.test.ts.snap +3 -3
- package/src/__tests__/attribute-fallback-wrap.test.ts +24 -0
- package/src/__tests__/compiler-stress-1244.test.ts +10 -13
- package/src/__tests__/conditional-fallback-wrap.test.ts +20 -0
- package/src/__tests__/html-event-handler-names.test.ts +107 -0
- package/src/__tests__/line-statement-text.test.ts +25 -0
- package/src/__tests__/lookup-literal-const.test.ts +46 -0
- package/src/__tests__/loop-param-accessor-key-member-safety.test.ts +66 -19
- package/src/__tests__/nullable-signal-attr.test.ts +124 -0
- package/src/__tests__/profile-binding-ids.test.ts +102 -0
- package/src/__tests__/root-prop-alias.test.ts +62 -0
- package/src/adapters/line-statement-text.ts +24 -0
- package/src/analyzer.ts +50 -26
- package/src/augment-inherited-props.ts +49 -0
- package/src/html-types.ts +15 -0
- package/src/index.ts +13 -2
- package/src/ir-to-client-js/collect-elements.ts +7 -1
- package/src/ir-to-client-js/control-flow/plan/build-component-loop.ts +2 -2
- package/src/ir-to-client-js/stringify/declaration-emit.ts +5 -5
- package/src/ir-to-client-js/utils.ts +88 -117
- package/src/jsx-to-ir.ts +7 -0
- package/src/nullable-signal-attr.ts +156 -0
- package/src/scope/root-prop-alias.ts +81 -0
|
@@ -9,12 +9,10 @@ import type { TopLevelLoop, BranchLoop, LoopOffset } from './types.ts'
|
|
|
9
9
|
import { isNonReferenceIdentifierPosition } from '../analyzer.ts'
|
|
10
10
|
import { buildLoopChainExpr } from '../loop-chain.ts'
|
|
11
11
|
import { templatePartsToJsExpr } from '../template-parts.ts'
|
|
12
|
-
import { escapeIdentifierForRegex, ID_BOUNDARY_BEFORE, ID_BOUNDARY_AFTER } from '../identifier-pattern.ts'
|
|
13
12
|
import {
|
|
14
13
|
iterateJsTokens,
|
|
15
14
|
isIdentifierLikeToken,
|
|
16
15
|
isTriviaKind,
|
|
17
|
-
replaceInExprContexts,
|
|
18
16
|
} from '../scanner/js-scanner.ts'
|
|
19
17
|
import {
|
|
20
18
|
BF_KEY as DATA_KEY,
|
|
@@ -756,8 +754,9 @@ export function wrapIndexParamAsAccessor(expr: string, indexParam: string): stri
|
|
|
756
754
|
* merely SPELLS `name` without referencing it — an object-literal key
|
|
757
755
|
* (`{ name: 1 }`), a shorthand property (`{ name }`, expanded to
|
|
758
756
|
* `{ name: name() }` in one edit — the same transformation #1244 gave the
|
|
759
|
-
* destructured-binding path
|
|
760
|
-
*
|
|
757
|
+
* destructured-binding path), and a member/property name (`obj.name`,
|
|
758
|
+
* `obj?.name`). The walk itself is `rewriteIdentifierReferences`, shared
|
|
759
|
+
* with that destructured-binding path (#3260).
|
|
761
760
|
*
|
|
762
761
|
* The previous implementation was a `\bname\b`-style regex
|
|
763
762
|
* (`ID_BOUNDARY_BEFORE`/`AFTER` only know about identifier-character
|
|
@@ -765,9 +764,8 @@ export function wrapIndexParamAsAccessor(expr: string, indexParam: string): stri
|
|
|
765
764
|
* a key or shorthand rewrite produces invalid JS (`{ name(): 1 }`,
|
|
766
765
|
* `{ name() }`) that fails at parse time; a member-name rewrite produces
|
|
767
766
|
* valid-but-wrong JS (`obj.name()`) that throws `TypeError: obj.name is not
|
|
768
|
-
* a function` at row-creation time (#3239). Parsing the real AST
|
|
769
|
-
*
|
|
770
|
-
* those positions a type of AST node this function can name and skip,
|
|
767
|
+
* a function` at row-creation time (#3239). Parsing the real AST makes each
|
|
768
|
+
* of those positions a type of AST node this function can name and skip,
|
|
771
769
|
* instead of a regex guessing from surrounding characters.
|
|
772
770
|
*
|
|
773
771
|
* A nested declaration that re-binds `name` (a parameter, a `const`/`let`,
|
|
@@ -800,25 +798,67 @@ export function wrapIndexParamAsAccessor(expr: string, indexParam: string): stri
|
|
|
800
798
|
* references reachable from within or after it).
|
|
801
799
|
*/
|
|
802
800
|
function rewriteIdentifierAsAccessor(expr: string, name: string): string {
|
|
803
|
-
|
|
804
|
-
|
|
805
|
-
|
|
801
|
+
return rewriteIdentifierReferences(expr, new Map([[name, `${name}()`]]), { skipCallee: true })
|
|
802
|
+
}
|
|
803
|
+
|
|
804
|
+
/**
|
|
805
|
+
* The AST walk shared by `rewriteIdentifierAsAccessor` (one name → `name()`)
|
|
806
|
+
* and `rewriteLoopBindingRefs` (each destructured binding → its
|
|
807
|
+
* `renderLoopBindingAccess` form): replace every genuine value reference to
|
|
808
|
+
* a name in `replacements` with its replacement text, in one pass. See
|
|
809
|
+
* `rewriteIdentifierAsAccessor`'s docstring for which positions count as a
|
|
810
|
+
* reference and why; both callers answer that question here, so they cannot
|
|
811
|
+
* drift apart (#3260).
|
|
812
|
+
*
|
|
813
|
+
* `skipCallee` leaves `name()` / `new name()` and a `function name()`
|
|
814
|
+
* declaration's own name alone. It is the accessor rewrite's no-double-wrap
|
|
815
|
+
* rule (#2592): there `name()` already IS the accessor call, and the old
|
|
816
|
+
* regex's "followed by (" lookahead skipped all three. A destructured
|
|
817
|
+
* binding has no such shape — `handler()` with `({ handler })` calls the
|
|
818
|
+
* item's field, so it must become `__bfItem().handler()` — and its caller
|
|
819
|
+
* passes `false`, which also leaves a shadowing function declaration to
|
|
820
|
+
* fail loudly, as it did under the old destructured regex.
|
|
821
|
+
*
|
|
822
|
+
* One pass over the AST, so a replacement is never re-scanned: a binding
|
|
823
|
+
* `a` with path `.a` cannot cascade into `__bfItem().__bfItem().a`.
|
|
824
|
+
*/
|
|
825
|
+
function rewriteIdentifierReferences(
|
|
826
|
+
expr: string,
|
|
827
|
+
replacements: ReadonlyMap<string, string>,
|
|
828
|
+
opts: { skipCallee: boolean },
|
|
829
|
+
): string {
|
|
830
|
+
// Fast path: no name occurs in the text at all → nothing to rewrite. A
|
|
831
|
+
// cheap substring check only ever causes an unnecessary parse below
|
|
832
|
+
// (e.g. a name occurs only as part of a longer identifier), never a wrong
|
|
806
833
|
// answer, since the AST walk re-checks the exact identifier text.
|
|
807
|
-
|
|
834
|
+
let any = false
|
|
835
|
+
for (const name of replacements.keys()) {
|
|
836
|
+
if (expr.includes(name)) { any = true; break }
|
|
837
|
+
}
|
|
838
|
+
if (!any) return expr
|
|
808
839
|
|
|
809
840
|
const wrapped = `(${expr})`
|
|
810
841
|
const sf = ts.createSourceFile('__bf_expr.ts', wrapped, ts.ScriptTarget.Latest, /* setParentNodes */ true)
|
|
811
842
|
const edits: Array<{ start: number; end: number; replacement: string }> = []
|
|
812
843
|
|
|
813
|
-
const visit = (node: ts.Node): void => {
|
|
844
|
+
const visit = (node: ts.Node, active: ReadonlyMap<string, string>): void => {
|
|
814
845
|
// A named function expression's own name is scoped to exactly its own
|
|
815
|
-
// body, so nothing reachable inside it can refer to the outer
|
|
816
|
-
//
|
|
817
|
-
// identifier (see
|
|
818
|
-
// `class` DECLARATION can't get the same
|
|
819
|
-
|
|
846
|
+
// body, so nothing reachable inside it can refer to the outer binding of
|
|
847
|
+
// that name — drop it for the whole subtree instead of only its
|
|
848
|
+
// declaration-name identifier (see `rewriteIdentifierAsAccessor`'s
|
|
849
|
+
// docstring for why a `function`/`class` DECLARATION can't get the same
|
|
850
|
+
// treatment this cheaply). Other names stay active inside.
|
|
851
|
+
if (ts.isFunctionExpression(node) && node.name && active.has(node.name.text)) {
|
|
852
|
+
const inner = new Map(active)
|
|
853
|
+
inner.delete(node.name.text)
|
|
854
|
+
if (inner.size === 0) return
|
|
855
|
+
ts.forEachChild(node, child => visit(child, inner))
|
|
856
|
+
return
|
|
857
|
+
}
|
|
820
858
|
|
|
821
|
-
if (ts.isIdentifier(node)
|
|
859
|
+
if (ts.isIdentifier(node)) {
|
|
860
|
+
const replacement = active.get(node.text)
|
|
861
|
+
if (replacement === undefined) return
|
|
822
862
|
const p = node.parent
|
|
823
863
|
// Pure-key / non-reference positions — never rewrite. Shared with
|
|
824
864
|
// `analyzer.ts`'s factory-rename `classify()`, which answers the same
|
|
@@ -834,26 +874,33 @@ function rewriteIdentifierAsAccessor(expr: string, name: string): string {
|
|
|
834
874
|
// old regex did NOT protect it either — wrapping it is a pre-existing
|
|
835
875
|
// syntax break, not a regression, so it's left to the shadowing-decl
|
|
836
876
|
// path below like any other declaration name.
|
|
837
|
-
|
|
838
|
-
|
|
839
|
-
|
|
877
|
+
//
|
|
878
|
+
// Accessor mode only: the old destructured-binding regex had no such
|
|
879
|
+
// exemption, so there a shadowing `function color() {}` broke the
|
|
880
|
+
// declaration loudly. Skipping its name here would keep the local
|
|
881
|
+
// function while rewriting its calls to `__bfItem().color()` — valid
|
|
882
|
+
// JS that calls the wrong value (#3260 review).
|
|
883
|
+
if (opts.skipCallee) {
|
|
884
|
+
if (ts.isFunctionDeclaration(p) && p.name === node) return
|
|
885
|
+
if (ts.isCallExpression(p) && p.expression === node) return // already `name()` — no double-wrap (#2592)
|
|
886
|
+
if (ts.isNewExpression(p) && p.expression === node) return // `new name()` — same "followed by (" shape as a call
|
|
887
|
+
}
|
|
840
888
|
|
|
841
|
-
// Shorthand property (`{ name }`) expands key +
|
|
842
|
-
// edit
|
|
843
|
-
//
|
|
844
|
-
//
|
|
845
|
-
//
|
|
846
|
-
//
|
|
847
|
-
// silently misrewritten.
|
|
889
|
+
// Shorthand property (`{ name }`) expands key + rewrites value in one
|
|
890
|
+
// edit (#1244). Every other reachable position here is a genuine
|
|
891
|
+
// value reference — OR a shadowing declaration name (nested
|
|
892
|
+
// parameter/`const`/`let`/destructured local): both get rewritten,
|
|
893
|
+
// per `rewriteIdentifierAsAccessor`'s docstring on why a shadow is
|
|
894
|
+
// left to fail loudly rather than silently misrewritten.
|
|
848
895
|
const isShorthand = ts.isShorthandPropertyAssignment(p) && p.name === node
|
|
849
896
|
const start = node.getStart(sf) - 1
|
|
850
897
|
const end = node.getEnd() - 1
|
|
851
|
-
edits.push({ start, end, replacement: isShorthand ? `${
|
|
898
|
+
edits.push({ start, end, replacement: isShorthand ? `${node.text}: ${replacement}` : replacement })
|
|
852
899
|
return
|
|
853
900
|
}
|
|
854
|
-
ts.forEachChild(node, visit)
|
|
901
|
+
ts.forEachChild(node, child => visit(child, active))
|
|
855
902
|
}
|
|
856
|
-
visit(sf)
|
|
903
|
+
visit(sf, replacements)
|
|
857
904
|
|
|
858
905
|
if (edits.length === 0) return expr
|
|
859
906
|
// Apply right-to-left so earlier offsets stay valid as later ones grow.
|
|
@@ -867,99 +914,23 @@ function rewriteIdentifierAsAccessor(expr: string, name: string): string {
|
|
|
867
914
|
|
|
868
915
|
/**
|
|
869
916
|
* Rewrite each reference to a destructured loop-param binding in
|
|
870
|
-
* `expr` to the corresponding `${accessor}${path}` form
|
|
917
|
+
* `expr` to the corresponding `${accessor}${path}` form
|
|
918
|
+
* (`renderLoopBindingAccess`).
|
|
871
919
|
*
|
|
872
|
-
*
|
|
873
|
-
*
|
|
874
|
-
* `
|
|
875
|
-
*
|
|
876
|
-
*
|
|
877
|
-
* `expandShorthandBindings` runs first so object-literal shorthand
|
|
878
|
-
* references (`{ name }`) are lifted to a string-key form before
|
|
879
|
-
* the identifier regex hits them — otherwise the rewrite would land
|
|
880
|
-
* `${accessor}` in a position JS reserves for bare identifiers
|
|
881
|
-
* (`{ __bfItem().name }` ⇒ SyntaxError). (#1244)
|
|
920
|
+
* Shares `rewriteIdentifierReferences`'s AST walk with the plain-param
|
|
921
|
+
* accessor rewrite, so an object-literal key (`{ color: 1 }`) or a member
|
|
922
|
+
* name (`obj.color`) that merely spells a binding name stays as written
|
|
923
|
+
* (#3260), and a shorthand property (`{ color }`) becomes
|
|
924
|
+
* `{ color: __bfItem().color }` (#1244).
|
|
882
925
|
*/
|
|
883
926
|
function rewriteLoopBindingRefs(
|
|
884
927
|
expr: string,
|
|
885
928
|
bindings: readonly LoopParamBinding[],
|
|
886
929
|
accessor: string,
|
|
887
930
|
): string {
|
|
888
|
-
const
|
|
889
|
-
for (const b of bindings)
|
|
890
|
-
|
|
891
|
-
const alt = bindings.map(b => escapeIdentifierForRegex(b.name)).join('|')
|
|
892
|
-
const re = new RegExp(`${ID_BOUNDARY_BEFORE}(${alt})${ID_BOUNDARY_AFTER}`, 'gu')
|
|
893
|
-
return replaceInExprContexts(preprocessed, re, (_m: string, name: string) =>
|
|
894
|
-
renderLoopBindingAccess(byName.get(name)!, accessor),
|
|
895
|
-
)
|
|
896
|
-
}
|
|
897
|
-
|
|
898
|
-
/**
|
|
899
|
-
* Expand object-literal shorthand properties whose name matches a
|
|
900
|
-
* destructured loop-param binding into the equivalent
|
|
901
|
-
* `"name": name` form (string-literal key + identifier value).
|
|
902
|
-
*
|
|
903
|
-
* Why: JS only accepts a bare identifier as a shorthand property
|
|
904
|
-
* name, so the downstream `${accessor}` rewrite of `{ color }` would
|
|
905
|
-
* produce `{ __bfItem().color }` — a SyntaxError that takes down the
|
|
906
|
-
* whole compiled component at module load time, before any runtime
|
|
907
|
-
* code runs. (#1244)
|
|
908
|
-
*
|
|
909
|
-
* Why string-literal key (not `name: name`): the subsequent
|
|
910
|
-
* identifier-replacement regex (`replaceInExprContexts`) skips string
|
|
911
|
-
* literal contents, so the key stays as the literal `"color"` while
|
|
912
|
-
* the value-position `color` gets rewritten to `__bfItem().color`.
|
|
913
|
-
* The alternative `name: name` would have the regex match BOTH
|
|
914
|
-
* occurrences and produce `{ __bfItem().color: __bfItem().color }`
|
|
915
|
-
* — same SyntaxError, just one indirection deeper.
|
|
916
|
-
*
|
|
917
|
-
* AST-based detection (TS `ShorthandPropertyAssignment`) is
|
|
918
|
-
* intentionally chosen over character-by-character lookbehind on the
|
|
919
|
-
* raw expression text — the codebase's recurring "hand-rolled JS
|
|
920
|
-
* source scanners" pattern (#1244 §D) explicitly flags that approach
|
|
921
|
-
* as drift-prone (computed keys, comments, nested template literals
|
|
922
|
-
* all need separate handling). The TS scanner already knows what a
|
|
923
|
-
* shorthand prop is.
|
|
924
|
-
*
|
|
925
|
-
* The `(${expr})` wrap forces TS to parse a bare object literal as
|
|
926
|
-
* an expression — otherwise `{ color }` would be a block statement
|
|
927
|
-
* containing an expression statement, with no `ShorthandPropertyAssignment`
|
|
928
|
-
* node to find.
|
|
929
|
-
*/
|
|
930
|
-
function expandShorthandBindings(expr: string, bindingNames: ReadonlySet<string>): string {
|
|
931
|
-
// Fast path: no `{` means no object literal means no shorthand to
|
|
932
|
-
// expand. Skip the parser cost on the common case.
|
|
933
|
-
if (!expr.includes('{')) return expr
|
|
934
|
-
const wrapped = `(${expr})`
|
|
935
|
-
const sf = ts.createSourceFile('__bf_expr.ts', wrapped, ts.ScriptTarget.Latest, /* setParentNodes */ true)
|
|
936
|
-
const edits: Array<{ start: number; end: number; replacement: string }> = []
|
|
937
|
-
const visit = (node: ts.Node): void => {
|
|
938
|
-
if (
|
|
939
|
-
ts.isShorthandPropertyAssignment(node)
|
|
940
|
-
&& ts.isIdentifier(node.name)
|
|
941
|
-
&& bindingNames.has(node.name.text)
|
|
942
|
-
) {
|
|
943
|
-
// `node` spans just the bare identifier on the shorthand entry.
|
|
944
|
-
// Position offsets are in `wrapped`; subtract the leading `(`
|
|
945
|
-
// to map back to `expr`.
|
|
946
|
-
const start = node.getStart(sf) - 1
|
|
947
|
-
const end = node.getEnd() - 1
|
|
948
|
-
const name = node.name.text
|
|
949
|
-
edits.push({ start, end, replacement: `${JSON.stringify(name)}: ${name}` })
|
|
950
|
-
}
|
|
951
|
-
ts.forEachChild(node, visit)
|
|
952
|
-
}
|
|
953
|
-
visit(sf)
|
|
954
|
-
if (edits.length === 0) return expr
|
|
955
|
-
// Apply right-to-left so earlier offsets stay valid as later ones
|
|
956
|
-
// grow.
|
|
957
|
-
edits.sort((a, b) => b.start - a.start)
|
|
958
|
-
let out = expr
|
|
959
|
-
for (const e of edits) {
|
|
960
|
-
out = out.slice(0, e.start) + e.replacement + out.slice(e.end)
|
|
961
|
-
}
|
|
962
|
-
return out
|
|
931
|
+
const replacements = new Map<string, string>()
|
|
932
|
+
for (const b of bindings) replacements.set(b.name, renderLoopBindingAccess(b, accessor))
|
|
933
|
+
return rewriteIdentifierReferences(expr, replacements, { skipCallee: false })
|
|
963
934
|
}
|
|
964
935
|
|
|
965
936
|
/**
|
package/src/jsx-to-ir.ts
CHANGED
|
@@ -9203,6 +9203,13 @@ function hasReactiveAttributes(attrs: IRAttribute[], ctx: TransformContext): boo
|
|
|
9203
9203
|
if (isSignalOrMemoReference(valueToCheck, ctx) || isPropsReference(valueToCheck, ctx)) {
|
|
9204
9204
|
return true
|
|
9205
9205
|
}
|
|
9206
|
+
// The Solid-style wrap-by-default fallback (#940): an attribute with a
|
|
9207
|
+
// call the analyzer can't prove non-reactive (`hidden={v().hide}`,
|
|
9208
|
+
// `v` reading an untyped or class-held store) gets a createEffect from
|
|
9209
|
+
// `decideWrapForAttr`. That effect addresses the element by slot id,
|
|
9210
|
+
// so the element needs one even with no other dynamic content —
|
|
9211
|
+
// otherwise the attribute is never applied on the client (#3266).
|
|
9212
|
+
if (attr.callsReactiveGetters || attr.hasFunctionCalls) return true
|
|
9206
9213
|
// Check if attribute references any active loop-bound VALUE names —
|
|
9207
9214
|
// loop root elements need a slotId so className can be updated
|
|
9208
9215
|
// reactively. REACTIVITY/slotId classifier — value bindings only
|
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Nullish-attribute omission for signal reads (#3304).
|
|
3
|
+
*
|
|
4
|
+
* Hono omits an attribute whose value is `undefined` or `null`
|
|
5
|
+
* (`title={s()}` with `s()` undefined renders no `title`). A DSL adapter
|
|
6
|
+
* emits `name="<value>"` unconditionally, so a nil value renders
|
|
7
|
+
* `title=""` unless the adapter guards the attribute on a nil check. Each
|
|
8
|
+
* DSL adapter already guards a bare reference to a no-default optional prop
|
|
9
|
+
* that way; this module is the one decision for the signal half, which
|
|
10
|
+
* every adapter consults instead of re-deriving it.
|
|
11
|
+
*
|
|
12
|
+
* Only the SSR value matters here — a signal's SSR value is its initial
|
|
13
|
+
* value — and the guard is presence-only: a non-nil value renders exactly
|
|
14
|
+
* as it did unguarded, so over-including a signal never changes output for
|
|
15
|
+
* a non-nil value.
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
import type { ParsedExpr } from './expression-parser.ts'
|
|
19
|
+
import type { ComponentIR, MemoInfo, SignalInfo, TypeInfo } from './types.ts'
|
|
20
|
+
|
|
21
|
+
function isNullishType(type: TypeInfo): boolean {
|
|
22
|
+
return type.kind === 'primitive' && (type.primitive === 'undefined' || type.primitive === 'null')
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/** `T | undefined` / `T | null`, or bare `undefined` / `null`. */
|
|
26
|
+
function admitsNullish(type: TypeInfo): boolean {
|
|
27
|
+
if (isNullishType(type)) return true
|
|
28
|
+
return type.kind === 'union' && (type.unionTypes ?? []).some(isNullishType)
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/** `undefined` / `null` as a parsed initial value. */
|
|
32
|
+
export function isNullishLiteral(expr: ParsedExpr | undefined): boolean {
|
|
33
|
+
if (!expr) return false
|
|
34
|
+
if (expr.kind === 'identifier') return expr.name === 'undefined'
|
|
35
|
+
return expr.kind === 'literal' && expr.literalType === 'null'
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Whether the signal's SSR value can be nullish. The witness is its type
|
|
40
|
+
* admitting `undefined`/`null` (a zero-arg `createSignal<T>()` is typed
|
|
41
|
+
* `T | undefined`, #3215), or, for an untyped signal, a literal `undefined` /
|
|
42
|
+
* `null` initial value.
|
|
43
|
+
*/
|
|
44
|
+
export function isNullableSignal(signal: SignalInfo): boolean {
|
|
45
|
+
if (admitsNullish(signal.type)) return true
|
|
46
|
+
return signal.type.kind === 'unknown' && isNullishLiteral(signal.parsed)
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/** Getter names of the component's signals whose SSR value can be nullish. */
|
|
50
|
+
export function collectNullableSignalGetters(ir: ComponentIR): Set<string> {
|
|
51
|
+
return new Set((ir.metadata.signals ?? []).filter(isNullableSignal).map(s => s.getter))
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* The getter an attribute value reads when the whole value is a bare,
|
|
56
|
+
* zero-argument getter call (`s()`); `null` for any other shape. A member
|
|
57
|
+
* read, a ternary or a template literal around the call is not matched:
|
|
58
|
+
* the attribute's presence then depends on more than the signal.
|
|
59
|
+
*/
|
|
60
|
+
export function bareGetterCallName(parsed: ParsedExpr | undefined): string | null {
|
|
61
|
+
if (!parsed || parsed.kind !== 'call' || parsed.args.length !== 0) return null
|
|
62
|
+
return parsed.callee.kind === 'identifier' ? parsed.callee.name : null
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* The signal getter whose nil check should guard this attribute, or `null`
|
|
67
|
+
* when the attribute is not a bare read of a nullable signal. `nullable` is
|
|
68
|
+
* the set `collectNullableSignalGetters` built for the component.
|
|
69
|
+
*/
|
|
70
|
+
export function nullableSignalAttrGetter(
|
|
71
|
+
parsed: ParsedExpr | undefined,
|
|
72
|
+
nullable: ReadonlySet<string>,
|
|
73
|
+
): string | null {
|
|
74
|
+
const getter = bareGetterCallName(parsed)
|
|
75
|
+
return getter !== null && nullable.has(getter) ? getter : null
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* What `attrValueMayBeNullish` needs to know about the component: its
|
|
80
|
+
* nullable signal getters and its memos by name.
|
|
81
|
+
*/
|
|
82
|
+
export interface NullishAttrContext {
|
|
83
|
+
readonly nullableSignals: ReadonlySet<string>
|
|
84
|
+
readonly memos: ReadonlyMap<string, MemoInfo>
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
export function collectNullishAttrContext(ir: ComponentIR): NullishAttrContext {
|
|
88
|
+
return {
|
|
89
|
+
nullableSignals: collectNullableSignalGetters(ir),
|
|
90
|
+
memos: new Map((ir.metadata.memos ?? []).map(m => [m.name, m])),
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* Whether an attribute value's SSR value can be `undefined` / `null`, so a
|
|
96
|
+
* DSL adapter must guard the attribute on a nil check to omit it the way
|
|
97
|
+
* Hono does (#3322) — the derived-value half of the bare-signal guard
|
|
98
|
+
* (`nullableSignalAttrGetter`, #3304). Matches a value that reaches a
|
|
99
|
+
* nullish value through:
|
|
100
|
+
*
|
|
101
|
+
* - a read of a nullable signal or of a memo whose type or body can be
|
|
102
|
+
* nullish (`label()` with `label = createMemo(() => s())`);
|
|
103
|
+
* - an optional member read (`user()?.name`), or a member read of a value
|
|
104
|
+
* that can itself be nullish;
|
|
105
|
+
* - a ternary branch, or an `??` / `||` / `&&` operand the result can be;
|
|
106
|
+
* - a literal `undefined` / `null`.
|
|
107
|
+
*
|
|
108
|
+
* Like the bare-signal guard it is presence-only: a non-nil value renders
|
|
109
|
+
* exactly as unguarded, so over-including a value never changes output for
|
|
110
|
+
* a present value (`''`, `0` and `false` still render). `isShadowed` names
|
|
111
|
+
* a getter a loop binding hides at the attribute site, which is then not
|
|
112
|
+
* the signal / memo; a memo's own body is read in its declaration scope.
|
|
113
|
+
*/
|
|
114
|
+
export function attrValueMayBeNullish(
|
|
115
|
+
parsed: ParsedExpr | undefined,
|
|
116
|
+
ctx: NullishAttrContext,
|
|
117
|
+
isShadowed: (name: string) => boolean = () => false,
|
|
118
|
+
): boolean {
|
|
119
|
+
const visiting = new Set<string>()
|
|
120
|
+
// `shadowed` is the scope the expression is read in: the attribute site's
|
|
121
|
+
// loop bindings at the top level, none inside a memo body — its getters
|
|
122
|
+
// resolve where the memo is declared, not where it is read.
|
|
123
|
+
const walk = (expr: ParsedExpr, shadowed: (name: string) => boolean): boolean => {
|
|
124
|
+
switch (expr.kind) {
|
|
125
|
+
case 'identifier':
|
|
126
|
+
return expr.name === 'undefined'
|
|
127
|
+
case 'literal':
|
|
128
|
+
return expr.literalType === 'null'
|
|
129
|
+
case 'call': {
|
|
130
|
+
const getter = bareGetterCallName(expr)
|
|
131
|
+
if (getter === null || shadowed(getter)) return false
|
|
132
|
+
if (ctx.nullableSignals.has(getter)) return true
|
|
133
|
+
const memo = ctx.memos.get(getter)
|
|
134
|
+
if (!memo || visiting.has(getter)) return false
|
|
135
|
+
if (admitsNullish(memo.type)) return true
|
|
136
|
+
visiting.add(getter)
|
|
137
|
+
const result = memo.parsed !== undefined && walk(memo.parsed, () => false)
|
|
138
|
+
visiting.delete(getter)
|
|
139
|
+
return result
|
|
140
|
+
}
|
|
141
|
+
case 'member':
|
|
142
|
+
return expr.optional || walk(expr.object, shadowed)
|
|
143
|
+
case 'index-access':
|
|
144
|
+
return walk(expr.object, shadowed)
|
|
145
|
+
case 'conditional':
|
|
146
|
+
return walk(expr.consequent, shadowed) || walk(expr.alternate, shadowed)
|
|
147
|
+
case 'logical':
|
|
148
|
+
return expr.op === '&&'
|
|
149
|
+
? walk(expr.left, shadowed) || walk(expr.right, shadowed)
|
|
150
|
+
: walk(expr.right, shadowed)
|
|
151
|
+
default:
|
|
152
|
+
return false
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
return parsed !== undefined && walk(parsed, isShadowed)
|
|
156
|
+
}
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import { collectLoopBoundNames } from '../adapters/loop-bound-names.ts'
|
|
2
|
+
import type { ComponentIR } from '../types.ts'
|
|
3
|
+
import type { BindingScope } from './binding-scope.ts'
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Root-prop aliases for template engines that flatten `props.X` to the bare
|
|
7
|
+
* template variable `X` (#3314). A loop that binds the same name (`.map(value
|
|
8
|
+
* => …)`) shadows that variable for its whole body, so an explicit
|
|
9
|
+
* `props.value` inside the row read the ROW's `value` instead of the prop.
|
|
10
|
+
*
|
|
11
|
+
* The adapter assigns each shadowed prop to an alias just before the loop
|
|
12
|
+
* header — where the bare name still means the prop — and an explicit
|
|
13
|
+
* `props.X` read inside the row uses the alias. Only the template syntax
|
|
14
|
+
* of the assignment is per-adapter; which names need one, and their alias
|
|
15
|
+
* name, are decided here once.
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* The template variable each prop in `propNames` is aliased to: `__bf_root_<name>`,
|
|
20
|
+
* extended with `_` until it names nothing else the component turns into a
|
|
21
|
+
* template variable — a prop, a local const or function, a signal or memo,
|
|
22
|
+
* any loop binding, or another prop's alias. The `__bf_` namespace is
|
|
23
|
+
* reserved only by convention, so an alias must not overwrite a user prop
|
|
24
|
+
* spelled `__bf_root_value` nor be shadowed by a binding of that name.
|
|
25
|
+
* Computed once per component so the assignment before a loop and every
|
|
26
|
+
* `props.X` read inside it agree on the name.
|
|
27
|
+
*/
|
|
28
|
+
export function rootPropAliasNames(ir: ComponentIR, propNames: Iterable<string>): ReadonlyMap<string, string> {
|
|
29
|
+
const { metadata } = ir
|
|
30
|
+
const props = [...propNames]
|
|
31
|
+
const taken = new Set<string>([
|
|
32
|
+
...props,
|
|
33
|
+
...metadata.localConstants.map(c => c.name),
|
|
34
|
+
...metadata.localFunctions.map(f => f.name),
|
|
35
|
+
...metadata.signals.flatMap(s => (s.setter ? [s.getter, s.setter] : [s.getter])),
|
|
36
|
+
...metadata.memos.map(m => m.name),
|
|
37
|
+
...collectLoopBoundNames(ir),
|
|
38
|
+
])
|
|
39
|
+
const aliases = new Map<string, string>()
|
|
40
|
+
for (const name of props) {
|
|
41
|
+
let alias = `__bf_root_${name}`
|
|
42
|
+
while (taken.has(alias)) alias += '_'
|
|
43
|
+
taken.add(alias)
|
|
44
|
+
aliases.set(name, alias)
|
|
45
|
+
}
|
|
46
|
+
return aliases
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Props the loop row newly shadows, with their aliases: names `row` binds
|
|
51
|
+
* that `outer` did not (an enclosing loop that already bound a name already
|
|
52
|
+
* aliased it, and a re-assignment here would capture that loop's row value)
|
|
53
|
+
* and that are component props (keys of `aliases`). In source order of
|
|
54
|
+
* `row`'s bindings.
|
|
55
|
+
*/
|
|
56
|
+
export function rootPropAliasesForLoop(
|
|
57
|
+
outer: BindingScope,
|
|
58
|
+
row: BindingScope,
|
|
59
|
+
aliases: ReadonlyMap<string, string>,
|
|
60
|
+
): { name: string; alias: string }[] {
|
|
61
|
+
const shadowed: { name: string; alias: string }[] = []
|
|
62
|
+
for (const name of row.boundNames()) {
|
|
63
|
+
const alias = aliases.get(name)
|
|
64
|
+
if (alias !== undefined && !outer.isBound(name)) shadowed.push({ name, alias })
|
|
65
|
+
}
|
|
66
|
+
return shadowed
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* The template variable an explicit `props.<name>` read resolves to at a
|
|
71
|
+
* position with `scope`: the alias (from `rootPropAliasNames`) while a loop
|
|
72
|
+
* binding shadows `name`, else `name` itself.
|
|
73
|
+
*/
|
|
74
|
+
export function rootPropReadName(
|
|
75
|
+
name: string,
|
|
76
|
+
scope: BindingScope,
|
|
77
|
+
aliases: ReadonlyMap<string, string>,
|
|
78
|
+
): string {
|
|
79
|
+
if (!scope.isBound(name)) return name
|
|
80
|
+
return aliases.get(name) ?? name
|
|
81
|
+
}
|