@barefootjs/jsx 0.39.7 → 0.39.9
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/callback-method.d.ts +36 -7
- package/dist/adapters/callback-method.d.ts.map +1 -1
- package/dist/adapters/component-props.d.ts +80 -13
- package/dist/adapters/component-props.d.ts.map +1 -1
- package/dist/adapters/dsl-template-adapter.d.ts +153 -0
- package/dist/adapters/dsl-template-adapter.d.ts.map +1 -0
- package/dist/adapters/jsx-adapter.d.ts +4 -0
- package/dist/adapters/jsx-adapter.d.ts.map +1 -1
- package/dist/adapters/literal-value.d.ts +2 -2
- package/dist/adapters/literal-value.d.ts.map +1 -1
- package/dist/adapters/loop-plan.d.ts +133 -9
- package/dist/adapters/loop-plan.d.ts.map +1 -1
- package/dist/adapters/parsed-expr-emitter.d.ts +44 -0
- package/dist/adapters/parsed-expr-emitter.d.ts.map +1 -1
- package/dist/adapters/style-object.d.ts +33 -0
- package/dist/adapters/style-object.d.ts.map +1 -0
- 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/children-nullish.d.ts +22 -0
- package/dist/children-nullish.d.ts.map +1 -0
- package/dist/errors.d.ts +2 -0
- package/dist/errors.d.ts.map +1 -1
- package/dist/expression-parser.d.ts +9 -19
- package/dist/expression-parser.d.ts.map +1 -1
- package/dist/free-refs.d.ts +6 -0
- package/dist/free-refs.d.ts.map +1 -1
- package/dist/index.d.ts +17 -14
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +15040 -13899
- 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/event-collision.d.ts +2 -1
- package/dist/ir-to-client-js/control-flow/plan/event-collision.d.ts.map +1 -1
- package/dist/ir-to-client-js/control-flow/plan/insert.d.ts +2 -2
- package/dist/ir-to-client-js/control-flow/plan/insert.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/control-flow/stringify/event-delegation.d.ts.map +1 -1
- package/dist/ir-to-client-js/control-flow/stringify/event-listener.d.ts +10 -9
- package/dist/ir-to-client-js/control-flow/stringify/event-listener.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-reactive.d.ts +1 -1
- package/dist/ir-to-client-js/emit-reactive.d.ts.map +1 -1
- package/dist/ir-to-client-js/emit-registration.d.ts +10 -8
- package/dist/ir-to-client-js/emit-registration.d.ts.map +1 -1
- package/dist/ir-to-client-js/generate-init.d.ts.map +1 -1
- package/dist/ir-to-client-js/html-template.d.ts +0 -9
- package/dist/ir-to-client-js/html-template.d.ts.map +1 -1
- package/dist/ir-to-client-js/identifiers.d.ts +6 -8
- package/dist/ir-to-client-js/identifiers.d.ts.map +1 -1
- package/dist/ir-to-client-js/imports.d.ts +23 -2
- package/dist/ir-to-client-js/imports.d.ts.map +1 -1
- package/dist/ir-to-client-js/phases/props-extraction.d.ts +6 -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/prune-unused-prop-extractions.d.ts +1 -1
- package/dist/ir-to-client-js/prune-unused-prop-extractions.d.ts.map +1 -1
- package/dist/ir-to-client-js/reactivity.d.ts.map +1 -1
- package/dist/ir-to-client-js/rewrite-destructured-props.d.ts +1 -1
- package/dist/ir-to-client-js/rewrite-destructured-props.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/safe-html.d.ts +20 -2
- package/dist/ir-to-client-js/safe-html.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/types.d.ts +3 -3
- package/dist/ir-to-client-js/types.d.ts.map +1 -1
- package/dist/ir-to-client-js/utils.d.ts +2 -11
- package/dist/ir-to-client-js/utils.d.ts.map +1 -1
- package/dist/jsx-to-ir.d.ts.map +1 -1
- package/dist/nullable-signal-attr.d.ts +40 -10
- package/dist/nullable-signal-attr.d.ts.map +1 -1
- package/dist/prop-rewrite.d.ts +35 -8
- package/dist/prop-rewrite.d.ts.map +1 -1
- package/dist/props-binding.d.ts +66 -1
- package/dist/props-binding.d.ts.map +1 -1
- package/dist/scanner/js-scanner.d.ts.map +1 -1
- package/dist/scanner/js-scanner.js +30 -2
- package/dist/scope/binding-scope.d.ts +30 -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/signal-prop-name-collision.d.ts +6 -5
- package/dist/signal-prop-name-collision.d.ts.map +1 -1
- package/dist/ssr-defaults.d.ts +28 -0
- package/dist/ssr-defaults.d.ts.map +1 -1
- package/dist/ssr-seed-plan.d.ts +39 -14
- package/dist/ssr-seed-plan.d.ts.map +1 -1
- package/dist/types.d.ts +133 -19
- package/dist/types.d.ts.map +1 -1
- package/dist/value-references.d.ts +21 -0
- package/dist/value-references.d.ts.map +1 -1
- package/package.json +2 -2
- package/src/__tests__/__snapshots__/doc-examples.test.ts.snap +18 -18
- package/src/__tests__/adapter-output.test.ts +5 -3
- package/src/__tests__/analyzer.test.ts +33 -0
- package/src/__tests__/arithmetic-lowering.test.ts +48 -0
- package/src/__tests__/binding-scope.test.ts +29 -0
- package/src/__tests__/children-read.test.ts +226 -0
- package/src/__tests__/client-template-escape-soundness.test.ts +31 -1
- package/src/__tests__/component-props.test.ts +68 -1
- package/src/__tests__/constant-inlining-tree.test.ts +136 -0
- package/src/__tests__/create-query.test.ts +1 -1
- package/src/__tests__/destructure-default-reads-earlier-param.test.ts +138 -0
- package/src/__tests__/dsl-template-adapter.test.ts +173 -0
- package/src/__tests__/emit-to-string-call.test.ts +58 -0
- package/src/__tests__/event-handler-verbatim.test.ts +73 -0
- package/src/__tests__/expression-parser.test.ts +1 -44
- package/src/__tests__/loop-param-accessor-key-member-safety.test.ts +90 -31
- package/src/__tests__/loop-plan.test.ts +131 -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__/nullable-signal-attr.test.ts +67 -3
- package/src/__tests__/props-alias-const.test.ts +60 -1
- package/src/__tests__/props-named-callback-param.test.ts +256 -0
- package/src/__tests__/reference-graph-identifiers.test.ts +161 -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__/runtime-import-detection.test.ts +134 -0
- package/src/__tests__/signal-prop-name-collision.test.ts +30 -0
- package/src/__tests__/ssr-defaults.test.ts +19 -1
- package/src/__tests__/ssr-seed-plan.test.ts +119 -0
- package/src/__tests__/style-object-plan.test.ts +54 -0
- package/src/__tests__/written-binding-substitution.test.ts +430 -0
- package/src/__tests__/zero-param-helper-inline.test.ts +57 -0
- package/src/adapters/callback-method.ts +48 -10
- package/src/adapters/component-props.ts +130 -12
- package/src/adapters/dsl-template-adapter.ts +259 -0
- package/src/adapters/jsx-adapter.ts +12 -6
- package/src/adapters/literal-value.ts +2 -2
- package/src/adapters/loop-plan.ts +201 -13
- package/src/adapters/parsed-expr-emitter.ts +67 -0
- package/src/adapters/style-object.ts +61 -0
- package/src/analyzer.ts +396 -16
- package/src/assignment-target.ts +79 -0
- package/src/children-nullish.ts +33 -0
- package/src/debug.ts +2 -2
- package/src/errors.ts +23 -0
- package/src/expression-parser.ts +56 -53
- package/src/free-refs.ts +28 -17
- package/src/index.ts +31 -10
- package/src/ir-to-client-js/compute-inlinability.ts +35 -5
- package/src/ir-to-client-js/control-flow/plan/build-component-loop.ts +21 -5
- package/src/ir-to-client-js/control-flow/plan/event-collision.ts +2 -1
- package/src/ir-to-client-js/control-flow/plan/insert.ts +2 -2
- 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/control-flow/stringify/event-delegation.ts +3 -2
- package/src/ir-to-client-js/control-flow/stringify/event-listener.ts +13 -12
- package/src/ir-to-client-js/csr-substitute.ts +82 -24
- package/src/ir-to-client-js/emit-reactive.ts +3 -3
- package/src/ir-to-client-js/emit-registration.ts +34 -58
- package/src/ir-to-client-js/generate-init.ts +17 -1
- package/src/ir-to-client-js/html-template.ts +48 -57
- package/src/ir-to-client-js/identifiers.ts +75 -24
- package/src/ir-to-client-js/imports.ts +56 -19
- package/src/ir-to-client-js/index.ts +2 -1
- package/src/ir-to-client-js/phases/event-handlers.ts +2 -2
- package/src/ir-to-client-js/phases/props-extraction.ts +39 -17
- package/src/ir-to-client-js/plan/build-declaration-emit.ts +2 -2
- 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/prune-unused-prop-extractions.ts +7 -4
- package/src/ir-to-client-js/reactivity.ts +2 -2
- package/src/ir-to-client-js/rewrite-destructured-props.ts +22 -5
- package/src/ir-to-client-js/rewrite-props-object.ts +14 -8
- package/src/ir-to-client-js/safe-html.ts +29 -35
- package/src/ir-to-client-js/stringify/static-array-child-init.ts +4 -1
- package/src/ir-to-client-js/types.ts +3 -2
- package/src/ir-to-client-js/utils.ts +42 -122
- package/src/jsx-to-ir.ts +310 -90
- package/src/nullable-signal-attr.ts +149 -33
- package/src/prop-rewrite.ts +72 -72
- package/src/props-binding.ts +90 -3
- package/src/scanner/__tests__/js-scanner.test.ts +37 -0
- package/src/scanner/js-scanner.ts +64 -4
- package/src/scope/binding-scope.ts +34 -3
- package/src/scope/lexical-scope-walk.ts +167 -0
- package/src/signal-prop-name-collision.ts +50 -24
- package/src/ssr-defaults.ts +47 -20
- package/src/ssr-seed-plan.ts +103 -23
- package/src/types.ts +140 -20
- package/src/value-references.ts +132 -0
|
@@ -13,7 +13,8 @@ import type { ClientJsContext } from './types.ts'
|
|
|
13
13
|
import { PROPS_PARAM, isCommentScopedRoot, initFunctionName } from './utils.ts'
|
|
14
14
|
import { buildReferencesGraph } from './build-references.ts'
|
|
15
15
|
import { computePropUsage } from './compute-prop-usage.ts'
|
|
16
|
-
import { IMPORT_PLACEHOLDER, MODULE_CONSTANTS_PLACEHOLDER } from './imports.ts'
|
|
16
|
+
import { IMPORT_PLACEHOLDER, MODULE_CONSTANTS_PLACEHOLDER, runtimeNameCollisions } from './imports.ts'
|
|
17
|
+
import { signalSecondBinding } from '../signal-initializer.ts'
|
|
17
18
|
import { emitRegistrationAndHydration, csrInlinableConstantsFromCtx } from './emit-registration.ts'
|
|
18
19
|
import { computeDeferredChildSlots } from './html-template.ts'
|
|
19
20
|
import { emitChildComponentImports } from './child-components.ts'
|
|
@@ -137,6 +138,21 @@ export function generateInitFunction(
|
|
|
137
138
|
const codeWithModuleConstants = pruneUnusedPropExtractions(
|
|
138
139
|
generatedCode.replace(MODULE_CONSTANTS_PLACEHOLDER, () => moduleConstantsCode),
|
|
139
140
|
)
|
|
141
|
+
ctx.warnings.push(
|
|
142
|
+
...runtimeNameCollisions(
|
|
143
|
+
[
|
|
144
|
+
...classification.moduleLevelConstants,
|
|
145
|
+
...classification.moduleLevelFunctions,
|
|
146
|
+
...classification.moduleLevelSignals.flatMap(s => {
|
|
147
|
+
const second = signalSecondBinding(s)
|
|
148
|
+
return [{ name: s.getter, loc: s.loc }, ...(second ? [{ name: second, loc: s.loc }] : [])]
|
|
149
|
+
}),
|
|
150
|
+
...classification.moduleLevelMemos,
|
|
151
|
+
],
|
|
152
|
+
ir.metadata.imports,
|
|
153
|
+
codeWithModuleConstants,
|
|
154
|
+
),
|
|
155
|
+
)
|
|
140
156
|
const allImportLines = resolveFinalImports(codeWithModuleConstants, ir, localImportPrefixes)
|
|
141
157
|
|
|
142
158
|
return codeWithModuleConstants.replace(IMPORT_PLACEHOLDER, () => allImportLines)
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
* IR → HTML template string generation and validation.
|
|
3
3
|
*/
|
|
4
4
|
|
|
5
|
-
import type { AttrValue, FlatMapCallback, IRAttribute, IRElement, IRExpression, IRNode, IRProp, MapCallbackPreamble } from '../types.ts'
|
|
5
|
+
import type { AttrValue, FlatMapCallback, IRAttribute, IRElement, IRExpression, IRNode, IRProp, MapCallbackPreamble, JsSourceText } from '../types.ts'
|
|
6
6
|
import { isBooleanAttr } from '../html-constants.ts'
|
|
7
7
|
import { toHtmlAttrName, attrValueToString, quotePropName, PROPS_PARAM, DATA_BF_PH, keyAttrName, loopStartMarker, loopEndMarker, loopItemMarker, freeIdsFromRefs, setIntersects, wrapExprWithLoopParams, specsInsideRow } from './utils.ts'
|
|
8
8
|
import type { LoopParamSpec } from './utils.ts'
|
|
@@ -18,29 +18,7 @@ import { BindingScope } from '../scope/binding-scope.ts'
|
|
|
18
18
|
import { interp, spliceChildValue, conditionalMarkup, renderChildCall, mappedRowsMarkup, withOuterBindings, dangerousInnerHtml, EMPTY_MARKUP } from './safe-html.ts'
|
|
19
19
|
import { markupSlotIdsOf } from './markup-slots.ts'
|
|
20
20
|
import { isEventHandlerName } from '../event-handler-name.ts'
|
|
21
|
-
|
|
22
|
-
/**
|
|
23
|
-
* Protect string literals from regex-based replacements.
|
|
24
|
-
* Returns protect/restore functions that extract string literals before
|
|
25
|
-
* regex replacements and restore them after.
|
|
26
|
-
*/
|
|
27
|
-
export function createStringProtector(): {
|
|
28
|
-
protect: (s: string) => string
|
|
29
|
-
restore: (s: string) => string
|
|
30
|
-
} {
|
|
31
|
-
const strings: string[] = []
|
|
32
|
-
const protect = (s: string): string => {
|
|
33
|
-
return s.replace(/'(?:[^'\\]|\\.)*'|"(?:[^"\\]|\\.)*"/g, (match) => {
|
|
34
|
-
const idx = strings.length
|
|
35
|
-
strings.push(match)
|
|
36
|
-
return `__STRLIT_${idx}__`
|
|
37
|
-
})
|
|
38
|
-
}
|
|
39
|
-
const restore = (s: string): string => {
|
|
40
|
-
return s.replace(/__STRLIT_(\d+)__/g, (_, idx) => strings[Number(idx)])
|
|
41
|
-
}
|
|
42
|
-
return { protect, restore }
|
|
43
|
-
}
|
|
21
|
+
import { writesAnyBinding } from '../props-binding.ts'
|
|
44
22
|
|
|
45
23
|
/**
|
|
46
24
|
* Split a template literal body into static segments and `${...}` interpolations,
|
|
@@ -327,7 +305,7 @@ function escapeAttrValueExpr(valExpr: string): string {
|
|
|
327
305
|
*/
|
|
328
306
|
function dangerouslyHtmlChildren(
|
|
329
307
|
attrs: ReadonlyArray<IRAttribute>,
|
|
330
|
-
toExpr: (v: { expr:
|
|
308
|
+
toExpr: (v: { expr: JsSourceText; templateExpr?: JsSourceText }) => string,
|
|
331
309
|
): string | null {
|
|
332
310
|
const attr = attrs.find(a => a.name === 'dangerouslySetInnerHTML')
|
|
333
311
|
if (!attr || attr.value.kind !== 'expression') return null
|
|
@@ -341,7 +319,7 @@ function dangerouslyHtmlChildren(
|
|
|
341
319
|
* template path; for non-`expression`/`spread`/`template` variants we
|
|
342
320
|
* delegate to the canonical projection.
|
|
343
321
|
*/
|
|
344
|
-
function transformKeyValue(value: AttrValue, transformExpr: (expr:
|
|
322
|
+
function transformKeyValue(value: AttrValue, transformExpr: (expr: JsSourceText, templateExpr?: JsSourceText) => string): string {
|
|
345
323
|
switch (value.kind) {
|
|
346
324
|
case 'expression':
|
|
347
325
|
case 'spread':
|
|
@@ -1838,8 +1816,22 @@ export function irToComponentTemplate(
|
|
|
1838
1816
|
return irToComponentTemplateWithOpts(node, { inlinableConstants, restSpreadNames, propsObjectName, loopDepth: -1, markupSlotIds, restPropsName })
|
|
1839
1817
|
}
|
|
1840
1818
|
|
|
1819
|
+
/**
|
|
1820
|
+
* The `csrSubstitute` environment that inlines each template constant by
|
|
1821
|
+
* name (#3535), or null when there is none to inline.
|
|
1822
|
+
*/
|
|
1823
|
+
function inlinableConstantEnv(inlinableConstants: ReadonlyMap<string, string> | undefined): CsrEnv | null {
|
|
1824
|
+
if (!inlinableConstants || inlinableConstants.size === 0) return null
|
|
1825
|
+
const substitutions = new Map<string, CsrSubstitution>()
|
|
1826
|
+
for (const [name, value] of inlinableConstants) {
|
|
1827
|
+
substitutions.set(name, { kind: 'identifier', replacement: value, freeIdentifiers: new Set() })
|
|
1828
|
+
}
|
|
1829
|
+
return { substitutions, propsObjectName: null }
|
|
1830
|
+
}
|
|
1831
|
+
|
|
1841
1832
|
function irToComponentTemplateWithOpts(node: IRNode, opts: TemplateOptions): string {
|
|
1842
1833
|
const { inlinableConstants, restSpreadNames, propsObjectName, restPropsName, loopDepth = 0 } = opts
|
|
1834
|
+
const constantEnv = inlinableConstantEnv(inlinableConstants)
|
|
1843
1835
|
// `recurse` preserves `inHoistedChildren` for structural pass-through
|
|
1844
1836
|
// (fragment / conditional); `childrenRecurse` clears it so element
|
|
1845
1837
|
// descendants don't re-emit the placeholder. (#1320)
|
|
@@ -1849,29 +1841,19 @@ function irToComponentTemplateWithOpts(node: IRNode, opts: TemplateOptions): str
|
|
|
1849
1841
|
// Bare prop name prefixing (org → _p.org) is handled by templateExpr
|
|
1850
1842
|
// from Phase 1 AST rewrite. Only constant inlining and props object
|
|
1851
1843
|
// normalization are done here. (#807)
|
|
1852
|
-
const transformExpr = (expr:
|
|
1853
|
-
|
|
1854
|
-
|
|
1855
|
-
|
|
1856
|
-
//
|
|
1857
|
-
|
|
1858
|
-
|
|
1859
|
-
|
|
1860
|
-
}
|
|
1861
|
-
}
|
|
1844
|
+
const transformExpr = (expr: JsSourceText, templateExpr?: JsSourceText): string => {
|
|
1845
|
+
// Inline constant references with their resolved values (#343), on the
|
|
1846
|
+
// expression tree: a constant's name inside a string or template
|
|
1847
|
+
// literal, a member name, or a binding that shadows it is never
|
|
1848
|
+
// replaced, and a shorthand `{ size }` expands to `size: (value)`
|
|
1849
|
+
// (#3535).
|
|
1850
|
+
const source = templateExpr ?? expr
|
|
1851
|
+
const restored = constantEnv ? csrSubstitute(source, constantEnv, opts.scope).rewritten : source
|
|
1862
1852
|
|
|
1863
|
-
// Normalize source-level props object access (e.g., props.xxx → _p.xxx)
|
|
1864
|
-
//
|
|
1865
|
-
//
|
|
1866
|
-
//
|
|
1867
|
-
// stands for (harmless for the placeholder itself, but it must not be
|
|
1868
|
-
// mistaken for unparsed leftover input by a caller reading `result`
|
|
1869
|
-
// before this point). Regex-based `\b${propsObjectName}\.` (removed
|
|
1870
|
-
// here) required the name to be immediately followed by `.`, so a
|
|
1871
|
-
// parenthesised receiver — `(props).label`, produced by this same
|
|
1872
|
-
// function's own constant-inlining step just above when the inlined
|
|
1873
|
-
// value is itself `props` (a local alias of the whole props object) —
|
|
1874
|
-
// silently kept the pre-rewrite name instead of `_p`.
|
|
1853
|
+
// Normalize source-level props object access (e.g., props.xxx → _p.xxx),
|
|
1854
|
+
// also on the tree: a parenthesised receiver — `(props).label`, produced
|
|
1855
|
+
// by the constant inlining just above when the inlined value is itself
|
|
1856
|
+
// `props` (a local alias of the whole props object) — resolves too.
|
|
1875
1857
|
//
|
|
1876
1858
|
// The gate stays keyed on `propsObjectName` for the destructured-prop
|
|
1877
1859
|
// case (unconditionally rewriting here would also change static-path
|
|
@@ -1881,7 +1863,6 @@ function irToComponentTemplateWithOpts(node: IRNode, opts: TemplateOptions): str
|
|
|
1881
1863
|
// param), so `rest.header` resolves to `_p.header` here the same way
|
|
1882
1864
|
// it already does in the init body (`generate-init.ts`, #2723) instead
|
|
1883
1865
|
// of reaching the template as a bare, undeclared `rest` (#2806).
|
|
1884
|
-
const restored = restore(result)
|
|
1885
1866
|
return (propsObjectName && propsObjectName !== PROPS_PARAM) || restPropsName
|
|
1886
1867
|
? rewritePropsObjectRef(restored, propsObjectName ?? null, restPropsName ?? null, { enclosingScope: opts.scope })
|
|
1887
1868
|
: restored
|
|
@@ -2382,12 +2363,14 @@ function propResolvesUnsafe(
|
|
|
2382
2363
|
env: CsrEnv,
|
|
2383
2364
|
unsafeLocalNames: ReadonlySet<string>,
|
|
2384
2365
|
): boolean {
|
|
2385
|
-
if (unsafeLocalNames.size === 0) return false
|
|
2386
2366
|
let source: string | undefined
|
|
2387
2367
|
switch (prop.value.kind) {
|
|
2388
2368
|
case 'expression':
|
|
2389
2369
|
case 'spread':
|
|
2390
|
-
|
|
2370
|
+
// The same text the CSR `component` emit reads (`transformExpr(expr,
|
|
2371
|
+
// templateExpr)`): a prop read Phase 1 already lowered to
|
|
2372
|
+
// `_p.<key> ?? <default>` in `templateExpr` names no init local (#3579).
|
|
2373
|
+
source = prop.value.templateExpr ?? prop.value.expr
|
|
2391
2374
|
break
|
|
2392
2375
|
case 'template':
|
|
2393
2376
|
source = attrValueToString(prop.value, { useTemplate: true }) ?? undefined
|
|
@@ -2397,8 +2380,8 @@ function propResolvesUnsafe(
|
|
|
2397
2380
|
return false
|
|
2398
2381
|
}
|
|
2399
2382
|
if (!source) return false
|
|
2400
|
-
const { freeIdentifiers } = csrSubstitute(source, env)
|
|
2401
|
-
return setIntersects(freeIdentifiers, unsafeLocalNames)
|
|
2383
|
+
const { freeIdentifiers, writes } = csrSubstitute(source, env)
|
|
2384
|
+
return writes.size > 0 || setIntersects(freeIdentifiers, unsafeLocalNames)
|
|
2402
2385
|
}
|
|
2403
2386
|
|
|
2404
2387
|
/**
|
|
@@ -2423,7 +2406,10 @@ export function computeDeferredChildSlots(
|
|
|
2423
2406
|
propsObjectName?: string | null,
|
|
2424
2407
|
): Set<string> {
|
|
2425
2408
|
const deferred = new Set<string>()
|
|
2426
|
-
|
|
2409
|
+
// A prop can also be dropped for writing a binding (`get={() => ++id}`,
|
|
2410
|
+
// see `transformExpr`), which only a component that writes one can have.
|
|
2411
|
+
if (!writesAnyBinding(ctx) && (!unsafeLocalNames || unsafeLocalNames.size === 0)) return deferred
|
|
2412
|
+
const unsafe = unsafeLocalNames ?? new Set<string>()
|
|
2427
2413
|
const env = buildCsrEnvForCtx(ctx, inlinableConstants, propsObjectName)
|
|
2428
2414
|
|
|
2429
2415
|
const visit = (n: IRNode): void => {
|
|
@@ -2445,7 +2431,7 @@ export function computeDeferredChildSlots(
|
|
|
2445
2431
|
if (p.name === 'ref') return false
|
|
2446
2432
|
if (isEventHandlerName(p.name)) return false
|
|
2447
2433
|
if (p.clientOnly) return false
|
|
2448
|
-
return propResolvesUnsafe(p, env,
|
|
2434
|
+
return propResolvesUnsafe(p, env, unsafe)
|
|
2449
2435
|
})
|
|
2450
2436
|
if (dropped) deferred.add(n.slotId)
|
|
2451
2437
|
}
|
|
@@ -2478,7 +2464,7 @@ export function computeDeferredChildSlots(
|
|
|
2478
2464
|
function generateCsrTemplateWithOpts(node: IRNode, opts: TemplateOptions): string {
|
|
2479
2465
|
const { restSpreadNames, propsObjectName, restPropsName, csrEnv, unsafeLocalNames, loopDepth = 0 } = opts
|
|
2480
2466
|
const env: CsrEnv = csrEnv ?? { substitutions: new Map(), propsObjectName: propsObjectName ?? null }
|
|
2481
|
-
const transformExpr = (expr:
|
|
2467
|
+
const transformExpr = (expr: JsSourceText, templateExpr?: JsSourceText): string => {
|
|
2482
2468
|
// Single AST substitution pass: replaces signal getter calls
|
|
2483
2469
|
// (`count()` → `(initialValue)`), memo calls (`bars()` → `(body)`),
|
|
2484
2470
|
// and inlinable-const refs (`label` → `(rewrittenValue)`) in one
|
|
@@ -2488,7 +2474,12 @@ function generateCsrTemplateWithOpts(node: IRNode, opts: TemplateOptions): strin
|
|
|
2488
2474
|
// the IR — emit does no string transformation of its own (#1277).
|
|
2489
2475
|
const source = templateExpr ?? expr
|
|
2490
2476
|
if (!source) return source
|
|
2491
|
-
const { rewritten, freeIdentifiers } = csrSubstitute(source, env, opts.scope)
|
|
2477
|
+
const { rewritten, freeIdentifiers, writes } = csrSubstitute(source, env, opts.scope)
|
|
2478
|
+
|
|
2479
|
+
// An expression that writes a binding (`{++nextId}`, a child prop
|
|
2480
|
+
// `() => ++start`) cannot run here: the template has no binding to
|
|
2481
|
+
// write, and the write belongs to init's run of the component.
|
|
2482
|
+
if (writes.size > 0) return UNSAFE_TEMPLATE_EXPR
|
|
2492
2483
|
|
|
2493
2484
|
// The CSR template runs at module scope, so any post-substitution
|
|
2494
2485
|
// free identifier that lands in `unsafeLocalNames` (init-body-only
|
|
@@ -11,6 +11,9 @@
|
|
|
11
11
|
* issue #1021 Stage B for the rationale.
|
|
12
12
|
*/
|
|
13
13
|
|
|
14
|
+
import ts from 'typescript'
|
|
15
|
+
import { isIdentifierLikeToken, isTriviaKind, iterateJsTokens } from '../scanner/js-scanner.ts'
|
|
16
|
+
|
|
14
17
|
/** JavaScript keywords and common globals to skip during identifier extraction. */
|
|
15
18
|
const KEYWORDS_AND_GLOBALS = new Set([
|
|
16
19
|
'true',
|
|
@@ -56,46 +59,94 @@ const KEYWORDS_AND_GLOBALS = new Set([
|
|
|
56
59
|
])
|
|
57
60
|
|
|
58
61
|
/**
|
|
59
|
-
*
|
|
62
|
+
* Words that can never name a binding in module (strict) code. Every other
|
|
63
|
+
* identifier-or-keyword token, contextual keywords included (`type`, `of`,
|
|
64
|
+
* `get`, …), can.
|
|
60
65
|
*/
|
|
61
|
-
|
|
62
|
-
const
|
|
63
|
-
if
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
66
|
+
const RESERVED_WORDS = new Set([
|
|
67
|
+
'await', 'break', 'case', 'catch', 'class', 'const', 'continue', 'debugger', 'default', 'delete', 'do',
|
|
68
|
+
'else', 'enum', 'export', 'extends', 'false', 'finally', 'for', 'function', 'if', 'implements', 'import',
|
|
69
|
+
'in', 'instanceof', 'interface', 'let', 'new', 'null', 'package', 'private', 'protected', 'public',
|
|
70
|
+
'return', 'static', 'super', 'switch', 'this', 'throw', 'true', 'try', 'typeof', 'var', 'void', 'while',
|
|
71
|
+
'with', 'yield',
|
|
72
|
+
])
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* Walk the tokens of `text` (`iterateJsTokens`, the shared JS lexer, which
|
|
76
|
+
* reads a regex literal as one token and closes template substitutions at
|
|
77
|
+
* their own `}`), calling `onName` for every identifier, member names
|
|
78
|
+
* included (`props.value` yields both: the reference graph reads a
|
|
79
|
+
* props-object member as a use of that prop), and `onSubstitution` with the
|
|
80
|
+
* source span of every outermost `${…}` body. String, regex and comment
|
|
81
|
+
* contents produce no names, and `$` is an identifier character, so
|
|
82
|
+
* `step$` / `$step` are found (#3536).
|
|
83
|
+
*
|
|
84
|
+
* A contextual keyword (`type`, `of`, `get`, …) is a legal binding name, so
|
|
85
|
+
* it is reported like an identifier; a reserved word is reported only as a
|
|
86
|
+
* member name (`props.default`).
|
|
87
|
+
*/
|
|
88
|
+
function scanNames(
|
|
89
|
+
text: string,
|
|
90
|
+
onName: (name: string) => void,
|
|
91
|
+
onSubstitution?: (start: number, end: number) => void,
|
|
92
|
+
): void {
|
|
93
|
+
// The source offset where each open template's current `${` body starts.
|
|
94
|
+
const bodyStarts: number[] = []
|
|
95
|
+
let prev: ts.SyntaxKind | undefined
|
|
96
|
+
for (const token of iterateJsTokens(text)) {
|
|
97
|
+
if (isTriviaKind(token.kind)) continue
|
|
98
|
+
if (token.kind === ts.SyntaxKind.TemplateHead) {
|
|
99
|
+
bodyStarts.push(token.end)
|
|
100
|
+
} else if (token.kind === ts.SyntaxKind.TemplateMiddle || token.kind === ts.SyntaxKind.TemplateTail) {
|
|
101
|
+
const bodyStart = bodyStarts.pop()
|
|
102
|
+
// Only an outermost body: a nested template stays inside its parent's.
|
|
103
|
+
if (bodyStart !== undefined && bodyStarts.length === 0) onSubstitution?.(bodyStart, token.pos)
|
|
104
|
+
if (token.kind === ts.SyntaxKind.TemplateMiddle) bodyStarts.push(token.end)
|
|
105
|
+
} else if (isIdentifierLikeToken(token.kind)) {
|
|
106
|
+
const name = text.slice(token.pos, token.end)
|
|
107
|
+
const isMemberName = prev === ts.SyntaxKind.DotToken || prev === ts.SyntaxKind.QuestionDotToken
|
|
108
|
+
if (isMemberName || !RESERVED_WORDS.has(name)) onName(name)
|
|
68
109
|
}
|
|
110
|
+
prev = token.kind
|
|
69
111
|
}
|
|
70
112
|
}
|
|
71
113
|
|
|
114
|
+
/** `text` as a template literal: already backtick-quoted, or quoted here. */
|
|
115
|
+
function asTemplateLiteral(text: string): string {
|
|
116
|
+
return text.startsWith('`') && text.endsWith('`') && text.length >= 2 ? text : `\`${text}\``
|
|
117
|
+
}
|
|
118
|
+
|
|
72
119
|
/**
|
|
73
|
-
*
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
120
|
+
* Extract identifiers from an expression string.
|
|
121
|
+
*/
|
|
122
|
+
export function extractIdentifiers(expr: string, set: Set<string>): void {
|
|
123
|
+
scanNames(expr, name => {
|
|
124
|
+
if (!isKeywordOrGlobal(name)) set.add(name)
|
|
125
|
+
})
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/**
|
|
129
|
+
* Yield each `${...}` substitution body inside a template-style string
|
|
130
|
+
* (backtick-quoted or not). Returns the inner expressions — the
|
|
131
|
+
* surrounding HTML / static text is stripped. Empty array when no
|
|
132
|
+
* substitutions are present. Nested braces and template literals inside a
|
|
133
|
+
* body are kept whole.
|
|
80
134
|
*/
|
|
81
135
|
export function extractTemplateExpressions(template: string): string[] {
|
|
82
|
-
const
|
|
136
|
+
const literal = asTemplateLiteral(template)
|
|
83
137
|
const out: string[] = []
|
|
84
|
-
|
|
85
|
-
while ((match = re.exec(template)) !== null) {
|
|
86
|
-
out.push(match[1])
|
|
87
|
-
}
|
|
138
|
+
scanNames(literal, () => {}, (start, end) => out.push(literal.slice(start, end)))
|
|
88
139
|
return out
|
|
89
140
|
}
|
|
90
141
|
|
|
91
142
|
/**
|
|
92
143
|
* Extract identifiers from template literal expressions.
|
|
93
|
-
*
|
|
144
|
+
* Only `${...}` substitutions are scanned; static text is not code.
|
|
94
145
|
*/
|
|
95
146
|
export function extractTemplateIdentifiers(template: string, set: Set<string>): void {
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
}
|
|
147
|
+
scanNames(asTemplateLiteral(template), name => {
|
|
148
|
+
if (!isKeywordOrGlobal(name)) set.add(name)
|
|
149
|
+
})
|
|
99
150
|
}
|
|
100
151
|
|
|
101
152
|
/**
|
|
@@ -3,9 +3,10 @@
|
|
|
3
3
|
*/
|
|
4
4
|
|
|
5
5
|
import ts from 'typescript'
|
|
6
|
-
import type { ComponentIR, IRNode } from '../types.ts'
|
|
6
|
+
import type { CompilerError, ComponentIR, ImportInfo, IRNode, SourceLocation } from '../types.ts'
|
|
7
|
+
import { createError, ErrorCodes } from '../errors.ts'
|
|
7
8
|
import { isClientBuiltinName } from '../builtins.ts'
|
|
8
|
-
import { collectValueReferencedNames } from '../value-references.ts'
|
|
9
|
+
import { analyzeEmittedReferences, collectValueReferencedNames } from '../value-references.ts'
|
|
9
10
|
import { identifierCallPattern } from '../identifier-pattern.ts'
|
|
10
11
|
|
|
11
12
|
// All exports from @barefootjs/client/runtime that may be used in generated code
|
|
@@ -84,32 +85,68 @@ export const RUNTIME_MODULE = '@barefootjs/client/runtime'
|
|
|
84
85
|
export const IMPORT_PLACEHOLDER = '/* __BAREFOOTJS_DOM_IMPORTS__ */'
|
|
85
86
|
export const MODULE_CONSTANTS_PLACEHOLDER = '/* __MODULE_LEVEL_CONSTANTS__ */'
|
|
86
87
|
|
|
88
|
+
/** Runtime names `detectUsedImports` can report: the candidates plus the slot finders. */
|
|
89
|
+
const RUNTIME_IMPORT_NAMES: ReadonlySet<string> = new Set<string>([...RUNTIME_IMPORT_CANDIDATES, '$', '$c', '$t'])
|
|
90
|
+
|
|
87
91
|
/**
|
|
88
|
-
* Detect which @barefootjs/client/runtime functions
|
|
92
|
+
* Detect which @barefootjs/client/runtime functions the generated code uses:
|
|
93
|
+
* the runtime names it references without binding them itself (#3537). A
|
|
94
|
+
* module-level user helper emitted as `var insert = …`, or a parameter that
|
|
95
|
+
* happens to be called `qsa`, binds its name, so it neither pulls in the
|
|
96
|
+
* runtime import nor collides with it as a duplicate declaration.
|
|
97
|
+
*
|
|
98
|
+
* Falls back to the per-name call scan only when the code does not parse
|
|
99
|
+
* cleanly, where narrowing on a partial parse could drop a needed import.
|
|
89
100
|
*/
|
|
90
101
|
export function detectUsedImports(code: string): Set<string> {
|
|
102
|
+
const refs = analyzeEmittedReferences(code)
|
|
103
|
+
if (refs) return new Set([...refs.free].filter(name => RUNTIME_IMPORT_NAMES.has(name)))
|
|
91
104
|
const used = new Set<string>()
|
|
92
105
|
for (const name of RUNTIME_IMPORT_CANDIDATES) {
|
|
93
|
-
|
|
94
|
-
if (identifierCallPattern(name).test(code)) {
|
|
95
|
-
used.add(name)
|
|
96
|
-
}
|
|
97
|
-
}
|
|
98
|
-
// Shorthand finders need special detection ($ is not a word character)
|
|
99
|
-
if (/\$c\s*\(/.test(code)) {
|
|
100
|
-
used.add('$c')
|
|
101
|
-
}
|
|
102
|
-
// Match $t( for text node finders
|
|
103
|
-
if (/\$t\s*\(/.test(code)) {
|
|
104
|
-
used.add('$t')
|
|
105
|
-
}
|
|
106
|
-
// Match $( but not $c( or $t( - use negative lookahead
|
|
107
|
-
if (/\$\s*\(/.test(code)) {
|
|
108
|
-
used.add('$')
|
|
106
|
+
if (identifierCallPattern(name).test(code)) used.add(name)
|
|
109
107
|
}
|
|
108
|
+
if (/\$c\s*\(/.test(code)) used.add('$c')
|
|
109
|
+
if (/\$t\s*\(/.test(code)) used.add('$t')
|
|
110
|
+
if (/\$\s*\(/.test(code)) used.add('$')
|
|
110
111
|
return used
|
|
111
112
|
}
|
|
112
113
|
|
|
114
|
+
/**
|
|
115
|
+
* BF057 for each module-level binding of the user's that takes a runtime
|
|
116
|
+
* helper's name (#3537): a module-level declaration the client JS re-emits,
|
|
117
|
+
* or a value import from anything but the runtime itself, whose name the
|
|
118
|
+
* emitted `code` also calls. That call may be the compiler's own, which the
|
|
119
|
+
* user's binding would capture, so it is refused rather than guessed at.
|
|
120
|
+
* This is exactly the case that used to emit both the binding and a runtime
|
|
121
|
+
* import of the same name, a duplicate-declaration SyntaxError. A binding
|
|
122
|
+
* the code never calls (`const qsa = {…}` read as `qsa.step`) stays legal.
|
|
123
|
+
*/
|
|
124
|
+
export function runtimeNameCollisions(
|
|
125
|
+
declarations: readonly { name: string; loc: SourceLocation }[],
|
|
126
|
+
imports: readonly ImportInfo[],
|
|
127
|
+
code: string,
|
|
128
|
+
): CompilerError[] {
|
|
129
|
+
const runtimeSources = new Set([RUNTIME_MODULE, '@barefootjs/client'])
|
|
130
|
+
const bound = [...declarations]
|
|
131
|
+
for (const imp of imports) {
|
|
132
|
+
if (imp.isTypeOnly || runtimeSources.has(imp.source)) continue
|
|
133
|
+
for (const spec of imp.specifiers) {
|
|
134
|
+
if (!spec.isTypeOnly) bound.push({ name: spec.alias ?? spec.name, loc: imp.loc })
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
const colliding = bound.filter(b => RUNTIME_IMPORT_NAMES.has(b.name))
|
|
138
|
+
if (colliding.length === 0) return []
|
|
139
|
+
const called = analyzeEmittedReferences(code)?.called
|
|
140
|
+
return colliding
|
|
141
|
+
.filter(b => called?.has(b.name) ?? true)
|
|
142
|
+
.map(b =>
|
|
143
|
+
createError(ErrorCodes.RUNTIME_HELPER_NAME_COLLISION, b.loc, {
|
|
144
|
+
message: `'${b.name}' is also a BarefootJS runtime helper, and the compiled client JS calls '${b.name}(…)'. ` +
|
|
145
|
+
'Rename the declaration, or import it under an alias.',
|
|
146
|
+
}),
|
|
147
|
+
)
|
|
148
|
+
}
|
|
149
|
+
|
|
113
150
|
/**
|
|
114
151
|
* Collect user-defined imports from @barefootjs/client (or the
|
|
115
152
|
* compiler-emitted /runtime subpath).
|
|
@@ -22,7 +22,7 @@ import { buildInlinableConstants, csrInlinableConstantsFromCtx } from './emit-re
|
|
|
22
22
|
import { buildEnvFromCtx } from './compute-inlinability.ts'
|
|
23
23
|
import { nameForRegistryRef, buildImportAliasMap, setActiveImportAliases } from './component-scope.ts'
|
|
24
24
|
import { resolveRestSpreadNames } from './prop-handling.ts'
|
|
25
|
-
import { IMPORT_PLACEHOLDER, RUNTIME_MODULE, detectUsedImports, collectExternalImports } from './imports.ts'
|
|
25
|
+
import { IMPORT_PLACEHOLDER, RUNTIME_MODULE, detectUsedImports, collectExternalImports, runtimeNameCollisions } from './imports.ts'
|
|
26
26
|
import { isInlinableInTemplate } from '../relocate.ts'
|
|
27
27
|
import { buildSourceMapFromIR, type SourceMapV3 } from './source-map.ts'
|
|
28
28
|
import { resolvePropDeclaredType, baseTypeName } from '../rich-type-evidence.ts'
|
|
@@ -391,6 +391,7 @@ function generateTemplateOnlyMount(ir: ComponentIR, ctx: ClientJsContext): strin
|
|
|
391
391
|
// same — without it, a constant whose value got inlined into the
|
|
392
392
|
// template (e.g. `useYjs(_p.x)`) leaves the template referencing a
|
|
393
393
|
// bare module-import name that's no longer imported (#1138 / #1133).
|
|
394
|
+
ctx.warnings.push(...runtimeNameCollisions([], ir.metadata.imports, generatedCode))
|
|
394
395
|
const externalImports = collectExternalImports(ir, generatedCode)
|
|
395
396
|
const allImports = externalImports.length > 0
|
|
396
397
|
? `${importLine}\n${externalImports.join('\n')}`
|
|
@@ -17,7 +17,7 @@
|
|
|
17
17
|
*/
|
|
18
18
|
|
|
19
19
|
import type { ClientJsContext } from '../types.ts'
|
|
20
|
-
import { toDomEventName, varSlotId,
|
|
20
|
+
import { toDomEventName, varSlotId, wrapHandlerForTurn } from '../utils.ts'
|
|
21
21
|
import type { LoopDelegationIndex } from '../control-flow/plan/loop-delegation-index.ts'
|
|
22
22
|
|
|
23
23
|
export function emitEventHandlers(
|
|
@@ -35,7 +35,7 @@ export function emitEventHandlers(
|
|
|
35
35
|
// profiling run attributes the reactive work it triggers to this turn.
|
|
36
36
|
const wrappedHandler = ctx.profile
|
|
37
37
|
? wrapHandlerForTurn(event.handler, `${ctx.componentName}#handler:${elem.slotId}:${event.name}`)
|
|
38
|
-
:
|
|
38
|
+
: event.handler.trim()
|
|
39
39
|
if (elem.slotId === '__scope') {
|
|
40
40
|
lines.push(` if (__scope) __scope.addEventListener('${eventName}', ${wrappedHandler})`)
|
|
41
41
|
} else {
|
|
@@ -1,6 +1,9 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* `props-extraction` phase — emit the one
|
|
3
|
-
*
|
|
2
|
+
* `props-extraction` phase — emit the one-time prop extractions:
|
|
3
|
+
* `const children = _p.children [?? <default>]` (or `const kids =
|
|
4
|
+
* _p.children` for a renamed `{ children: kids }`), and `let start =
|
|
5
|
+
* _p.start [?? <default>]` for each prop the component writes
|
|
6
|
+
* (`ParamInfo.reassigned`, see `keepsDeclaredValue`).
|
|
4
7
|
*
|
|
5
8
|
* `children` must stay a ONE-TIME read: a prop's slot-children value is a
|
|
6
9
|
* getter that INSTANTIATES child components when read (see
|
|
@@ -18,13 +21,10 @@
|
|
|
18
21
|
* downstream code there reads `_p.X` directly via the late-stage rename.
|
|
19
22
|
*/
|
|
20
23
|
|
|
21
|
-
import type
|
|
24
|
+
import { type PropUsage, isChildrenPropParam } from '../../types.ts'
|
|
22
25
|
import { computePropsUsedAsConditions } from '../compute-prop-usage.ts'
|
|
23
|
-
import { propReadBase, propReadFallback } from '../../props-binding.ts'
|
|
26
|
+
import { propReadBase, propReadFallback, keepsDeclaredValue } from '../../props-binding.ts'
|
|
24
27
|
import type { ClientJsContext } from '../types.ts'
|
|
25
|
-
import { PROPS_PARAM } from '../utils.ts'
|
|
26
|
-
|
|
27
|
-
const CHILDREN = 'children'
|
|
28
28
|
|
|
29
29
|
export function emitPropsExtraction(
|
|
30
30
|
lines: string[],
|
|
@@ -33,16 +33,38 @@ export function emitPropsExtraction(
|
|
|
33
33
|
propUsage: Map<string, PropUsage>,
|
|
34
34
|
): void {
|
|
35
35
|
if (ctx.propsObjectName) return
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
36
|
+
emitWrittenPropLocals(lines, ctx, propUsage)
|
|
37
|
+
// The children prop's local binding: `children`, or the renamed local of
|
|
38
|
+
// `{ children: kids }` (`isChildrenPropParam`, #3572), which reads
|
|
39
|
+
// `_p.children` the same one-time way. A local named `children` that binds
|
|
40
|
+
// another prop (`{ items: children }`) is an ordinary prop, read live.
|
|
41
|
+
// `neededProps` holds only props-param locals, so a component with no
|
|
42
|
+
// children param has nothing to extract.
|
|
43
|
+
const prop = ctx.propsParams.find(isChildrenPropParam)
|
|
44
|
+
if (!prop) return
|
|
45
|
+
const local = prop.name
|
|
46
|
+
if (!neededProps.has(local)) return
|
|
43
47
|
|
|
44
|
-
const usedAsCondition = computePropsUsedAsConditions(ctx, neededProps).has(
|
|
45
|
-
const fallback = propReadFallback(prop, propUsage.get(
|
|
48
|
+
const usedAsCondition = computePropsUsedAsConditions(ctx, neededProps).has(local)
|
|
49
|
+
const fallback = propReadFallback(prop, propUsage.get(local), usedAsCondition)
|
|
46
50
|
const read = propReadBase(prop) + (fallback === null ? '' : ` ?? ${fallback}`)
|
|
47
|
-
|
|
51
|
+
// A children binding the component writes needs a `let` to take the write.
|
|
52
|
+
lines.push(` ${keepsDeclaredValue(prop) ? 'const' : 'let'} ${local} = ${read}`, '')
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* A prop the component writes (`++start`, `start = clamp(start)`) is a
|
|
57
|
+
* local it owns, as in JS: declared once from the prop, then read and
|
|
58
|
+
* written in place. It is declared whether or not `neededProps` lists it,
|
|
59
|
+
* since a write is itself a use.
|
|
60
|
+
*/
|
|
61
|
+
function emitWrittenPropLocals(lines: string[], ctx: ClientJsContext, propUsage: Map<string, PropUsage>): void {
|
|
62
|
+
const written = ctx.propsParams.filter(p => !p.isRest && !isChildrenPropParam(p) && !keepsDeclaredValue(p))
|
|
63
|
+
if (written.length === 0) return
|
|
64
|
+
const usedAsCondition = computePropsUsedAsConditions(ctx, new Set(written.map(p => p.name)))
|
|
65
|
+
for (const prop of written) {
|
|
66
|
+
const fallback = propReadFallback(prop, propUsage.get(prop.name), usedAsCondition.has(prop.name))
|
|
67
|
+
lines.push(` let ${prop.name} = ${propReadBase(prop)}${fallback === null ? '' : ` ?? ${fallback}`}`)
|
|
68
|
+
}
|
|
69
|
+
lines.push('')
|
|
48
70
|
}
|
|
@@ -13,7 +13,7 @@ import type { Declaration } from '../declaration-sort.ts'
|
|
|
13
13
|
import type { ClientJsContext } from '../types.ts'
|
|
14
14
|
import type { ParamInfo, SignalInfo } from '../../types.ts'
|
|
15
15
|
import { inferDefaultValue, PROPS_PARAM, readsBareProp } from '../utils.ts'
|
|
16
|
-
import { livePropReadExpr } from '../../props-binding.ts'
|
|
16
|
+
import { livePropReadExpr, propDefaultText } from '../../props-binding.ts'
|
|
17
17
|
import { signalFactoryInitializer, signalSecondBinding } from '../../signal-initializer.ts'
|
|
18
18
|
import type {
|
|
19
19
|
ControlledSignalEffectPlan,
|
|
@@ -138,7 +138,7 @@ function resolveSignalInitialValue(signal: SignalInfo, lookups: DeclarationEmitL
|
|
|
138
138
|
const controlled = lookups.controlledBySignal.get(signal)
|
|
139
139
|
if (controlled) {
|
|
140
140
|
const prop = lookups.propByName.get(controlled.propName)
|
|
141
|
-
const defaultVal = prop
|
|
141
|
+
const defaultVal = (prop && propDefaultText(prop)) ?? inferDefaultValue(signal.type)
|
|
142
142
|
// `_p` is always keyed by the caller-facing name (#2524 CSR half).
|
|
143
143
|
return `${PROPS_PARAM}.${prop?.sourceName ?? controlled.propName} ?? ${defaultVal}`
|
|
144
144
|
}
|
|
@@ -130,6 +130,13 @@ function buildSingleCompPlan(
|
|
|
130
130
|
indexParam: elem.index || '__idx',
|
|
131
131
|
outerPreludeStatements: staticPreludeStatements(elem.preamble),
|
|
132
132
|
propsExpr: buildStaticPropsExpr(props),
|
|
133
|
+
// #3607: the static twin of the component-root loop's `rowEvents` — a
|
|
134
|
+
// static array has no delegation pass for a component row either
|
|
135
|
+
// (`loopEventDelegationVariant`), so each hydrated row attaches the
|
|
136
|
+
// handlers on its forwarded children directly.
|
|
137
|
+
rowEvents: elem.bindings.events
|
|
138
|
+
.filter(ev => ev.nestedLoops.length === 0)
|
|
139
|
+
.map(ev => ({ childSlotId: ev.childSlotId, eventName: ev.eventName, handler: ev.handler })),
|
|
133
140
|
}
|
|
134
141
|
}
|
|
135
142
|
|
|
@@ -25,6 +25,7 @@
|
|
|
25
25
|
* shapes were determined by inline branching on the IR.
|
|
26
26
|
*/
|
|
27
27
|
|
|
28
|
+
import type { LoopRowEventBinding } from '../control-flow/plan/loop.ts'
|
|
28
29
|
import type { PreludeStatements } from '../control-flow/plan/inner-loop.ts'
|
|
29
30
|
import type { LoopChildRefBinding } from '../control-flow/plan/loop.ts'
|
|
30
31
|
import type { LoopChildReactiveAttr, LoopChildReactiveText } from '../types.ts'
|
|
@@ -71,6 +72,13 @@ export interface SingleCompInitPlan {
|
|
|
71
72
|
outerPreludeStatements: PreludeStatements
|
|
72
73
|
/** Pre-built props object expression for the child component. */
|
|
73
74
|
propsExpr: PropsExpr
|
|
75
|
+
/**
|
|
76
|
+
* Event handlers on elements forwarded as the row component's own
|
|
77
|
+
* `children`, outside any inner loop (#3607). Attached to each hydrated
|
|
78
|
+
* row after its `initChild`. Unwrapped: the forEach binds `param` to the
|
|
79
|
+
* plain item, as for `propsExpr`.
|
|
80
|
+
*/
|
|
81
|
+
rowEvents: readonly LoopRowEventBinding[]
|
|
74
82
|
}
|
|
75
83
|
|
|
76
84
|
/** Plan for one depth-0 component inside `loop.nestedComponents`. */
|