prunelle 0.1.1 → 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 +137 -40
- package/package.json +11 -2
- package/src/a11y/declaration.ts +373 -0
- package/src/a11y/index.ts +477 -0
- package/src/a11y/rgaa-map.ts +230 -0
- package/src/attrs.ts +11 -3
- package/src/build/bundle.ts +89 -41
- package/src/build/byte-size.ts +11 -0
- package/src/build/client-entries.ts +3 -2
- package/src/build/client-plugin.ts +11 -5
- package/src/build/client-prune.ts +56 -6
- package/src/build/compile.ts +13 -10
- package/src/build/ecoindex.ts +238 -0
- package/src/build/files.ts +15 -0
- package/src/build/first-load.ts +85 -14
- package/src/build/graph.ts +29 -39
- package/src/build/index.ts +225 -122
- package/src/build/island-promote.ts +311 -0
- package/src/build/islands.ts +14 -203
- package/src/build/manifest.ts +56 -116
- package/src/build/output.ts +3 -6
- package/src/build/plugin-host.ts +115 -41
- package/src/build/precompress.ts +16 -3
- package/src/build/prerender.ts +177 -39
- package/src/build/reach.ts +200 -0
- package/src/build/render-rules.ts +2 -2
- package/src/build/report.ts +48 -5
- package/src/build/resolve.ts +8 -6
- package/src/build/routes.ts +175 -10
- package/src/build/static-headers.ts +112 -0
- package/src/build/static.ts +139 -39
- 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 +101 -26
- package/src/cli/commands.ts +120 -11
- package/src/cli/docs.ts +403 -0
- package/src/cli/inspect.ts +105 -15
- package/src/cli/mcp-tools.ts +492 -0
- package/src/cli/mcp.ts +205 -0
- package/src/cli/new.ts +16 -5
- package/src/cli/render.ts +416 -0
- package/src/cli/upgrade-names.ts +73 -13
- package/src/cli/upgrade.ts +156 -22
- package/src/compiler/analyze.ts +87 -54
- package/src/compiler/browser-globals.ts +140 -17
- 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 +407 -352
- package/src/compiler/data-keys.ts +199 -192
- package/src/compiler/dev-source.ts +87 -0
- package/src/compiler/dom-attrs.ts +138 -65
- package/src/compiler/dom.ts +517 -442
- package/src/compiler/expr-context.ts +260 -81
- package/src/compiler/foreign-imports.ts +88 -0
- package/src/compiler/mistakes.ts +23 -2
- package/src/compiler/props-body.ts +60 -0
- package/src/compiler/props.ts +87 -36
- package/src/compiler/scope.ts +400 -333
- package/src/compiler/server-only.ts +190 -151
- package/src/compiler/shared.ts +42 -14
- package/src/compiler/signal-reads.ts +6 -2
- package/src/compiler/spread-handlers.ts +295 -0
- package/src/compiler/ssr-prunelle-tags.ts +27 -14
- package/src/compiler/ssr.ts +220 -89
- package/src/context.ts +34 -0
- package/src/dev/error-log.ts +177 -0
- package/src/dev/host-check.ts +32 -0
- package/src/dev/index.ts +94 -34
- package/src/dev/open-editor.ts +228 -0
- package/src/dev/protocol.ts +3 -0
- package/src/dev/reload.ts +5 -3
- package/src/dev/supervisor.ts +50 -25
- package/src/dev/watchers.ts +74 -13
- package/src/dom/action.ts +47 -21
- package/src/dom/attrs.ts +15 -3
- package/src/dom/extras.ts +57 -40
- package/src/dom/index.ts +54 -14
- package/src/dom/island.ts +102 -68
- package/src/dom/loop.ts +66 -32
- package/src/eco/declaration.ts +328 -0
- package/src/eco/index.ts +69 -0
- package/src/index.ts +2 -2
- package/src/jsx-runtime.ts +2 -2
- package/src/plugins/config.ts +18 -12
- package/src/plugins/context.ts +44 -1
- package/src/plugins/index.ts +2 -2
- package/src/plugins/load.ts +30 -10
- package/src/plugins/order.ts +38 -1
- package/src/plugins/public.ts +6 -3
- package/src/plugins/services.ts +84 -0
- package/src/plugins/types.ts +51 -2
- package/src/reactivity/graph.ts +10 -0
- package/src/reactivity/index.ts +24 -16
- package/src/reactivity/selector.ts +31 -4
- package/src/reactivity/store.ts +64 -20
- package/src/server/action-flash.ts +158 -0
- package/src/server/action-protect.ts +199 -0
- package/src/server/action-registry.ts +1 -1
- package/src/server/action.ts +188 -152
- package/src/server/app-secret.ts +63 -0
- package/src/server/assets.ts +34 -2
- package/src/server/compress.ts +96 -10
- package/src/server/consent.ts +59 -22
- package/src/server/cookies.ts +16 -2
- package/src/server/document.ts +25 -10
- package/src/server/env.ts +14 -0
- package/src/server/errors.ts +308 -65
- package/src/server/form-schema.ts +337 -0
- package/src/server/health.ts +275 -0
- package/src/server/http.ts +186 -39
- package/src/server/index.ts +272 -273
- package/src/server/loader.ts +15 -2
- package/src/server/page.ts +15 -0
- package/src/server/param-matchers.ts +112 -0
- package/src/server/plugin-host.ts +125 -88
- package/src/server/public.ts +6 -5
- package/src/server/rate-limit.ts +102 -36
- package/src/server/request-body.ts +1 -1
- package/src/server/route-cache.ts +541 -0
- package/src/server/route-table.ts +356 -11
- package/src/server/router.ts +125 -40
- package/src/server/routing.ts +112 -63
- package/src/server/sanitize-html.ts +309 -0
- package/src/server/security.ts +279 -52
- package/src/server/serve-handlers.ts +31 -7
- package/src/server/standard-schema.ts +17 -0
- package/src/server/start.ts +60 -16
- package/src/server/static-dir.ts +91 -12
- package/src/server/stream.ts +68 -27
- package/src/ssr/consent-banner.ts +58 -0
- package/src/ssr/context.ts +106 -8
- package/src/ssr/index.ts +151 -4
- package/src/ssr/navigation.ts +2 -2
- package/src/ssr/region.ts +40 -19
- package/src/style.ts +64 -1
- package/src/test/browser.ts +28 -13
- package/src/test/index.ts +97 -12
- 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/templates/_shared/AGENTS.md +2 -2
- package/templates/_shared/gitignore +5 -0
- package/templates/_shared/mcp.json +11 -0
- package/templates/_shared/src/routes/legal.tsx +15 -1
- package/templates/_shared/tsconfig.json +5 -0
- package/templates/app/.env.example +4 -0
- package/templates/app/package.json +2 -1
- package/templates/app/prunelle.config.ts +2 -0
- package/templates/app/src/auth.ts +4 -6
- package/templates/app/src/mail.ts +4 -0
- package/templates/app/src/routes/_layout.tsx +9 -1
- package/templates/app/src/routes/dashboard/index.tsx +42 -8
- package/templates/app/src/routes/login.tsx +49 -30
- package/templates/app/src/routes/privacy.tsx +25 -0
- package/templates/app/src/styles/app.css +8 -1
- package/templates/app/tests/app.test.ts +44 -3
- package/templates/blog/package.json +1 -1
- package/templates/blog/prunelle.config.ts +1 -1
- package/templates/blog/src/routes/_layout.tsx +6 -1
- package/templates/blog/src/routes/index.tsx +5 -1
- package/templates/blog/src/styles/app.css +6 -1
- package/templates/fullstack/package.json +1 -1
- package/templates/fullstack/src/components/GreetForm.tsx +2 -1
- package/templates/fullstack/src/routes/_layout.tsx +4 -0
- package/templates/fullstack/src/styles/app.css +11 -1
- package/templates/minimal/package.json +1 -1
- package/templates/minimal/src/routes/_layout.tsx +4 -0
- package/templates/minimal/src/styles/app.css +11 -1
- package/ts-plugin.cjs +4277 -0
|
@@ -4,6 +4,14 @@ import type { ArrowFunctionExpression, Function as OxcFunction, Node, Program }
|
|
|
4
4
|
import { visitorKeys } from 'oxc-parser'
|
|
5
5
|
import { collectPatternNames, unwrapTsExpr } from './shared'
|
|
6
6
|
|
|
7
|
+
const TS_EXPR_WRAPPERS = new Set([
|
|
8
|
+
'TSAsExpression',
|
|
9
|
+
'TSNonNullExpression',
|
|
10
|
+
'TSSatisfiesExpression',
|
|
11
|
+
'TSTypeAssertion',
|
|
12
|
+
'TSInstantiationExpression',
|
|
13
|
+
])
|
|
14
|
+
|
|
7
15
|
function staticPropKey(prop: Node): string | undefined {
|
|
8
16
|
if (prop.type !== 'Property')
|
|
9
17
|
return undefined
|
|
@@ -76,220 +84,219 @@ function resolveDefaultFunction(
|
|
|
76
84
|
return undefined
|
|
77
85
|
}
|
|
78
86
|
|
|
79
|
-
|
|
80
|
-
'
|
|
81
|
-
'
|
|
82
|
-
'TSSatisfiesExpression',
|
|
83
|
-
'TSTypeAssertion',
|
|
84
|
-
'TSInstantiationExpression',
|
|
85
|
-
])
|
|
87
|
+
type ParamTarget =
|
|
88
|
+
| { kind: 'done', keys: string[] | undefined }
|
|
89
|
+
| { kind: 'track', dataVar?: string | undefined, propsVar?: string | undefined }
|
|
86
90
|
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
const
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
91
|
+
function resolveDataParam(firstParam: Node): ParamTarget {
|
|
92
|
+
if (firstParam.type === 'Identifier')
|
|
93
|
+
return { kind: 'track', propsVar: firstParam.name }
|
|
94
|
+
if (firstParam.type !== 'ObjectPattern')
|
|
95
|
+
return { kind: 'done', keys: undefined }
|
|
96
|
+
let dataProp: Node | undefined
|
|
97
|
+
for (const prop of firstParam.properties) {
|
|
98
|
+
if (prop.type === 'RestElement')
|
|
99
|
+
return { kind: 'done', keys: undefined }
|
|
100
|
+
const key = staticPropKey(prop)
|
|
101
|
+
// A computed/non-static key in the props pattern might bind `data`, so bail out safely instead of stripping live keys.
|
|
102
|
+
if (key === undefined)
|
|
103
|
+
return { kind: 'done', keys: undefined }
|
|
104
|
+
if (key === 'data')
|
|
105
|
+
dataProp = prop
|
|
106
|
+
}
|
|
107
|
+
if (dataProp === undefined || dataProp.type !== 'Property')
|
|
108
|
+
return { kind: 'done', keys: [] }
|
|
109
|
+
const binding = unwrapParamPattern(dataProp.value)
|
|
110
|
+
if (binding.type === 'ObjectPattern') {
|
|
111
|
+
const nested = extractObjectPatternKeys(binding)
|
|
112
|
+
return { kind: 'done', keys: nested === undefined ? undefined : [...new Set(nested)].sort() }
|
|
113
|
+
}
|
|
114
|
+
return binding.type === 'Identifier' ? { kind: 'track', dataVar: binding.name } : { kind: 'done', keys: undefined }
|
|
115
|
+
}
|
|
98
116
|
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
117
|
+
interface DataScanState {
|
|
118
|
+
fn: OxcFunction | ArrowFunctionExpression
|
|
119
|
+
dataVar: string | undefined
|
|
120
|
+
propsVar: string | undefined
|
|
121
|
+
keys: Set<string>
|
|
122
|
+
escaped: boolean
|
|
123
|
+
ancestors: Node[]
|
|
124
|
+
}
|
|
104
125
|
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
return undefined
|
|
124
|
-
return [...new Set(nested)].sort()
|
|
125
|
-
}
|
|
126
|
-
if (binding.type !== 'Identifier')
|
|
127
|
-
return undefined
|
|
128
|
-
dataVar = binding.name
|
|
126
|
+
function isNonReferenceIdent(ast: Node, parent: Node | undefined): boolean {
|
|
127
|
+
if (parent === undefined)
|
|
128
|
+
return false
|
|
129
|
+
return (parent.type === 'MemberExpression' && !parent.computed && parent.property === ast)
|
|
130
|
+
|| (parent.type === 'Property' && !parent.computed && parent.key === ast && !parent.shorthand)
|
|
131
|
+
|| (parent.type === 'VariableDeclarator' && parent.id === ast)
|
|
132
|
+
|| (parent.type === 'LabeledStatement' && parent.label === ast)
|
|
133
|
+
|| (parent.type === 'FunctionDeclaration' && parent.id === ast)
|
|
134
|
+
|| (parent.type === 'FunctionExpression' && parent.id === ast)
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
function recordDataAccess(holder: Node | undefined, target: Node, state: DataScanState): void {
|
|
138
|
+
if (holder?.type === 'MemberExpression' && holder.object === target) {
|
|
139
|
+
const k = memberKeyName(holder)
|
|
140
|
+
if (k === undefined)
|
|
141
|
+
state.escaped = true
|
|
142
|
+
else
|
|
143
|
+
state.keys.add(k)
|
|
129
144
|
}
|
|
130
|
-
else if (
|
|
131
|
-
|
|
145
|
+
else if (holder?.type === 'VariableDeclarator' && holder.init === target) {
|
|
146
|
+
const nested = extractObjectPatternKeys(holder.id)
|
|
147
|
+
if (nested === undefined)
|
|
148
|
+
state.escaped = true
|
|
149
|
+
else
|
|
150
|
+
for (const k of nested) state.keys.add(k)
|
|
132
151
|
}
|
|
133
152
|
else {
|
|
134
|
-
|
|
153
|
+
state.escaped = true
|
|
135
154
|
}
|
|
155
|
+
}
|
|
136
156
|
|
|
137
|
-
|
|
138
|
-
const ancestors
|
|
139
|
-
const
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
157
|
+
function checkIdentifierDataRef(ast: Extract<Node, { type: 'Identifier' }>, state: DataScanState): void {
|
|
158
|
+
const { ancestors, dataVar, propsVar } = state
|
|
159
|
+
const parent = ancestors[ancestors.length - 1]
|
|
160
|
+
if (isNonReferenceIdent(ast, parent))
|
|
161
|
+
return
|
|
162
|
+
if (dataVar !== undefined && ast.name === dataVar) {
|
|
163
|
+
recordDataAccess(parent, ast, state)
|
|
164
|
+
return
|
|
165
|
+
}
|
|
166
|
+
if (propsVar === undefined || ast.name !== propsVar)
|
|
167
|
+
return
|
|
168
|
+
if (parent?.type !== 'MemberExpression' || parent.object !== ast) {
|
|
169
|
+
state.escaped = true
|
|
170
|
+
return
|
|
171
|
+
}
|
|
172
|
+
const propName = memberKeyName(parent)
|
|
173
|
+
if (propName === undefined) {
|
|
174
|
+
state.escaped = true
|
|
175
|
+
return
|
|
176
|
+
}
|
|
177
|
+
if (propName !== 'data')
|
|
178
|
+
return
|
|
179
|
+
const grand = ancestors[ancestors.length - 2]
|
|
180
|
+
const holder = grand?.type === 'ChainExpression' ? ancestors[ancestors.length - 3] : grand
|
|
181
|
+
const target = grand?.type === 'ChainExpression' ? grand : parent
|
|
182
|
+
recordDataAccess(holder, target, state)
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
function walkDataPatternDefaults(pattern: Node | null | undefined, state: DataScanState): void {
|
|
186
|
+
if (state.escaped || pattern === null || pattern === undefined)
|
|
187
|
+
return
|
|
188
|
+
switch (pattern.type) {
|
|
189
|
+
case 'AssignmentPattern':
|
|
190
|
+
walkDataPatternDefaults(pattern.left, state)
|
|
191
|
+
walkDataNode(pattern.right, state)
|
|
145
192
|
return
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
const type = ast.type
|
|
149
|
-
if (typeof type !== 'string')
|
|
193
|
+
case 'RestElement':
|
|
194
|
+
walkDataPatternDefaults(pattern.argument, state)
|
|
150
195
|
return
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
if (TS_EXPR_WRAPPERS.has(type))
|
|
154
|
-
walkWithParents((ast as { expression?: unknown }).expression)
|
|
196
|
+
case 'TSParameterProperty':
|
|
197
|
+
walkDataPatternDefaults(pattern.parameter, state)
|
|
155
198
|
return
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
// walking parameter defaults before each parameter's bound names enter scope.
|
|
160
|
-
if (ast !== fn && (type === 'FunctionDeclaration' || type === 'FunctionExpression' || type === 'ArrowFunctionExpression')) {
|
|
161
|
-
const innerFn = ast as OxcFunction | ArrowFunctionExpression
|
|
162
|
-
const shadowed = new Set<string>()
|
|
163
|
-
if (innerFn.type !== 'ArrowFunctionExpression' && innerFn.id !== null)
|
|
164
|
-
shadowed.add(innerFn.id.name)
|
|
165
|
-
let isShadowed = (dataVar !== undefined && shadowed.has(dataVar)) || (propsVar !== undefined && shadowed.has(propsVar))
|
|
166
|
-
for (const p of innerFn.params) {
|
|
167
|
-
if (isShadowed)
|
|
168
|
-
return
|
|
169
|
-
walkPatternDefaults(p)
|
|
170
|
-
collectPatternNames(p, shadowed)
|
|
171
|
-
if ((dataVar !== undefined && shadowed.has(dataVar)) || (propsVar !== undefined && shadowed.has(propsVar)))
|
|
172
|
-
isShadowed = true
|
|
173
|
-
}
|
|
174
|
-
if (!isShadowed && innerFn.body !== null)
|
|
175
|
-
walkWithParents(innerFn.body)
|
|
199
|
+
case 'ArrayPattern':
|
|
200
|
+
for (const el of pattern.elements)
|
|
201
|
+
walkDataPatternDefaults(el, state)
|
|
176
202
|
return
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
const grand = ancestors[ancestors.length - 2]
|
|
182
|
-
const isNonRef = parent !== undefined && (
|
|
183
|
-
(parent.type === 'MemberExpression' && !parent.computed && parent.property === ast)
|
|
184
|
-
|| (parent.type === 'Property' && !parent.computed && parent.key === ast && !parent.shorthand)
|
|
185
|
-
|| (parent.type === 'VariableDeclarator' && parent.id === ast)
|
|
186
|
-
|| (parent.type === 'LabeledStatement' && parent.label === ast)
|
|
187
|
-
|| (parent.type === 'FunctionDeclaration' && parent.id === ast)
|
|
188
|
-
|| (parent.type === 'FunctionExpression' && parent.id === ast)
|
|
189
|
-
)
|
|
190
|
-
|
|
191
|
-
if (!isNonRef && dataVar !== undefined && ast.name === dataVar) {
|
|
192
|
-
if (parent?.type === 'MemberExpression' && parent.object === ast) {
|
|
193
|
-
const k = memberKeyName(parent)
|
|
194
|
-
if (k === undefined)
|
|
195
|
-
escaped = true
|
|
196
|
-
else
|
|
197
|
-
keys.add(k)
|
|
198
|
-
}
|
|
199
|
-
else if (parent?.type === 'VariableDeclarator' && parent.init === ast) {
|
|
200
|
-
const nested = extractObjectPatternKeys(parent.id)
|
|
201
|
-
if (nested === undefined)
|
|
202
|
-
escaped = true
|
|
203
|
-
else
|
|
204
|
-
for (const k of nested) keys.add(k)
|
|
205
|
-
}
|
|
206
|
-
else {
|
|
207
|
-
escaped = true
|
|
208
|
-
}
|
|
209
|
-
}
|
|
210
|
-
else if (!isNonRef && propsVar !== undefined && ast.name === propsVar) {
|
|
211
|
-
if (parent?.type === 'MemberExpression' && parent.object === ast) {
|
|
212
|
-
const propName = memberKeyName(parent)
|
|
213
|
-
if (propName === undefined) {
|
|
214
|
-
escaped = true
|
|
215
|
-
}
|
|
216
|
-
else if (propName === 'data') {
|
|
217
|
-
const holder = grand?.type === 'ChainExpression' ? ancestors[ancestors.length - 3] : grand
|
|
218
|
-
const target = grand?.type === 'ChainExpression' ? grand : parent
|
|
219
|
-
if (holder?.type === 'MemberExpression' && holder.object === target) {
|
|
220
|
-
const k = memberKeyName(holder)
|
|
221
|
-
if (k === undefined)
|
|
222
|
-
escaped = true
|
|
223
|
-
else
|
|
224
|
-
keys.add(k)
|
|
225
|
-
}
|
|
226
|
-
else if (holder?.type === 'VariableDeclarator' && holder.init === target) {
|
|
227
|
-
const nested = extractObjectPatternKeys(holder.id)
|
|
228
|
-
if (nested === undefined)
|
|
229
|
-
escaped = true
|
|
230
|
-
else
|
|
231
|
-
for (const k of nested) keys.add(k)
|
|
232
|
-
}
|
|
233
|
-
else {
|
|
234
|
-
escaped = true
|
|
235
|
-
}
|
|
236
|
-
}
|
|
203
|
+
case 'ObjectPattern':
|
|
204
|
+
for (const prop of pattern.properties) {
|
|
205
|
+
if (prop.type === 'RestElement') {
|
|
206
|
+
walkDataPatternDefaults(prop.argument, state)
|
|
237
207
|
}
|
|
238
208
|
else {
|
|
239
|
-
|
|
209
|
+
if (prop.computed)
|
|
210
|
+
walkDataNode(prop.key, state)
|
|
211
|
+
walkDataPatternDefaults(prop.value, state)
|
|
240
212
|
}
|
|
241
213
|
}
|
|
242
|
-
}
|
|
243
|
-
|
|
244
|
-
ancestors.push(ast)
|
|
245
|
-
const childKeys = visitorKeys[type]
|
|
246
|
-
if (childKeys !== undefined) {
|
|
247
|
-
const rec = ast as unknown as Record<string, unknown>
|
|
248
|
-
for (let i = 0; i < childKeys.length; i++) {
|
|
249
|
-
const val = rec[childKeys[i]!]
|
|
250
|
-
if (val !== null && typeof val === 'object')
|
|
251
|
-
walkWithParents(val)
|
|
252
|
-
}
|
|
253
|
-
}
|
|
254
|
-
ancestors.pop()
|
|
255
214
|
}
|
|
215
|
+
}
|
|
256
216
|
|
|
257
|
-
|
|
258
|
-
|
|
217
|
+
function walkInnerFunction(innerFn: OxcFunction | ArrowFunctionExpression, state: DataScanState): void {
|
|
218
|
+
const shadowed = new Set<string>()
|
|
219
|
+
if (innerFn.type !== 'ArrowFunctionExpression' && innerFn.id !== null)
|
|
220
|
+
shadowed.add(innerFn.id.name)
|
|
221
|
+
const shadowsTarget = (): boolean =>
|
|
222
|
+
(state.dataVar !== undefined && shadowed.has(state.dataVar))
|
|
223
|
+
|| (state.propsVar !== undefined && shadowed.has(state.propsVar))
|
|
224
|
+
if (shadowsTarget())
|
|
225
|
+
return
|
|
226
|
+
for (const p of innerFn.params) {
|
|
227
|
+
walkDataPatternDefaults(p, state)
|
|
228
|
+
collectPatternNames(p, shadowed)
|
|
229
|
+
if (shadowsTarget())
|
|
259
230
|
return
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
231
|
+
}
|
|
232
|
+
if (innerFn.body !== null)
|
|
233
|
+
walkDataNode(innerFn.body, state)
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
function walkDataNode(node: unknown, state: DataScanState): void {
|
|
237
|
+
if (state.escaped || node === null || typeof node !== 'object')
|
|
238
|
+
return
|
|
239
|
+
if (Array.isArray(node)) {
|
|
240
|
+
for (let i = 0; i < node.length; i++)
|
|
241
|
+
walkDataNode(node[i], state)
|
|
242
|
+
return
|
|
243
|
+
}
|
|
244
|
+
const ast = node as Node
|
|
245
|
+
const type = ast.type
|
|
246
|
+
if (typeof type !== 'string')
|
|
247
|
+
return
|
|
248
|
+
if (type.startsWith('TS')) {
|
|
249
|
+
if (TS_EXPR_WRAPPERS.has(type))
|
|
250
|
+
walkDataNode((ast as { expression?: unknown }).expression, state)
|
|
251
|
+
return
|
|
252
|
+
}
|
|
253
|
+
if (ast !== state.fn && (type === 'FunctionDeclaration' || type === 'FunctionExpression' || type === 'ArrowFunctionExpression')) {
|
|
254
|
+
walkInnerFunction(ast as OxcFunction | ArrowFunctionExpression, state)
|
|
255
|
+
return
|
|
256
|
+
}
|
|
257
|
+
if (type === 'Identifier')
|
|
258
|
+
checkIdentifierDataRef(ast, state)
|
|
259
|
+
|
|
260
|
+
state.ancestors.push(ast)
|
|
261
|
+
const childKeys = visitorKeys[type]
|
|
262
|
+
if (childKeys !== undefined) {
|
|
263
|
+
const rec = ast as unknown as Record<string, unknown>
|
|
264
|
+
for (let i = 0; i < childKeys.length; i++) {
|
|
265
|
+
const val = rec[childKeys[i]!]
|
|
266
|
+
if (val !== null && typeof val === 'object')
|
|
267
|
+
walkDataNode(val, state)
|
|
286
268
|
}
|
|
287
269
|
}
|
|
270
|
+
state.ancestors.pop()
|
|
271
|
+
}
|
|
288
272
|
|
|
273
|
+
/** Statically bounds which top-level keys of `props.data` a page island reads; returns `undefined` if `data` escapes. */
|
|
274
|
+
export function analyzeDefaultDataKeys(
|
|
275
|
+
program: Program,
|
|
276
|
+
defaultLocal: string | undefined,
|
|
277
|
+
localInits: ReadonlyMap<string, Node>,
|
|
278
|
+
): string[] | undefined {
|
|
279
|
+
const fn = resolveDefaultFunction(program, defaultLocal, localInits)
|
|
280
|
+
if (fn === undefined || fn.body === null)
|
|
281
|
+
return undefined
|
|
282
|
+
if (fn.params.length === 0)
|
|
283
|
+
return []
|
|
284
|
+
|
|
285
|
+
const target = resolveDataParam(unwrapParamPattern(fn.params[0]!))
|
|
286
|
+
if (target.kind === 'done')
|
|
287
|
+
return target.keys
|
|
288
|
+
|
|
289
|
+
const state: DataScanState = {
|
|
290
|
+
fn,
|
|
291
|
+
dataVar: target.dataVar,
|
|
292
|
+
propsVar: target.propsVar,
|
|
293
|
+
keys: new Set<string>(),
|
|
294
|
+
escaped: false,
|
|
295
|
+
ancestors: [],
|
|
296
|
+
}
|
|
289
297
|
// Walk outer parameter defaults (e.g. `function Page({ data, title = data.title })`) before the body.
|
|
290
298
|
for (const p of fn.params)
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
return escaped ? undefined : [...keys].sort()
|
|
299
|
+
walkDataPatternDefaults(p, state)
|
|
300
|
+
walkDataNode(fn.body, state)
|
|
301
|
+
return state.escaped ? undefined : [...state.keys].sort()
|
|
295
302
|
}
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
// Dev-only `data-pru-src="file:line:col"` source-location tagging for SSR and DOM compilers.
|
|
2
|
+
|
|
3
|
+
import type { JSXElement } from 'oxc-parser'
|
|
4
|
+
import { isAbsolute, relative } from 'node:path'
|
|
5
|
+
import { escapeStaticAttr, jsxTagName } from './shared'
|
|
6
|
+
|
|
7
|
+
const DEV_SOURCE_SKIP_TAGS = new Set([
|
|
8
|
+
'html',
|
|
9
|
+
'head',
|
|
10
|
+
'title',
|
|
11
|
+
'meta',
|
|
12
|
+
'link',
|
|
13
|
+
'base',
|
|
14
|
+
'style',
|
|
15
|
+
'script',
|
|
16
|
+
'noscript',
|
|
17
|
+
])
|
|
18
|
+
|
|
19
|
+
const NOOP_DEV_SOURCE = (): string => ''
|
|
20
|
+
|
|
21
|
+
function toDevRelPath(filename: string, devSourceRoot: string): string {
|
|
22
|
+
if (!isAbsolute(filename) && isAbsolute(devSourceRoot))
|
|
23
|
+
return filename.replaceAll('\\', '/')
|
|
24
|
+
const rel = relative(devSourceRoot, filename).replaceAll('\\', '/')
|
|
25
|
+
if (rel === '' || rel === '..' || rel.startsWith('../') || isAbsolute(rel))
|
|
26
|
+
return filename.replaceAll('\\', '/')
|
|
27
|
+
return rel
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
function hasDevSourceAttr(node: JSXElement): boolean {
|
|
31
|
+
for (const attr of node.openingElement.attributes) {
|
|
32
|
+
if (attr.type === 'JSXAttribute' && jsxTagName(attr.name) === 'data-pru-src')
|
|
33
|
+
return true
|
|
34
|
+
}
|
|
35
|
+
return false
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/** Precomputes the character offset of every 1-based line start in `code`. */
|
|
39
|
+
export function buildLineStarts(code: string): number[] {
|
|
40
|
+
const starts = [0]
|
|
41
|
+
for (let i = 0; i < code.length; i++) {
|
|
42
|
+
if (code.charCodeAt(i) === 10)
|
|
43
|
+
starts.push(i + 1)
|
|
44
|
+
}
|
|
45
|
+
return starts
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/** O(log N) lookup of 1-based `{ line, col }` for a character offset using precomputed `lineStarts`. */
|
|
49
|
+
export function offsetToLineColFast(lineStarts: readonly number[], offset: number): { line: number, col: number } {
|
|
50
|
+
const clamped = offset < 0 ? 0 : offset
|
|
51
|
+
let lo = 0
|
|
52
|
+
let hi = lineStarts.length - 1
|
|
53
|
+
while (lo < hi) {
|
|
54
|
+
const mid = (lo + hi + 1) >> 1
|
|
55
|
+
if (lineStarts[mid]! <= clamped)
|
|
56
|
+
lo = mid
|
|
57
|
+
else
|
|
58
|
+
hi = mid - 1
|
|
59
|
+
}
|
|
60
|
+
return { line: lo + 1, col: clamped - lineStarts[lo]! + 1 }
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/** Formats ` data-pru-src="relPath:line:col"` for an intrinsic JSX element, or `''` when skipped. */
|
|
64
|
+
export function formatDevSourceAttr(
|
|
65
|
+
node: JSXElement,
|
|
66
|
+
tag: string,
|
|
67
|
+
relPath: string,
|
|
68
|
+
lineStarts: readonly number[],
|
|
69
|
+
): string {
|
|
70
|
+
if (DEV_SOURCE_SKIP_TAGS.has(tag) || hasDevSourceAttr(node))
|
|
71
|
+
return ''
|
|
72
|
+
const { line, col } = offsetToLineColFast(lineStarts, node.openingElement.start)
|
|
73
|
+
return ` data-pru-src="${escapeStaticAttr(`${relPath}:${line}:${col}`)}"`
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/** Returns a per-element `data-pru-src` formatter when `devSourceRoot` is set, or a zero-cost no-op otherwise. */
|
|
77
|
+
export function createDevSourceAttr(
|
|
78
|
+
filename: string,
|
|
79
|
+
code: string,
|
|
80
|
+
devSourceRoot: string | undefined,
|
|
81
|
+
): (node: JSXElement, tag: string) => string {
|
|
82
|
+
if (devSourceRoot === undefined)
|
|
83
|
+
return NOOP_DEV_SOURCE
|
|
84
|
+
const relPath = toDevRelPath(filename, devSourceRoot)
|
|
85
|
+
const lineStarts = buildLineStarts(code)
|
|
86
|
+
return (node, tag) => formatDevSourceAttr(node, tag, relPath, lineStarts)
|
|
87
|
+
}
|