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
|
@@ -9,10 +9,12 @@ export const CADDY_SNIPPET = 'prunelle.caddy'
|
|
|
9
9
|
export const NGINX_SNIPPET = 'prunelle.nginx.conf'
|
|
10
10
|
|
|
11
11
|
// A page carries its own script and style policy as a `<meta>` tag with the hashes of that page, so the headers keep only the directives a `<meta>` tag cannot set or a page never varies.
|
|
12
|
-
const HEADER_CSP_DIRECTIVES = new Set(['frame-ancestors', 'base-uri', 'form-action', 'object-src'])
|
|
12
|
+
const HEADER_CSP_DIRECTIVES = new Set(['frame-ancestors', 'base-uri', 'form-action', 'object-src', 'report-uri', 'sandbox'])
|
|
13
|
+
const REDIRECT_STATUSES = new Set([301, 302, 303, 307, 308])
|
|
13
14
|
const WHITESPACE_RE = /\s+/
|
|
14
15
|
const DYNAMIC_PATH_RE = /[*:]/
|
|
15
16
|
const FORCE_MARK_RE = /!$/
|
|
17
|
+
const UNSAFE_REDIRECT_TOKEN_RE = /[\s;{}'"$\\]/
|
|
16
18
|
|
|
17
19
|
export interface StaticRedirect {
|
|
18
20
|
from: string
|
|
@@ -20,16 +22,29 @@ export interface StaticRedirect {
|
|
|
20
22
|
status: number
|
|
21
23
|
}
|
|
22
24
|
|
|
23
|
-
|
|
25
|
+
function isSafeRedirect(rule: StaticRedirect): boolean {
|
|
26
|
+
return REDIRECT_STATUSES.has(rule.status)
|
|
27
|
+
&& rule.from !== ''
|
|
28
|
+
&& rule.to !== ''
|
|
29
|
+
&& !rule.from.startsWith('#')
|
|
30
|
+
&& !rule.to.startsWith('#')
|
|
31
|
+
&& !DYNAMIC_PATH_RE.test(rule.from)
|
|
32
|
+
&& !UNSAFE_REDIRECT_TOKEN_RE.test(rule.from)
|
|
33
|
+
&& !UNSAFE_REDIRECT_TOKEN_RE.test(rule.to)
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/** The `from to [status]` lines of a `_redirects` file that a snippet can state: an exact 3xx redirect, no splat, placeholder, or config-breaking character. */
|
|
24
37
|
export function parseRedirects(text: string): StaticRedirect[] {
|
|
25
38
|
const rules: StaticRedirect[] = []
|
|
26
39
|
for (const line of text.split('\n')) {
|
|
27
40
|
const fields = line.trim().split(WHITESPACE_RE)
|
|
28
41
|
const [from, to, status = '301'] = fields
|
|
29
42
|
const code = Number(status.replace(FORCE_MARK_RE, ''))
|
|
30
|
-
if (from === undefined || to === undefined
|
|
43
|
+
if (from === undefined || to === undefined)
|
|
31
44
|
continue
|
|
32
|
-
|
|
45
|
+
const rule: StaticRedirect = { from, to, status: code }
|
|
46
|
+
if (isSafeRedirect(rule))
|
|
47
|
+
rules.push(rule)
|
|
33
48
|
}
|
|
34
49
|
return rules
|
|
35
50
|
}
|
|
@@ -67,8 +82,10 @@ export function caddySnippet(security: SecurityHeadersOptions | false | undefine
|
|
|
67
82
|
const entries = headerEntries(security)
|
|
68
83
|
if (entries.length > 0)
|
|
69
84
|
lines.push('header {', ...entries.map(([name, value]) => `\t${titleCase(name)} ${quote(value)}`), '}')
|
|
70
|
-
for (const rule of redirects)
|
|
71
|
-
|
|
85
|
+
for (const rule of redirects) {
|
|
86
|
+
if (isSafeRedirect(rule))
|
|
87
|
+
lines.push(`redir ${rule.from} ${rule.to} ${rule.status}`)
|
|
88
|
+
}
|
|
72
89
|
return `${lines.join('\n')}\n`
|
|
73
90
|
}
|
|
74
91
|
|
|
@@ -76,8 +93,10 @@ export function nginxSnippet(security: SecurityHeadersOptions | false | undefine
|
|
|
76
93
|
const lines = ['# Written by `prunelle build --static`. Inside a server block: `include /path/to/dist/prunelle.nginx.conf;`.', '# A location block that sets its own add_header drops these: repeat the include there.']
|
|
77
94
|
for (const [name, value] of headerEntries(security))
|
|
78
95
|
lines.push(`add_header ${titleCase(name)} ${quote(value)} always;`)
|
|
79
|
-
for (const rule of redirects)
|
|
80
|
-
|
|
96
|
+
for (const rule of redirects) {
|
|
97
|
+
if (isSafeRedirect(rule))
|
|
98
|
+
lines.push(`location = ${rule.from} { return ${rule.status} ${rule.to}; }`)
|
|
99
|
+
}
|
|
81
100
|
return `${lines.join('\n')}\n`
|
|
82
101
|
}
|
|
83
102
|
|
package/src/cli/check-forms.ts
CHANGED
|
@@ -330,52 +330,77 @@ function collectSelectOptions(selectEl: JSXElement, file: string): SelectOptionI
|
|
|
330
330
|
return options
|
|
331
331
|
}
|
|
332
332
|
|
|
333
|
-
function
|
|
333
|
+
function controlFormMatch(
|
|
334
|
+
attrs: readonly JSXElement['openingElement']['attributes'][number][],
|
|
335
|
+
propsScope: PropsScope,
|
|
336
|
+
formId: string | undefined,
|
|
337
|
+
hasDynamicId: boolean,
|
|
338
|
+
externalOnly: boolean,
|
|
339
|
+
): 'match' | 'skip' | 'opaque' {
|
|
340
|
+
const formAttr = findAttr(attrs, 'form')
|
|
341
|
+
if (formAttr === undefined) return externalOnly ? 'skip' : 'match'
|
|
342
|
+
const target = resolveStaticStringAttr(formAttr, propsScope)
|
|
343
|
+
if (!target.known) return 'opaque'
|
|
344
|
+
if (formId !== undefined && target.value === formId) return 'match'
|
|
345
|
+
return hasDynamicId ? 'opaque' : 'skip'
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
function recordControlField(node: JSXElement, tag: string, file: string, propsScope: PropsScope, fields: CollectedField[]): boolean {
|
|
349
|
+
const attrs = node.openingElement.attributes
|
|
350
|
+
const nameAttr = findAttr(attrs, 'name')
|
|
351
|
+
const hasSpread = attrs.some(a => a.type === 'JSXSpreadAttribute')
|
|
352
|
+
if (tag === 'button') {
|
|
353
|
+
if (nameAttr === undefined) return false
|
|
354
|
+
const typeVal = resolveStaticStringAttr(findAttr(attrs, 'type'), propsScope)
|
|
355
|
+
if (typeVal.known && (typeVal.value.toLowerCase() === 'button' || typeVal.value.toLowerCase() === 'reset')) return false
|
|
356
|
+
}
|
|
357
|
+
if (nameAttr === undefined) return hasSpread && tag !== 'button'
|
|
358
|
+
const resolvedName = resolveStaticStringAttr(nameAttr, propsScope)
|
|
359
|
+
if (!resolvedName.known) return true
|
|
360
|
+
if (resolvedName.value !== '') {
|
|
361
|
+
fields.push({
|
|
362
|
+
name: resolvedName.value,
|
|
363
|
+
tag,
|
|
364
|
+
file,
|
|
365
|
+
offset: nameAttr.start,
|
|
366
|
+
...(tag === 'select' ? { options: collectSelectOptions(node, file) } : {}),
|
|
367
|
+
})
|
|
368
|
+
}
|
|
369
|
+
return false
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
function collectFormFields(formNode: JSXElement, enclosingRoot: Node, rootFile: string, graph: ModuleGraph): { fields: CollectedField[], hasOpaqueFields: boolean } {
|
|
334
373
|
const fields: CollectedField[] = []
|
|
335
374
|
let hasOpaqueFields = false
|
|
375
|
+
const formAttrs = formNode.openingElement.attributes
|
|
376
|
+
const idAttr = findAttr(formAttrs, 'id')
|
|
377
|
+
const idVal = resolveStaticStringAttr(idAttr, EMPTY_PROPS_SCOPE)
|
|
378
|
+
const formId = idVal.known && idVal.value !== '' ? idVal.value : undefined
|
|
379
|
+
const hasDynamicId = (idAttr !== undefined && !idVal.known) || (idAttr === undefined && formAttrs.some(a => a.type === 'JSXSpreadAttribute'))
|
|
336
380
|
|
|
337
|
-
const visitTree = (target: Node | readonly Node[], file: string, propsScope: PropsScope, visited: ReadonlySet<string
|
|
381
|
+
const visitTree = (target: Node | readonly Node[], file: string, propsScope: PropsScope, visited: ReadonlySet<string>, externalOnly: boolean): void => {
|
|
338
382
|
const analysis = graph.analyses.get(file)
|
|
339
383
|
if (analysis === undefined) return
|
|
340
384
|
|
|
341
385
|
walk(target, (node) => {
|
|
342
386
|
if (node.type !== 'JSXElement') return undefined
|
|
387
|
+
if (node === formNode) return false
|
|
343
388
|
const tag = jsxTagName(node.openingElement.name)
|
|
344
|
-
if (tag === 'form') return false
|
|
389
|
+
if (!externalOnly && tag === 'form') return false
|
|
345
390
|
|
|
346
391
|
if (tag === 'input' || tag === 'select' || tag === 'textarea' || tag === 'button') {
|
|
347
|
-
const
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
if (tag === 'button') {
|
|
351
|
-
if (nameAttr === undefined) return undefined
|
|
352
|
-
const typeVal = resolveStaticStringAttr(findAttr(attrs, 'type'), propsScope)
|
|
353
|
-
if (typeVal.known && (typeVal.value.toLowerCase() === 'button' || typeVal.value.toLowerCase() === 'reset')) return undefined
|
|
354
|
-
}
|
|
355
|
-
if (nameAttr === undefined) {
|
|
356
|
-
if (hasSpread && tag !== 'button') hasOpaqueFields = true
|
|
357
|
-
return undefined
|
|
358
|
-
}
|
|
359
|
-
const resolvedName = resolveStaticStringAttr(nameAttr, propsScope)
|
|
360
|
-
if (!resolvedName.known) { hasOpaqueFields = true; return undefined }
|
|
361
|
-
if (resolvedName.value !== '') {
|
|
362
|
-
fields.push({
|
|
363
|
-
name: resolvedName.value,
|
|
364
|
-
tag,
|
|
365
|
-
file,
|
|
366
|
-
offset: nameAttr.start,
|
|
367
|
-
...(tag === 'select' ? { options: collectSelectOptions(node, file) } : {}),
|
|
368
|
-
})
|
|
369
|
-
}
|
|
392
|
+
const match = controlFormMatch(node.openingElement.attributes, propsScope, formId, hasDynamicId, externalOnly)
|
|
393
|
+
if (match === 'opaque') hasOpaqueFields = true
|
|
394
|
+
else if (match === 'match' && recordControlField(node, tag, file, propsScope, fields)) hasOpaqueFields = true
|
|
370
395
|
return tag === 'select' ? false : undefined
|
|
371
396
|
}
|
|
372
397
|
|
|
373
398
|
if (isComponentTag(tag)) {
|
|
374
|
-
if (node.children.length > 0) visitTree(node.children, file, propsScope, visited)
|
|
399
|
+
if (node.children.length > 0) visitTree(node.children, file, propsScope, visited, externalOnly)
|
|
375
400
|
if (resolvePrunelleTag(tag, analysis.imports) !== undefined) return false
|
|
376
401
|
const resolved = resolveComponentTarget(tag, file, analysis, graph)
|
|
377
402
|
if (resolved === undefined) {
|
|
378
|
-
if (!isKnownNonInputExternal(tag, node.openingElement.attributes, analysis)) hasOpaqueFields = true
|
|
403
|
+
if (!externalOnly && !isKnownNonInputExternal(tag, node.openingElement.attributes, analysis)) hasOpaqueFields = true
|
|
379
404
|
return false
|
|
380
405
|
}
|
|
381
406
|
if (visited.has(resolved.key)) return false
|
|
@@ -387,7 +412,7 @@ function collectFormFields(formNode: JSXElement, rootFile: string, graph: Module
|
|
|
387
412
|
}
|
|
388
413
|
const nextVisited = new Set(visited)
|
|
389
414
|
nextVisited.add(resolved.key)
|
|
390
|
-
visitTree(resolved.node, resolved.file, extractPropsScope(resolved.node, nextBindings), nextVisited)
|
|
415
|
+
visitTree(resolved.node, resolved.file, extractPropsScope(resolved.node, nextBindings), nextVisited, externalOnly)
|
|
391
416
|
return false
|
|
392
417
|
}
|
|
393
418
|
|
|
@@ -395,7 +420,9 @@ function collectFormFields(formNode: JSXElement, rootFile: string, graph: Module
|
|
|
395
420
|
})
|
|
396
421
|
}
|
|
397
422
|
|
|
398
|
-
visitTree(formNode.children, rootFile, EMPTY_PROPS_SCOPE, new Set())
|
|
423
|
+
visitTree(formNode.children, rootFile, EMPTY_PROPS_SCOPE, new Set(), false)
|
|
424
|
+
if (formId !== undefined || hasDynamicId)
|
|
425
|
+
visitTree(enclosingRoot, rootFile, EMPTY_PROPS_SCOPE, new Set(), true)
|
|
399
426
|
return { fields, hasOpaqueFields }
|
|
400
427
|
}
|
|
401
428
|
|
|
@@ -409,7 +436,8 @@ function collectModuleForms(file: string, analysis: ModuleAnalysis, graph: Modul
|
|
|
409
436
|
if (actionAttr?.value?.type !== 'JSXExpressionContainer' || actionAttr.value.expression.type === 'JSXEmptyExpression') return undefined
|
|
410
437
|
const actionRef = resolver.resolveExpr(actionAttr.value.expression)
|
|
411
438
|
if (actionRef === undefined) return undefined
|
|
412
|
-
const
|
|
439
|
+
const enclosingRoot = analysis.program.body.find(stmt => node.start >= stmt.start && node.end <= stmt.end) ?? analysis.program
|
|
440
|
+
const { fields, hasOpaqueFields } = collectFormFields(node, enclosingRoot, file, graph)
|
|
413
441
|
forms.push({ action: actionRef, file, offset: node.start, fields, hasOpaqueFields })
|
|
414
442
|
return undefined
|
|
415
443
|
})
|
|
@@ -588,10 +616,8 @@ export async function checkAppForms(input: FormCheckInput): Promise<FormCheckRes
|
|
|
588
616
|
for (const form of forms) {
|
|
589
617
|
const loaded = await loadActionModule(form.action.file, moduleCache)
|
|
590
618
|
if ('failure' in loaded) {
|
|
591
|
-
if (!unimportable.has(form.action.file)) {
|
|
619
|
+
if (!unimportable.has(form.action.file) && warn('form/unchecked', form.file, form.offset, `${relPosix(root, form.action.file)} fails to import (${loaded.failure}), so prunelle check cannot read its actions: the forms posting to them go unchecked for their fields and protection. Move what fails into the action's handler, or make it import without the running app.`))
|
|
592
620
|
unimportable.add(form.action.file)
|
|
593
|
-
warn('form/unchecked', form.file, form.offset, `${relPosix(root, form.action.file)} fails to import (${loaded.failure}), so prunelle check cannot read its actions: the forms posting to them go unchecked for their fields and protection. Move what fails into the action's handler, or make it import without the running app.`)
|
|
594
|
-
}
|
|
595
621
|
continue
|
|
596
622
|
}
|
|
597
623
|
const act = loaded.mod[form.action.exportName]
|
package/src/cli/check-imports.ts
CHANGED
|
@@ -1,12 +1,18 @@
|
|
|
1
1
|
// Finds an import no file or package answers, so check names it before build stops on it.
|
|
2
2
|
|
|
3
3
|
import { dirname } from 'node:path'
|
|
4
|
+
import { assertNoForeignImport } from '../compiler/foreign-imports'
|
|
5
|
+
import { collectMistakes } from '../compiler/mistakes'
|
|
4
6
|
|
|
5
|
-
const SKIPPED = /^(?:prunelle(?:\/|$)
|
|
7
|
+
const SKIPPED = /^(?:prunelle(?:\/|$)|@kayjs\/|~icons\/|virtual:)/
|
|
8
|
+
|
|
9
|
+
function isRefusedForeignImport(file: string, source: string, specifiers: readonly { imported: string }[]): boolean {
|
|
10
|
+
return specifiers.some(s => collectMistakes(() => assertNoForeignImport(source, s.imported, file)).error !== undefined)
|
|
11
|
+
}
|
|
6
12
|
|
|
7
13
|
/** The problem with an import specifier Bun's resolver finds nothing for, or undefined when it resolves or prunelle resolves it itself. */
|
|
8
|
-
export function unresolvedImport(file: string, rel: string, source: string): string | undefined {
|
|
9
|
-
if (SKIPPED.test(source))
|
|
14
|
+
export function unresolvedImport(file: string, rel: string, source: string, specifiers: readonly { imported: string }[] = []): string | undefined {
|
|
15
|
+
if (SKIPPED.test(source) || isRefusedForeignImport(file, source, specifiers))
|
|
10
16
|
return undefined
|
|
11
17
|
try {
|
|
12
18
|
Bun.resolveSync(source, dirname(file))
|
package/src/cli/check.ts
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
// `prunelle check [dir]`: validates routes, Prunelle compiler & island rules (unified with `prunelle build`), config, and TypeScript types.
|
|
2
2
|
|
|
3
|
-
import type { IslandValueUse } from '../build/island-promote'
|
|
4
3
|
import type { IslandIssue } from '../build/islands'
|
|
5
4
|
import type { ModuleAnalysis } from '../compiler/analyze'
|
|
6
5
|
import type { ComplianceConfig } from '../compiler/compliance'
|
|
@@ -9,13 +8,14 @@ import { existsSync, readFileSync } from 'node:fs'
|
|
|
9
8
|
import { basename, join, relative, resolve } from 'node:path'
|
|
10
9
|
import { isInNodeModules, isNotSourceDir, isTsFile, relPosix, walkFiles, walkRoutes } from '../build/files'
|
|
11
10
|
import { ModuleGraph } from '../build/graph'
|
|
11
|
+
import { formatIslandValueWarnings } from '../build/island-promote'
|
|
12
12
|
import { nodeEnvWarning, renderProblems, serverNodeEnvReads } from '../build/render-rules'
|
|
13
13
|
import { analyzeIslands } from '../build/islands'
|
|
14
14
|
import { ancestorFiles, discoverRouteMatchers, fileToRoutePath, hasRouteHandlerExport, missingPathsProblem, normalizeRoutePattern, routeExportProblemLocation, routeFileProblem, SPECIAL_ROUTE_FILES } from '../build/routes'
|
|
15
15
|
import { findUnknownStatusPages } from '../build/status-pages'
|
|
16
16
|
import { analyzeModule } from '../compiler/analyze'
|
|
17
17
|
import { findCachedRoutePrivateReads } from '../compiler/cacheability'
|
|
18
|
-
import { collectProjectComplianceWarnings,
|
|
18
|
+
import { collectProjectComplianceWarnings, pluginSetsLang, shouldEmitWarning } from '../compiler/compliance'
|
|
19
19
|
import { collectMistakes, PrunelleMistake, serverOnlyTagError } from '../compiler/mistakes'
|
|
20
20
|
import { DIAGNOSTIC_PREFIX_RE, formatDiagnostic, formatLoc } from '../compiler/shared'
|
|
21
21
|
import { createAppContext, loadConfig, resolvePlugins, runWithAppContext } from '../plugins/index'
|
|
@@ -145,16 +145,6 @@ function loadCheckedModule(
|
|
|
145
145
|
return undefined
|
|
146
146
|
}
|
|
147
147
|
|
|
148
|
-
// An imported island read as a value hydrates what holds it whole: a warning, as a table of islands may mean it.
|
|
149
|
-
function islandValueWarnings(uses: readonly IslandValueUse[], root: string, sources: Map<string, string>, compliance: ComplianceConfig | undefined): string[] {
|
|
150
|
-
const rule = 'rgesn/island-by-value'
|
|
151
|
-
if (isRuleIgnored(rule, compliance))
|
|
152
|
-
return []
|
|
153
|
-
return uses
|
|
154
|
-
.filter(use => !isIgnoredAt(parsePrunelleIgnores(sourceOf(use.file, sources)), rule, use.offset))
|
|
155
|
-
.map(use => formatDiagnostic(locOf(root, use.file, sources, use.offset), `[${rule}] ${use.name} is an island read as a value, not rendered as <${use.name} />, so what holds it hydrates as an island and ships its code too: render <${use.name} /> itself where it shows, or // prunelle-ignore ${rule} when the whole component should hydrate.`, 'warning'))
|
|
156
|
-
}
|
|
157
|
-
|
|
158
148
|
function formatIslandIssue(issue: IslandIssue, root: string, sources: Map<string, string>): string {
|
|
159
149
|
const rel = relPosix(root, issue.file)
|
|
160
150
|
switch (issue.kind) {
|
|
@@ -325,7 +315,7 @@ export async function checkProject(rootDir: string, options: { skipTypecheck?: b
|
|
|
325
315
|
})
|
|
326
316
|
for (const issue of islands.issues)
|
|
327
317
|
errors.push(formatIslandIssue(issue, root, sources))
|
|
328
|
-
warnings.push(...
|
|
318
|
+
warnings.push(...formatIslandValueWarnings(islands.valueUses, root, graph, f => sourceOf(f, sources), compliance))
|
|
329
319
|
for (const { file, warning } of islands.promotedWarnings) {
|
|
330
320
|
if (shouldEmitWarning(warning, compliance, site))
|
|
331
321
|
warnings.push(formatDiagnostic(locOf(root, file, sources, warning.start), warning.message.replaceAll(file, relPosix(root, file)), 'warning'))
|
|
@@ -338,7 +328,7 @@ export async function checkProject(rootDir: string, options: { skipTypecheck?: b
|
|
|
338
328
|
continue
|
|
339
329
|
const rel = relPosix(root, file)
|
|
340
330
|
for (const imp of analysis.imports) {
|
|
341
|
-
const unresolved = unresolvedImport(file, rel, imp.source)
|
|
331
|
+
const unresolved = unresolvedImport(file, rel, imp.source, imp.specifiers)
|
|
342
332
|
if (unresolved !== undefined)
|
|
343
333
|
errors.push(formatDiagnostic(locOf(root, file, sources, imp.start), unresolved))
|
|
344
334
|
if (imp.source === '@prunellejs/auth') {
|
|
@@ -395,7 +385,7 @@ export async function checkProject(rootDir: string, options: { skipTypecheck?: b
|
|
|
395
385
|
}
|
|
396
386
|
|
|
397
387
|
// 3d. French web law compliance checks (CSS third-party URLs, LCEN legal notice, RGAA accessibility statement)
|
|
398
|
-
warnings.push(...collectProjectComplianceWarnings({ root, srcDir, routesDir, routeFiles, analyses, compliance, consentMaxAge: loadedConfig.consent?.maxAge, site, lang: loadedConfig.lang, langFromPlugin: pluginSetsLang(plugins) }))
|
|
388
|
+
warnings.push(...collectProjectComplianceWarnings({ root, srcDir, routesDir, publicDir, routeFiles, analyses, compliance, consentMaxAge: loadedConfig.consent?.maxAge, site, lang: loadedConfig.lang, langFromPlugin: pluginSetsLang(plugins) }))
|
|
399
389
|
|
|
400
390
|
// 3d. Form field / schema checks & public action protection warnings (#4, #5)
|
|
401
391
|
const formCheck = await checkAppForms({ root, routesDir, routeFiles, graph, sources, compliance })
|
package/src/cli/new.ts
CHANGED
|
@@ -47,6 +47,6 @@ export function scaffoldProject(targetDir: string, options: ScaffoldOptions = {}
|
|
|
47
47
|
// Drafted rather than copied, so it is dated on the day the app is made and never drifts from what `prunelle a11y --draft` writes.
|
|
48
48
|
if (existsSync(join(root, DEFAULT_DECLARATION_PATH)))
|
|
49
49
|
overwritten.push(DEFAULT_DECLARATION_PATH)
|
|
50
|
-
written.push(draftAccessibilityStatement({ root }).relativePath)
|
|
50
|
+
written.push(draftAccessibilityStatement({ root, force: options.force }).relativePath)
|
|
51
51
|
return { root, files: written, overwritten }
|
|
52
52
|
}
|
package/src/cli/upgrade.ts
CHANGED
|
@@ -36,10 +36,8 @@ const WIRE_RULES: [RegExp, string][] = [
|
|
|
36
36
|
[/\bkay:(consent|navigate|region)\b/g, 'pru:$1'],
|
|
37
37
|
[/\bkay_(session|consent)\b/g, 'pru_$1'],
|
|
38
38
|
]
|
|
39
|
-
// The kay CLI
|
|
40
|
-
const
|
|
41
|
-
// The kay CLI where a build or CI file runs it, after a runner or before one of its commands, so a user or a runner label named kay stays (#142).
|
|
42
|
-
const KAY_COMMAND = String.raw`(?:new|check|inspect|docs|render|mcp|dev|build|eco|start|upgrade|test|[a-z]+-[a-z-]+)(?![\w:-])`
|
|
39
|
+
// The kay CLI where a script, build, CI or agent file runs it, after a runner or before one of its commands, so a user, label or prose named kay stays (#142, #165).
|
|
40
|
+
const KAY_COMMAND = String.raw`(?:new|check|inspect|docs|render|mcp|dev|build|eco|a11y|start|upgrade|test|[a-z]+-[a-z-]+)(?![\w:-])`
|
|
43
41
|
const RUN_CLI_RULES: [RegExp, string][] = [
|
|
44
42
|
[/(?<=\b(?:bunx|npx|bun(?:\s+(?:x|run))?)\s+|"bunx",\s*")kay(?![\w.:/-])/g, 'prunelle'],
|
|
45
43
|
[new RegExp(String.raw`(?<![\w.@/-])kay(?=(?:\s+|",\s*")${KAY_COMMAND})`, 'g'), 'prunelle'],
|
|
@@ -99,10 +97,8 @@ function upgradeDir(root: string, inheritedAliases?: ReadonlyMap<string, string>
|
|
|
99
97
|
write(file, text, upgradeGitignore(text, isApp))
|
|
100
98
|
else if (CONFIG_FILE_RE.test(file))
|
|
101
99
|
write(file, text, apply(text, PATH_RULES))
|
|
102
|
-
else if (BUILD_FILE_RE.test(file))
|
|
100
|
+
else if (BUILD_FILE_RE.test(file) || AGENT_FILE_RE.test(file))
|
|
103
101
|
write(file, text, apply(apply(text, PATH_RULES), RUN_CLI_RULES))
|
|
104
|
-
else if (AGENT_FILE_RE.test(file))
|
|
105
|
-
write(file, text, apply(apply(text, PATH_RULES), [CLI_RULE]))
|
|
106
102
|
}
|
|
107
103
|
for (const dir of CI_DIRS) {
|
|
108
104
|
if (!existsSync(join(root, dir)))
|
|
@@ -318,7 +314,7 @@ function upgradePackageJson(text: string): string {
|
|
|
318
314
|
}
|
|
319
315
|
const scripts = json.scripts as Record<string, string> | undefined
|
|
320
316
|
if (scripts !== undefined)
|
|
321
|
-
json.scripts = Object.fromEntries(Object.entries(scripts).map(([name, cmd]) => [name, apply(apply(cmd, PATH_RULES),
|
|
317
|
+
json.scripts = Object.fromEntries(Object.entries(scripts).map(([name, cmd]) => [name, apply(apply(cmd, PATH_RULES), RUN_CLI_RULES)]))
|
|
322
318
|
const after = JSON.stringify(json, null, indent)
|
|
323
319
|
return after === before ? text : `${after}\n`
|
|
324
320
|
}
|
package/src/compiler/analyze.ts
CHANGED
|
@@ -19,6 +19,8 @@ import { walkScoped } from './scope'
|
|
|
19
19
|
import { calleeName, createExprContext, decodeHtmlEntities, EVENT_ATTR, formatCodeFrame, formatDiagnostic, formatLoc, isComponentTag, isJsxNode, jsxTagName, resolvePrunelleTag, unwrapTsExpr } from './shared'
|
|
20
20
|
import { checkBodyPropsDestructure } from './props-body'
|
|
21
21
|
import { checkBareSignals } from './signal-reads'
|
|
22
|
+
import type { BindingUnit } from './spread-handlers'
|
|
23
|
+
import { bindingUnits, checkStaticSpreadHandlers, collectUnitInits, findSpreadInteractiveAttr } from './spread-handlers'
|
|
22
24
|
|
|
23
25
|
const REACTIVE_IMPORTS = new Set(['signal', 'computed', 'effect', 'onMount', 'onCleanup', 'selector', 'store', 'useAction'])
|
|
24
26
|
// Primitives whose result is reactive state another module can import and read.
|
|
@@ -392,42 +394,6 @@ function scanExportedDeclaration(scan: DeclarationScan, decl: NonNullable<Export
|
|
|
392
394
|
}
|
|
393
395
|
}
|
|
394
396
|
|
|
395
|
-
// A module's body as top-level bindings, each with the node holding its code; module-level statements go under ''.
|
|
396
|
-
function bindingUnits(program: Program): { name: string, node: Node }[] {
|
|
397
|
-
const units: { name: string, node: Node }[] = []
|
|
398
|
-
const add = (node: Node): void => {
|
|
399
|
-
if (node.type.startsWith('TS'))
|
|
400
|
-
return
|
|
401
|
-
if ((node.type === 'FunctionDeclaration' || node.type === 'ClassDeclaration') && node.id !== null) {
|
|
402
|
-
units.push({ name: node.id.name, node })
|
|
403
|
-
}
|
|
404
|
-
else if (node.type === 'VariableDeclaration') {
|
|
405
|
-
for (const d of node.declarations)
|
|
406
|
-
units.push({ name: d.id.type === 'Identifier' ? d.id.name : '', node: d })
|
|
407
|
-
}
|
|
408
|
-
else {
|
|
409
|
-
units.push({ name: '', node })
|
|
410
|
-
}
|
|
411
|
-
}
|
|
412
|
-
for (const stmt of program.body) {
|
|
413
|
-
if (stmt.type === 'ExportNamedDeclaration') {
|
|
414
|
-
if (stmt.declaration !== null)
|
|
415
|
-
add(stmt.declaration)
|
|
416
|
-
}
|
|
417
|
-
else if (stmt.type === 'ExportDefaultDeclaration') {
|
|
418
|
-
const decl = unwrapTsExpr(stmt.declaration)
|
|
419
|
-
if ((decl.type === 'FunctionDeclaration' || decl.type === 'ClassDeclaration') && decl.id !== null)
|
|
420
|
-
add(decl)
|
|
421
|
-
else if (decl.type !== 'Identifier')
|
|
422
|
-
units.push({ name: 'default', node: decl })
|
|
423
|
-
}
|
|
424
|
-
else if (stmt.type !== 'ImportDeclaration') {
|
|
425
|
-
add(stmt)
|
|
426
|
-
}
|
|
427
|
-
}
|
|
428
|
-
return units
|
|
429
|
-
}
|
|
430
|
-
|
|
431
397
|
/** What the walk over a module's body finds. */
|
|
432
398
|
interface BodyScan {
|
|
433
399
|
hasJsx: boolean
|
|
@@ -437,14 +403,15 @@ interface BodyScan {
|
|
|
437
403
|
jsxTags: Set<string>
|
|
438
404
|
dynamicImports: { source: string, start: number }[]
|
|
439
405
|
bindings: BindingFacts[]
|
|
406
|
+
units: BindingUnit[]
|
|
440
407
|
}
|
|
441
408
|
|
|
442
409
|
const TS_EXPR_WRAPPERS = new Set(['TSAsExpression', 'TSNonNullExpression', 'TSSatisfiesExpression', 'TSTypeAssertion', 'TSInstantiationExpression'])
|
|
443
410
|
|
|
444
411
|
// Walks each top-level binding for JSX, mistakes, action() placement and what makes it run in the browser.
|
|
445
412
|
function scanBody(program: Program, filename: string, code: string, imports: ImportScan, decls: DeclarationScan, warnings: CompilerWarning[], loadCode: ReadonlySet<Node>): BodyScan {
|
|
446
|
-
const body: BodyScan = { hasJsx: false, usesRenderContext: imports.usesRenderContext, deferredIslandTags: new Set(), hydrateModes: new Set(), jsxTags: new Set(), dynamicImports: [], bindings: [] }
|
|
447
413
|
const units = bindingUnits(program)
|
|
414
|
+
const body: BodyScan = { hasJsx: false, usesRenderContext: imports.usesRenderContext, deferredIslandTags: new Set(), hydrateModes: new Set(), jsxTags: new Set(), dynamicImports: [], bindings: [], units }
|
|
448
415
|
const tracked = new Set(units.map(u => u.name))
|
|
449
416
|
for (const imp of imports.imports) {
|
|
450
417
|
for (const spec of imp.specifiers)
|
|
@@ -481,6 +448,7 @@ function scanBody(program: Program, filename: string, code: string, imports: Imp
|
|
|
481
448
|
},
|
|
482
449
|
})
|
|
483
450
|
const isLoadUnit = loadCode.has(unit.node) || (unit.node.type === 'VariableDeclarator' && unit.node.init !== null && loadCode.has(unit.node.init))
|
|
451
|
+
const unitInits = collectUnitInits(unit.node, decls.localInits)
|
|
484
452
|
walk(unit.node, (node): boolean | void => {
|
|
485
453
|
if (node.type.startsWith('TS') && !TS_EXPR_WRAPPERS.has(node.type))
|
|
486
454
|
return false
|
|
@@ -497,7 +465,7 @@ function scanBody(program: Program, filename: string, code: string, imports: Imp
|
|
|
497
465
|
body.dynamicImports.push({ source: src, start: node.start })
|
|
498
466
|
}
|
|
499
467
|
else if (node.type === 'JSXOpeningElement') {
|
|
500
|
-
scanOpeningElement(node, binding, body, imports, filename)
|
|
468
|
+
scanOpeningElement(node, binding, body, imports, unitInits, filename)
|
|
501
469
|
const tag = jsxTagName(node.name)
|
|
502
470
|
if (isComponentTag(tag))
|
|
503
471
|
ref(binding, tag.split('.')[0]!, node.start)
|
|
@@ -516,7 +484,7 @@ function scanBody(program: Program, filename: string, code: string, imports: Imp
|
|
|
516
484
|
return body
|
|
517
485
|
}
|
|
518
486
|
|
|
519
|
-
function scanOpeningElement(node: Extract<Node, { type: 'JSXOpeningElement' }>, binding: BindingFacts, body: BodyScan, imports: ImportScan, filename: string): void {
|
|
487
|
+
function scanOpeningElement(node: Extract<Node, { type: 'JSXOpeningElement' }>, binding: BindingFacts, body: BodyScan, imports: ImportScan, unitInits: ReadonlyMap<string, Node>, filename: string): void {
|
|
520
488
|
const tag = jsxTagName(node.name)
|
|
521
489
|
const isComponent = isComponentTag(tag)
|
|
522
490
|
if (isComponent) {
|
|
@@ -531,7 +499,15 @@ function scanOpeningElement(node: Extract<Node, { type: 'JSXOpeningElement' }>,
|
|
|
531
499
|
}
|
|
532
500
|
}
|
|
533
501
|
for (const attr of node.attributes) {
|
|
534
|
-
if (attr.type
|
|
502
|
+
if (attr.type === 'JSXSpreadAttribute') {
|
|
503
|
+
if (!isComponent && tag !== '') {
|
|
504
|
+
const found = findSpreadInteractiveAttr(attr.argument, unitInits)
|
|
505
|
+
if (found !== undefined)
|
|
506
|
+
binding.fact ??= { reason: `uses ${found}`, offset: attr.start }
|
|
507
|
+
}
|
|
508
|
+
continue
|
|
509
|
+
}
|
|
510
|
+
if (attr.name.type !== 'JSXIdentifier')
|
|
535
511
|
continue
|
|
536
512
|
const attrName = attr.name.name
|
|
537
513
|
if (isComponent && attrName === '$hydrate') {
|
|
@@ -624,8 +600,10 @@ export function analyzeModule(filename: string, code: string, program: Program =
|
|
|
624
600
|
}
|
|
625
601
|
const hasAsyncDefault = decls.hasAsyncDefault || (decls.defaultLocal !== undefined && decls.asyncLocals.has(decls.defaultLocal))
|
|
626
602
|
const interactivity = resolveInteractivity(body.bindings, decls.exportLocals)
|
|
627
|
-
if (body.hasJsx)
|
|
603
|
+
if (body.hasJsx) {
|
|
628
604
|
checkBodyPropsDestructure(program, code, interactivity.browserSpans, jsxNodes, filename, warnings)
|
|
605
|
+
checkStaticSpreadHandlers(body.units, decls.localInits, interactivity, filename, warnings)
|
|
606
|
+
}
|
|
629
607
|
const page = interactivity.exports.get('default')
|
|
630
608
|
if (hasAsyncDefault && page?.interactive === true)
|
|
631
609
|
guarded(page.offset ?? 0, () => { throw asyncInteractiveError(filename, page.reason, page.offset) })
|