rnwind 0.0.1 → 0.0.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 (330) hide show
  1. package/lib/cjs/core/parser/animation.cjs +427 -0
  2. package/lib/cjs/core/parser/animation.cjs.map +1 -0
  3. package/lib/cjs/core/parser/animation.d.ts +126 -0
  4. package/lib/cjs/core/parser/border-dispatcher.cjs +180 -0
  5. package/lib/cjs/core/parser/border-dispatcher.cjs.map +1 -0
  6. package/lib/cjs/core/parser/border-dispatcher.d.ts +15 -0
  7. package/lib/cjs/core/parser/case-convert.cjs +15 -0
  8. package/lib/cjs/core/parser/case-convert.cjs.map +1 -0
  9. package/lib/cjs/core/parser/case-convert.d.ts +6 -0
  10. package/lib/cjs/core/parser/color-properties-dispatcher.cjs +84 -0
  11. package/lib/cjs/core/parser/color-properties-dispatcher.cjs.map +1 -0
  12. package/lib/cjs/core/parser/color-properties-dispatcher.d.ts +19 -0
  13. package/lib/cjs/core/parser/color.cjs +164 -0
  14. package/lib/cjs/core/parser/color.cjs.map +1 -0
  15. package/lib/cjs/core/parser/color.d.ts +12 -0
  16. package/lib/cjs/core/parser/constants.cjs +21 -0
  17. package/lib/cjs/core/parser/constants.cjs.map +1 -0
  18. package/lib/cjs/core/parser/constants.d.ts +8 -0
  19. package/lib/cjs/core/parser/declaration.cjs +347 -0
  20. package/lib/cjs/core/parser/declaration.cjs.map +1 -0
  21. package/lib/cjs/core/parser/declaration.d.ts +15 -0
  22. package/lib/cjs/core/parser/gradient.cjs +132 -0
  23. package/lib/cjs/core/parser/gradient.cjs.map +1 -0
  24. package/lib/cjs/core/parser/gradient.d.ts +59 -0
  25. package/lib/cjs/core/parser/haptics.cjs +73 -0
  26. package/lib/cjs/core/parser/haptics.cjs.map +1 -0
  27. package/lib/cjs/core/parser/haptics.d.ts +47 -0
  28. package/lib/cjs/core/parser/index.d.ts +8 -0
  29. package/lib/cjs/core/parser/keyframes.cjs +95 -0
  30. package/lib/cjs/core/parser/keyframes.cjs.map +1 -0
  31. package/lib/cjs/core/parser/keyframes.d.ts +26 -0
  32. package/lib/cjs/core/parser/layout-dispatcher.cjs +100 -0
  33. package/lib/cjs/core/parser/layout-dispatcher.cjs.map +1 -0
  34. package/lib/cjs/core/parser/layout-dispatcher.d.ts +14 -0
  35. package/lib/cjs/core/parser/length.cjs +96 -0
  36. package/lib/cjs/core/parser/length.cjs.map +1 -0
  37. package/lib/cjs/core/parser/length.d.ts +48 -0
  38. package/lib/cjs/core/parser/motion-dispatcher.cjs +77 -0
  39. package/lib/cjs/core/parser/motion-dispatcher.cjs.map +1 -0
  40. package/lib/cjs/core/parser/motion-dispatcher.d.ts +11 -0
  41. package/lib/cjs/core/parser/property.cjs +22 -0
  42. package/lib/cjs/core/parser/property.cjs.map +1 -0
  43. package/lib/cjs/core/parser/property.d.ts +8 -0
  44. package/lib/cjs/core/parser/safe-area.cjs +404 -0
  45. package/lib/cjs/core/parser/safe-area.cjs.map +1 -0
  46. package/lib/cjs/core/parser/safe-area.d.ts +39 -0
  47. package/lib/cjs/core/parser/selector.cjs +22 -0
  48. package/lib/cjs/core/parser/selector.cjs.map +1 -0
  49. package/lib/cjs/core/parser/selector.d.ts +11 -0
  50. package/lib/cjs/core/parser/shorthand.cjs +156 -0
  51. package/lib/cjs/core/parser/shorthand.cjs.map +1 -0
  52. package/lib/cjs/core/parser/shorthand.d.ts +61 -0
  53. package/lib/cjs/core/parser/text-truncate.cjs +78 -0
  54. package/lib/cjs/core/parser/text-truncate.cjs.map +1 -0
  55. package/lib/cjs/core/parser/text-truncate.d.ts +44 -0
  56. package/lib/cjs/core/parser/theme-vars.cjs +414 -0
  57. package/lib/cjs/core/parser/theme-vars.cjs.map +1 -0
  58. package/lib/cjs/core/parser/theme-vars.d.ts +61 -0
  59. package/lib/cjs/core/parser/tokens.cjs +304 -0
  60. package/lib/cjs/core/parser/tokens.cjs.map +1 -0
  61. package/lib/cjs/core/parser/tokens.d.ts +45 -0
  62. package/lib/cjs/core/parser/transform.cjs +198 -0
  63. package/lib/cjs/core/parser/transform.cjs.map +1 -0
  64. package/lib/cjs/core/parser/transform.d.ts +36 -0
  65. package/lib/cjs/core/parser/tw-parser.cjs +1567 -0
  66. package/lib/cjs/core/parser/tw-parser.cjs.map +1 -0
  67. package/lib/cjs/core/parser/tw-parser.d.ts +194 -0
  68. package/lib/cjs/core/parser/types.d.ts +37 -0
  69. package/lib/cjs/core/parser/typography-dispatcher.cjs +93 -0
  70. package/lib/cjs/core/parser/typography-dispatcher.cjs.map +1 -0
  71. package/lib/cjs/core/parser/typography-dispatcher.d.ts +11 -0
  72. package/lib/cjs/core/parser/typography.cjs +97 -0
  73. package/lib/cjs/core/parser/typography.cjs.map +1 -0
  74. package/lib/cjs/core/parser/typography.d.ts +43 -0
  75. package/lib/cjs/core/style-builder/build-style.cjs +397 -0
  76. package/lib/cjs/core/style-builder/build-style.cjs.map +1 -0
  77. package/lib/cjs/core/style-builder/build-style.d.ts +54 -0
  78. package/lib/cjs/core/style-builder/index.d.ts +3 -0
  79. package/lib/cjs/core/style-builder/union-builder.cjs +326 -0
  80. package/lib/cjs/core/style-builder/union-builder.cjs.map +1 -0
  81. package/lib/cjs/core/style-builder/union-builder.d.ts +128 -0
  82. package/lib/cjs/core/types.d.ts +14 -0
  83. package/lib/cjs/metro/dts.cjs +127 -0
  84. package/lib/cjs/metro/dts.cjs.map +1 -0
  85. package/lib/cjs/metro/dts.d.ts +16 -0
  86. package/lib/cjs/metro/index.cjs +19 -0
  87. package/lib/cjs/metro/index.cjs.map +1 -0
  88. package/lib/cjs/metro/index.d.ts +9 -0
  89. package/lib/cjs/metro/resolver.cjs +47 -0
  90. package/lib/cjs/metro/resolver.cjs.map +1 -0
  91. package/lib/cjs/metro/resolver.d.ts +22 -0
  92. package/lib/cjs/metro/state.cjs +251 -0
  93. package/lib/cjs/metro/state.cjs.map +1 -0
  94. package/lib/cjs/metro/state.d.ts +72 -0
  95. package/lib/cjs/metro/transform-ast.cjs +1255 -0
  96. package/lib/cjs/metro/transform-ast.cjs.map +1 -0
  97. package/lib/cjs/metro/transform-ast.d.ts +73 -0
  98. package/lib/cjs/metro/transformer.cjs +345 -0
  99. package/lib/cjs/metro/transformer.cjs.map +1 -0
  100. package/lib/cjs/metro/transformer.d.ts +47 -0
  101. package/lib/cjs/metro/warn-unknown-classes.cjs +86 -0
  102. package/lib/cjs/metro/warn-unknown-classes.cjs.map +1 -0
  103. package/lib/cjs/metro/warn-unknown-classes.d.ts +21 -0
  104. package/lib/cjs/metro/with-config.cjs +196 -0
  105. package/lib/cjs/metro/with-config.cjs.map +1 -0
  106. package/lib/cjs/metro/with-config.d.ts +57 -0
  107. package/lib/cjs/runtime/chain-handlers.cjs +37 -0
  108. package/lib/cjs/runtime/chain-handlers.cjs.map +1 -0
  109. package/lib/cjs/runtime/chain-handlers.d.ts +33 -0
  110. package/lib/cjs/runtime/components/rnwind-provider.cjs +98 -0
  111. package/lib/cjs/runtime/components/rnwind-provider.cjs.map +1 -0
  112. package/lib/cjs/runtime/components/rnwind-provider.d.ts +84 -0
  113. package/lib/cjs/runtime/gradient-types.d.ts +58 -0
  114. package/lib/cjs/runtime/haptics.cjs +113 -0
  115. package/lib/cjs/runtime/haptics.cjs.map +1 -0
  116. package/lib/cjs/runtime/haptics.d.ts +48 -0
  117. package/lib/cjs/runtime/hooks/use-css.cjs +21 -0
  118. package/lib/cjs/runtime/hooks/use-css.cjs.map +1 -0
  119. package/lib/cjs/runtime/hooks/use-css.d.ts +11 -0
  120. package/lib/cjs/runtime/hooks/use-interact.cjs +46 -0
  121. package/lib/cjs/runtime/hooks/use-interact.cjs.map +1 -0
  122. package/lib/cjs/runtime/hooks/use-interact.d.ts +42 -0
  123. package/lib/cjs/runtime/hooks/use-scheme.cjs +68 -0
  124. package/lib/cjs/runtime/hooks/use-scheme.cjs.map +1 -0
  125. package/lib/cjs/runtime/hooks/use-scheme.d.ts +34 -0
  126. package/lib/cjs/runtime/index.cjs +45 -0
  127. package/lib/cjs/runtime/index.cjs.map +1 -0
  128. package/lib/cjs/runtime/index.d.ts +27 -0
  129. package/lib/cjs/runtime/interactive-box.cjs +35 -0
  130. package/lib/cjs/runtime/interactive-box.cjs.map +1 -0
  131. package/lib/cjs/runtime/interactive-box.d.ts +40 -0
  132. package/lib/cjs/runtime/lookup-css.cjs +542 -0
  133. package/lib/cjs/runtime/lookup-css.cjs.map +1 -0
  134. package/lib/cjs/runtime/lookup-css.d.ts +164 -0
  135. package/lib/cjs/runtime/types.d.ts +29 -0
  136. package/lib/cjs/testing/index.cjs +367 -0
  137. package/lib/cjs/testing/index.cjs.map +1 -0
  138. package/lib/cjs/testing/index.d.ts +145 -0
  139. package/lib/esm/core/parser/animation.d.ts +126 -0
  140. package/lib/esm/core/parser/animation.mjs +408 -0
  141. package/lib/esm/core/parser/animation.mjs.map +1 -0
  142. package/lib/esm/core/parser/border-dispatcher.d.ts +15 -0
  143. package/lib/esm/core/parser/border-dispatcher.mjs +178 -0
  144. package/lib/esm/core/parser/border-dispatcher.mjs.map +1 -0
  145. package/lib/esm/core/parser/case-convert.d.ts +6 -0
  146. package/lib/esm/core/parser/case-convert.mjs +13 -0
  147. package/lib/esm/core/parser/case-convert.mjs.map +1 -0
  148. package/lib/esm/core/parser/color-properties-dispatcher.d.ts +19 -0
  149. package/lib/esm/core/parser/color-properties-dispatcher.mjs +82 -0
  150. package/lib/esm/core/parser/color-properties-dispatcher.mjs.map +1 -0
  151. package/lib/esm/core/parser/color.d.ts +12 -0
  152. package/lib/esm/core/parser/color.mjs +162 -0
  153. package/lib/esm/core/parser/color.mjs.map +1 -0
  154. package/lib/esm/core/parser/constants.d.ts +8 -0
  155. package/lib/esm/core/parser/constants.mjs +13 -0
  156. package/lib/esm/core/parser/constants.mjs.map +1 -0
  157. package/lib/esm/core/parser/declaration.d.ts +15 -0
  158. package/lib/esm/core/parser/declaration.mjs +345 -0
  159. package/lib/esm/core/parser/declaration.mjs.map +1 -0
  160. package/lib/esm/core/parser/gradient.d.ts +59 -0
  161. package/lib/esm/core/parser/gradient.mjs +130 -0
  162. package/lib/esm/core/parser/gradient.mjs.map +1 -0
  163. package/lib/esm/core/parser/haptics.d.ts +47 -0
  164. package/lib/esm/core/parser/haptics.mjs +71 -0
  165. package/lib/esm/core/parser/haptics.mjs.map +1 -0
  166. package/lib/esm/core/parser/index.d.ts +8 -0
  167. package/lib/esm/core/parser/keyframes.d.ts +26 -0
  168. package/lib/esm/core/parser/keyframes.mjs +91 -0
  169. package/lib/esm/core/parser/keyframes.mjs.map +1 -0
  170. package/lib/esm/core/parser/layout-dispatcher.d.ts +14 -0
  171. package/lib/esm/core/parser/layout-dispatcher.mjs +98 -0
  172. package/lib/esm/core/parser/layout-dispatcher.mjs.map +1 -0
  173. package/lib/esm/core/parser/length.d.ts +48 -0
  174. package/lib/esm/core/parser/length.mjs +90 -0
  175. package/lib/esm/core/parser/length.mjs.map +1 -0
  176. package/lib/esm/core/parser/motion-dispatcher.d.ts +11 -0
  177. package/lib/esm/core/parser/motion-dispatcher.mjs +75 -0
  178. package/lib/esm/core/parser/motion-dispatcher.mjs.map +1 -0
  179. package/lib/esm/core/parser/property.d.ts +8 -0
  180. package/lib/esm/core/parser/property.mjs +20 -0
  181. package/lib/esm/core/parser/property.mjs.map +1 -0
  182. package/lib/esm/core/parser/safe-area.d.ts +39 -0
  183. package/lib/esm/core/parser/safe-area.mjs +402 -0
  184. package/lib/esm/core/parser/safe-area.mjs.map +1 -0
  185. package/lib/esm/core/parser/selector.d.ts +11 -0
  186. package/lib/esm/core/parser/selector.mjs +20 -0
  187. package/lib/esm/core/parser/selector.mjs.map +1 -0
  188. package/lib/esm/core/parser/shorthand.d.ts +61 -0
  189. package/lib/esm/core/parser/shorthand.mjs +148 -0
  190. package/lib/esm/core/parser/shorthand.mjs.map +1 -0
  191. package/lib/esm/core/parser/text-truncate.d.ts +44 -0
  192. package/lib/esm/core/parser/text-truncate.mjs +75 -0
  193. package/lib/esm/core/parser/text-truncate.mjs.map +1 -0
  194. package/lib/esm/core/parser/theme-vars.d.ts +61 -0
  195. package/lib/esm/core/parser/theme-vars.mjs +409 -0
  196. package/lib/esm/core/parser/theme-vars.mjs.map +1 -0
  197. package/lib/esm/core/parser/tokens.d.ts +45 -0
  198. package/lib/esm/core/parser/tokens.mjs +298 -0
  199. package/lib/esm/core/parser/tokens.mjs.map +1 -0
  200. package/lib/esm/core/parser/transform.d.ts +36 -0
  201. package/lib/esm/core/parser/transform.mjs +193 -0
  202. package/lib/esm/core/parser/transform.mjs.map +1 -0
  203. package/lib/esm/core/parser/tw-parser.d.ts +194 -0
  204. package/lib/esm/core/parser/tw-parser.mjs +1565 -0
  205. package/lib/esm/core/parser/tw-parser.mjs.map +1 -0
  206. package/lib/esm/core/parser/types.d.ts +37 -0
  207. package/lib/esm/core/parser/typography-dispatcher.d.ts +11 -0
  208. package/lib/esm/core/parser/typography-dispatcher.mjs +91 -0
  209. package/lib/esm/core/parser/typography-dispatcher.mjs.map +1 -0
  210. package/lib/esm/core/parser/typography.d.ts +43 -0
  211. package/lib/esm/core/parser/typography.mjs +91 -0
  212. package/lib/esm/core/parser/typography.mjs.map +1 -0
  213. package/lib/esm/core/style-builder/build-style.d.ts +54 -0
  214. package/lib/esm/core/style-builder/build-style.mjs +395 -0
  215. package/lib/esm/core/style-builder/build-style.mjs.map +1 -0
  216. package/lib/esm/core/style-builder/index.d.ts +3 -0
  217. package/lib/esm/core/style-builder/union-builder.d.ts +128 -0
  218. package/lib/esm/core/style-builder/union-builder.mjs +324 -0
  219. package/lib/esm/core/style-builder/union-builder.mjs.map +1 -0
  220. package/lib/esm/core/types.d.ts +14 -0
  221. package/lib/esm/metro/dts.d.ts +16 -0
  222. package/lib/esm/metro/dts.mjs +125 -0
  223. package/lib/esm/metro/dts.mjs.map +1 -0
  224. package/lib/esm/metro/index.d.ts +9 -0
  225. package/lib/esm/metro/index.mjs +6 -0
  226. package/lib/esm/metro/index.mjs.map +1 -0
  227. package/lib/esm/metro/resolver.d.ts +22 -0
  228. package/lib/esm/metro/resolver.mjs +43 -0
  229. package/lib/esm/metro/resolver.mjs.map +1 -0
  230. package/lib/esm/metro/state.d.ts +72 -0
  231. package/lib/esm/metro/state.mjs +243 -0
  232. package/lib/esm/metro/state.mjs.map +1 -0
  233. package/lib/esm/metro/transform-ast.d.ts +73 -0
  234. package/lib/esm/metro/transform-ast.mjs +1234 -0
  235. package/lib/esm/metro/transform-ast.mjs.map +1 -0
  236. package/lib/esm/metro/transformer.d.ts +47 -0
  237. package/lib/esm/metro/transformer.mjs +322 -0
  238. package/lib/esm/metro/transformer.mjs.map +1 -0
  239. package/lib/esm/metro/warn-unknown-classes.d.ts +21 -0
  240. package/lib/esm/metro/warn-unknown-classes.mjs +84 -0
  241. package/lib/esm/metro/warn-unknown-classes.mjs.map +1 -0
  242. package/lib/esm/metro/with-config.d.ts +57 -0
  243. package/lib/esm/metro/with-config.mjs +194 -0
  244. package/lib/esm/metro/with-config.mjs.map +1 -0
  245. package/lib/esm/runtime/chain-handlers.d.ts +33 -0
  246. package/lib/esm/runtime/chain-handlers.mjs +34 -0
  247. package/lib/esm/runtime/chain-handlers.mjs.map +1 -0
  248. package/lib/esm/runtime/components/rnwind-provider.d.ts +84 -0
  249. package/lib/esm/runtime/components/rnwind-provider.mjs +94 -0
  250. package/lib/esm/runtime/components/rnwind-provider.mjs.map +1 -0
  251. package/lib/esm/runtime/gradient-types.d.ts +58 -0
  252. package/lib/esm/runtime/haptics.d.ts +48 -0
  253. package/lib/esm/runtime/haptics.mjs +110 -0
  254. package/lib/esm/runtime/haptics.mjs.map +1 -0
  255. package/lib/esm/runtime/hooks/use-css.d.ts +11 -0
  256. package/lib/esm/runtime/hooks/use-css.mjs +19 -0
  257. package/lib/esm/runtime/hooks/use-css.mjs.map +1 -0
  258. package/lib/esm/runtime/hooks/use-interact.d.ts +42 -0
  259. package/lib/esm/runtime/hooks/use-interact.mjs +44 -0
  260. package/lib/esm/runtime/hooks/use-interact.mjs.map +1 -0
  261. package/lib/esm/runtime/hooks/use-scheme.d.ts +34 -0
  262. package/lib/esm/runtime/hooks/use-scheme.mjs +63 -0
  263. package/lib/esm/runtime/hooks/use-scheme.mjs.map +1 -0
  264. package/lib/esm/runtime/index.d.ts +27 -0
  265. package/lib/esm/runtime/index.mjs +18 -0
  266. package/lib/esm/runtime/index.mjs.map +1 -0
  267. package/lib/esm/runtime/interactive-box.d.ts +40 -0
  268. package/lib/esm/runtime/interactive-box.mjs +33 -0
  269. package/lib/esm/runtime/interactive-box.mjs.map +1 -0
  270. package/lib/esm/runtime/lookup-css.d.ts +164 -0
  271. package/lib/esm/runtime/lookup-css.mjs +531 -0
  272. package/lib/esm/runtime/lookup-css.mjs.map +1 -0
  273. package/lib/esm/runtime/types.d.ts +29 -0
  274. package/lib/esm/testing/index.d.ts +145 -0
  275. package/lib/esm/testing/index.mjs +344 -0
  276. package/lib/esm/testing/index.mjs.map +1 -0
  277. package/package.json +79 -13
  278. package/preset.css +1171 -0
  279. package/src/core/parser/animation.ts +404 -0
  280. package/src/core/parser/border-dispatcher.ts +176 -0
  281. package/src/core/parser/case-convert.ts +10 -0
  282. package/src/core/parser/color-properties-dispatcher.ts +78 -0
  283. package/src/core/parser/color.ts +157 -0
  284. package/src/core/parser/constants.ts +11 -0
  285. package/src/core/parser/declaration.ts +340 -0
  286. package/src/core/parser/gradient.ts +148 -0
  287. package/src/core/parser/haptics.ts +88 -0
  288. package/src/core/parser/index.ts +8 -0
  289. package/src/core/parser/keyframes.ts +84 -0
  290. package/src/core/parser/layout-dispatcher.ts +92 -0
  291. package/src/core/parser/length.ts +100 -0
  292. package/src/core/parser/motion-dispatcher.ts +89 -0
  293. package/src/core/parser/property.ts +15 -0
  294. package/src/core/parser/safe-area.ts +404 -0
  295. package/src/core/parser/selector.ts +17 -0
  296. package/src/core/parser/shorthand.ts +152 -0
  297. package/src/core/parser/text-truncate.ts +79 -0
  298. package/src/core/parser/theme-vars.ts +412 -0
  299. package/src/core/parser/tokens.ts +286 -0
  300. package/src/core/parser/transform.ts +195 -0
  301. package/src/core/parser/tw-parser.ts +1709 -0
  302. package/src/core/parser/types.ts +45 -0
  303. package/src/core/parser/typography-dispatcher.ts +83 -0
  304. package/src/core/parser/typography.ts +83 -0
  305. package/src/core/style-builder/build-style.ts +442 -0
  306. package/src/core/style-builder/index.ts +3 -0
  307. package/src/core/style-builder/union-builder.ts +328 -0
  308. package/src/core/types.ts +15 -0
  309. package/src/metro/dts.ts +128 -0
  310. package/src/metro/index.ts +9 -0
  311. package/src/metro/resolver.ts +42 -0
  312. package/src/metro/state.ts +257 -0
  313. package/src/metro/transform-ast.ts +1498 -0
  314. package/src/metro/transformer.ts +347 -0
  315. package/src/metro/warn-unknown-classes.ts +79 -0
  316. package/src/metro/with-config.ts +229 -0
  317. package/src/runtime/chain-handlers.ts +47 -0
  318. package/src/runtime/components/rnwind-provider.tsx +144 -0
  319. package/src/runtime/gradient-types.ts +60 -0
  320. package/src/runtime/haptics.ts +120 -0
  321. package/src/runtime/hooks/use-css.ts +16 -0
  322. package/src/runtime/hooks/use-interact.ts +65 -0
  323. package/src/runtime/hooks/use-scheme.ts +63 -0
  324. package/src/runtime/index.ts +54 -0
  325. package/src/runtime/interactive-box.tsx +57 -0
  326. package/src/runtime/lookup-css.ts +628 -0
  327. package/src/runtime/types.ts +32 -0
  328. package/src/testing/index.ts +507 -0
  329. package/src/types/tailwindcss-node.d.ts +33 -0
  330. package/src/index.ts +0 -1
