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,520 @@
1
+ // Refuses JSX that compiles but does the wrong thing, mostly habits from React, Solid and Astro, with an error naming the fix.
2
+
3
+ import type { JSXAttribute, JSXAttributeItem, JSXElement, Node } from 'oxc-parser'
4
+ import type { ComplianceRule } from './compliance'
5
+ import type { ServerOnlyInIsland } from './interactivity'
6
+ import { URL_ATTRS } from '../attrs'
7
+ import { walk } from './ast'
8
+ import { calleeName, EVENT_ATTR, filterMeaningfulChildren, isComponentTag, isUnsafeUrlAttr, jsxTagName, unwrapParens, unwrapTsExpr, VOID_ELEMENTS } from './shared'
9
+
10
+ // The prunelle prop for each DOM event, keyed by the event name prunelle binds (`onKeyDown` binds `keydown`).
11
+ const EVENT_PROPS = new Map(Object.entries({
12
+ abort: 'onAbort', animationcancel: 'onAnimationCancel', animationend: 'onAnimationEnd', animationiteration: 'onAnimationIteration', animationstart: 'onAnimationStart',
13
+ auxclick: 'onAuxClick', beforeinput: 'onBeforeInput', beforetoggle: 'onBeforeToggle', blur: 'onBlur', cancel: 'onCancel',
14
+ canplay: 'onCanPlay', canplaythrough: 'onCanPlayThrough', change: 'onChange', click: 'onClick', close: 'onClose',
15
+ compositionend: 'onCompositionEnd', compositionstart: 'onCompositionStart', compositionupdate: 'onCompositionUpdate',
16
+ contextmenu: 'onContextMenu', copy: 'onCopy', cut: 'onCut', dblclick: 'onDblClick', drag: 'onDrag', dragend: 'onDragEnd',
17
+ dragenter: 'onDragEnter', dragleave: 'onDragLeave', dragover: 'onDragOver', dragstart: 'onDragStart', drop: 'onDrop',
18
+ durationchange: 'onDurationChange', emptied: 'onEmptied', ended: 'onEnded', error: 'onError', focus: 'onFocus',
19
+ focusin: 'onFocusIn', focusout: 'onFocusOut', formdata: 'onFormData', gotpointercapture: 'onGotPointerCapture',
20
+ input: 'onInput', invalid: 'onInvalid', keydown: 'onKeyDown', keypress: 'onKeyPress', keyup: 'onKeyUp', load: 'onLoad',
21
+ loadeddata: 'onLoadedData', loadedmetadata: 'onLoadedMetadata', loadstart: 'onLoadStart',
22
+ lostpointercapture: 'onLostPointerCapture', mousedown: 'onMouseDown', mouseenter: 'onMouseEnter', mouseleave: 'onMouseLeave',
23
+ mousemove: 'onMouseMove', mouseout: 'onMouseOut', mouseover: 'onMouseOver', mouseup: 'onMouseUp', paste: 'onPaste',
24
+ pause: 'onPause', play: 'onPlay', playing: 'onPlaying', pointercancel: 'onPointerCancel', pointerdown: 'onPointerDown',
25
+ pointerenter: 'onPointerEnter', pointerleave: 'onPointerLeave', pointermove: 'onPointerMove', pointerout: 'onPointerOut',
26
+ pointerover: 'onPointerOver', pointerup: 'onPointerUp', progress: 'onProgress', ratechange: 'onRateChange', reset: 'onReset',
27
+ resize: 'onResize', scroll: 'onScroll', scrollend: 'onScrollEnd', securitypolicyviolation: 'onSecurityPolicyViolation',
28
+ seeked: 'onSeeked', seeking: 'onSeeking', select: 'onSelect', selectionchange: 'onSelectionChange',
29
+ selectstart: 'onSelectStart', slotchange: 'onSlotChange', stalled: 'onStalled', submit: 'onSubmit', suspend: 'onSuspend',
30
+ timeupdate: 'onTimeUpdate', toggle: 'onToggle', touchcancel: 'onTouchCancel', touchend: 'onTouchEnd',
31
+ touchmove: 'onTouchMove', touchstart: 'onTouchStart', transitioncancel: 'onTransitionCancel',
32
+ transitionend: 'onTransitionEnd', transitionrun: 'onTransitionRun', transitionstart: 'onTransitionStart',
33
+ volumechange: 'onVolumeChange', waiting: 'onWaiting', wheel: 'onWheel',
34
+ afterprint: 'onAfterPrint', beforeprint: 'onBeforePrint', beforeunload: 'onBeforeUnload', hashchange: 'onHashChange',
35
+ message: 'onMessage', offline: 'onOffline', online: 'onOnline', pagehide: 'onPageHide', pageshow: 'onPageShow',
36
+ popstate: 'onPopState', storage: 'onStorage', unload: 'onUnload', fullscreenchange: 'onFullscreenChange',
37
+ fullscreenerror: 'onFullscreenError', loadend: 'onLoadEnd', search: 'onSearch', beforematch: 'onBeforeMatch',
38
+ command: 'onCommand', cuechange: 'onCueChange', encrypted: 'onEncrypted', waitingforkey: 'onWaitingForKey',
39
+ pointerrawupdate: 'onPointerRawUpdate', contextlost: 'onContextLost', contextrestored: 'onContextRestored',
40
+ contentvisibilityautostatechange: 'onContentVisibilityAutoStateChange',
41
+ }))
42
+
43
+ // React names whose DOM event is spelled differently.
44
+ const RENAMED_EVENTS = new Map([['doubleclick', 'dblclick']])
45
+ const CAPTURE_SUFFIX = 'capture'
46
+ const LOWERCASE_EVENT = /^on[a-z]/
47
+
48
+ function eventProp(event: string): string {
49
+ return EVENT_PROPS.get(event) ?? `on${event.charAt(0).toUpperCase()}${event.slice(1)}`
50
+ }
51
+
52
+ /** A compiler mistake, with the source offset of the code it names once known. */
53
+ export class PrunelleMistake extends Error {
54
+ start: number | undefined = undefined
55
+ }
56
+
57
+ /** Code that compiles but usually means something else, at its source offset. */
58
+ export interface CompilerWarning {
59
+ message: string
60
+ start: number
61
+ rule?: ComplianceRule | undefined
62
+ urlOrigin?: string | undefined
63
+ }
64
+
65
+ export function mistake(what: string, filename: string, fix: string): PrunelleMistake {
66
+ return new PrunelleMistake(`prunelle compiler: ${what} in ${filename}: ${fix}.`)
67
+ }
68
+
69
+ /** `$hydrate` where nothing can hydrate: on an element (`component` false) or on a component that is not an island. The one wording the compiler, the build and `prunelle check` share. */
70
+ export function hydrateMistake(tag: string, filename: string, component: boolean): PrunelleMistake {
71
+ return mistake(`$hydrate on <${tag}>`, filename, component
72
+ ? `${tag} is not an island, so it never hydrates; drop $hydrate, or give ${tag} state or handlers`
73
+ : 'only components can be islands; drop $hydrate, or move it onto a component tag')
74
+ }
75
+
76
+ let collected: PrunelleMistake[] | undefined
77
+
78
+ /** Runs one check, a mistake it throws placed at `start`. The build stops at the first mistake; while `collectMistakes` runs, the mistake is recorded and the caller goes on. */
79
+ export function guarded(start: number, check: () => void): void {
80
+ try {
81
+ check()
82
+ }
83
+ catch (err) {
84
+ if (!(err instanceof PrunelleMistake))
85
+ throw err
86
+ err.start ??= start
87
+ if (collected === undefined)
88
+ throw err
89
+ collected.push(err)
90
+ }
91
+ }
92
+
93
+ /** Runs `fn` with every guarded check recording its mistake; `error` is what `fn` threw past them, a syntax error or a mistake that stops the analysis. */
94
+ export function collectMistakes<T>(fn: () => T): { value: T | undefined, mistakes: PrunelleMistake[], error: unknown } {
95
+ const prev = collected
96
+ const mistakes: PrunelleMistake[] = []
97
+ collected = mistakes
98
+ try {
99
+ return { value: fn(), mistakes, error: undefined }
100
+ }
101
+ catch (error) {
102
+ return { value: undefined, mistakes, error }
103
+ }
104
+ finally {
105
+ collected = prev
106
+ }
107
+ }
108
+
109
+ export function islandRemoved(filename: string, start?: number): PrunelleMistake {
110
+ const err = mistake('island()', filename, 'it is gone; write export const hydrate = \'visible\' in the component\'s file, or set $hydrate where it renders')
111
+ err.start = start
112
+ return err
113
+ }
114
+
115
+ // `<Head>` and `<Suspense>` run during server rendering only, so in an island they never update.
116
+ export function serverOnlyTagError(filename: string, { tag, island, reason, offset }: ServerOnlyInIsland): PrunelleMistake {
117
+ const err = mistake(`<${tag}> in an island`, filename, `${island} is an island because it ${reason}; move <${tag}> to the page or a layout, outside any component with state or handlers`)
118
+ err.start = offset
119
+ return err
120
+ }
121
+
122
+ /** The error for an Astro `---` block opening a file, at the first dash. */
123
+ export function frontmatterMistake(filename: string, start: number): PrunelleMistake {
124
+ const err = mistake('a --- frontmatter block', filename, 'prunelle files are plain TypeScript with no --- fence; write that code above the component, or inside it, and delete both --- lines')
125
+ err.start = start
126
+ return err
127
+ }
128
+
129
+ const KAY_SOURCE_RE = /^@kayjs\/(kay(?=\/|$))?/
130
+
131
+ /** Refuses an import of kay, the name prunelle had before: the app would load both. */
132
+ export function assertNoKayImport(source: string, filename: string): void {
133
+ const match = KAY_SOURCE_RE.exec(source)
134
+ if (match !== null)
135
+ throw mistake(`import from '${source}'`, filename, `write '${source.replace(KAY_SOURCE_RE, match[1] === undefined ? '@prunellejs/' : 'prunelle')}' (prunelle upgrade rewrites every import)`)
136
+ }
137
+
138
+ const HYDRATE_MODES = new Set(['load', 'visible', 'idle', 'interaction', 'client'])
139
+ const MEDIA_MODE = /^media\(.+\)$/
140
+
141
+ /** Throws on a hydrate mode the browser runtime would silently treat as `load`; `*` stands for a mode picked at runtime. */
142
+ export function checkHydrateMode(mode: string, what: string, filename: string): void {
143
+ if (mode !== '*' && !HYDRATE_MODES.has(mode) && !MEDIA_MODE.test(mode))
144
+ throw mistake(what, filename, 'use load, visible, idle, interaction, client or media(<query>)')
145
+ }
146
+
147
+ /** Extracts a static string literal or zero-interpolation template literal after unwrapping TS assertions. */
148
+ export function staticStringValue(node: Node | null | undefined): string | undefined {
149
+ if (node === null || node === undefined)
150
+ return undefined
151
+ const val = unwrapTsExpr(node)
152
+ if (val.type === 'Literal' && typeof val.value === 'string')
153
+ return val.value
154
+ return val.type === 'TemplateLiteral' && val.expressions.length === 0 ? (val.quasis[0]?.value.cooked ?? undefined) : undefined
155
+ }
156
+
157
+ /** Validates an `export const hydrate = ...` initializer and returns its string mode when statically known. */
158
+ export function checkExportHydrate(init: Node | null | undefined, filename: string): string | undefined {
159
+ if (init === null || init === undefined)
160
+ return undefined
161
+ const mode = staticStringValue(init)
162
+ if (mode !== undefined) {
163
+ guarded(init.start, () => checkHydrateMode(mode, `hydrate = "${mode}"`, filename))
164
+ return mode
165
+ }
166
+ const val = unwrapTsExpr(init)
167
+ if (val.type === 'Literal' || val.type === 'ArrayExpression' || val.type === 'ObjectExpression' || (val.type === 'Identifier' && val.name === 'undefined'))
168
+ guarded(init.start, () => { throw mistake('hydrate', filename, 'use load, visible, idle, interaction, client or media(<query>)') })
169
+ return undefined
170
+ }
171
+
172
+ function checkHandler(name: string, value: JSXAttribute['value'], filename: string, code: string): void {
173
+ if (value === null)
174
+ throw mistake(name, filename, `pass a function, ${name}={() => ...}`)
175
+ if (value.type === 'Literal')
176
+ throw mistake(`${name}="..."`, filename, `pass a function, ${name}={() => ...}`)
177
+ if (value.type !== 'JSXExpressionContainer')
178
+ return
179
+ const expr = unwrapParens(value.expression)
180
+ if ((expr.type === 'Literal' && typeof expr.value === 'string') || expr.type === 'TemplateLiteral')
181
+ throw mistake(`${name}="..."`, filename, `pass a function, ${name}={() => ...}`)
182
+ if (expr.type === 'UpdateExpression' || expr.type === 'AssignmentExpression') {
183
+ const source = code.slice(expr.start, expr.end)
184
+ throw mistake(`${name} runs ${source} at render`, filename, `write ${name}={() => ${source}}`)
185
+ }
186
+ }
187
+
188
+ // SVG presentation attributes React spells in camelCase; SVG is case-sensitive, so the camelCase name sets nothing.
189
+ const KEBAB_SVG_ATTRS = new Set([
190
+ 'alignment-baseline', 'baseline-shift', 'clip-path', 'clip-rule', 'color-interpolation', 'color-interpolation-filters',
191
+ 'dominant-baseline', 'enable-background', 'fill-opacity', 'fill-rule', 'flood-color', 'flood-opacity', 'font-family',
192
+ 'font-size', 'font-size-adjust', 'font-stretch', 'font-style', 'font-variant', 'font-weight', 'image-rendering',
193
+ 'letter-spacing', 'lighting-color', 'marker-end', 'marker-mid', 'marker-start', 'paint-order', 'pointer-events',
194
+ 'shape-rendering', 'stop-color', 'stop-opacity', 'stroke-dasharray', 'stroke-dashoffset', 'stroke-linecap',
195
+ 'stroke-linejoin', 'stroke-miterlimit', 'stroke-opacity', 'stroke-width', 'text-anchor', 'text-decoration',
196
+ 'text-rendering', 'transform-origin', 'unicode-bidi', 'vector-effect', 'word-spacing', 'writing-mode',
197
+ ])
198
+ const CAMEL_HUMP = /[A-Z]/g
199
+ // React's uncontrolled-input props and the attribute each one stands for.
200
+ const REACT_DEFAULTS = new Map([['defaultValue', 'value'], ['defaultChecked', 'checked']])
201
+ const BOOLEAN_ATTRS = new Set([
202
+ 'allowfullscreen', 'async', 'autofocus', 'autoplay', 'checked', 'controls', 'default', 'defer', 'disabled',
203
+ 'formnovalidate', 'hidden', 'inert', 'ismap', 'loop', 'multiple', 'muted', 'nomodule', 'novalidate', 'open',
204
+ 'playsinline', 'readonly', 'required', 'reversed', 'selected',
205
+ ])
206
+
207
+ function checkRef(value: JSXAttribute['value'], filename: string): void {
208
+ if (value === null)
209
+ throw mistake('ref', filename, 'pass a function, ref={el => ...}')
210
+ if (value.type === 'Literal')
211
+ throw mistake('ref="..."', filename, 'pass a function, ref={el => ...}')
212
+ if (value.type === 'JSXExpressionContainer' && unwrapParens(value.expression).type === 'ObjectExpression')
213
+ throw mistake('ref={{ ... }}', filename, 'pass a function, ref={el => ...}, prunelle has no ref objects')
214
+ }
215
+
216
+ const CAMEL_ARIA = /^aria[A-Z]/
217
+
218
+ function checkSpelling(name: string, value: JSXAttribute['value'], filename: string): void {
219
+ if (CAMEL_ARIA.test(name))
220
+ throw mistake(name, filename, `write aria-${name.slice(4).toLowerCase()}={...}, ARIA attributes use kebab-case`)
221
+ const kebab = name.replace(CAMEL_HUMP, hump => `-${hump.toLowerCase()}`)
222
+ if (kebab !== name && KEBAB_SVG_ATTRS.has(kebab))
223
+ throw mistake(name, filename, `write ${kebab}={...}, SVG attributes are case-sensitive`)
224
+ if (name === 'xlinkHref')
225
+ throw mistake(name, filename, 'write xlink:href={...}, or href={...}')
226
+ const attribute = REACT_DEFAULTS.get(name)
227
+ if (attribute !== undefined)
228
+ throw mistake(name, filename, `write ${attribute}={...}`)
229
+ if (value?.type === 'Literal' && value.value === 'false' && BOOLEAN_ATTRS.has(name.toLowerCase()))
230
+ throw mistake(`${name}="false"`, filename, `write ${name}={false}, any string turns it on`)
231
+ }
232
+
233
+ // Astro's client: directives and the $hydrate mode each one means.
234
+ const CLIENT_DIRECTIVES = new Map([['load', 'load'], ['visible', 'visible'], ['idle', 'idle'], ['only', 'client'], ['media', 'media(<query>)']])
235
+ // XML namespaces SVG and MathML really use; every other prefix is a directive from another framework.
236
+ const XML_NAMESPACES = new Set(['xlink', 'xml', 'xmlns'])
237
+
238
+ function checkNamespacedAttribute(namespace: string, name: string, filename: string): void {
239
+ if (XML_NAMESPACES.has(namespace))
240
+ return
241
+ const what = `${namespace}:${name}`
242
+ if (namespace === 'on')
243
+ throw mistake(what, filename, `write ${eventProp(name.toLowerCase())}={...}`)
244
+ if (namespace === 'client')
245
+ throw mistake(what, filename, `write $hydrate="${CLIENT_DIRECTIVES.get(name) ?? 'load'}"`)
246
+ if (what === 'class:list')
247
+ throw mistake(what, filename, 'pass one string to class, joining the names yourself')
248
+ if (what === 'set:html')
249
+ throw mistake(what, filename, 'write innerHTML={...}')
250
+ if (what === 'set:text')
251
+ throw mistake(what, filename, 'write the text as children')
252
+ if (namespace === 'use')
253
+ throw mistake(what, filename, `call ${name} in a ref`)
254
+ if (namespace === 'prop' || namespace === 'attr' || namespace === 'bool')
255
+ throw mistake(what, filename, `write ${name}={...}`)
256
+ throw mistake(what, filename, `prunelle has no ${namespace}: directives`)
257
+ }
258
+
259
+ // The source of `x` in React's `{{ __html: x }}`, or undefined when the attribute holds something else.
260
+ function reactHtmlSource(value: JSXAttribute['value'], code: string): string | undefined {
261
+ if (value?.type !== 'JSXExpressionContainer')
262
+ return undefined
263
+ const expr = unwrapParens(value.expression)
264
+ if (expr.type !== 'ObjectExpression')
265
+ return undefined
266
+ for (const p of expr.properties) {
267
+ if (p.type !== 'Property')
268
+ continue
269
+ const keyName = p.key.type === 'Identifier' ? p.key.name : p.key.type === 'Literal' ? String(p.key.value) : undefined
270
+ if (keyName === '__html')
271
+ return code.slice(p.value.start, p.value.end)
272
+ }
273
+ return undefined
274
+ }
275
+
276
+ function checkInnerHtml(element: JSXElement, tag: string, attr: JSXAttribute, filename: string, code: string): void {
277
+ const html = reactHtmlSource(attr.value, code)
278
+ if (html !== undefined)
279
+ throw mistake('innerHTML={{ __html }}', filename, `write innerHTML={${html}}, prunelle takes the HTML string itself`)
280
+ if (VOID_ELEMENTS.has(tag))
281
+ throw mistake(`innerHTML on <${tag}>`, filename, 'a void element has no content, drop innerHTML')
282
+ if (filterMeaningfulChildren(element.children).length > 0)
283
+ throw mistake('innerHTML with children', filename, 'keep one, the children or innerHTML')
284
+ }
285
+
286
+ function staticStringAttr(value: JSXAttribute['value']): string | undefined {
287
+ if (value === null)
288
+ return undefined
289
+ if (value.type === 'Literal' && typeof value.value === 'string')
290
+ return value.value
291
+ if (value.type === 'JSXExpressionContainer' && value.expression.type !== 'JSXEmptyExpression') {
292
+ const expr = unwrapParens(value.expression)
293
+ if (expr.type === 'Literal' && typeof expr.value === 'string')
294
+ return expr.value
295
+ if (expr.type === 'TemplateLiteral' && expr.expressions.length === 0)
296
+ return expr.quasis[0]?.value.cooked ?? undefined
297
+ }
298
+ return undefined
299
+ }
300
+
301
+ function checkElementAttribute(attr: JSXAttribute, tag: string, isCustomElement: boolean, filename: string, code: string, warnings: CompilerWarning[]): void {
302
+ if (attr.name.type !== 'JSXIdentifier')
303
+ return
304
+ const name = attr.name.name
305
+ if (name === '$hydrate')
306
+ throw hydrateMistake(tag, filename, false)
307
+ if (name.toLowerCase() === 'srcdoc')
308
+ throw mistake(`${name} on <${tag}>`, filename, 'prunelle blocks srcdoc for XSS safety; serve the document from a route and load it with src')
309
+ if (name === 'dangerouslySetInnerHTML')
310
+ throw mistake(name, filename, `write innerHTML={${reactHtmlSource(attr.value, code) ?? '...'}}`)
311
+ if (name === 'classList')
312
+ throw mistake(name, filename, 'pass one string to class, joining the names yourself')
313
+ if (name === 'ref')
314
+ return checkRef(attr.value, filename)
315
+ checkSpelling(name, attr.value, filename)
316
+ if (URL_ATTRS.has(name.toLowerCase())) {
317
+ const urlText = staticStringAttr(attr.value)
318
+ if (urlText !== undefined && isUnsafeUrlAttr(name, urlText, tag))
319
+ warnings.push({ message: `prunelle compiler: unsafe URL in ${name} on <${tag}> in ${filename}; prunelle strips javascript:, vbscript:, and non-image data: URLs.`, start: attr.start })
320
+ }
321
+ if (LOWERCASE_EVENT.test(name) && EVENT_PROPS.has(name.slice(2)))
322
+ throw mistake(name, filename, `write ${eventProp(name.slice(2))}={...}, prunelle binds camelCase event props only`)
323
+ if (!EVENT_ATTR.test(name))
324
+ return
325
+ checkHandler(name, attr.value, filename, code)
326
+ if (!isCustomElement)
327
+ checkEventName(name, filename)
328
+ }
329
+
330
+ function checkEventName(name: string, filename: string): void {
331
+ const event = name.slice(2).toLowerCase()
332
+ if (EVENT_PROPS.has(event))
333
+ return
334
+ const renamed = RENAMED_EVENTS.get(event)
335
+ if (renamed !== undefined)
336
+ throw mistake(name, filename, `write ${eventProp(renamed)}={...}`)
337
+ if (event.endsWith(CAPTURE_SUFFIX) && EVENT_PROPS.has(event.slice(0, -CAPTURE_SUFFIX.length)))
338
+ throw mistake(name, filename, `prunelle has no capture props, call addEventListener('${event.slice(0, -CAPTURE_SUFFIX.length)}', fn, { capture: true }) in a ref`)
339
+ throw mistake(name, filename, `no DOM event is named "${event}"; for a custom event, call addEventListener in a ref`)
340
+ }
341
+
342
+ // Plain containers that never take focus unless given a tabIndex, so their onFocus/onBlur never fire.
343
+ const UNFOCUSABLE_TAGS = new Set([
344
+ 'article', 'aside', 'div', 'fieldset', 'footer', 'form', 'header', 'label', 'li', 'main', 'nav', 'ol', 'p', 'section',
345
+ 'span', 'table', 'tbody', 'td', 'th', 'thead', 'tr', 'ul',
346
+ ])
347
+ const FOCUS_EVENTS = new Map([['onFocus', 'onFocusIn'], ['onBlur', 'onFocusOut']])
348
+ // Input types whose change event waits for the field to lose focus.
349
+ const TEXT_INPUT_TYPES = new Set(['text', 'search', 'email', 'url', 'tel', 'password', 'number'])
350
+
351
+ function staticAttr(attributes: readonly JSXAttributeItem[], name: string): JSXAttribute | undefined {
352
+ for (const a of attributes) {
353
+ if (a.type === 'JSXAttribute' && a.name.type === 'JSXIdentifier' && a.name.name === name)
354
+ return a
355
+ }
356
+ return undefined
357
+ }
358
+
359
+ function checkFocusAndChange(tag: string, attributes: readonly JSXAttributeItem[], filename: string, warnings: CompilerWarning[]): void {
360
+ const canFocus = !UNFOCUSABLE_TAGS.has(tag) || attributes.some(a =>
361
+ a.type === 'JSXSpreadAttribute'
362
+ || (a.name.type === 'JSXIdentifier' && ['tabIndex', 'tabindex', 'contentEditable', 'contenteditable'].includes(a.name.name)),
363
+ )
364
+ for (const [prop, bubbling] of FOCUS_EVENTS) {
365
+ const attr = canFocus ? undefined : staticAttr(attributes, prop)
366
+ if (attr !== undefined)
367
+ guarded(attr.start, () => { throw mistake(`${prop} on <${tag}>`, filename, `focus does not bubble, write ${bubbling}={...}, or give the <${tag}> a tabIndex`) })
368
+ }
369
+ const onChange = staticAttr(attributes, 'onChange')
370
+ if (onChange === undefined)
371
+ return
372
+ const type = staticAttr(attributes, 'type')?.value
373
+ const isTextField = tag === 'textarea' || (tag === 'input' && (type === undefined || (type?.type === 'Literal' && typeof type.value === 'string' && TEXT_INPUT_TYPES.has(type.value))))
374
+ if (isTextField)
375
+ warnings.push({ message: `prunelle compiler: onChange on <${tag}> in ${filename} fires when the field loses focus, not per keystroke; write onInput={...} for every keystroke.`, start: onChange.start })
376
+ }
377
+
378
+ /** Throws when a JSX element would compile into something other than what its author meant, and adds to `warnings` what may be meant but usually is not. */
379
+ export function checkMistakes(
380
+ element: JSXElement,
381
+ filename: string,
382
+ code: string,
383
+ warnings: CompilerWarning[],
384
+ resolveTag: (tag: string) => string | undefined = t => (t === 'Suspense' || t === 'Region' ? t : undefined),
385
+ ): void {
386
+ const tag = jsxTagName(element.openingElement.name)
387
+ const canonical = resolveTag(tag)
388
+ const isElement = tag !== '' && !isComponentTag(tag)
389
+ const isCustomElement = tag.includes('-')
390
+ if (tag === 'slot') {
391
+ const named = staticAttr(element.openingElement.attributes, 'name') !== undefined
392
+ guarded(element.start, () => { throw mistake('<slot>', filename, named ? 'prunelle has no slots, pass the named part as its own prop, <Card footer={<p />} />, and write {footer} where it goes' : 'prunelle has no slots, take children as a prop, function Card({ children }) { return <div>{children}</div> }, and write {children} where the content goes') })
393
+ }
394
+ if (canonical === 'Suspense') {
395
+ const children = filterMeaningfulChildren(element.children).length
396
+ if (children > 1)
397
+ guarded(element.start, () => { throw mistake(`<Suspense> with ${children} children`, filename, 'give it one child, and one <Suspense> per async component') })
398
+ }
399
+ const attributes = element.openingElement.attributes
400
+ guarded(element.start, () => checkRegionMarkup(tag, canonical, isElement, attributes, filename, element, resolveTag))
401
+ if (isElement)
402
+ guarded(element.start, () => checkKeepScroll(element, attributes, filename))
403
+ const childrenAttr = isElement ? staticAttr(attributes, 'children') : undefined
404
+ if (isElement && VOID_ELEMENTS.has(tag) && (filterMeaningfulChildren(element.children).length > 0 || childrenAttr !== undefined))
405
+ guarded(element.start, () => { throw mistake(`<${tag}> with children`, filename, `a void element has no content; write <${tag} />`) })
406
+ else if (childrenAttr !== undefined)
407
+ guarded(childrenAttr.start, () => { throw mistake(`children attribute on <${tag}>`, filename, `pass children between tags, <${tag}>...</${tag}>`) })
408
+ const className = isElement && staticAttr(attributes, 'class') !== undefined ? staticAttr(attributes, 'className') : undefined
409
+ if (className !== undefined)
410
+ guarded(className.start, () => { throw mistake('class and className', filename, 'keep one, they set the same attribute') })
411
+ const htmlFor = isElement && staticAttr(attributes, 'for') !== undefined ? staticAttr(attributes, 'htmlFor') : undefined
412
+ if (htmlFor !== undefined)
413
+ guarded(htmlFor.start, () => { throw mistake('for and htmlFor', filename, 'keep one, they set the same attribute') })
414
+ if (isElement)
415
+ checkFocusAndChange(tag, attributes, filename, warnings)
416
+ for (const attr of attributes) {
417
+ if (attr.type !== 'JSXAttribute')
418
+ continue
419
+ guarded(attr.start, () => {
420
+ if (attr.name.type === 'JSXNamespacedName')
421
+ checkNamespacedAttribute(attr.name.namespace.name, attr.name.name.name, filename)
422
+ else if (isElement && attr.name.name === 'innerHTML')
423
+ checkInnerHtml(element, tag, attr, filename, code)
424
+ else if (isElement)
425
+ checkElementAttribute(attr, tag, isCustomElement, filename, code, warnings)
426
+ })
427
+ }
428
+ }
429
+
430
+ // A literal string value of an attribute, when it has one.
431
+ function literalText(attr: JSXAttribute): string | undefined {
432
+ return attr.value?.type === 'Literal' && typeof attr.value.value === 'string' ? attr.value.value : undefined
433
+ }
434
+
435
+ // A region may hold others, each under its own name: the page tells a region from the one around it by name.
436
+ function checkRegionNesting(
437
+ element: JSXElement,
438
+ attributes: readonly JSXAttributeItem[],
439
+ filename: string,
440
+ resolveTag: (tag: string) => string | undefined,
441
+ ): void {
442
+ const outer = staticAttr(attributes, 'name')
443
+ const name = outer === undefined ? undefined : literalText(outer)
444
+ if (name === undefined)
445
+ return
446
+ walk(element.children, (node) => {
447
+ if (node.type !== 'JSXElement' || resolveTag(jsxTagName(node.openingElement.name)) !== 'Region')
448
+ return
449
+ const inner = staticAttr(node.openingElement.attributes, 'name')
450
+ if (inner !== undefined && literalText(inner) === name)
451
+ throw mistake(`<Region name="${name}"> inside <Region name="${name}">`, filename, 'give the inner one its own name')
452
+ })
453
+ }
454
+
455
+ // The markup `regions()` from `@prunellejs/spa` reads: <Region name>, and data-pru-target, data-pru-append and data-pru-live on the elements that can use them.
456
+ function checkRegionMarkup(
457
+ tag: string,
458
+ canonical: string | undefined,
459
+ isElement: boolean,
460
+ attributes: readonly JSXAttributeItem[],
461
+ filename: string,
462
+ element: JSXElement,
463
+ resolveTag: (tag: string) => string | undefined,
464
+ ): void {
465
+ if (canonical === 'Region')
466
+ checkRegionNesting(element, attributes, filename, resolveTag)
467
+ if (canonical === 'Region' && staticAttr(attributes, 'name') === undefined && !attributes.some(a => a.type === 'JSXSpreadAttribute'))
468
+ throw mistake('<Region> without a name', filename, 'write <Region name="pane">, the name a link names in data-pru-target')
469
+ if (!isElement)
470
+ return
471
+ const target = staticAttr(attributes, 'data-pru-target')
472
+ if (target !== undefined && tag !== 'a' && tag !== 'form')
473
+ throw mistake(`data-pru-target on <${tag}>`, filename, 'put it on the <a href> or the <form> that should swap the region')
474
+ const method = tag === 'form' && target !== undefined ? staticAttr(attributes, 'method') : undefined
475
+ const methodText = method === undefined ? undefined : literalText(method)
476
+ if (methodText !== undefined && methodText.toLowerCase() !== 'get' && staticAttr(attributes, 'data-pru-live') !== undefined)
477
+ throw mistake(`data-pru-live on a <form method="${methodText}">`, filename, 'a form that swaps while typing is a GET: use method="get", or drop data-pru-live')
478
+ if (target === undefined && staticAttr(attributes, 'data-pru-append') !== undefined)
479
+ throw mistake('data-pru-append without data-pru-target', filename, 'name the region the next page is appended to: data-pru-target="results"')
480
+ if (staticAttr(attributes, 'data-pru-live') !== undefined && (tag !== 'form' || target === undefined))
481
+ throw mistake('data-pru-live', filename, 'it belongs on a <form method="get" data-pru-target="..."> that swaps while typing')
482
+ }
483
+
484
+ // data-pru-keep-scroll names a panel for the script keeping its scroll offset across page loads, so its key is a literal the page can be read for, and an element inside the panel cannot take the same key.
485
+ function checkKeepScroll(element: JSXElement, attributes: readonly JSXAttributeItem[], filename: string): void {
486
+ const attr = staticAttr(attributes, 'data-pru-keep-scroll')
487
+ if (attr === undefined)
488
+ return
489
+ const keyOf = (a: JSXAttribute) => a.value?.type === 'JSXExpressionContainer' ? staticStringValue(a.value.expression as Node) : literalText(a)
490
+ const key = keyOf(attr)
491
+ if (key === undefined || key.trim() === '' || /["<>&]/.test(key))
492
+ throw mistake('data-pru-keep-scroll without a literal key', filename, 'write data-pru-keep-scroll="side": a plain string naming the panel, with no quote, < > or &')
493
+ walk(element.children, (node) => {
494
+ if (node.type !== 'JSXElement')
495
+ return
496
+ const inner = staticAttr(node.openingElement.attributes, 'data-pru-keep-scroll')
497
+ if (inner !== undefined && keyOf(inner) === key)
498
+ throw mistake(`data-pru-keep-scroll="${key}" inside data-pru-keep-scroll="${key}"`, filename, 'give each panel its own key')
499
+ })
500
+ }
501
+
502
+ const DIRECTIVE_FIXES: Record<string, string> = {
503
+ 'use client': 'delete it, prunelle makes a component an island by itself when it uses state or handlers',
504
+ 'use server': 'delete it, a server function is an action() exported from its own file',
505
+ }
506
+
507
+ /** Throws on a React or Next habit plain code carries: a 'use client' or 'use server' directive, or an effect(async ...). */
508
+ export function checkHabits(node: Node, isEffect: (callee: string) => boolean, filename: string): void {
509
+ if (node.type === 'ExpressionStatement' && 'directive' in node && typeof node.directive === 'string') {
510
+ const fix = DIRECTIVE_FIXES[node.directive]
511
+ if (fix !== undefined)
512
+ guarded(node.start, () => { throw mistake(`'${node.directive}'`, filename, fix) })
513
+ return
514
+ }
515
+ if (node.type !== 'CallExpression' || !isEffect(calleeName(node.callee)))
516
+ return
517
+ const fn = node.arguments[0]
518
+ if ((fn?.type === 'ArrowFunctionExpression' || fn?.type === 'FunctionExpression') && fn.async)
519
+ guarded(fn.start, () => { throw mistake('effect(async ...)', filename, 'effect tracks only what it reads before the first await and takes a cleanup function, not a Promise; start the async work inside a plain effect') })
520
+ }
@@ -0,0 +1,50 @@
1
+ // Finds the `process.env.NODE_ENV` reads of a module: `prunelle build` never sets it, so a page it prerenders reads it unset (#3681).
2
+
3
+ import type { MemberExpression, Node, Program } from 'oxc-parser'
4
+ import { walk } from './ast'
5
+ import { topLevelNames } from './browser-globals'
6
+ import { unwrapTsExpr } from './shared'
7
+
8
+ const MENTIONS_RE = /\bNODE_ENV\b/
9
+
10
+ function keyOf(member: MemberExpression): string | undefined {
11
+ if (!member.computed && member.property.type === 'Identifier')
12
+ return member.property.name
13
+ if (member.computed && member.property.type === 'Literal' && typeof member.property.value === 'string')
14
+ return member.property.value
15
+ return undefined
16
+ }
17
+
18
+ // `process.env` or `globalThis.process.env`, with `process` not bound by the module.
19
+ function isProcessEnv(node: Node, shadowing: ReadonlySet<string>): boolean {
20
+ const env = unwrapTsExpr(node)
21
+ if (env.type !== 'MemberExpression' || keyOf(env) !== 'env')
22
+ return false
23
+ const proc = unwrapTsExpr(env.object)
24
+ if (proc.type === 'Identifier')
25
+ return proc.name === 'process' && !shadowing.has('process')
26
+ if (proc.type !== 'MemberExpression' || keyOf(proc) !== 'process')
27
+ return false
28
+ const base = unwrapTsExpr(proc.object)
29
+ return base.type === 'Identifier' && base.name === 'globalThis' && !shadowing.has('globalThis')
30
+ }
31
+
32
+ /** Offsets of each `process.env.NODE_ENV` read, `const { NODE_ENV } = process.env` included. */
33
+ export function findNodeEnvReads(program: Program, code: string): number[] {
34
+ if (!MENTIONS_RE.test(code))
35
+ return []
36
+ const shadowing = topLevelNames(program)
37
+ const reads: number[] = []
38
+ walk(program, (node) => {
39
+ if (node.type === 'MemberExpression' && keyOf(node) === 'NODE_ENV' && isProcessEnv(node.object, shadowing)) {
40
+ reads.push(node.start)
41
+ }
42
+ else if (node.type === 'VariableDeclarator' && node.id.type === 'ObjectPattern' && node.init !== null && isProcessEnv(node.init, shadowing)) {
43
+ for (const prop of node.id.properties) {
44
+ if (prop.type === 'Property' && !prop.computed && prop.key.type === 'Identifier' && prop.key.name === 'NODE_ENV')
45
+ reads.push(prop.start)
46
+ }
47
+ }
48
+ })
49
+ return reads
50
+ }