prunelle 0.0.0-stage → 0.2.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/llms.txt +148 -0
- package/package.json +53 -4
- package/src/ambient.d.ts +6 -0
- package/src/attrs.ts +35 -0
- package/src/build/bundle.ts +613 -0
- package/src/build/carry.ts +57 -0
- package/src/build/client-entries.ts +107 -0
- package/src/build/client-plugin.ts +169 -0
- package/src/build/client-prune.ts +118 -0
- package/src/build/compile.ts +128 -0
- package/src/build/files.ts +156 -0
- package/src/build/first-load.ts +164 -0
- package/src/build/graph.ts +247 -0
- package/src/build/index.ts +614 -0
- package/src/build/is-inside.ts +12 -0
- package/src/build/islands.ts +641 -0
- package/src/build/manifest.ts +281 -0
- package/src/build/output.ts +84 -0
- package/src/build/plugin-host.ts +220 -0
- package/src/build/precompress.ts +139 -0
- package/src/build/prerender.ts +150 -0
- package/src/build/render-rules.ts +205 -0
- package/src/build/report.ts +247 -0
- package/src/build/resolve.ts +50 -0
- package/src/build/routes.ts +338 -0
- package/src/build/server-entry.ts +38 -0
- package/src/build/static.ts +163 -0
- package/src/build/status-pages.ts +127 -0
- package/src/cli/check-forms.ts +654 -0
- package/src/cli/check-links.ts +619 -0
- package/src/cli/check.ts +367 -0
- package/src/cli/commands.ts +265 -0
- package/src/cli/index.ts +70 -0
- package/src/cli/inspect.ts +335 -0
- package/src/cli/new.ts +41 -0
- package/src/cli/upgrade-names.ts +209 -0
- package/src/cli/upgrade.ts +354 -0
- package/src/compiler/analyze.ts +618 -0
- package/src/compiler/ast.ts +82 -0
- package/src/compiler/browser-globals.ts +127 -0
- package/src/compiler/compliance-rgaa.ts +502 -0
- package/src/compiler/compliance.ts +497 -0
- package/src/compiler/data-keys.ts +295 -0
- package/src/compiler/dom-attrs.ts +379 -0
- package/src/compiler/dom.ts +566 -0
- package/src/compiler/emit.ts +131 -0
- package/src/compiler/expr-context.ts +408 -0
- package/src/compiler/index.ts +8 -0
- package/src/compiler/interactivity.ts +366 -0
- package/src/compiler/mistakes.ts +499 -0
- package/src/compiler/node-env.ts +50 -0
- package/src/compiler/patch.ts +97 -0
- package/src/compiler/props.ts +460 -0
- package/src/compiler/scope.ts +568 -0
- package/src/compiler/server-only.ts +326 -0
- package/src/compiler/shared.ts +369 -0
- package/src/compiler/signal-reads.ts +242 -0
- package/src/compiler/slots.ts +227 -0
- package/src/compiler/ssr-helpers.ts +555 -0
- package/src/compiler/ssr-island.ts +90 -0
- package/src/compiler/ssr-prunelle-tags.ts +56 -0
- package/src/compiler/ssr.ts +491 -0
- package/src/context.ts +165 -0
- package/src/dev/child.ts +30 -0
- package/src/dev/index.ts +208 -0
- package/src/dev/modules.ts +68 -0
- package/src/dev/protocol.ts +21 -0
- package/src/dev/queue.ts +79 -0
- package/src/dev/reload.ts +157 -0
- package/src/dev/supervisor.ts +187 -0
- package/src/dev/watchers.ts +215 -0
- package/src/dom/action.ts +164 -0
- package/src/dom/attrs.ts +72 -0
- package/src/dom/claim.ts +120 -0
- package/src/dom/extras.ts +562 -0
- package/src/dom/index.ts +289 -0
- package/src/dom/island.ts +284 -0
- package/src/dom/loop.ts +450 -0
- package/src/dom/revive.ts +36 -0
- package/src/index.ts +22 -0
- package/src/jsx-runtime.ts +566 -0
- package/src/plugins/config.ts +86 -0
- package/src/plugins/context.ts +66 -0
- package/src/plugins/index.ts +14 -0
- package/src/plugins/load.ts +70 -0
- package/src/plugins/order.ts +111 -0
- package/src/plugins/public.ts +12 -0
- package/src/plugins/requires.ts +47 -0
- package/src/plugins/services.ts +35 -0
- package/src/plugins/types.ts +242 -0
- package/src/reactivity/graph.ts +249 -0
- package/src/reactivity/index.ts +533 -0
- package/src/reactivity/selector.ts +51 -0
- package/src/reactivity/store.ts +208 -0
- package/src/server/action-protect.ts +186 -0
- package/src/server/action-registry.ts +78 -0
- package/src/server/action.ts +498 -0
- package/src/server/app-entry.ts +23 -0
- package/src/server/assets.ts +136 -0
- package/src/server/compress.ts +560 -0
- package/src/server/consent.ts +253 -0
- package/src/server/cookies.ts +139 -0
- package/src/server/document.ts +271 -0
- package/src/server/env.ts +33 -0
- package/src/server/errors.ts +382 -0
- package/src/server/form-schema.ts +343 -0
- package/src/server/health.ts +244 -0
- package/src/server/http.ts +508 -0
- package/src/server/index.ts +687 -0
- package/src/server/lifecycle.ts +75 -0
- package/src/server/loader.ts +118 -0
- package/src/server/page.ts +107 -0
- package/src/server/param-matchers.ts +112 -0
- package/src/server/plugin-host.ts +256 -0
- package/src/server/prerendered.ts +71 -0
- package/src/server/public.ts +19 -0
- package/src/server/rate-limit.ts +133 -0
- package/src/server/region.ts +96 -0
- package/src/server/request-body.ts +27 -0
- package/src/server/request-path.ts +73 -0
- package/src/server/request-scope.ts +22 -0
- package/src/server/route-table.ts +82 -0
- package/src/server/router.ts +261 -0
- package/src/server/routing.ts +377 -0
- package/src/server/security.ts +472 -0
- package/src/server/serve-handlers.ts +83 -0
- package/src/server/served-file.ts +128 -0
- package/src/server/standard-schema.ts +87 -0
- package/src/server/start.ts +99 -0
- package/src/server/static-dir.ts +167 -0
- package/src/server/stream.ts +192 -0
- package/src/ssr/context.ts +350 -0
- package/src/ssr/index.ts +596 -0
- package/src/ssr/navigation.ts +96 -0
- package/src/ssr/region.ts +171 -0
- package/src/style.ts +33 -0
- package/src/test/browser.ts +192 -0
- package/src/test/index.ts +191 -0
- package/src/unrenderable.ts +35 -0
- package/templates/_shared/AGENTS.md +10 -0
- package/templates/_shared/CLAUDE.md +1 -0
- package/templates/_shared/gitignore +3 -0
- package/templates/_shared/public/robots.txt +2 -0
- package/templates/_shared/src/routes/legal.tsx +13 -0
- package/templates/_shared/tsconfig.json +20 -0
- package/templates/app/.env.example +4 -0
- package/templates/app/package.json +21 -0
- package/templates/app/prunelle.config.ts +11 -0
- package/templates/app/src/auth.ts +14 -0
- package/templates/app/src/routes/_layout.tsx +32 -0
- package/templates/app/src/routes/dashboard/_middleware.ts +3 -0
- package/templates/app/src/routes/dashboard/index.tsx +13 -0
- package/templates/app/src/routes/index.tsx +15 -0
- package/templates/app/src/routes/login.tsx +34 -0
- package/templates/app/src/styles/app.css +7 -0
- package/templates/app/tests/app.test.ts +32 -0
- package/templates/blog/content/posts/welcome.md +9 -0
- package/templates/blog/package.json +22 -0
- package/templates/blog/prunelle.config.ts +23 -0
- package/templates/blog/src/posts.ts +26 -0
- package/templates/blog/src/routes/_layout.tsx +20 -0
- package/templates/blog/src/routes/blog/[slug].tsx +31 -0
- package/templates/blog/src/routes/index.tsx +33 -0
- package/templates/blog/src/styles/app.css +10 -0
- package/templates/fullstack/package.json +19 -0
- package/templates/fullstack/prunelle.config.ts +6 -0
- package/templates/fullstack/src/actions/greet.ts +19 -0
- package/templates/fullstack/src/components/Counter.tsx +10 -0
- package/templates/fullstack/src/components/GreetForm.tsx +27 -0
- package/templates/fullstack/src/routes/_layout.tsx +14 -0
- package/templates/fullstack/src/routes/api/health.ts +5 -0
- package/templates/fullstack/src/routes/index.tsx +13 -0
- package/templates/fullstack/src/styles/app.css +29 -0
- package/templates/fullstack/tests/app.test.ts +37 -0
- package/templates/minimal/package.json +19 -0
- package/templates/minimal/prunelle.config.ts +6 -0
- package/templates/minimal/src/components/Counter.tsx +10 -0
- package/templates/minimal/src/routes/_layout.tsx +14 -0
- package/templates/minimal/src/routes/index.tsx +11 -0
- package/templates/minimal/src/styles/app.css +22 -0
- package/README.md +0 -3
|
@@ -0,0 +1,242 @@
|
|
|
1
|
+
// Refuses a local signal() or computed() read without `.value`, in JSX or in plain code, following each name through its scopes.
|
|
2
|
+
|
|
3
|
+
import type { Node, Program } from 'oxc-parser'
|
|
4
|
+
import { visitorKeys } from 'oxc-parser'
|
|
5
|
+
import { walk } from './ast'
|
|
6
|
+
import { guarded, mistake } from './mistakes'
|
|
7
|
+
import { calleeName, EVENT_ATTR, isComponentTag, jsxTagName, unwrapTsExpr } from './shared'
|
|
8
|
+
|
|
9
|
+
// Each name a scope binds, true when it holds a signal() or computed().
|
|
10
|
+
type Scope = Map<string, boolean>
|
|
11
|
+
|
|
12
|
+
function bindPattern(scope: Scope, pattern: Node | null | undefined, isSignal = false): void {
|
|
13
|
+
if (pattern === null || pattern === undefined)
|
|
14
|
+
return
|
|
15
|
+
if (pattern.type === 'Identifier') {
|
|
16
|
+
scope.set(pattern.name, isSignal)
|
|
17
|
+
return
|
|
18
|
+
}
|
|
19
|
+
walk(pattern, (node) => {
|
|
20
|
+
if (node.type === 'Identifier')
|
|
21
|
+
scope.set(node.name, false)
|
|
22
|
+
// A default value or a computed key reads names, it binds none.
|
|
23
|
+
if (node.type === 'AssignmentPattern')
|
|
24
|
+
bindPattern(scope, node.left)
|
|
25
|
+
else if (node.type === 'Property')
|
|
26
|
+
bindPattern(scope, node.value)
|
|
27
|
+
return node.type !== 'AssignmentPattern' && node.type !== 'Property'
|
|
28
|
+
})
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
function bindStatements(scope: Scope, statements: readonly Node[], signalCallees: ReadonlySet<string>, importedSignals?: ReadonlySet<string>): void {
|
|
32
|
+
for (const stmt of statements) {
|
|
33
|
+
const decl = (stmt.type === 'ExportNamedDeclaration' || stmt.type === 'ExportDefaultDeclaration') ? stmt.declaration : stmt
|
|
34
|
+
if (decl?.type === 'VariableDeclaration') {
|
|
35
|
+
for (const d of decl.declarations) {
|
|
36
|
+
const init = d.init === null ? undefined : unwrapTsExpr(d.init)
|
|
37
|
+
bindPattern(scope, d.id, init?.type === 'CallExpression' && signalCallees.has(calleeName(init.callee)))
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
else if ((decl?.type === 'FunctionDeclaration' || decl?.type === 'ClassDeclaration') && decl.id !== null) {
|
|
41
|
+
scope.set(decl.id.name, false)
|
|
42
|
+
}
|
|
43
|
+
else if (decl?.type === 'ImportDeclaration') {
|
|
44
|
+
for (const spec of decl.specifiers)
|
|
45
|
+
scope.set(spec.local.name, importedSignals?.has(spec.local.name) === true)
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
// The names a node opens a scope for, or undefined when it opens none.
|
|
51
|
+
function scopeOf(node: Node, signalCallees: ReadonlySet<string>, importedSignals?: ReadonlySet<string>): Scope | undefined {
|
|
52
|
+
const scope: Scope = new Map()
|
|
53
|
+
switch (node.type) {
|
|
54
|
+
case 'Program':
|
|
55
|
+
bindStatements(scope, node.body, signalCallees, importedSignals)
|
|
56
|
+
return scope
|
|
57
|
+
case 'BlockStatement':
|
|
58
|
+
case 'StaticBlock':
|
|
59
|
+
bindStatements(scope, node.body, signalCallees)
|
|
60
|
+
return scope
|
|
61
|
+
case 'FunctionDeclaration':
|
|
62
|
+
case 'FunctionExpression':
|
|
63
|
+
case 'ArrowFunctionExpression':
|
|
64
|
+
if (node.type === 'FunctionExpression' && node.id !== null)
|
|
65
|
+
scope.set(node.id.name, false)
|
|
66
|
+
for (const param of node.params)
|
|
67
|
+
bindPattern(scope, param)
|
|
68
|
+
return scope
|
|
69
|
+
case 'ForStatement':
|
|
70
|
+
case 'ForInStatement':
|
|
71
|
+
case 'ForOfStatement': {
|
|
72
|
+
const head = node.type === 'ForStatement' ? node.init : node.left
|
|
73
|
+
if (head?.type === 'VariableDeclaration')
|
|
74
|
+
bindStatements(scope, [head], signalCallees)
|
|
75
|
+
return scope
|
|
76
|
+
}
|
|
77
|
+
case 'CatchClause':
|
|
78
|
+
bindPattern(scope, node.param)
|
|
79
|
+
return scope
|
|
80
|
+
case 'SwitchStatement':
|
|
81
|
+
for (const c of node.cases)
|
|
82
|
+
bindStatements(scope, c.consequent, signalCallees)
|
|
83
|
+
return scope
|
|
84
|
+
default:
|
|
85
|
+
return undefined
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
interface SignalLookup {
|
|
90
|
+
isSignal: (name: string) => boolean
|
|
91
|
+
isSignalMember: (obj: string, prop: string) => boolean
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
// Visits every node of the program with the signals in scope there.
|
|
95
|
+
function walkWithSignals(
|
|
96
|
+
program: Program,
|
|
97
|
+
signalCallees: ReadonlySet<string>,
|
|
98
|
+
importedSignals: ReadonlySet<string>,
|
|
99
|
+
importedSignalMembers: ReadonlyMap<string, ReadonlySet<string>>,
|
|
100
|
+
visit: (node: Node, lookup: SignalLookup) => void,
|
|
101
|
+
): void {
|
|
102
|
+
const scopes: Scope[] = []
|
|
103
|
+
const lookup: SignalLookup = {
|
|
104
|
+
isSignal(name: string): boolean {
|
|
105
|
+
for (let i = scopes.length - 1; i >= 0; i--) {
|
|
106
|
+
const found = scopes[i]!.get(name)
|
|
107
|
+
if (found !== undefined)
|
|
108
|
+
return found
|
|
109
|
+
}
|
|
110
|
+
return false
|
|
111
|
+
},
|
|
112
|
+
isSignalMember(obj: string, prop: string): boolean {
|
|
113
|
+
const members = importedSignalMembers.get(obj)
|
|
114
|
+
if (members === undefined || !members.has(prop))
|
|
115
|
+
return false
|
|
116
|
+
// Any inner scope binding `obj` shadows the namespace import.
|
|
117
|
+
for (let i = scopes.length - 1; i >= 1; i--) {
|
|
118
|
+
if (scopes[i]!.has(obj))
|
|
119
|
+
return false
|
|
120
|
+
}
|
|
121
|
+
return true
|
|
122
|
+
},
|
|
123
|
+
}
|
|
124
|
+
const visitor = (node: Node): boolean | void => {
|
|
125
|
+
visit(node, lookup)
|
|
126
|
+
const scope = scopeOf(node, signalCallees, importedSignals)
|
|
127
|
+
if (scope === undefined)
|
|
128
|
+
return
|
|
129
|
+
scopes.push(scope)
|
|
130
|
+
for (const key of visitorKeys[node.type] ?? [])
|
|
131
|
+
walk((node as unknown as Record<string, unknown>)[key], visitor)
|
|
132
|
+
scopes.pop()
|
|
133
|
+
return false
|
|
134
|
+
}
|
|
135
|
+
walk(program, visitor)
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
// The signal an expression reads without `.value` where it would print or test it.
|
|
139
|
+
function bareSignal(expr: Node, lookup: SignalLookup): { name: string, start: number } | undefined {
|
|
140
|
+
const node = unwrapTsExpr(expr)
|
|
141
|
+
if (node.type === 'Identifier')
|
|
142
|
+
return lookup.isSignal(node.name) ? { name: node.name, start: node.start } : undefined
|
|
143
|
+
if (node.type === 'MemberExpression' && !node.computed && node.object.type === 'Identifier' && node.property.type === 'Identifier') {
|
|
144
|
+
if (lookup.isSignalMember(node.object.name, node.property.name))
|
|
145
|
+
return { name: `${node.object.name}.${node.property.name}`, start: node.start }
|
|
146
|
+
return undefined
|
|
147
|
+
}
|
|
148
|
+
if (node.type === 'LogicalExpression' || node.type === 'BinaryExpression')
|
|
149
|
+
return bareSignal(node.left, lookup) ?? bareSignal(node.right, lookup)
|
|
150
|
+
if (node.type === 'ConditionalExpression')
|
|
151
|
+
return bareSignal(node.test, lookup) ?? bareSignal(node.consequent, lookup) ?? bareSignal(node.alternate, lookup)
|
|
152
|
+
if (node.type === 'UnaryExpression')
|
|
153
|
+
return bareSignal(node.argument, lookup)
|
|
154
|
+
if (node.type === 'TemplateLiteral') {
|
|
155
|
+
for (const part of node.expressions) {
|
|
156
|
+
const found = bareSignal(part, lookup)
|
|
157
|
+
if (found !== undefined)
|
|
158
|
+
return found
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
return undefined
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
// The expressions JSX prints or tests: children on elements, fragments and components, and attributes on elements other than handlers and refs.
|
|
165
|
+
function jsxReads(node: Node): Node[] {
|
|
166
|
+
if (node.type !== 'JSXElement' && node.type !== 'JSXFragment')
|
|
167
|
+
return []
|
|
168
|
+
const reads: Node[] = []
|
|
169
|
+
for (const child of node.children) {
|
|
170
|
+
if (child.type === 'JSXExpressionContainer' && child.expression.type !== 'JSXEmptyExpression')
|
|
171
|
+
reads.push(child.expression)
|
|
172
|
+
}
|
|
173
|
+
if (node.type !== 'JSXElement' || isComponentTag(jsxTagName(node.openingElement.name)))
|
|
174
|
+
return reads
|
|
175
|
+
for (const attr of node.openingElement.attributes) {
|
|
176
|
+
if (attr.type !== 'JSXAttribute' || attr.name.type !== 'JSXIdentifier')
|
|
177
|
+
continue
|
|
178
|
+
const name = attr.name.name
|
|
179
|
+
if (name !== '' && name !== 'ref' && name !== 'key' && !EVENT_ATTR.test(name) && attr.value?.type === 'JSXExpressionContainer' && attr.value.expression.type !== 'JSXEmptyExpression')
|
|
180
|
+
reads.push(attr.value.expression)
|
|
181
|
+
}
|
|
182
|
+
return reads
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
// The expressions plain code tests: an if, a loop or a ternary condition, and the operand of `!`.
|
|
186
|
+
function codeTests(node: Node): Node[] {
|
|
187
|
+
switch (node.type) {
|
|
188
|
+
case 'IfStatement':
|
|
189
|
+
case 'WhileStatement':
|
|
190
|
+
case 'DoWhileStatement':
|
|
191
|
+
case 'ConditionalExpression':
|
|
192
|
+
return [node.test]
|
|
193
|
+
case 'ForStatement':
|
|
194
|
+
return node.test === null ? [] : [node.test]
|
|
195
|
+
case 'UnaryExpression':
|
|
196
|
+
return node.operator === '!' ? [node.argument] : []
|
|
197
|
+
default:
|
|
198
|
+
return []
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
// `const { value } = count` copies a signal's value once, so the copy never updates.
|
|
203
|
+
function destructuredSignal(node: Node, lookup: SignalLookup): string | undefined {
|
|
204
|
+
if (node.type !== 'VariableDeclarator' || node.id.type !== 'ObjectPattern' || node.init === null)
|
|
205
|
+
return undefined
|
|
206
|
+
const init = unwrapTsExpr(node.init)
|
|
207
|
+
const name = init.type === 'Identifier' && lookup.isSignal(init.name)
|
|
208
|
+
? init.name
|
|
209
|
+
: init.type === 'MemberExpression' && !init.computed && init.object.type === 'Identifier' && init.property.type === 'Identifier' && lookup.isSignalMember(init.object.name, init.property.name)
|
|
210
|
+
? `${init.object.name}.${init.property.name}`
|
|
211
|
+
: undefined
|
|
212
|
+
if (name === undefined)
|
|
213
|
+
return undefined
|
|
214
|
+
const takesValue = node.id.properties.some(p => p.type === 'Property' && !p.computed && p.key.type === 'Identifier' && p.key.name === 'value')
|
|
215
|
+
return takesValue ? name : undefined
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
/** Throws when JSX or plain code prints or tests a local or imported signal/computed without `.value`, or copies its value out by destructuring. */
|
|
219
|
+
export function checkBareSignals(
|
|
220
|
+
program: Program,
|
|
221
|
+
signalCallees: ReadonlySet<string>,
|
|
222
|
+
filename: string,
|
|
223
|
+
importedSignals: ReadonlySet<string> = new Set(),
|
|
224
|
+
importedSignalMembers: ReadonlyMap<string, ReadonlySet<string>> = new Map(),
|
|
225
|
+
): void {
|
|
226
|
+
if (signalCallees.size === 0 && importedSignals.size === 0 && importedSignalMembers.size === 0)
|
|
227
|
+
return
|
|
228
|
+
// One read sits under several checks, `{!open}` under both the JSX child and the `!`, and is reported once.
|
|
229
|
+
const reported = new Set<number>()
|
|
230
|
+
walkWithSignals(program, signalCallees, importedSignals, importedSignalMembers, (node, lookup) => {
|
|
231
|
+
for (const expr of [...jsxReads(node), ...codeTests(node)]) {
|
|
232
|
+
const found = bareSignal(expr, lookup)
|
|
233
|
+
if (found === undefined || reported.has(found.start))
|
|
234
|
+
continue
|
|
235
|
+
reported.add(found.start)
|
|
236
|
+
guarded(found.start, () => { throw mistake(`${found.name} without .value`, filename, `write ${found.name}.value`) })
|
|
237
|
+
}
|
|
238
|
+
const copied = destructuredSignal(node, lookup)
|
|
239
|
+
if (copied !== undefined)
|
|
240
|
+
guarded(node.start, () => { throw mistake(`{ value } = ${copied}`, filename, `read ${copied}.value where it is used, a copied value never updates`) })
|
|
241
|
+
})
|
|
242
|
+
}
|
|
@@ -0,0 +1,227 @@
|
|
|
1
|
+
// Shared JSX child classification and hydration marker planner used identically by SSR & DOM compilers.
|
|
2
|
+
|
|
3
|
+
import type {
|
|
4
|
+
ArrowFunctionExpression,
|
|
5
|
+
BlockStatement,
|
|
6
|
+
CallExpression,
|
|
7
|
+
ConditionalExpression,
|
|
8
|
+
Expression,
|
|
9
|
+
Function as OxcFunction,
|
|
10
|
+
JSXChild,
|
|
11
|
+
JSXElement,
|
|
12
|
+
JSXExpressionContainer,
|
|
13
|
+
JSXFragment,
|
|
14
|
+
LogicalExpression,
|
|
15
|
+
Node,
|
|
16
|
+
ParamPattern,
|
|
17
|
+
} from 'oxc-parser'
|
|
18
|
+
import type { ExprContext } from './expr-context'
|
|
19
|
+
import { isNodeCapableExpr } from './expr-context'
|
|
20
|
+
import {
|
|
21
|
+
blockReturnsJsx,
|
|
22
|
+
cleanJsxText,
|
|
23
|
+
filterMeaningfulChildren,
|
|
24
|
+
isComponentTag,
|
|
25
|
+
isJsxNode,
|
|
26
|
+
jsxTagName,
|
|
27
|
+
staticLiteral,
|
|
28
|
+
unwrapParens,
|
|
29
|
+
} from './shared'
|
|
30
|
+
|
|
31
|
+
export type { ExprContext } from './expr-context'
|
|
32
|
+
export { createExprContext, isDefinitelyPrimitive, isNodeCapableExpr, mayReturnNode } from './expr-context'
|
|
33
|
+
|
|
34
|
+
export interface MapCallMatch {
|
|
35
|
+
call: CallExpression
|
|
36
|
+
target: Expression
|
|
37
|
+
cb: ArrowFunctionExpression | OxcFunction
|
|
38
|
+
body: JSXElement | JSXFragment | BlockStatement
|
|
39
|
+
isJsxBody: boolean
|
|
40
|
+
params: readonly ParamPattern[]
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/** Recognizes `arr.map((item, idx?) => <jsx> | { ... return <jsx> })` identically in SSR and DOM compilers. */
|
|
44
|
+
export function matchJsxMapCall(expr: Node, jsxNodes?: WeakSet<Node>): MapCallMatch | undefined {
|
|
45
|
+
if (
|
|
46
|
+
expr.type !== 'CallExpression'
|
|
47
|
+
|| expr.callee.type !== 'MemberExpression'
|
|
48
|
+
|| expr.callee.computed
|
|
49
|
+
|| expr.callee.property.type !== 'Identifier'
|
|
50
|
+
|| expr.callee.property.name !== 'map'
|
|
51
|
+
|| expr.arguments.length !== 1
|
|
52
|
+
) {
|
|
53
|
+
return undefined
|
|
54
|
+
}
|
|
55
|
+
const arg = expr.arguments[0]
|
|
56
|
+
if (arg === undefined || (arg.type !== 'ArrowFunctionExpression' && arg.type !== 'FunctionExpression'))
|
|
57
|
+
return undefined
|
|
58
|
+
if (arg.async || arg.body === null)
|
|
59
|
+
return undefined
|
|
60
|
+
// 3rd `(item, idx, arr)` param or rest param cannot be mapped cleanly to `(item, idx)` loop signals.
|
|
61
|
+
if (arg.params.length > 2 || arg.params.some(p => p.type === 'RestElement'))
|
|
62
|
+
return undefined
|
|
63
|
+
const body = unwrapParens(arg.body)
|
|
64
|
+
if (isJsxNode(body))
|
|
65
|
+
return { call: expr, target: expr.callee.object, cb: arg, body, isJsxBody: true, params: arg.params }
|
|
66
|
+
if (body.type === 'BlockStatement' && blockReturnsJsx(body, jsxNodes))
|
|
67
|
+
return { call: expr, target: expr.callee.object, cb: arg, body, isJsxBody: false, params: arg.params }
|
|
68
|
+
return undefined
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
export function isStaticBranch(node: Node): boolean {
|
|
72
|
+
const n = unwrapParens(node)
|
|
73
|
+
if (isJsxNode(n))
|
|
74
|
+
return true
|
|
75
|
+
if (n.type === 'Identifier' && n.name === 'undefined')
|
|
76
|
+
return true
|
|
77
|
+
return staticLiteral(n).ok
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
export type ChildSlotKind =
|
|
81
|
+
| { kind: 'static-text', text: string, fromLiteral: boolean }
|
|
82
|
+
| { kind: 'element', node: JSXElement }
|
|
83
|
+
| { kind: 'component', node: JSXElement }
|
|
84
|
+
| { kind: 'fragment', node: JSXFragment }
|
|
85
|
+
| { kind: 'loop', match: MapCallMatch }
|
|
86
|
+
| { kind: 'and-show', expr: LogicalExpression, right: JSXElement | JSXFragment }
|
|
87
|
+
| { kind: 'ternary-show', expr: ConditionalExpression, cons: Node, alt: Node }
|
|
88
|
+
| { kind: 'expr', container: JSXExpressionContainer, expr: Expression }
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* Single shared child classifier for SSR & DOM codegen.
|
|
92
|
+
* Both emitters derive exact hydration marker placement (`<!--k[-->...<!--k]-->` vs `<!---->`) from this classification.
|
|
93
|
+
*/
|
|
94
|
+
export function classifyJsxChild(child: JSXChild, jsxNodes?: WeakSet<Node>): ChildSlotKind | undefined {
|
|
95
|
+
if (child.type === 'JSXText') {
|
|
96
|
+
const text = cleanJsxText(child.value)
|
|
97
|
+
return text === '' ? undefined : { kind: 'static-text', text, fromLiteral: false }
|
|
98
|
+
}
|
|
99
|
+
if (child.type === 'JSXFragment')
|
|
100
|
+
return { kind: 'fragment', node: child }
|
|
101
|
+
if (child.type === 'JSXElement') {
|
|
102
|
+
const tag = jsxTagName(child.openingElement.name)
|
|
103
|
+
return isComponentTag(tag)
|
|
104
|
+
? { kind: 'component', node: child }
|
|
105
|
+
: { kind: 'element', node: child }
|
|
106
|
+
}
|
|
107
|
+
if (child.type === 'JSXExpressionContainer') {
|
|
108
|
+
if (child.expression.type === 'JSXEmptyExpression')
|
|
109
|
+
return undefined
|
|
110
|
+
const expr = child.expression
|
|
111
|
+
if (expr.type === 'Literal' && typeof expr.value === 'string') {
|
|
112
|
+
return expr.value === ''
|
|
113
|
+
? undefined
|
|
114
|
+
: { kind: 'static-text', text: expr.value, fromLiteral: true }
|
|
115
|
+
}
|
|
116
|
+
const mapMatch = matchJsxMapCall(expr, jsxNodes)
|
|
117
|
+
if (mapMatch !== undefined)
|
|
118
|
+
return { kind: 'loop', match: mapMatch }
|
|
119
|
+
if (expr.type === 'LogicalExpression' && expr.operator === '&&') {
|
|
120
|
+
const right = unwrapParens(expr.right)
|
|
121
|
+
if (isJsxNode(right))
|
|
122
|
+
return { kind: 'and-show', expr, right }
|
|
123
|
+
}
|
|
124
|
+
if (expr.type === 'ConditionalExpression') {
|
|
125
|
+
const cons = unwrapParens(expr.consequent)
|
|
126
|
+
const alt = unwrapParens(expr.alternate)
|
|
127
|
+
// Use `show()` only when at least one branch is JSX and the other branch is JSX or a static literal/nullish;
|
|
128
|
+
// mixed dynamic text (`cond ? <span/> : count.value`) goes through `expr` (`insert`) so both branches stay reactive.
|
|
129
|
+
if ((isJsxNode(cons) && isStaticBranch(alt)) || (isJsxNode(alt) && isStaticBranch(cons)))
|
|
130
|
+
return { kind: 'ternary-show', expr, cons, alt }
|
|
131
|
+
}
|
|
132
|
+
return { kind: 'expr', container: child, expr }
|
|
133
|
+
}
|
|
134
|
+
return undefined
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
export type HydrationMarker = 'none' | 'text-open' | 'text' | 'range-open' | 'range'
|
|
138
|
+
|
|
139
|
+
export interface PlannedChildSlot {
|
|
140
|
+
child: JSXChild
|
|
141
|
+
slot: ChildSlotKind
|
|
142
|
+
marker: HydrationMarker
|
|
143
|
+
open: string
|
|
144
|
+
close: string
|
|
145
|
+
nodeCapable: boolean
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
export interface PlanChildSlotsOptions {
|
|
149
|
+
jsxNodes?: WeakSet<Node> | undefined
|
|
150
|
+
exprContext?: ExprContext | undefined
|
|
151
|
+
emitMarkers?: boolean | undefined
|
|
152
|
+
inFragment?: boolean | undefined
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
const MARKER_DELIMITERS: Record<HydrationMarker, readonly [open: string, close: string]> = {
|
|
156
|
+
'none': ['', ''],
|
|
157
|
+
'text-open': ['<!---->', ''],
|
|
158
|
+
'text': ['<!---->', '<!---->'],
|
|
159
|
+
'range-open': ['<!--k[-->', ''],
|
|
160
|
+
'range': ['<!--k[-->', '<!--k]-->'],
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
/**
|
|
164
|
+
* Single hydration-marker planner shared by SSR and DOM compilers.
|
|
165
|
+
* Computes each child slot's marker shape once so SSR markup and DOM hydration walks stay in lockstep.
|
|
166
|
+
*/
|
|
167
|
+
export function planChildSlots(children: readonly JSXChild[], options: PlanChildSlotsOptions = {}): PlannedChildSlot[] {
|
|
168
|
+
const { jsxNodes, exprContext, emitMarkers = true, inFragment = false } = options
|
|
169
|
+
const rawSlots: { child: JSXChild, slot: ChildSlotKind }[] = []
|
|
170
|
+
for (const child of filterMeaningfulChildren(children)) {
|
|
171
|
+
const slot = classifyJsxChild(child, jsxNodes)
|
|
172
|
+
if (slot !== undefined)
|
|
173
|
+
rawSlots.push({ child, slot })
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
const hasSiblings = rawSlots.length > 1
|
|
177
|
+
// A fragment may sit inside another range, so its dynamic slots are bracketed even alone and always closed, for the markers to balance (#3596).
|
|
178
|
+
const bracketed = emitMarkers && (hasSiblings || inFragment)
|
|
179
|
+
const planned: PlannedChildSlot[] = []
|
|
180
|
+
let hadVariableRange = false
|
|
181
|
+
|
|
182
|
+
for (let i = 0; i < rawSlots.length; i++) {
|
|
183
|
+
const { child, slot } = rawSlots[i]!
|
|
184
|
+
const isLast = i === rawSlots.length - 1
|
|
185
|
+
let marker: HydrationMarker = 'none'
|
|
186
|
+
let nodeCapable = false
|
|
187
|
+
|
|
188
|
+
switch (slot.kind) {
|
|
189
|
+
case 'static-text':
|
|
190
|
+
case 'element':
|
|
191
|
+
break
|
|
192
|
+
case 'component':
|
|
193
|
+
case 'fragment':
|
|
194
|
+
if (emitMarkers && hasSiblings)
|
|
195
|
+
marker = !isLast || inFragment ? 'range' : 'range-open'
|
|
196
|
+
hadVariableRange = true
|
|
197
|
+
break
|
|
198
|
+
case 'loop':
|
|
199
|
+
if (emitMarkers && (inFragment || !isLast || hadVariableRange))
|
|
200
|
+
marker = 'range'
|
|
201
|
+
hadVariableRange = true
|
|
202
|
+
break
|
|
203
|
+
case 'and-show':
|
|
204
|
+
case 'ternary-show':
|
|
205
|
+
if (bracketed)
|
|
206
|
+
marker = 'range'
|
|
207
|
+
hadVariableRange = true
|
|
208
|
+
break
|
|
209
|
+
case 'expr':
|
|
210
|
+
nodeCapable = isNodeCapableExpr(slot.expr, jsxNodes, exprContext)
|
|
211
|
+
if (nodeCapable) {
|
|
212
|
+
if (bracketed)
|
|
213
|
+
marker = !isLast || inFragment ? 'range' : 'range-open'
|
|
214
|
+
hadVariableRange = true
|
|
215
|
+
}
|
|
216
|
+
else if (bracketed) {
|
|
217
|
+
marker = !isLast || inFragment ? 'text' : 'text-open'
|
|
218
|
+
}
|
|
219
|
+
break
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
const [open, close] = MARKER_DELIMITERS[marker]
|
|
223
|
+
planned.push({ child, slot, marker, open, close, nodeCapable })
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
return planned
|
|
227
|
+
}
|