@@ -0,0 +1,1234 @@
1
+ import * as t from '@babel/types';
2
+ import traverseImport from '@babel/traverse';
3
+ import { createHash } from 'node:crypto';
4
+ import { mayContainTextTruncate, detectTextTruncate } from '../core/parser/text-truncate.mjs';
5
+
6
+ const traverse = traverseImport.default ?? traverseImport;
7
+ /**
8
+ * Name of the internal rnwind context hook the transformer injects at
9
+ * each component top. Must start with `use` + uppercase letter so
10
+ * react-refresh's babel plugin folds it into each component's
11
+ * fast-refresh signature — otherwise stale signatures preserve fiber
12
+ * state across transformer changes and the rendered hook list shifts,
13
+ * which React surfaces as "change in the order of Hooks" runtime errors.
14
+ */
15
+ const USE_RNWIND_INTERNAL = 'useR_';
16
+ /** Internal alias of `useMountHaptic` — emit `_hm` instead of the public name to dodge shadowing. */
17
+ const USE_MOUNT_HAPTIC = '_hm';
18
+ /** Internal alias of `triggerHaptic` — `_ht` so a user-defined `triggerHaptic` won't shadow it. */
19
+ const TRIGGER_HAPTIC = '_ht';
20
+ /** Internal alias of `lookupCss` — `_l` so user can't accidentally shadow at the JSX call sites. */
21
+ const LOOKUP_CSS = '_l';
22
+ /** Package specifier rnwind runtime primitives import from. */
23
+ const RUNTIME_MODULE = 'rnwind';
24
+ /** Local binding name for the rnwind context hook result inside components. */
25
+ const CONTEXT_BINDING = '_t';
26
+ /**
27
+ * Name of the per-instance wrapper component rnwind substitutes for any
28
+ * JSX site that uses active: / focus: variants. One `useInteract()` lives
29
+ * INSIDE each InteractiveBox so siblings never share state — a previous
30
+ * design that called `useInteract()` once per enclosing component caused
31
+ * "press one button, all buttons glow" because every site read the same
32
+ * `_i.state` reference.
33
+ */
34
+ const INTERACTIVE_BOX = '_ib';
35
+ /** Leading variant prefixes that trigger interact wiring. */
36
+ const INTERACTIVE_PREFIXES = ['active:', 'focus:'];
37
+ /**
38
+ * Regex matching the atom-name shape of every `*-safe` utility the
39
+ * rnwind preset ships. Used by the transformer to decide whether a
40
+ * file needs the `const _i = useInsets()` binding injected. Matches
41
+ * three families:
42
+ * - `*-safe` exactly (e.g. `pt-safe`, `inset-safe`).
43
+ * - `*-safe-or-<N>` / `*-safe-offset-<N>` (e.g. `mt-safe-or-4`).
44
+ * - `*-screen-safe` (e.g. `h-screen-safe`, `min-h-screen-safe`).
45
+ *
46
+ * Dynamic className expressions always inject `_i` — we can't inspect
47
+ * the string at build time, and the runtime slow path reads `SAFE_ATOMS`
48
+ * to pick up any user-defined safe utility the preset doesn't cover.
49
+ */
50
+ const SAFE_ATOM_PATTERN = /(?:(?:^|-)safe(?:-or-|-offset-|$))|(?:-screen-safe$)/;
51
+ /**
52
+ * RN host components that NEVER emit press / focus events — wrapping
53
+ * them in `<InteractiveBox>` buys nothing and costs `useInteract()`'s
54
+ * hooks per mount. When the JSX tag is in this set, we skip the
55
+ * wrapper even for dynamic `className={…}` expressions. On a list of
56
+ * 1000 `<View className={rowCls}>` rows this drops 6 × 1000 hook
57
+ * initialisations per mount.
58
+ *
59
+ * The set is intentionally conservative — anything not listed keeps
60
+ * the wrapper. Custom components, `Animated.View`, and
61
+ * `Pressable`/`TextInput` all still get interactive variants on
62
+ * dynamic className.
63
+ */
64
+ const NON_INTERACTIVE_HOST_TAGS = new Set([
65
+ 'View',
66
+ 'Text',
67
+ 'ScrollView',
68
+ 'SafeAreaView',
69
+ 'Image',
70
+ 'ImageBackground',
71
+ 'FlatList',
72
+ 'SectionList',
73
+ 'VirtualizedList',
74
+ 'KeyboardAvoidingView',
75
+ 'ActivityIndicator',
76
+ 'RefreshControl',
77
+ 'Fragment',
78
+ ]);
79
+ /**
80
+ * Built-in prefix that's always active — covers the React Native
81
+ * ecosystem's `contentContainerStyle` pattern on ScrollView / FlatList /
82
+ * SectionList. Users who pass `classNamePrefixes` get their list merged
83
+ * on top, never replacing this.
84
+ */
85
+ const DEFAULT_CLASSNAME_PREFIXES = ['contentContainer'];
86
+ /**
87
+ * Mutate an already-parsed Babel AST in place:
88
+ * - Rewrite every JSX `className="…"` / `className={expr}` attribute to
89
+ * `style={lookupCss(<ref|expr>, _s, <existingStyle>)}`. Static string
90
+ * literals get a module-scope `const _c_<hash> = Object.freeze(['a',
91
+ * 'b'])` hoist so React sees an identity-stable array across renders.
92
+ * - Preserve any adjacent `style={…}` prop — it becomes the third
93
+ * argument so user inline styles keep working (and trump atoms).
94
+ * - Inject `const _s = useScheme()` at the top of the enclosing
95
+ * function component (idempotent — one injection per component).
96
+ * - Prepend `import { lookupCss, useScheme } from 'rnwind'`.
97
+ * - Prepend a side-effect `import 'rnwind/__generated/style'` so the
98
+ * union registry is loaded before any hoist runs.
99
+ * @param ast Babel File AST (usually handed to us by Metro).
100
+ * @param options Extra inputs — side-effect import specifiers + parser metadata.
101
+ * @returns Transform outcome flags + the hoist table.
102
+ */
103
+ function transformAst(ast, options) {
104
+ const hoister = createHoister();
105
+ const gradientHoister = createGradientHoister();
106
+ const literals = [];
107
+ const prefixSet = buildPrefixSet(options.classNamePrefixes);
108
+ const hapticHoister = createHapticHoister();
109
+ const rewriteCtx = {
110
+ needsInsets: false,
111
+ gradientAtoms: options.gradientAtoms ?? EMPTY_GRADIENT_ATOMS,
112
+ gradientHoister,
113
+ hapticAtoms: options.hapticAtoms ?? EMPTY_HAPTIC_ATOMS,
114
+ hapticHoister,
115
+ mountByComponent: new Map(),
116
+ needsHapticsHook: false,
117
+ };
118
+ let touched = false;
119
+ let usedLookupCss = false;
120
+ let usedInteractiveBox = false;
121
+ traverse(ast, {
122
+ JSXAttribute(attributePath) {
123
+ const { node } = attributePath;
124
+ if (!t.isJSXIdentifier(node.name))
125
+ return;
126
+ const target = classifyAttributeName(node.name.name, prefixSet);
127
+ if (!target)
128
+ return;
129
+ const rewritten = rewriteClassNameAttribute(attributePath, hoister, literals, rewriteCtx, target);
130
+ if (!rewritten)
131
+ return;
132
+ touched = true;
133
+ if (rewritten.injectedInteract)
134
+ usedInteractiveBox = true;
135
+ else
136
+ usedLookupCss = true;
137
+ },
138
+ });
139
+ if (!touched && options.styleSpecifiers.length === 0)
140
+ return { touched: false, hoistedArrays: hoister.entries, literals };
141
+ // Inject `useMountHaptic(<hoisted>)` per component that had bare
142
+ // haptic atoms. Done post-traversal so we know every aggregated
143
+ // request up front and can hoist one frozen array per component.
144
+ injectMountHapticCalls(rewriteCtx);
145
+ const usedMountHaptic = rewriteCtx.mountByComponent.size > 0;
146
+ prependRuntimeImports(ast, {
147
+ usedLookupCss,
148
+ usedInteractiveBox,
149
+ usedMountHaptic,
150
+ usedTriggerHaptic: rewriteCtx.needsHapticsHook,
151
+ touched,
152
+ }, options.styleSpecifiers);
153
+ if (hoister.entries.size > 0)
154
+ injectHoistedConsts(ast, hoister.entries);
155
+ if (gradientHoister.entries.size > 0)
156
+ injectGradientConsts(ast, gradientHoister.entries);
157
+ if (hapticHoister.entries.size > 0)
158
+ injectHapticConsts(ast, hapticHoister.entries);
159
+ return { touched, hoistedArrays: hoister.entries, literals };
160
+ }
161
+ /** Default empty gradient-atoms map used when callers don't supply one. */
162
+ const EMPTY_GRADIENT_ATOMS = new Map();
163
+ /** Default empty haptic-atoms map used when callers don't supply one. */
164
+ const EMPTY_HAPTIC_ATOMS = new Map();
165
+ /**
166
+ * Merge the built-in default prefix with the caller-supplied list. The
167
+ * default (`contentContainer`) is always present; user entries are
168
+ * additive. Returned as a Set so the hot-path visitor classifies one
169
+ * attribute in O(1).
170
+ * @param userPrefixes Extra prefixes the caller wants active.
171
+ * @returns Sorted effective prefix set.
172
+ */
173
+ function buildPrefixSet(userPrefixes) {
174
+ const out = new Set(DEFAULT_CLASSNAME_PREFIXES);
175
+ if (userPrefixes)
176
+ for (const prefix of userPrefixes)
177
+ out.add(prefix);
178
+ return out;
179
+ }
180
+ /**
181
+ * Decide whether a JSX attribute name is one the transformer should
182
+ * rewrite, and derive the replacement prop name when it is.
183
+ *
184
+ * `className` is the classic path. `<prefix>ClassName` where `prefix`
185
+ * is in the active set becomes `<prefix>Style`. Everything else returns
186
+ * `null` and the visitor moves on.
187
+ * @param name JSXAttribute's identifier text.
188
+ * @param prefixes Effective prefix set for this transform.
189
+ * @returns Rewrite target record, or `null` when the attribute is not ours.
190
+ */
191
+ function classifyAttributeName(name, prefixes) {
192
+ if (name === 'className')
193
+ return { kind: 'className', styleProp: 'style' };
194
+ if (!name.endsWith('ClassName'))
195
+ return null;
196
+ const prefix = name.slice(0, -'ClassName'.length);
197
+ if (prefix.length === 0 || !prefixes.has(prefix))
198
+ return null;
199
+ return { kind: 'prefix', styleProp: `${prefix}Style` };
200
+ }
201
+ /**
202
+ * Rewrite one `className` JSXAttribute node.
203
+ *
204
+ * Two paths:
205
+ * - **Non-interactive** (literal with no `active:` / `focus:` tokens):
206
+ * emit `style={lookupCss(<ref|expr>, _s [, userStyle])}` inline on
207
+ * the existing tag. The JSX site keeps its original component.
208
+ * - **Interactive** (literal with an interactive token OR any dynamic
209
+ * expression): replace the JSXElement's tag with `<InteractiveBox>`,
210
+ * move the original tag into a `_rw.as` spec prop, and forward all
211
+ * other attributes untouched. Each InteractiveBox instance calls
212
+ * `useInteract()` internally so sibling elements don't share state.
213
+ *
214
+ * If the element has a sibling `style={…}` attribute it's removed and
215
+ * its expression threads through as the user-style merge source.
216
+ * @param attributePath The JSXAttribute path.
217
+ * @param hoister Per-file hoist table.
218
+ * @param literals Output array — each static literal gets pushed so the
219
+ * caller can feed them into the parser / atom ledger.
220
+ * @param rewriteCtx
221
+ * @param target
222
+ * @returns Outcome flags, or `null` when the attribute was unrewritable.
223
+ */
224
+ function rewriteClassNameAttribute(attributePath, hoister, literals, rewriteCtx, target) {
225
+ const { node } = attributePath;
226
+ const { value } = node;
227
+ if (!value)
228
+ return null;
229
+ const buildResult = buildFirstArgument(value, hoister, literals, rewriteCtx);
230
+ if (!buildResult)
231
+ return null;
232
+ const { parent } = attributePath;
233
+ if (!t.isJSXOpeningElement(parent))
234
+ return null;
235
+ const userStyleExpr = extractAndDropSiblingStyle(parent, target.styleProp);
236
+ // Single context binding `_t = _r()` — carries scheme, fontScale,
237
+ // insets together so React tracks all three as render deps via one
238
+ // useContext read.
239
+ const ctxBinding = injectContextHook(attributePath);
240
+ applyDerivedJsxAttributes(attributePath, parent, buildResult, target, rewriteCtx);
241
+ // Prefixed rewrites (`<prefix>ClassName`) target a passive sub-surface
242
+ // that can't receive press / focus — skip the InteractiveBox wrapper
243
+ // even for dynamic expressions. Only the plain `className` path is
244
+ // eligible for InteractiveBox routing.
245
+ if (target.kind === 'className' && buildResult.mayBeInteractive && isTagInteractive(parent.name)) {
246
+ rewriteAsInteractiveBox(attributePath, parent, buildResult.expression, ctxBinding, userStyleExpr);
247
+ return { injectedInteract: true };
248
+ }
249
+ const args = [buildResult.expression, t.identifier(ctxBinding)];
250
+ // 3rd arg = userStyle (sibling style={…}). 4th arg = interactState
251
+ // (always undefined in the non-interactive branch).
252
+ if (userStyleExpr)
253
+ args.push(userStyleExpr);
254
+ const call = t.callExpression(t.identifier(LOOKUP_CSS), args);
255
+ attributePath.replaceWith(t.jsxAttribute(t.jsxIdentifier(target.styleProp), t.jsxExpressionContainer(call)));
256
+ return { injectedInteract: false };
257
+ }
258
+ /**
259
+ * Apply every JSX attribute + side-effect derived from a parsed
260
+ * className literal: gradient props, truncate props, mount-haptic
261
+ * aggregation, and event-haptic handler chaining. Collected in one
262
+ * helper so {@link rewriteClassNameAttribute} stays under the
263
+ * complexity cap.
264
+ * @param attributePath Path of the className attribute being rewritten.
265
+ * @param parent Opening element to mutate.
266
+ * @param result Per-literal derived state.
267
+ * @param target Rewrite target (only `className`-kind gets derived attrs).
268
+ * @param rewriteCtx Rewrite-wide state.
269
+ */
270
+ function applyDerivedJsxAttributes(attributePath, parent, result, target, rewriteCtx) {
271
+ if (target.kind !== 'className')
272
+ return;
273
+ if (result.gradientAttrs)
274
+ appendGradientAttributes(parent, result.gradientAttrs);
275
+ if (result.truncateAttrs)
276
+ appendGradientAttributes(parent, result.truncateAttrs);
277
+ if (result.mountHaptics)
278
+ recordMountHaptics(attributePath, result.mountHaptics, rewriteCtx);
279
+ if (result.eventHaptics)
280
+ injectEventHapticHandlers(attributePath, parent, result.eventHaptics, rewriteCtx);
281
+ }
282
+ /**
283
+ * Splice gradient JSX attributes (`colors={…}` / `start={…}` /
284
+ * `end={…}`) into a JSXOpeningElement's attribute list, replacing
285
+ * any already-present attribute with the same name. Users who manually
286
+ * set `colors=` on the same element lose; rnwind's class-derived
287
+ * values win — matching how `className`-resolved styles override
288
+ * inline `style={…}`.
289
+ * @param opening JSXOpeningElement to mutate.
290
+ * @param gradientAttrs Freshly built JSX attributes.
291
+ * @param gradientAttributes
292
+ */
293
+ function appendGradientAttributes(opening, gradientAttributes) {
294
+ const names = new Set();
295
+ for (const attribute of gradientAttributes)
296
+ if (t.isJSXIdentifier(attribute.name))
297
+ names.add(attribute.name.name);
298
+ opening.attributes = opening.attributes.filter((attribute) => {
299
+ if (!t.isJSXAttribute(attribute))
300
+ return true;
301
+ if (!t.isJSXIdentifier(attribute.name))
302
+ return true;
303
+ return !names.has(attribute.name.name);
304
+ });
305
+ opening.attributes.push(...gradientAttributes);
306
+ }
307
+ /**
308
+ * Whether a JSX tag can fire press / focus events. Pure host-tag check
309
+ * against {@link NON_INTERACTIVE_HOST_TAGS}: anything in the set is
310
+ * definitely non-interactive; anything else (custom component,
311
+ * `Animated.View`, etc.) is treated as potentially interactive so the
312
+ * InteractiveBox wrapper is still applied.
313
+ * @param name JSXOpeningElement name node.
314
+ * @returns `true` when the tag might emit press / focus events.
315
+ */
316
+ function isTagInteractive(name) {
317
+ if (t.isJSXIdentifier(name))
318
+ return !NON_INTERACTIVE_HOST_TAGS.has(name.name);
319
+ // Member expressions (`Animated.View`, `Foo.Bar`): conservatively
320
+ // treat as interactive since the outer object's semantics are opaque.
321
+ return true;
322
+ }
323
+ /**
324
+ * Replace the JSXElement's tag with `<InteractiveBox>`, packing the
325
+ * original tag, the className ref / expression, the scheme binding, and
326
+ * any user style into a single `_rw` spec prop. All other attributes
327
+ * forward through unchanged.
328
+ *
329
+ * The replacement keeps the element's children — only the opening /
330
+ * closing tag name changes, plus the className attribute is replaced by
331
+ * `_rw` (and a preceding `style` attribute was already spliced out).
332
+ * @param attributePath JSXAttribute path for the className being rewritten.
333
+ * @param opening JSXOpeningElement the attribute lives on.
334
+ * @param classNameExpr The first-arg expression (hoisted ref or dynamic).
335
+ * @param schemeBinding Name of the `_s = useScheme()` binding.
336
+ * @param ctxBinding
337
+ * @param userStyleExpr Optional user style spliced from a sibling `style={…}`.
338
+ * @param insetsBinding `_i = useInsets()` binding name when the rewrite needs insets, else null.
339
+ * @param fontScaleBinding `_fs = useFontScale()` binding name — always present since every rewrite injects it.
340
+ */
341
+ function rewriteAsInteractiveBox(attributePath, opening, classNameExpr, ctxBinding, userStyleExpr) {
342
+ const originalTagExpr = jsxNameToExpression(opening.name);
343
+ const rwProperties = [
344
+ t.objectProperty(t.identifier('as'), originalTagExpr),
345
+ t.objectProperty(t.identifier('cn'), classNameExpr),
346
+ t.objectProperty(t.identifier('t'), t.identifier(ctxBinding)),
347
+ ];
348
+ if (userStyleExpr)
349
+ rwProperties.push(t.objectProperty(t.identifier('us'), userStyleExpr));
350
+ const rwAttribute = t.jsxAttribute(t.jsxIdentifier('_rw'), t.jsxExpressionContainer(t.objectExpression(rwProperties)));
351
+ // Swap the className attribute out for `_rw`, keeping it at the
352
+ // attribute's original position so any surrounding spread attrs stay
353
+ // honouring the user's intended order.
354
+ attributePath.replaceWith(rwAttribute);
355
+ opening.name = t.jsxIdentifier(INTERACTIVE_BOX);
356
+ const jsxElement = findParentJsxElement(attributePath);
357
+ if (jsxElement?.closingElement)
358
+ jsxElement.closingElement.name = t.jsxIdentifier(INTERACTIVE_BOX);
359
+ }
360
+ /**
361
+ * Walk from a JSXAttribute path up to its JSXElement ancestor.
362
+ * @param attributePath JSXAttribute path.
363
+ * @returns The enclosing JSXElement, or `null` when the shape is unexpected.
364
+ */
365
+ function findParentJsxElement(attributePath) {
366
+ const openingPath = attributePath.parentPath;
367
+ if (!openingPath)
368
+ return null;
369
+ const elementPath = openingPath.parentPath;
370
+ if (!elementPath)
371
+ return null;
372
+ const { node } = elementPath;
373
+ return t.isJSXElement(node) ? node : null;
374
+ }
375
+ /**
376
+ * Convert a JSX opening-element name (identifier or member expression)
377
+ * into a regular JS expression we can splice into the `_rw.as` object
378
+ * property. `<Animated.View>` → `Animated.View`, `<Pressable>` →
379
+ * `Pressable`.
380
+ * @param name JSXOpeningElement name node.
381
+ * @returns Equivalent identifier / member-expression node.
382
+ */
383
+ function jsxNameToExpression(name) {
384
+ if (t.isJSXIdentifier(name))
385
+ return t.identifier(name.name);
386
+ if (t.isJSXMemberExpression(name)) {
387
+ return t.memberExpression(jsxNameToExpression(name.object), t.identifier(name.property.name));
388
+ }
389
+ throw new Error(`rnwind: unsupported JSX tag shape "${name.type ?? 'unknown'}" for interactive className`);
390
+ }
391
+ /**
392
+ * Decide what the first arg of the rewritten `lookupCss(...)` call
393
+ * should be:
394
+ * - Static string literal (`"…"` or `{"…"}` or static template): tokenize,
395
+ * push literal text for the ledger, return a hoisted const reference.
396
+ * - Dynamic expression: forward the expression unchanged; runtime
397
+ * tokenizes the string result at render time.
398
+ * @param value Attribute's value node (StringLiteral or JSXExpressionContainer).
399
+ * @param hoister Hoist table.
400
+ * @param literals Output array for static literals.
401
+ * @param rewriteCtx
402
+ * @returns The first-arg expression + interact-eligibility flag, or `null`.
403
+ */
404
+ function buildFirstArgument(value, hoister, literals, rewriteCtx) {
405
+ if (t.isStringLiteral(value))
406
+ return literalResult(value.value, hoister, literals, rewriteCtx);
407
+ if (!t.isJSXExpressionContainer(value))
408
+ return null;
409
+ const { expression } = value;
410
+ if (t.isJSXEmptyExpression(expression))
411
+ return null;
412
+ if (t.isStringLiteral(expression))
413
+ return literalResult(expression.value, hoister, literals, rewriteCtx);
414
+ if (t.isTemplateLiteral(expression) && expression.expressions.length === 0 && expression.quasis[0]) {
415
+ const text = expression.quasis[0].value.cooked ?? expression.quasis[0].value.raw;
416
+ return literalResult(text, hoister, literals, rewriteCtx);
417
+ }
418
+ // Dynamic expression — can't inspect atoms at build time. Assume safe
419
+ // is possible and pull in `_i` so the runtime can resolve any
420
+ // `*-safe` class a consumer composes at runtime. The runtime fast
421
+ // path is still taken when the dynamic string resolves to a plain
422
+ // non-safe atom list (SAFE_ATOMS index check gates the slow path).
423
+ rewriteCtx.needsInsets = true;
424
+ return { expression: expression, mayBeInteractive: true, needsInsets: true };
425
+ }
426
+ /**
427
+ * Package a literal classname into a hoisted atom-array ref and scan it
428
+ * for interactive prefixes (`active:`, `focus:`) + safe-area patterns.
429
+ * Pre-scanned literals without any interactive tokens skip the (small
430
+ * but measurable) cost of injecting a `useInteract()` hook; literals
431
+ * without any `*-safe` token skip the insets arg, keeping the runtime
432
+ * fast path engaged.
433
+ * @param text Raw classname string.
434
+ * @param hoister Hoist table.
435
+ * @param literals Output array for literal-text sink.
436
+ * @param rewriteCtx Rewrite-wide state; updated when any atom needs insets.
437
+ * @returns The expression + per-rewrite flags.
438
+ */
439
+ function literalResult(text, hoister, literals, rewriteCtx) {
440
+ literals.push(text);
441
+ const atoms = tokenize(text);
442
+ const { gradientAttrs, remaining: afterGradient } = extractGradientSpec(atoms, rewriteCtx);
443
+ const { truncateAttrs, remaining: afterTruncate } = extractTextTruncateSpec(afterGradient);
444
+ const { mountHaptics, eventHaptics, remaining } = extractHapticSpec(afterTruncate, rewriteCtx);
445
+ const mayBeInteractive = remaining.some((atom) => INTERACTIVE_PREFIXES.some((prefix) => atom.startsWith(prefix)));
446
+ const needsInsets = remaining.some((atom) => SAFE_ATOM_PATTERN.test(atom));
447
+ if (needsInsets)
448
+ rewriteCtx.needsInsets = true;
449
+ return {
450
+ expression: hoister.refFor(remaining),
451
+ mayBeInteractive,
452
+ needsInsets,
453
+ gradientAttrs: gradientAttrs.length > 0 ? gradientAttrs : undefined,
454
+ truncateAttrs: truncateAttrs.length > 0 ? truncateAttrs : undefined,
455
+ mountHaptics: mountHaptics.length > 0 ? mountHaptics : undefined,
456
+ eventHaptics: eventHaptics.length > 0 ? eventHaptics : undefined,
457
+ };
458
+ }
459
+ /**
460
+ * Scan the atom list for gradient roles (direction + from/via/to
461
+ * colours), strip those atoms out, and produce the JSX attributes the
462
+ * rewrite will splice onto the opening element:
463
+ * colors={_g_<hash>} start={_gs_<hash>} end={_ge_<hash>}
464
+ * When the atom list doesn't contain a complete gradient (no direction
465
+ * OR no colour stops), the gradient atoms pass through untouched —
466
+ * they'd resolve to `{}` in the runtime anyway. This keeps the
467
+ * transform conservative.
468
+ * @param atoms Tokenised atom list from the literal.
469
+ * @param rewriteCtx Rewrite-wide state (for the hoister).
470
+ * @returns The gradient JSX attrs (possibly empty) and the non-gradient remainder.
471
+ */
472
+ function extractGradientSpec(atoms, rewriteCtx) {
473
+ const { gradientAtoms } = rewriteCtx;
474
+ if (gradientAtoms.size === 0)
475
+ return { gradientAttrs: [], remaining: atoms };
476
+ let direction = null;
477
+ let from = null;
478
+ let via = null;
479
+ let to = null;
480
+ const remaining = [];
481
+ for (const atom of atoms) {
482
+ const info = gradientAtoms.get(atom);
483
+ if (!info) {
484
+ remaining.push(atom);
485
+ continue;
486
+ }
487
+ switch (info.role) {
488
+ case 'direction': {
489
+ direction = info.dir;
490
+ break;
491
+ }
492
+ case 'from': {
493
+ from = info.color;
494
+ break;
495
+ }
496
+ case 'via': {
497
+ via = info.color;
498
+ break;
499
+ }
500
+ case 'to': {
501
+ {
502
+ to = info.color;
503
+ // No default
504
+ }
505
+ break;
506
+ }
507
+ }
508
+ // Gradient atoms deliberately drop from `remaining` — they're
509
+ // consumed at build time and don't need a runtime style slot.
510
+ }
511
+ if (direction === null || direction === 'unknown' || (from === null && to === null)) {
512
+ // No recognisable gradient — put atoms back so they at least
513
+ // attempt to resolve through lookupCss.
514
+ return { gradientAttrs: [], remaining: atoms };
515
+ }
516
+ const colors = gradientColors(from, via, to);
517
+ const points = directionToPoints(direction);
518
+ const colorsRef = rewriteCtx.gradientHoister.refForColors(colors);
519
+ const startRef = rewriteCtx.gradientHoister.refForPoint(points.start, 'start');
520
+ const endRef = rewriteCtx.gradientHoister.refForPoint(points.end, 'end');
521
+ const attributes = [
522
+ t.jsxAttribute(t.jsxIdentifier('colors'), t.jsxExpressionContainer(colorsRef)),
523
+ t.jsxAttribute(t.jsxIdentifier('start'), t.jsxExpressionContainer(startRef)),
524
+ t.jsxAttribute(t.jsxIdentifier('end'), t.jsxExpressionContainer(endRef)),
525
+ ];
526
+ return { gradientAttrs: attributes, remaining };
527
+ }
528
+ /**
529
+ * Scan the atom list for text-truncate utilities (`truncate`,
530
+ * `line-clamp-<N>`, `line-clamp-none`, `text-ellipsis`, `text-clip`),
531
+ * strip them out, and produce the JSX attributes the rewrite will
532
+ * splice onto the opening element: `numberOfLines={N}` and/or
533
+ * `ellipsizeMode="tail"|"clip"`.
534
+ *
535
+ * Merge rule mirrors Tailwind's cascade — later atoms override earlier
536
+ * ones. `numberOfLines: 0` (the `line-clamp-none` reset) suppresses
537
+ * emission entirely; a standalone `text-ellipsis` / `text-clip` with no
538
+ * companion line count also emits nothing because `ellipsizeMode`
539
+ * alone has no effect on RN `<Text>`.
540
+ * @param atoms Tokenised atom list.
541
+ * @returns The truncate JSX attrs (possibly empty) and the non-truncate remainder.
542
+ */
543
+ function extractTextTruncateSpec(atoms) {
544
+ if (!mayContainTextTruncate(atoms))
545
+ return { truncateAttrs: [], remaining: atoms };
546
+ let numberOfLines;
547
+ let ellipsizeMode;
548
+ const remaining = [];
549
+ for (const atom of atoms) {
550
+ const info = detectTextTruncate(atom);
551
+ if (!info) {
552
+ remaining.push(atom);
553
+ continue;
554
+ }
555
+ const { numberOfLines: infoLines, ellipsizeMode: infoMode } = info;
556
+ if (infoLines !== undefined)
557
+ numberOfLines = infoLines;
558
+ if (infoMode !== undefined)
559
+ ellipsizeMode = infoMode;
560
+ }
561
+ const attributes = buildTruncateAttributes(numberOfLines, ellipsizeMode);
562
+ return { truncateAttrs: attributes, remaining };
563
+ }
564
+ /**
565
+ * Assemble JSXAttribute nodes for the resolved truncate props. Drops
566
+ * `numberOfLines` when zero (reset) and drops `ellipsizeMode` when not
567
+ * paired with a positive line count — matching RN's behaviour where
568
+ * `ellipsizeMode` needs `numberOfLines` to do anything.
569
+ * @param numberOfLines Resolved clamp count, or undefined.
570
+ * @param ellipsizeMode Resolved ellipsize mode, or undefined.
571
+ * @returns Zero, one, or two JSX attributes.
572
+ */
573
+ function buildTruncateAttributes(numberOfLines, ellipsizeMode) {
574
+ const attributes = [];
575
+ if (numberOfLines !== undefined && numberOfLines > 0) {
576
+ attributes.push(t.jsxAttribute(t.jsxIdentifier('numberOfLines'), t.jsxExpressionContainer(t.numericLiteral(numberOfLines))));
577
+ if (ellipsizeMode !== undefined) {
578
+ attributes.push(t.jsxAttribute(t.jsxIdentifier('ellipsizeMode'), t.stringLiteral(ellipsizeMode)));
579
+ }
580
+ }
581
+ return attributes;
582
+ }
583
+ /**
584
+ * Map of variant-prefix → trigger. Bare atoms (no colon) resolve to
585
+ * `'mount'` through {@link extractHapticSpec}; these entries cover the
586
+ * explicit `active:` / `focus:` / `hover:` cases.
587
+ */
588
+ const HAPTIC_VARIANT_TRIGGER = {
589
+ active: 'pressIn',
590
+ focus: 'focus',
591
+ hover: 'hover',
592
+ };
593
+ /** Map a non-mount haptic trigger to the JSX event prop it chains onto. */
594
+ const HAPTIC_EVENT_PROP = {
595
+ pressIn: 'onPressIn',
596
+ pressOut: 'onPressOut',
597
+ focus: 'onFocus',
598
+ hover: 'onMouseEnter',
599
+ };
600
+ /**
601
+ * Scan atom list for haptic utilities. Bare `haptic-*` → mount trigger;
602
+ * `active:haptic-*` / `focus:haptic-*` / `hover:haptic-*` → the matching
603
+ * event trigger. Matched atoms are stripped from the remainder so the
604
+ * runtime style resolver never tries to look up `--rnwind-haptic`.
605
+ * @param atoms Post-gradient, post-truncate atom list.
606
+ * @param rewriteCtx Rewrite-wide state (for the haptic-atom map).
607
+ * @returns Mount + event haptic entries, plus the non-haptic remainder.
608
+ */
609
+ function extractHapticSpec(atoms, rewriteCtx) {
610
+ const { hapticAtoms } = rewriteCtx;
611
+ if (hapticAtoms.size === 0)
612
+ return { mountHaptics: [], eventHaptics: [], remaining: atoms };
613
+ const mountHaptics = [];
614
+ const eventHaptics = [];
615
+ const remaining = [];
616
+ for (const atom of atoms) {
617
+ const resolved = resolveHapticAtom(atom, hapticAtoms);
618
+ if (!resolved) {
619
+ remaining.push(atom);
620
+ continue;
621
+ }
622
+ if (resolved.trigger === 'mount')
623
+ mountHaptics.push(resolved.request);
624
+ else
625
+ eventHaptics.push({ request: resolved.request, trigger: resolved.trigger });
626
+ }
627
+ return { mountHaptics, eventHaptics, remaining };
628
+ }
629
+ /**
630
+ * Classify one atom against the parser's haptic map. A colon-free atom
631
+ * maps to `'mount'`; `active:` / `focus:` / `hover:` prefixes map to
632
+ * the matching event trigger. Other prefixes return `null` so they
633
+ * fall through to the regular style path.
634
+ * @param atom Atom name, possibly variant-prefixed.
635
+ * @param hapticAtoms Parser-surfaced haptic metadata.
636
+ * @returns `{request, trigger}` on match, null otherwise.
637
+ */
638
+ function resolveHapticAtom(atom, hapticAtoms) {
639
+ // Direct lookup first — Tailwind v4 registers the variant-prefixed
640
+ // class (e.g. `active:haptic-medium`) as its own rule, and the
641
+ // parser's nested-rule walk surfaces the marker under that key.
642
+ const direct = hapticAtoms.get(atom);
643
+ if (direct) {
644
+ const colon = atom.indexOf(':');
645
+ if (colon === -1)
646
+ return { request: direct, trigger: 'mount' };
647
+ const trigger = HAPTIC_VARIANT_TRIGGER[atom.slice(0, colon)];
648
+ if (trigger)
649
+ return { request: direct, trigger };
650
+ return null;
651
+ }
652
+ // Fallback — try stripping a known variant prefix and looking up
653
+ // the bare class. Handles cases where the parser only registered
654
+ // the base utility (the variant rule may be missing if only the
655
+ // bare class is otherwise used in the theme).
656
+ const colon = atom.indexOf(':');
657
+ if (colon === -1)
658
+ return null;
659
+ const prefix = atom.slice(0, colon);
660
+ const trigger = HAPTIC_VARIANT_TRIGGER[prefix];
661
+ if (!trigger)
662
+ return null;
663
+ const bare = hapticAtoms.get(atom.slice(colon + 1));
664
+ if (!bare)
665
+ return null;
666
+ return { request: bare, trigger };
667
+ }
668
+ /**
669
+ * Append mount-haptic requests to the aggregate keyed by the JSX site's
670
+ * enclosing component body. Post-traversal the transformer injects one
671
+ * `useMountHaptic(<hoisted>)` call per component.
672
+ * @param attributePath The JSXAttribute path the haptic came from.
673
+ * @param requests Mount requests gathered from this literal.
674
+ * @param rewriteCtx Rewrite-wide state.
675
+ */
676
+ function recordMountHaptics(attributePath, requests, rewriteCtx) {
677
+ const body = findComponentBody(attributePath);
678
+ if (!body)
679
+ return;
680
+ const bucket = rewriteCtx.mountByComponent.get(body.node);
681
+ if (bucket) {
682
+ bucket.push(...requests);
683
+ return;
684
+ }
685
+ rewriteCtx.mountByComponent.set(body.node, [...requests]);
686
+ }
687
+ /**
688
+ * Splice one chained event handler per event-haptic entry onto the
689
+ * JSXOpeningElement. Each handler calls `triggerHaptic(_h, <request>,
690
+ * '<trigger>')` and then forwards to any pre-existing user handler.
691
+ * @param attributePath Path of the className attribute being rewritten.
692
+ * @param opening Opening element to mutate.
693
+ * @param entries Event-haptic entries.
694
+ * @param rewriteCtx Rewrite-wide state (for the hoister).
695
+ */
696
+ function injectEventHapticHandlers(attributePath, opening, entries, rewriteCtx) {
697
+ // Make sure `_t = _r()` is in scope — haptic dispatcher reads `_t.onHaptics`.
698
+ injectContextHook(attributePath);
699
+ rewriteCtx.needsHapticsHook = true;
700
+ const byTrigger = new Map();
701
+ for (const { request, trigger } of entries) {
702
+ const list = byTrigger.get(trigger);
703
+ if (list)
704
+ list.push(request);
705
+ else
706
+ byTrigger.set(trigger, [request]);
707
+ }
708
+ for (const [trigger, requests] of byTrigger) {
709
+ const eventProperty = HAPTIC_EVENT_PROP[trigger];
710
+ const existing = extractAndDropSiblingStyle(opening, eventProperty);
711
+ const handler = buildChainedHapticHandler(rewriteCtx, requests, trigger, existing);
712
+ opening.attributes.push(t.jsxAttribute(t.jsxIdentifier(eventProperty), t.jsxExpressionContainer(handler)));
713
+ }
714
+ }
715
+ /**
716
+ * Build the inline arrow body for one chained handler — dispatch every
717
+ * request in `requests` via `triggerHaptic`, then forward the event to
718
+ * the user-supplied handler (if any) via `existing?.(event)`.
719
+ * @param rewriteCtx Rewrite-wide state.
720
+ * @param requests Requests that share this trigger.
721
+ * @param trigger Lifecycle trigger this handler fires on.
722
+ * @param existing User-supplied event handler expression, or null.
723
+ * @returns ArrowFunctionExpression ready to splice into a JSXAttribute.
724
+ */
725
+ function buildChainedHapticHandler(rewriteCtx, requests, trigger, existing) {
726
+ const eventId = t.identifier('_e');
727
+ const body = [];
728
+ for (const request of requests) {
729
+ const ref = rewriteCtx.hapticHoister.refForRequest(request);
730
+ body.push(t.expressionStatement(t.callExpression(t.identifier(TRIGGER_HAPTIC), [
731
+ t.memberExpression(t.identifier(CONTEXT_BINDING), t.identifier('onHaptics')),
732
+ ref,
733
+ t.stringLiteral(trigger),
734
+ ])));
735
+ }
736
+ if (existing) {
737
+ body.push(t.expressionStatement(t.optionalCallExpression(existing, [eventId], true)));
738
+ }
739
+ return t.arrowFunctionExpression([eventId], t.blockStatement(body));
740
+ }
741
+ /**
742
+ * Walk the aggregated `mountByComponent` map and inject a single
743
+ * `useMountHaptic(<hoisted requests>)` call per component body.
744
+ * The hoist yields a stable `const _hm_<hash>` referencing a frozen
745
+ * array of request objects.
746
+ * @param rewriteCtx Rewrite-wide state.
747
+ */
748
+ function injectMountHapticCalls(rewriteCtx) {
749
+ for (const [body, requests] of rewriteCtx.mountByComponent) {
750
+ const ref = rewriteCtx.hapticHoister.refForRequestList(requests);
751
+ const declaration = t.expressionStatement(t.callExpression(t.identifier(USE_MOUNT_HAPTIC), [ref]));
752
+ body.body.unshift(declaration);
753
+ }
754
+ }
755
+ /**
756
+ * Derive a stable cache key for one {@link HapticRequest}. Keys are
757
+ * used both for hoister interning and mount-list digesting.
758
+ * @param request Haptic request.
759
+ * @returns Canonical key text.
760
+ */
761
+ function keyForHapticRequest(request) {
762
+ if (request.kind === 'impact')
763
+ return `impact:${request.style}`;
764
+ if (request.kind === 'notification')
765
+ return `notification:${request.type}`;
766
+ return 'selection';
767
+ }
768
+ /**
769
+ * Build the haptic hoist table. Single requests and mount-request
770
+ * lists each get their own module-scope frozen const so component
771
+ * bodies only reference stable identifiers — no per-render allocation.
772
+ * @returns HapticHoister API.
773
+ */
774
+ function createHapticHoister() {
775
+ const pool = createInternPool();
776
+ const refForRequest = (request) => pool.intern('_hr', `req:${keyForHapticRequest(request)}`, { kind: 'request', request });
777
+ const refForRequestList = (requests) => pool.intern('_hm', `list:${requests.map((request) => keyForHapticRequest(request)).join('|')}`, {
778
+ kind: 'list',
779
+ requests,
780
+ });
781
+ return { refForRequest, refForRequestList, entries: pool.entries };
782
+ }
783
+ /**
784
+ * Generic intern-pool: one shared cache for module-scope consts keyed
785
+ * by an arbitrary string. Returns a stable `t.Identifier` per key and
786
+ * records `{name → entry}` for the emitter pass.
787
+ * @returns Intern API.
788
+ */
789
+ function createInternPool() {
790
+ const byKey = new Map();
791
+ const entries = new Map();
792
+ const intern = (prefix, key, entry) => {
793
+ const existing = byKey.get(key);
794
+ if (existing)
795
+ return existing;
796
+ const hash = createHash('sha256').update(key).digest('hex').slice(0, 12);
797
+ const name = `${prefix}_${hash}`;
798
+ const ident = t.identifier(name);
799
+ byKey.set(key, ident);
800
+ entries.set(name, entry);
801
+ return ident;
802
+ };
803
+ return { intern, entries };
804
+ }
805
+ /**
806
+ * Emit `const _hr_<hash> = Object.freeze({...})` and `const _hm_<hash>
807
+ * = Object.freeze([{...}, ...])` statements at module scope — one per
808
+ * hoister entry.
809
+ * @param ast Babel File AST.
810
+ * @param entries Hoister entries.
811
+ */
812
+ function injectHapticConsts(ast, entries) {
813
+ const declarations = [];
814
+ for (const [name, entry] of entries) {
815
+ if (entry.kind === 'request') {
816
+ declarations.push(t.variableDeclaration('const', [
817
+ t.variableDeclarator(t.identifier(name), freezeExpression(requestLiteral(entry.request))),
818
+ ]));
819
+ }
820
+ else {
821
+ declarations.push(t.variableDeclaration('const', [
822
+ t.variableDeclarator(t.identifier(name), freezeExpression(t.arrayExpression(entry.requests.map((request) => freezeExpression(requestLiteral(request)))))),
823
+ ]));
824
+ }
825
+ }
826
+ ast.program.body.unshift(...declarations);
827
+ }
828
+ /**
829
+ * Build an `Object.freeze(...)` call around the given expression.
830
+ * @param value Expression to freeze.
831
+ * @returns CallExpression node.
832
+ */
833
+ function freezeExpression(value) {
834
+ return t.callExpression(t.memberExpression(t.identifier('Object'), t.identifier('freeze')), [value]);
835
+ }
836
+ /**
837
+ * Build an object-literal representation of a {@link HapticRequest} —
838
+ * `{kind: 'impact', style: 'Light'}` etc.
839
+ * @param request Haptic request.
840
+ * @returns ObjectExpression node.
841
+ */
842
+ function requestLiteral(request) {
843
+ const properties = [
844
+ t.objectProperty(t.identifier('kind'), t.stringLiteral(request.kind)),
845
+ ];
846
+ if (request.kind === 'impact')
847
+ properties.push(t.objectProperty(t.identifier('style'), t.stringLiteral(request.style)));
848
+ else if (request.kind === 'notification')
849
+ properties.push(t.objectProperty(t.identifier('type'), t.stringLiteral(request.type)));
850
+ return t.objectExpression(properties);
851
+ }
852
+ /**
853
+ * Normalise the `from/via/to` triple into the array
854
+ * `<LinearGradient colors={…}>` expects: drop `null` entries while
855
+ * keeping the source order.
856
+ * @param from Hex colour for `from-*`, or null.
857
+ * @param via Hex colour for `via-*`, or null.
858
+ * @param to Hex colour for `to-*`, or null.
859
+ * @returns Colour array (at least one entry guaranteed by the caller).
860
+ */
861
+ function gradientColors(from, via, to) {
862
+ const out = [];
863
+ if (from !== null)
864
+ out.push(from);
865
+ if (via !== null)
866
+ out.push(via);
867
+ if (to !== null)
868
+ out.push(to);
869
+ return out;
870
+ }
871
+ /**
872
+ * Map Tailwind's stock direction tag to the `(start, end)` pair of
873
+ * unit-square points expo-linear-gradient expects. Pure constants —
874
+ * the same as NativeWind and the wider RN-gradient community.
875
+ * @param dir Compact direction tag from the parser.
876
+ * @returns Start + end point records.
877
+ */
878
+ function directionToPoints(dir) {
879
+ switch (dir) {
880
+ case 'to-r': {
881
+ return { start: { x: 0, y: 0.5 }, end: { x: 1, y: 0.5 } };
882
+ }
883
+ case 'to-l': {
884
+ return { start: { x: 1, y: 0.5 }, end: { x: 0, y: 0.5 } };
885
+ }
886
+ case 'to-t': {
887
+ return { start: { x: 0.5, y: 1 }, end: { x: 0.5, y: 0 } };
888
+ }
889
+ case 'to-b': {
890
+ return { start: { x: 0.5, y: 0 }, end: { x: 0.5, y: 1 } };
891
+ }
892
+ case 'to-tr': {
893
+ return { start: { x: 0, y: 1 }, end: { x: 1, y: 0 } };
894
+ }
895
+ case 'to-tl': {
896
+ return { start: { x: 1, y: 1 }, end: { x: 0, y: 0 } };
897
+ }
898
+ case 'to-br': {
899
+ return { start: { x: 0, y: 0 }, end: { x: 1, y: 1 } };
900
+ }
901
+ case 'to-bl': {
902
+ return { start: { x: 1, y: 0 }, end: { x: 0, y: 1 } };
903
+ }
904
+ default: {
905
+ return { start: { x: 0, y: 0.5 }, end: { x: 1, y: 0.5 } };
906
+ }
907
+ }
908
+ }
909
+ /**
910
+ * Look for a sibling style attribute on the same JSXOpeningElement, drop
911
+ * it, and return its expression for the caller to pass as the `lookupCss`
912
+ * third arg. The attribute name is parameterised so the prefix path can
913
+ * pull `contentContainerStyle` (et al.) instead of plain `style`.
914
+ * @param parent JSXOpeningElement containing the className we're rewriting.
915
+ * @param styleProp The exact sibling attribute name to look for.
916
+ * @param styleProperty
917
+ * @returns Expression from the dropped attribute, or `null`.
918
+ */
919
+ function extractAndDropSiblingStyle(parent, styleProperty) {
920
+ const { attributes } = parent;
921
+ for (let index = 0; index < attributes.length; index += 1) {
922
+ const attribute = attributes[index];
923
+ if (!t.isJSXAttribute(attribute))
924
+ continue;
925
+ if (!t.isJSXIdentifier(attribute.name) || attribute.name.name !== styleProperty)
926
+ continue;
927
+ const { value } = attribute;
928
+ if (!value || !t.isJSXExpressionContainer(value))
929
+ return null;
930
+ const { expression } = value;
931
+ if (t.isJSXEmptyExpression(expression))
932
+ return null;
933
+ attributes.splice(index, 1);
934
+ return expression;
935
+ }
936
+ return null;
937
+ }
938
+ const INJECTED = new WeakSet();
939
+ /**
940
+ * Walk up from the rewrite site to the nearest enclosing function
941
+ * component and inject `const _t = _r()` at the top of its body. This
942
+ * is the SINGLE rnwind context binding — `_t` carries scheme,
943
+ * fontScale, insets, etc. Idempotent per component.
944
+ * @param path Path of any node inside the component's JSX.
945
+ * @returns The binding name (`_t`).
946
+ */
947
+ function injectContextHook(path) {
948
+ const componentBody = findComponentBody(path);
949
+ if (!componentBody)
950
+ return CONTEXT_BINDING;
951
+ if (INJECTED.has(componentBody.node))
952
+ return CONTEXT_BINDING;
953
+ INJECTED.add(componentBody.node);
954
+ const declaration = t.variableDeclaration('const', [
955
+ t.variableDeclarator(t.identifier(CONTEXT_BINDING), t.callExpression(t.identifier(USE_RNWIND_INTERNAL), [])),
956
+ ]);
957
+ componentBody.unshiftContainer('body', declaration);
958
+ return CONTEXT_BINDING;
959
+ }
960
+ /**
961
+ * Walk up from `path` to the nearest recognised function component.
962
+ * Accepts:
963
+ * - `function Capital() {}` declarations.
964
+ * - `const Capital = () => …` / `const Capital = function () {}` bindings.
965
+ * - `forwardRef(…)` / `memo(…)` argument callbacks.
966
+ * - `export default function () {}`.
967
+ *
968
+ * Arrow components with expression bodies get promoted to block bodies
969
+ * so the hook can be `unshift`ed.
970
+ * @param path Starting path.
971
+ * @returns BlockStatement path of the component's body, or `null`.
972
+ */
973
+ function findComponentBody(path) {
974
+ let current = path;
975
+ while (current) {
976
+ const fn = current.findParent((parent) => parent.isFunction());
977
+ if (!fn)
978
+ return null;
979
+ if (isComponentFunction(fn))
980
+ return ensureBlockBody(fn);
981
+ current = fn;
982
+ }
983
+ return null;
984
+ }
985
+ /**
986
+ * Classify a function path as a React component per the three accepted
987
+ * shapes (PascalCase decl, PascalCase var assignment, forwardRef/memo
988
+ * argument, default export).
989
+ * @param fn Function-like path.
990
+ * @returns Whether the path is a React function component.
991
+ */
992
+ function isComponentFunction(fn) {
993
+ if (fn.isFunctionDeclaration()) {
994
+ const { id } = fn.node;
995
+ if (!id)
996
+ return isExportDefaultValue(fn);
997
+ return isPascalCase(id.name);
998
+ }
999
+ if (fn.isArrowFunctionExpression() || fn.isFunctionExpression()) {
1000
+ return isAssignedToPascalCase(fn) || isHocArgument(fn) || isExportDefaultValue(fn);
1001
+ }
1002
+ return false;
1003
+ }
1004
+ /**
1005
+ * Whether this function is the value of an `export default`.
1006
+ * @param fn Babel path pointing at the function node.
1007
+ * @returns True when the node is directly the default export value.
1008
+ */
1009
+ function isExportDefaultValue(fn) {
1010
+ const { parent } = fn;
1011
+ if (t.isExportDefaultDeclaration(parent))
1012
+ return parent.declaration === fn.node;
1013
+ return false;
1014
+ }
1015
+ /**
1016
+ * Whether this arrow/function-expression is the init of `const Capital = …`.
1017
+ * @param fn Babel path pointing at the function node.
1018
+ * @returns True when the enclosing declarator's id starts with an uppercase letter.
1019
+ */
1020
+ function isAssignedToPascalCase(fn) {
1021
+ const { parent } = fn;
1022
+ if (!t.isVariableDeclarator(parent))
1023
+ return false;
1024
+ if (!t.isIdentifier(parent.id))
1025
+ return false;
1026
+ return isPascalCase(parent.id.name);
1027
+ }
1028
+ /**
1029
+ * Whether this fn is the first argument to `forwardRef(...)` / `memo(...)`.
1030
+ * @param fn Babel path pointing at the function node.
1031
+ * @returns True when wrapped by a recognized React HOC call.
1032
+ */
1033
+ function isHocArgument(fn) {
1034
+ const { parent } = fn;
1035
+ if (!t.isCallExpression(parent))
1036
+ return false;
1037
+ if (parent.arguments[0] !== fn.node)
1038
+ return false;
1039
+ const { callee } = parent;
1040
+ if (!t.isIdentifier(callee))
1041
+ return false;
1042
+ return callee.name === 'forwardRef' || callee.name === 'memo';
1043
+ }
1044
+ /**
1045
+ * Identifier-starts-with-uppercase — Conventional React component marker.
1046
+ * @param name Identifier text.
1047
+ * @returns True when the first character is `A`–`Z`.
1048
+ */
1049
+ function isPascalCase(name) {
1050
+ const first = name.charAt(0);
1051
+ return first >= 'A' && first <= 'Z';
1052
+ }
1053
+ /**
1054
+ * Promote an expression-bodied arrow to a block so we can unshift statements in.
1055
+ * @param fn Babel path at the function / arrow whose body should be a block.
1056
+ * @returns The path, mutated in place when the body was an expression.
1057
+ */
1058
+ function ensureBlockBody(fn) {
1059
+ const bodyPath = fn.get('body');
1060
+ if (Array.isArray(bodyPath))
1061
+ throw new Error('rnwind: unexpected multi-body function node');
1062
+ if (bodyPath.isBlockStatement())
1063
+ return bodyPath;
1064
+ const node = bodyPath.node;
1065
+ bodyPath.replaceWith(t.blockStatement([t.returnStatement(node)]));
1066
+ return bodyPath;
1067
+ }
1068
+ /**
1069
+ * Build a per-file hoist table. Every unique source-order atom list gets
1070
+ * one module-scope `const _c_<hash> = Object.freeze(['a', 'b'])`. Order
1071
+ * is part of the hash key — `className="a b"` and `className="b a"`
1072
+ * intentionally produce different hoisted consts because RN's style
1073
+ * flatten is order-dependent (later atoms override earlier ones for
1074
+ * conflicting props). Canonicalizing by sort would collapse
1075
+ * `opacity-100 opacity-0` and `opacity-0 opacity-100` to the same atom
1076
+ * list and silently break the user's intended last-wins override.
1077
+ * @returns Hoister API.
1078
+ */
1079
+ function createHoister() {
1080
+ const byKey = new Map();
1081
+ const entries = new Map();
1082
+ const refFor = (atoms) => {
1083
+ const ordered = [...atoms];
1084
+ const canonical = ordered.join('\0');
1085
+ const hit = byKey.get(canonical);
1086
+ if (hit)
1087
+ return t.identifier(hit.name);
1088
+ const hash = createHash('sha256').update(canonical).digest('hex').slice(0, 12);
1089
+ const name = `_c_${hash}`;
1090
+ byKey.set(canonical, { name, atoms: ordered });
1091
+ entries.set(name, ordered);
1092
+ return t.identifier(name);
1093
+ };
1094
+ return { refFor, entries };
1095
+ }
1096
+ /**
1097
+ * Build the gradient hoist table. Colour arrays and `(x,y)` point
1098
+ * records each get their own module-scope `const _g_<hash>` so the
1099
+ * JSX site references a stable identity — `<LinearGradient
1100
+ * colors={_g_hash}>`'s prop never changes across renders, which lets
1101
+ * React's prop-diff short-circuit and keeps native-side gradient
1102
+ * rebuilds off the hot path.
1103
+ * @returns GradientHoister API.
1104
+ */
1105
+ function createGradientHoister() {
1106
+ const pool = createInternPool();
1107
+ const refForColors = (colors) => pool.intern('_g', `colors:${colors.join('|')}`, { kind: 'colors', colors });
1108
+ const refForPoint = (point, role) => {
1109
+ const prefix = role === 'start' ? '_gs' : '_ge';
1110
+ return pool.intern(prefix, `point:${point.x},${point.y}`, { kind: 'point', point });
1111
+ };
1112
+ return { refForColors, refForPoint, entries: pool.entries };
1113
+ }
1114
+ /**
1115
+ * Prepend the runtime + style imports to the file's program body.
1116
+ * Only the specifiers actually used by the rewritten code are added
1117
+ * — a file with only interactive rewrites skips `lookupCss` entirely
1118
+ * (it lives inside InteractiveBox) and vice versa.
1119
+ * @param ast File AST.
1120
+ * @param flags Which runtime symbols the rewritten code references.
1121
+ * @param styleSpecifiers Side-effect import specifiers (style.js + keyframes.js).
1122
+ */
1123
+ function prependRuntimeImports(ast, flags, styleSpecifiers) {
1124
+ const heads = [];
1125
+ for (const specifier of styleSpecifiers) {
1126
+ heads.push(t.importDeclaration([], t.stringLiteral(specifier)));
1127
+ }
1128
+ if (flags.touched) {
1129
+ heads.push(t.importDeclaration(buildRuntimeSpecifiers(flags), t.stringLiteral(RUNTIME_MODULE)));
1130
+ }
1131
+ if (heads.length > 0)
1132
+ ast.program.body.unshift(...heads);
1133
+ }
1134
+ /**
1135
+ * Build the import specifiers for the `rnwind` runtime module — only
1136
+ * symbols the rewritten code actually references. Extracted from
1137
+ * {@link prependRuntimeImports} to keep cognitive complexity low.
1138
+ * @param flags Per-file usage flags.
1139
+ * @returns The import specifiers to splice into the runtime import.
1140
+ */
1141
+ function buildRuntimeSpecifiers(flags) {
1142
+ const specifiers = [];
1143
+ const named = (name) => {
1144
+ specifiers.push(t.importSpecifier(t.identifier(name), t.identifier(name)));
1145
+ };
1146
+ if (flags.usedLookupCss)
1147
+ named(LOOKUP_CSS);
1148
+ named(USE_RNWIND_INTERNAL);
1149
+ if (flags.usedMountHaptic)
1150
+ named(USE_MOUNT_HAPTIC);
1151
+ if (flags.usedTriggerHaptic)
1152
+ named(TRIGGER_HAPTIC);
1153
+ if (flags.usedInteractiveBox)
1154
+ named(INTERACTIVE_BOX);
1155
+ return specifiers;
1156
+ }
1157
+ /**
1158
+ * Splice hoisted `const _c_<hash> = ['flex-1', 'bg-primary', ...]`
1159
+ * atom-list declarations into the file right after the imports so
1160
+ * every JSX rewrite site sees them in scope.
1161
+ *
1162
+ * The JSX site references the const as `lookupCss(_c0, _s, userStyle,
1163
+ * …)`. The runtime caches its resolved style array per
1164
+ * (hoist, scheme, stateIndex) against a global version counter, so
1165
+ * subsequent renders return the SAME array reference — zero
1166
+ * allocation on the hot path.
1167
+ * @param ast File AST.
1168
+ * @param entries Hoist table (const name → atom names).
1169
+ */
1170
+ function injectHoistedConsts(ast, entries) {
1171
+ const decls = [];
1172
+ for (const [name, atoms] of entries) {
1173
+ const array = t.arrayExpression(atoms.map((atom) => t.stringLiteral(atom)));
1174
+ decls.push(t.variableDeclaration('const', [t.variableDeclarator(t.identifier(name), array)]));
1175
+ }
1176
+ spliceAfterImports(ast, decls);
1177
+ }
1178
+ /**
1179
+ * Splice gradient const declarations after the imports. Each entry is
1180
+ * either `colors` (frozen string array) or a `point` ({x, y} object
1181
+ * literal) so `<LinearGradient>` gets a stable ref for every gradient
1182
+ * shape.
1183
+ * @param ast File AST to mutate.
1184
+ * @param entries Gradient-hoister entries.
1185
+ */
1186
+ function injectGradientConsts(ast, entries) {
1187
+ const decls = [];
1188
+ for (const [name, entry] of entries) {
1189
+ const init = entry.kind === 'colors'
1190
+ ? t.callExpression(t.memberExpression(t.identifier('Object'), t.identifier('freeze')), [
1191
+ t.arrayExpression(entry.colors.map((c) => t.stringLiteral(c))),
1192
+ ])
1193
+ : t.callExpression(t.memberExpression(t.identifier('Object'), t.identifier('freeze')), [
1194
+ t.objectExpression([
1195
+ t.objectProperty(t.identifier('x'), t.numericLiteral(entry.point.x)),
1196
+ t.objectProperty(t.identifier('y'), t.numericLiteral(entry.point.y)),
1197
+ ]),
1198
+ ]);
1199
+ decls.push(t.variableDeclaration('const', [t.variableDeclarator(t.identifier(name), init)]));
1200
+ }
1201
+ spliceAfterImports(ast, decls);
1202
+ }
1203
+ /**
1204
+ * Insert a block of declarations right after the last import in the
1205
+ * program body. Shared helper for atom-hoist and gradient-hoist.
1206
+ * @param ast File AST.
1207
+ * @param decls Declarations to splice in (already-built statements).
1208
+ */
1209
+ function spliceAfterImports(ast, decls) {
1210
+ if (decls.length === 0)
1211
+ return;
1212
+ const { body } = ast.program;
1213
+ let index = 0;
1214
+ while (index < body.length && t.isImportDeclaration(body[index]))
1215
+ index += 1;
1216
+ body.splice(index, 0, ...decls);
1217
+ }
1218
+ /**
1219
+ * Tokenize a classname literal — split on whitespace, drop empties.
1220
+ * Mirrors what Tailwind + the runtime tokenizer expect.
1221
+ * @param literal Raw classname text.
1222
+ * @returns Atom names in document order.
1223
+ */
1224
+ function tokenize(literal) {
1225
+ const out = [];
1226
+ for (const piece of literal.split(/\s+/)) {
1227
+ if (piece.length > 0)
1228
+ out.push(piece);
1229
+ }
1230
+ return out;
1231
+ }
1232
+
1233
+ export { transformAst };
1234
+ //# sourceMappingURL=transform-ast.mjs.map