prunelle 0.7.1 → 0.7.3
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 +5 -5
- package/package.json +1 -1
- package/src/a11y/declaration.ts +4 -0
- package/src/a11y/index.ts +11 -0
- package/src/build/bundle.ts +2 -2
- package/src/build/files.ts +9 -0
- package/src/build/first-load.ts +2 -3
- package/src/build/index.ts +6 -12
- package/src/build/island-promote.ts +69 -8
- package/src/build/output.ts +3 -6
- package/src/build/plugin-host.ts +4 -2
- package/src/build/precompress.ts +1 -1
- package/src/build/prerender.ts +7 -4
- package/src/build/reach.ts +92 -28
- package/src/build/static-headers.ts +27 -8
- package/src/cli/check-forms.ts +60 -34
- package/src/cli/check-imports.ts +9 -3
- package/src/cli/check.ts +5 -15
- package/src/cli/new.ts +1 -1
- package/src/cli/upgrade.ts +4 -8
- package/src/compiler/analyze.ts +19 -41
- package/src/compiler/cacheability-private.ts +566 -0
- package/src/compiler/cacheability.ts +21 -322
- package/src/compiler/compliance-eco.ts +4 -4
- package/src/compiler/compliance-urls.ts +5 -3
- package/src/compiler/compliance.ts +68 -17
- package/src/compiler/expr-context.ts +113 -44
- package/src/compiler/spread-handlers.ts +295 -0
- package/src/dom/extras.ts +28 -26
- package/src/dom/island.ts +49 -34
- package/src/eco/declaration.ts +87 -16
- package/src/eco/index.ts +7 -3
- package/src/plugins/config.ts +13 -8
- package/src/reactivity/store.ts +19 -7
- package/src/server/action.ts +137 -40
- package/src/server/errors.ts +3 -0
- package/src/server/form-schema.ts +6 -12
- package/src/server/index.ts +6 -6
- package/src/server/page.ts +15 -0
- package/src/server/route-cache.ts +99 -3
- package/src/server/route-table.ts +23 -13
- package/src/server/standard-schema.ts +17 -0
- package/src/ssr/context.ts +1 -1
- package/src/ssr/index.ts +4 -2
- package/src/ssr/region.ts +18 -9
|
@@ -147,20 +147,40 @@ function mapTargetArrayName(target: Node): string | undefined {
|
|
|
147
147
|
return undefined
|
|
148
148
|
}
|
|
149
149
|
|
|
150
|
+
function markAmbiguousPropObject(
|
|
151
|
+
name: string,
|
|
152
|
+
map: Map<string, Set<string>>,
|
|
153
|
+
ambiguous: Set<string>,
|
|
154
|
+
): void {
|
|
155
|
+
map.delete(name)
|
|
156
|
+
ambiguous.add(name)
|
|
157
|
+
}
|
|
158
|
+
|
|
150
159
|
function registerTypedParamKeys(
|
|
151
160
|
name: string,
|
|
152
161
|
keys: Set<string>,
|
|
153
|
-
|
|
154
|
-
|
|
162
|
+
map: Map<string, Set<string>>,
|
|
163
|
+
ambiguous: Set<string>,
|
|
155
164
|
): void {
|
|
156
|
-
if (
|
|
165
|
+
if (ambiguous.has(name))
|
|
157
166
|
return
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
167
|
+
const prev = map.get(name)
|
|
168
|
+
if (prev !== undefined) {
|
|
169
|
+
const shared = new Set<string>()
|
|
170
|
+
for (const k of prev) {
|
|
171
|
+
if (keys.has(k))
|
|
172
|
+
shared.add(k)
|
|
173
|
+
}
|
|
174
|
+
if (shared.size > 0)
|
|
175
|
+
map.set(name, shared)
|
|
176
|
+
else
|
|
177
|
+
markAmbiguousPropObject(name, map, ambiguous)
|
|
161
178
|
return
|
|
162
179
|
}
|
|
163
|
-
|
|
180
|
+
if (keys.size > 0)
|
|
181
|
+
map.set(name, keys)
|
|
182
|
+
else
|
|
183
|
+
markAmbiguousPropObject(name, map, ambiguous)
|
|
164
184
|
}
|
|
165
185
|
|
|
166
186
|
function rootIdentifierName(node: Node): string | undefined {
|
|
@@ -228,17 +248,31 @@ interface ParamTypeState {
|
|
|
228
248
|
nonPrimNames: Set<string>
|
|
229
249
|
sigCandidates: Set<string>
|
|
230
250
|
nonSigNames: Set<string>
|
|
251
|
+
propsParams: Set<string>
|
|
231
252
|
primitivePropKeys: Map<string, Set<string>>
|
|
232
253
|
ambiguousPropObjects: Set<string>
|
|
233
254
|
arrayElemPrimKeys: Map<string, Set<string>>
|
|
255
|
+
ambiguousArrayObjects: Set<string>
|
|
234
256
|
markNonPrim: (pat: Node | null | undefined) => void
|
|
235
257
|
}
|
|
236
258
|
|
|
259
|
+
function markPatternUnknownObjects(pat: Node | null | undefined, state: ParamTypeState): void {
|
|
260
|
+
if (pat === null || pat === undefined)
|
|
261
|
+
return
|
|
262
|
+
const names = new Set<string>()
|
|
263
|
+
collectPatternNames(pat, names)
|
|
264
|
+
for (const name of names) {
|
|
265
|
+
markAmbiguousPropObject(name, state.primitivePropKeys, state.ambiguousPropObjects)
|
|
266
|
+
markAmbiguousPropObject(name, state.arrayElemPrimKeys, state.ambiguousArrayObjects)
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
|
|
237
270
|
function recordObjectPatternTypes(pat: ObjectPattern, typeNode: Node | null | undefined, state: ParamTypeState): void {
|
|
238
271
|
const info = extractTypeLiteralPrimitives(typeNode)
|
|
239
272
|
for (const prop of pat.properties) {
|
|
240
273
|
if (prop.type === 'RestElement') {
|
|
241
274
|
state.markNonPrim(prop.argument)
|
|
275
|
+
markPatternUnknownObjects(prop.argument, state)
|
|
242
276
|
continue
|
|
243
277
|
}
|
|
244
278
|
const key = !prop.computed ? memberPropKey(prop.key) : undefined
|
|
@@ -247,22 +281,76 @@ function recordObjectPatternTypes(pat: ObjectPattern, typeNode: Node | null | un
|
|
|
247
281
|
if (info.prims.has(key)) {
|
|
248
282
|
state.primCandidates.add(val.name)
|
|
249
283
|
state.nonSigNames.add(val.name)
|
|
284
|
+
markPatternUnknownObjects(val, state)
|
|
250
285
|
continue
|
|
251
286
|
}
|
|
252
287
|
if (info.primSignals.has(key)) {
|
|
253
288
|
state.sigCandidates.add(val.name)
|
|
254
289
|
state.nonPrimNames.add(val.name)
|
|
290
|
+
markPatternUnknownObjects(val, state)
|
|
255
291
|
continue
|
|
256
292
|
}
|
|
257
293
|
const elemPrims = info.arrayElemPrims.get(key)
|
|
258
294
|
if (elemPrims !== undefined)
|
|
259
|
-
|
|
295
|
+
registerTypedParamKeys(val.name, elemPrims, state.arrayElemPrimKeys, state.ambiguousArrayObjects)
|
|
296
|
+
else
|
|
297
|
+
markAmbiguousPropObject(val.name, state.arrayElemPrimKeys, state.ambiguousArrayObjects)
|
|
260
298
|
const nested = info.nestedPrims.get(key)
|
|
261
299
|
if (nested !== undefined)
|
|
262
300
|
registerTypedParamKeys(val.name, nested, state.primitivePropKeys, state.ambiguousPropObjects)
|
|
301
|
+
else
|
|
302
|
+
markAmbiguousPropObject(val.name, state.primitivePropKeys, state.ambiguousPropObjects)
|
|
303
|
+
state.markNonPrim(val)
|
|
304
|
+
continue
|
|
263
305
|
}
|
|
264
306
|
state.markNonPrim(val)
|
|
307
|
+
markPatternUnknownObjects(val, state)
|
|
308
|
+
}
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
function recordParam(
|
|
312
|
+
param: ParamPattern,
|
|
313
|
+
isComponentFirstParam: boolean,
|
|
314
|
+
isMapItemParam: boolean,
|
|
315
|
+
isMapIndexParam: boolean,
|
|
316
|
+
state: ParamTypeState,
|
|
317
|
+
): void {
|
|
318
|
+
const inner = param.type === 'AssignmentPattern' ? param.left : param
|
|
319
|
+
if (inner.type === 'ObjectPattern') {
|
|
320
|
+
recordObjectPatternTypes(inner, inner.typeAnnotation?.typeAnnotation, state)
|
|
321
|
+
return
|
|
322
|
+
}
|
|
323
|
+
if (inner.type === 'Identifier') {
|
|
324
|
+
const ann = inner.typeAnnotation?.typeAnnotation
|
|
325
|
+
if (isMapIndexParam || isPrimitiveTypeNode(ann)) {
|
|
326
|
+
state.primCandidates.add(inner.name)
|
|
327
|
+
state.nonSigNames.add(inner.name)
|
|
328
|
+
markPatternUnknownObjects(inner, state)
|
|
329
|
+
return
|
|
330
|
+
}
|
|
331
|
+
if (isPrimitiveSignalTypeNode(ann)) {
|
|
332
|
+
state.sigCandidates.add(inner.name)
|
|
333
|
+
state.nonPrimNames.add(inner.name)
|
|
334
|
+
markPatternUnknownObjects(inner, state)
|
|
335
|
+
return
|
|
336
|
+
}
|
|
337
|
+
if (isComponentFirstParam)
|
|
338
|
+
state.propsParams.add(inner.name)
|
|
339
|
+
const info = extractTypeLiteralPrimitives(ann)
|
|
340
|
+
if (info !== undefined)
|
|
341
|
+
registerTypedParamKeys(inner.name, info.prims, state.primitivePropKeys, state.ambiguousPropObjects)
|
|
342
|
+
else if (!isMapItemParam)
|
|
343
|
+
markAmbiguousPropObject(inner.name, state.primitivePropKeys, state.ambiguousPropObjects)
|
|
344
|
+
const elemPrims = extractArrayElementPrimitives(ann)
|
|
345
|
+
if (elemPrims !== undefined)
|
|
346
|
+
registerTypedParamKeys(inner.name, elemPrims, state.arrayElemPrimKeys, state.ambiguousArrayObjects)
|
|
347
|
+
else
|
|
348
|
+
markAmbiguousPropObject(inner.name, state.arrayElemPrimKeys, state.ambiguousArrayObjects)
|
|
349
|
+
state.markNonPrim(inner)
|
|
350
|
+
return
|
|
265
351
|
}
|
|
352
|
+
state.markNonPrim(inner)
|
|
353
|
+
markPatternUnknownObjects(inner, state)
|
|
266
354
|
}
|
|
267
355
|
|
|
268
356
|
/**
|
|
@@ -278,6 +366,7 @@ export function createExprContext(program: Program, jsxNodes: WeakSet<Node>): Ex
|
|
|
278
366
|
const primitivePropKeys = new Map<string, Set<string>>()
|
|
279
367
|
const ambiguousPropObjects = new Set<string>()
|
|
280
368
|
const arrayElemPrimKeys = new Map<string, Set<string>>()
|
|
369
|
+
const ambiguousArrayObjects = new Set<string>()
|
|
281
370
|
const jsxVars = new Set<string>()
|
|
282
371
|
const deferredInits: { name: string, init: Node, isSignal: boolean, signalKind?: 'signal' | 'computed' }[] = []
|
|
283
372
|
|
|
@@ -293,59 +382,32 @@ export function createExprContext(program: Program, jsxNodes: WeakSet<Node>): Ex
|
|
|
293
382
|
nonPrimNames,
|
|
294
383
|
sigCandidates,
|
|
295
384
|
nonSigNames,
|
|
385
|
+
propsParams,
|
|
296
386
|
primitivePropKeys,
|
|
297
387
|
ambiguousPropObjects,
|
|
298
388
|
arrayElemPrimKeys,
|
|
389
|
+
ambiguousArrayObjects,
|
|
299
390
|
markNonPrim,
|
|
300
391
|
}
|
|
301
392
|
|
|
302
|
-
const recordParam = (param: ParamPattern, isComponentFirstParam: boolean, isMapIndexParam: boolean): void => {
|
|
303
|
-
const inner = param.type === 'AssignmentPattern' ? param.left : param
|
|
304
|
-
if (inner.type === 'ObjectPattern') {
|
|
305
|
-
recordObjectPatternTypes(inner, inner.typeAnnotation?.typeAnnotation, paramState)
|
|
306
|
-
return
|
|
307
|
-
}
|
|
308
|
-
if (inner.type === 'Identifier') {
|
|
309
|
-
const ann = inner.typeAnnotation?.typeAnnotation
|
|
310
|
-
if (isMapIndexParam || isPrimitiveTypeNode(ann)) {
|
|
311
|
-
primCandidates.add(inner.name)
|
|
312
|
-
nonSigNames.add(inner.name)
|
|
313
|
-
return
|
|
314
|
-
}
|
|
315
|
-
if (isPrimitiveSignalTypeNode(ann)) {
|
|
316
|
-
sigCandidates.add(inner.name)
|
|
317
|
-
nonPrimNames.add(inner.name)
|
|
318
|
-
return
|
|
319
|
-
}
|
|
320
|
-
if (isComponentFirstParam)
|
|
321
|
-
propsParams.add(inner.name)
|
|
322
|
-
const info = extractTypeLiteralPrimitives(ann)
|
|
323
|
-
if (info !== undefined)
|
|
324
|
-
registerTypedParamKeys(inner.name, info.prims, primitivePropKeys, ambiguousPropObjects)
|
|
325
|
-
const elemPrims = extractArrayElementPrimitives(ann)
|
|
326
|
-
if (elemPrims !== undefined)
|
|
327
|
-
arrayElemPrimKeys.set(inner.name, elemPrims)
|
|
328
|
-
}
|
|
329
|
-
markNonPrim(inner)
|
|
330
|
-
}
|
|
331
|
-
|
|
332
393
|
const mapCallbacks = new WeakSet<Node>()
|
|
333
|
-
const mapItemTargets
|
|
394
|
+
const mapItemTargets = new Map<Node, { targetName: string, paramName: string }>()
|
|
334
395
|
walk(program, (node) => {
|
|
335
396
|
const mapMatch = matchJsxMapCall(node, jsxNodes)
|
|
336
397
|
if (mapMatch !== undefined) {
|
|
337
398
|
mapCallbacks.add(mapMatch.cb)
|
|
338
399
|
const targetName = mapTargetArrayName(mapMatch.target)
|
|
339
400
|
const first = mapMatch.params[0]
|
|
340
|
-
const paramName = first?.type === 'Identifier' ? first.name : undefined
|
|
401
|
+
const paramName = first?.type === 'Identifier' && first.typeAnnotation == null ? first.name : undefined
|
|
341
402
|
if (targetName !== undefined && paramName !== undefined)
|
|
342
|
-
mapItemTargets.
|
|
403
|
+
mapItemTargets.set(mapMatch.cb, { targetName, paramName })
|
|
343
404
|
}
|
|
344
405
|
})
|
|
345
406
|
|
|
346
407
|
walk(program, (node) => {
|
|
347
408
|
if (node.type === 'VariableDeclaration') {
|
|
348
409
|
for (const d of node.declarations) {
|
|
410
|
+
markPatternUnknownObjects(d.id, paramState)
|
|
349
411
|
if (d.init !== null && containsJsx(d.init, jsxNodes))
|
|
350
412
|
collectPatternNames(d.id, jsxVars)
|
|
351
413
|
if (d.id.type === 'Identifier') {
|
|
@@ -382,19 +444,24 @@ export function createExprContext(program: Program, jsxNodes: WeakSet<Node>): Ex
|
|
|
382
444
|
}
|
|
383
445
|
if (node.type === 'ImportSpecifier' || node.type === 'ImportDefaultSpecifier' || node.type === 'ImportNamespaceSpecifier') {
|
|
384
446
|
markNonPrim(node.local)
|
|
447
|
+
markPatternUnknownObjects(node.local, paramState)
|
|
385
448
|
return
|
|
386
449
|
}
|
|
387
|
-
if (node.type === 'FunctionDeclaration' || node.type === 'ClassDeclaration')
|
|
450
|
+
if (node.type === 'FunctionDeclaration' || node.type === 'ClassDeclaration') {
|
|
388
451
|
markNonPrim(node.id)
|
|
452
|
+
markPatternUnknownObjects(node.id, paramState)
|
|
453
|
+
}
|
|
389
454
|
if (node.type === 'FunctionDeclaration' || node.type === 'FunctionExpression' || node.type === 'ArrowFunctionExpression') {
|
|
390
455
|
const isMapCb = mapCallbacks.has(node)
|
|
456
|
+
const isMapItemCb = mapItemTargets.has(node)
|
|
391
457
|
const isComponent = !isMapCb && node.body !== null && containsJsx(node.body, jsxNodes)
|
|
392
458
|
for (let i = 0; i < node.params.length; i++)
|
|
393
|
-
recordParam(node.params[i]!, isComponent && i === 0, isMapCb && i === 1)
|
|
459
|
+
recordParam(node.params[i]!, isComponent && i === 0, isMapItemCb && i === 0, isMapCb && i === 1, paramState)
|
|
394
460
|
return
|
|
395
461
|
}
|
|
396
462
|
if (node.type === 'CatchClause') {
|
|
397
463
|
markNonPrim(node.param)
|
|
464
|
+
markPatternUnknownObjects(node.param, paramState)
|
|
398
465
|
return
|
|
399
466
|
}
|
|
400
467
|
if (node.type === 'AssignmentExpression') {
|
|
@@ -419,10 +486,12 @@ export function createExprContext(program: Program, jsxNodes: WeakSet<Node>): Ex
|
|
|
419
486
|
}
|
|
420
487
|
})
|
|
421
488
|
|
|
422
|
-
for (const { targetName, paramName } of mapItemTargets) {
|
|
489
|
+
for (const { targetName, paramName } of mapItemTargets.values()) {
|
|
423
490
|
const elemPrims = arrayElemPrimKeys.get(targetName)
|
|
424
491
|
if (elemPrims !== undefined)
|
|
425
492
|
registerTypedParamKeys(paramName, elemPrims, primitivePropKeys, ambiguousPropObjects)
|
|
493
|
+
else
|
|
494
|
+
markAmbiguousPropObject(paramName, primitivePropKeys, ambiguousPropObjects)
|
|
426
495
|
}
|
|
427
496
|
|
|
428
497
|
for (const n of nonPrimNames)
|
|
@@ -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
|
+
}
|
package/src/dom/extras.ts
CHANGED
|
@@ -237,24 +237,35 @@ function clearInserted(current: Node | Node[] | null): void {
|
|
|
237
237
|
}
|
|
238
238
|
}
|
|
239
239
|
|
|
240
|
-
// Inserts the nodes and texts of a rendered array before the marker;
|
|
241
|
-
function insertAll(parent: Node, marker: Node | null, items: unknown[]): Node[] {
|
|
240
|
+
// Inserts the nodes and texts of a rendered array before the marker; nested arrays flatten and fragments contribute their children.
|
|
241
|
+
function insertAll(parent: Node, marker: Node | null, items: unknown[], ssrNodes?: Node[]): Node[] {
|
|
242
242
|
const f = document.createDocumentFragment()
|
|
243
243
|
const nodes: Node[] = []
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
244
|
+
const append = (list: unknown[]): void => {
|
|
245
|
+
for (let i = 0; i < list.length; i++) {
|
|
246
|
+
const item = list[i]
|
|
247
|
+
if (Array.isArray(item)) {
|
|
248
|
+
append(item)
|
|
249
|
+
}
|
|
250
|
+
else if (item instanceof Node) {
|
|
251
|
+
if (item.nodeType === 11)
|
|
252
|
+
nodes.push(...Array.from(item.childNodes))
|
|
253
|
+
else
|
|
254
|
+
nodes.push(item)
|
|
255
|
+
f.appendChild(item)
|
|
256
|
+
}
|
|
257
|
+
else if (item != null && typeof item !== 'boolean' && item !== '') {
|
|
258
|
+
const t = document.createTextNode(String(item))
|
|
259
|
+
nodes.push(t)
|
|
260
|
+
f.appendChild(t)
|
|
261
|
+
}
|
|
256
262
|
}
|
|
257
263
|
}
|
|
264
|
+
append(items)
|
|
265
|
+
if (ssrNodes !== undefined) {
|
|
266
|
+
const kept = new Set(nodes)
|
|
267
|
+
clearInserted(ssrNodes.filter(n => !kept.has(n)))
|
|
268
|
+
}
|
|
258
269
|
parent.insertBefore(f, marker)
|
|
259
270
|
return nodes
|
|
260
271
|
}
|
|
@@ -377,11 +388,7 @@ export function insert(parent: Node, value: () => unknown, marker: Node | null =
|
|
|
377
388
|
|
|
378
389
|
if (Array.isArray(next)) {
|
|
379
390
|
clearRendered(current)
|
|
380
|
-
|
|
381
|
-
const kept = new Set(next)
|
|
382
|
-
clearInserted(ssrNodes.filter(n => !kept.has(n)))
|
|
383
|
-
}
|
|
384
|
-
current = insertAll(host, marker, next)
|
|
391
|
+
current = insertAll(host, marker, next, ssrNodes)
|
|
385
392
|
}
|
|
386
393
|
})
|
|
387
394
|
}
|
|
@@ -566,12 +573,7 @@ export function show(
|
|
|
566
573
|
return
|
|
567
574
|
}
|
|
568
575
|
|
|
569
|
-
if (Array.isArray(rendered))
|
|
570
|
-
|
|
571
|
-
const kept = new Set(rendered)
|
|
572
|
-
clearInserted(res.nodes.filter(n => !kept.has(n)))
|
|
573
|
-
}
|
|
574
|
-
nodes = insertAll(parent, marker, rendered)
|
|
575
|
-
}
|
|
576
|
+
if (Array.isArray(rendered))
|
|
577
|
+
nodes = insertAll(parent, marker, rendered, res.nodes)
|
|
576
578
|
})
|
|
577
579
|
}
|