@barefootjs/jsx 0.39.1 → 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/adapters/parsed-expr-emitter.d.ts +2 -2
- package/dist/adapters/parsed-expr-emitter.d.ts.map +1 -1
- 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 +6 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1527 -1169
- package/dist/ir-to-client-js/collect-elements.d.ts.map +1 -1
- package/dist/ir-to-client-js/safe-html.d.ts +1 -1
- package/dist/ir-to-client-js/safe-html.d.ts.map +1 -1
- package/dist/ir-to-client-js/utils.d.ts.map +1 -1
- package/dist/jsx-to-ir.d.ts.map +1 -1
- package/dist/literal-member-index.d.ts +6 -0
- package/dist/literal-member-index.d.ts.map +1 -0
- package/dist/nullable-signal-attr.d.ts +73 -0
- package/dist/nullable-signal-attr.d.ts.map +1 -0
- package/dist/scope/binding-scope.d.ts +11 -0
- package/dist/scope/binding-scope.d.ts.map +1 -1
- package/dist/scope/root-prop-alias.d.ts +43 -0
- package/dist/scope/root-prop-alias.d.ts.map +1 -0
- package/dist/signal-prop-name-collision.d.ts +20 -0
- package/dist/signal-prop-name-collision.d.ts.map +1 -0
- package/dist/ssr-defaults.d.ts.map +1 -1
- package/dist/ssr-seed-plan.d.ts +3 -2
- package/dist/ssr-seed-plan.d.ts.map +1 -1
- 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__/binding-scope.test.ts +27 -0
- package/src/__tests__/client-template-escape-soundness.test.ts +17 -0
- package/src/__tests__/compiler-stress-1244.test.ts +10 -13
- package/src/__tests__/conditional-fallback-wrap.test.ts +20 -0
- package/src/__tests__/conditional-nested-loop-bindings.test.ts +41 -0
- package/src/__tests__/destructured-from-props-localconst-value.test.ts +3 -2
- package/src/__tests__/html-event-handler-names.test.ts +107 -0
- package/src/__tests__/line-statement-text.test.ts +25 -0
- package/src/__tests__/literal-member-index.test.ts +14 -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/__tests__/signal-prop-name-collision.test.ts +171 -0
- package/src/__tests__/ssr-defaults.test.ts +6 -7
- package/src/__tests__/ssr-seed-plan.test.ts +23 -0
- package/src/__tests__/staged-ir/02-shadow-guards.test.ts +1 -1
- package/src/__tests__/staged-ir/08-relocate-unit.test.ts +6 -3
- package/src/adapters/line-statement-text.ts +24 -0
- package/src/adapters/parsed-expr-emitter.ts +3 -3
- package/src/analyzer.ts +60 -26
- package/src/augment-inherited-props.ts +49 -0
- package/src/html-types.ts +15 -0
- package/src/index.ts +14 -2
- package/src/ir-to-client-js/collect-elements.ts +15 -10
- package/src/ir-to-client-js/control-flow/plan/build-component-loop.ts +2 -2
- package/src/ir-to-client-js/safe-html.ts +19 -6
- 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 +21 -2
- package/src/literal-member-index.ts +12 -0
- package/src/nullable-signal-attr.ts +156 -0
- package/src/scope/binding-scope.ts +31 -1
- package/src/scope/root-prop-alias.ts +81 -0
- package/src/signal-prop-name-collision.ts +164 -0
- package/src/ssr-defaults.ts +3 -5
- package/src/ssr-seed-plan.ts +14 -2
|
@@ -469,14 +469,15 @@ export function collectInnerLoops(
|
|
|
469
469
|
*
|
|
470
470
|
* Used by both the top-level `case 'loop'` in `collectElements` and the
|
|
471
471
|
* branch-loop collector in `collectBranchLoops`. Each call site applies
|
|
472
|
-
* its own final-emission rule over `innerLoops
|
|
473
|
-
*
|
|
474
|
-
*
|
|
472
|
+
* its own final-emission rule over `innerLoops`. Static top-level loops
|
|
473
|
+
* hydrate existing rows; branch-scoped loops materialize rows via mapArray
|
|
474
|
+
* and therefore use composite rendering for static nested loops too.
|
|
475
475
|
*/
|
|
476
476
|
function decideLoopRendering(
|
|
477
477
|
loop: IRLoop,
|
|
478
478
|
siblingOffsets: Map<IRLoop, IRNode[]>,
|
|
479
479
|
ctx: ClientJsContext | undefined,
|
|
480
|
+
branchScope = false,
|
|
480
481
|
): { useElementReconciliation: boolean; innerLoops: NestedLoop[] | undefined } {
|
|
481
482
|
const hasNestedComps = (loop.nestedComponents?.length ?? 0) > 0
|
|
482
483
|
// Collect inner loops even when the outer item is a child component
|
|
@@ -495,7 +496,7 @@ function decideLoopRendering(
|
|
|
495
496
|
)
|
|
496
497
|
const hasInnerLoops = (innerLoops?.length ?? 0) > 0
|
|
497
498
|
const useElementReconciliation =
|
|
498
|
-
!loop.childComponent && !loop.isStaticArray && (hasNestedComps || hasInnerLoops)
|
|
499
|
+
!loop.childComponent && (!loop.isStaticArray || (branchScope && hasInnerLoops)) && (hasNestedComps || hasInnerLoops)
|
|
499
500
|
return { useElementReconciliation, innerLoops }
|
|
500
501
|
}
|
|
501
502
|
|
|
@@ -1291,6 +1292,12 @@ function collectBranchReactiveAttrs(node: IRNode, ctx: ClientJsContext): Conditi
|
|
|
1291
1292
|
* Collect reactive text expressions from a conditional branch IR subtree.
|
|
1292
1293
|
* Walks the branch tree to find expression nodes that need createEffect updates.
|
|
1293
1294
|
* Does NOT recurse into nested conditionals (they get their own insert() call).
|
|
1295
|
+
*
|
|
1296
|
+
* Uses the same wrap decision as top-level text (`decideWrapFromAstFlags`,
|
|
1297
|
+
* #937): a call the analyzer can't prove non-reactive (`{v().text}`, `v`
|
|
1298
|
+
* reading an untyped or class-held store) wraps too. Gating on
|
|
1299
|
+
* `n.reactive` alone left such text frozen at its first value inside a
|
|
1300
|
+
* branch while the same expression outside it updated (#3266).
|
|
1294
1301
|
*/
|
|
1295
1302
|
function collectBranchTextEffects(node: IRNode): ConditionalBranchTextEffect[] {
|
|
1296
1303
|
const effects: ConditionalBranchTextEffect[] = []
|
|
@@ -1301,7 +1308,7 @@ function collectBranchTextEffects(node: IRNode): ConditionalBranchTextEffect[] {
|
|
|
1301
1308
|
// element / fragment / component / provider / async use default descent.
|
|
1302
1309
|
...stopAt<null>('conditional', 'ifStatement', 'loop'),
|
|
1303
1310
|
expression: ({ node: n }) => {
|
|
1304
|
-
if (n.
|
|
1311
|
+
if (decideWrapFromAstFlags(n).wrap && n.slotId && !n.clientOnly) {
|
|
1305
1312
|
effects.push({ slotId: n.slotId, expression: n.expr })
|
|
1306
1313
|
}
|
|
1307
1314
|
},
|
|
@@ -1342,10 +1349,8 @@ function collectBranchLoops(
|
|
|
1342
1349
|
// the top-level `useElementReconciliation` rule so a `.map()` directly
|
|
1343
1350
|
// inside an outer `.map()` gets its own reactive mapArray even when
|
|
1344
1351
|
// the outer loop lives inside a conditional branch.
|
|
1345
|
-
//
|
|
1346
|
-
//
|
|
1347
|
-
// is handled at the enclosing branch-loop level below (`if (ctx)` block
|
|
1348
|
-
// around `collectLoopChildReactiveTexts`), not per inner loop.
|
|
1352
|
+
// Branch rows are materialized by mapArray even for static arrays.
|
|
1353
|
+
// Collect each inner row's bindings and reconcile nested rows there too.
|
|
1349
1354
|
// flatMap PROJECTION loop — see the top-level handler's note: children
|
|
1350
1355
|
// are SSR-only; the client rides the descriptor mapArray path.
|
|
1351
1356
|
const projectionInner =
|
|
@@ -1355,7 +1360,7 @@ function collectBranchLoops(
|
|
|
1355
1360
|
|
|
1356
1361
|
const { useElementReconciliation, innerLoops: innerLoopsCollected } = projectionInner
|
|
1357
1362
|
? { useElementReconciliation: false, innerLoops: undefined }
|
|
1358
|
-
: decideLoopRendering(n, siblingOffsets,
|
|
1363
|
+
: decideLoopRendering(n, siblingOffsets, ctx, true)
|
|
1359
1364
|
|
|
1360
1365
|
// Build the item template from loop children.
|
|
1361
1366
|
// Use loopDepth=0: this loop gets its own mapArray/mapArrayAnchored call (independent
|
|
@@ -62,7 +62,7 @@ function collectTextOnlySlotIds(nodes: readonly IRNode[], into: Set<string>): vo
|
|
|
62
62
|
/** @internal — prefer `buildLoopPlan`. */
|
|
63
63
|
export function buildComponentLoopPlan(elem: TopLevelLoop, profileComponentName?: string): ComponentLoopPlan {
|
|
64
64
|
const { name } = elem.childComponent!
|
|
65
|
-
const propsExpr = buildComponentPropsExpr(elem.childComponent!, elem.param,
|
|
65
|
+
const propsExpr = buildComponentPropsExpr(elem.childComponent!, elem.param, elem.paramBindings, elem.index)
|
|
66
66
|
const keyExpr = wrapLoopParamAsAccessor(elem.key || '__idx', elem.param, elem.paramBindings, elem.index)
|
|
67
67
|
const { head: paramHead, unwrap: paramUnwrap } = destructureLoopParam(elem.param, elem.paramBindings)
|
|
68
68
|
|
|
@@ -103,7 +103,7 @@ export function buildComponentLoopPlan(elem: TopLevelLoop, profileComponentName?
|
|
|
103
103
|
return {
|
|
104
104
|
componentName: comp.name,
|
|
105
105
|
selector: buildCompSelector(comp),
|
|
106
|
-
propsExpr: buildComponentPropsExpr(comp, elem.param,
|
|
106
|
+
propsExpr: buildComponentPropsExpr(comp, elem.param, elem.paramBindings, elem.index),
|
|
107
107
|
// #3064: marker-scoped where possible — see `buildChildrenTextEffect`.
|
|
108
108
|
childrenTextEffect: childrenRefsLoop
|
|
109
109
|
? buildChildrenTextEffect(comp.children!, rawChildrenExpr!, wrap)
|
|
@@ -37,6 +37,7 @@
|
|
|
37
37
|
*/
|
|
38
38
|
|
|
39
39
|
import type { IRExpression } from '../types.ts'
|
|
40
|
+
import { parseExpression, type ParsedExpr } from '../expression-parser.ts'
|
|
40
41
|
|
|
41
42
|
/**
|
|
42
43
|
* A JS expression string that evaluates, in the client template, to HTML
|
|
@@ -170,20 +171,33 @@ export const EMPTY_MARKUP: SafeHtml = safeHtml("''")
|
|
|
170
171
|
|
|
171
172
|
/**
|
|
172
173
|
* Recognizes a JSX child-position expression that is exactly a reference to
|
|
173
|
-
* the reserved `children` prop
|
|
174
|
+
* the reserved `children` prop, optionally with `?? ''`: bare `children` (destructured) or
|
|
174
175
|
* `<receiver>.children` for any single-identifier receiver (`props.children`,
|
|
175
176
|
* a custom props-param name, a loop-scoped alias closing over props, ...).
|
|
176
177
|
*
|
|
177
178
|
* Deliberately LOOSER than `isTransparentFragment` (`jsx-to-ir.ts`), which
|
|
178
179
|
* runs on the TS AST with the analyzer-resolved props name in hand. This
|
|
179
|
-
* layer
|
|
180
|
+
* layer parses IR expressions without analyzer context, so any single-identifier receiver
|
|
180
181
|
* is the available approximation. The looseness costs nothing measurable: an
|
|
181
182
|
* unrelated `.children` member (a tree node's own `children` array, say) is a
|
|
182
183
|
* reactive member expression that gets a `slotId` and takes the slotted arm
|
|
183
184
|
* of `spliceChildValue` before this predicate is consulted.
|
|
184
185
|
*/
|
|
185
186
|
function isChildrenPassthroughExpr(expr: string): boolean {
|
|
186
|
-
return
|
|
187
|
+
return isChildrenPassthrough(parseExpression(expr))
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
function isChildrenPassthrough(expr: ParsedExpr): boolean {
|
|
191
|
+
if (expr.kind === 'identifier') return expr.name === 'children'
|
|
192
|
+
if (expr.kind === 'member') {
|
|
193
|
+
return expr.object.kind === 'identifier' && expr.property === 'children'
|
|
194
|
+
&& !expr.computed && !expr.optional
|
|
195
|
+
}
|
|
196
|
+
// An empty fallback adds no untrusted text to the compiler-built markup.
|
|
197
|
+
// Other fallbacks still take the escape-by-default path.
|
|
198
|
+
return expr.kind === 'logical' && expr.op === '??'
|
|
199
|
+
&& expr.right.kind === 'literal' && expr.right.value === ''
|
|
200
|
+
&& isChildrenPassthrough(expr.left)
|
|
187
201
|
}
|
|
188
202
|
|
|
189
203
|
export interface ChildSpliceContext {
|
|
@@ -220,7 +234,7 @@ export interface ChildSpliceContext {
|
|
|
220
234
|
* 6. otherwise → `escapedText`
|
|
221
235
|
*
|
|
222
236
|
* Arm 5 tests BOTH `node.expr` (the original source text, stable across
|
|
223
|
-
* builders) and the caller's resolved form (
|
|
237
|
+
* builders) and the caller's resolved form (with parentheses handled structurally): a
|
|
224
238
|
* destructured-and-renamed children (`const { children: kids } = props`)
|
|
225
239
|
* reads `kids` in the source and `(_p.children)` after substitution, and
|
|
226
240
|
* either alone misses one of the two shapes (#2786).
|
|
@@ -235,8 +249,7 @@ export function spliceChildValue(
|
|
|
235
249
|
if (node.slotId) {
|
|
236
250
|
return cx.markupSlotIds?.has(node.slotId) ? escapedTextOrMarkup(valueExpr) : escapedText(valueExpr)
|
|
237
251
|
}
|
|
238
|
-
|
|
239
|
-
if (isChildrenPassthroughExpr(node.expr) || isChildrenPassthroughExpr(resolved)) {
|
|
252
|
+
if (isChildrenPassthroughExpr(node.expr) || isChildrenPassthroughExpr(valueExpr)) {
|
|
240
253
|
return childrenMarkup(valueExpr)
|
|
241
254
|
}
|
|
242
255
|
return escapedText(valueExpr)
|
|
@@ -88,7 +88,7 @@ function emitSignal(lines: string[], plan: SignalEmitPlan): void {
|
|
|
88
88
|
lines.push(` const [${bindings}] = ${plan.initializerOverride}`)
|
|
89
89
|
return
|
|
90
90
|
}
|
|
91
|
-
const
|
|
91
|
+
const args = `${plan.initialValueExpr}${bfIdArg(plan.bfId)}`
|
|
92
92
|
if (plan.branchCondition) {
|
|
93
93
|
// #1414 cell #8: signal declared inside an early-return `if`-block.
|
|
94
94
|
// Hoist as `let` so closures and event handlers hoisted to outer
|
|
@@ -99,12 +99,12 @@ function emitSignal(lines: string[], plan: SignalEmitPlan): void {
|
|
|
99
99
|
if (plan.setter) {
|
|
100
100
|
lines.push(plan.getterElided ? ` let ${plan.setter}` : ` let ${plan.getter}, ${plan.setter}`)
|
|
101
101
|
lines.push(` if (${plan.branchCondition}) {`)
|
|
102
|
-
lines.push(` ;[${getterSlot}, ${plan.setter}] = createSignal(${
|
|
102
|
+
lines.push(` ;[${getterSlot}, ${plan.setter}] = createSignal(${args})`)
|
|
103
103
|
lines.push(` }`)
|
|
104
104
|
} else {
|
|
105
105
|
lines.push(` let ${plan.getter}`)
|
|
106
106
|
lines.push(` if (${plan.branchCondition}) {`)
|
|
107
|
-
lines.push(` ;[${plan.getter}] = createSignal(${
|
|
107
|
+
lines.push(` ;[${plan.getter}] = createSignal(${args})`)
|
|
108
108
|
lines.push(` }`)
|
|
109
109
|
}
|
|
110
110
|
// Controlled effects don't apply to branch-conditioned signals — a
|
|
@@ -113,9 +113,9 @@ function emitSignal(lines: string[], plan: SignalEmitPlan): void {
|
|
|
113
113
|
return
|
|
114
114
|
}
|
|
115
115
|
if (plan.setter) {
|
|
116
|
-
lines.push(` const [${getterSlot}, ${plan.setter}] = createSignal(${
|
|
116
|
+
lines.push(` const [${getterSlot}, ${plan.setter}] = createSignal(${args})`)
|
|
117
117
|
} else {
|
|
118
|
-
lines.push(` const [${plan.getter}] = createSignal(${
|
|
118
|
+
lines.push(` const [${plan.getter}] = createSignal(${args})`)
|
|
119
119
|
}
|
|
120
120
|
if (plan.controlledEffect) {
|
|
121
121
|
emitControlledEffect(lines, plan.controlledEffect)
|
|
@@ -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
|
@@ -40,7 +40,7 @@ import {
|
|
|
40
40
|
isReactiveOrigin,
|
|
41
41
|
AttrValueOf,
|
|
42
42
|
} from './types.ts'
|
|
43
|
-
import { type AnalyzerContext, type MultiReturnJsxInfo, getSourceLocation, collectReactiveGetterNames } from './analyzer-context.ts'
|
|
43
|
+
import { type AnalyzerContext, type MultiReturnJsxInfo, getSourceLocation, collectReactiveGetterNames, tsTypeToTypeInfo } from './analyzer-context.ts'
|
|
44
44
|
import { parseExpression, isSupported, parseBlockBody, foldBlockToExpr, predicateTernaryToLogical, tsNodeToParsedExpr, sortComparatorFromArrow, stringifyParsedExpr, cssKebabCase, CALLBACK_METHODS, type ParsedExpr } from './expression-parser.ts'
|
|
45
45
|
import type { IRLoopSort, FunctionInfo, ConstantInfo, ParamInfo } from './types.ts'
|
|
46
46
|
import { formatParamWithType } from './module-exports.ts'
|
|
@@ -4831,11 +4831,13 @@ function transformMapCall(
|
|
|
4831
4831
|
let children: IRNode[] = []
|
|
4832
4832
|
let paramBindings: LoopParamBinding[] | undefined
|
|
4833
4833
|
let flatMapCallback: FlatMapCallback | undefined
|
|
4834
|
+
let itemTypeNode: ts.Node | undefined
|
|
4834
4835
|
|
|
4835
4836
|
if (ts.isArrowFunction(callback)) {
|
|
4836
4837
|
// Extract parameter names and type annotations
|
|
4837
4838
|
if (callback.parameters.length > 0) {
|
|
4838
4839
|
const firstParam = callback.parameters[0]
|
|
4840
|
+
itemTypeNode = firstParam
|
|
4839
4841
|
param = firstParam.name.getText(ctx.sourceFile)
|
|
4840
4842
|
if (firstParam.type) {
|
|
4841
4843
|
paramType = firstParam.type.getText(ctx.sourceFile)
|
|
@@ -4864,6 +4866,7 @@ function transformMapCall(
|
|
|
4864
4866
|
ts.isBindingElement(elements[1]) && ts.isIdentifier(elements[1].name)) {
|
|
4865
4867
|
index = elements[0].name.text
|
|
4866
4868
|
param = elements[1].name.text
|
|
4869
|
+
itemTypeNode = elements[1].name
|
|
4867
4870
|
// Don't populate paramBindings — the destructure is fully
|
|
4868
4871
|
// resolved into index + param by the iteration shape.
|
|
4869
4872
|
} else {
|
|
@@ -5587,13 +5590,22 @@ function transformMapCall(
|
|
|
5587
5590
|
// createComponent replacement).
|
|
5588
5591
|
const nestedComponents = collectNestedComponents(children).filter(c => c.name !== childComponent?.name)
|
|
5589
5592
|
|
|
5593
|
+
// Scalar operand evidence only. Inferring object rows here would bypass
|
|
5594
|
+
// adapters' existing row-structure capability checks and their diagnostics.
|
|
5595
|
+
const callbackType = itemTypeNode && ctx.analyzer.checker
|
|
5596
|
+
? tsTypeToTypeInfo(ctx.analyzer.checker.getTypeAtLocation(itemTypeNode), ctx.analyzer.checker)
|
|
5597
|
+
: null
|
|
5598
|
+
const scalarItemType = callbackType?.kind === 'primitive' ||
|
|
5599
|
+
(callbackType?.kind === 'union' && callbackType.unionTypes?.every(t => t.kind === 'primitive'))
|
|
5600
|
+
? callbackType : null
|
|
5601
|
+
|
|
5590
5602
|
return {
|
|
5591
5603
|
type: 'loop',
|
|
5592
5604
|
method: method === 'flatMap' ? 'flatMap' : undefined,
|
|
5593
5605
|
array,
|
|
5594
5606
|
templateArray,
|
|
5595
5607
|
arrayType: null,
|
|
5596
|
-
itemType:
|
|
5608
|
+
itemType: scalarItemType,
|
|
5597
5609
|
param,
|
|
5598
5610
|
index,
|
|
5599
5611
|
key,
|
|
@@ -9191,6 +9203,13 @@ function hasReactiveAttributes(attrs: IRAttribute[], ctx: TransformContext): boo
|
|
|
9191
9203
|
if (isSignalOrMemoReference(valueToCheck, ctx) || isPropsReference(valueToCheck, ctx)) {
|
|
9192
9204
|
return true
|
|
9193
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
|
|
9194
9213
|
// Check if attribute references any active loop-bound VALUE names —
|
|
9195
9214
|
// loop root elements need a slotId so className can be updated
|
|
9196
9215
|
// reactively. REACTIVITY/slotId classifier — value bindings only
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { ParsedExpr } from './expression-parser.ts'
|
|
2
|
+
|
|
3
|
+
/** Recover an index operand from a computed member's property key.
|
|
4
|
+
* Only canonical array indices become numbers: keys such as "01" must
|
|
5
|
+
* remain strings. Both `obj[0]` and `obj['0']` use the same JS key. */
|
|
6
|
+
export function literalMemberIndex(property: string): ParsedExpr {
|
|
7
|
+
const index = Number(property)
|
|
8
|
+
if (Number.isInteger(index) && index >= 0 && index < 0xffffffff && String(index) === property) {
|
|
9
|
+
return { kind: 'literal', value: index, literalType: 'number' }
|
|
10
|
+
}
|
|
11
|
+
return { kind: 'literal', value: property, literalType: 'string' }
|
|
12
|
+
}
|