prunelle 0.0.0-stage → 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.
Files changed (222) hide show
  1. package/LICENSE +21 -0
  2. package/llms.txt +245 -0
  3. package/package.json +62 -4
  4. package/src/a11y/declaration.ts +373 -0
  5. package/src/a11y/index.ts +477 -0
  6. package/src/a11y/rgaa-map.ts +230 -0
  7. package/src/ambient.d.ts +6 -0
  8. package/src/attrs.ts +43 -0
  9. package/src/build/bundle.ts +661 -0
  10. package/src/build/byte-size.ts +11 -0
  11. package/src/build/carry.ts +57 -0
  12. package/src/build/client-entries.ts +107 -0
  13. package/src/build/client-plugin.ts +175 -0
  14. package/src/build/client-prune.ts +168 -0
  15. package/src/build/compile.ts +130 -0
  16. package/src/build/ecoindex.ts +238 -0
  17. package/src/build/files.ts +171 -0
  18. package/src/build/first-load.ts +235 -0
  19. package/src/build/graph.ts +237 -0
  20. package/src/build/index.ts +678 -0
  21. package/src/build/is-inside.ts +12 -0
  22. package/src/build/island-promote.ts +311 -0
  23. package/src/build/islands.ts +452 -0
  24. package/src/build/manifest.ts +204 -0
  25. package/src/build/output.ts +81 -0
  26. package/src/build/plugin-host.ts +294 -0
  27. package/src/build/precompress.ts +139 -0
  28. package/src/build/prerender.ts +288 -0
  29. package/src/build/reach.ts +200 -0
  30. package/src/build/render-rules.ts +205 -0
  31. package/src/build/report.ts +290 -0
  32. package/src/build/resolve.ts +52 -0
  33. package/src/build/routes.ts +342 -0
  34. package/src/build/server-entry.ts +38 -0
  35. package/src/build/static-headers.ts +112 -0
  36. package/src/build/static.ts +209 -0
  37. package/src/build/status-pages.ts +127 -0
  38. package/src/build/style-bundles.ts +526 -0
  39. package/src/cli/check-forms.ts +636 -0
  40. package/src/cli/check-imports.ts +31 -0
  41. package/src/cli/check-links.ts +632 -0
  42. package/src/cli/check-security.ts +64 -0
  43. package/src/cli/check.ts +409 -0
  44. package/src/cli/commands.ts +372 -0
  45. package/src/cli/docs.ts +403 -0
  46. package/src/cli/index.ts +70 -0
  47. package/src/cli/inspect.ts +406 -0
  48. package/src/cli/mcp-tools.ts +492 -0
  49. package/src/cli/mcp.ts +205 -0
  50. package/src/cli/new.ts +52 -0
  51. package/src/cli/render.ts +416 -0
  52. package/src/cli/upgrade-names.ts +209 -0
  53. package/src/cli/upgrade.ts +367 -0
  54. package/src/compiler/analyze.ts +651 -0
  55. package/src/compiler/ast.ts +82 -0
  56. package/src/compiler/browser-globals.ts +250 -0
  57. package/src/compiler/cacheability-private.ts +566 -0
  58. package/src/compiler/cacheability.ts +335 -0
  59. package/src/compiler/compliance-eco.ts +71 -0
  60. package/src/compiler/compliance-rgaa.ts +614 -0
  61. package/src/compiler/compliance-urls.ts +148 -0
  62. package/src/compiler/compliance.ts +653 -0
  63. package/src/compiler/data-keys.ts +302 -0
  64. package/src/compiler/dev-source.ts +87 -0
  65. package/src/compiler/dom-attrs.ts +452 -0
  66. package/src/compiler/dom.ts +641 -0
  67. package/src/compiler/emit.ts +131 -0
  68. package/src/compiler/expr-context.ts +587 -0
  69. package/src/compiler/foreign-imports.ts +88 -0
  70. package/src/compiler/index.ts +8 -0
  71. package/src/compiler/interactivity.ts +366 -0
  72. package/src/compiler/mistakes.ts +520 -0
  73. package/src/compiler/node-env.ts +50 -0
  74. package/src/compiler/patch.ts +97 -0
  75. package/src/compiler/props-body.ts +60 -0
  76. package/src/compiler/props.ts +511 -0
  77. package/src/compiler/scope.ts +635 -0
  78. package/src/compiler/server-only.ts +365 -0
  79. package/src/compiler/shared.ts +397 -0
  80. package/src/compiler/signal-reads.ts +246 -0
  81. package/src/compiler/slots.ts +227 -0
  82. package/src/compiler/spread-handlers.ts +295 -0
  83. package/src/compiler/ssr-helpers.ts +555 -0
  84. package/src/compiler/ssr-island.ts +90 -0
  85. package/src/compiler/ssr-prunelle-tags.ts +69 -0
  86. package/src/compiler/ssr.ts +525 -0
  87. package/src/context.ts +199 -0
  88. package/src/dev/child.ts +30 -0
  89. package/src/dev/error-log.ts +177 -0
  90. package/src/dev/host-check.ts +32 -0
  91. package/src/dev/index.ts +268 -0
  92. package/src/dev/modules.ts +68 -0
  93. package/src/dev/open-editor.ts +228 -0
  94. package/src/dev/protocol.ts +24 -0
  95. package/src/dev/queue.ts +79 -0
  96. package/src/dev/reload.ts +159 -0
  97. package/src/dev/supervisor.ts +212 -0
  98. package/src/dev/watchers.ts +276 -0
  99. package/src/dom/action.ts +187 -0
  100. package/src/dom/attrs.ts +84 -0
  101. package/src/dom/claim.ts +120 -0
  102. package/src/dom/extras.ts +579 -0
  103. package/src/dom/index.ts +329 -0
  104. package/src/dom/island.ts +318 -0
  105. package/src/dom/loop.ts +484 -0
  106. package/src/dom/revive.ts +36 -0
  107. package/src/eco/declaration.ts +328 -0
  108. package/src/eco/index.ts +69 -0
  109. package/src/index.ts +22 -0
  110. package/src/jsx-runtime.ts +566 -0
  111. package/src/plugins/config.ts +92 -0
  112. package/src/plugins/context.ts +109 -0
  113. package/src/plugins/index.ts +14 -0
  114. package/src/plugins/load.ts +90 -0
  115. package/src/plugins/order.ts +148 -0
  116. package/src/plugins/public.ts +15 -0
  117. package/src/plugins/requires.ts +47 -0
  118. package/src/plugins/services.ts +119 -0
  119. package/src/plugins/types.ts +278 -0
  120. package/src/reactivity/graph.ts +259 -0
  121. package/src/reactivity/index.ts +541 -0
  122. package/src/reactivity/selector.ts +78 -0
  123. package/src/reactivity/store.ts +252 -0
  124. package/src/server/action-flash.ts +158 -0
  125. package/src/server/action-protect.ts +199 -0
  126. package/src/server/action-registry.ts +78 -0
  127. package/src/server/action.ts +511 -0
  128. package/src/server/app-entry.ts +23 -0
  129. package/src/server/app-secret.ts +63 -0
  130. package/src/server/assets.ts +168 -0
  131. package/src/server/compress.ts +646 -0
  132. package/src/server/consent.ts +290 -0
  133. package/src/server/cookies.ts +153 -0
  134. package/src/server/document.ts +286 -0
  135. package/src/server/env.ts +47 -0
  136. package/src/server/errors.ts +396 -0
  137. package/src/server/form-schema.ts +337 -0
  138. package/src/server/health.ts +275 -0
  139. package/src/server/http.ts +588 -0
  140. package/src/server/index.ts +615 -0
  141. package/src/server/lifecycle.ts +75 -0
  142. package/src/server/loader.ts +131 -0
  143. package/src/server/page.ts +122 -0
  144. package/src/server/param-matchers.ts +112 -0
  145. package/src/server/plugin-host.ts +267 -0
  146. package/src/server/prerendered.ts +71 -0
  147. package/src/server/public.ts +20 -0
  148. package/src/server/rate-limit.ts +199 -0
  149. package/src/server/region.ts +96 -0
  150. package/src/server/request-body.ts +27 -0
  151. package/src/server/request-path.ts +73 -0
  152. package/src/server/request-scope.ts +22 -0
  153. package/src/server/route-cache.ts +541 -0
  154. package/src/server/route-table.ts +424 -0
  155. package/src/server/router.ts +261 -0
  156. package/src/server/routing.ts +388 -0
  157. package/src/server/sanitize-html.ts +309 -0
  158. package/src/server/security.ts +550 -0
  159. package/src/server/serve-handlers.ts +93 -0
  160. package/src/server/served-file.ts +128 -0
  161. package/src/server/standard-schema.ts +104 -0
  162. package/src/server/start.ts +143 -0
  163. package/src/server/static-dir.ts +246 -0
  164. package/src/server/stream.ts +233 -0
  165. package/src/ssr/consent-banner.ts +58 -0
  166. package/src/ssr/context.ts +399 -0
  167. package/src/ssr/index.ts +624 -0
  168. package/src/ssr/navigation.ts +96 -0
  169. package/src/ssr/region.ts +192 -0
  170. package/src/style.ts +96 -0
  171. package/src/test/browser.ts +201 -0
  172. package/src/test/index.ts +262 -0
  173. package/src/ts-plugin/diagnostics.ts +505 -0
  174. package/src/ts-plugin/fixes.ts +364 -0
  175. package/src/ts-plugin/index.ts +194 -0
  176. package/src/unrenderable.ts +35 -0
  177. package/templates/_shared/AGENTS.md +10 -0
  178. package/templates/_shared/CLAUDE.md +1 -0
  179. package/templates/_shared/gitignore +8 -0
  180. package/templates/_shared/mcp.json +11 -0
  181. package/templates/_shared/public/robots.txt +2 -0
  182. package/templates/_shared/src/routes/legal.tsx +22 -0
  183. package/templates/_shared/tsconfig.json +25 -0
  184. package/templates/app/.env.example +8 -0
  185. package/templates/app/package.json +22 -0
  186. package/templates/app/prunelle.config.ts +13 -0
  187. package/templates/app/src/auth.ts +12 -0
  188. package/templates/app/src/mail.ts +4 -0
  189. package/templates/app/src/routes/_layout.tsx +40 -0
  190. package/templates/app/src/routes/dashboard/_middleware.ts +3 -0
  191. package/templates/app/src/routes/dashboard/index.tsx +47 -0
  192. package/templates/app/src/routes/index.tsx +15 -0
  193. package/templates/app/src/routes/login.tsx +53 -0
  194. package/templates/app/src/routes/privacy.tsx +25 -0
  195. package/templates/app/src/styles/app.css +14 -0
  196. package/templates/app/tests/app.test.ts +73 -0
  197. package/templates/blog/content/posts/welcome.md +9 -0
  198. package/templates/blog/package.json +22 -0
  199. package/templates/blog/prunelle.config.ts +23 -0
  200. package/templates/blog/src/posts.ts +26 -0
  201. package/templates/blog/src/routes/_layout.tsx +25 -0
  202. package/templates/blog/src/routes/blog/[slug].tsx +31 -0
  203. package/templates/blog/src/routes/index.tsx +33 -0
  204. package/templates/blog/src/styles/app.css +15 -0
  205. package/templates/fullstack/package.json +19 -0
  206. package/templates/fullstack/prunelle.config.ts +6 -0
  207. package/templates/fullstack/src/actions/greet.ts +19 -0
  208. package/templates/fullstack/src/components/Counter.tsx +10 -0
  209. package/templates/fullstack/src/components/GreetForm.tsx +28 -0
  210. package/templates/fullstack/src/routes/_layout.tsx +18 -0
  211. package/templates/fullstack/src/routes/api/health.ts +5 -0
  212. package/templates/fullstack/src/routes/index.tsx +13 -0
  213. package/templates/fullstack/src/styles/app.css +39 -0
  214. package/templates/fullstack/tests/app.test.ts +37 -0
  215. package/templates/minimal/package.json +19 -0
  216. package/templates/minimal/prunelle.config.ts +6 -0
  217. package/templates/minimal/src/components/Counter.tsx +10 -0
  218. package/templates/minimal/src/routes/_layout.tsx +18 -0
  219. package/templates/minimal/src/routes/index.tsx +11 -0
  220. package/templates/minimal/src/styles/app.css +32 -0
  221. package/ts-plugin.cjs +4277 -0
  222. package/README.md +0 -3
