@barefootjs/jsx 0.39.8 → 0.40.0
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 +1 -20
- package/dist/adapters/attr-plan.d.ts.map +1 -1
- package/dist/adapters/callback-method.d.ts +19 -7
- package/dist/adapters/callback-method.d.ts.map +1 -1
- package/dist/adapters/dsl-template-adapter.d.ts +79 -1
- package/dist/adapters/dsl-template-adapter.d.ts.map +1 -1
- package/dist/adapters/loop-plan.d.ts +173 -9
- package/dist/adapters/loop-plan.d.ts.map +1 -1
- package/dist/adapters/parsed-expr-emitter.d.ts +20 -3
- package/dist/adapters/parsed-expr-emitter.d.ts.map +1 -1
- package/dist/analyzer.d.ts.map +1 -1
- package/dist/assignment-target.d.ts +26 -0
- package/dist/assignment-target.d.ts.map +1 -0
- package/dist/augment-inherited-props.d.ts.map +1 -1
- package/dist/css-layer-prefixer.d.ts.map +1 -1
- package/dist/debug.d.ts.map +1 -1
- package/dist/errors.d.ts +1 -0
- package/dist/errors.d.ts.map +1 -1
- package/dist/index.d.ts +6 -6
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2566 -2007
- package/dist/ir-to-client-js/compute-inlinability.d.ts.map +1 -1
- package/dist/ir-to-client-js/control-flow/plan/build-component-loop.d.ts.map +1 -1
- package/dist/ir-to-client-js/control-flow/plan/loop.d.ts +24 -1
- package/dist/ir-to-client-js/control-flow/plan/loop.d.ts.map +1 -1
- package/dist/ir-to-client-js/control-flow/shared.d.ts +16 -1
- package/dist/ir-to-client-js/control-flow/shared.d.ts.map +1 -1
- package/dist/ir-to-client-js/control-flow/stringify/component-loop.d.ts +6 -1
- package/dist/ir-to-client-js/control-flow/stringify/component-loop.d.ts.map +1 -1
- package/dist/ir-to-client-js/csr-substitute.d.ts +13 -2
- package/dist/ir-to-client-js/csr-substitute.d.ts.map +1 -1
- package/dist/ir-to-client-js/emit-registration.d.ts.map +1 -1
- package/dist/ir-to-client-js/html-template.d.ts.map +1 -1
- package/dist/ir-to-client-js/phases/props-extraction.d.ts +5 -3
- package/dist/ir-to-client-js/phases/props-extraction.d.ts.map +1 -1
- package/dist/ir-to-client-js/plan/static-array-child-init.d.ts +8 -0
- package/dist/ir-to-client-js/plan/static-array-child-init.d.ts.map +1 -1
- package/dist/ir-to-client-js/rewrite-props-object.d.ts.map +1 -1
- package/dist/ir-to-client-js/stringify/static-array-child-init.d.ts +1 -0
- package/dist/ir-to-client-js/stringify/static-array-child-init.d.ts.map +1 -1
- package/dist/ir-to-client-js/utils.d.ts +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/module-exports.d.ts.map +1 -1
- package/dist/prop-rewrite.d.ts +17 -8
- package/dist/prop-rewrite.d.ts.map +1 -1
- package/dist/props-binding.d.ts +57 -0
- package/dist/props-binding.d.ts.map +1 -1
- package/dist/scope/binding-scope.d.ts +32 -2
- package/dist/scope/binding-scope.d.ts.map +1 -1
- package/dist/scope/lexical-scope-walk.d.ts +26 -0
- package/dist/scope/lexical-scope-walk.d.ts.map +1 -0
- package/dist/template-parts.d.ts +1 -1
- package/dist/template-parts.d.ts.map +1 -1
- package/dist/types.d.ts +30 -2
- package/dist/types.d.ts.map +1 -1
- package/package.json +2 -2
- package/src/__tests__/arithmetic-lowering.test.ts +5 -1
- package/src/__tests__/attr-plan.test.ts +1 -14
- package/src/__tests__/binding-scope.test.ts +29 -0
- package/src/__tests__/client-template-escape-soundness.test.ts +17 -4
- package/src/__tests__/client-template-static-delimiters.test.ts +57 -0
- package/src/__tests__/create-query.test.ts +1 -1
- package/src/__tests__/dsl-template-adapter.test.ts +70 -0
- package/src/__tests__/emit-to-string-call.test.ts +58 -0
- package/src/__tests__/extract-function-params.test.ts +37 -0
- package/src/__tests__/issue-2960-multi-root-cond-same-tag.test.ts +18 -0
- package/src/__tests__/jsx-text-linebreak-whitespace.test.ts +11 -0
- package/src/__tests__/loop-param-accessor-key-member-safety.test.ts +90 -31
- package/src/__tests__/loop-plan.test.ts +196 -1
- package/src/__tests__/loop-row-child-children-handler.test.ts +129 -0
- package/src/__tests__/loop-row-child-children-inner-loop-shadow.test.ts +74 -0
- package/src/__tests__/loop-row-child-children-ref.test.ts +117 -0
- package/src/__tests__/props-alias-const.test.ts +60 -1
- package/src/__tests__/props-named-callback-param.test.ts +256 -0
- package/src/__tests__/renamed-body-destructure-template.test.ts +86 -0
- package/src/__tests__/rewrite-props-object.test.ts +90 -0
- package/src/__tests__/template-parts-literal-boundary.test.ts +22 -0
- package/src/__tests__/written-binding-substitution.test.ts +430 -0
- package/src/adapters/attr-plan.ts +2 -54
- package/src/adapters/callback-method.ts +25 -10
- package/src/adapters/dsl-template-adapter.ts +155 -2
- package/src/adapters/loop-plan.ts +267 -14
- package/src/adapters/parsed-expr-emitter.ts +37 -4
- package/src/analyzer.ts +283 -2
- package/src/assignment-target.ts +79 -0
- package/src/augment-inherited-props.ts +5 -9
- package/src/css-layer-prefixer.ts +4 -5
- package/src/debug.ts +1 -0
- package/src/errors.ts +11 -0
- package/src/index.ts +23 -4
- package/src/ir-to-client-js/compute-inlinability.ts +7 -4
- package/src/ir-to-client-js/control-flow/plan/build-component-loop.ts +21 -5
- package/src/ir-to-client-js/control-flow/plan/loop.ts +25 -1
- package/src/ir-to-client-js/control-flow/shared.ts +41 -3
- package/src/ir-to-client-js/control-flow/stringify/component-loop.ts +23 -3
- package/src/ir-to-client-js/csr-substitute.ts +70 -22
- package/src/ir-to-client-js/emit-reactive.ts +2 -2
- package/src/ir-to-client-js/emit-registration.ts +6 -1
- package/src/ir-to-client-js/html-template.ts +70 -52
- package/src/ir-to-client-js/phases/props-extraction.ts +26 -5
- package/src/ir-to-client-js/plan/build-static-array-child-init.ts +7 -0
- package/src/ir-to-client-js/plan/static-array-child-init.ts +8 -0
- package/src/ir-to-client-js/rewrite-destructured-props.ts +2 -2
- package/src/ir-to-client-js/rewrite-props-object.ts +14 -8
- package/src/ir-to-client-js/stringify/static-array-child-init.ts +4 -1
- package/src/ir-to-client-js/utils.ts +41 -97
- package/src/jsx-to-ir.ts +65 -32
- package/src/loop-destructure.ts +4 -1
- package/src/module-exports.ts +13 -21
- package/src/prop-rewrite.ts +31 -72
- package/src/props-binding.ts +73 -1
- package/src/rich-type-refusal.ts +3 -1
- package/src/scope/binding-scope.ts +39 -3
- package/src/scope/lexical-scope-walk.ts +167 -0
- package/src/template-parts.ts +4 -2
- package/src/template-position-walk.ts +3 -1
- package/src/types.ts +27 -2
package/src/jsx-to-ir.ts
CHANGED
|
@@ -55,7 +55,7 @@ import {
|
|
|
55
55
|
collectAstPropRefs,
|
|
56
56
|
rewriteScopedValueRefs,
|
|
57
57
|
} from './prop-rewrite.ts'
|
|
58
|
-
import { boundPropLocalNames, buildPropAliasMap, isPropsAliasConst, resolveAliasOrigin, resolveRestSpreadOriginCore, livePropReadExpr } from './props-binding.ts'
|
|
58
|
+
import { boundPropLocalNames, buildPropAliasMap, isPropsAliasConst, isRenamedBodyPropParam, keepsDeclaredValue, resolveAliasOrigin, resolveRestSpreadOriginCore, livePropReadExpr } from './props-binding.ts'
|
|
59
59
|
import { PROPS_PARAM } from './ir-to-client-js/utils.ts'
|
|
60
60
|
import { walkIR } from './ir-to-client-js/walker.ts'
|
|
61
61
|
import { resolveFreeRefs, rebindHelperParamRefs, isNameBound as isNameBoundInEnv, type BindingEnvironment } from './free-refs.ts'
|
|
@@ -745,17 +745,7 @@ function collectBranchLocalPropRefsViaSubstitution(
|
|
|
745
745
|
*/
|
|
746
746
|
function getDestructuredPropNames(ctx: TransformContext): Set<string> | null {
|
|
747
747
|
if (ctx._destructuredPropNames === undefined) {
|
|
748
|
-
const shadowed =
|
|
749
|
-
if (ctx.analyzer.propsObjectName) {
|
|
750
|
-
for (const s of ctx.analyzer.signals) {
|
|
751
|
-
shadowed.add(s.getter)
|
|
752
|
-
if (s.setter) shadowed.add(s.setter)
|
|
753
|
-
}
|
|
754
|
-
for (const m of ctx.analyzer.memos) shadowed.add(m.name)
|
|
755
|
-
for (const c of ctx.analyzer.localConstants) {
|
|
756
|
-
if (!isPropsAliasConst(c, ctx.analyzer.propsObjectName)) shadowed.add(c.name)
|
|
757
|
-
}
|
|
758
|
-
}
|
|
748
|
+
const shadowed = propShadowingNames(ctx.analyzer)
|
|
759
749
|
const eligible = ctx.analyzer.propsParams.filter(p => !shadowed.has(p.name))
|
|
760
750
|
const names = eligible.map(p => p.name)
|
|
761
751
|
ctx._destructuredPropNames = names.length > 0 ? new Set(names) : null
|
|
@@ -772,6 +762,35 @@ function getDestructuredPropNames(ctx: TransformContext): Set<string> | null {
|
|
|
772
762
|
return ctx._destructuredPropNames ?? null
|
|
773
763
|
}
|
|
774
764
|
|
|
765
|
+
/**
|
|
766
|
+
* The component-scope names that shadow a prop of the same name in a
|
|
767
|
+
* `(props)`-arg component (`getDestructuredPropNames`' shadow guard): its
|
|
768
|
+
* signals, memos and locals, except a local that is the prop itself.
|
|
769
|
+
*/
|
|
770
|
+
function propShadowingNames(analyzer: AnalyzerContext): Set<string> {
|
|
771
|
+
const shadowed = new Set<string>()
|
|
772
|
+
if (!analyzer.propsObjectName) return shadowed
|
|
773
|
+
for (const s of analyzer.signals) {
|
|
774
|
+
shadowed.add(s.getter)
|
|
775
|
+
if (s.setter) shadowed.add(s.setter)
|
|
776
|
+
}
|
|
777
|
+
for (const m of analyzer.memos) shadowed.add(m.name)
|
|
778
|
+
for (const c of analyzer.localConstants) {
|
|
779
|
+
// A body destructure binding that is reassigned or mutated no longer
|
|
780
|
+
// holds its prop, renamed or not, so the template keeps it a local.
|
|
781
|
+
if (c.propsDestructure && !keepsDeclaredValue(c)) {
|
|
782
|
+
shadowed.add(c.name)
|
|
783
|
+
continue
|
|
784
|
+
}
|
|
785
|
+
if (isPropsAliasConst(c, analyzer.propsObjectName)) continue
|
|
786
|
+
// A renamed body binding with a default reads as its own prop entry,
|
|
787
|
+
// like the parameter form (#3579).
|
|
788
|
+
if (isRenamedBodyPropParam(c, analyzer.propsParams)) continue
|
|
789
|
+
shadowed.add(c.name)
|
|
790
|
+
}
|
|
791
|
+
return shadowed
|
|
792
|
+
}
|
|
793
|
+
|
|
775
794
|
/**
|
|
776
795
|
* Companion to `getDestructuredPropNames`: local-name → `ParamInfo` for the
|
|
777
796
|
* same shadow-filtered eligible set. Both caches are written in one pass.
|
|
@@ -1178,10 +1197,18 @@ function attachParsedAttrValues(
|
|
|
1178
1197
|
node: IRNode,
|
|
1179
1198
|
parse: (trimmed: string) => ParsedExpr,
|
|
1180
1199
|
parseValue: (trimmed: string) => ParsedExpr,
|
|
1200
|
+
resolve: (parsed: ParsedExpr) => ParsedExpr,
|
|
1181
1201
|
): void {
|
|
1182
|
-
const attach = (value:
|
|
1183
|
-
|
|
1184
|
-
|
|
1202
|
+
const attach = (value: AttrValue, spreads: boolean): void => {
|
|
1203
|
+
if (value.kind === 'template' || value.kind === 'expression') {
|
|
1204
|
+
for (const part of value.parts ?? []) {
|
|
1205
|
+
if (part.type === 'expression') part.parsed = resolve(part.parsed)
|
|
1206
|
+
}
|
|
1207
|
+
}
|
|
1208
|
+
if (value.kind === 'expression' || (spreads && value.kind === 'spread')) {
|
|
1209
|
+
const trimmed = value.expr.trim()
|
|
1210
|
+
if (trimmed) value.parsed = value.kind === 'spread' ? parse(trimmed) : parseValue(trimmed)
|
|
1211
|
+
}
|
|
1185
1212
|
}
|
|
1186
1213
|
if (node.type === 'element') {
|
|
1187
1214
|
for (const attr of node.attrs) attach(attr.value, true)
|
|
@@ -1221,7 +1248,8 @@ function attachParsedExpressions(node: IRNode, analyzer: AnalyzerContext, scope:
|
|
|
1221
1248
|
const trimmed = node.condition.trim()
|
|
1222
1249
|
if (trimmed) node.parsedCondition = parse(trimmed)
|
|
1223
1250
|
}
|
|
1224
|
-
attachParsedAttrValues(node, parse, parseValue
|
|
1251
|
+
attachParsedAttrValues(node, parse, parseValue,
|
|
1252
|
+
parsed => resolveCallbackMethodFunctionReferences(parsed, analyzer, bound))
|
|
1225
1253
|
switch (node.type) {
|
|
1226
1254
|
case 'element':
|
|
1227
1255
|
case 'component':
|
|
@@ -2646,7 +2674,11 @@ function cleanJsxWhitespace(raw: string): string {
|
|
|
2646
2674
|
const isLastLine = i === lines.length - 1
|
|
2647
2675
|
let line = lines[i].replace(/\t/g, ' ')
|
|
2648
2676
|
if (!isFirstLine) line = line.replace(/^ +/, '')
|
|
2649
|
-
if (!isLastLine)
|
|
2677
|
+
if (!isLastLine) {
|
|
2678
|
+
let end = line.length
|
|
2679
|
+
while (end > 0 && line[end - 1] === ' ') end--
|
|
2680
|
+
line = line.slice(0, end)
|
|
2681
|
+
}
|
|
2650
2682
|
if (line !== '') kept.push(line)
|
|
2651
2683
|
}
|
|
2652
2684
|
return kept.join(' ')
|
|
@@ -6750,7 +6782,8 @@ function attrValueText(value: AttrValue): string {
|
|
|
6750
6782
|
if (value.kind !== 'template') return ''
|
|
6751
6783
|
const out: string[] = []
|
|
6752
6784
|
for (const p of value.parts) {
|
|
6753
|
-
if (p.type === '
|
|
6785
|
+
if (p.type === 'expression') out.push(p.expr)
|
|
6786
|
+
else if (p.type === 'ternary') out.push(p.condition, p.whenTrue, p.whenFalse)
|
|
6754
6787
|
else if (p.type === 'lookup') out.push(p.key)
|
|
6755
6788
|
}
|
|
6756
6789
|
return out.join(' ')
|
|
@@ -8159,10 +8192,10 @@ function parseTemplateLiteral(
|
|
|
8159
8192
|
if (ternary) {
|
|
8160
8193
|
parts.push(ternary)
|
|
8161
8194
|
} else {
|
|
8162
|
-
//
|
|
8195
|
+
// Preserve the expression as a distinct part.
|
|
8163
8196
|
const val = ctx.getJS(span.expression)
|
|
8164
8197
|
const tVal = rewriteBarePropRefs(val, span.expression, ctx)
|
|
8165
|
-
parts.push({ type: '
|
|
8198
|
+
parts.push({ type: 'expression', expr: val, templateExpr: tVal, parsed: parseValueExpr(val) })
|
|
8166
8199
|
}
|
|
8167
8200
|
} else {
|
|
8168
8201
|
// Try to resolve `${IDENT}` against local consts (string literal
|
|
@@ -8173,10 +8206,10 @@ function parseTemplateLiteral(
|
|
|
8173
8206
|
if (resolved) {
|
|
8174
8207
|
parts.push(...resolved)
|
|
8175
8208
|
} else {
|
|
8176
|
-
//
|
|
8209
|
+
// Preserve unresolved expressions for the target renderer.
|
|
8177
8210
|
const val = ctx.getJS(span.expression)
|
|
8178
8211
|
const tVal = rewriteBarePropRefs(val, span.expression, ctx)
|
|
8179
|
-
parts.push({ type: '
|
|
8212
|
+
parts.push({ type: 'expression', expr: val, templateExpr: tVal, parsed: parseValueExpr(val) })
|
|
8180
8213
|
}
|
|
8181
8214
|
}
|
|
8182
8215
|
|
|
@@ -8465,7 +8498,7 @@ function tryResolveIdentifierAsTemplateLiteral(
|
|
|
8465
8498
|
// substitution or a Record lookup) to bother emitting structured
|
|
8466
8499
|
// IR — otherwise the existing identifier-text path is more
|
|
8467
8500
|
// efficient. Spans that don't resolve fall through to a bare
|
|
8468
|
-
//
|
|
8501
|
+
// expression part (matching `parseTemplateLiteral`'s default).
|
|
8469
8502
|
let resolvedAny = false
|
|
8470
8503
|
const parts: IRTemplatePart[] = []
|
|
8471
8504
|
if (ast.head.text) parts.push({ type: 'string', value: ast.head.text })
|
|
@@ -8476,15 +8509,14 @@ function tryResolveIdentifierAsTemplateLiteral(
|
|
|
8476
8509
|
resolvedAny = true
|
|
8477
8510
|
parts.push(...resolved)
|
|
8478
8511
|
} else {
|
|
8479
|
-
// Unresolved
|
|
8480
|
-
// as a bare ${expr} placeholder so adapters can substitute the
|
|
8481
|
-
// matching prop/param at render time.
|
|
8512
|
+
// Unresolved spans retain their parsed expression for adapter emission.
|
|
8482
8513
|
const text = astText(span.expression)
|
|
8483
8514
|
const templateText = rewriteBarePropRefs(text, span.expression, ctx)
|
|
8484
8515
|
parts.push({
|
|
8485
|
-
type: '
|
|
8486
|
-
|
|
8487
|
-
|
|
8516
|
+
type: 'expression',
|
|
8517
|
+
expr: text,
|
|
8518
|
+
templateExpr: templateText,
|
|
8519
|
+
parsed: parseValueExpr(text),
|
|
8488
8520
|
})
|
|
8489
8521
|
}
|
|
8490
8522
|
if (span.literal.text) parts.push({ type: 'string', value: span.literal.text })
|
|
@@ -9053,7 +9085,7 @@ function processComponentProps(
|
|
|
9053
9085
|
// shorthand (`<X disabled />` → `disabled={true}`).
|
|
9054
9086
|
if (value.kind === 'template') {
|
|
9055
9087
|
// Collapse using the raw part values, but also carry the
|
|
9056
|
-
// template-variant projection (parts' `
|
|
9088
|
+
// template-variant projection (parts' `templateExpr` /
|
|
9057
9089
|
// `templateCondition` / `templateKey`, i.e. destructured-prop refs
|
|
9058
9090
|
// rewritten to `_p.X`) as `templateExpr`. Without it, the
|
|
9059
9091
|
// `templateExpr` derivation below walks the ORIGINAL initializer AST
|
|
@@ -9872,10 +9904,11 @@ function attrValueReactivityProbe(value: AttrValue): string | null {
|
|
|
9872
9904
|
return value.expr
|
|
9873
9905
|
case 'template':
|
|
9874
9906
|
return value.parts.map((p: IRTemplatePart) => {
|
|
9875
|
-
if (p.type === 'string') return
|
|
9907
|
+
if (p.type === 'string') return ''
|
|
9908
|
+
if (p.type === 'expression') return p.expr
|
|
9876
9909
|
if (p.type === 'ternary') return p.condition
|
|
9877
9910
|
return p.key
|
|
9878
|
-
}).join('')
|
|
9911
|
+
}).join(' ')
|
|
9879
9912
|
case 'literal':
|
|
9880
9913
|
case 'boolean-attr':
|
|
9881
9914
|
case 'boolean-shorthand':
|
package/src/loop-destructure.ts
CHANGED
|
@@ -140,7 +140,10 @@ function restNamesMisused(loop: IRLoop, names: string[]): boolean {
|
|
|
140
140
|
}
|
|
141
141
|
}
|
|
142
142
|
const part = (p: IRTemplatePart): void => {
|
|
143
|
-
if (p.type === '
|
|
143
|
+
if (p.type === 'expression') {
|
|
144
|
+
check(p.expr)
|
|
145
|
+
check(p.templateExpr)
|
|
146
|
+
} else if (p.type === 'ternary') {
|
|
144
147
|
check(p.condition)
|
|
145
148
|
check(p.templateCondition)
|
|
146
149
|
check(p.whenTrue)
|
package/src/module-exports.ts
CHANGED
|
@@ -304,27 +304,19 @@ function isAssignmentOperator(kind: ts.SyntaxKind): boolean {
|
|
|
304
304
|
* Strips type annotations and default values.
|
|
305
305
|
*/
|
|
306
306
|
export function extractFunctionParams(value: string): string {
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
}
|
|
321
|
-
// Function expression: function(a, b) { ... }
|
|
322
|
-
const funcMatch = value.match(/^(?:async\s*)?function\s*\w*\s*\(([^)]*)\)/)
|
|
323
|
-
if (funcMatch) {
|
|
324
|
-
return funcMatch[1]
|
|
325
|
-
.split(',')
|
|
326
|
-
.map((p) => p.trim().split(':')[0].split('=')[0].trim())
|
|
327
|
-
.filter(Boolean)
|
|
307
|
+
for (const kind of [ts.ScriptKind.TSX, ts.ScriptKind.TS]) {
|
|
308
|
+
const source = ts.createSourceFile('function.tsx', `(${value}\n)`, ts.ScriptTarget.Latest, true, kind)
|
|
309
|
+
const diagnostics = 'parseDiagnostics' in source ? source.parseDiagnostics : undefined
|
|
310
|
+
if (Array.isArray(diagnostics) && diagnostics.length > 0) continue
|
|
311
|
+
const statement = source.statements[0]
|
|
312
|
+
if (!statement || !ts.isExpressionStatement(statement)) return ''
|
|
313
|
+
|
|
314
|
+
let expression = statement.expression
|
|
315
|
+
while (ts.isParenthesizedExpression(expression)) expression = expression.expression
|
|
316
|
+
if (!ts.isArrowFunction(expression) && !ts.isFunctionExpression(expression)) return ''
|
|
317
|
+
|
|
318
|
+
return expression.parameters
|
|
319
|
+
.map(parameter => `${parameter.dotDotDotToken ? '...' : ''}${parameter.name.getText(source)}`)
|
|
328
320
|
.join(', ')
|
|
329
321
|
}
|
|
330
322
|
return ''
|
package/src/prop-rewrite.ts
CHANGED
|
@@ -7,84 +7,23 @@
|
|
|
7
7
|
* references in the emitted text via a second scope-aware walk over
|
|
8
8
|
* the text's own AST.
|
|
9
9
|
*
|
|
10
|
-
* Scope model: a name
|
|
11
|
-
*
|
|
12
|
-
*
|
|
13
|
-
*
|
|
14
|
-
*
|
|
15
|
-
*
|
|
16
|
-
*
|
|
17
|
-
*
|
|
10
|
+
* Scope model (`walkWithScope`, `scope/lexical-scope-walk.ts`): a name
|
|
11
|
+
* bound INSIDE the expression — a nested arrow / function parameter, a
|
|
12
|
+
* block-scoped declaration, a catch variable — refers to that binding,
|
|
13
|
+
* not the prop, for the whole binding's scope. Both the discovery walk
|
|
14
|
+
* and the rewrite walk carry the same binding stack, so
|
|
15
|
+
* `items.map((title) => title.a)` never turns into the syntactically
|
|
16
|
+
* invalid `.map((_p.title) => _p.title.a)` when `title` is also a prop.
|
|
17
|
+
* (Names bound by loop callbacks that ENCLOSE the expression are the
|
|
18
|
+
* caller's job — see the `ctx.scope` filter in
|
|
18
19
|
* `jsx-to-ir.ts`'s `rewriteBarePropRefs` wrapper, #2222.)
|
|
19
20
|
*/
|
|
20
21
|
|
|
21
22
|
import ts from 'typescript'
|
|
22
23
|
import { PROPS_PARAM } from './ir-to-client-js/utils.ts'
|
|
23
24
|
import { createTemplateAwareStringProtector } from './ir-to-client-js/html-template.ts'
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
function collectBindingNames(name: ts.BindingName, out: Set<string>): void {
|
|
27
|
-
if (ts.isIdentifier(name)) {
|
|
28
|
-
out.add(name.text)
|
|
29
|
-
return
|
|
30
|
-
}
|
|
31
|
-
for (const el of name.elements) {
|
|
32
|
-
if (ts.isBindingElement(el)) collectBindingNames(el.name, out)
|
|
33
|
-
}
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
/**
|
|
37
|
-
* Names bound by `n` for its subtree, or null when `n` introduces no
|
|
38
|
-
* scope. Function-likes bind their parameters (and a function
|
|
39
|
-
* expression its own name); blocks bind their statement-level
|
|
40
|
-
* variable/function declarations; catch clauses bind their variable.
|
|
41
|
-
*/
|
|
42
|
-
function scopeFrameOf(n: ts.Node): Set<string> | null {
|
|
43
|
-
if (ts.isFunctionLike(n)) {
|
|
44
|
-
const frame = new Set<string>()
|
|
45
|
-
for (const p of n.parameters) collectBindingNames(p.name, frame)
|
|
46
|
-
if ((ts.isFunctionExpression(n) || ts.isFunctionDeclaration(n)) && n.name) frame.add(n.name.text)
|
|
47
|
-
return frame.size > 0 ? frame : null
|
|
48
|
-
}
|
|
49
|
-
if (ts.isBlock(n)) {
|
|
50
|
-
const frame = new Set<string>()
|
|
51
|
-
for (const st of n.statements) {
|
|
52
|
-
if (ts.isVariableStatement(st)) {
|
|
53
|
-
for (const d of st.declarationList.declarations) collectBindingNames(d.name, frame)
|
|
54
|
-
} else if (ts.isFunctionDeclaration(st) && st.name) {
|
|
55
|
-
frame.add(st.name.text)
|
|
56
|
-
}
|
|
57
|
-
}
|
|
58
|
-
return frame.size > 0 ? frame : null
|
|
59
|
-
}
|
|
60
|
-
if (ts.isCatchClause(n) && n.variableDeclaration) {
|
|
61
|
-
const frame = new Set<string>()
|
|
62
|
-
collectBindingNames(n.variableDeclaration.name, frame)
|
|
63
|
-
return frame.size > 0 ? frame : null
|
|
64
|
-
}
|
|
65
|
-
return null
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
/**
|
|
69
|
-
* Depth-first walk that maintains the binding stack described above and
|
|
70
|
-
* reports every identifier along with whether a binding within `root`
|
|
71
|
-
* currently shadows it.
|
|
72
|
-
*/
|
|
73
|
-
function walkWithScope(
|
|
74
|
-
root: ts.Node,
|
|
75
|
-
visit: (ident: ts.Identifier, parent: ts.Node | undefined, shadowed: boolean) => void,
|
|
76
|
-
): void {
|
|
77
|
-
const scopeStack: Set<string>[] = []
|
|
78
|
-
const isShadowed = (name: string) => scopeStack.some(frame => frame.has(name))
|
|
79
|
-
function rec(n: ts.Node, parent?: ts.Node) {
|
|
80
|
-
const frame = scopeFrameOf(n)
|
|
81
|
-
if (frame) scopeStack.push(frame)
|
|
82
|
-
if (ts.isIdentifier(n)) visit(n, parent, isShadowed(n.text))
|
|
83
|
-
ts.forEachChild(n, child => rec(child, n))
|
|
84
|
-
if (frame) scopeStack.pop()
|
|
85
|
-
}
|
|
86
|
-
rec(root)
|
|
87
|
-
}
|
|
25
|
+
import { isAssignmentTargetIdentifier } from './assignment-target.ts'
|
|
26
|
+
import { walkWithScope } from './scope/lexical-scope-walk.ts'
|
|
88
27
|
|
|
89
28
|
/**
|
|
90
29
|
* True when `n` sits in a non-value position where a prop rewrite must
|
|
@@ -148,6 +87,22 @@ export function collectAstPropRefs(
|
|
|
148
87
|
})
|
|
149
88
|
}
|
|
150
89
|
|
|
90
|
+
/**
|
|
91
|
+
* The identifiers below `node` that write a name in `names`
|
|
92
|
+
* (`isAssignmentTargetIdentifier`: an assignment, an update, a destructuring
|
|
93
|
+
* assignment or a `for … of/in` head), nested functions included. A write to
|
|
94
|
+
* a name an inner binding shadows (a handler's own `let start`, a
|
|
95
|
+
* `for (let i …)`) is that binding's, not the outer one's, and is left out.
|
|
96
|
+
*/
|
|
97
|
+
export function collectAssignmentTargets(node: ts.Node, names: ReadonlySet<string>): ts.Identifier[] {
|
|
98
|
+
const targets: ts.Identifier[] = []
|
|
99
|
+
walkWithScope(node, (n, _parent, shadowed) => {
|
|
100
|
+
if (shadowed || !names.has(n.text)) return
|
|
101
|
+
if (isAssignmentTargetIdentifier(n)) targets.push(n)
|
|
102
|
+
})
|
|
103
|
+
return targets
|
|
104
|
+
}
|
|
105
|
+
|
|
151
106
|
/**
|
|
152
107
|
* Scope-aware rewrite: parse `text` as an expression, walk it with the
|
|
153
108
|
* binding stack, and splice `replacementFor(name)` onto exactly the
|
|
@@ -194,6 +149,9 @@ export function rewriteScopedValueRefs(
|
|
|
194
149
|
walkWithScope(sf, (n, parent, shadowed) => {
|
|
195
150
|
if (shadowed || !names.has(n.text)) return
|
|
196
151
|
if (isNonValuePosition(n, parent)) return
|
|
152
|
+
// A written name (`++title`, `title = x`) keeps its binding: a
|
|
153
|
+
// replacement value is not an assignment target.
|
|
154
|
+
if (isAssignmentTargetIdentifier(n)) return
|
|
197
155
|
const start = n.getStart(sf) - usedPrefix.length
|
|
198
156
|
const end = n.getEnd() - usedPrefix.length
|
|
199
157
|
if (start < 0 || end > text.length) return
|
|
@@ -235,6 +193,7 @@ export function splitScopedValueRefs(text: string, names: ReadonlySet<string>):
|
|
|
235
193
|
walkWithScope(sf, (n, parent, shadowed) => {
|
|
236
194
|
if (shadowed || !names.has(n.text)) return
|
|
237
195
|
if (isNonValuePosition(n, parent)) return
|
|
196
|
+
if (isAssignmentTargetIdentifier(n)) return
|
|
238
197
|
const start = n.getStart(sf) - prefix.length
|
|
239
198
|
const end = n.getEnd() - prefix.length
|
|
240
199
|
if (start < 0 || end > text.length) return
|
package/src/props-binding.ts
CHANGED
|
@@ -96,6 +96,77 @@ export function isPropsAliasConst(
|
|
|
96
96
|
return read !== null && read.key === c.name && (!read.hasDefault || c.mutatedAfterDeclaration === true)
|
|
97
97
|
}
|
|
98
98
|
|
|
99
|
+
/**
|
|
100
|
+
* Whether a component-scope binding keeps the value its declaration gives
|
|
101
|
+
* it, so it may be read as the prop it was destructured from (a live
|
|
102
|
+
* `_p.<key> ?? <default>` read).
|
|
103
|
+
*
|
|
104
|
+
* - A body binding (`ConstantInfo`) must be a `const` nothing mutates
|
|
105
|
+
* after the declaration. A `let` may be reassigned (`local =
|
|
106
|
+
* ['changed']`, or a folded `if (!local) local = 'y'`) and a mutated
|
|
107
|
+
* value (`items.push(x)`, `markMutatedConstants`) has changed.
|
|
108
|
+
* - A destructured prop parameter (`ParamInfo`, no declaration kind)
|
|
109
|
+
* keeps the value the caller passed unless the component writes it.
|
|
110
|
+
*
|
|
111
|
+
* Either way a binding the component writes anywhere (`reassigned`, set on
|
|
112
|
+
* both by `markWrittenBindings`, analyzer.ts) does not: a write needs a
|
|
113
|
+
* local, so the init body declares one (`emitPropsExtraction`) and keeps
|
|
114
|
+
* every reference to it. The one rule for every site that turns a props
|
|
115
|
+
* binding into a live prop read: the init body's alias rewrite
|
|
116
|
+
* (`resolveBodyPropAliases`) and parameter rewrite
|
|
117
|
+
* (`rewriteDestructuredPropReads`), the reactive attribute rewrite
|
|
118
|
+
* (`rewriteDestructuredPropsInExpr`), and the template's prop shadow guard
|
|
119
|
+
* (`propShadowingNames`, `jsx-to-ir.ts`, #3579).
|
|
120
|
+
*/
|
|
121
|
+
export function keepsDeclaredValue(
|
|
122
|
+
b: { declarationKind?: ConstantInfo['declarationKind']; mutatedAfterDeclaration?: boolean; reassigned?: boolean },
|
|
123
|
+
): boolean {
|
|
124
|
+
if (b.reassigned === true || b.mutatedAfterDeclaration === true) return false
|
|
125
|
+
return b.declarationKind === undefined || b.declarationKind === 'const'
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/**
|
|
129
|
+
* Whether the component writes any of its bindings (`reassigned`, set by
|
|
130
|
+
* `markWrittenBindings`): a prop parameter or a component-scope `let`. Such
|
|
131
|
+
* a component's client template has to check each expression for a write
|
|
132
|
+
* (`csrSubstitute`'s `writes`), which only the CSR template emitter does, so
|
|
133
|
+
* it never takes the static template fast path (`emit-registration.ts`),
|
|
134
|
+
* and a child whose prop writes a binding is deferred to init
|
|
135
|
+
* (`computeDeferredChildSlots`).
|
|
136
|
+
*/
|
|
137
|
+
export function writesAnyBinding(b: {
|
|
138
|
+
propsParams: readonly Pick<ParamInfo, 'reassigned'>[]
|
|
139
|
+
localConstants: readonly Pick<ConstantInfo, 'reassigned'>[]
|
|
140
|
+
}): boolean {
|
|
141
|
+
return b.propsParams.some(p => p.reassigned === true) || b.localConstants.some(c => c.reassigned === true)
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
/**
|
|
145
|
+
* Whether a renamed binding of a body destructure (`const { b: local = d } =
|
|
146
|
+
* props`) is a prop under its local name: the analyzer gives such a binding
|
|
147
|
+
* with a default its own `ParamInfo` (`name: local`, `sourceName: b`, the
|
|
148
|
+
* default's fields), the same entry the parameter form `function C({ b:
|
|
149
|
+
* local = d })` has. A template then reads the prop through that entry
|
|
150
|
+
* (`livePropReadExpr`), default included, instead of inlining the const.
|
|
151
|
+
*
|
|
152
|
+
* Only a binding that keeps its declared value (`keepsDeclaredValue`): a
|
|
153
|
+
* reassigned or mutated one stays a local and keeps the unsafe-local path
|
|
154
|
+
* (an empty slot, a deferred child).
|
|
155
|
+
*
|
|
156
|
+
* Kept apart from `isPropsAliasConst`: that one also decides relocate's
|
|
157
|
+
* binding kind, whose `prop` lift reads `_p.<key>` without a default, so a
|
|
158
|
+
* renamed binding classified there would lose its default.
|
|
159
|
+
*/
|
|
160
|
+
export function isRenamedBodyPropParam(
|
|
161
|
+
c: Pick<ConstantInfo, 'name' | 'propsDestructure' | 'declarationKind' | 'mutatedAfterDeclaration' | 'reassigned'>,
|
|
162
|
+
propsParams: readonly ParamInfo[],
|
|
163
|
+
): boolean {
|
|
164
|
+
const key = c.propsDestructure?.sourceKey
|
|
165
|
+
if (key === undefined || key === c.name) return false
|
|
166
|
+
if (!keepsDeclaredValue(c)) return false
|
|
167
|
+
return propsParams.some(p => !p.isRest && p.name === c.name && p.sourceName === key)
|
|
168
|
+
}
|
|
169
|
+
|
|
99
170
|
/**
|
|
100
171
|
* How a parsed signal initializer reads a prop: a member chain rooted at the
|
|
101
172
|
* props object (`props.<name>`, `props.row.count`, `props.items[0]`) or a
|
|
@@ -276,7 +347,7 @@ export function resolveBodyPropAliases(
|
|
|
276
347
|
if (propsObjectName === null) return aliases
|
|
277
348
|
for (const c of localConstants) {
|
|
278
349
|
if (c.isModule || !c.value) continue
|
|
279
|
-
if (requireLiveRewriteSafe && (c
|
|
350
|
+
if (requireLiveRewriteSafe && !keepsDeclaredValue(c)) continue
|
|
280
351
|
// Re-parse `value` (rather than walking `c.parsed`) so the default's
|
|
281
352
|
// SOURCE TEXT is available verbatim — `ParsedExpr` structures the
|
|
282
353
|
// default but doesn't retain its original formatting, and this reuses
|
|
@@ -326,6 +397,7 @@ export function boundPropLocalNames(b: PropsParamBindings): ReadonlySet<string>
|
|
|
326
397
|
|
|
327
398
|
const EMPTY_SET: ReadonlySet<string> = new Set()
|
|
328
399
|
|
|
400
|
+
|
|
329
401
|
/**
|
|
330
402
|
* The subset of `ParamInfo` a live prop read actually needs. A body-level
|
|
331
403
|
* pure-alias local (`resolveBodyPropAliases`) has no real `ParamInfo` — it
|
package/src/rich-type-refusal.ts
CHANGED
|
@@ -373,7 +373,9 @@ function walkTemplateParts(
|
|
|
373
373
|
seen: Set<string>,
|
|
374
374
|
): void {
|
|
375
375
|
for (const part of parts) {
|
|
376
|
-
if (part.type === '
|
|
376
|
+
if (part.type === 'expression') {
|
|
377
|
+
checkExpr(part.parsed, loc, meta, bindings, matchers, errors, seen)
|
|
378
|
+
} else if (part.type === 'ternary') {
|
|
377
379
|
const trimmed = part.condition.trim()
|
|
378
380
|
if (trimmed) checkExpr(parseExpression(trimmed), loc, meta, bindings, matchers, errors, seen)
|
|
379
381
|
} else if (part.type === 'lookup') {
|
|
@@ -40,6 +40,14 @@ export type ScopeBindingSource = 'item' | 'index' | 'destructure' | 'preamble' |
|
|
|
40
40
|
export interface ScopeBinding {
|
|
41
41
|
readonly source: ScopeBindingSource
|
|
42
42
|
readonly stringValue?: boolean
|
|
43
|
+
/**
|
|
44
|
+
* The row a `'item'` / `'destructure'` binding reads: the loop's item type
|
|
45
|
+
* and the accessor path from the item to the binding (empty for the plain
|
|
46
|
+
* item). Absent when the loop has no item type or the binding is a rest
|
|
47
|
+
* element. Lets a typed backend resolve the type of a row read
|
|
48
|
+
* (`row.id`, a destructured `id`) at the read.
|
|
49
|
+
*/
|
|
50
|
+
readonly row?: { readonly itemType: LoopValueType; readonly segments: readonly RowPathSegment[] }
|
|
43
51
|
}
|
|
44
52
|
|
|
45
53
|
export interface ScopeFrame {
|
|
@@ -56,16 +64,32 @@ export interface ScopeFrame {
|
|
|
56
64
|
export interface LoopBindingSource {
|
|
57
65
|
readonly param: string
|
|
58
66
|
readonly itemType?: LoopValueType | null
|
|
67
|
+
/**
|
|
68
|
+
* The item type a binding's `row` carries, when the caller knows more than
|
|
69
|
+
* `itemType` (which is scalar evidence only): a backend that resolves an
|
|
70
|
+
* object row's type from the loop's array source. Defaults to `itemType`.
|
|
71
|
+
*/
|
|
72
|
+
readonly rowType?: LoopValueType | null
|
|
59
73
|
readonly index?: string | null
|
|
60
|
-
readonly paramBindings?: readonly {
|
|
74
|
+
readonly paramBindings?: readonly {
|
|
75
|
+
readonly name: string
|
|
76
|
+
readonly segments?: readonly RowPathSegment[]
|
|
77
|
+
readonly rest?: unknown
|
|
78
|
+
}[]
|
|
61
79
|
readonly preamble?: { readonly declaredNames: readonly string[] } | null
|
|
62
80
|
}
|
|
63
81
|
|
|
64
|
-
|
|
82
|
+
/** Structural pick of `TypeInfo` (`packages/jsx/src/types.ts`). */
|
|
83
|
+
export interface LoopValueType {
|
|
65
84
|
readonly primitive?: string
|
|
66
85
|
readonly unionTypes?: readonly LoopValueType[]
|
|
67
86
|
}
|
|
68
87
|
|
|
88
|
+
/** Structural copy of `LoopBindingPathSegment` (`packages/jsx/src/types.ts`). */
|
|
89
|
+
export type RowPathSegment =
|
|
90
|
+
| { readonly kind: 'field'; readonly key: string; readonly isIdent: boolean }
|
|
91
|
+
| { readonly kind: 'index'; readonly index: number }
|
|
92
|
+
|
|
69
93
|
function isStringLoopValue(type: LoopValueType): boolean {
|
|
70
94
|
return type.primitive === 'string' ||
|
|
71
95
|
(type.unionTypes !== undefined && type.unionTypes.length > 0 && type.unionTypes.every(isStringLoopValue))
|
|
@@ -121,12 +145,19 @@ export class BindingScope {
|
|
|
121
145
|
*/
|
|
122
146
|
enterLoopRow(loop: LoopBindingSource): BindingScope {
|
|
123
147
|
const bindings = new Map<string, ScopeBinding>()
|
|
148
|
+
const rowType = loop.rowType ?? loop.itemType
|
|
124
149
|
if (loop.paramBindings && loop.paramBindings.length > 0) {
|
|
125
|
-
for (const b of loop.paramBindings)
|
|
150
|
+
for (const b of loop.paramBindings) {
|
|
151
|
+
bindings.set(b.name, {
|
|
152
|
+
source: 'destructure',
|
|
153
|
+
...(rowType && b.segments && b.rest === undefined ? { row: { itemType: rowType, segments: b.segments } } : {}),
|
|
154
|
+
})
|
|
155
|
+
}
|
|
126
156
|
} else {
|
|
127
157
|
bindings.set(loop.param, {
|
|
128
158
|
source: 'item',
|
|
129
159
|
...(loop.itemType ? { stringValue: isStringLoopValue(loop.itemType) } : {}),
|
|
160
|
+
...(rowType ? { row: { itemType: rowType, segments: [] } } : {}),
|
|
130
161
|
})
|
|
131
162
|
}
|
|
132
163
|
if (loop.index != null) bindings.set(loop.index, { source: 'index' })
|
|
@@ -182,6 +213,11 @@ export class BindingScope {
|
|
|
182
213
|
return hit ? hit.binding.stringValue === true : outer(name)
|
|
183
214
|
}
|
|
184
215
|
|
|
216
|
+
/** The names the innermost frame binds, in binding order. */
|
|
217
|
+
innermostNames(): readonly string[] {
|
|
218
|
+
return this.frames[0] ? [...this.frames[0].bindings.keys()] : []
|
|
219
|
+
}
|
|
220
|
+
|
|
185
221
|
/** Names this frame shadows in an enclosing callback/row frame. */
|
|
186
222
|
shadowedNames(): readonly string[] {
|
|
187
223
|
if (!this.frames[0]) return []
|