prunelle 0.7.7 → 0.9.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/llms.txt +63 -46
- package/package.json +7 -2
- package/src/a11y/declaration.ts +6 -10
- package/src/a11y/index.ts +69 -125
- package/src/animate.ts +127 -0
- package/src/build/asides.ts +51 -0
- package/src/build/bundle.ts +88 -134
- package/src/build/client-entries.ts +6 -6
- package/src/build/client-outputs.ts +75 -0
- package/src/build/client-plugin.ts +9 -4
- package/src/build/ecoindex.ts +4 -2
- package/src/build/files.ts +18 -0
- package/src/build/graph.ts +15 -7
- package/src/build/index.ts +20 -10
- package/src/build/island-promote.ts +1 -1
- package/src/build/islands.ts +4 -1
- package/src/build/key-types.ts +31 -0
- package/src/build/manifest.ts +19 -1
- package/src/build/plugin-host.ts +2 -0
- package/src/build/prerender.ts +2 -2
- package/src/build/reach.ts +14 -1
- package/src/build/report.ts +1 -1
- package/src/build/route-types.ts +16 -0
- package/src/build/routes.ts +24 -15
- package/src/build/seed-ids.ts +65 -0
- package/src/build/seed-writes.ts +111 -0
- package/src/build/static-headers.ts +2 -2
- package/src/build/static.ts +1 -1
- package/src/cli/check-cache.ts +51 -0
- package/src/cli/check-consent.ts +95 -0
- package/src/cli/check-forms.ts +2 -2
- package/src/cli/check-links-patterns.ts +101 -0
- package/src/cli/check-links.ts +16 -100
- package/src/cli/check-regions.ts +185 -0
- package/src/cli/check-security.ts +57 -6
- package/src/cli/check.ts +40 -17
- package/src/cli/commands.ts +134 -14
- package/src/cli/docs.ts +21 -2
- package/src/cli/index.ts +1 -1
- package/src/cli/inspect.ts +2 -0
- package/src/cli/mcp-tools.ts +2 -2
- package/src/cli/new.ts +9 -5
- package/src/cli/upgrade-names.ts +14 -2
- package/src/cli/upgrade.ts +3 -0
- package/src/compiler/analyze.ts +18 -4
- package/src/compiler/cacheability.ts +24 -2
- package/src/compiler/compliance-contrast.ts +284 -0
- package/src/compiler/compliance-empty.ts +24 -0
- package/src/compiler/compliance-rgaa-content.ts +95 -0
- package/src/compiler/compliance-rgaa-criteria.ts +45 -0
- package/src/compiler/compliance-rgaa-pages.ts +140 -0
- package/src/compiler/compliance-rgaa.ts +19 -0
- package/src/compiler/compliance-third-party.ts +59 -0
- package/src/compiler/compliance-urls.ts +52 -0
- package/src/compiler/compliance.ts +72 -42
- package/src/compiler/cookie-inventory.ts +119 -0
- package/src/compiler/dom-attrs.ts +4 -1
- package/src/compiler/emit.ts +1 -1
- package/src/compiler/foreign-imports.ts +5 -3
- package/src/compiler/mistakes.ts +18 -9
- package/src/compiler/props-pattern.ts +211 -0
- package/src/compiler/props.ts +177 -346
- package/src/compiler/shared.ts +9 -16
- package/src/compiler/ssr-emit.ts +510 -0
- package/src/compiler/ssr-helpers.ts +170 -121
- package/src/compiler/ssr.ts +25 -440
- package/src/context.ts +14 -1
- package/src/dev/child.ts +1 -1
- package/src/dev/hot-islands.ts +71 -0
- package/src/dev/index.ts +31 -18
- package/src/dev/protocol.ts +1 -1
- package/src/dev/queue.ts +3 -2
- package/src/dev/reload.ts +1 -1
- package/src/dev/supervisor.ts +3 -1
- package/src/dev/watchers.ts +6 -2
- package/src/dom/action.ts +97 -14
- package/src/dom/attrs.ts +8 -0
- package/src/dom/extras.ts +2 -1
- package/src/dom/field-marks.ts +24 -0
- package/src/dom/hot.ts +9 -0
- package/src/dom/index.ts +25 -5
- package/src/dom/island-hot.ts +29 -0
- package/src/dom/island.ts +13 -2
- package/src/dom/prefetch.ts +132 -0
- package/src/dom/regions.ts +498 -0
- package/src/dom/swap.ts +245 -0
- package/src/dom/trusted.ts +22 -0
- package/src/dom/upload.ts +44 -0
- package/src/eco/declaration.ts +39 -40
- package/src/eco/index.ts +5 -5
- package/src/href.ts +31 -0
- package/src/index.ts +9 -3
- package/src/plugins/config.ts +1 -1
- package/src/plugins/context.ts +7 -1
- package/src/plugins/daily.ts +27 -0
- package/src/plugins/index.ts +3 -3
- package/src/plugins/order.ts +1 -1
- package/src/plugins/public.ts +8 -5
- package/src/plugins/services.ts +88 -14
- package/src/plugins/types.ts +121 -10
- package/src/reactivity/hmr.ts +90 -0
- package/src/reactivity/index.ts +38 -18
- package/src/reactivity/resource.ts +115 -0
- package/src/reactivity/seed.ts +39 -0
- package/src/reactivity/server-seed.ts +194 -0
- package/src/reactivity/store.ts +55 -1
- package/src/rgpd/draft.ts +171 -0
- package/src/rgpd/forms.ts +85 -0
- package/src/rgpd/index.ts +57 -0
- package/src/server/action-flash.ts +1 -0
- package/src/server/action-registry.ts +6 -8
- package/src/server/action.ts +51 -19
- package/src/server/app-secret.ts +22 -17
- package/src/server/cache-invalidation.ts +96 -0
- package/src/server/compress.ts +3 -1
- package/src/server/consent.ts +16 -2
- package/src/server/cookie-inventory.ts +78 -0
- package/src/server/cookies.ts +50 -0
- package/src/server/csp-report.ts +86 -0
- package/src/server/discard.ts +13 -0
- package/src/server/document.ts +7 -4
- package/src/server/env.ts +1 -1
- package/src/server/errors.ts +9 -2
- package/src/server/field-errors.ts +91 -0
- package/src/server/form-schema.ts +16 -11
- package/src/server/health.ts +35 -14
- package/src/server/http.ts +25 -10
- package/src/server/index.ts +102 -299
- package/src/server/loader.ts +1 -0
- package/src/server/page.ts +11 -24
- package/src/server/plugin-host.ts +26 -8
- package/src/server/public.ts +14 -3
- package/src/server/rate-limit.ts +39 -10
- package/src/server/region.ts +3 -0
- package/src/server/request-handler.ts +370 -0
- package/src/server/request-log.ts +94 -0
- package/src/server/route-cache.ts +41 -6
- package/src/server/route-input.ts +54 -0
- package/src/server/route-table.ts +35 -19
- package/src/server/routing.ts +12 -11
- package/src/server/sanitize-html.ts +1 -1
- package/src/server/security.ts +81 -20
- package/src/server/serve-handlers.ts +9 -5
- package/src/server/shared-store.ts +314 -0
- package/src/server/start.ts +12 -5
- package/src/server/static-dir.ts +14 -10
- package/src/server/stream.ts +2 -1
- package/src/server/surface.ts +39 -0
- package/src/server/webhook.ts +48 -0
- package/src/server/workers.ts +168 -0
- package/src/ssr/consent-banner.ts +20 -16
- package/src/ssr/consent-texts.ts +40 -0
- package/src/ssr/context.ts +26 -3
- package/src/ssr/cookie-table.ts +75 -0
- package/src/ssr/index.ts +23 -13
- package/src/ssr/navigation.ts +161 -14
- package/src/test/browser.ts +50 -22
- package/src/test/build-reuse.ts +70 -0
- package/src/test/index.ts +45 -6
- package/src/test/mount.ts +53 -0
- package/src/test/outside.ts +94 -0
- package/templates/_shared/AGENTS.md +1 -1
- package/templates/_shared/Dockerfile +22 -0
- package/templates/_shared/dockerignore +9 -0
- package/templates/_shared/gitignore +2 -0
- package/templates/app/.env.example +3 -1
- package/templates/app/compose.yaml +32 -0
- package/templates/app/db/migrations/001_notes.sql +7 -0
- package/templates/app/db/schema.ts +11 -0
- package/templates/app/package.json +1 -0
- package/templates/app/prunelle.config.ts +6 -0
- package/templates/app/src/actions/notes.ts +26 -0
- package/templates/app/src/auth.ts +4 -2
- package/templates/app/src/db.ts +5 -0
- package/templates/app/src/routes/dashboard/index.tsx +26 -1
- package/templates/app/src/routes/privacy.tsx +6 -2
- package/templates/app/tests/app.test.ts +14 -3
- package/templates/plugin/AGENTS.md +11 -0
- package/templates/plugin/CLAUDE.md +1 -0
- package/templates/plugin/README.md +27 -0
- package/templates/plugin/gitignore +2 -0
- package/templates/plugin/llms.txt +19 -0
- package/templates/plugin/package.json +33 -0
- package/templates/plugin/src/index.ts +40 -0
- package/templates/plugin/tests/fixture/prunelle.config.ts +4 -0
- package/templates/plugin/tests/fixture/src/routes/index.tsx +11 -0
- package/templates/plugin/tests/plugin.test.ts +42 -0
- package/templates/plugin/tsconfig.json +19 -0
- package/ts-plugin.cjs +3674 -2196
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
// The RGAA criteria a page meets through its layouts as much as through itself: a link past the blocks every page repeats (12.7) and a title (8.5).
|
|
2
|
+
|
|
3
|
+
import type { JSXChild, JSXElement, Program } from 'oxc-parser'
|
|
4
|
+
import type { ModuleAnalysis } from './analyze'
|
|
5
|
+
import { basename } from 'node:path'
|
|
6
|
+
import { relPosix } from '../build/files'
|
|
7
|
+
import { ancestorFiles, fileToRoutePath } from '../build/routes'
|
|
8
|
+
import { walk } from './ast'
|
|
9
|
+
import { isComponentTag, jsxTagName, staticLiteral, unwrapTsExpr } from './shared'
|
|
10
|
+
|
|
11
|
+
export type RgaaPageRule = 'rgaa/skip-link' | 'rgaa/page-title'
|
|
12
|
+
|
|
13
|
+
export interface RgaaPageFinding {
|
|
14
|
+
rule: RgaaPageRule
|
|
15
|
+
/** From the project root. */
|
|
16
|
+
file: string
|
|
17
|
+
line: number
|
|
18
|
+
message: string
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
// Blocks a layout repeats on every page, which a keyboard user tabs through on each one unless a link jumps past them.
|
|
22
|
+
const REPEATED_BLOCKS = new Set(['header', 'nav'])
|
|
23
|
+
|
|
24
|
+
function hrefOf(el: JSXElement): string | undefined {
|
|
25
|
+
for (const attr of el.openingElement.attributes) {
|
|
26
|
+
if (attr.type !== 'JSXAttribute' || attr.name.type !== 'JSXIdentifier' || attr.name.name !== 'href' || attr.value === null)
|
|
27
|
+
continue
|
|
28
|
+
if (attr.value.type === 'Literal')
|
|
29
|
+
return String(attr.value.value)
|
|
30
|
+
if (attr.value.type === 'JSXExpressionContainer' && attr.value.expression.type !== 'JSXEmptyExpression') {
|
|
31
|
+
const literal = staticLiteral(unwrapTsExpr(attr.value.expression))
|
|
32
|
+
return literal.ok && typeof literal.value === 'string' ? literal.value : undefined
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
return undefined
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/** What a layout renders that 12.7 cares about: the offset of its first `<header>` or `<nav>`, and whether it links to an anchor of the page (`<a href="#main">`). */
|
|
39
|
+
function layoutBlocks(program: Program): { block: number | undefined, skipLink: boolean } {
|
|
40
|
+
let block: number | undefined
|
|
41
|
+
let skipLink = false
|
|
42
|
+
walk(program, (node) => {
|
|
43
|
+
if (node.type !== 'JSXElement')
|
|
44
|
+
return
|
|
45
|
+
const tag = jsxTagName(node.openingElement.name)
|
|
46
|
+
if (block === undefined && REPEATED_BLOCKS.has(tag))
|
|
47
|
+
block = node.start
|
|
48
|
+
const href = tag === 'a' ? hrefOf(node) : undefined
|
|
49
|
+
if (href !== undefined && href.length > 1 && href.startsWith('#'))
|
|
50
|
+
skipLink = true
|
|
51
|
+
})
|
|
52
|
+
return { block, skipLink }
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
// A <title> with text, or a component or an expression that may render one.
|
|
56
|
+
function mayHoldTitle(children: readonly JSXChild[]): boolean {
|
|
57
|
+
for (const child of children) {
|
|
58
|
+
if (child.type === 'JSXExpressionContainer' && child.expression.type !== 'JSXEmptyExpression')
|
|
59
|
+
return true
|
|
60
|
+
if (child.type === 'JSXFragment' && mayHoldTitle(child.children))
|
|
61
|
+
return true
|
|
62
|
+
if (child.type !== 'JSXElement')
|
|
63
|
+
continue
|
|
64
|
+
const tag = jsxTagName(child.openingElement.name)
|
|
65
|
+
if (isComponentTag(tag) && tag !== 'Head')
|
|
66
|
+
return true
|
|
67
|
+
if (tag === 'title' && child.children.some(c => c.type !== 'JSXText' || c.value.trim() !== ''))
|
|
68
|
+
return true
|
|
69
|
+
}
|
|
70
|
+
return false
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/** Whether a module renders a `<Head>` holding a title, or something that may render one. */
|
|
74
|
+
function rendersTitle(program: Program): boolean {
|
|
75
|
+
let found = false
|
|
76
|
+
walk(program, (node) => {
|
|
77
|
+
if (found)
|
|
78
|
+
return false
|
|
79
|
+
if (node.type === 'JSXElement' && jsxTagName(node.openingElement.name) === 'Head' && mayHoldTitle(node.children))
|
|
80
|
+
found = true
|
|
81
|
+
return undefined
|
|
82
|
+
})
|
|
83
|
+
return found
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
function lineAt(code: string, offset: number): number {
|
|
87
|
+
let line = 1
|
|
88
|
+
for (let i = code.indexOf('\n'); i !== -1 && i < offset; i = code.indexOf('\n', i + 1))
|
|
89
|
+
line++
|
|
90
|
+
return line
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
export interface RgaaPageInput {
|
|
94
|
+
root: string
|
|
95
|
+
routesDir: string
|
|
96
|
+
routeFiles: readonly string[]
|
|
97
|
+
analyses: ReadonlyMap<string, ModuleAnalysis>
|
|
98
|
+
/** Whether a plugin (`@prunellejs/seo`) reports a page with no title itself. */
|
|
99
|
+
titleChecked: boolean
|
|
100
|
+
source: (file: string) => string
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/** A layout that renders a header or a navigation with no skip link in it or above it, and a page that neither it nor its layouts give a title. A file not analysed counts as compliant, as nothing is known of it. */
|
|
104
|
+
export function rgaaPageFindings(input: RgaaPageInput): RgaaPageFinding[] {
|
|
105
|
+
const { root, routesDir, routeFiles, analyses, titleChecked, source } = input
|
|
106
|
+
const findings: RgaaPageFinding[] = []
|
|
107
|
+
const blocks = new Map<string, { block: number | undefined, skipLink: boolean }>()
|
|
108
|
+
const blocksOf = (file: string): { block: number | undefined, skipLink: boolean } => {
|
|
109
|
+
let found = blocks.get(file)
|
|
110
|
+
if (found === undefined) {
|
|
111
|
+
const program = analyses.get(file)?.program
|
|
112
|
+
found = program === undefined ? { block: undefined, skipLink: true } : layoutBlocks(program)
|
|
113
|
+
blocks.set(file, found)
|
|
114
|
+
}
|
|
115
|
+
return found
|
|
116
|
+
}
|
|
117
|
+
for (const file of routeFiles) {
|
|
118
|
+
const layouts = ancestorFiles(routesDir, file, ['_layout.tsx'])
|
|
119
|
+
if (basename(file) === '_layout.tsx') {
|
|
120
|
+
const { block } = blocksOf(file)
|
|
121
|
+
if (block !== undefined && !layouts.some(layout => blocksOf(layout).skipLink)) {
|
|
122
|
+
const rel = relPosix(root, file)
|
|
123
|
+
findings.push({ rule: 'rgaa/skip-link', file: rel, line: lineAt(source(file), block), message: `${rel} repeats a header or a navigation on every page with no link to jump past it, which a keyboard user tabs through on each page: add <a href="#main" class="skip-link">Aller au contenu</a> as its first element and id="main" on <main>.` })
|
|
124
|
+
}
|
|
125
|
+
continue
|
|
126
|
+
}
|
|
127
|
+
const analysis = analyses.get(file)
|
|
128
|
+
if (titleChecked || basename(file).startsWith('_') || analysis === undefined || !analysis.exportedNames.includes('default'))
|
|
129
|
+
continue
|
|
130
|
+
const titled = [file, ...layouts].some((f) => {
|
|
131
|
+
const program = analyses.get(f)?.program
|
|
132
|
+
return program === undefined || rendersTitle(program)
|
|
133
|
+
})
|
|
134
|
+
if (!titled) {
|
|
135
|
+
const rel = relPosix(root, file)
|
|
136
|
+
findings.push({ rule: 'rgaa/page-title', file: rel, line: 1, message: `Route "${fileToRoutePath(routesDir, file)}" has no <title>, so a tab, a bookmark and a screen reader all name it by its URL: add <Head><title>...</title></Head> to ${rel}, or a default one to its _layout.tsx.` })
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
return findings
|
|
140
|
+
}
|
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
import type { JSXAttribute, JSXAttributeItem, JSXChild, JSXElement, Node } from 'oxc-parser'
|
|
4
4
|
import type { CompilerWarning } from './mistakes'
|
|
5
|
+
import { genericLinkText, tableWithoutCaption, videoWithoutCaptions } from './compliance-rgaa-content'
|
|
5
6
|
import { cleanJsxText, isComponentTag, jsxTagName, staticLiteral, unwrapParens, unwrapTsExpr } from './shared'
|
|
6
7
|
|
|
7
8
|
export const RGAA_JSX_RULES = [
|
|
@@ -26,6 +27,9 @@ export const RGAA_JSX_RULES = [
|
|
|
26
27
|
'rgaa/frame-title',
|
|
27
28
|
'rgaa/empty-heading',
|
|
28
29
|
'rgaa/autocomplete',
|
|
30
|
+
'rgaa/generic-link-text',
|
|
31
|
+
'rgaa/video-captions',
|
|
32
|
+
'rgaa/table-caption',
|
|
29
33
|
] as const
|
|
30
34
|
|
|
31
35
|
export type RgaaJsxRule = (typeof RGAA_JSX_RULES)[number]
|
|
@@ -567,6 +571,20 @@ function checkAutocomplete(element: JSXElement, tag: string, ctx: RgaaElementCon
|
|
|
567
571
|
emitWarning(ctx, 'rgaa/autocomplete', element.start, `[rgaa/autocomplete] <${tag}${name !== undefined ? ` name="${name}"` : ''}> in ${ctx.filename} collects personal data without autocomplete: add autocomplete="${token}".`)
|
|
568
572
|
}
|
|
569
573
|
|
|
574
|
+
// What a link says read alone, a video's captions and a table's title.
|
|
575
|
+
function checkContent(element: JSXElement, tag: string, ctx: RgaaElementContext): void {
|
|
576
|
+
const attrs = element.openingElement.attributes
|
|
577
|
+
const text = tag === 'a' && !hasLabelAttr(attrs) ? genericLinkText(element) : undefined
|
|
578
|
+
if (text !== undefined)
|
|
579
|
+
emitWarning(ctx, 'rgaa/generic-link-text', element.start, `[rgaa/generic-link-text] <a>${text}</a> in ${ctx.filename} does not say where it goes when read alone, as a screen reader lists links: name the target in its text, or add an aria-label that starts with "${text}" and names it.`)
|
|
580
|
+
// A muted video with no controls has no sound to caption: a background loop.
|
|
581
|
+
const silent = hasHandler(attrs, ['muted']) && !hasHandler(attrs, ['controls'])
|
|
582
|
+
if (tag === 'video' && !silent && !isAriaHidden(attrs) && videoWithoutCaptions(element))
|
|
583
|
+
emitWarning(ctx, 'rgaa/video-captions', element.start, `[rgaa/video-captions] <video> in ${ctx.filename} has no captions: add <track kind="captions" src="....vtt" srclang="fr">, or mute it and drop its controls when it has no sound to follow.`)
|
|
584
|
+
if (tag === 'table' && !isPresentational(attrs) && !hasLabelAttr(attrs) && tableWithoutCaption(element))
|
|
585
|
+
emitWarning(ctx, 'rgaa/table-caption', element.start, `[rgaa/table-caption] <table> in ${ctx.filename} has no title: add a <caption> as its first child, or aria-labelledby naming the heading above it; a layout table takes role="presentation".`)
|
|
586
|
+
}
|
|
587
|
+
|
|
570
588
|
// The ui Select draws its own combobox, so it needs a name as a native <select> does.
|
|
571
589
|
const SELECT_COMPONENT_RE = /(?:^|\.)Select$/
|
|
572
590
|
|
|
@@ -610,5 +628,6 @@ export function checkRgaaElement(element: JSXElement, tag: string, ctx: RgaaElem
|
|
|
610
628
|
checkKeyboardReach(element, tag, ctx)
|
|
611
629
|
checkMissingNames(element, tag, ctx)
|
|
612
630
|
checkAutocomplete(element, tag, ctx)
|
|
631
|
+
checkContent(element, tag, ctx)
|
|
613
632
|
}
|
|
614
633
|
}
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
// The rgpd/third-party-url warnings of a JSX element: a literal URL, a URL the source does not show, and a URL given to a component.
|
|
2
|
+
|
|
3
|
+
import type { JSXElement } from 'oxc-parser'
|
|
4
|
+
import type { CompilerWarning } from './mistakes'
|
|
5
|
+
import { thirdPartyComponentLoads, thirdPartyLoads, unreadableLoads } from './compliance-urls'
|
|
6
|
+
|
|
7
|
+
const FIX = 'self-host it (e.g. @prunellejs/seo fonts or public/) or wrap it in <ConsentGate category="...">'
|
|
8
|
+
|
|
9
|
+
/** Warns about the URLs a plain element loads, the ones prunelle can read and the ones it cannot. */
|
|
10
|
+
export function checkThirdPartyElement(
|
|
11
|
+
element: JSXElement,
|
|
12
|
+
tag: string,
|
|
13
|
+
filename: string,
|
|
14
|
+
isIgnored: (offset: number) => boolean,
|
|
15
|
+
warnings: CompilerWarning[],
|
|
16
|
+
): void {
|
|
17
|
+
const named = new Set<string>()
|
|
18
|
+
for (const load of thirdPartyLoads(element, tag)) {
|
|
19
|
+
named.add(load.attr)
|
|
20
|
+
if (isIgnored(load.start))
|
|
21
|
+
continue
|
|
22
|
+
warnings.push({
|
|
23
|
+
rule: 'rgpd/third-party-url',
|
|
24
|
+
urlOrigin: load.origin,
|
|
25
|
+
start: load.start,
|
|
26
|
+
message: `[rgpd/third-party-url] <${tag} ${load.attr}="${load.url}"> in ${filename} loads a third-party resource without consent: ${FIX}.`,
|
|
27
|
+
})
|
|
28
|
+
}
|
|
29
|
+
for (const load of unreadableLoads(element, tag)) {
|
|
30
|
+
// An attribute that already names a third-party URL has its warning.
|
|
31
|
+
if (named.has(load.attr) || isIgnored(load.start))
|
|
32
|
+
continue
|
|
33
|
+
warnings.push({
|
|
34
|
+
rule: 'rgpd/third-party-url',
|
|
35
|
+
start: load.start,
|
|
36
|
+
message: `[rgpd/third-party-url] <${tag} ${load.attr}={...}> in ${filename} loads a URL prunelle cannot read, which may be another site's: ${FIX}, or add // prunelle-ignore rgpd/third-party-url when it always stays on this site.`,
|
|
37
|
+
})
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/** Warns about a third-party URL given to a component, which may load it. */
|
|
42
|
+
export function checkThirdPartyComponent(
|
|
43
|
+
element: JSXElement,
|
|
44
|
+
tag: string,
|
|
45
|
+
filename: string,
|
|
46
|
+
isIgnored: (offset: number) => boolean,
|
|
47
|
+
warnings: CompilerWarning[],
|
|
48
|
+
): void {
|
|
49
|
+
for (const load of thirdPartyComponentLoads(element, tag)) {
|
|
50
|
+
if (isIgnored(load.start))
|
|
51
|
+
continue
|
|
52
|
+
warnings.push({
|
|
53
|
+
rule: 'rgpd/third-party-url',
|
|
54
|
+
urlOrigin: load.origin,
|
|
55
|
+
start: load.start,
|
|
56
|
+
message: `[rgpd/third-party-url] <${tag} ${load.attr}="${load.url}"> in ${filename} gives a third-party URL to a component that may load it without consent: ${FIX}.`,
|
|
57
|
+
})
|
|
58
|
+
}
|
|
59
|
+
}
|
|
@@ -128,6 +128,58 @@ function loadingAttrs(element: JSXElement, tag: string): Array<[string, JSXAttri
|
|
|
128
128
|
return out
|
|
129
129
|
}
|
|
130
130
|
|
|
131
|
+
// What a template literal's static head says about where its URL points: a path on this site, a complete host, or a scheme with no URL to read.
|
|
132
|
+
const DECIDED_HEAD_RES = [/^\/(?!\/)./s, /^(?:https?:)?\/\/[^/?#]+[/?#]/i, /^(?:data|blob|mailto|tel):/i, /^[^:/?#]+\//, /^[#?]/]
|
|
133
|
+
|
|
134
|
+
// Whether an expression gives a URL the source does not show: a name, a call, a member, or a template whose start does not tell where it points.
|
|
135
|
+
function isUnreadableUrl(expr: Expression): boolean {
|
|
136
|
+
const node = unwrapTsExpr(expr)
|
|
137
|
+
switch (node.type) {
|
|
138
|
+
case 'Literal':
|
|
139
|
+
case 'ObjectExpression':
|
|
140
|
+
return false
|
|
141
|
+
case 'TemplateLiteral':
|
|
142
|
+
return node.expressions.length > 0 && !DECIDED_HEAD_RES.some(re => re.test(node.quasis[0]?.value.cooked ?? ''))
|
|
143
|
+
case 'ConditionalExpression':
|
|
144
|
+
return isUnreadableUrl(node.consequent) || isUnreadableUrl(node.alternate)
|
|
145
|
+
case 'LogicalExpression':
|
|
146
|
+
return isUnreadableUrl(node.left) || isUnreadableUrl(node.right)
|
|
147
|
+
default:
|
|
148
|
+
return true
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/** The loading attributes of a plain element whose URL the source does not show, so prunelle cannot tell if it leaves the site. */
|
|
153
|
+
export function unreadableLoads(element: JSXElement, tag: string): Array<{ attr: string, start: number }> {
|
|
154
|
+
const out: Array<{ attr: string, start: number }> = []
|
|
155
|
+
for (const [name, attr] of loadingAttrs(element, tag)) {
|
|
156
|
+
const value = attr?.value
|
|
157
|
+
if (name !== 'style' && attr !== undefined && value?.type === 'JSXExpressionContainer' && value.expression.type !== 'JSXEmptyExpression' && isUnreadableUrl(value.expression))
|
|
158
|
+
out.push({ attr: name, start: attr.start })
|
|
159
|
+
}
|
|
160
|
+
return out
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
// A link component takes the visitor somewhere else, it does not load anything.
|
|
164
|
+
const LINK_COMPONENT_RE = /(?:^|\.)(?:\w*Link|A|Anchor)$/
|
|
165
|
+
|
|
166
|
+
/** Third-party origins a component receives as a literal `src`, `href` or `url` prop, which it may well load. */
|
|
167
|
+
export function thirdPartyComponentLoads(element: JSXElement, tag: string): ThirdPartyLoad[] {
|
|
168
|
+
const out: ThirdPartyLoad[] = []
|
|
169
|
+
const attrs = element.openingElement.attributes
|
|
170
|
+
for (const name of ['src', 'href', 'url']) {
|
|
171
|
+
const attr = findAttr(attrs, name)
|
|
172
|
+
if (attr === undefined || (name === 'href' && LINK_COMPONENT_RE.test(tag)))
|
|
173
|
+
continue
|
|
174
|
+
for (const url of attrStrings(attr)) {
|
|
175
|
+
const origin = thirdPartyOrigin(url.split(EXPR_PLACEHOLDER, 1)[0]!)
|
|
176
|
+
if (origin !== undefined)
|
|
177
|
+
out.push({ attr: name, url, origin, start: attr.start })
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
return out
|
|
181
|
+
}
|
|
182
|
+
|
|
131
183
|
/** Distinct third-party origins each loading attribute of a plain element points at. */
|
|
132
184
|
export function thirdPartyLoads(element: JSXElement, tag: string): ThirdPartyLoad[] {
|
|
133
185
|
const out: ThirdPartyLoad[] = []
|
|
@@ -4,6 +4,8 @@ import type { JSXElement, Node, Program } from 'oxc-parser'
|
|
|
4
4
|
import type { ModuleAnalysis } from './analyze'
|
|
5
5
|
import type { EcoJsxRule } from './compliance-eco'
|
|
6
6
|
import type { RgaaJsxRule, RgaaScope } from './compliance-rgaa'
|
|
7
|
+
import type { ContrastRule } from './compliance-contrast'
|
|
8
|
+
import type { RgaaPageRule } from './compliance-rgaa-pages'
|
|
7
9
|
import type { CompilerWarning } from './mistakes'
|
|
8
10
|
import { existsSync, readFileSync } from 'node:fs'
|
|
9
11
|
import { basename, join } from 'node:path'
|
|
@@ -26,22 +28,33 @@ import {
|
|
|
26
28
|
} from './compliance-rgaa'
|
|
27
29
|
import { STATEMENT_PLACEHOLDER } from '../a11y/declaration'
|
|
28
30
|
import { checkEcoElement, ECO_JSX_RULES, largePublicFileWarnings } from './compliance-eco'
|
|
29
|
-
import {
|
|
31
|
+
import { contrastFindings } from './compliance-contrast'
|
|
32
|
+
import { rendersNoText } from './compliance-empty'
|
|
33
|
+
import { rgaaPageFindings } from './compliance-rgaa-pages'
|
|
34
|
+
import { checkThirdPartyComponent, checkThirdPartyElement } from './compliance-third-party'
|
|
35
|
+
import { CSS_IMPORT_RE, CSS_URL_RE, parseOrigin, thirdPartyOrigin } from './compliance-urls'
|
|
30
36
|
import { formatDiagnostic, formatLoc, getLineStarts, isComponentTag, jsxTagName, lineIndexAtOffset, unwrapTsExpr } from './shared'
|
|
31
37
|
|
|
32
38
|
export type ComplianceRule =
|
|
33
39
|
| RgaaJsxRule
|
|
40
|
+
| RgaaPageRule
|
|
41
|
+
| ContrastRule
|
|
34
42
|
| EcoJsxRule
|
|
35
43
|
| 'rgesn/large-public-file'
|
|
36
44
|
| 'rgesn/declaration'
|
|
37
45
|
| 'rgesn/island-by-value'
|
|
38
46
|
| 'rgpd/third-party-url'
|
|
39
47
|
| 'rgpd/consent-retention'
|
|
48
|
+
| 'rgpd/consent-preferences'
|
|
49
|
+
| 'rgpd/cookie-category'
|
|
50
|
+
| 'rgpd/cookie-purpose'
|
|
40
51
|
| 'rgpd/privacy-notice'
|
|
41
52
|
| 'lcen/legal-notice'
|
|
42
53
|
| 'rgaa/document-lang'
|
|
43
54
|
| 'rgaa/accessibility-statement'
|
|
44
55
|
| 'security/csrf-off'
|
|
56
|
+
| 'security/proxy-no-site'
|
|
57
|
+
| 'security/html-sink'
|
|
45
58
|
| FormWarningRule
|
|
46
59
|
|
|
47
60
|
/** The warnings of the form-vs-action check (`cli/check-forms.ts`), silenced like the compliance rules. */
|
|
@@ -66,11 +79,19 @@ const COMPLIANCE_RULES = new Set<ComplianceRule>([
|
|
|
66
79
|
'rgesn/island-by-value',
|
|
67
80
|
'rgpd/third-party-url',
|
|
68
81
|
'rgpd/consent-retention',
|
|
82
|
+
'rgpd/consent-preferences',
|
|
83
|
+
'rgpd/cookie-category',
|
|
84
|
+
'rgpd/cookie-purpose',
|
|
69
85
|
'rgpd/privacy-notice',
|
|
70
86
|
'lcen/legal-notice',
|
|
71
87
|
'rgaa/document-lang',
|
|
72
88
|
'rgaa/accessibility-statement',
|
|
89
|
+
'rgaa/skip-link',
|
|
90
|
+
'rgaa/page-title',
|
|
91
|
+
'rgaa/contrast',
|
|
73
92
|
'security/csrf-off',
|
|
93
|
+
'security/proxy-no-site',
|
|
94
|
+
'security/html-sink',
|
|
74
95
|
'action/unguarded-public-form',
|
|
75
96
|
'form/invalid-option',
|
|
76
97
|
'form/schema-unsupported',
|
|
@@ -89,14 +110,15 @@ const PERSONAL_DATA_TYPES = new Set(['email', 'tel'])
|
|
|
89
110
|
const PERSONAL_DATA_NAMES = new Set(['email', 'phone', 'tel'])
|
|
90
111
|
const ACCESSIBILITY_PATHS = new Set(['/accessibilite', '/accessibility', '/declaration-accessibilite', '/accessibility-statement'])
|
|
91
112
|
const ECO_DECLARATION_PATHS = new Set(['/ecoconception', '/declaration-ecoconception', '/eco-design'])
|
|
92
|
-
// The
|
|
93
|
-
const TEMPLATE_PLACEHOLDER =
|
|
113
|
+
// The markers in front of every detail the publisher must supply: `[to fill in` in the `prunelle new` notices, `[à compléter` in the pages `prunelle a11y`, `prunelle eco` and `prunelle rgpd` draft.
|
|
114
|
+
const TEMPLATE_PLACEHOLDER = /\[to fill in/g
|
|
115
|
+
const DRAFT_PLACEHOLDER = new RegExp(STATEMENT_PLACEHOLDER.replace('[', '\\['), 'g')
|
|
94
116
|
|
|
95
|
-
interface NoticeDetails { rule: ComplianceRule, page: string, law: string, marker:
|
|
117
|
+
interface NoticeDetails { rule: ComplianceRule, page: string, law: string, marker: RegExp }
|
|
96
118
|
const LEGAL_NOTICE_DETAILS: NoticeDetails = { rule: 'lcen/legal-notice', page: 'legal notice', law: 'French law (LCEN art. 1-1) requires the publisher\'s identity, address and phone, the publication director and the host\'s name, address and phone', marker: TEMPLATE_PLACEHOLDER }
|
|
97
|
-
const PRIVACY_NOTICE_DETAILS: NoticeDetails = { rule: 'rgpd/privacy-notice', page: 'privacy notice', law: 'the RGPD (art. 13) requires the controller\'s identity and contact, the purposes and legal basis, the retention periods, the recipients and the rights of the people concerned', marker: TEMPLATE_PLACEHOLDER }
|
|
98
|
-
const ECO_DECLARATION_DETAILS: NoticeDetails = { rule: 'rgesn/declaration', page: 'eco-design declaration', law: 'the RGESN 2024 asks it to name the service, the evaluating entity and a contact, the minimum hardware profile and the planned actions, and to justify each criterion', marker:
|
|
99
|
-
const STATEMENT_DETAILS: NoticeDetails = { rule: 'rgaa/accessibility-statement', page: 'accessibility statement', law: 'the RGAA requires it to name the publisher, the site it covers and the pages audited, and to give a contact for accessibility feedback', marker:
|
|
119
|
+
const PRIVACY_NOTICE_DETAILS: NoticeDetails = { rule: 'rgpd/privacy-notice', page: 'privacy notice', law: 'the RGPD (art. 13) requires the controller\'s identity and contact, the purposes and legal basis, the retention periods, the recipients and the rights of the people concerned', marker: new RegExp(`${TEMPLATE_PLACEHOLDER.source}|${DRAFT_PLACEHOLDER.source}`, 'g') }
|
|
120
|
+
const ECO_DECLARATION_DETAILS: NoticeDetails = { rule: 'rgesn/declaration', page: 'eco-design declaration', law: 'the RGESN 2024 asks it to name the service, the evaluating entity and a contact, the minimum hardware profile and the planned actions, and to justify each criterion', marker: DRAFT_PLACEHOLDER }
|
|
121
|
+
const STATEMENT_DETAILS: NoticeDetails = { rule: 'rgaa/accessibility-statement', page: 'accessibility statement', law: 'the RGAA requires it to name the publisher, the site it covers and the pages audited, and to give a contact for accessibility feedback', marker: DRAFT_PLACEHOLDER }
|
|
100
122
|
const LOCALE_PREFIX_RE = /^\/(?:[a-z]{2}(?:-[a-z]{2})?|:[a-z_]\w*(?:=[^/]+)?\??|\[\[?[^\]]+\]\]?)(\/.+)$/i
|
|
101
123
|
|
|
102
124
|
const LINE_IGNORE_RE = /(?:^|[\s{;(,])\/\/\s*prunelle-ignore(-file)?[ \t]+([^\r\n]+)/g
|
|
@@ -246,25 +268,6 @@ function collectDirectLabelTargets(root: Node | null | undefined): Set<string> {
|
|
|
246
268
|
return targets
|
|
247
269
|
}
|
|
248
270
|
|
|
249
|
-
function checkThirdPartyElement(
|
|
250
|
-
element: JSXElement,
|
|
251
|
-
tag: string,
|
|
252
|
-
filename: string,
|
|
253
|
-
ignores: IgnoreMap,
|
|
254
|
-
warnings: CompilerWarning[],
|
|
255
|
-
): void {
|
|
256
|
-
for (const load of thirdPartyLoads(element, tag)) {
|
|
257
|
-
if (isIgnoredAt(ignores, 'rgpd/third-party-url', load.start))
|
|
258
|
-
continue
|
|
259
|
-
warnings.push({
|
|
260
|
-
rule: 'rgpd/third-party-url',
|
|
261
|
-
urlOrigin: load.origin,
|
|
262
|
-
start: load.start,
|
|
263
|
-
message: `[rgpd/third-party-url] <${tag} ${load.attr}="${load.url}"> in ${filename} loads a third-party resource without consent: self-host it (e.g. @prunellejs/seo fonts or public/) or wrap it in <ConsentGate category="...">.`,
|
|
264
|
-
})
|
|
265
|
-
}
|
|
266
|
-
}
|
|
267
|
-
|
|
268
271
|
// A gate on the essential category always renders its children, so it holds nothing back until consent.
|
|
269
272
|
function opensConsentGate(element: JSXElement, resolvedTag: string | undefined): boolean {
|
|
270
273
|
return resolvedTag === 'ConsentGate' && staticStringValueOf(findAttr(element.openingElement.attributes, 'category')?.value ?? null) !== 'essential'
|
|
@@ -375,14 +378,17 @@ export function checkCompliance(
|
|
|
375
378
|
isIgnored,
|
|
376
379
|
warnings,
|
|
377
380
|
}
|
|
381
|
+
const thirdPartyIgnored = (offset: number): boolean => isIgnoredAt(ignores, 'rgpd/third-party-url', offset)
|
|
378
382
|
if (isComponentTag(tag)) {
|
|
379
383
|
checkRgaaComponent(node, tag, ctx)
|
|
384
|
+
if (ancestors.consentGateDepth === 0)
|
|
385
|
+
checkThirdPartyComponent(node, tag, filename, thirdPartyIgnored, warnings)
|
|
380
386
|
}
|
|
381
387
|
else {
|
|
382
388
|
checkRgaaElement(node, tag, ctx)
|
|
383
389
|
checkEcoElement(node, tag, ctx)
|
|
384
390
|
if (ancestors.consentGateDepth === 0)
|
|
385
|
-
checkThirdPartyElement(node, tag, filename,
|
|
391
|
+
checkThirdPartyElement(node, tag, filename, thirdPartyIgnored, warnings)
|
|
386
392
|
}
|
|
387
393
|
}
|
|
388
394
|
visit(attrs, ancestors, scope, labelTargets)
|
|
@@ -529,6 +535,8 @@ export interface ProjectComplianceInput {
|
|
|
529
535
|
lang?: string | undefined
|
|
530
536
|
/** A plugin sets `<html lang>` per request, so a missing `lang` is not a mistake. */
|
|
531
537
|
langFromPlugin?: boolean | undefined
|
|
538
|
+
/** A plugin reports a page with no title itself, so `rgaa/page-title` would say it twice. */
|
|
539
|
+
titleChecked?: boolean | undefined
|
|
532
540
|
}
|
|
533
541
|
|
|
534
542
|
/** True when a plugin sets `<html lang>` from the request's locale (`@prunellejs/i18n`). */
|
|
@@ -536,6 +544,18 @@ export function pluginSetsLang(plugins: readonly { name: string }[]): boolean {
|
|
|
536
544
|
return plugins.some(p => p.name === '@prunellejs/i18n')
|
|
537
545
|
}
|
|
538
546
|
|
|
547
|
+
function contrastWarnings(relFile: string, code: string): string[] {
|
|
548
|
+
const ignores = parsePrunelleIgnores(code)
|
|
549
|
+
return contrastFindings(code)
|
|
550
|
+
.filter(finding => !isIgnoredAt(ignores, 'rgaa/contrast', finding.offset))
|
|
551
|
+
.map(finding => formatDiagnostic(formatLoc(relFile, code, finding.offset), finding.message, 'warning'))
|
|
552
|
+
}
|
|
553
|
+
|
|
554
|
+
/** True when a plugin warns about a page with no title (`@prunellejs/seo`). */
|
|
555
|
+
export function pluginChecksTitle(plugins: readonly { name: string }[]): boolean {
|
|
556
|
+
return plugins.some(p => p.name === '@prunellejs/seo')
|
|
557
|
+
}
|
|
558
|
+
|
|
539
559
|
/** Collects project-level CSS third-party URL warnings and the LCEN, RGPD and RGAA notice warnings for `prunelle check` and production `prunelle build`. */
|
|
540
560
|
export function collectProjectComplianceWarnings(input: ProjectComplianceInput): string[] {
|
|
541
561
|
const { root, srcDir, routesDir, routeFiles, analyses, compliance, consentMaxAge, site, lang, langFromPlugin } = input
|
|
@@ -555,6 +575,8 @@ export function collectProjectComplianceWarnings(input: ProjectComplianceInput):
|
|
|
555
575
|
for (const cssFile of [...srcFiles.filter(file => file.endsWith('.css')), ...publicCss]) {
|
|
556
576
|
const code = readFileSync(cssFile, 'utf8')
|
|
557
577
|
warnings.push(...checkCssCompliance(relPosix(root, cssFile), code, compliance, site))
|
|
578
|
+
if (!isRuleIgnored('rgaa/contrast', compliance))
|
|
579
|
+
warnings.push(...contrastWarnings(relPosix(root, cssFile), code))
|
|
558
580
|
}
|
|
559
581
|
|
|
560
582
|
const pagePaths: string[] = []
|
|
@@ -567,7 +589,7 @@ export function collectProjectComplianceWarnings(input: ProjectComplianceInput):
|
|
|
567
589
|
const routePath = fileToRoutePath(routesDir, file)
|
|
568
590
|
pagePaths.push(routePath)
|
|
569
591
|
if (isLegalNoticePath(routePath, compliance) && !isRuleIgnored('lcen/legal-notice', compliance))
|
|
570
|
-
warnings.push(...
|
|
592
|
+
warnings.push(...legalNoticeWarnings(relPosix(root, file), readFileSync(file, 'utf8'), analysis))
|
|
571
593
|
if (matchesNoticePath(routePath, compliance?.privacyNotice, DEFAULT_PRIVACY_PATHS) && !isRuleIgnored('rgpd/privacy-notice', compliance))
|
|
572
594
|
warnings.push(...placeholderWarnings(relPosix(root, file), readFileSync(file, 'utf8'), PRIVACY_NOTICE_DETAILS))
|
|
573
595
|
// Any site that publishes a statement, public body or not, publishes what it says.
|
|
@@ -579,6 +601,10 @@ export function collectProjectComplianceWarnings(input: ProjectComplianceInput):
|
|
|
579
601
|
warnings.push(...checkRouteCompliance(pagePaths, compliance))
|
|
580
602
|
if (pagePaths.length > 0 && lang === undefined && langFromPlugin !== true && !isRuleIgnored('rgaa/document-lang', compliance))
|
|
581
603
|
warnings.push(formatDiagnostic('prunelle.config.ts:1:1', '[rgaa/document-lang] No lang in prunelle.config.ts, so every page says <html lang="en"> and @prunellejs/ui speaks English: set lang: \'fr\' (or the language your pages are written in).', 'warning'))
|
|
604
|
+
for (const finding of rgaaPageFindings({ root, routesDir, routeFiles, analyses, titleChecked: input.titleChecked === true, source: file => readFileSync(file, 'utf8') })) {
|
|
605
|
+
if (!isRuleIgnored(finding.rule, compliance) && !parsePrunelleIgnores(readFileSync(join(root, finding.file), 'utf8')).fileRules.has(finding.rule))
|
|
606
|
+
warnings.push(formatDiagnostic(`${finding.file}:${finding.line}:1`, `[${finding.rule}] ${finding.message}`, 'warning'))
|
|
607
|
+
}
|
|
582
608
|
warnings.push(...largePublicFileWarnings(root, isRuleIgnored('rgesn/large-public-file', compliance), publicDir))
|
|
583
609
|
if (!isRuleIgnored('rgpd/privacy-notice', compliance) && !pagePaths.some(p => matchesNoticePath(p, compliance?.privacyNotice, DEFAULT_PRIVACY_PATHS))) {
|
|
584
610
|
const tsxFiles = [...new Set([...srcFiles.filter(file => file.endsWith('.tsx')), ...routeFiles.filter(file => file.endsWith('.tsx'))])].sort()
|
|
@@ -587,7 +613,7 @@ export function collectProjectComplianceWarnings(input: ProjectComplianceInput):
|
|
|
587
613
|
return warnings
|
|
588
614
|
}
|
|
589
615
|
|
|
590
|
-
function isPersonalDataInput(el: JSXElement): boolean {
|
|
616
|
+
export function isPersonalDataInput(el: JSXElement): boolean {
|
|
591
617
|
if (jsxTagName(el.openingElement.name) !== 'input')
|
|
592
618
|
return false
|
|
593
619
|
const attrs = el.openingElement.attributes
|
|
@@ -635,21 +661,25 @@ function privacyNoticeWarnings(root: string, tsxFiles: readonly string[], analys
|
|
|
635
661
|
return []
|
|
636
662
|
}
|
|
637
663
|
|
|
664
|
+
// A legal notice route that renders no text is a page, not a notice; one with text still has its placeholders checked.
|
|
665
|
+
function legalNoticeWarnings(relFile: string, code: string, analysis: ModuleAnalysis): string[] {
|
|
666
|
+
const { rule, page, law } = LEGAL_NOTICE_DETAILS
|
|
667
|
+
if (parsePrunelleIgnores(code).fileRules.has(rule) || !rendersNoText(analysis.program))
|
|
668
|
+
return placeholderWarnings(relFile, code, LEGAL_NOTICE_DETAILS)
|
|
669
|
+
return [formatDiagnostic(`${relFile}:1:1`, `[${rule}] The ${page} page ${relFile} renders no text: ${law}; write it in the page.`, 'warning')]
|
|
670
|
+
}
|
|
671
|
+
|
|
638
672
|
// One warning for the first unfilled detail outside comments and ignores: the publisher fills them all in one pass.
|
|
639
673
|
function placeholderWarnings(relFile: string, code: string, notice: NoticeDetails): string[] {
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
const
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
const comments = parseSync(relFile, code, { lang: relFile.endsWith('.tsx') ? 'tsx' : 'ts' }).comments
|
|
646
|
-
const inComment = (offset: number): boolean => comments.some(c => offset >= c.start && offset < c.end)
|
|
647
|
-
for (let from = 0; ;) {
|
|
648
|
-
const offset = code.indexOf(notice.marker, from)
|
|
649
|
-
if (offset === -1)
|
|
674
|
+
let ignores: ReturnType<typeof parsePrunelleIgnores> | undefined
|
|
675
|
+
let comments: { start: number, end: number }[] | undefined
|
|
676
|
+
for (const { index: offset, 0: marker } of code.matchAll(notice.marker)) {
|
|
677
|
+
ignores ??= parsePrunelleIgnores(code)
|
|
678
|
+
if (ignores.fileRules.has(notice.rule))
|
|
650
679
|
return []
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
680
|
+
comments ??= parseSync(relFile, code, { lang: relFile.endsWith('.tsx') ? 'tsx' : 'ts' }).comments
|
|
681
|
+
if (!comments.some(c => offset >= c.start && offset < c.end) && !isIgnoredAt(ignores, notice.rule, offset))
|
|
682
|
+
return [formatDiagnostic(formatLoc(relFile, code, offset), `[${notice.rule}] The ${notice.page} still holds "${marker} ...]" placeholders: ${notice.law}; replace every placeholder in ${relFile}.`, 'warning')]
|
|
654
683
|
}
|
|
684
|
+
return []
|
|
655
685
|
}
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
// The cookies an app sets with `cookies.set(...)`, read from its source for `<CookieTable>` and `prunelle check`.
|
|
2
|
+
|
|
3
|
+
import type { Argument, Expression, Node, ObjectExpression, Program } from 'oxc-parser'
|
|
4
|
+
import { parseSync } from 'oxc-parser'
|
|
5
|
+
import { walk } from './ast'
|
|
6
|
+
import { unwrapTsExpr } from './shared'
|
|
7
|
+
import { staticLiteralOrTemplate } from './ssr-helpers'
|
|
8
|
+
|
|
9
|
+
/** One cookie the app sets; a field left out could not be read from the source. `lifetime` is in seconds, or `'session'` for a cookie that ends with the browser session. */
|
|
10
|
+
export interface AppCookie {
|
|
11
|
+
name?: string | undefined
|
|
12
|
+
purpose?: string | undefined
|
|
13
|
+
category?: string | undefined
|
|
14
|
+
lifetime?: number | 'session' | undefined
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export interface ScannedCookie extends AppCookie {
|
|
18
|
+
file: string
|
|
19
|
+
/** Offset of the first `cookies.set` call for this cookie. */
|
|
20
|
+
start: number
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/** `ctx.cookies.set(...)` and `cookies.set(...)`, but not `map.set(...)`. */
|
|
24
|
+
export function isCookieSet(callee: Expression): boolean {
|
|
25
|
+
const member = unwrapTsExpr(callee)
|
|
26
|
+
if (member.type !== 'MemberExpression' || member.computed || member.property.type !== 'Identifier' || member.property.name !== 'set')
|
|
27
|
+
return false
|
|
28
|
+
const owner = unwrapTsExpr(member.object)
|
|
29
|
+
if (owner.type === 'Identifier')
|
|
30
|
+
return owner.name === 'cookies'
|
|
31
|
+
return owner.type === 'MemberExpression' && !owner.computed && owner.property.type === 'Identifier' && owner.property.name === 'cookies'
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function staticString(node: Node | undefined): string | undefined {
|
|
35
|
+
if (node === undefined)
|
|
36
|
+
return undefined
|
|
37
|
+
const literal = staticLiteralOrTemplate(node)
|
|
38
|
+
return literal.ok && typeof literal.value === 'string' ? literal.value : undefined
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
// `31536000` and `60 * 60 * 24 * 365`.
|
|
42
|
+
function staticSeconds(node: Node): number | undefined {
|
|
43
|
+
const n = unwrapTsExpr(node)
|
|
44
|
+
if (n.type === 'Literal')
|
|
45
|
+
return typeof n.value === 'number' ? n.value : undefined
|
|
46
|
+
if (n.type !== 'BinaryExpression' || (n.operator !== '*' && n.operator !== '+'))
|
|
47
|
+
return undefined
|
|
48
|
+
const left = staticSeconds(n.left)
|
|
49
|
+
const right = staticSeconds(n.right)
|
|
50
|
+
if (left === undefined || right === undefined)
|
|
51
|
+
return undefined
|
|
52
|
+
return n.operator === '*' ? left * right : left + right
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
function propertiesOf(options: ObjectExpression): { props: Map<string, Node>, open: boolean } {
|
|
56
|
+
const props = new Map<string, Node>()
|
|
57
|
+
let open = false
|
|
58
|
+
for (const prop of options.properties) {
|
|
59
|
+
const key = prop.type === 'Property' && !prop.computed && prop.key.type === 'Identifier' ? prop.key.name : undefined
|
|
60
|
+
if (key === undefined || prop.type !== 'Property')
|
|
61
|
+
open = true
|
|
62
|
+
else
|
|
63
|
+
props.set(key, prop.value)
|
|
64
|
+
}
|
|
65
|
+
return { props, open }
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
// A spread or computed key may hold any option, so an option it could hold is unreadable rather than absent.
|
|
69
|
+
function readOptions(options: Argument | undefined): Omit<AppCookie, 'name'> {
|
|
70
|
+
if (options === undefined)
|
|
71
|
+
return { category: 'essential', lifetime: 'session' }
|
|
72
|
+
if (options.type !== 'ObjectExpression')
|
|
73
|
+
return {}
|
|
74
|
+
const { props, open } = propertiesOf(options)
|
|
75
|
+
const maxAge = props.get('maxAge')
|
|
76
|
+
return {
|
|
77
|
+
purpose: staticString(props.get('purpose')),
|
|
78
|
+
category: props.has('category') ? staticString(props.get('category')) : open ? undefined : 'essential',
|
|
79
|
+
lifetime: maxAge !== undefined ? staticSeconds(maxAge) : props.has('expires') || open ? undefined : 'session',
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/** Each `cookies.set(...)` call of a module, in source order. */
|
|
84
|
+
export function scanCookieSets(file: string, code: string, program?: Program): ScannedCookie[] {
|
|
85
|
+
if (!code.includes('cookies'))
|
|
86
|
+
return []
|
|
87
|
+
const found: ScannedCookie[] = []
|
|
88
|
+
walk(program ?? parseSync(file, code, { lang: file.endsWith('x') ? 'tsx' : 'ts' }).program, (node): undefined => {
|
|
89
|
+
if (node.type === 'CallExpression' && isCookieSet(node.callee))
|
|
90
|
+
found.push({ file, start: node.start, name: staticString(node.arguments[0]), ...readOptions(node.arguments[2]) })
|
|
91
|
+
return undefined
|
|
92
|
+
})
|
|
93
|
+
return found
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/** One entry per cookie name, each field from the first call that sets it readably; a cookie whose name cannot be read keeps an entry of its own. */
|
|
97
|
+
export function mergeCookies(scanned: readonly ScannedCookie[]): ScannedCookie[] {
|
|
98
|
+
const byName = new Map<string, ScannedCookie>()
|
|
99
|
+
const merged: ScannedCookie[] = []
|
|
100
|
+
for (const cookie of scanned) {
|
|
101
|
+
const seen = cookie.name === undefined ? undefined : byName.get(cookie.name)
|
|
102
|
+
if (seen !== undefined) {
|
|
103
|
+
seen.purpose ??= cookie.purpose
|
|
104
|
+
seen.category ??= cookie.category
|
|
105
|
+
seen.lifetime ??= cookie.lifetime
|
|
106
|
+
continue
|
|
107
|
+
}
|
|
108
|
+
const entry = { ...cookie }
|
|
109
|
+
merged.push(entry)
|
|
110
|
+
if (cookie.name !== undefined)
|
|
111
|
+
byName.set(cookie.name, entry)
|
|
112
|
+
}
|
|
113
|
+
return merged
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/** The fields `<CookieTable>` cannot show, in the order of its columns. */
|
|
117
|
+
export function unreadableFields(cookie: AppCookie): ('name' | 'purpose' | 'category' | 'lifetime')[] {
|
|
118
|
+
return (['name', 'purpose', 'category', 'lifetime'] as const).filter(field => cookie[field] === undefined)
|
|
119
|
+
}
|