prunelle 0.1.1 → 0.7.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/llms.txt +137 -40
- package/package.json +11 -2
- package/src/a11y/declaration.ts +373 -0
- package/src/a11y/index.ts +477 -0
- package/src/a11y/rgaa-map.ts +230 -0
- package/src/attrs.ts +11 -3
- package/src/build/bundle.ts +89 -41
- package/src/build/byte-size.ts +11 -0
- package/src/build/client-entries.ts +3 -2
- package/src/build/client-plugin.ts +11 -5
- package/src/build/client-prune.ts +56 -6
- package/src/build/compile.ts +13 -10
- package/src/build/ecoindex.ts +238 -0
- package/src/build/files.ts +15 -0
- package/src/build/first-load.ts +85 -14
- package/src/build/graph.ts +29 -39
- package/src/build/index.ts +225 -122
- package/src/build/island-promote.ts +311 -0
- package/src/build/islands.ts +14 -203
- package/src/build/manifest.ts +56 -116
- package/src/build/output.ts +3 -6
- package/src/build/plugin-host.ts +115 -41
- package/src/build/precompress.ts +16 -3
- package/src/build/prerender.ts +177 -39
- package/src/build/reach.ts +200 -0
- package/src/build/render-rules.ts +2 -2
- package/src/build/report.ts +48 -5
- package/src/build/resolve.ts +8 -6
- package/src/build/routes.ts +175 -10
- package/src/build/static-headers.ts +112 -0
- package/src/build/static.ts +139 -39
- package/src/build/status-pages.ts +127 -0
- package/src/build/style-bundles.ts +526 -0
- package/src/cli/check-forms.ts +636 -0
- package/src/cli/check-imports.ts +31 -0
- package/src/cli/check-links.ts +632 -0
- package/src/cli/check-security.ts +64 -0
- package/src/cli/check.ts +101 -26
- package/src/cli/commands.ts +120 -11
- package/src/cli/docs.ts +403 -0
- package/src/cli/inspect.ts +105 -15
- package/src/cli/mcp-tools.ts +492 -0
- package/src/cli/mcp.ts +205 -0
- package/src/cli/new.ts +16 -5
- package/src/cli/render.ts +416 -0
- package/src/cli/upgrade-names.ts +73 -13
- package/src/cli/upgrade.ts +156 -22
- package/src/compiler/analyze.ts +87 -54
- package/src/compiler/browser-globals.ts +140 -17
- package/src/compiler/cacheability-private.ts +566 -0
- package/src/compiler/cacheability.ts +335 -0
- package/src/compiler/compliance-eco.ts +71 -0
- package/src/compiler/compliance-rgaa.ts +614 -0
- package/src/compiler/compliance-urls.ts +148 -0
- package/src/compiler/compliance.ts +407 -352
- package/src/compiler/data-keys.ts +199 -192
- package/src/compiler/dev-source.ts +87 -0
- package/src/compiler/dom-attrs.ts +138 -65
- package/src/compiler/dom.ts +517 -442
- package/src/compiler/expr-context.ts +260 -81
- package/src/compiler/foreign-imports.ts +88 -0
- package/src/compiler/mistakes.ts +23 -2
- package/src/compiler/props-body.ts +60 -0
- package/src/compiler/props.ts +87 -36
- package/src/compiler/scope.ts +400 -333
- package/src/compiler/server-only.ts +190 -151
- package/src/compiler/shared.ts +42 -14
- package/src/compiler/signal-reads.ts +6 -2
- package/src/compiler/spread-handlers.ts +295 -0
- package/src/compiler/ssr-prunelle-tags.ts +27 -14
- package/src/compiler/ssr.ts +220 -89
- package/src/context.ts +34 -0
- package/src/dev/error-log.ts +177 -0
- package/src/dev/host-check.ts +32 -0
- package/src/dev/index.ts +94 -34
- package/src/dev/open-editor.ts +228 -0
- package/src/dev/protocol.ts +3 -0
- package/src/dev/reload.ts +5 -3
- package/src/dev/supervisor.ts +50 -25
- package/src/dev/watchers.ts +74 -13
- package/src/dom/action.ts +47 -21
- package/src/dom/attrs.ts +15 -3
- package/src/dom/extras.ts +57 -40
- package/src/dom/index.ts +54 -14
- package/src/dom/island.ts +102 -68
- package/src/dom/loop.ts +66 -32
- package/src/eco/declaration.ts +328 -0
- package/src/eco/index.ts +69 -0
- package/src/index.ts +2 -2
- package/src/jsx-runtime.ts +2 -2
- package/src/plugins/config.ts +18 -12
- package/src/plugins/context.ts +44 -1
- package/src/plugins/index.ts +2 -2
- package/src/plugins/load.ts +30 -10
- package/src/plugins/order.ts +38 -1
- package/src/plugins/public.ts +6 -3
- package/src/plugins/services.ts +84 -0
- package/src/plugins/types.ts +51 -2
- package/src/reactivity/graph.ts +10 -0
- package/src/reactivity/index.ts +24 -16
- package/src/reactivity/selector.ts +31 -4
- package/src/reactivity/store.ts +64 -20
- package/src/server/action-flash.ts +158 -0
- package/src/server/action-protect.ts +199 -0
- package/src/server/action-registry.ts +1 -1
- package/src/server/action.ts +188 -152
- package/src/server/app-secret.ts +63 -0
- package/src/server/assets.ts +34 -2
- package/src/server/compress.ts +96 -10
- package/src/server/consent.ts +59 -22
- package/src/server/cookies.ts +16 -2
- package/src/server/document.ts +25 -10
- package/src/server/env.ts +14 -0
- package/src/server/errors.ts +308 -65
- package/src/server/form-schema.ts +337 -0
- package/src/server/health.ts +275 -0
- package/src/server/http.ts +186 -39
- package/src/server/index.ts +272 -273
- package/src/server/loader.ts +15 -2
- package/src/server/page.ts +15 -0
- package/src/server/param-matchers.ts +112 -0
- package/src/server/plugin-host.ts +125 -88
- package/src/server/public.ts +6 -5
- package/src/server/rate-limit.ts +102 -36
- package/src/server/request-body.ts +1 -1
- package/src/server/route-cache.ts +541 -0
- package/src/server/route-table.ts +356 -11
- package/src/server/router.ts +125 -40
- package/src/server/routing.ts +112 -63
- package/src/server/sanitize-html.ts +309 -0
- package/src/server/security.ts +279 -52
- package/src/server/serve-handlers.ts +31 -7
- package/src/server/standard-schema.ts +17 -0
- package/src/server/start.ts +60 -16
- package/src/server/static-dir.ts +91 -12
- package/src/server/stream.ts +68 -27
- package/src/ssr/consent-banner.ts +58 -0
- package/src/ssr/context.ts +106 -8
- package/src/ssr/index.ts +151 -4
- package/src/ssr/navigation.ts +2 -2
- package/src/ssr/region.ts +40 -19
- package/src/style.ts +64 -1
- package/src/test/browser.ts +28 -13
- package/src/test/index.ts +97 -12
- package/src/ts-plugin/diagnostics.ts +505 -0
- package/src/ts-plugin/fixes.ts +364 -0
- package/src/ts-plugin/index.ts +194 -0
- package/templates/_shared/AGENTS.md +2 -2
- package/templates/_shared/gitignore +5 -0
- package/templates/_shared/mcp.json +11 -0
- package/templates/_shared/src/routes/legal.tsx +15 -1
- package/templates/_shared/tsconfig.json +5 -0
- package/templates/app/.env.example +4 -0
- package/templates/app/package.json +2 -1
- package/templates/app/prunelle.config.ts +2 -0
- package/templates/app/src/auth.ts +4 -6
- package/templates/app/src/mail.ts +4 -0
- package/templates/app/src/routes/_layout.tsx +9 -1
- package/templates/app/src/routes/dashboard/index.tsx +42 -8
- package/templates/app/src/routes/login.tsx +49 -30
- package/templates/app/src/routes/privacy.tsx +25 -0
- package/templates/app/src/styles/app.css +8 -1
- package/templates/app/tests/app.test.ts +44 -3
- package/templates/blog/package.json +1 -1
- package/templates/blog/prunelle.config.ts +1 -1
- package/templates/blog/src/routes/_layout.tsx +6 -1
- package/templates/blog/src/routes/index.tsx +5 -1
- package/templates/blog/src/styles/app.css +6 -1
- package/templates/fullstack/package.json +1 -1
- package/templates/fullstack/src/components/GreetForm.tsx +2 -1
- package/templates/fullstack/src/routes/_layout.tsx +4 -0
- package/templates/fullstack/src/styles/app.css +11 -1
- package/templates/minimal/package.json +1 -1
- package/templates/minimal/src/routes/_layout.tsx +4 -0
- package/templates/minimal/src/styles/app.css +11 -1
- package/ts-plugin.cjs +4277 -0
|
@@ -1,22 +1,50 @@
|
|
|
1
1
|
// French web law compliance checks (RGAA, RGPD, LCEN): static TSX/CSS warnings and route-level checks with explicit silencing.
|
|
2
2
|
|
|
3
|
-
import type {
|
|
3
|
+
import type { JSXElement, Node, Program } from 'oxc-parser'
|
|
4
4
|
import type { ModuleAnalysis } from './analyze'
|
|
5
|
+
import type { EcoJsxRule } from './compliance-eco'
|
|
6
|
+
import type { RgaaJsxRule, RgaaScope } from './compliance-rgaa'
|
|
5
7
|
import type { CompilerWarning } from './mistakes'
|
|
6
8
|
import { existsSync, readFileSync } from 'node:fs'
|
|
7
|
-
import { basename } from 'node:path'
|
|
8
|
-
import { visitorKeys } from 'oxc-parser'
|
|
9
|
+
import { basename, join } from 'node:path'
|
|
10
|
+
import { parseSync, visitorKeys } from 'oxc-parser'
|
|
9
11
|
import { relPosix, walkFiles } from '../build/files'
|
|
10
12
|
import { fileToRoutePath } from '../build/routes'
|
|
11
|
-
import {
|
|
13
|
+
import { walk } from './ast'
|
|
14
|
+
import {
|
|
15
|
+
attrTargetKey,
|
|
16
|
+
checkRgaaComponent,
|
|
17
|
+
checkRgaaElement,
|
|
18
|
+
cloneRgaaScope,
|
|
19
|
+
createRgaaScope,
|
|
20
|
+
findAttr,
|
|
21
|
+
hasNonEmptyAttr,
|
|
22
|
+
INTERACTIVE_ANCESTORS,
|
|
23
|
+
mergeRgaaScope,
|
|
24
|
+
RGAA_JSX_RULES,
|
|
25
|
+
staticStringValueOf,
|
|
26
|
+
} from './compliance-rgaa'
|
|
27
|
+
import { STATEMENT_PLACEHOLDER } from '../a11y/declaration'
|
|
28
|
+
import { checkEcoElement, ECO_JSX_RULES, largePublicFileWarnings } from './compliance-eco'
|
|
29
|
+
import { CSS_IMPORT_RE, CSS_URL_RE, parseOrigin, thirdPartyLoads, thirdPartyOrigin } from './compliance-urls'
|
|
30
|
+
import { formatDiagnostic, formatLoc, getLineStarts, isComponentTag, jsxTagName, lineIndexAtOffset, unwrapTsExpr } from './shared'
|
|
12
31
|
|
|
13
32
|
export type ComplianceRule =
|
|
14
|
-
|
|
|
15
|
-
|
|
|
16
|
-
| '
|
|
33
|
+
| RgaaJsxRule
|
|
34
|
+
| EcoJsxRule
|
|
35
|
+
| 'rgesn/large-public-file'
|
|
36
|
+
| 'rgesn/declaration'
|
|
37
|
+
| 'rgesn/island-by-value'
|
|
17
38
|
| 'rgpd/third-party-url'
|
|
39
|
+
| 'rgpd/consent-retention'
|
|
40
|
+
| 'rgpd/privacy-notice'
|
|
18
41
|
| 'lcen/legal-notice'
|
|
42
|
+
| 'rgaa/document-lang'
|
|
19
43
|
| 'rgaa/accessibility-statement'
|
|
44
|
+
| FormWarningRule
|
|
45
|
+
|
|
46
|
+
/** The warnings of the form-vs-action check (`cli/check-forms.ts`), silenced like the compliance rules. */
|
|
47
|
+
export type FormWarningRule = 'action/unguarded-public-form' | 'form/invalid-option' | 'form/schema-unsupported' | 'form/unchecked'
|
|
20
48
|
|
|
21
49
|
export interface ComplianceConfig {
|
|
22
50
|
/** Compliance rules to silence across the app. */
|
|
@@ -25,57 +53,60 @@ export interface ComplianceConfig {
|
|
|
25
53
|
publicBody?: boolean | undefined
|
|
26
54
|
/** Custom route path for the legal notice page, or `false` to silence `lcen/legal-notice`. */
|
|
27
55
|
legalNotice?: string | false | undefined
|
|
56
|
+
/** Custom route path for the privacy notice page, or `false` to silence `rgpd/privacy-notice`. */
|
|
57
|
+
privacyNotice?: string | false | undefined
|
|
28
58
|
}
|
|
29
59
|
|
|
30
60
|
const COMPLIANCE_RULES = new Set<ComplianceRule>([
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
'
|
|
61
|
+
...RGAA_JSX_RULES,
|
|
62
|
+
...ECO_JSX_RULES,
|
|
63
|
+
'rgesn/large-public-file',
|
|
64
|
+
'rgesn/declaration',
|
|
65
|
+
'rgesn/island-by-value',
|
|
34
66
|
'rgpd/third-party-url',
|
|
67
|
+
'rgpd/consent-retention',
|
|
68
|
+
'rgpd/privacy-notice',
|
|
35
69
|
'lcen/legal-notice',
|
|
70
|
+
'rgaa/document-lang',
|
|
36
71
|
'rgaa/accessibility-statement',
|
|
72
|
+
'action/unguarded-public-form',
|
|
73
|
+
'form/invalid-option',
|
|
74
|
+
'form/schema-unsupported',
|
|
75
|
+
'form/unchecked',
|
|
37
76
|
])
|
|
38
77
|
|
|
39
|
-
|
|
40
|
-
const
|
|
41
|
-
const
|
|
42
|
-
const DEFAULT_LEGAL_PATHS = new Set(['/mentions-legales', '/legal', '/legal-notice', '/imprint', '/impressum'
|
|
78
|
+
// The CNIL recommends keeping a cookie choice, consent or refusal, 6 months before asking again.
|
|
79
|
+
const CNIL_CHOICE_MAX_AGE = 15_552_000
|
|
80
|
+
const INLINE_MAP_METHODS = new Set(['map', 'flatMap'])
|
|
81
|
+
const DEFAULT_LEGAL_PATHS = new Set(['/mentions-legales', '/legal', '/legal-notice', '/imprint', '/impressum'])
|
|
82
|
+
const DEFAULT_PRIVACY_PATHS = new Set(['/privacy', '/privacy-policy', '/confidentialite', '/politique-de-confidentialite', '/donnees-personnelles', '/datenschutz'])
|
|
83
|
+
// A component named Label (or ui.Label) wraps its control like <label>.
|
|
84
|
+
const LABEL_COMPONENT_RE = /(?:^|\.)Label$/
|
|
85
|
+
// `<input>` type/name values that collect an email address or a phone number.
|
|
86
|
+
const PERSONAL_DATA_TYPES = new Set(['email', 'tel'])
|
|
87
|
+
const PERSONAL_DATA_NAMES = new Set(['email', 'phone', 'tel'])
|
|
43
88
|
const ACCESSIBILITY_PATHS = new Set(['/accessibilite', '/accessibility', '/declaration-accessibilite', '/accessibility-statement'])
|
|
89
|
+
const ECO_DECLARATION_PATHS = new Set(['/ecoconception', '/declaration-ecoconception', '/eco-design'])
|
|
90
|
+
// The marker the `prunelle new` legal and privacy notices put in front of every detail the publisher must supply.
|
|
91
|
+
const TEMPLATE_PLACEHOLDER = '[to fill in'
|
|
92
|
+
|
|
93
|
+
interface NoticeDetails { rule: ComplianceRule, page: string, law: string, marker: string }
|
|
94
|
+
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 }
|
|
95
|
+
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 }
|
|
96
|
+
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: STATEMENT_PLACEHOLDER }
|
|
97
|
+
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: STATEMENT_PLACEHOLDER }
|
|
98
|
+
const LOCALE_PREFIX_RE = /^\/(?:[a-z]{2}(?:-[a-z]{2})?|:[a-z_]\w*(?:=[^/]+)?\??|\[\[?[^\]]+\]\]?)(\/.+)$/i
|
|
44
99
|
|
|
45
100
|
const LINE_IGNORE_RE = /(?:^|[\s{;(,])\/\/\s*prunelle-ignore(-file)?[ \t]+([^\r\n]+)/g
|
|
46
101
|
const BLOCK_IGNORE_RE = /\/\*\s*prunelle-ignore(-file)?\s+([\s\S]*?)\*\//g
|
|
47
102
|
const CSS_COMMENT_RE = /\/\*[\s\S]*?\*\//g
|
|
48
|
-
const CSS_IMPORT_RE = /@import\s+(?:url\(\s*)?(?:'([^']+)'|"([^"]+)"|([^'"\s);]+))/gi
|
|
49
|
-
const CSS_URL_RE = /url\(\s*(?:'([^']+)'|"([^"]+)"|([^'"\s)]+))\s*\)/gi
|
|
50
103
|
|
|
51
104
|
interface IgnoreMap {
|
|
52
|
-
lineStarts: number[]
|
|
105
|
+
lineStarts: readonly number[]
|
|
53
106
|
fileRules: Set<ComplianceRule>
|
|
54
107
|
lineRules: Map<number, Set<ComplianceRule>>
|
|
55
108
|
}
|
|
56
109
|
|
|
57
|
-
function buildLineStarts(code: string): number[] {
|
|
58
|
-
const starts = [0]
|
|
59
|
-
for (let i = 0; i < code.length; i++) {
|
|
60
|
-
if (code.charCodeAt(i) === 10)
|
|
61
|
-
starts.push(i + 1)
|
|
62
|
-
}
|
|
63
|
-
return starts
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
function offsetToLine(lineStarts: readonly number[], offset: number): number {
|
|
67
|
-
let lo = 0
|
|
68
|
-
let hi = lineStarts.length - 1
|
|
69
|
-
while (lo < hi) {
|
|
70
|
-
const mid = (lo + hi + 1) >> 1
|
|
71
|
-
if (lineStarts[mid]! <= offset)
|
|
72
|
-
lo = mid
|
|
73
|
-
else
|
|
74
|
-
hi = mid - 1
|
|
75
|
-
}
|
|
76
|
-
return lo + 1
|
|
77
|
-
}
|
|
78
|
-
|
|
79
110
|
function extractRules(text: string): ComplianceRule[] {
|
|
80
111
|
const out: ComplianceRule[] = []
|
|
81
112
|
for (const token of text.trim().split(/[\s,]+/)) {
|
|
@@ -86,7 +117,7 @@ function extractRules(text: string): ComplianceRule[] {
|
|
|
86
117
|
}
|
|
87
118
|
|
|
88
119
|
export function parsePrunelleIgnores(code: string): IgnoreMap {
|
|
89
|
-
const lineStarts =
|
|
120
|
+
const lineStarts = getLineStarts(code)
|
|
90
121
|
const fileRules = new Set<ComplianceRule>()
|
|
91
122
|
const lineRules = new Map<number, Set<ComplianceRule>>()
|
|
92
123
|
|
|
@@ -99,8 +130,8 @@ export function parsePrunelleIgnores(code: string): IgnoreMap {
|
|
|
99
130
|
fileRules.add(r)
|
|
100
131
|
return
|
|
101
132
|
}
|
|
102
|
-
const startLine =
|
|
103
|
-
const endLine =
|
|
133
|
+
const startLine = lineIndexAtOffset(lineStarts, start) + 1
|
|
134
|
+
const endLine = lineIndexAtOffset(lineStarts, end) + 1
|
|
104
135
|
for (const line of [startLine, endLine, endLine + 1]) {
|
|
105
136
|
let set = lineRules.get(line)
|
|
106
137
|
if (set === undefined) {
|
|
@@ -120,49 +151,21 @@ export function parsePrunelleIgnores(code: string): IgnoreMap {
|
|
|
120
151
|
return { lineStarts, fileRules, lineRules }
|
|
121
152
|
}
|
|
122
153
|
|
|
123
|
-
function isIgnoredAt(ignores: IgnoreMap, rule: ComplianceRule, offset: number): boolean {
|
|
154
|
+
export function isIgnoredAt(ignores: IgnoreMap, rule: ComplianceRule, offset: number): boolean {
|
|
124
155
|
if (ignores.fileRules.has(rule))
|
|
125
156
|
return true
|
|
126
|
-
const line =
|
|
157
|
+
const line = lineIndexAtOffset(ignores.lineStarts, offset) + 1
|
|
127
158
|
return ignores.lineRules.get(line)?.has(rule) === true
|
|
128
159
|
}
|
|
129
160
|
|
|
130
161
|
export function isRuleIgnored(rule: ComplianceRule, compliance?: ComplianceConfig | undefined): boolean {
|
|
131
162
|
if (rule === 'lcen/legal-notice' && compliance?.legalNotice === false)
|
|
132
163
|
return true
|
|
164
|
+
if (rule === 'rgpd/privacy-notice' && compliance?.privacyNotice === false)
|
|
165
|
+
return true
|
|
133
166
|
return compliance?.ignore?.includes(rule) === true
|
|
134
167
|
}
|
|
135
168
|
|
|
136
|
-
export function parseOrigin(url: string | undefined): string | undefined {
|
|
137
|
-
if (url === undefined || url.trim() === '')
|
|
138
|
-
return undefined
|
|
139
|
-
try {
|
|
140
|
-
return new URL(url.trim()).origin
|
|
141
|
-
}
|
|
142
|
-
catch {
|
|
143
|
-
return undefined
|
|
144
|
-
}
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
/** Returns the external origin when `rawUrl` points to a non-localhost third-party URL, or `undefined` otherwise. */
|
|
148
|
-
export function thirdPartyOrigin(rawUrl: string): string | undefined {
|
|
149
|
-
const trimmed = rawUrl.trim()
|
|
150
|
-
if (!/^(?:https?:)?\/\//i.test(trimmed))
|
|
151
|
-
return undefined
|
|
152
|
-
try {
|
|
153
|
-
const parsed = new URL(trimmed.startsWith('//') ? `https:${trimmed}` : trimmed)
|
|
154
|
-
if (parsed.protocol !== 'http:' && parsed.protocol !== 'https:')
|
|
155
|
-
return undefined
|
|
156
|
-
const host = parsed.hostname.toLowerCase()
|
|
157
|
-
if (LOCAL_HOSTS.has(host) || host.endsWith('.localhost'))
|
|
158
|
-
return undefined
|
|
159
|
-
return parsed.origin
|
|
160
|
-
}
|
|
161
|
-
catch {
|
|
162
|
-
return undefined
|
|
163
|
-
}
|
|
164
|
-
}
|
|
165
|
-
|
|
166
169
|
/** Whether a compiler warning should be surfaced given `compliance` and `site` config. */
|
|
167
170
|
export function shouldEmitWarning(warning: CompilerWarning, compliance?: ComplianceConfig | undefined, site?: string | undefined): boolean {
|
|
168
171
|
if (warning.rule !== undefined && isRuleIgnored(warning.rule, compliance))
|
|
@@ -175,174 +178,69 @@ export function shouldEmitWarning(warning: CompilerWarning, compliance?: Complia
|
|
|
175
178
|
return true
|
|
176
179
|
}
|
|
177
180
|
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
return undefined
|
|
184
|
-
}
|
|
185
|
-
|
|
186
|
-
function hasSpread(attributes: readonly JSXAttributeItem[]): boolean {
|
|
187
|
-
return attributes.some(a => a.type === 'JSXSpreadAttribute')
|
|
188
|
-
}
|
|
189
|
-
|
|
190
|
-
function staticStringValueOf(value: JSXAttribute['value']): string | undefined {
|
|
191
|
-
if (value === null)
|
|
192
|
-
return undefined
|
|
193
|
-
if (value.type === 'Literal' && typeof value.value === 'string')
|
|
194
|
-
return value.value
|
|
195
|
-
if (value.type === 'JSXExpressionContainer' && value.expression.type !== 'JSXEmptyExpression') {
|
|
196
|
-
const expr = unwrapTsExpr(value.expression)
|
|
197
|
-
if (expr.type === 'Literal' && typeof expr.value === 'string')
|
|
198
|
-
return expr.value
|
|
199
|
-
if (expr.type === 'TemplateLiteral' && expr.expressions.length === 0)
|
|
200
|
-
return expr.quasis[0]?.value.cooked ?? undefined
|
|
201
|
-
}
|
|
202
|
-
return undefined
|
|
203
|
-
}
|
|
204
|
-
|
|
205
|
-
function attrTargetKey(attr: JSXAttribute | undefined): string | undefined {
|
|
206
|
-
if (attr === undefined || attr.value === null)
|
|
207
|
-
return undefined
|
|
208
|
-
const str = staticStringValueOf(attr.value)
|
|
209
|
-
if (str !== undefined && str.trim() !== '')
|
|
210
|
-
return `s:${str}`
|
|
211
|
-
if (attr.value.type === 'JSXExpressionContainer' && attr.value.expression.type !== 'JSXEmptyExpression') {
|
|
212
|
-
const expr = unwrapTsExpr(attr.value.expression)
|
|
213
|
-
if (expr.type === 'Identifier')
|
|
214
|
-
return `i:${expr.name}`
|
|
215
|
-
}
|
|
216
|
-
return undefined
|
|
181
|
+
interface AncestorState {
|
|
182
|
+
labelDepth: number
|
|
183
|
+
consentGateDepth: number
|
|
184
|
+
interactiveAncestor: string | undefined
|
|
185
|
+
inLoop: boolean
|
|
217
186
|
}
|
|
218
187
|
|
|
219
|
-
function
|
|
220
|
-
|
|
221
|
-
if (attr === undefined || attr.value === null)
|
|
188
|
+
function isInlineMapCall(node: Node): node is Extract<Node, { type: 'CallExpression' }> {
|
|
189
|
+
if (node.type !== 'CallExpression' || node.arguments.length === 0)
|
|
222
190
|
return false
|
|
223
|
-
const
|
|
224
|
-
if (
|
|
225
|
-
return str.trim() !== ''
|
|
226
|
-
if (attr.value.type === 'JSXExpressionContainer' && attr.value.expression.type !== 'JSXEmptyExpression') {
|
|
227
|
-
const expr = unwrapParens(attr.value.expression)
|
|
228
|
-
if (expr.type === 'Literal' && (expr.value === null || expr.value === false || expr.value === ''))
|
|
229
|
-
return false
|
|
230
|
-
if (expr.type === 'Identifier' && expr.name === 'undefined')
|
|
231
|
-
return false
|
|
232
|
-
return true
|
|
233
|
-
}
|
|
234
|
-
return false
|
|
235
|
-
}
|
|
236
|
-
|
|
237
|
-
function hasLabelAttr(attributes: readonly JSXAttributeItem[]): boolean {
|
|
238
|
-
return hasNonEmptyAttr(attributes, 'aria-label')
|
|
239
|
-
|| hasNonEmptyAttr(attributes, 'aria-labelledby')
|
|
240
|
-
|| hasNonEmptyAttr(attributes, 'title')
|
|
241
|
-
}
|
|
242
|
-
|
|
243
|
-
function isAriaHidden(attributes: readonly JSXAttributeItem[]): boolean {
|
|
244
|
-
const attr = findAttr(attributes, 'aria-hidden')
|
|
245
|
-
if (attr === undefined)
|
|
191
|
+
const callee = unwrapTsExpr(node.callee)
|
|
192
|
+
if (callee.type !== 'MemberExpression' || callee.computed || callee.property.type !== 'Identifier' || !INLINE_MAP_METHODS.has(callee.property.name))
|
|
246
193
|
return false
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
const expr = unwrapParens(attr.value.expression)
|
|
253
|
-
return expr.type === 'Literal' && (expr.value === true || expr.value === 'true')
|
|
254
|
-
}
|
|
255
|
-
return false
|
|
194
|
+
const first = node.arguments[0]!
|
|
195
|
+
if (first.type === 'SpreadElement')
|
|
196
|
+
return false
|
|
197
|
+
const cb = unwrapTsExpr(first)
|
|
198
|
+
return cb.type === 'ArrowFunctionExpression' || cb.type === 'FunctionExpression'
|
|
256
199
|
}
|
|
257
200
|
|
|
258
|
-
function
|
|
259
|
-
|
|
260
|
-
if (inner.type === 'Literal')
|
|
261
|
-
return typeof inner.value === 'string' ? inner.value.trim() !== '' : typeof inner.value === 'number'
|
|
262
|
-
if (inner.type === 'Identifier' && inner.name === 'undefined')
|
|
263
|
-
return false
|
|
264
|
-
return true
|
|
201
|
+
function isFunctionNode(node: Node): boolean {
|
|
202
|
+
return node.type === 'FunctionDeclaration' || node.type === 'FunctionExpression' || node.type === 'ArrowFunctionExpression'
|
|
265
203
|
}
|
|
266
204
|
|
|
267
|
-
function
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
205
|
+
// Collects `<label for / htmlFor>` targets owned by the current component/function (including inline `.map()` callbacks, excluding nested functions).
|
|
206
|
+
function collectDirectLabelTargets(root: Node | null | undefined): Set<string> {
|
|
207
|
+
const targets = new Set<string>()
|
|
208
|
+
const walkScope = (value: unknown, isRoot: boolean): void => {
|
|
209
|
+
if (value === null || typeof value !== 'object')
|
|
210
|
+
return
|
|
211
|
+
if (Array.isArray(value)) {
|
|
212
|
+
for (let i = 0; i < value.length; i++)
|
|
213
|
+
walkScope(value[i], false)
|
|
214
|
+
return
|
|
272
215
|
}
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
216
|
+
const node = value as Node
|
|
217
|
+
if (typeof node.type !== 'string')
|
|
218
|
+
return
|
|
219
|
+
if (!isRoot && isFunctionNode(node))
|
|
220
|
+
return
|
|
221
|
+
if (isInlineMapCall(node)) {
|
|
222
|
+
walkScope(node.callee, false)
|
|
223
|
+
const cb = unwrapTsExpr(node.arguments[0]!)
|
|
224
|
+
if (cb.type === 'ArrowFunctionExpression' || cb.type === 'FunctionExpression')
|
|
225
|
+
walkScope(cb.body, false)
|
|
226
|
+
for (let i = 1; i < node.arguments.length; i++)
|
|
227
|
+
walkScope(node.arguments[i], false)
|
|
228
|
+
return
|
|
276
229
|
}
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
230
|
+
if (node.type === 'JSXElement' && jsxTagName(node.openingElement.name) === 'label') {
|
|
231
|
+
const attrs = node.openingElement.attributes
|
|
232
|
+
const key = attrTargetKey(findAttr(attrs, 'for') ?? findAttr(attrs, 'htmlFor'))
|
|
233
|
+
if (key !== undefined)
|
|
234
|
+
targets.add(key)
|
|
280
235
|
}
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
if (hasSpread(attrs) || hasLabelAttr(attrs))
|
|
287
|
-
return true
|
|
288
|
-
if (tag === 'img') {
|
|
289
|
-
if (hasNonEmptyAttr(attrs, 'alt'))
|
|
290
|
-
return true
|
|
291
|
-
continue
|
|
292
|
-
}
|
|
293
|
-
if (tag === 'svg') {
|
|
294
|
-
const hasTitle = child.children.some(c =>
|
|
295
|
-
c.type === 'JSXElement' && jsxTagName(c.openingElement.name) === 'title' && hasAccessibleName(c.children),
|
|
296
|
-
)
|
|
297
|
-
if (hasTitle)
|
|
298
|
-
return true
|
|
299
|
-
continue
|
|
300
|
-
}
|
|
301
|
-
if (isComponentTag(tag)) {
|
|
302
|
-
if (hasNonEmptyAttr(attrs, 'label') || hasAccessibleName(child.children))
|
|
303
|
-
return true
|
|
304
|
-
continue
|
|
305
|
-
}
|
|
306
|
-
if (hasAccessibleName(child.children))
|
|
307
|
-
return true
|
|
236
|
+
const keys = visitorKeys[node.type]
|
|
237
|
+
if (keys !== undefined) {
|
|
238
|
+
const rec = node as unknown as Record<string, unknown>
|
|
239
|
+
for (let i = 0; i < keys.length; i++)
|
|
240
|
+
walkScope(rec[keys[i]!], false)
|
|
308
241
|
}
|
|
309
242
|
}
|
|
310
|
-
|
|
311
|
-
}
|
|
312
|
-
|
|
313
|
-
function walkAst(value: unknown, enter: (node: Node) => void, exit?: (node: Node) => void): void {
|
|
314
|
-
if (value === null || typeof value !== 'object')
|
|
315
|
-
return
|
|
316
|
-
if (Array.isArray(value)) {
|
|
317
|
-
for (let i = 0; i < value.length; i++)
|
|
318
|
-
walkAst(value[i], enter, exit)
|
|
319
|
-
return
|
|
320
|
-
}
|
|
321
|
-
const node = value as Node
|
|
322
|
-
if (typeof node.type !== 'string')
|
|
323
|
-
return
|
|
324
|
-
enter(node)
|
|
325
|
-
const keys = visitorKeys[node.type]
|
|
326
|
-
if (keys !== undefined) {
|
|
327
|
-
for (let i = 0; i < keys.length; i++)
|
|
328
|
-
walkAst((node as unknown as Record<string, unknown>)[keys[i]!], enter, exit)
|
|
329
|
-
}
|
|
330
|
-
exit?.(node)
|
|
331
|
-
}
|
|
332
|
-
|
|
333
|
-
function collectLabelTargets(program: Program): Set<string> {
|
|
334
|
-
const targets = new Set<string>()
|
|
335
|
-
walkAst(program, (node) => {
|
|
336
|
-
if (node.type !== 'JSXElement')
|
|
337
|
-
return
|
|
338
|
-
const tag = jsxTagName(node.openingElement.name)
|
|
339
|
-
if (tag !== 'label')
|
|
340
|
-
return
|
|
341
|
-
const attrs = node.openingElement.attributes
|
|
342
|
-
const key = attrTargetKey(findAttr(attrs, 'for') ?? findAttr(attrs, 'htmlFor'))
|
|
343
|
-
if (key !== undefined)
|
|
344
|
-
targets.add(key)
|
|
345
|
-
})
|
|
243
|
+
walkScope(root, true)
|
|
346
244
|
return targets
|
|
347
245
|
}
|
|
348
246
|
|
|
@@ -353,85 +251,31 @@ function checkThirdPartyElement(
|
|
|
353
251
|
ignores: IgnoreMap,
|
|
354
252
|
warnings: CompilerWarning[],
|
|
355
253
|
): void {
|
|
356
|
-
const
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
if (tag === 'script' || tag === 'iframe') {
|
|
360
|
-
attr = findAttr(attrs, 'src')
|
|
361
|
-
attrName = 'src'
|
|
362
|
-
}
|
|
363
|
-
else if (tag === 'link') {
|
|
364
|
-
const relVal = staticStringValueOf(findAttr(attrs, 'rel')?.value ?? null)
|
|
365
|
-
if (relVal === undefined)
|
|
366
|
-
return
|
|
367
|
-
const rels = relVal.toLowerCase().split(/\s+/)
|
|
368
|
-
if (!rels.some(r => THIRD_PARTY_LINK_RELS.has(r)))
|
|
369
|
-
return
|
|
370
|
-
attr = findAttr(attrs, 'href')
|
|
371
|
-
attrName = 'href'
|
|
372
|
-
}
|
|
373
|
-
if (attr === undefined)
|
|
374
|
-
return
|
|
375
|
-
const url = staticStringValueOf(attr.value)
|
|
376
|
-
if (url === undefined)
|
|
377
|
-
return
|
|
378
|
-
const origin = thirdPartyOrigin(url)
|
|
379
|
-
if (origin === undefined || isIgnoredAt(ignores, 'rgpd/third-party-url', attr.start))
|
|
380
|
-
return
|
|
381
|
-
warnings.push({
|
|
382
|
-
rule: 'rgpd/third-party-url',
|
|
383
|
-
urlOrigin: origin,
|
|
384
|
-
start: attr.start,
|
|
385
|
-
message: `[rgpd/third-party-url] <${tag} ${attrName}="${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="...">.`,
|
|
386
|
-
})
|
|
387
|
-
}
|
|
388
|
-
|
|
389
|
-
function checkJsxElementCompliance(
|
|
390
|
-
element: JSXElement,
|
|
391
|
-
filename: string,
|
|
392
|
-
inLabel: boolean,
|
|
393
|
-
inConsentGate: boolean,
|
|
394
|
-
labelTargets: ReadonlySet<string>,
|
|
395
|
-
ignores: IgnoreMap,
|
|
396
|
-
warnings: CompilerWarning[],
|
|
397
|
-
): void {
|
|
398
|
-
const tag = jsxTagName(element.openingElement.name)
|
|
399
|
-
if (tag === '' || isComponentTag(tag))
|
|
400
|
-
return
|
|
401
|
-
const attrs = element.openingElement.attributes
|
|
402
|
-
const spread = hasSpread(attrs)
|
|
403
|
-
|
|
404
|
-
if (tag === 'img' && !spread && findAttr(attrs, 'alt') === undefined && !isIgnoredAt(ignores, 'rgaa/img-alt', element.start)) {
|
|
254
|
+
for (const load of thirdPartyLoads(element, tag)) {
|
|
255
|
+
if (isIgnoredAt(ignores, 'rgpd/third-party-url', load.start))
|
|
256
|
+
continue
|
|
405
257
|
warnings.push({
|
|
406
|
-
rule: '
|
|
407
|
-
|
|
408
|
-
|
|
258
|
+
rule: 'rgpd/third-party-url',
|
|
259
|
+
urlOrigin: load.origin,
|
|
260
|
+
start: load.start,
|
|
261
|
+
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="...">.`,
|
|
409
262
|
})
|
|
410
263
|
}
|
|
264
|
+
}
|
|
411
265
|
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
if ((inputType === undefined || !NON_LABELLED_INPUT_TYPES.has(inputType)) && !linked && !isIgnoredAt(ignores, 'rgaa/form-label', element.start)) {
|
|
417
|
-
warnings.push({
|
|
418
|
-
rule: 'rgaa/form-label',
|
|
419
|
-
start: element.start,
|
|
420
|
-
message: `[rgaa/form-label] <${tag}> in ${filename} has no associated label: wrap it in <label>, link <label for="id">, or add aria-label="..." / aria-labelledby="...".`,
|
|
421
|
-
})
|
|
422
|
-
}
|
|
423
|
-
}
|
|
424
|
-
|
|
425
|
-
if ((tag === 'button' || tag === 'a') && !spread && !hasLabelAttr(attrs) && !hasAccessibleName(element.children) && !isIgnoredAt(ignores, 'rgaa/accessible-name', element.start)) {
|
|
426
|
-
warnings.push({
|
|
427
|
-
rule: 'rgaa/accessible-name',
|
|
428
|
-
start: element.start,
|
|
429
|
-
message: `[rgaa/accessible-name] <${tag}> in ${filename} has no accessible name: add visible text or aria-label="...".`,
|
|
430
|
-
})
|
|
431
|
-
}
|
|
266
|
+
// A gate on the essential category always renders its children, so it holds nothing back until consent.
|
|
267
|
+
function opensConsentGate(element: JSXElement, resolvedTag: string | undefined): boolean {
|
|
268
|
+
return resolvedTag === 'ConsentGate' && staticStringValueOf(findAttr(element.openingElement.attributes, 'category')?.value ?? null) !== 'essential'
|
|
269
|
+
}
|
|
432
270
|
|
|
433
|
-
|
|
434
|
-
|
|
271
|
+
function definitelyExits(stmt: Node): boolean {
|
|
272
|
+
if (stmt.type === 'ReturnStatement' || stmt.type === 'ThrowStatement')
|
|
273
|
+
return true
|
|
274
|
+
if (stmt.type === 'BlockStatement')
|
|
275
|
+
return stmt.body.length > 0 && definitelyExits(stmt.body[stmt.body.length - 1]!)
|
|
276
|
+
if (stmt.type === 'IfStatement')
|
|
277
|
+
return stmt.alternate !== null && definitelyExits(stmt.consequent) && definitelyExits(stmt.alternate)
|
|
278
|
+
return false
|
|
435
279
|
}
|
|
436
280
|
|
|
437
281
|
/** Runs RGAA & RGPD static JSX compliance checks on `program`, appending warnings (never throwing). */
|
|
@@ -443,36 +287,126 @@ export function checkCompliance(
|
|
|
443
287
|
resolveTag: (tag: string) => string | undefined,
|
|
444
288
|
): void {
|
|
445
289
|
const ignores = parsePrunelleIgnores(code)
|
|
446
|
-
const
|
|
447
|
-
|
|
448
|
-
let consentGateDepth = 0
|
|
449
|
-
const labelFlags: boolean[] = []
|
|
450
|
-
const consentFlags: boolean[] = []
|
|
290
|
+
const isIgnored = (rule: RgaaJsxRule | EcoJsxRule, elementStart: number, attrStart?: number): boolean =>
|
|
291
|
+
isIgnoredAt(ignores, rule, elementStart) || (attrStart !== undefined && isIgnoredAt(ignores, rule, attrStart))
|
|
451
292
|
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
(
|
|
455
|
-
|
|
456
|
-
|
|
293
|
+
const visitChildren = (node: Node, ancestors: AncestorState, scope: RgaaScope, labelTargets: ReadonlySet<string>): void => {
|
|
294
|
+
const keys = visitorKeys[node.type]
|
|
295
|
+
if (keys === undefined)
|
|
296
|
+
return
|
|
297
|
+
const rec = node as unknown as Record<string, unknown>
|
|
298
|
+
for (let i = 0; i < keys.length; i++)
|
|
299
|
+
visit(rec[keys[i]!], ancestors, scope, labelTargets)
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
const visit = (value: unknown, ancestors: AncestorState, scope: RgaaScope, labelTargets: ReadonlySet<string>): void => {
|
|
303
|
+
if (value === null || typeof value !== 'object')
|
|
304
|
+
return
|
|
305
|
+
if (Array.isArray(value)) {
|
|
306
|
+
for (let i = 0; i < value.length; i++)
|
|
307
|
+
visit(value[i], ancestors, scope, labelTargets)
|
|
308
|
+
return
|
|
309
|
+
}
|
|
310
|
+
const node = value as Node
|
|
311
|
+
if (typeof node.type !== 'string')
|
|
312
|
+
return
|
|
313
|
+
|
|
314
|
+
if (isInlineMapCall(node)) {
|
|
315
|
+
visit(node.callee, ancestors, scope, labelTargets)
|
|
316
|
+
const cb = unwrapTsExpr(node.arguments[0]!)
|
|
317
|
+
if (cb.type === 'ArrowFunctionExpression' || cb.type === 'FunctionExpression') {
|
|
318
|
+
const loopAncestors: AncestorState = ancestors.inLoop ? ancestors : { ...ancestors, inLoop: true }
|
|
319
|
+
visitChildren(cb, loopAncestors, createRgaaScope(), labelTargets)
|
|
320
|
+
}
|
|
321
|
+
for (let i = 1; i < node.arguments.length; i++)
|
|
322
|
+
visit(node.arguments[i], ancestors, scope, labelTargets)
|
|
323
|
+
return
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
if (isFunctionNode(node)) {
|
|
327
|
+
const resetAncestors: AncestorState = {
|
|
328
|
+
labelDepth: 0,
|
|
329
|
+
consentGateDepth: ancestors.consentGateDepth,
|
|
330
|
+
interactiveAncestor: undefined,
|
|
331
|
+
inLoop: false,
|
|
332
|
+
}
|
|
333
|
+
visitChildren(node, resetAncestors, createRgaaScope(), collectDirectLabelTargets(node))
|
|
334
|
+
return
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
if (node.type === 'ConditionalExpression') {
|
|
338
|
+
visit(node.test, ancestors, scope, labelTargets)
|
|
339
|
+
const branchA = cloneRgaaScope(scope)
|
|
340
|
+
const branchB = cloneRgaaScope(scope)
|
|
341
|
+
visit(node.consequent, ancestors, branchA, labelTargets)
|
|
342
|
+
visit(node.alternate, ancestors, branchB, labelTargets)
|
|
343
|
+
mergeRgaaScope(scope, branchA)
|
|
344
|
+
mergeRgaaScope(scope, branchB)
|
|
345
|
+
return
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
if (node.type === 'IfStatement') {
|
|
349
|
+
visit(node.test, ancestors, scope, labelTargets)
|
|
350
|
+
const branchA = cloneRgaaScope(scope)
|
|
351
|
+
visit(node.consequent, ancestors, branchA, labelTargets)
|
|
352
|
+
const branchB = cloneRgaaScope(scope)
|
|
353
|
+
if (node.alternate !== null)
|
|
354
|
+
visit(node.alternate, ancestors, branchB, labelTargets)
|
|
355
|
+
if (!definitelyExits(node.consequent))
|
|
356
|
+
mergeRgaaScope(scope, branchA)
|
|
357
|
+
if (node.alternate !== null && !definitelyExits(node.alternate))
|
|
358
|
+
mergeRgaaScope(scope, branchB)
|
|
359
|
+
return
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
if (node.type === 'JSXElement') {
|
|
457
363
|
const tag = jsxTagName(node.openingElement.name)
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
364
|
+
const attrs = node.openingElement.attributes
|
|
365
|
+
if (tag !== '') {
|
|
366
|
+
const ctx = {
|
|
367
|
+
filename,
|
|
368
|
+
inLabel: ancestors.labelDepth > 0,
|
|
369
|
+
inLoop: ancestors.inLoop,
|
|
370
|
+
interactiveAncestor: ancestors.interactiveAncestor,
|
|
371
|
+
labelTargets,
|
|
372
|
+
scope,
|
|
373
|
+
isIgnored,
|
|
374
|
+
warnings,
|
|
375
|
+
}
|
|
376
|
+
if (isComponentTag(tag)) {
|
|
377
|
+
checkRgaaComponent(node, tag, ctx)
|
|
378
|
+
}
|
|
379
|
+
else {
|
|
380
|
+
checkRgaaElement(node, tag, ctx)
|
|
381
|
+
checkEcoElement(node, tag, ctx)
|
|
382
|
+
if (ancestors.consentGateDepth === 0)
|
|
383
|
+
checkThirdPartyElement(node, tag, filename, ignores, warnings)
|
|
384
|
+
}
|
|
385
|
+
}
|
|
386
|
+
visit(attrs, ancestors, scope, labelTargets)
|
|
387
|
+
const opensLabel = tag === 'label' || (isComponentTag(tag) && (LABEL_COMPONENT_RE.test(tag) || hasNonEmptyAttr(attrs, 'label')))
|
|
388
|
+
const opensGate = opensConsentGate(node, resolveTag(tag))
|
|
389
|
+
const nextInteractive = INTERACTIVE_ANCESTORS.has(tag) ? tag : ancestors.interactiveAncestor
|
|
390
|
+
const childAncestors = opensLabel || opensGate || nextInteractive !== ancestors.interactiveAncestor
|
|
391
|
+
? {
|
|
392
|
+
labelDepth: ancestors.labelDepth + (opensLabel ? 1 : 0),
|
|
393
|
+
consentGateDepth: ancestors.consentGateDepth + (opensGate ? 1 : 0),
|
|
394
|
+
interactiveAncestor: nextInteractive,
|
|
395
|
+
inLoop: ancestors.inLoop,
|
|
396
|
+
}
|
|
397
|
+
: ancestors
|
|
398
|
+
visit(node.children, childAncestors, scope, labelTargets)
|
|
399
|
+
return
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
visitChildren(node, ancestors, scope, labelTargets)
|
|
403
|
+
}
|
|
404
|
+
|
|
405
|
+
visit(
|
|
406
|
+
program,
|
|
407
|
+
{ labelDepth: 0, consentGateDepth: 0, interactiveAncestor: undefined, inLoop: false },
|
|
408
|
+
createRgaaScope(),
|
|
409
|
+
collectDirectLabelTargets(program),
|
|
476
410
|
)
|
|
477
411
|
}
|
|
478
412
|
|
|
@@ -531,18 +465,38 @@ function normalizePath(path: string): string {
|
|
|
531
465
|
return withSlash.length > 1 && withSlash.endsWith('/') ? withSlash.slice(0, -1).toLowerCase() : withSlash.toLowerCase()
|
|
532
466
|
}
|
|
533
467
|
|
|
468
|
+
function stripLocalePrefix(normalizedPath: string): string {
|
|
469
|
+
const m = LOCALE_PREFIX_RE.exec(normalizedPath)
|
|
470
|
+
return m !== null ? m[1]! : normalizedPath
|
|
471
|
+
}
|
|
472
|
+
|
|
473
|
+
function matchesNoticePath(routePath: string, custom: string | false | undefined, defaults: ReadonlySet<string>): boolean {
|
|
474
|
+
const path = normalizePath(routePath)
|
|
475
|
+
const bare = stripLocalePrefix(path)
|
|
476
|
+
if (typeof custom === 'string') {
|
|
477
|
+
const wanted = normalizePath(custom)
|
|
478
|
+
return path === wanted || bare === wanted
|
|
479
|
+
}
|
|
480
|
+
return defaults.has(path) || defaults.has(bare)
|
|
481
|
+
}
|
|
482
|
+
|
|
483
|
+
function isLegalNoticePath(routePath: string, compliance: ComplianceConfig | undefined): boolean {
|
|
484
|
+
return matchesNoticePath(routePath, compliance?.legalNotice, DEFAULT_LEGAL_PATHS)
|
|
485
|
+
}
|
|
486
|
+
|
|
487
|
+
function isStatementPath(routePath: string): boolean {
|
|
488
|
+
return matchesNoticePath(routePath, undefined, ACCESSIBILITY_PATHS)
|
|
489
|
+
}
|
|
490
|
+
|
|
534
491
|
/** Checks project-level LCEN legal notice and RGAA accessibility statement routes. */
|
|
535
492
|
export function checkRouteCompliance(pageRoutePaths: readonly string[], compliance?: ComplianceConfig | undefined): string[] {
|
|
536
493
|
if (pageRoutePaths.length === 0)
|
|
537
494
|
return []
|
|
538
|
-
const normalized = new Set(pageRoutePaths.map(normalizePath))
|
|
539
495
|
const out: string[] = []
|
|
540
496
|
|
|
541
497
|
if (!isRuleIgnored('lcen/legal-notice', compliance)) {
|
|
542
498
|
const custom = typeof compliance?.legalNotice === 'string' ? normalizePath(compliance.legalNotice) : undefined
|
|
543
|
-
const hasLegal =
|
|
544
|
-
? normalized.has(custom)
|
|
545
|
-
: [...normalized].some(p => DEFAULT_LEGAL_PATHS.has(p))
|
|
499
|
+
const hasLegal = pageRoutePaths.some(p => isLegalNoticePath(p, compliance))
|
|
546
500
|
if (!hasLegal) {
|
|
547
501
|
const expected = custom ?? '/mentions-legales (or /legal)'
|
|
548
502
|
out.push(formatDiagnostic('prunelle.config.ts:1:1', `[lcen/legal-notice] App has no legal notice route: add ${expected}, set compliance.legalNotice, or silence with compliance: { legalNotice: false }.`, 'warning'))
|
|
@@ -550,9 +504,8 @@ export function checkRouteCompliance(pageRoutePaths: readonly string[], complian
|
|
|
550
504
|
}
|
|
551
505
|
|
|
552
506
|
if (compliance?.publicBody === true && !isRuleIgnored('rgaa/accessibility-statement', compliance)) {
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
out.push(formatDiagnostic('prunelle.config.ts:1:1', '[rgaa/accessibility-statement] Public-body site (compliance.publicBody: true) has no accessibility statement route: add /accessibilite (or /accessibility).', 'warning'))
|
|
507
|
+
if (!pageRoutePaths.some(isStatementPath)) {
|
|
508
|
+
out.push(formatDiagnostic('prunelle.config.ts:1:1', '[rgaa/accessibility-statement] Public-body site (compliance.publicBody: true) has no accessibility statement route: add /accessibilite (or /accessibility), or draft one with `prunelle a11y --draft`.', 'warning'))
|
|
556
509
|
}
|
|
557
510
|
}
|
|
558
511
|
|
|
@@ -563,26 +516,43 @@ export interface ProjectComplianceInput {
|
|
|
563
516
|
root: string
|
|
564
517
|
srcDir: string
|
|
565
518
|
routesDir: string
|
|
519
|
+
publicDir?: string | undefined
|
|
566
520
|
routeFiles: readonly string[]
|
|
567
521
|
analyses: ReadonlyMap<string, ModuleAnalysis>
|
|
568
522
|
compliance?: ComplianceConfig | undefined
|
|
523
|
+
/** `consent.maxAge` from the config, in seconds. */
|
|
524
|
+
consentMaxAge?: number | undefined
|
|
569
525
|
site?: string | undefined
|
|
526
|
+
/** `lang` from the config. */
|
|
527
|
+
lang?: string | undefined
|
|
528
|
+
/** A plugin sets `<html lang>` per request, so a missing `lang` is not a mistake. */
|
|
529
|
+
langFromPlugin?: boolean | undefined
|
|
570
530
|
}
|
|
571
531
|
|
|
572
|
-
/**
|
|
532
|
+
/** True when a plugin sets `<html lang>` from the request's locale (`@prunellejs/i18n`). */
|
|
533
|
+
export function pluginSetsLang(plugins: readonly { name: string }[]): boolean {
|
|
534
|
+
return plugins.some(p => p.name === '@prunellejs/i18n')
|
|
535
|
+
}
|
|
536
|
+
|
|
537
|
+
/** Collects project-level CSS third-party URL warnings and the LCEN, RGPD and RGAA notice warnings for `prunelle check` and production `prunelle build`. */
|
|
573
538
|
export function collectProjectComplianceWarnings(input: ProjectComplianceInput): string[] {
|
|
574
|
-
const { root, srcDir, routesDir, routeFiles, analyses, compliance, site } = input
|
|
539
|
+
const { root, srcDir, routesDir, routeFiles, analyses, compliance, consentMaxAge, site, lang, langFromPlugin } = input
|
|
540
|
+
const publicDir = input.publicDir ?? join(root, 'public')
|
|
575
541
|
const warnings: string[] = []
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
542
|
+
if (consentMaxAge !== undefined && consentMaxAge > CNIL_CHOICE_MAX_AGE && !isRuleIgnored('rgpd/consent-retention', compliance))
|
|
543
|
+
warnings.push(formatDiagnostic('prunelle.config.ts:1:1', `[rgpd/consent-retention] consent.maxAge keeps the visitor's choice ${Math.round(consentMaxAge / 86_400)} days, past the 6 months the CNIL recommends before asking again: lower it to 15552000 (180 days) or remove it.`, 'warning'))
|
|
544
|
+
|
|
545
|
+
const srcFiles = existsSync(srcDir)
|
|
546
|
+
? walkFiles(srcDir, {
|
|
547
|
+
skipDir: name => name === 'node_modules' || name === 'dist' || name.startsWith('.'),
|
|
548
|
+
keep: entry => entry.name.endsWith('.css') || entry.name.endsWith('.tsx'),
|
|
549
|
+
}).sort()
|
|
550
|
+
: []
|
|
551
|
+
// publicDir is served as is, so its stylesheets load whatever they point at too.
|
|
552
|
+
const publicCss = walkFiles(publicDir, { keep: entry => entry.isFile() && entry.name.endsWith('.css') }).sort()
|
|
553
|
+
for (const cssFile of [...srcFiles.filter(file => file.endsWith('.css')), ...publicCss]) {
|
|
554
|
+
const code = readFileSync(cssFile, 'utf8')
|
|
555
|
+
warnings.push(...checkCssCompliance(relPosix(root, cssFile), code, compliance, site))
|
|
586
556
|
}
|
|
587
557
|
|
|
588
558
|
const pagePaths: string[] = []
|
|
@@ -590,9 +560,94 @@ export function collectProjectComplianceWarnings(input: ProjectComplianceInput):
|
|
|
590
560
|
if (basename(file).startsWith('_'))
|
|
591
561
|
continue
|
|
592
562
|
const analysis = analyses.get(file)
|
|
593
|
-
if (analysis
|
|
594
|
-
|
|
563
|
+
if (analysis === undefined || !analysis.exportedNames.includes('default'))
|
|
564
|
+
continue
|
|
565
|
+
const routePath = fileToRoutePath(routesDir, file)
|
|
566
|
+
pagePaths.push(routePath)
|
|
567
|
+
if (isLegalNoticePath(routePath, compliance) && !isRuleIgnored('lcen/legal-notice', compliance))
|
|
568
|
+
warnings.push(...placeholderWarnings(relPosix(root, file), readFileSync(file, 'utf8'), LEGAL_NOTICE_DETAILS))
|
|
569
|
+
if (matchesNoticePath(routePath, compliance?.privacyNotice, DEFAULT_PRIVACY_PATHS) && !isRuleIgnored('rgpd/privacy-notice', compliance))
|
|
570
|
+
warnings.push(...placeholderWarnings(relPosix(root, file), readFileSync(file, 'utf8'), PRIVACY_NOTICE_DETAILS))
|
|
571
|
+
// Any site that publishes a statement, public body or not, publishes what it says.
|
|
572
|
+
if (isStatementPath(routePath) && !isRuleIgnored('rgaa/accessibility-statement', compliance))
|
|
573
|
+
warnings.push(...placeholderWarnings(relPosix(root, file), readFileSync(file, 'utf8'), STATEMENT_DETAILS))
|
|
574
|
+
if (matchesNoticePath(routePath, undefined, ECO_DECLARATION_PATHS) && !isRuleIgnored('rgesn/declaration', compliance))
|
|
575
|
+
warnings.push(...placeholderWarnings(relPosix(root, file), readFileSync(file, 'utf8'), ECO_DECLARATION_DETAILS))
|
|
595
576
|
}
|
|
596
577
|
warnings.push(...checkRouteCompliance(pagePaths, compliance))
|
|
578
|
+
if (pagePaths.length > 0 && lang === undefined && langFromPlugin !== true && !isRuleIgnored('rgaa/document-lang', compliance))
|
|
579
|
+
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'))
|
|
580
|
+
warnings.push(...largePublicFileWarnings(root, isRuleIgnored('rgesn/large-public-file', compliance), publicDir))
|
|
581
|
+
if (!isRuleIgnored('rgpd/privacy-notice', compliance) && !pagePaths.some(p => matchesNoticePath(p, compliance?.privacyNotice, DEFAULT_PRIVACY_PATHS))) {
|
|
582
|
+
const tsxFiles = [...new Set([...srcFiles.filter(file => file.endsWith('.tsx')), ...routeFiles.filter(file => file.endsWith('.tsx'))])].sort()
|
|
583
|
+
warnings.push(...privacyNoticeWarnings(root, tsxFiles, analyses, compliance?.privacyNotice))
|
|
584
|
+
}
|
|
597
585
|
return warnings
|
|
598
586
|
}
|
|
587
|
+
|
|
588
|
+
function isPersonalDataInput(el: JSXElement): boolean {
|
|
589
|
+
if (jsxTagName(el.openingElement.name) !== 'input')
|
|
590
|
+
return false
|
|
591
|
+
const attrs = el.openingElement.attributes
|
|
592
|
+
const typeAttr = findAttr(attrs, 'type')
|
|
593
|
+
const typeVal = typeAttr === undefined ? undefined : staticStringValueOf(typeAttr.value)?.toLowerCase()
|
|
594
|
+
if (typeVal !== undefined && PERSONAL_DATA_TYPES.has(typeVal))
|
|
595
|
+
return true
|
|
596
|
+
const nameAttr = findAttr(attrs, 'name')
|
|
597
|
+
const nameVal = nameAttr === undefined ? undefined : staticStringValueOf(nameAttr.value)?.toLowerCase()
|
|
598
|
+
return nameVal !== undefined && PERSONAL_DATA_NAMES.has(nameVal)
|
|
599
|
+
}
|
|
600
|
+
|
|
601
|
+
function findPersonalDataInputOffset(program: Program, ignores: IgnoreMap): number | undefined {
|
|
602
|
+
let found: number | undefined
|
|
603
|
+
walk(program, (node): boolean | undefined => {
|
|
604
|
+
if (found !== undefined)
|
|
605
|
+
return false
|
|
606
|
+
if (node.type === 'JSXElement' && isPersonalDataInput(node) && !isIgnoredAt(ignores, 'rgpd/privacy-notice', node.start)) {
|
|
607
|
+
found = node.start
|
|
608
|
+
return false
|
|
609
|
+
}
|
|
610
|
+
return undefined
|
|
611
|
+
})
|
|
612
|
+
return found
|
|
613
|
+
}
|
|
614
|
+
|
|
615
|
+
// One warning, at the first un-ignored form field collecting an email or a phone number: a single privacy page covers every form.
|
|
616
|
+
function privacyNoticeWarnings(root: string, tsxFiles: readonly string[], analyses: ReadonlyMap<string, ModuleAnalysis>, custom: string | false | undefined): string[] {
|
|
617
|
+
for (const file of tsxFiles) {
|
|
618
|
+
const analysis = analyses.get(file)
|
|
619
|
+
if (analysis !== undefined && !analysis.hasJsx)
|
|
620
|
+
continue
|
|
621
|
+
const code = readFileSync(file, 'utf8')
|
|
622
|
+
const ignores = parsePrunelleIgnores(code)
|
|
623
|
+
if (ignores.fileRules.has('rgpd/privacy-notice'))
|
|
624
|
+
continue
|
|
625
|
+
const program = analysis?.program ?? parseSync(file, code, { lang: 'tsx' }).program
|
|
626
|
+
const offset = findPersonalDataInputOffset(program, ignores)
|
|
627
|
+
if (offset === undefined)
|
|
628
|
+
continue
|
|
629
|
+
const relFile = relPosix(root, file)
|
|
630
|
+
const expected = typeof custom === 'string' ? normalizePath(custom) : '/privacy (or /confidentialite)'
|
|
631
|
+
return [formatDiagnostic(formatLoc(relFile, code, offset), `[rgpd/privacy-notice] ${relFile} collects an email or a phone number but the app has no privacy notice route: RGPD art. 13 requires telling people, as their data is collected, who processes it, why, for how long and what their rights are; add ${expected} and link it beside the form, set compliance.privacyNotice, or silence with compliance: { privacyNotice: false }.`, 'warning')]
|
|
632
|
+
}
|
|
633
|
+
return []
|
|
634
|
+
}
|
|
635
|
+
|
|
636
|
+
// One warning for the first unfilled detail outside comments and ignores: the publisher fills them all in one pass.
|
|
637
|
+
function placeholderWarnings(relFile: string, code: string, notice: NoticeDetails): string[] {
|
|
638
|
+
if (!code.includes(notice.marker))
|
|
639
|
+
return []
|
|
640
|
+
const ignores = parsePrunelleIgnores(code)
|
|
641
|
+
if (ignores.fileRules.has(notice.rule))
|
|
642
|
+
return []
|
|
643
|
+
const comments = parseSync(relFile, code, { lang: relFile.endsWith('.tsx') ? 'tsx' : 'ts' }).comments
|
|
644
|
+
const inComment = (offset: number): boolean => comments.some(c => offset >= c.start && offset < c.end)
|
|
645
|
+
for (let from = 0; ;) {
|
|
646
|
+
const offset = code.indexOf(notice.marker, from)
|
|
647
|
+
if (offset === -1)
|
|
648
|
+
return []
|
|
649
|
+
if (!inComment(offset) && !isIgnoredAt(ignores, notice.rule, offset))
|
|
650
|
+
return [formatDiagnostic(formatLoc(relFile, code, offset), `[${notice.rule}] The ${notice.page} still holds "${notice.marker} ...]" placeholders: ${notice.law}; replace every placeholder in ${relFile}.`, 'warning')]
|
|
651
|
+
from = offset + notice.marker.length
|
|
652
|
+
}
|
|
653
|
+
}
|