prunelle 0.0.0-stage → 0.7.2
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/LICENSE +21 -0
- package/llms.txt +245 -0
- package/package.json +62 -4
- package/src/a11y/declaration.ts +373 -0
- package/src/a11y/index.ts +477 -0
- package/src/a11y/rgaa-map.ts +230 -0
- package/src/ambient.d.ts +6 -0
- package/src/attrs.ts +43 -0
- package/src/build/bundle.ts +661 -0
- package/src/build/byte-size.ts +11 -0
- package/src/build/carry.ts +57 -0
- package/src/build/client-entries.ts +107 -0
- package/src/build/client-plugin.ts +175 -0
- package/src/build/client-prune.ts +168 -0
- package/src/build/compile.ts +130 -0
- package/src/build/ecoindex.ts +238 -0
- package/src/build/files.ts +171 -0
- package/src/build/first-load.ts +235 -0
- package/src/build/graph.ts +237 -0
- package/src/build/index.ts +678 -0
- package/src/build/is-inside.ts +12 -0
- package/src/build/island-promote.ts +311 -0
- package/src/build/islands.ts +452 -0
- package/src/build/manifest.ts +204 -0
- package/src/build/output.ts +81 -0
- package/src/build/plugin-host.ts +294 -0
- package/src/build/precompress.ts +139 -0
- package/src/build/prerender.ts +288 -0
- package/src/build/reach.ts +200 -0
- package/src/build/render-rules.ts +205 -0
- package/src/build/report.ts +290 -0
- package/src/build/resolve.ts +52 -0
- package/src/build/routes.ts +342 -0
- package/src/build/server-entry.ts +38 -0
- package/src/build/static-headers.ts +112 -0
- package/src/build/static.ts +209 -0
- package/src/build/status-pages.ts +127 -0
- package/src/build/style-bundles.ts +526 -0
- package/src/cli/check-forms.ts +636 -0
- package/src/cli/check-imports.ts +31 -0
- package/src/cli/check-links.ts +632 -0
- package/src/cli/check-security.ts +64 -0
- package/src/cli/check.ts +409 -0
- package/src/cli/commands.ts +372 -0
- package/src/cli/docs.ts +403 -0
- package/src/cli/index.ts +70 -0
- package/src/cli/inspect.ts +406 -0
- package/src/cli/mcp-tools.ts +492 -0
- package/src/cli/mcp.ts +205 -0
- package/src/cli/new.ts +52 -0
- package/src/cli/render.ts +416 -0
- package/src/cli/upgrade-names.ts +209 -0
- package/src/cli/upgrade.ts +367 -0
- package/src/compiler/analyze.ts +651 -0
- package/src/compiler/ast.ts +82 -0
- package/src/compiler/browser-globals.ts +250 -0
- package/src/compiler/cacheability-private.ts +566 -0
- package/src/compiler/cacheability.ts +335 -0
- package/src/compiler/compliance-eco.ts +71 -0
- package/src/compiler/compliance-rgaa.ts +614 -0
- package/src/compiler/compliance-urls.ts +148 -0
- package/src/compiler/compliance.ts +653 -0
- package/src/compiler/data-keys.ts +302 -0
- package/src/compiler/dev-source.ts +87 -0
- package/src/compiler/dom-attrs.ts +452 -0
- package/src/compiler/dom.ts +641 -0
- package/src/compiler/emit.ts +131 -0
- package/src/compiler/expr-context.ts +587 -0
- package/src/compiler/foreign-imports.ts +88 -0
- package/src/compiler/index.ts +8 -0
- package/src/compiler/interactivity.ts +366 -0
- package/src/compiler/mistakes.ts +520 -0
- package/src/compiler/node-env.ts +50 -0
- package/src/compiler/patch.ts +97 -0
- package/src/compiler/props-body.ts +60 -0
- package/src/compiler/props.ts +511 -0
- package/src/compiler/scope.ts +635 -0
- package/src/compiler/server-only.ts +365 -0
- package/src/compiler/shared.ts +397 -0
- package/src/compiler/signal-reads.ts +246 -0
- package/src/compiler/slots.ts +227 -0
- package/src/compiler/spread-handlers.ts +295 -0
- package/src/compiler/ssr-helpers.ts +555 -0
- package/src/compiler/ssr-island.ts +90 -0
- package/src/compiler/ssr-prunelle-tags.ts +69 -0
- package/src/compiler/ssr.ts +525 -0
- package/src/context.ts +199 -0
- package/src/dev/child.ts +30 -0
- package/src/dev/error-log.ts +177 -0
- package/src/dev/host-check.ts +32 -0
- package/src/dev/index.ts +268 -0
- package/src/dev/modules.ts +68 -0
- package/src/dev/open-editor.ts +228 -0
- package/src/dev/protocol.ts +24 -0
- package/src/dev/queue.ts +79 -0
- package/src/dev/reload.ts +159 -0
- package/src/dev/supervisor.ts +212 -0
- package/src/dev/watchers.ts +276 -0
- package/src/dom/action.ts +187 -0
- package/src/dom/attrs.ts +84 -0
- package/src/dom/claim.ts +120 -0
- package/src/dom/extras.ts +579 -0
- package/src/dom/index.ts +329 -0
- package/src/dom/island.ts +318 -0
- package/src/dom/loop.ts +484 -0
- package/src/dom/revive.ts +36 -0
- package/src/eco/declaration.ts +328 -0
- package/src/eco/index.ts +69 -0
- package/src/index.ts +22 -0
- package/src/jsx-runtime.ts +566 -0
- package/src/plugins/config.ts +92 -0
- package/src/plugins/context.ts +109 -0
- package/src/plugins/index.ts +14 -0
- package/src/plugins/load.ts +90 -0
- package/src/plugins/order.ts +148 -0
- package/src/plugins/public.ts +15 -0
- package/src/plugins/requires.ts +47 -0
- package/src/plugins/services.ts +119 -0
- package/src/plugins/types.ts +278 -0
- package/src/reactivity/graph.ts +259 -0
- package/src/reactivity/index.ts +541 -0
- package/src/reactivity/selector.ts +78 -0
- package/src/reactivity/store.ts +252 -0
- package/src/server/action-flash.ts +158 -0
- package/src/server/action-protect.ts +199 -0
- package/src/server/action-registry.ts +78 -0
- package/src/server/action.ts +511 -0
- package/src/server/app-entry.ts +23 -0
- package/src/server/app-secret.ts +63 -0
- package/src/server/assets.ts +168 -0
- package/src/server/compress.ts +646 -0
- package/src/server/consent.ts +290 -0
- package/src/server/cookies.ts +153 -0
- package/src/server/document.ts +286 -0
- package/src/server/env.ts +47 -0
- package/src/server/errors.ts +396 -0
- package/src/server/form-schema.ts +337 -0
- package/src/server/health.ts +275 -0
- package/src/server/http.ts +588 -0
- package/src/server/index.ts +615 -0
- package/src/server/lifecycle.ts +75 -0
- package/src/server/loader.ts +131 -0
- package/src/server/page.ts +122 -0
- package/src/server/param-matchers.ts +112 -0
- package/src/server/plugin-host.ts +267 -0
- package/src/server/prerendered.ts +71 -0
- package/src/server/public.ts +20 -0
- package/src/server/rate-limit.ts +199 -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-cache.ts +541 -0
- package/src/server/route-table.ts +424 -0
- package/src/server/router.ts +261 -0
- package/src/server/routing.ts +388 -0
- package/src/server/sanitize-html.ts +309 -0
- package/src/server/security.ts +550 -0
- package/src/server/serve-handlers.ts +93 -0
- package/src/server/served-file.ts +128 -0
- package/src/server/standard-schema.ts +104 -0
- package/src/server/start.ts +143 -0
- package/src/server/static-dir.ts +246 -0
- package/src/server/stream.ts +233 -0
- package/src/ssr/consent-banner.ts +58 -0
- package/src/ssr/context.ts +399 -0
- package/src/ssr/index.ts +624 -0
- package/src/ssr/navigation.ts +96 -0
- package/src/ssr/region.ts +192 -0
- package/src/style.ts +96 -0
- package/src/test/browser.ts +201 -0
- package/src/test/index.ts +262 -0
- package/src/ts-plugin/diagnostics.ts +505 -0
- package/src/ts-plugin/fixes.ts +364 -0
- package/src/ts-plugin/index.ts +194 -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 +8 -0
- package/templates/_shared/mcp.json +11 -0
- package/templates/_shared/public/robots.txt +2 -0
- package/templates/_shared/src/routes/legal.tsx +22 -0
- package/templates/_shared/tsconfig.json +25 -0
- package/templates/app/.env.example +8 -0
- package/templates/app/package.json +22 -0
- package/templates/app/prunelle.config.ts +13 -0
- package/templates/app/src/auth.ts +12 -0
- package/templates/app/src/mail.ts +4 -0
- package/templates/app/src/routes/_layout.tsx +40 -0
- package/templates/app/src/routes/dashboard/_middleware.ts +3 -0
- package/templates/app/src/routes/dashboard/index.tsx +47 -0
- package/templates/app/src/routes/index.tsx +15 -0
- package/templates/app/src/routes/login.tsx +53 -0
- package/templates/app/src/routes/privacy.tsx +25 -0
- package/templates/app/src/styles/app.css +14 -0
- package/templates/app/tests/app.test.ts +73 -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 +25 -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 +15 -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 +28 -0
- package/templates/fullstack/src/routes/_layout.tsx +18 -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 +39 -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 +18 -0
- package/templates/minimal/src/routes/index.tsx +11 -0
- package/templates/minimal/src/styles/app.css +32 -0
- package/ts-plugin.cjs +4277 -0
- package/README.md +0 -3
|
@@ -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
|
+
}
|
|
@@ -0,0 +1,295 @@
|
|
|
1
|
+
// Static detection of event handlers and refs passed through JSX spread attributes (`{...props}`, `{...rest}`, `{...{ onClick }}`).
|
|
2
|
+
|
|
3
|
+
import type { Function as OxcFunction, Node, Program } from 'oxc-parser'
|
|
4
|
+
import type { Interactivity } from './interactivity'
|
|
5
|
+
import type { CompilerWarning } from './mistakes'
|
|
6
|
+
import { walk } from './ast'
|
|
7
|
+
import { EVENT_ATTR, isComponentTag, jsxTagName, unwrapTsExpr } from './shared'
|
|
8
|
+
|
|
9
|
+
export interface BindingUnit {
|
|
10
|
+
name: string
|
|
11
|
+
node: Node
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
// A module's body as top-level bindings, each with the node holding its code; module-level statements go under ''.
|
|
15
|
+
export function bindingUnits(program: Program): BindingUnit[] {
|
|
16
|
+
const units: BindingUnit[] = []
|
|
17
|
+
const add = (node: Node): void => {
|
|
18
|
+
if (node.type.startsWith('TS'))
|
|
19
|
+
return
|
|
20
|
+
if ((node.type === 'FunctionDeclaration' || node.type === 'ClassDeclaration') && node.id !== null) {
|
|
21
|
+
units.push({ name: node.id.name, node })
|
|
22
|
+
}
|
|
23
|
+
else if (node.type === 'VariableDeclaration') {
|
|
24
|
+
for (const d of node.declarations)
|
|
25
|
+
units.push({ name: d.id.type === 'Identifier' ? d.id.name : '', node: d })
|
|
26
|
+
}
|
|
27
|
+
else {
|
|
28
|
+
units.push({ name: '', node })
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
for (const stmt of program.body) {
|
|
32
|
+
if (stmt.type === 'ExportNamedDeclaration') {
|
|
33
|
+
if (stmt.declaration !== null)
|
|
34
|
+
add(stmt.declaration)
|
|
35
|
+
}
|
|
36
|
+
else if (stmt.type === 'ExportDefaultDeclaration') {
|
|
37
|
+
const decl = unwrapTsExpr(stmt.declaration)
|
|
38
|
+
if ((decl.type === 'FunctionDeclaration' || decl.type === 'ClassDeclaration') && decl.id !== null)
|
|
39
|
+
add(decl)
|
|
40
|
+
else if (decl.type !== 'Identifier')
|
|
41
|
+
units.push({ name: 'default', node: decl })
|
|
42
|
+
}
|
|
43
|
+
else if (stmt.type !== 'ImportDeclaration') {
|
|
44
|
+
add(stmt)
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
return units
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/** Collects `const` / variable initialisers declared inside `unitNode`, layered over top-level `topInits`. */
|
|
51
|
+
export function collectUnitInits(unitNode: Node, topInits: ReadonlyMap<string, Node>): ReadonlyMap<string, Node> {
|
|
52
|
+
let merged: Map<string, Node> | undefined
|
|
53
|
+
walk(unitNode, (node) => {
|
|
54
|
+
if (node.type === 'VariableDeclarator' && node.id.type === 'Identifier' && node.init !== null) {
|
|
55
|
+
merged ??= new Map(topInits)
|
|
56
|
+
merged.set(node.id.name, node.init)
|
|
57
|
+
}
|
|
58
|
+
})
|
|
59
|
+
return merged ?? topInits
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
function propKeyName(key: Node): string | undefined {
|
|
63
|
+
if (key.type === 'Identifier')
|
|
64
|
+
return key.name
|
|
65
|
+
if (key.type === 'Literal' && typeof key.value === 'string')
|
|
66
|
+
return key.value
|
|
67
|
+
return undefined
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
function isInteractiveKey(name: string): boolean {
|
|
71
|
+
return EVENT_ATTR.test(name) || name === 'ref'
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
function isOmittedHandlerValue(value: Node | null | undefined): boolean {
|
|
75
|
+
if (value === null || value === undefined)
|
|
76
|
+
return false
|
|
77
|
+
if (value.type !== 'JSXExpressionContainer')
|
|
78
|
+
return false
|
|
79
|
+
if (value.expression.type === 'JSXEmptyExpression')
|
|
80
|
+
return true
|
|
81
|
+
const expr = unwrapTsExpr(value.expression)
|
|
82
|
+
return (expr.type === 'Identifier' && expr.name === 'undefined')
|
|
83
|
+
|| (expr.type === 'Literal' && (expr.value === null || expr.value === false))
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
function isObjectAssignCall(node: Extract<Node, { type: 'CallExpression' }>): boolean {
|
|
87
|
+
return node.callee.type === 'MemberExpression'
|
|
88
|
+
&& !node.callee.computed
|
|
89
|
+
&& node.callee.object.type === 'Identifier'
|
|
90
|
+
&& node.callee.object.name === 'Object'
|
|
91
|
+
&& node.callee.property.type === 'Identifier'
|
|
92
|
+
&& node.callee.property.name === 'assign'
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/** Finds the first `on*` event handler or `ref` statically present in a JSX spread expression. */
|
|
96
|
+
export function findSpreadInteractiveAttr(expr: Node, inits: ReadonlyMap<string, Node>, seen: Set<string> = new Set()): string | undefined {
|
|
97
|
+
const cur = unwrapTsExpr(expr)
|
|
98
|
+
if (cur.type === 'ObjectExpression') {
|
|
99
|
+
for (const prop of cur.properties) {
|
|
100
|
+
if (prop.type === 'Property' && !prop.computed) {
|
|
101
|
+
const key = propKeyName(prop.key)
|
|
102
|
+
if (key !== undefined && isInteractiveKey(key))
|
|
103
|
+
return key
|
|
104
|
+
}
|
|
105
|
+
else if (prop.type === 'SpreadElement') {
|
|
106
|
+
const found = findSpreadInteractiveAttr(prop.argument, inits, seen)
|
|
107
|
+
if (found !== undefined)
|
|
108
|
+
return found
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
return undefined
|
|
112
|
+
}
|
|
113
|
+
if (cur.type === 'ConditionalExpression')
|
|
114
|
+
return findSpreadInteractiveAttr(cur.consequent, inits, seen) ?? findSpreadInteractiveAttr(cur.alternate, inits, seen)
|
|
115
|
+
if (cur.type === 'LogicalExpression')
|
|
116
|
+
return cur.operator === '&&' ? findSpreadInteractiveAttr(cur.right, inits, seen) : findSpreadInteractiveAttr(cur.left, inits, seen) ?? findSpreadInteractiveAttr(cur.right, inits, seen)
|
|
117
|
+
if (cur.type === 'CallExpression' && isObjectAssignCall(cur)) {
|
|
118
|
+
for (const arg of cur.arguments) {
|
|
119
|
+
const target = arg.type === 'SpreadElement' ? arg.argument : arg
|
|
120
|
+
const found = findSpreadInteractiveAttr(target, inits, seen)
|
|
121
|
+
if (found !== undefined)
|
|
122
|
+
return found
|
|
123
|
+
}
|
|
124
|
+
return undefined
|
|
125
|
+
}
|
|
126
|
+
if (cur.type === 'Identifier' && !seen.has(cur.name)) {
|
|
127
|
+
const init = inits.get(cur.name)
|
|
128
|
+
if (init !== undefined) {
|
|
129
|
+
seen.add(cur.name)
|
|
130
|
+
return findSpreadInteractiveAttr(init, inits, seen)
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
return undefined
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
function unwrapComponentFn(node: Node): OxcFunction | Extract<Node, { type: 'ArrowFunctionExpression' }> | undefined {
|
|
137
|
+
const target = node.type === 'VariableDeclarator' ? (node.init === null ? undefined : unwrapTsExpr(node.init)) : unwrapTsExpr(node)
|
|
138
|
+
if (target === undefined)
|
|
139
|
+
return undefined
|
|
140
|
+
if (target.type === 'FunctionDeclaration' || target.type === 'FunctionExpression' || target.type === 'ArrowFunctionExpression')
|
|
141
|
+
return target
|
|
142
|
+
return undefined
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
function collectPropVars(fn: OxcFunction | Extract<Node, { type: 'ArrowFunctionExpression' }>): Set<string> {
|
|
146
|
+
const vars = new Set<string>()
|
|
147
|
+
const p0 = fn.params[0]
|
|
148
|
+
if (p0 === undefined)
|
|
149
|
+
return vars
|
|
150
|
+
const pat = p0.type === 'AssignmentPattern' ? p0.left : p0
|
|
151
|
+
if (pat.type === 'Identifier') {
|
|
152
|
+
vars.add(pat.name)
|
|
153
|
+
}
|
|
154
|
+
else if (pat.type === 'ObjectPattern') {
|
|
155
|
+
for (const prop of pat.properties) {
|
|
156
|
+
if (prop.type === 'RestElement' && prop.argument.type === 'Identifier')
|
|
157
|
+
vars.add(prop.argument.name)
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
if (vars.size === 0 || fn.body === null)
|
|
161
|
+
return vars
|
|
162
|
+
walk(fn.body, (node) => {
|
|
163
|
+
if (node.type !== 'VariableDeclarator' || node.init === null)
|
|
164
|
+
return
|
|
165
|
+
const init = unwrapTsExpr(node.init)
|
|
166
|
+
const carriesProps = (init.type === 'Identifier' && vars.has(init.name))
|
|
167
|
+
|| (init.type === 'CallExpression' && init.arguments.some(a => a.type !== 'SpreadElement' && unwrapTsExpr(a).type === 'Identifier' && vars.has((unwrapTsExpr(a) as Extract<Node, { type: 'Identifier' }>).name)))
|
|
168
|
+
if (!carriesProps)
|
|
169
|
+
return
|
|
170
|
+
if (node.id.type === 'Identifier') {
|
|
171
|
+
vars.add(node.id.name)
|
|
172
|
+
}
|
|
173
|
+
else if (node.id.type === 'ObjectPattern') {
|
|
174
|
+
for (const prop of node.id.properties) {
|
|
175
|
+
if (prop.type === 'RestElement' && prop.argument.type === 'Identifier')
|
|
176
|
+
vars.add(prop.argument.name)
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
})
|
|
180
|
+
return vars
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
function exprCarriesPropVar(expr: Node, propVars: ReadonlySet<string>): boolean {
|
|
184
|
+
const cur = unwrapTsExpr(expr)
|
|
185
|
+
if (cur.type === 'Identifier')
|
|
186
|
+
return propVars.has(cur.name)
|
|
187
|
+
if (cur.type === 'ObjectExpression')
|
|
188
|
+
return cur.properties.some(p => p.type === 'SpreadElement' && exprCarriesPropVar(p.argument, propVars))
|
|
189
|
+
if (cur.type === 'ConditionalExpression')
|
|
190
|
+
return exprCarriesPropVar(cur.consequent, propVars) || exprCarriesPropVar(cur.alternate, propVars)
|
|
191
|
+
if (cur.type === 'LogicalExpression')
|
|
192
|
+
return exprCarriesPropVar(cur.left, propVars) || exprCarriesPropVar(cur.right, propVars)
|
|
193
|
+
if (cur.type === 'CallExpression')
|
|
194
|
+
return cur.arguments.some(a => exprCarriesPropVar(a.type === 'SpreadElement' ? a.argument : a, propVars))
|
|
195
|
+
return false
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
function findStaticPropSpreaders(units: readonly BindingUnit[], inIsland: (offset: number) => boolean): Set<string> {
|
|
199
|
+
const direct = new Set<string>()
|
|
200
|
+
const forwardsTo = new Map<string, Set<string>>()
|
|
201
|
+
const staticNames = new Set<string>()
|
|
202
|
+
|
|
203
|
+
for (const unit of units) {
|
|
204
|
+
if (unit.name === '' || inIsland(unit.node.start))
|
|
205
|
+
continue
|
|
206
|
+
const fn = unwrapComponentFn(unit.node)
|
|
207
|
+
if (fn === undefined || fn.body === null)
|
|
208
|
+
continue
|
|
209
|
+
staticNames.add(unit.name)
|
|
210
|
+
const propVars = collectPropVars(fn)
|
|
211
|
+
if (propVars.size === 0)
|
|
212
|
+
continue
|
|
213
|
+
walk(fn.body, (node) => {
|
|
214
|
+
if (node.type !== 'JSXOpeningElement')
|
|
215
|
+
return
|
|
216
|
+
const tag = jsxTagName(node.name)
|
|
217
|
+
if (tag === '')
|
|
218
|
+
return
|
|
219
|
+
const spreadsProps = node.attributes.some(a => a.type === 'JSXSpreadAttribute' && exprCarriesPropVar(a.argument, propVars))
|
|
220
|
+
if (!spreadsProps)
|
|
221
|
+
return
|
|
222
|
+
if (!isComponentTag(tag)) {
|
|
223
|
+
direct.add(unit.name)
|
|
224
|
+
}
|
|
225
|
+
else if (!tag.includes('.')) {
|
|
226
|
+
const set = forwardsTo.get(unit.name) ?? new Set<string>()
|
|
227
|
+
set.add(tag)
|
|
228
|
+
forwardsTo.set(unit.name, set)
|
|
229
|
+
}
|
|
230
|
+
})
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
for (let changed = true; changed;) {
|
|
234
|
+
changed = false
|
|
235
|
+
for (const [name, targets] of forwardsTo) {
|
|
236
|
+
if (direct.has(name) || !staticNames.has(name))
|
|
237
|
+
continue
|
|
238
|
+
for (const target of targets) {
|
|
239
|
+
if (direct.has(target)) {
|
|
240
|
+
direct.add(name)
|
|
241
|
+
changed = true
|
|
242
|
+
break
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
return direct
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
/** Warns when non-interactive code passes an `on*` handler or `ref` to a local static component that spreads props onto a DOM element. */
|
|
251
|
+
export function checkStaticSpreadHandlers(
|
|
252
|
+
units: readonly BindingUnit[],
|
|
253
|
+
topInits: ReadonlyMap<string, Node>,
|
|
254
|
+
interactivity: Interactivity,
|
|
255
|
+
filename: string,
|
|
256
|
+
warnings: CompilerWarning[],
|
|
257
|
+
): void {
|
|
258
|
+
const inIsland = (offset: number): boolean => interactivity.islandSpans.some(([start, end]) => offset >= start && offset < end)
|
|
259
|
+
const spreaders = findStaticPropSpreaders(units, inIsland)
|
|
260
|
+
if (spreaders.size === 0)
|
|
261
|
+
return
|
|
262
|
+
|
|
263
|
+
for (const unit of units) {
|
|
264
|
+
if (inIsland(unit.node.start))
|
|
265
|
+
continue
|
|
266
|
+
const inits = collectUnitInits(unit.node, topInits)
|
|
267
|
+
walk(unit.node, (node) => {
|
|
268
|
+
if (node.type !== 'JSXOpeningElement')
|
|
269
|
+
return
|
|
270
|
+
const tag = jsxTagName(node.name)
|
|
271
|
+
if (!spreaders.has(tag))
|
|
272
|
+
return
|
|
273
|
+
for (const attr of node.attributes) {
|
|
274
|
+
if (attr.type === 'JSXAttribute' && attr.name.type === 'JSXIdentifier') {
|
|
275
|
+
const name = attr.name.name
|
|
276
|
+
if (isInteractiveKey(name) && !isOmittedHandlerValue(attr.value)) {
|
|
277
|
+
warnings.push({
|
|
278
|
+
start: attr.start,
|
|
279
|
+
message: `prunelle compiler: "${name}" passed to <${tag}> in ${filename} will not bind because <${tag}> is static and its caller is not an island: move the handler inside <${tag}>, or render <${tag}> from an island.`,
|
|
280
|
+
})
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
else if (attr.type === 'JSXSpreadAttribute') {
|
|
284
|
+
const found = findSpreadInteractiveAttr(attr.argument, inits)
|
|
285
|
+
if (found !== undefined) {
|
|
286
|
+
warnings.push({
|
|
287
|
+
start: attr.start,
|
|
288
|
+
message: `prunelle compiler: "${found}" spread into <${tag}> in ${filename} will not bind because <${tag}> is static and its caller is not an island: move the handler inside <${tag}>, or render <${tag}> from an island.`,
|
|
289
|
+
})
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
})
|
|
294
|
+
}
|
|
295
|
+
}
|