@barefootjs/jsx 0.39.6 → 0.39.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/adapters/attr-plan.d.ts +8 -1
- package/dist/adapters/attr-plan.d.ts.map +1 -1
- package/dist/adapters/callback-method.d.ts +44 -0
- 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/computed-const.d.ts +52 -0
- package/dist/adapters/computed-const.d.ts.map +1 -0
- package/dist/adapters/dangerous-inner-html.d.ts +10 -0
- package/dist/adapters/dangerous-inner-html.d.ts.map +1 -1
- package/dist/adapters/dsl-template-adapter.d.ts +151 -0
- package/dist/adapters/dsl-template-adapter.d.ts.map +1 -0
- package/dist/adapters/element-shell.d.ts +44 -0
- package/dist/adapters/element-shell.d.ts.map +1 -0
- package/dist/adapters/interface.d.ts +2 -0
- package/dist/adapters/interface.d.ts.map +1 -1
- 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/parsed-expr-emitter.d.ts +27 -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/augment-inherited-props.d.ts +7 -0
- package/dist/augment-inherited-props.d.ts.map +1 -1
- package/dist/children-nullish.d.ts +22 -0
- package/dist/children-nullish.d.ts.map +1 -0
- package/dist/errors.d.ts +1 -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/fold-render-reassignments.d.ts +43 -0
- package/dist/fold-render-reassignments.d.ts.map +1 -0
- package/dist/free-refs.d.ts +15 -0
- package/dist/free-refs.d.ts.map +1 -1
- package/dist/index.d.ts +21 -14
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +24620 -23233
- package/dist/ir-to-client-js/compute-inlinability.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/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 +0 -13
- 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 +9 -19
- 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/index.d.ts.map +1 -1
- package/dist/ir-to-client-js/phases/props-extraction.d.ts +3 -2
- package/dist/ir-to-client-js/phases/props-extraction.d.ts.map +1 -1
- package/dist/ir-to-client-js/plan/declaration-emit.d.ts +5 -0
- package/dist/ir-to-client-js/plan/declaration-emit.d.ts.map +1 -1
- package/dist/ir-to-client-js/prop-handling.d.ts +11 -12
- package/dist/ir-to-client-js/prop-handling.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/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/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 +15 -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 +43 -12
- package/dist/nullable-signal-attr.d.ts.map +1 -1
- package/dist/prop-rewrite.d.ts +18 -0
- package/dist/prop-rewrite.d.ts.map +1 -1
- package/dist/props-binding.d.ts +43 -6
- package/dist/props-binding.d.ts.map +1 -1
- package/dist/relocate.d.ts +16 -0
- package/dist/relocate.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/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 +37 -18
- package/dist/ssr-seed-plan.d.ts.map +1 -1
- package/dist/types.d.ts +162 -20
- 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 +44 -0
- package/src/__tests__/block-memo-type.test.ts +51 -0
- package/src/__tests__/callback-method.test.ts +75 -0
- package/src/__tests__/children-read.test.ts +226 -0
- package/src/__tests__/client-only-date-lowering.test.ts +2 -2
- package/src/__tests__/client-template-escape-soundness.test.ts +31 -1
- package/src/__tests__/component-props.test.ts +68 -1
- package/src/__tests__/computed-const.test.ts +101 -0
- package/src/__tests__/constant-inlining-tree.test.ts +136 -0
- 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__/element-shell.test.ts +78 -0
- package/src/__tests__/event-handler-verbatim.test.ts +73 -0
- package/src/__tests__/expression-parser.test.ts +1 -44
- package/src/__tests__/fold-render-reassignments.test.ts +96 -0
- package/src/__tests__/inlinable-local-method-call.test.ts +81 -0
- package/src/__tests__/memo-body-expr.test.ts +120 -0
- package/src/__tests__/nullable-signal-attr.test.ts +67 -3
- package/src/__tests__/prop-handling.test.ts +35 -16
- package/src/__tests__/props-alias-const.test.ts +65 -0
- package/src/__tests__/reference-graph-identifiers.test.ts +161 -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__/signal-prop-seed.test.ts +90 -0
- package/src/__tests__/ssr-defaults.test.ts +19 -1
- package/src/__tests__/ssr-seed-plan.test.ts +173 -1
- package/src/__tests__/static-template-reachability.test.ts +137 -0
- package/src/__tests__/style-object-plan.test.ts +54 -0
- package/src/__tests__/zero-param-helper-inline.test.ts +57 -0
- package/src/adapters/attr-plan.ts +29 -1
- package/src/adapters/callback-method.ts +85 -0
- package/src/adapters/component-props.ts +130 -12
- package/src/adapters/computed-const.ts +185 -0
- package/src/adapters/dangerous-inner-html.ts +33 -0
- package/src/adapters/dsl-template-adapter.ts +248 -0
- package/src/adapters/element-shell.ts +160 -0
- package/src/adapters/interface.ts +8 -0
- package/src/adapters/jsx-adapter.ts +12 -6
- package/src/adapters/literal-value.ts +2 -2
- package/src/adapters/parsed-expr-emitter.ts +34 -0
- package/src/adapters/style-object.ts +61 -0
- package/src/analyzer.ts +382 -94
- package/src/augment-inherited-props.ts +12 -0
- package/src/children-nullish.ts +33 -0
- package/src/debug.ts +2 -2
- package/src/errors.ts +12 -0
- package/src/expression-parser.ts +56 -53
- package/src/fold-render-reassignments.ts +314 -0
- package/src/free-refs.ts +77 -17
- package/src/index.ts +27 -10
- package/src/ir-to-client-js/compute-inlinability.ts +122 -19
- 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/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 +25 -37
- package/src/ir-to-client-js/emit-reactive.ts +2 -2
- package/src/ir-to-client-js/emit-registration.ts +31 -59
- package/src/ir-to-client-js/generate-init.ts +17 -1
- package/src/ir-to-client-js/html-template.ts +56 -120
- 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 +5 -3
- package/src/ir-to-client-js/init-declarations.ts +1 -1
- package/src/ir-to-client-js/phases/event-handlers.ts +2 -2
- package/src/ir-to-client-js/phases/props-extraction.ts +16 -15
- package/src/ir-to-client-js/plan/build-declaration-emit.ts +26 -52
- package/src/ir-to-client-js/plan/declaration-emit.ts +5 -0
- package/src/ir-to-client-js/prop-handling.ts +13 -54
- 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 +27 -9
- package/src/ir-to-client-js/safe-html.ts +29 -35
- package/src/ir-to-client-js/stringify/declaration-emit.ts +1 -1
- package/src/ir-to-client-js/types.ts +3 -2
- package/src/ir-to-client-js/utils.ts +13 -26
- package/src/jsx-to-ir.ts +318 -130
- package/src/nullable-signal-attr.ts +156 -37
- package/src/prop-rewrite.ts +41 -0
- package/src/props-binding.ts +117 -7
- package/src/relocate.ts +40 -8
- package/src/scanner/__tests__/js-scanner.test.ts +37 -0
- package/src/scanner/js-scanner.ts +64 -4
- package/src/signal-prop-name-collision.ts +50 -24
- package/src/ssr-defaults.ts +47 -20
- package/src/ssr-seed-plan.ts +121 -29
- package/src/types.ts +165 -21
- package/src/value-references.ts +132 -0
|
@@ -0,0 +1,248 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The `generate()` the eight DSL template adapters (Blade, ERB, Jinja,
|
|
3
|
+
* Mojolicious, Pebble, MiniJinja, Twig, Xslate) share (#3475), with the
|
|
4
|
+
* per-compile state it sets up. Each adapter renders the nodes in its own
|
|
5
|
+
* template language; how one compile begins and ends lives here once.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import { augmentInheritedPropAccesses, collectModuleStringConsts } from '../augment-inherited-props.ts'
|
|
9
|
+
import { buildImportAliasMap } from '../ir-to-client-js/component-scope.ts'
|
|
10
|
+
import { prepareLoweringMatchers, type LoweringMatcher } from '../lowering-registry.ts'
|
|
11
|
+
import {
|
|
12
|
+
collectNullableSignalGetters,
|
|
13
|
+
collectNullishAttrContext,
|
|
14
|
+
type NullishAttrContext,
|
|
15
|
+
} from '../nullable-signal-attr.ts'
|
|
16
|
+
import { BindingScope } from '../scope/binding-scope.ts'
|
|
17
|
+
import { rootPropAliasNames } from '../scope/root-prop-alias.ts'
|
|
18
|
+
import type { CompilerError, ComponentIR, IRIfStatement, IRMetadata } from '../types.ts'
|
|
19
|
+
import { collectImportedLoopChildComponentErrors } from './component-tree.ts'
|
|
20
|
+
import { computedConstContext, type ComputedConstContext } from './computed-const.ts'
|
|
21
|
+
import { searchParamsLocalNames } from './env-signal.ts'
|
|
22
|
+
import { BaseAdapter, type AdapterGenerateOptions, type AdapterOutput } from './interface.ts'
|
|
23
|
+
import { collectBooleanTypedProps, collectNullableOptionalProps } from './prop-classes.ts'
|
|
24
|
+
import { templateNameCollisionErrors } from './template-name-collisions.ts'
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* How an adapter that renames reserved names spells a source identifier as
|
|
28
|
+
* a template variable, and the engine name its collision diagnostic uses.
|
|
29
|
+
*/
|
|
30
|
+
export interface TemplateIdentifierSpelling {
|
|
31
|
+
/** The adapter's mangling (`jinjaIdent`, `twigIdent`, …). */
|
|
32
|
+
ident(name: string): string
|
|
33
|
+
/** The engine name in the diagnostic ("Jinja", "MiniJinja", …). */
|
|
34
|
+
label: string
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export abstract class DslTemplateAdapter extends BaseAdapter {
|
|
38
|
+
// ===========================================================================
|
|
39
|
+
// Per-compile state, set at `generate()` entry
|
|
40
|
+
// ===========================================================================
|
|
41
|
+
|
|
42
|
+
protected componentName: string = ''
|
|
43
|
+
/** Diagnostics collected while rendering, merged into `ir.errors` at the end. */
|
|
44
|
+
protected errors: CompilerError[] = []
|
|
45
|
+
/**
|
|
46
|
+
* SolidJS-style props identifier (`function(props: P)`). The spread
|
|
47
|
+
* emission builds a props-object bag from it without re-walking the IR.
|
|
48
|
+
*/
|
|
49
|
+
protected propsObjectName: string | null = null
|
|
50
|
+
/**
|
|
51
|
+
* Name of the destructured `...rest` binding (`function X({ ...rest })`),
|
|
52
|
+
* if any. A closed-type rest-expanded attribute's expression is
|
|
53
|
+
* `${restPropsName}.${key}` (`expandSpreadAttribute`, #3057). Stripping
|
|
54
|
+
* this prefix the same way `propsObjectName.` is stripped lets the
|
|
55
|
+
* nullable-optional-props guard recognise it as a bare optional reference
|
|
56
|
+
* instead of falling through to unconditional emission.
|
|
57
|
+
*/
|
|
58
|
+
protected restPropsName: string | null = null
|
|
59
|
+
protected propsParams: { name: string; optional: boolean }[] = []
|
|
60
|
+
/** Props whose declared TS type is boolean (`collectBooleanTypedProps`). */
|
|
61
|
+
protected booleanTypedProps: Set<string> = new Set()
|
|
62
|
+
/** Names (signal getters + props) whose value is a string (`stringValueNamesFor`). */
|
|
63
|
+
protected stringValueNames: Set<string> = new Set()
|
|
64
|
+
/**
|
|
65
|
+
* Module-scope pure-string consts (`const x = 'literal'`), keyed by name →
|
|
66
|
+
* unescaped value. A className template literal that references such a
|
|
67
|
+
* const (`className={`${x} ${className}`}`) must inline the literal: the
|
|
68
|
+
* const is module-scope, so it never reaches the per-render context.
|
|
69
|
+
*/
|
|
70
|
+
protected moduleStringConsts: Map<string, string> = new Map()
|
|
71
|
+
/**
|
|
72
|
+
* (#1922) Local binding names the request-scoped `searchParams()` env
|
|
73
|
+
* signal is imported under (handles `import { searchParams as sp }`). When
|
|
74
|
+
* non-empty the emitter lowers a `<binding>().get(k)` call to a real method
|
|
75
|
+
* call on the per-request `searchParams` reader instead of the generic
|
|
76
|
+
* deref. Read by the top-level ParsedExpr emitter.
|
|
77
|
+
*/
|
|
78
|
+
protected _searchParamsLocals: Set<string> = new Set()
|
|
79
|
+
/**
|
|
80
|
+
* Call-lowering matchers active for this component (#2057), read by the
|
|
81
|
+
* top-level emitter. Covers both userland plugins and the compiler's
|
|
82
|
+
* built-in plugins (e.g. `queryHref`, #2042): one uniform path, no per-API
|
|
83
|
+
* branch.
|
|
84
|
+
*/
|
|
85
|
+
protected _loweringMatchers: LoweringMatcher[] = []
|
|
86
|
+
/** The component's computed consts, inlined where a template reads them (#3494). */
|
|
87
|
+
protected computedConsts: ComputedConstContext | null = null
|
|
88
|
+
/**
|
|
89
|
+
* Local + module constants from the IR, used by the conditional-spread and
|
|
90
|
+
* `Record<staticKeys, scalar>[propKey]` lowering paths (#textarea /
|
|
91
|
+
* #checkbox): the spread emission resolves a bare local const
|
|
92
|
+
* (`const sizeAttrs = size ? {…} : {}`) to its initializer text.
|
|
93
|
+
*/
|
|
94
|
+
protected localConstants: IRMetadata['localConstants'] = []
|
|
95
|
+
/** Each prop's collision-free root alias (#3314), decided once per component. */
|
|
96
|
+
protected rootPropAliases: ReadonlyMap<string, string> = new Map()
|
|
97
|
+
/**
|
|
98
|
+
* The one canonical, position-accurate "names bound by an enclosing loop
|
|
99
|
+
* callback" service (#2482 Stage 2). Every shadow-guard site reads this
|
|
100
|
+
* one threaded, immutable scope: `renderLoop` enters the row with
|
|
101
|
+
* `enterLoopRow` around the loop's children and restores the outer scope
|
|
102
|
+
* by reference afterwards, mirroring `ctx.scope` in `jsx-to-ir.ts`.
|
|
103
|
+
* `IRLoop` structurally satisfies `LoopBindingSource`, so `renderLoop`
|
|
104
|
+
* passes the loop node straight to `enterLoopRow`.
|
|
105
|
+
*/
|
|
106
|
+
protected scope: BindingScope = BindingScope.EMPTY
|
|
107
|
+
/**
|
|
108
|
+
* Optional, no-default props that are absent when the caller omits them
|
|
109
|
+
* (`collectNullableOptionalProps`). A bare-reference attribute of one is
|
|
110
|
+
* guarded with the template's presence test, so the attribute drops rather
|
|
111
|
+
* than rendering `attr=""` (Hono-style nullish omission, e.g. textarea's
|
|
112
|
+
* `rows`).
|
|
113
|
+
*/
|
|
114
|
+
protected nullableOptionalProps: Set<string> = new Set()
|
|
115
|
+
/** Getters of nullable signals (`collectNullableSignalGetters`, #3304). */
|
|
116
|
+
protected nullableSignalGetters: Set<string> = new Set()
|
|
117
|
+
/** Signals and memos `attrValueMayBeNullish` reads (#3322). */
|
|
118
|
+
protected nullishAttrCtx: NullishAttrContext = { nullableSignals: new Set(), memos: new Map() }
|
|
119
|
+
/**
|
|
120
|
+
* Local alias -> declared/exported name for imported components (#2822,
|
|
121
|
+
* the SSR-side counterpart of #2777's client-JS registry-key fix). A child
|
|
122
|
+
* referenced under an import alias (`import { Foo as Bar }`, `<Bar/>`)
|
|
123
|
+
* must build its cross-template call against the child's own declared
|
|
124
|
+
* name (`Foo`, what `foo.tsx` registers its template as), never the
|
|
125
|
+
* caller-local binding. Read by `childTemplateName`.
|
|
126
|
+
*/
|
|
127
|
+
protected importAliases: Map<string, string> = new Map()
|
|
128
|
+
/** Numbers the children captures of a child component that has no slot id. */
|
|
129
|
+
protected childrenCaptureCounter = 0
|
|
130
|
+
|
|
131
|
+
// ===========================================================================
|
|
132
|
+
// What each adapter supplies
|
|
133
|
+
// ===========================================================================
|
|
134
|
+
|
|
135
|
+
/** The adapter's name in the BF103 imported-loop-child diagnostic ("The Jinja adapter emits …"). */
|
|
136
|
+
protected abstract readonly loopChildDiagnosticName: string
|
|
137
|
+
/**
|
|
138
|
+
* For an adapter that renames reserved names: two source names the
|
|
139
|
+
* renaming turns into one template variable would read the same value, so
|
|
140
|
+
* `generate()` refuses them (#3404). `null` when the adapter renames none.
|
|
141
|
+
*/
|
|
142
|
+
protected readonly templateIdentifierSpelling: TemplateIdentifierSpelling | null = null
|
|
143
|
+
/** The names whose value is a string, for `stringValueNames`. */
|
|
144
|
+
protected abstract stringValueNamesFor(ir: ComponentIR): Set<string>
|
|
145
|
+
/**
|
|
146
|
+
* Sets the adapter's own per-compile state. Runs after the shared state
|
|
147
|
+
* above and before any diagnostic or rendering.
|
|
148
|
+
*/
|
|
149
|
+
protected resetAdapterState(_ir: ComponentIR): void {}
|
|
150
|
+
/** The root `if` statement of a component with conditional returns. */
|
|
151
|
+
protected abstract renderIfStatement(ifStmt: IRIfStatement): string
|
|
152
|
+
/** The script registrations the template starts with. */
|
|
153
|
+
protected abstract generateScriptRegistrations(
|
|
154
|
+
ir: ComponentIR,
|
|
155
|
+
scriptBaseName?: string,
|
|
156
|
+
scriptAssets?: string[],
|
|
157
|
+
preloadAssets?: string[],
|
|
158
|
+
): string
|
|
159
|
+
/**
|
|
160
|
+
* SSR context consumers (`const x = useContext(Ctx)`): seeds each local
|
|
161
|
+
* from the active provider value, or the `createContext` default. The
|
|
162
|
+
* provider side pushes the value via `emitProvider` (#1297).
|
|
163
|
+
*/
|
|
164
|
+
protected abstract contextConsumerSeed(ir: ComponentIR): string
|
|
165
|
+
/**
|
|
166
|
+
* Prop/signal-derived memos with a `null` static SSR default (e.g.
|
|
167
|
+
* `createMemo(() => props.value * 10)`), computed in-template from the
|
|
168
|
+
* already-seeded prop/signal vars, mirroring Go's generated child
|
|
169
|
+
* constructor (#1297).
|
|
170
|
+
*/
|
|
171
|
+
protected abstract derivedMemoSeed(ir: ComponentIR): string
|
|
172
|
+
|
|
173
|
+
generate(ir: ComponentIR, options?: AdapterGenerateOptions): AdapterOutput {
|
|
174
|
+
this.resetCompileState(ir)
|
|
175
|
+
this.collectCompileErrors(ir, options)
|
|
176
|
+
|
|
177
|
+
const templateBody = ir.root.type === 'if-statement'
|
|
178
|
+
? this.renderIfStatement(ir.root as IRIfStatement)
|
|
179
|
+
: this.renderNode(ir.root)
|
|
180
|
+
const scriptReg = options?.skipScriptRegistration
|
|
181
|
+
? ''
|
|
182
|
+
: this.generateScriptRegistrations(ir, options?.scriptBaseName, options?.scriptAssets, options?.preloadAssets)
|
|
183
|
+
const ctxSeed = this.contextConsumerSeed(ir)
|
|
184
|
+
const memoSeed = this.derivedMemoSeed(ir)
|
|
185
|
+
const template = `${scriptReg}${ctxSeed}${memoSeed}${templateBody}\n`
|
|
186
|
+
|
|
187
|
+
if (this.errors.length > 0) {
|
|
188
|
+
ir.errors.push(...this.errors)
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
// A DSL template has no JS-style imports / types / default-export
|
|
192
|
+
// sections. The `templatesPerComponent` mode emits one file per
|
|
193
|
+
// component using the raw `template` value; sections are populated for
|
|
194
|
+
// contract uniformity so the compiler never has to string-parse the
|
|
195
|
+
// template.
|
|
196
|
+
return {
|
|
197
|
+
template,
|
|
198
|
+
sections: { imports: '', types: '', component: template, defaultExport: '' },
|
|
199
|
+
extension: this.extension,
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
private resetCompileState(ir: ComponentIR): void {
|
|
204
|
+
this.componentName = ir.metadata.componentName
|
|
205
|
+
this.propsObjectName = ir.metadata.propsObjectName ?? null
|
|
206
|
+
this.restPropsName = ir.metadata.restPropsName ?? null
|
|
207
|
+
// (#checkbox) Enumerate the props-object pattern's inherited attribute
|
|
208
|
+
// accesses (`props.className`/`id`/`disabled`) into propsParams, before
|
|
209
|
+
// deriving `nullableOptionalProps` below, so a bare optional attribute
|
|
210
|
+
// like `id={props.id}` gets the template's presence guard. This mutates
|
|
211
|
+
// the IR: a harness that serialized it before `generate` doesn't see it.
|
|
212
|
+
augmentInheritedPropAccesses(ir)
|
|
213
|
+
this.propsParams = ir.metadata.propsParams.map(p => ({ name: p.name, optional: p.optional }))
|
|
214
|
+
this.booleanTypedProps = collectBooleanTypedProps(ir)
|
|
215
|
+
this.localConstants = ir.metadata.localConstants ?? []
|
|
216
|
+
this.rootPropAliases = rootPropAliasNames(ir, this.propsParams.map(p => p.name))
|
|
217
|
+
this.scope = BindingScope.EMPTY
|
|
218
|
+
this.nullableOptionalProps = collectNullableOptionalProps(ir)
|
|
219
|
+
this.nullableSignalGetters = collectNullableSignalGetters(ir)
|
|
220
|
+
this.nullishAttrCtx = collectNullishAttrContext(ir)
|
|
221
|
+
this.stringValueNames = this.stringValueNamesFor(ir)
|
|
222
|
+
this.moduleStringConsts = collectModuleStringConsts(ir.metadata.localConstants)
|
|
223
|
+
this._searchParamsLocals = searchParamsLocalNames(ir.metadata)
|
|
224
|
+
this._loweringMatchers = prepareLoweringMatchers(ir.metadata)
|
|
225
|
+
this.computedConsts = computedConstContext(ir.metadata, name => this.scope.isBound(name))
|
|
226
|
+
this.importAliases = buildImportAliasMap(ir.metadata.imports ?? [])
|
|
227
|
+
this.childrenCaptureCounter = 0
|
|
228
|
+
this.errors = []
|
|
229
|
+
this.resetAdapterState(ir)
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
private collectCompileErrors(ir: ComponentIR, options?: AdapterGenerateOptions): void {
|
|
233
|
+
const spelling = this.templateIdentifierSpelling
|
|
234
|
+
if (spelling) {
|
|
235
|
+
const propNames = this.propsParams.map(p => p.name)
|
|
236
|
+
this.errors.push(...templateNameCollisionErrors(ir, propNames, spelling.ident, spelling.label, this.componentName))
|
|
237
|
+
}
|
|
238
|
+
// A child component referenced inside a loop body that is imported from
|
|
239
|
+
// a sibling .tsx renders as a cross-template call, which resolves only if
|
|
240
|
+
// the sibling template is registered alongside the parent at render
|
|
241
|
+
// time. Surface it loudly here, unless the caller (e.g. the barefoot
|
|
242
|
+
// CLI) guarantees that every sibling template is registered on the same
|
|
243
|
+
// template instance.
|
|
244
|
+
if (!options?.siblingTemplatesRegistered) {
|
|
245
|
+
this.errors.push(...collectImportedLoopChildComponentErrors(ir, this.componentName, this.loopChildDiagnosticName))
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
}
|
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The HTML element every template adapter writes the same way (#3475): its
|
|
3
|
+
* attributes, its hydration markers, its children, and the void-element
|
|
4
|
+
* form. The adapter supplies how each value is spelled in its template
|
|
5
|
+
* language; the order and the conditions live here once.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import { BF_PORTAL_OWNER, BF_REGION } from '@barefootjs/shared'
|
|
9
|
+
import type { IRElement, IRFragment, IRNode } from '../types.ts'
|
|
10
|
+
import { emitAttrValue, type AttrValueEmitter } from './attr-value-emitter.ts'
|
|
11
|
+
import { isDangerousInnerHtmlAttr } from './dangerous-inner-html.ts'
|
|
12
|
+
|
|
13
|
+
const VOID_ELEMENTS = [
|
|
14
|
+
'area', 'base', 'br', 'col', 'embed', 'hr', 'img', 'input',
|
|
15
|
+
'link', 'meta', 'param', 'source', 'track', 'wbr',
|
|
16
|
+
]
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* The element's own attributes, each lowered through `emitter`, as a
|
|
20
|
+
* leading-space list (`''` when none renders).
|
|
21
|
+
*/
|
|
22
|
+
export function renderElementAttributes(element: IRElement, emitter: AttrValueEmitter): string {
|
|
23
|
+
const parts: string[] = []
|
|
24
|
+
|
|
25
|
+
for (const attr of element.attrs) {
|
|
26
|
+
// `/* @client */` attribute bindings are deferred to hydrate: the
|
|
27
|
+
// client runtime sets/patches the attribute in a mount effect (the
|
|
28
|
+
// CSR template omits it; ir-to-client-js emits the setAttribute
|
|
29
|
+
// effect). Skip SSR emission so the server omits the attribute and
|
|
30
|
+
// the unsupported-expression lowering is never reached for a deferred
|
|
31
|
+
// predicate (no BF101 / BF102). #1966
|
|
32
|
+
if (attr.clientOnly) continue
|
|
33
|
+
// `dangerouslySetInnerHTML` never renders as an HTML attribute — it's
|
|
34
|
+
// handled by `renderDangerousInnerHtml` instead, which replaces the
|
|
35
|
+
// element's children. Skip it here so its `{ __html: ... }` object
|
|
36
|
+
// literal never reaches the generic object-literal BF101 refusal
|
|
37
|
+
// (which would double-report alongside the purpose-built one).
|
|
38
|
+
if (isDangerousInnerHtmlAttr(attr)) continue
|
|
39
|
+
// `key` never renders as a literal HTML attribute — #2753 resolves it
|
|
40
|
+
// onto `element.keyAttr` (name AND value) at IR-build time, emitted
|
|
41
|
+
// separately by `renderElementShell`. A stray `key` that jsx-to-ir.ts did
|
|
42
|
+
// NOT recognize as a loop row root (so `keyAttr` was never set) has no
|
|
43
|
+
// defined rendering outside a `.map()` anyway; drop it rather than
|
|
44
|
+
// leak an invalid `key="..."` HTML attribute.
|
|
45
|
+
if (attr.name === 'key') continue
|
|
46
|
+
// Rewrite JSX special-prop names to their HTML-attribute counterparts (#1475).
|
|
47
|
+
const attrName = attr.name === 'className' ? 'class' : attr.name
|
|
48
|
+
const lowered = emitAttrValue(attr.value, emitter, attrName)
|
|
49
|
+
if (lowered) parts.push(lowered)
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
return parts.length > 0 ? ' ' + parts.join(' ') : ''
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/** How one adapter spells the parts of an element {@link renderElementShell} writes. */
|
|
56
|
+
export interface ElementShellSpelling {
|
|
57
|
+
/** Lowers each attribute value, the row key included. */
|
|
58
|
+
attrEmitter: AttrValueEmitter
|
|
59
|
+
/** The element's `dangerouslySetInnerHTML` content, or `null` when it has none. */
|
|
60
|
+
renderDangerousInnerHtml(element: IRElement): string | null
|
|
61
|
+
renderChildren(children: IRNode[]): string
|
|
62
|
+
renderScopeMarker(instanceIdExpr: string): string
|
|
63
|
+
renderSlotMarker(slotId: string): string
|
|
64
|
+
/**
|
|
65
|
+
* The raw print of the runtime's `data-key` attribute, for a render root
|
|
66
|
+
* that carries whatever key its own caller supplies (the `bf` instance's
|
|
67
|
+
* `data_key` field).
|
|
68
|
+
*/
|
|
69
|
+
dataKeyRelay: string
|
|
70
|
+
/** The escaped print of this instance's scope id, the `bf-po` value. */
|
|
71
|
+
scopeIdValue: string
|
|
72
|
+
/** Sends an SSR-portal element to the `bf.portals()` outlet instead of placing it inline. */
|
|
73
|
+
wrapSsrPortalElement(rendered: string): string
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/** The element, from its open tag to its close tag. */
|
|
77
|
+
export function renderElementShell(element: IRElement, spelling: ElementShellSpelling): string {
|
|
78
|
+
const tag = element.tag
|
|
79
|
+
const attrs = renderElementAttributes(element, spelling.attrEmitter)
|
|
80
|
+
const dangerousHtml = spelling.renderDangerousInnerHtml(element)
|
|
81
|
+
const children = dangerousHtml !== null ? dangerousHtml : spelling.renderChildren(element.children)
|
|
82
|
+
|
|
83
|
+
let hydrationAttrs = ''
|
|
84
|
+
if (element.needsScope) {
|
|
85
|
+
hydrationAttrs += ` ${spelling.renderScopeMarker('')}`
|
|
86
|
+
}
|
|
87
|
+
// #2753: `element.keyAttr` is the ONE IR-resolved decision for this
|
|
88
|
+
// element's row-key attribute — replacing both the `carriesDataKey`
|
|
89
|
+
// boolean and the adapters' own `rootScopeNodes`/`needsScope` check
|
|
90
|
+
// (mechanism 2: a render-root relay, including the #2732 fragment-root
|
|
91
|
+
// case) and the separate `currentLoopKeyDepth`-driven rewrite of a
|
|
92
|
+
// literal `key` attribute the adapters used to do in `renderAttributes`
|
|
93
|
+
// (mechanism 1: a `.map()` row root compiled inline here).
|
|
94
|
+
if (element.keyAttr) {
|
|
95
|
+
if (element.keyAttr.value !== undefined) {
|
|
96
|
+
const lowered = emitAttrValue(
|
|
97
|
+
{ kind: 'expression', expr: element.keyAttr.value },
|
|
98
|
+
spelling.attrEmitter,
|
|
99
|
+
element.keyAttr.name,
|
|
100
|
+
)
|
|
101
|
+
if (lowered) hydrationAttrs += ` ${lowered}`
|
|
102
|
+
} else {
|
|
103
|
+
// Relay: this element is one of THIS component's own render roots,
|
|
104
|
+
// carrying whatever key its OWN caller supplies at runtime when this
|
|
105
|
+
// component is itself invoked as a caller's keyed loop row. Mirrors
|
|
106
|
+
// Hono stamping data-key on each loop item's root, including
|
|
107
|
+
// early-return (if-statement) roots (#1297), and #2732's fragment-root
|
|
108
|
+
// case.
|
|
109
|
+
hydrationAttrs += ` ${spelling.dataKeyRelay}`
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
if (element.slotId) {
|
|
113
|
+
hydrationAttrs += ` ${spelling.renderSlotMarker(element.slotId)}`
|
|
114
|
+
}
|
|
115
|
+
// Page-lifecycle boundary lowered from `<Region>` (spec/router.md). The id
|
|
116
|
+
// is a deterministic static string (`<file scope>:<index>`), so it emits as
|
|
117
|
+
// a plain literal attribute — no template tag.
|
|
118
|
+
if (element.regionId) {
|
|
119
|
+
hydrationAttrs += ` ${BF_REGION}="${element.regionId}"`
|
|
120
|
+
}
|
|
121
|
+
// #3119: the `ref`-callback SSR-portal pattern (`ssrPortalOwnerScope`,
|
|
122
|
+
// `isSsrPortalRefCallback` in `@barefootjs/jsx`) — the overlay/content
|
|
123
|
+
// pattern the dialog-style primitives use. Stamp `bf-po` directly on
|
|
124
|
+
// this element's own tag, matching exactly what the client
|
|
125
|
+
// `createPortal(el, document.body, { ownerScope })` stamps onto the
|
|
126
|
+
// SAME element at hydrate time (`packages/client/src/runtime/
|
|
127
|
+
// portal.ts`), mirroring the Hono reference adapter's identical
|
|
128
|
+
// `BF_PORTAL_OWNER` stamp. The element itself is then routed through
|
|
129
|
+
// `wrapSsrPortalElement` below instead of returned for inline
|
|
130
|
+
// placement, so it renders at the portal outlet the app's own layout
|
|
131
|
+
// places near `</body>`.
|
|
132
|
+
if (element.ssrPortalOwnerScope) {
|
|
133
|
+
hydrationAttrs += ` ${BF_PORTAL_OWNER}="${spelling.scopeIdValue}"`
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
const rendered = VOID_ELEMENTS.includes(tag.toLowerCase())
|
|
137
|
+
? `<${tag}${attrs}${hydrationAttrs}>`
|
|
138
|
+
: `<${tag}${attrs}${hydrationAttrs}>${children}</${tag}>`
|
|
139
|
+
|
|
140
|
+
if (element.ssrPortalOwnerScope) {
|
|
141
|
+
return spelling.wrapSsrPortalElement(rendered)
|
|
142
|
+
}
|
|
143
|
+
return rendered
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
/**
|
|
147
|
+
* A fragment's children. A component-root fragment has no element of its
|
|
148
|
+
* own to carry the scope, so `wrapScopeComment` brackets the children with
|
|
149
|
+
* the comment-based scope marker pair; its end marker bounds the scope's
|
|
150
|
+
* sibling range, or queries from the fragment scope leak onto later
|
|
151
|
+
* siblings owned by the parent (#2289).
|
|
152
|
+
*/
|
|
153
|
+
export function renderFragmentScope(
|
|
154
|
+
fragment: IRFragment,
|
|
155
|
+
renderChildren: (children: IRNode[]) => string,
|
|
156
|
+
wrapScopeComment: (rendered: string) => string,
|
|
157
|
+
): string {
|
|
158
|
+
const children = renderChildren(fragment.children)
|
|
159
|
+
return fragment.needsScopeComment ? wrapScopeComment(children) : children
|
|
160
|
+
}
|
|
@@ -388,6 +388,14 @@ export abstract class BaseAdapter implements TemplateAdapter {
|
|
|
388
388
|
return children.map((child) => this.renderNode(child)).join('')
|
|
389
389
|
}
|
|
390
390
|
|
|
391
|
+
/** `null` for a `null` / `undefined` expression, which renders nothing; otherwise the node. */
|
|
392
|
+
protected renderNodeOrNull(node: IRNode): string | null {
|
|
393
|
+
if (node.type === 'expression' && (node.expr === 'null' || node.expr === 'undefined')) {
|
|
394
|
+
return null
|
|
395
|
+
}
|
|
396
|
+
return this.renderNode(node)
|
|
397
|
+
}
|
|
398
|
+
|
|
391
399
|
/** Default: render fallback + children inline (no streaming). Override for streaming support. */
|
|
392
400
|
renderAsync(node: IRAsync): string {
|
|
393
401
|
return this.renderNode(node.fallback) + this.renderChildren(node.children)
|
|
@@ -258,11 +258,11 @@ export abstract class JsxAdapter extends BaseAdapter {
|
|
|
258
258
|
lines.push(` ${keyword} ${constant.name}${typeAnnotation}`)
|
|
259
259
|
continue
|
|
260
260
|
}
|
|
261
|
-
|
|
262
|
-
//
|
|
261
|
+
// Skip client-only constructs in SSR (`ConstantInfo.systemConstructKind`,
|
|
262
|
+
// decided by the analyzer from the initializer's AST, #3542):
|
|
263
263
|
// - createContext() — only used client-side via provideContext/useContext
|
|
264
264
|
// - new WeakMap() — client-side cross-component shared state
|
|
265
|
-
if (
|
|
265
|
+
if (constant.systemConstructKind) continue
|
|
266
266
|
|
|
267
267
|
// Skip unreachable constants (only used in event handler code paths)
|
|
268
268
|
if (!reachable.has(constant.name)) continue
|
|
@@ -489,9 +489,8 @@ export abstract class JsxAdapter extends BaseAdapter {
|
|
|
489
489
|
entries.push({ line: c.loc.start.line, text: `${exportKw}${keyword} ${c.name}${typeAnnotation}` })
|
|
490
490
|
continue
|
|
491
491
|
}
|
|
492
|
-
|
|
493
|
-
if (
|
|
494
|
-
if (c.isExported && /^createContext\b/.test(trimmed)) continue
|
|
492
|
+
if (c.systemConstructKind === 'weakMap') continue
|
|
493
|
+
if (c.isExported && c.systemConstructKind === 'createContext') continue
|
|
495
494
|
const value = preserveTypes ? (c.typedValue ?? c.value) : c.value
|
|
496
495
|
// Preserve an explicit module-scope `let` type annotation from source
|
|
497
496
|
// (#2589) — see the function-scope sibling above for rationale. `const`
|
|
@@ -565,8 +564,15 @@ export abstract class JsxAdapter extends BaseAdapter {
|
|
|
565
564
|
* `expr` (a presence peel, a prop-ref rewrite) fails the identity check
|
|
566
565
|
* and wins, so this can only ever add the annotation, never undo a
|
|
567
566
|
* downstream edit.
|
|
567
|
+
*
|
|
568
|
+
* The same identity rule brings back the author's own type syntax
|
|
569
|
+
* (`value={store as never}`, #2573): `typed.tsx` is written only while
|
|
570
|
+
* `expr` is still the text it was captured beside.
|
|
568
571
|
*/
|
|
569
572
|
protected expressionValueToJs(value: Extract<AttrValue, { kind: 'expression' }>): string {
|
|
573
|
+
if (this.jsxConfig.preserveTypes && value.typed && value.expr === value.typed.forExpr) {
|
|
574
|
+
return value.typed.tsx
|
|
575
|
+
}
|
|
570
576
|
if (
|
|
571
577
|
this.jsxConfig.preserveTypes &&
|
|
572
578
|
value.parts &&
|
|
@@ -8,7 +8,7 @@
|
|
|
8
8
|
*/
|
|
9
9
|
|
|
10
10
|
import type { ParsedExpr } from '../expression-parser.ts'
|
|
11
|
-
import type { TypeInfo } from '../types.ts'
|
|
11
|
+
import type { TypeInfo, JsSourceText } from '../types.ts'
|
|
12
12
|
|
|
13
13
|
/** True when `type` is the `string` primitive. */
|
|
14
14
|
export function isStringTypeInfo(type: TypeInfo | undefined): boolean {
|
|
@@ -16,7 +16,7 @@ export function isStringTypeInfo(type: TypeInfo | undefined): boolean {
|
|
|
16
16
|
}
|
|
17
17
|
|
|
18
18
|
/** True when `initialValue` is a bare string-literal expression. */
|
|
19
|
-
export function isBareStringLiteral(initialValue:
|
|
19
|
+
export function isBareStringLiteral(initialValue: JsSourceText | undefined): boolean {
|
|
20
20
|
if (!initialValue) return false
|
|
21
21
|
const v = initialValue.trim()
|
|
22
22
|
return (v.startsWith("'") && v.endsWith("'")) || (v.startsWith('"') && v.endsWith('"'))
|
|
@@ -305,6 +305,29 @@ export function isNumericArithmeticResult(
|
|
|
305
305
|
return expr.op === '-' || expr.op === '*' || expr.op === '/' || expr.op === '%'
|
|
306
306
|
}
|
|
307
307
|
|
|
308
|
+
/** A runtime helper of the template-helper catalogue (`spec/template-helpers.md`)
|
|
309
|
+
* that a binary arithmetic operator lowers to. */
|
|
310
|
+
export type ArithmeticHelper = 'mod'
|
|
311
|
+
|
|
312
|
+
/**
|
|
313
|
+
* The runtime helper a JS binary arithmetic operator lowers to on every
|
|
314
|
+
* template backend, or `null` when the backend spells the operator itself
|
|
315
|
+
* (#3474). The adapter only spells the call, in its own syntax
|
|
316
|
+
* (`bf.mod(a, b)`, `$bf->mod(a, b)`, `bf_mod a b`), over operands already
|
|
317
|
+
* coerced (`arithmeticOperandCoercion`) and grouped ({@link groupBinaryOperand}).
|
|
318
|
+
*
|
|
319
|
+
* `%` is JS's truncated remainder: the result takes the dividend's sign
|
|
320
|
+
* (`-7 % 2` is `-1`, `7 % -2` is `1`), keeps a fraction (`7.5 % 2` is `1.5`),
|
|
321
|
+
* and is `NaN` for a zero divisor or a `NaN` / infinite dividend. No
|
|
322
|
+
* engine's native operator computes it: Ruby, Python and Perl floor the
|
|
323
|
+
* quotient (`-7 % 2` is `1`), MiniJinja takes the Euclidean remainder, PHP,
|
|
324
|
+
* Perl and Kolon convert the operands to integers first (`NaN % 2` is `0`),
|
|
325
|
+
* and an integer zero divisor raises in Java, PHP and MiniJinja (#3574).
|
|
326
|
+
*/
|
|
327
|
+
export function arithmeticHelper(op: string): ArithmeticHelper | null {
|
|
328
|
+
return op === '%' ? 'mod' : null
|
|
329
|
+
}
|
|
330
|
+
|
|
308
331
|
/**
|
|
309
332
|
* Wrap an emitted binary/logical/ternary OPERAND in parentheses so the
|
|
310
333
|
* source grouping the `ParsedExpr` tree encodes survives infix
|
|
@@ -325,6 +348,17 @@ export function groupBinaryOperand(operand: ParsedExpr, emitted: string): string
|
|
|
325
348
|
: emitted
|
|
326
349
|
}
|
|
327
350
|
|
|
351
|
+
/**
|
|
352
|
+
* Wrap an emitted unary-minus OPERAND in parentheses where the target's
|
|
353
|
+
* prefix `-` would otherwise bind to only part of it (#3474): a compound
|
|
354
|
+
* operand for the reason {@link groupBinaryOperand} gives (`-(a + b)`
|
|
355
|
+
* re-emitted as `-a + b`), and a nested unary operand, since `--x` is a
|
|
356
|
+
* decrement in PHP and Perl. A leaf operand stays unwrapped.
|
|
357
|
+
*/
|
|
358
|
+
export function groupUnaryOperand(operand: ParsedExpr, emitted: string): string {
|
|
359
|
+
return operand.kind === 'unary' ? `(${emitted})` : groupBinaryOperand(operand, emitted)
|
|
360
|
+
}
|
|
361
|
+
|
|
328
362
|
/**
|
|
329
363
|
* Split an `object-literal`'s (order-preserving) `properties` into rendered
|
|
330
364
|
* SEGMENTS an adapter's `objectLiteral()` folds with its own merge idiom
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `style={{ … }}` object-literal lowering plan, shared by the template
|
|
3
|
+
* adapters (#3541).
|
|
4
|
+
*
|
|
5
|
+
* Every adapter used to re-parse the attribute text through
|
|
6
|
+
* `parseStyleObjectEntries` (a fresh `ts.createSourceFile`) and then each
|
|
7
|
+
* dynamic value through `parseExpression`. The IR already carries the
|
|
8
|
+
* object-literal tree on `ExpressionAttr.parsed`, so the plan reads that:
|
|
9
|
+
* the CSS keys are decided here once, and an adapter spells only its
|
|
10
|
+
* `style_object` runtime call. Reading the tree also accepts the shorthand
|
|
11
|
+
* `{ color }`, which the text parser refused.
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
import type { ExpressionAttr } from '../types.ts'
|
|
15
|
+
import {
|
|
16
|
+
cssKebabCase,
|
|
17
|
+
isSupported,
|
|
18
|
+
stringifyParsedExpr,
|
|
19
|
+
type ParsedExpr,
|
|
20
|
+
} from '../expression-parser.ts'
|
|
21
|
+
|
|
22
|
+
/** One CSS declaration of a style object: a static string, or a value to lower. */
|
|
23
|
+
export type StyleObjectPlanEntry =
|
|
24
|
+
| { cssKey: string; kind: 'literal'; value: string }
|
|
25
|
+
| { cssKey: string; kind: 'expr'; expr: string; parsed: ParsedExpr }
|
|
26
|
+
|
|
27
|
+
/** The static string a value spells, when it is a string or a substitution-free template literal. */
|
|
28
|
+
function staticString(value: ParsedExpr): string | null {
|
|
29
|
+
if (value.kind === 'literal' && value.literalType === 'string' && typeof value.value === 'string') return value.value
|
|
30
|
+
if (value.kind === 'template-literal' && value.parts.every(p => p.type === 'string')) {
|
|
31
|
+
return value.parts.map(p => (p.type === 'string' ? p.value : '')).join('')
|
|
32
|
+
}
|
|
33
|
+
return null
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* The CSS declarations of a `style` attribute's object literal, or `null`
|
|
38
|
+
* when the value is not an object literal every entry of which an adapter
|
|
39
|
+
* can lower: a spread, a numeric key, or a value outside the template
|
|
40
|
+
* subset keeps the attribute on the adapter's refusal path.
|
|
41
|
+
*/
|
|
42
|
+
export function planStyleObject(value: Pick<ExpressionAttr, 'expr' | 'parsed'>): StyleObjectPlanEntry[] | null {
|
|
43
|
+
// Phase 1 attaches `parsed` to every expression attribute; without it
|
|
44
|
+
// there is no tree to plan from, and the attribute stays on the refusal path.
|
|
45
|
+
const parsed = value.parsed
|
|
46
|
+
if (parsed?.kind !== 'object-literal' || parsed.properties.length === 0) return null
|
|
47
|
+
const entries: StyleObjectPlanEntry[] = []
|
|
48
|
+
for (const prop of parsed.properties) {
|
|
49
|
+
if (prop.kind !== 'prop' || prop.keyKind === 'numeric') return null
|
|
50
|
+
const cssKey = cssKebabCase(prop.key)
|
|
51
|
+
const literal = staticString(prop.value)
|
|
52
|
+
if (literal !== null) {
|
|
53
|
+
entries.push({ cssKey, kind: 'literal', value: literal })
|
|
54
|
+
} else if (isSupported(prop.value).supported) {
|
|
55
|
+
entries.push({ cssKey, kind: 'expr', expr: stringifyParsedExpr(prop.value), parsed: prop.value })
|
|
56
|
+
} else {
|
|
57
|
+
return null
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
return entries
|
|
61
|
+
}
|