@@ -0,0 +1,653 @@
1
+ // French web law compliance checks (RGAA, RGPD, LCEN): static TSX/CSS warnings and route-level checks with explicit silencing.
2
+
3
+ import type { JSXElement, Node, Program } from 'oxc-parser'
4
+ import type { ModuleAnalysis } from './analyze'
5
+ import type { EcoJsxRule } from './compliance-eco'
6
+ import type { RgaaJsxRule, RgaaScope } from './compliance-rgaa'
7
+ import type { CompilerWarning } from './mistakes'
8
+ import { existsSync, readFileSync } from 'node:fs'
9
+ import { basename, join } from 'node:path'
10
+ import { parseSync, visitorKeys } from 'oxc-parser'
11
+ import { relPosix, walkFiles } from '../build/files'
12
+ import { fileToRoutePath } from '../build/routes'
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'
31
+
32
+ export type ComplianceRule =
33
+ | RgaaJsxRule
34
+ | EcoJsxRule
35
+ | 'rgesn/large-public-file'
36
+ | 'rgesn/declaration'
37
+ | 'rgesn/island-by-value'
38
+ | 'rgpd/third-party-url'
39
+ | 'rgpd/consent-retention'
40
+ | 'rgpd/privacy-notice'
41
+ | 'lcen/legal-notice'
42
+ | 'rgaa/document-lang'
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'
48
+
49
+ export interface ComplianceConfig {
50
+ /** Compliance rules to silence across the app. */
51
+ ignore?: readonly ComplianceRule[] | undefined
52
+ /** Set `true` for French public-sector sites (RGAA requires an accessibility statement page). */
53
+ publicBody?: boolean | undefined
54
+ /** Custom route path for the legal notice page, or `false` to silence `lcen/legal-notice`. */
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
58
+ }
59
+
60
+ const COMPLIANCE_RULES = new Set<ComplianceRule>([
61
+ ...RGAA_JSX_RULES,
62
+ ...ECO_JSX_RULES,
63
+ 'rgesn/large-public-file',
64
+ 'rgesn/declaration',
65
+ 'rgesn/island-by-value',
66
+ 'rgpd/third-party-url',
67
+ 'rgpd/consent-retention',
68
+ 'rgpd/privacy-notice',
69
+ 'lcen/legal-notice',
70
+ 'rgaa/document-lang',
71
+ 'rgaa/accessibility-statement',
72
+ 'action/unguarded-public-form',
73
+ 'form/invalid-option',
74
+ 'form/schema-unsupported',
75
+ 'form/unchecked',
76
+ ])
77
+
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'])
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
99
+
100
+ const LINE_IGNORE_RE = /(?:^|[\s{;(,])\/\/\s*prunelle-ignore(-file)?[ \t]+([^\r\n]+)/g
101
+ const BLOCK_IGNORE_RE = /\/\*\s*prunelle-ignore(-file)?\s+([\s\S]*?)\*\//g
102
+ const CSS_COMMENT_RE = /\/\*[\s\S]*?\*\//g
103
+
104
+ interface IgnoreMap {
105
+ lineStarts: readonly number[]
106
+ fileRules: Set<ComplianceRule>
107
+ lineRules: Map<number, Set<ComplianceRule>>
108
+ }
109
+
110
+ function extractRules(text: string): ComplianceRule[] {
111
+ const out: ComplianceRule[] = []
112
+ for (const token of text.trim().split(/[\s,]+/)) {
113
+ if (COMPLIANCE_RULES.has(token as ComplianceRule))
114
+ out.push(token as ComplianceRule)
115
+ }
116
+ return out
117
+ }
118
+
119
+ export function parsePrunelleIgnores(code: string): IgnoreMap {
120
+ const lineStarts = getLineStarts(code)
121
+ const fileRules = new Set<ComplianceRule>()
122
+ const lineRules = new Map<number, Set<ComplianceRule>>()
123
+
124
+ const record = (isFile: boolean, start: number, end: number, rawRules: string): void => {
125
+ const rules = extractRules(rawRules)
126
+ if (rules.length === 0)
127
+ return
128
+ if (isFile) {
129
+ for (const r of rules)
130
+ fileRules.add(r)
131
+ return
132
+ }
133
+ const startLine = lineIndexAtOffset(lineStarts, start) + 1
134
+ const endLine = lineIndexAtOffset(lineStarts, end) + 1
135
+ for (const line of [startLine, endLine, endLine + 1]) {
136
+ let set = lineRules.get(line)
137
+ if (set === undefined) {
138
+ set = new Set()
139
+ lineRules.set(line, set)
140
+ }
141
+ for (const r of rules)
142
+ set.add(r)
143
+ }
144
+ }
145
+
146
+ for (const re of [LINE_IGNORE_RE, BLOCK_IGNORE_RE]) {
147
+ re.lastIndex = 0
148
+ for (let m = re.exec(code); m !== null; m = re.exec(code))
149
+ record(m[1] === '-file', m.index, m.index + m[0].length, m[2]!)
150
+ }
151
+ return { lineStarts, fileRules, lineRules }
152
+ }
153
+
154
+ export function isIgnoredAt(ignores: IgnoreMap, rule: ComplianceRule, offset: number): boolean {
155
+ if (ignores.fileRules.has(rule))
156
+ return true
157
+ const line = lineIndexAtOffset(ignores.lineStarts, offset) + 1
158
+ return ignores.lineRules.get(line)?.has(rule) === true
159
+ }
160
+
161
+ export function isRuleIgnored(rule: ComplianceRule, compliance?: ComplianceConfig | undefined): boolean {
162
+ if (rule === 'lcen/legal-notice' && compliance?.legalNotice === false)
163
+ return true
164
+ if (rule === 'rgpd/privacy-notice' && compliance?.privacyNotice === false)
165
+ return true
166
+ return compliance?.ignore?.includes(rule) === true
167
+ }
168
+
169
+ /** Whether a compiler warning should be surfaced given `compliance` and `site` config. */
170
+ export function shouldEmitWarning(warning: CompilerWarning, compliance?: ComplianceConfig | undefined, site?: string | undefined): boolean {
171
+ if (warning.rule !== undefined && isRuleIgnored(warning.rule, compliance))
172
+ return false
173
+ if (warning.rule === 'rgpd/third-party-url' && warning.urlOrigin !== undefined) {
174
+ const siteOrigin = parseOrigin(site)
175
+ if (siteOrigin !== undefined && warning.urlOrigin === siteOrigin)
176
+ return false
177
+ }
178
+ return true
179
+ }
180
+
181
+ interface AncestorState {
182
+ labelDepth: number
183
+ consentGateDepth: number
184
+ interactiveAncestor: string | undefined
185
+ inLoop: boolean
186
+ }
187
+
188
+ function isInlineMapCall(node: Node): node is Extract<Node, { type: 'CallExpression' }> {
189
+ if (node.type !== 'CallExpression' || node.arguments.length === 0)
190
+ return false
191
+ const callee = unwrapTsExpr(node.callee)
192
+ if (callee.type !== 'MemberExpression' || callee.computed || callee.property.type !== 'Identifier' || !INLINE_MAP_METHODS.has(callee.property.name))
193
+ 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'
199
+ }
200
+
201
+ function isFunctionNode(node: Node): boolean {
202
+ return node.type === 'FunctionDeclaration' || node.type === 'FunctionExpression' || node.type === 'ArrowFunctionExpression'
203
+ }
204
+
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
215
+ }
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
229
+ }
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)
235
+ }
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)
241
+ }
242
+ }
243
+ walkScope(root, true)
244
+ return targets
245
+ }
246
+
247
+ function checkThirdPartyElement(
248
+ element: JSXElement,
249
+ tag: string,
250
+ filename: string,
251
+ ignores: IgnoreMap,
252
+ warnings: CompilerWarning[],
253
+ ): void {
254
+ for (const load of thirdPartyLoads(element, tag)) {
255
+ if (isIgnoredAt(ignores, 'rgpd/third-party-url', load.start))
256
+ continue
257
+ warnings.push({
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="...">.`,
262
+ })
263
+ }
264
+ }
265
+
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
+ }
270
+
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
279
+ }
280
+
281
+ /** Runs RGAA & RGPD static JSX compliance checks on `program`, appending warnings (never throwing). */
282
+ export function checkCompliance(
283
+ program: Program,
284
+ filename: string,
285
+ code: string,
286
+ warnings: CompilerWarning[],
287
+ resolveTag: (tag: string) => string | undefined,
288
+ ): void {
289
+ const ignores = parsePrunelleIgnores(code)
290
+ const isIgnored = (rule: RgaaJsxRule | EcoJsxRule, elementStart: number, attrStart?: number): boolean =>
291
+ isIgnoredAt(ignores, rule, elementStart) || (attrStart !== undefined && isIgnoredAt(ignores, rule, attrStart))
292
+
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') {
363
+ const tag = jsxTagName(node.openingElement.name)
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),
410
+ )
411
+ }
412
+
413
+ /** Scans a CSS file for third-party `@import` or `url(...)` origins (RGPD), respecting `/* prunelle-ignore *\/` and config. */
414
+ export function checkCssCompliance(
415
+ relFile: string,
416
+ code: string,
417
+ compliance?: ComplianceConfig | undefined,
418
+ site?: string | undefined,
419
+ ): string[] {
420
+ if (isRuleIgnored('rgpd/third-party-url', compliance))
421
+ return []
422
+ const ignores = parsePrunelleIgnores(code)
423
+ if (ignores.fileRules.has('rgpd/third-party-url'))
424
+ return []
425
+ const siteOrigin = parseOrigin(site)
426
+
427
+ const commentSpans: [number, number][] = []
428
+ CSS_COMMENT_RE.lastIndex = 0
429
+ for (let m = CSS_COMMENT_RE.exec(code); m !== null; m = CSS_COMMENT_RE.exec(code))
430
+ commentSpans.push([m.index, m.index + m[0].length])
431
+ const inComment = (offset: number): boolean => commentSpans.some(([s, e]) => offset >= s && offset < e)
432
+
433
+ const seenUrls = new Set<string>()
434
+ const out: string[] = []
435
+ const recordUrl = (offset: number, rawUrl: string): void => {
436
+ if (inComment(offset))
437
+ return
438
+ const key = `${offset}:${rawUrl}`
439
+ if (seenUrls.has(key))
440
+ return
441
+ const origin = thirdPartyOrigin(rawUrl)
442
+ if (origin === undefined || (siteOrigin !== undefined && origin === siteOrigin))
443
+ return
444
+ if (isIgnoredAt(ignores, 'rgpd/third-party-url', offset))
445
+ return
446
+ seenUrls.add(key)
447
+ const loc = formatLoc(relFile, code, offset)
448
+ out.push(formatDiagnostic(loc, `[rgpd/third-party-url] "${rawUrl}" in ${relFile} loads a third-party resource without consent: self-host it (e.g. @prunellejs/seo fonts or public/) or gate it with <ConsentGate category="...">.`, 'warning'))
449
+ }
450
+
451
+ for (const re of [CSS_IMPORT_RE, CSS_URL_RE]) {
452
+ re.lastIndex = 0
453
+ for (let m = re.exec(code); m !== null; m = re.exec(code)) {
454
+ const url = m[1] ?? m[2] ?? m[3]
455
+ if (url !== undefined)
456
+ recordUrl(m.index + m[0].indexOf(url), url)
457
+ }
458
+ }
459
+ return out
460
+ }
461
+
462
+ function normalizePath(path: string): string {
463
+ const trimmed = path.trim()
464
+ const withSlash = trimmed.startsWith('/') ? trimmed : `/${trimmed}`
465
+ return withSlash.length > 1 && withSlash.endsWith('/') ? withSlash.slice(0, -1).toLowerCase() : withSlash.toLowerCase()
466
+ }
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
+
491
+ /** Checks project-level LCEN legal notice and RGAA accessibility statement routes. */
492
+ export function checkRouteCompliance(pageRoutePaths: readonly string[], compliance?: ComplianceConfig | undefined): string[] {
493
+ if (pageRoutePaths.length === 0)
494
+ return []
495
+ const out: string[] = []
496
+
497
+ if (!isRuleIgnored('lcen/legal-notice', compliance)) {
498
+ const custom = typeof compliance?.legalNotice === 'string' ? normalizePath(compliance.legalNotice) : undefined
499
+ const hasLegal = pageRoutePaths.some(p => isLegalNoticePath(p, compliance))
500
+ if (!hasLegal) {
501
+ const expected = custom ?? '/mentions-legales (or /legal)'
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'))
503
+ }
504
+ }
505
+
506
+ if (compliance?.publicBody === true && !isRuleIgnored('rgaa/accessibility-statement', compliance)) {
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'))
509
+ }
510
+ }
511
+
512
+ return out
513
+ }
514
+
515
+ export interface ProjectComplianceInput {
516
+ root: string
517
+ srcDir: string
518
+ routesDir: string
519
+ publicDir?: string | undefined
520
+ routeFiles: readonly string[]
521
+ analyses: ReadonlyMap<string, ModuleAnalysis>
522
+ compliance?: ComplianceConfig | undefined
523
+ /** `consent.maxAge` from the config, in seconds. */
524
+ consentMaxAge?: number | undefined
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
530
+ }
531
+
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`. */
538
+ export function collectProjectComplianceWarnings(input: ProjectComplianceInput): string[] {
539
+ const { root, srcDir, routesDir, routeFiles, analyses, compliance, consentMaxAge, site, lang, langFromPlugin } = input
540
+ const publicDir = input.publicDir ?? join(root, 'public')
541
+ const warnings: string[] = []
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))
556
+ }
557
+
558
+ const pagePaths: string[] = []
559
+ for (const file of routeFiles) {
560
+ if (basename(file).startsWith('_'))
561
+ continue
562
+ const analysis = analyses.get(file)
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))
576
+ }
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
+ }
585
+ return warnings
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
+ }