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,82 @@
1
+ // The generic oxc AST walk every compiler pass shares, kept apart from the analyzer so the passes it calls can import it without a cycle.
2
+
3
+ import type { Node, Program } from 'oxc-parser'
4
+ import { visitorKeys } from 'oxc-parser'
5
+
6
+ export type AstNode = Node
7
+
8
+ export function computeJsxNodes(program: Program): WeakSet<Node> {
9
+ const jsxNodes = new WeakSet<Node>()
10
+ const visit = (node: unknown): boolean => {
11
+ if (node === null || typeof node !== 'object')
12
+ return false
13
+ if (Array.isArray(node)) {
14
+ let any = false
15
+ for (let i = 0; i < node.length; i++) {
16
+ if (visit(node[i]))
17
+ any = true
18
+ }
19
+ return any
20
+ }
21
+ const ast = node as Node
22
+ const type = ast.type
23
+ if (typeof type !== 'string')
24
+ return false
25
+ let found = type === 'JSXElement' || type === 'JSXFragment'
26
+ const keys = visitorKeys[type]
27
+ if (keys !== undefined) {
28
+ const rec = ast as unknown as Record<string, unknown>
29
+ for (let i = 0; i < keys.length; i++) {
30
+ const val = rec[keys[i]!]
31
+ if (val !== null && typeof val === 'object' && visit(val))
32
+ found = true
33
+ }
34
+ }
35
+ else {
36
+ for (const [key, val] of Object.entries(ast)) {
37
+ if (key === 'start' || key === 'end' || key === 'type')
38
+ continue
39
+ if (val !== null && typeof val === 'object' && visit(val))
40
+ found = true
41
+ }
42
+ }
43
+ if (found)
44
+ jsxNodes.add(ast)
45
+ return found
46
+ }
47
+ visit(program)
48
+ return jsxNodes
49
+ }
50
+
51
+ /** Visits `node` and its children depth first; a visitor returning `false` skips the children of that node. */
52
+ export function walk(node: unknown, visit: (node: Node) => boolean | void): void {
53
+ if (node === null || typeof node !== 'object')
54
+ return
55
+ if (Array.isArray(node)) {
56
+ for (let i = 0; i < node.length; i++)
57
+ walk(node[i], visit)
58
+ return
59
+ }
60
+ const ast = node as Node
61
+ const type = ast.type
62
+ if (typeof type !== 'string')
63
+ return
64
+ if (visit(ast) === false)
65
+ return
66
+ const keys = visitorKeys[type]
67
+ if (keys !== undefined) {
68
+ const rec = ast as unknown as Record<string, unknown>
69
+ for (let i = 0; i < keys.length; i++) {
70
+ const val = rec[keys[i]!]
71
+ if (val !== null && typeof val === 'object')
72
+ walk(val, visit)
73
+ }
74
+ return
75
+ }
76
+ for (const [key, val] of Object.entries(ast)) {
77
+ if (key === 'start' || key === 'end' || key === 'type')
78
+ continue
79
+ if (val !== null && typeof val === 'object')
80
+ walk(val, visit)
81
+ }
82
+ }
@@ -0,0 +1,250 @@
1
+ // Warns on a browser global read while a component renders or its module loads: the server has no `window`, so the first request throws.
2
+
3
+ import type { ArrowFunctionExpression, Node, Function as OxcFunction, Program, Statement } from 'oxc-parser'
4
+ import type { CompilerWarning } from './mistakes'
5
+ import { walk } from './ast'
6
+ import { walkScoped } from './scope'
7
+ import { collectPatternNames, containsJsx, unwrapTsExpr } from './shared'
8
+
9
+ // Globals a browser defines and Bun does not, chosen so that no common local name (`open`, `close`, `Node`) is among them.
10
+ const BROWSER_GLOBALS = new Set([
11
+ 'window', 'document', 'localStorage', 'sessionStorage', 'location', 'history', 'screen', 'indexedDB', 'caches', 'customElements',
12
+ 'matchMedia', 'requestAnimationFrame', 'cancelAnimationFrame', 'getComputedStyle', 'getSelection',
13
+ 'innerWidth', 'innerHeight', 'scrollX', 'scrollY', 'devicePixelRatio', 'visualViewport',
14
+ 'IntersectionObserver', 'ResizeObserver', 'MutationObserver', 'HTMLElement', 'Notification',
15
+ ])
16
+
17
+ // Most modules name none of them, which a scan of the text settles before any walk.
18
+ const MENTIONS_RE = new RegExp(`\\b(?:${[...BROWSER_GLOBALS].join('|')})\\b`)
19
+
20
+ // Array methods whose callback runs at once, so while the component renders.
21
+ const EAGER_CALLBACK_METHODS = new Set(['map', 'flatMap', 'filter', 'forEach', 'reduce', 'some', 'every', 'find', 'findIndex', 'sort', 'toSorted'])
22
+
23
+ const DEFINED_TYPEOF_VALUES = new Set(['object', 'function'])
24
+
25
+ type FunctionNode = ArrowFunctionExpression | OxcFunction
26
+
27
+ interface Read { name: string, start: number }
28
+
29
+ interface GuardSpan {
30
+ start: number
31
+ end: number
32
+ globals: ReadonlySet<string>
33
+ }
34
+
35
+ interface GuardPolarity {
36
+ positive: Set<string>
37
+ negative: Set<string>
38
+ }
39
+
40
+ function topLevelFunctions(program: Program): Map<string, FunctionNode> {
41
+ const fns = new Map<string, FunctionNode>()
42
+ for (const stmt of program.body) {
43
+ if (stmt.type === 'ExportDefaultDeclaration') {
44
+ const decl = unwrapTsExpr(stmt.declaration)
45
+ if (decl.type === 'FunctionDeclaration' || decl.type === 'FunctionExpression')
46
+ fns.set(decl.id?.name ?? 'default', decl)
47
+ else if (decl.type === 'ArrowFunctionExpression')
48
+ fns.set('default', decl)
49
+ continue
50
+ }
51
+ const decl = stmt.type === 'ExportNamedDeclaration' ? stmt.declaration : stmt
52
+ if (decl?.type === 'FunctionDeclaration' && decl.id !== null) {
53
+ fns.set(decl.id.name, decl)
54
+ }
55
+ else if (decl?.type === 'VariableDeclaration') {
56
+ for (const d of decl.declarations) {
57
+ const init = d.init === null ? undefined : unwrapTsExpr(d.init)
58
+ if (d.id.type === 'Identifier' && (init?.type === 'ArrowFunctionExpression' || init?.type === 'FunctionExpression'))
59
+ fns.set(d.id.name, init)
60
+ }
61
+ }
62
+ }
63
+ return fns
64
+ }
65
+
66
+ // Names the module binds at its top level, which shadow a global of the same name everywhere in it.
67
+ export function topLevelNames(program: Program): Set<string> {
68
+ const names = new Set<string>()
69
+ for (const stmt of program.body) {
70
+ const decl = (stmt.type === 'ExportNamedDeclaration' || stmt.type === 'ExportDefaultDeclaration') ? stmt.declaration : stmt
71
+ if (decl?.type === 'ImportDeclaration') {
72
+ for (const spec of decl.specifiers)
73
+ names.add(spec.local.name)
74
+ }
75
+ else if (decl?.type === 'VariableDeclaration') {
76
+ for (const d of decl.declarations)
77
+ collectPatternNames(d.id, names)
78
+ }
79
+ else if ((decl?.type === 'FunctionDeclaration' || decl?.type === 'ClassDeclaration') && decl.id !== null) {
80
+ names.add(decl.id.name)
81
+ }
82
+ }
83
+ return names
84
+ }
85
+
86
+ function typeofBrowserGlobal(node: Node): string | undefined {
87
+ const inner = unwrapTsExpr(node)
88
+ if (inner.type === 'UnaryExpression' && inner.operator === 'typeof' && inner.argument.type === 'Identifier' && BROWSER_GLOBALS.has(inner.argument.name))
89
+ return inner.argument.name
90
+ return undefined
91
+ }
92
+
93
+ function stringLiteralValue(node: Node): string | undefined {
94
+ const inner = unwrapTsExpr(node)
95
+ return inner.type === 'Literal' && typeof inner.value === 'string' ? inner.value : undefined
96
+ }
97
+
98
+ function extractTypeofGuard(node: Node): GuardPolarity {
99
+ const positive = new Set<string>()
100
+ const negative = new Set<string>()
101
+ const inner = unwrapTsExpr(node)
102
+ if (inner.type === 'UnaryExpression' && inner.operator === '!') {
103
+ const sub = extractTypeofGuard(inner.argument)
104
+ return { positive: sub.negative, negative: sub.positive }
105
+ }
106
+ if (inner.type === 'LogicalExpression') {
107
+ const left = extractTypeofGuard(inner.left)
108
+ const right = extractTypeofGuard(inner.right)
109
+ if (inner.operator === '&&') {
110
+ for (const g of left.positive) positive.add(g)
111
+ for (const g of right.positive) positive.add(g)
112
+ }
113
+ else if (inner.operator === '||') {
114
+ for (const g of left.negative) negative.add(g)
115
+ for (const g of right.negative) negative.add(g)
116
+ }
117
+ return { positive, negative }
118
+ }
119
+ if (inner.type === 'BinaryExpression' && (inner.operator === '===' || inner.operator === '==' || inner.operator === '!==' || inner.operator === '!=')) {
120
+ const g = typeofBrowserGlobal(inner.left) ?? typeofBrowserGlobal(inner.right)
121
+ const lit = typeofBrowserGlobal(inner.left) !== undefined ? stringLiteralValue(inner.right) : stringLiteralValue(inner.left)
122
+ if (g !== undefined && lit !== undefined) {
123
+ const isNeg = inner.operator === '!==' || inner.operator === '!='
124
+ if (lit === 'undefined')
125
+ (isNeg ? positive : negative).add(g)
126
+ else if (DEFINED_TYPEOF_VALUES.has(lit))
127
+ (isNeg ? negative : positive).add(g)
128
+ }
129
+ }
130
+ return { positive, negative }
131
+ }
132
+
133
+ function definitelyExits(stmt: Statement): boolean {
134
+ if (stmt.type === 'ReturnStatement' || stmt.type === 'ThrowStatement')
135
+ return true
136
+ if (stmt.type === 'BlockStatement')
137
+ return stmt.body.length > 0 && definitelyExits(stmt.body[stmt.body.length - 1]!)
138
+ if (stmt.type === 'IfStatement')
139
+ return stmt.alternate !== null && definitelyExits(stmt.consequent) && definitelyExits(stmt.alternate)
140
+ return false
141
+ }
142
+
143
+ function collectEarlyReturnGuards(stmts: readonly Statement[], guards: GuardSpan[]): void {
144
+ const lastEnd = stmts[stmts.length - 1]?.end
145
+ if (lastEnd === undefined)
146
+ return
147
+ for (let i = 0; i < stmts.length - 1; i++) {
148
+ const stmt = stmts[i]!
149
+ if (stmt.type !== 'IfStatement' || !definitelyExits(stmt.consequent))
150
+ continue
151
+ const { negative } = extractTypeofGuard(stmt.test)
152
+ if (negative.size > 0)
153
+ guards.push({ start: stmts[i + 1]!.start, end: lastEnd, globals: negative })
154
+ }
155
+ }
156
+
157
+ function collectProgramMetadata(program: Program): { eagerCallbacks: WeakSet<Node>, calleeStarts: Set<number>, guards: GuardSpan[] } {
158
+ const eagerCallbacks = new WeakSet<Node>()
159
+ const calleeStarts = new Set<number>()
160
+ const guards: GuardSpan[] = []
161
+
162
+ const addGuard = (node: Node | null | undefined, globals: ReadonlySet<string>): void => {
163
+ if (node !== null && node !== undefined && globals.size > 0)
164
+ guards.push({ start: node.start, end: node.end, globals })
165
+ }
166
+
167
+ walk(program, (node) => {
168
+ if (node.type === 'CallExpression') {
169
+ const callee = unwrapTsExpr(node.callee)
170
+ if (callee.type === 'Identifier')
171
+ calleeStarts.add(callee.start)
172
+ else if (callee.type === 'MemberExpression' && !callee.computed && callee.property.type === 'Identifier' && EAGER_CALLBACK_METHODS.has(callee.property.name) && node.arguments[0] !== undefined)
173
+ eagerCallbacks.add(unwrapTsExpr(node.arguments[0]))
174
+ }
175
+ else if (node.type === 'UnaryExpression' && node.operator === 'typeof' && node.argument.type === 'Identifier' && BROWSER_GLOBALS.has(node.argument.name)) {
176
+ guards.push({ start: node.argument.start, end: node.argument.end, globals: new Set([node.argument.name]) })
177
+ }
178
+ else if (node.type === 'IfStatement' || node.type === 'ConditionalExpression') {
179
+ const { positive, negative } = extractTypeofGuard(node.test)
180
+ addGuard(node.consequent, positive)
181
+ addGuard(node.alternate, negative)
182
+ }
183
+ else if (node.type === 'LogicalExpression') {
184
+ const { positive, negative } = extractTypeofGuard(node.left)
185
+ if (node.operator === '&&')
186
+ addGuard(node.right, positive)
187
+ else if (node.operator === '||')
188
+ addGuard(node.right, negative)
189
+ }
190
+ else if (node.type === 'BlockStatement' || node.type === 'Program') {
191
+ collectEarlyReturnGuards(node.body, guards)
192
+ }
193
+ })
194
+
195
+ return { eagerCallbacks, calleeStarts, guards }
196
+ }
197
+
198
+ /** Warns on each browser global the module reads at its top level, or while one of its components renders, itself or through a top-level helper it calls. */
199
+ export function checkBrowserGlobals(program: Program, code: string, filename: string, warnings: CompilerWarning[], jsxNodes?: WeakSet<Node>): void {
200
+ if (!MENTIONS_RE.test(code))
201
+ return
202
+ const shadowing = topLevelNames(program)
203
+ const helpers = topLevelFunctions(program)
204
+ const { eagerCallbacks, calleeStarts, guards } = collectProgramMetadata(program)
205
+
206
+ const isNameGuarded = (offset: number, name: string): boolean =>
207
+ guards.some(g => offset >= g.start && offset < g.end && (g.globals.has(name) || g.globals.has('window')))
208
+
209
+ const readsOf = new Map<Node, Read[]>()
210
+ const collect = (root: Node): Read[] => {
211
+ const known = readsOf.get(root)
212
+ if (known !== undefined)
213
+ return known
214
+ const reads: Read[] = []
215
+ readsOf.set(root, reads)
216
+ walkScoped(root, {
217
+ onFunction: fn => !eagerCallbacks.has(fn),
218
+ onIdentifier: (id) => {
219
+ if (BROWSER_GLOBALS.has(id.name) && !shadowing.has(id.name)) {
220
+ if (!isNameGuarded(id.start, id.name))
221
+ reads.push({ name: id.name, start: id.start })
222
+ return
223
+ }
224
+ const helper = calleeStarts.has(id.start) ? helpers.get(id.name) : undefined
225
+ if (helper !== undefined) {
226
+ for (const read of collect(helper)) {
227
+ if (!isNameGuarded(id.start, read.name))
228
+ reads.push(read)
229
+ }
230
+ }
231
+ },
232
+ })
233
+ return reads
234
+ }
235
+
236
+ const reported = new Set<number>()
237
+ const report = (reads: readonly Read[], when: string): void => {
238
+ for (const read of reads) {
239
+ if (reported.has(read.start))
240
+ continue
241
+ reported.add(read.start)
242
+ warnings.push({ start: read.start, message: `prunelle compiler: \`${read.name}\` read ${when} in ${filename}: the server has no \`${read.name}\`, so it throws; read it in onMount() or an event handler, or check \`typeof ${read.name} !== 'undefined'\` first.` })
243
+ }
244
+ }
245
+ report(collect(program), 'when the module loads')
246
+ for (const [name, fn] of helpers) {
247
+ if (containsJsx(fn, jsxNodes))
248
+ report(collect(fn), `while <${name}> renders`)
249
+ }
250
+ }