@wyw-in-js/transform 2.0.0-alpha.0 → 2.0.0

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 (279) hide show
  1. package/esm/cache.js +7 -0
  2. package/esm/cache.js.map +1 -1
  3. package/esm/debug/fileReporter.js +35 -1
  4. package/esm/debug/fileReporter.js.map +1 -1
  5. package/esm/eval/broker.js +230 -89
  6. package/esm/eval/broker.js.map +1 -1
  7. package/esm/eval/protocol.js.map +1 -1
  8. package/esm/eval/runner.js +143 -24
  9. package/esm/eval/runner.js.map +1 -1
  10. package/esm/module.js +70 -43
  11. package/esm/module.js.map +1 -1
  12. package/esm/transform/Entrypoint.types.js.map +1 -1
  13. package/esm/transform/generators/collect.js +1 -0
  14. package/esm/transform/generators/collect.js.map +1 -1
  15. package/esm/transform/generators/processEntrypoint.js +35 -20
  16. package/esm/transform/generators/processEntrypoint.js.map +1 -1
  17. package/esm/transform/generators/resolveImports.js +48 -2
  18. package/esm/transform/generators/resolveImports.js.map +1 -1
  19. package/esm/transform/generators/resolveStaticOxcValues/cache.js +182 -0
  20. package/esm/transform/generators/resolveStaticOxcValues/cache.js.map +1 -0
  21. package/esm/transform/generators/resolveStaticOxcValues/candidateResolver.js +146 -0
  22. package/esm/transform/generators/resolveStaticOxcValues/candidateResolver.js.map +1 -0
  23. package/esm/transform/generators/resolveStaticOxcValues/dependencies.js +33 -0
  24. package/esm/transform/generators/resolveStaticOxcValues/dependencies.js.map +1 -0
  25. package/esm/transform/generators/resolveStaticOxcValues/environment.js +29 -0
  26. package/esm/transform/generators/resolveStaticOxcValues/environment.js.map +1 -0
  27. package/esm/transform/generators/resolveStaticOxcValues/exportResolver.js +351 -0
  28. package/esm/transform/generators/resolveStaticOxcValues/exportResolver.js.map +1 -0
  29. package/esm/transform/generators/resolveStaticOxcValues/exportTargets.js +253 -0
  30. package/esm/transform/generators/resolveStaticOxcValues/exportTargets.js.map +1 -0
  31. package/esm/transform/generators/resolveStaticOxcValues/objectAssign.js +310 -0
  32. package/esm/transform/generators/resolveStaticOxcValues/objectAssign.js.map +1 -0
  33. package/esm/transform/generators/resolveStaticOxcValues/objectAssignStaticExport.js +74 -0
  34. package/esm/transform/generators/resolveStaticOxcValues/objectAssignStaticExport.js.map +1 -0
  35. package/esm/transform/generators/resolveStaticOxcValues/opaqueRuntime.js +229 -0
  36. package/esm/transform/generators/resolveStaticOxcValues/opaqueRuntime.js.map +1 -0
  37. package/esm/transform/generators/resolveStaticOxcValues/processorStaticExport.js +172 -0
  38. package/esm/transform/generators/resolveStaticOxcValues/processorStaticExport.js.map +1 -0
  39. package/esm/transform/generators/resolveStaticOxcValues/processorStaticModel.js +476 -0
  40. package/esm/transform/generators/resolveStaticOxcValues/processorStaticModel.js.map +1 -0
  41. package/esm/transform/generators/resolveStaticOxcValues/processorTarget.js +55 -0
  42. package/esm/transform/generators/resolveStaticOxcValues/processorTarget.js.map +1 -0
  43. package/esm/transform/generators/resolveStaticOxcValues/programAnalysis.js +123 -0
  44. package/esm/transform/generators/resolveStaticOxcValues/programAnalysis.js.map +1 -0
  45. package/esm/transform/generators/resolveStaticOxcValues/prune.js +272 -0
  46. package/esm/transform/generators/resolveStaticOxcValues/prune.js.map +1 -0
  47. package/esm/transform/generators/resolveStaticOxcValues/resolveStaticOxcPreevalValues.js +184 -0
  48. package/esm/transform/generators/resolveStaticOxcValues/resolveStaticOxcPreevalValues.js.map +1 -0
  49. package/esm/transform/generators/resolveStaticOxcValues/staticExpression.js +298 -0
  50. package/esm/transform/generators/resolveStaticOxcValues/staticExpression.js.map +1 -0
  51. package/esm/transform/generators/resolveStaticOxcValues/staticExpressionDependencies.js +298 -0
  52. package/esm/transform/generators/resolveStaticOxcValues/staticExpressionDependencies.js.map +1 -0
  53. package/esm/transform/generators/resolveStaticOxcValues/types.js +3 -0
  54. package/esm/transform/generators/resolveStaticOxcValues/types.js.map +1 -0
  55. package/esm/transform/generators/resolveStaticOxcValues/zeroArgFunctionStaticExport.js +59 -0
  56. package/esm/transform/generators/resolveStaticOxcValues/zeroArgFunctionStaticExport.js.map +1 -0
  57. package/esm/transform/generators/resolveStaticOxcValues.js +1 -2910
  58. package/esm/transform/generators/resolveStaticOxcValues.js.map +1 -1
  59. package/esm/transform/generators/transform.js +58 -45
  60. package/esm/transform/generators/transform.js.map +1 -1
  61. package/esm/transform/helpers/loadWywOptions.js +33 -4
  62. package/esm/transform/helpers/loadWywOptions.js.map +1 -1
  63. package/esm/transform.js +3 -1
  64. package/esm/transform.js.map +1 -1
  65. package/esm/utils/EventEmitter.js +55 -9
  66. package/esm/utils/EventEmitter.js.map +1 -1
  67. package/esm/utils/applyOxcProcessors/applyOxcProcessors.js +160 -0
  68. package/esm/utils/applyOxcProcessors/applyOxcProcessors.js.map +1 -0
  69. package/esm/utils/applyOxcProcessors/cleanupBindings.js +157 -0
  70. package/esm/utils/applyOxcProcessors/cleanupBindings.js.map +1 -0
  71. package/esm/utils/applyOxcProcessors/cleanupRemovals.js +517 -0
  72. package/esm/utils/applyOxcProcessors/cleanupRemovals.js.map +1 -0
  73. package/esm/utils/applyOxcProcessors/displayName.js +93 -0
  74. package/esm/utils/applyOxcProcessors/displayName.js.map +1 -0
  75. package/esm/utils/applyOxcProcessors/expressionValues.js +152 -0
  76. package/esm/utils/applyOxcProcessors/expressionValues.js.map +1 -0
  77. package/esm/utils/applyOxcProcessors/processorFactory.js +62 -0
  78. package/esm/utils/applyOxcProcessors/processorFactory.js.map +1 -0
  79. package/esm/utils/applyOxcProcessors/processorUsages.js +143 -0
  80. package/esm/utils/applyOxcProcessors/processorUsages.js.map +1 -0
  81. package/esm/utils/applyOxcProcessors/sameFileStaticValues.js +207 -0
  82. package/esm/utils/applyOxcProcessors/sameFileStaticValues.js.map +1 -0
  83. package/esm/utils/applyOxcProcessors/shared.js +29 -0
  84. package/esm/utils/applyOxcProcessors/shared.js.map +1 -0
  85. package/esm/utils/applyOxcProcessors/types.js +2 -0
  86. package/esm/utils/applyOxcProcessors/types.js.map +1 -0
  87. package/esm/utils/applyOxcProcessors.js +1 -1216
  88. package/esm/utils/applyOxcProcessors.js.map +1 -1
  89. package/esm/utils/collectOxcExportsAndImports.js +8 -0
  90. package/esm/utils/collectOxcExportsAndImports.js.map +1 -1
  91. package/esm/utils/collectOxcRuntime/normalizeRuntimeCode.js +157 -0
  92. package/esm/utils/collectOxcRuntime/normalizeRuntimeCode.js.map +1 -0
  93. package/esm/utils/collectOxcRuntime/sourceMap.js +36 -0
  94. package/esm/utils/collectOxcRuntime/sourceMap.js.map +1 -0
  95. package/esm/utils/collectOxcRuntime/types.js +2 -0
  96. package/esm/utils/collectOxcRuntime/types.js.map +1 -0
  97. package/esm/utils/collectOxcRuntime.js +5 -193
  98. package/esm/utils/collectOxcRuntime.js.map +1 -1
  99. package/esm/utils/collectOxcTemplateDependencies/expressionExtraction.js +496 -0
  100. package/esm/utils/collectOxcTemplateDependencies/expressionExtraction.js.map +1 -0
  101. package/esm/utils/collectOxcTemplateDependencies/expressionReplacements.js +113 -0
  102. package/esm/utils/collectOxcTemplateDependencies/expressionReplacements.js.map +1 -0
  103. package/esm/utils/collectOxcTemplateDependencies/scopeAnalysis.js +387 -0
  104. package/esm/utils/collectOxcTemplateDependencies/scopeAnalysis.js.map +1 -0
  105. package/esm/utils/collectOxcTemplateDependencies/staticBindings.js +17 -0
  106. package/esm/utils/collectOxcTemplateDependencies/staticBindings.js.map +1 -0
  107. package/esm/utils/collectOxcTemplateDependencies/staticEvaluator.js +540 -0
  108. package/esm/utils/collectOxcTemplateDependencies/staticEvaluator.js.map +1 -0
  109. package/esm/utils/collectOxcTemplateDependencies/types.js +2 -0
  110. package/esm/utils/collectOxcTemplateDependencies/types.js.map +1 -0
  111. package/esm/utils/collectOxcTemplateDependencies.js +3 -1397
  112. package/esm/utils/collectOxcTemplateDependencies.js.map +1 -1
  113. package/esm/utils/nativeResolver.js +93 -0
  114. package/esm/utils/nativeResolver.js.map +1 -0
  115. package/esm/utils/oxc/ast.js +58 -0
  116. package/esm/utils/oxc/ast.js.map +1 -0
  117. package/esm/utils/oxc/parse.js +3 -0
  118. package/esm/utils/oxc/parse.js.map +1 -0
  119. package/esm/utils/oxc/replacements.js +14 -0
  120. package/esm/utils/oxc/replacements.js.map +1 -0
  121. package/esm/utils/oxc/sourceLocations.js +59 -0
  122. package/esm/utils/oxc/sourceLocations.js.map +1 -0
  123. package/esm/utils/oxcPreevalStage/evalStrategy.js +3 -0
  124. package/esm/utils/oxcPreevalStage/evalStrategy.js.map +1 -0
  125. package/esm/utils/oxcPreevalStage/prepareCode.js +21 -0
  126. package/esm/utils/oxcPreevalStage/prepareCode.js.map +1 -0
  127. package/esm/utils/oxcPreevalStage/prevalExport.js +23 -0
  128. package/esm/utils/oxcPreevalStage/prevalExport.js.map +1 -0
  129. package/esm/utils/oxcPreevalStage/processors.js +17 -0
  130. package/esm/utils/oxcPreevalStage/processors.js.map +1 -0
  131. package/esm/utils/oxcPreevalStage/staticOverlay.js +18 -0
  132. package/esm/utils/oxcPreevalStage/staticOverlay.js.map +1 -0
  133. package/esm/utils/oxcPreevalStage/types.js +2 -0
  134. package/esm/utils/oxcPreevalStage/types.js.map +1 -0
  135. package/esm/utils/oxcPreevalStage.js +17 -73
  136. package/esm/utils/oxcPreevalStage.js.map +1 -1
  137. package/esm/utils/oxcPreevalTransforms.js +106 -66
  138. package/esm/utils/oxcPreevalTransforms.js.map +1 -1
  139. package/esm/utils/parseOxc.js +6 -1
  140. package/esm/utils/parseOxc.js.map +1 -1
  141. package/esm/utils/processorStaticSemantics.js +157 -0
  142. package/esm/utils/processorStaticSemantics.js.map +1 -0
  143. package/esm/utils/resolveWithConditions.js +3 -3
  144. package/esm/utils/resolveWithConditions.js.map +1 -1
  145. package/package.json +6 -5
  146. package/types/cache.js +8 -0
  147. package/types/debug/fileReporter.js +46 -1
  148. package/types/eval/broker.d.ts +6 -2
  149. package/types/eval/broker.js +244 -95
  150. package/types/eval/protocol.d.ts +15 -1
  151. package/types/module.d.ts +4 -1
  152. package/types/module.js +97 -48
  153. package/types/transform/Entrypoint.types.d.ts +4 -0
  154. package/types/transform/generators/collect.js +1 -0
  155. package/types/transform/generators/resolveImports.d.ts +3 -1
  156. package/types/transform/generators/resolveImports.js +49 -2
  157. package/types/transform/generators/resolveStaticOxcValues/cache.d.ts +25 -0
  158. package/types/transform/generators/resolveStaticOxcValues/cache.js +182 -0
  159. package/types/transform/generators/resolveStaticOxcValues/candidateResolver.d.ts +5 -0
  160. package/types/transform/generators/resolveStaticOxcValues/candidateResolver.js +149 -0
  161. package/types/transform/generators/resolveStaticOxcValues/dependencies.d.ts +6 -0
  162. package/types/transform/generators/resolveStaticOxcValues/dependencies.js +37 -0
  163. package/types/transform/generators/resolveStaticOxcValues/environment.d.ts +12 -0
  164. package/types/transform/generators/resolveStaticOxcValues/environment.js +32 -0
  165. package/types/transform/generators/resolveStaticOxcValues/exportResolver.d.ts +4 -0
  166. package/types/transform/generators/resolveStaticOxcValues/exportResolver.js +358 -0
  167. package/types/transform/generators/resolveStaticOxcValues/exportTargets.d.ts +20 -0
  168. package/types/transform/generators/resolveStaticOxcValues/exportTargets.js +285 -0
  169. package/types/transform/generators/resolveStaticOxcValues/objectAssign.d.ts +41 -0
  170. package/types/transform/generators/resolveStaticOxcValues/objectAssign.js +352 -0
  171. package/types/transform/generators/resolveStaticOxcValues/objectAssignStaticExport.d.ts +6 -0
  172. package/types/transform/generators/resolveStaticOxcValues/objectAssignStaticExport.js +88 -0
  173. package/types/transform/generators/resolveStaticOxcValues/opaqueRuntime.d.ts +13 -0
  174. package/types/transform/generators/resolveStaticOxcValues/opaqueRuntime.js +263 -0
  175. package/types/transform/generators/resolveStaticOxcValues/processorStaticExport.d.ts +4 -0
  176. package/types/transform/generators/resolveStaticOxcValues/processorStaticExport.js +191 -0
  177. package/types/transform/generators/resolveStaticOxcValues/processorStaticModel.d.ts +40 -0
  178. package/types/transform/generators/resolveStaticOxcValues/processorStaticModel.js +516 -0
  179. package/types/transform/generators/resolveStaticOxcValues/processorTarget.d.ts +5 -0
  180. package/types/transform/generators/resolveStaticOxcValues/processorTarget.js +62 -0
  181. package/types/transform/generators/resolveStaticOxcValues/programAnalysis.d.ts +11 -0
  182. package/types/transform/generators/resolveStaticOxcValues/programAnalysis.js +133 -0
  183. package/types/transform/generators/resolveStaticOxcValues/prune.d.ts +15 -0
  184. package/types/transform/generators/resolveStaticOxcValues/prune.js +312 -0
  185. package/types/transform/generators/resolveStaticOxcValues/resolveStaticOxcPreevalValues.d.ts +2 -0
  186. package/types/transform/generators/resolveStaticOxcValues/resolveStaticOxcPreevalValues.js +198 -0
  187. package/types/transform/generators/resolveStaticOxcValues/staticExpression.d.ts +25 -0
  188. package/types/transform/generators/resolveStaticOxcValues/staticExpression.js +391 -0
  189. package/types/transform/generators/resolveStaticOxcValues/staticExpressionDependencies.d.ts +14 -0
  190. package/types/transform/generators/resolveStaticOxcValues/staticExpressionDependencies.js +304 -0
  191. package/types/transform/generators/resolveStaticOxcValues/types.d.ts +99 -0
  192. package/types/transform/generators/resolveStaticOxcValues/types.js +2 -0
  193. package/types/transform/generators/resolveStaticOxcValues/zeroArgFunctionStaticExport.d.ts +7 -0
  194. package/types/transform/generators/resolveStaticOxcValues/zeroArgFunctionStaticExport.js +64 -0
  195. package/types/transform/generators/resolveStaticOxcValues.d.ts +1 -2
  196. package/types/transform/generators/resolveStaticOxcValues.js +1 -3235
  197. package/types/transform/generators/transform.js +64 -49
  198. package/types/transform/helpers/loadWywOptions.js +23 -3
  199. package/types/transform.js +3 -1
  200. package/types/utils/EventEmitter.d.ts +16 -1
  201. package/types/utils/EventEmitter.js +57 -9
  202. package/types/utils/applyOxcProcessors/applyOxcProcessors.d.ts +9 -0
  203. package/types/utils/applyOxcProcessors/applyOxcProcessors.js +169 -0
  204. package/types/utils/applyOxcProcessors/cleanupBindings.d.ts +12 -0
  205. package/types/utils/applyOxcProcessors/cleanupBindings.js +189 -0
  206. package/types/utils/applyOxcProcessors/cleanupRemovals.d.ts +16 -0
  207. package/types/utils/applyOxcProcessors/cleanupRemovals.js +567 -0
  208. package/types/utils/applyOxcProcessors/displayName.d.ts +9 -0
  209. package/types/utils/applyOxcProcessors/displayName.js +113 -0
  210. package/types/utils/applyOxcProcessors/expressionValues.d.ts +11 -0
  211. package/types/utils/applyOxcProcessors/expressionValues.js +170 -0
  212. package/types/utils/applyOxcProcessors/processorFactory.d.ts +7 -0
  213. package/types/utils/applyOxcProcessors/processorFactory.js +70 -0
  214. package/types/utils/applyOxcProcessors/processorUsages.d.ts +16 -0
  215. package/types/utils/applyOxcProcessors/processorUsages.js +163 -0
  216. package/types/utils/applyOxcProcessors/sameFileStaticValues.d.ts +23 -0
  217. package/types/utils/applyOxcProcessors/sameFileStaticValues.js +211 -0
  218. package/types/utils/applyOxcProcessors/shared.d.ts +10 -0
  219. package/types/utils/applyOxcProcessors/shared.js +37 -0
  220. package/types/utils/applyOxcProcessors/types.d.ts +88 -0
  221. package/types/utils/applyOxcProcessors/types.js +1 -0
  222. package/types/utils/applyOxcProcessors.d.ts +1 -16
  223. package/types/utils/applyOxcProcessors.js +1 -1391
  224. package/types/utils/collectOxcExportsAndImports.js +8 -0
  225. package/types/utils/collectOxcRuntime/normalizeRuntimeCode.d.ts +1 -0
  226. package/types/utils/collectOxcRuntime/normalizeRuntimeCode.js +185 -0
  227. package/types/utils/collectOxcRuntime/sourceMap.d.ts +2 -0
  228. package/types/utils/collectOxcRuntime/sourceMap.js +37 -0
  229. package/types/utils/collectOxcRuntime/types.d.ts +17 -0
  230. package/types/utils/collectOxcRuntime/types.js +1 -0
  231. package/types/utils/collectOxcRuntime.d.ts +2 -12
  232. package/types/utils/collectOxcRuntime.js +5 -224
  233. package/types/utils/collectOxcTemplateDependencies/expressionExtraction.d.ts +6 -0
  234. package/types/utils/collectOxcTemplateDependencies/expressionExtraction.js +550 -0
  235. package/types/utils/collectOxcTemplateDependencies/expressionReplacements.d.ts +11 -0
  236. package/types/utils/collectOxcTemplateDependencies/expressionReplacements.js +131 -0
  237. package/types/utils/collectOxcTemplateDependencies/scopeAnalysis.d.ts +20 -0
  238. package/types/utils/collectOxcTemplateDependencies/scopeAnalysis.js +434 -0
  239. package/types/utils/collectOxcTemplateDependencies/staticBindings.d.ts +7 -0
  240. package/types/utils/collectOxcTemplateDependencies/staticBindings.js +13 -0
  241. package/types/utils/collectOxcTemplateDependencies/staticEvaluator.d.ts +13 -0
  242. package/types/utils/collectOxcTemplateDependencies/staticEvaluator.js +626 -0
  243. package/types/utils/collectOxcTemplateDependencies/types.d.ts +116 -0
  244. package/types/utils/collectOxcTemplateDependencies/types.js +1 -0
  245. package/types/utils/collectOxcTemplateDependencies.d.ts +4 -38
  246. package/types/utils/collectOxcTemplateDependencies.js +3 -1580
  247. package/types/utils/nativeResolver.d.ts +13 -0
  248. package/types/utils/nativeResolver.js +91 -0
  249. package/types/utils/oxc/ast.d.ts +4 -0
  250. package/types/utils/oxc/ast.js +61 -0
  251. package/types/utils/oxc/parse.d.ts +3 -0
  252. package/types/utils/oxc/parse.js +2 -0
  253. package/types/utils/oxc/replacements.d.ts +12 -0
  254. package/types/utils/oxc/replacements.js +18 -0
  255. package/types/utils/oxc/sourceLocations.d.ts +5 -0
  256. package/types/utils/oxc/sourceLocations.js +63 -0
  257. package/types/utils/oxcPreevalStage/evalStrategy.d.ts +3 -0
  258. package/types/utils/oxcPreevalStage/evalStrategy.js +2 -0
  259. package/types/utils/oxcPreevalStage/prepareCode.d.ts +3 -0
  260. package/types/utils/oxcPreevalStage/prepareCode.js +20 -0
  261. package/types/utils/oxcPreevalStage/prevalExport.d.ts +1 -0
  262. package/types/utils/oxcPreevalStage/prevalExport.js +22 -0
  263. package/types/utils/oxcPreevalStage/processors.d.ts +10 -0
  264. package/types/utils/oxcPreevalStage/processors.js +16 -0
  265. package/types/utils/oxcPreevalStage/staticOverlay.d.ts +3 -0
  266. package/types/utils/oxcPreevalStage/staticOverlay.js +23 -0
  267. package/types/utils/oxcPreevalStage/types.d.ts +22 -0
  268. package/types/utils/oxcPreevalStage/types.js +1 -0
  269. package/types/utils/oxcPreevalStage.d.ts +2 -18
  270. package/types/utils/oxcPreevalStage.js +17 -79
  271. package/types/utils/oxcPreevalTransforms.js +111 -75
  272. package/types/utils/parseOxc.js +6 -1
  273. package/types/utils/processorStaticSemantics.d.ts +13 -0
  274. package/types/utils/processorStaticSemantics.js +191 -0
  275. package/types/utils/resolveWithConditions.js +3 -3
  276. package/esm/eval/resolverStrategy.js +0 -51
  277. package/esm/eval/resolverStrategy.js.map +0 -1
  278. package/types/eval/resolverStrategy.d.ts +0 -13
  279. package/types/eval/resolverStrategy.js +0 -46
@@ -0,0 +1,567 @@
1
+ /* eslint-disable no-restricted-syntax,no-continue */
2
+ import { getOxcNodeChildren, isOxcNode } from '../oxc/ast';
3
+ import { applyOxcReplacements } from '../oxc/replacements';
4
+ import { collectDeclaredNames, collectImportLocalNames, collectReferencedNames, collectRemovableNamesFromStatements, collectTopLevelBindings, collectTopLevelBindingsFromStatements, collectTopLevelStatementInfos, getImportSpecifierLocalName, isNodeReference, } from './cleanupBindings';
5
+ import { GENERATED_HELPER_NAME_RE, parseOxc } from './shared';
6
+ export const createScopedCleanupScope = (parent) => ({
7
+ bindings: new Map(),
8
+ parent,
9
+ });
10
+ export const resolveScopedBinding = (scope, name) => {
11
+ let current = scope;
12
+ while (current) {
13
+ const bindingId = current.bindings.get(name);
14
+ if (bindingId) {
15
+ return bindingId;
16
+ }
17
+ current = current.parent;
18
+ }
19
+ return null;
20
+ };
21
+ export const collectScopedBindingInfos = (program) => {
22
+ const bindings = new Map();
23
+ let sequence = 0;
24
+ const addBinding = (scope, name, kind, declaration) => {
25
+ const id = `${kind}:${name}:${declaration.start}:${sequence}`;
26
+ sequence += 1;
27
+ bindings.set(id, {
28
+ declaration,
29
+ dependencies: new Set(),
30
+ externalReferences: 0,
31
+ id,
32
+ incomingFromBindings: new Set(),
33
+ kind,
34
+ name,
35
+ });
36
+ scope.bindings.set(name, id);
37
+ return id;
38
+ };
39
+ const addPatternBindings = (scope, pattern, kind, declaration) => {
40
+ collectDeclaredNames(pattern).forEach((name) => {
41
+ addBinding(scope, name, kind, declaration);
42
+ });
43
+ };
44
+ const recordReference = (scope, name, ownerBindingId) => {
45
+ const targetId = resolveScopedBinding(scope, name);
46
+ if (!targetId) {
47
+ return;
48
+ }
49
+ const target = bindings.get(targetId);
50
+ if (!target) {
51
+ return;
52
+ }
53
+ if (ownerBindingId && ownerBindingId !== targetId) {
54
+ target.incomingFromBindings.add(ownerBindingId);
55
+ bindings.get(ownerBindingId)?.dependencies.add(targetId);
56
+ return;
57
+ }
58
+ target.externalReferences += 1;
59
+ };
60
+ let walk;
61
+ function walkPatternReferenceSubexpressions(node, scope, ownerBindingId) {
62
+ if (node.type === 'Identifier') {
63
+ return;
64
+ }
65
+ if (node.type === 'TSParameterProperty') {
66
+ walkPatternReferenceSubexpressions(node.parameter, scope, ownerBindingId);
67
+ return;
68
+ }
69
+ if (node.type === 'RestElement') {
70
+ walkPatternReferenceSubexpressions(node.argument, scope, ownerBindingId);
71
+ return;
72
+ }
73
+ if (node.type === 'AssignmentPattern') {
74
+ walkPatternReferenceSubexpressions(node.left, scope, ownerBindingId);
75
+ walk(node.right, scope, node, ownerBindingId);
76
+ return;
77
+ }
78
+ if (node.type === 'ObjectPattern') {
79
+ node.properties.forEach((property) => {
80
+ if (property.type === 'RestElement') {
81
+ walkPatternReferenceSubexpressions(property.argument, scope, ownerBindingId);
82
+ return;
83
+ }
84
+ if (property.computed && isOxcNode(property.key)) {
85
+ walk(property.key, scope, property, ownerBindingId);
86
+ }
87
+ walkPatternReferenceSubexpressions(property.value, scope, ownerBindingId);
88
+ });
89
+ return;
90
+ }
91
+ if (node.type === 'ArrayPattern') {
92
+ node.elements.forEach((element) => {
93
+ if (element && isOxcNode(element)) {
94
+ walkPatternReferenceSubexpressions(element, scope, ownerBindingId);
95
+ }
96
+ });
97
+ }
98
+ }
99
+ walk = (node, scope, parent = null, ownerBindingId = null) => {
100
+ if (node.type === 'ImportDeclaration') {
101
+ const { specifiers } = node;
102
+ if (Array.isArray(specifiers)) {
103
+ specifiers.forEach((specifier) => {
104
+ const { local } = specifier;
105
+ if (isOxcNode(local) &&
106
+ local.type === 'Identifier' &&
107
+ typeof local.name === 'string') {
108
+ addBinding(scope, local.name, 'import', node);
109
+ }
110
+ });
111
+ }
112
+ return;
113
+ }
114
+ if (node.type === 'ExportNamedDeclaration' && node.declaration) {
115
+ walk(node.declaration, scope, node, ownerBindingId);
116
+ collectTopLevelBindings(node).forEach((name) => {
117
+ recordReference(scope, name, ownerBindingId);
118
+ });
119
+ return;
120
+ }
121
+ if (node.type === 'ExportDefaultDeclaration') {
122
+ const { declaration } = node;
123
+ if (isOxcNode(declaration)) {
124
+ walk(declaration, scope, node, ownerBindingId);
125
+ if ((declaration.type === 'FunctionDeclaration' ||
126
+ declaration.type === 'ClassDeclaration') &&
127
+ declaration.id) {
128
+ recordReference(scope, declaration.id.name, ownerBindingId);
129
+ }
130
+ }
131
+ return;
132
+ }
133
+ if (node.type === 'VariableDeclaration') {
134
+ const { declarations } = node;
135
+ if (!Array.isArray(declarations)) {
136
+ return;
137
+ }
138
+ declarations.forEach((declarator) => {
139
+ const { id } = declarator;
140
+ if (isOxcNode(id)) {
141
+ addPatternBindings(scope, id, 'variable', node);
142
+ }
143
+ });
144
+ declarations.forEach((declarator) => {
145
+ const { id } = declarator;
146
+ const { init } = declarator;
147
+ if (!isOxcNode(id) || !isOxcNode(init)) {
148
+ return;
149
+ }
150
+ const ownerName = collectDeclaredNames(id)[0] ?? null;
151
+ const nextOwner = ownerName !== null ? resolveScopedBinding(scope, ownerName) : null;
152
+ walk(init, scope, declarator, nextOwner);
153
+ });
154
+ return;
155
+ }
156
+ if (node.type === 'FunctionDeclaration' && node.id) {
157
+ const functionBindingId = addBinding(scope, node.id.name, 'function', node);
158
+ const fnScope = createScopedCleanupScope(scope);
159
+ node.params.forEach((param) => {
160
+ addPatternBindings(fnScope, param, 'param', param);
161
+ walkPatternReferenceSubexpressions(param, fnScope, functionBindingId);
162
+ });
163
+ if (node.body) {
164
+ walk(node.body, fnScope, node, functionBindingId);
165
+ }
166
+ return;
167
+ }
168
+ if (node.type === 'FunctionExpression' ||
169
+ node.type === 'ArrowFunctionExpression') {
170
+ const fnScope = createScopedCleanupScope(scope);
171
+ if (node.type === 'FunctionExpression' && node.id) {
172
+ addBinding(fnScope, node.id.name, 'function', node);
173
+ }
174
+ node.params.forEach((param) => {
175
+ addPatternBindings(fnScope, param, 'param', param);
176
+ walkPatternReferenceSubexpressions(param, fnScope, ownerBindingId);
177
+ });
178
+ if (node.body) {
179
+ walk(node.body, fnScope, node, ownerBindingId);
180
+ }
181
+ return;
182
+ }
183
+ if (node.type === 'BlockStatement') {
184
+ const blockScope = createScopedCleanupScope(scope);
185
+ getOxcNodeChildren(node).forEach((child) => walk(child, blockScope, node, ownerBindingId));
186
+ return;
187
+ }
188
+ if (isNodeReference(node, parent) &&
189
+ 'name' in node &&
190
+ typeof node.name === 'string') {
191
+ recordReference(scope, node.name, ownerBindingId);
192
+ }
193
+ getOxcNodeChildren(node).forEach((child) => walk(child, scope, node, ownerBindingId));
194
+ };
195
+ walk(program, createScopedCleanupScope(null));
196
+ return bindings;
197
+ };
198
+ export const collectScopedRemovableBindingIds = (bindings, initialNames) => {
199
+ const removable = new Set();
200
+ let changed = true;
201
+ while (changed) {
202
+ changed = false;
203
+ for (const binding of bindings.values()) {
204
+ if (!removable.has(binding.id) &&
205
+ binding.kind !== 'import' &&
206
+ binding.kind !== 'param' &&
207
+ binding.externalReferences === 0) {
208
+ const seededByName = initialNames.has(binding.name) ||
209
+ (GENERATED_HELPER_NAME_RE.test(binding.name) &&
210
+ binding.incomingFromBindings.size === 0);
211
+ const allIncomingRemovable = binding.incomingFromBindings.size > 0 &&
212
+ [...binding.incomingFromBindings].every((sourceId) => removable.has(sourceId));
213
+ if ((seededByName && binding.incomingFromBindings.size === 0) ||
214
+ allIncomingRemovable) {
215
+ removable.add(binding.id);
216
+ changed = true;
217
+ }
218
+ }
219
+ }
220
+ }
221
+ return removable;
222
+ };
223
+ export function expandImportRemovalRange(code, start, end) {
224
+ let removalStart = start;
225
+ while (removalStart > 0 &&
226
+ (code[removalStart - 1] === ' ' || code[removalStart - 1] === '\t')) {
227
+ removalStart -= 1;
228
+ }
229
+ let removalEnd = end;
230
+ if (code[removalEnd] === ';') {
231
+ removalEnd += 1;
232
+ }
233
+ while (removalEnd < code.length &&
234
+ (code[removalEnd] === ' ' || code[removalEnd] === '\t')) {
235
+ removalEnd += 1;
236
+ }
237
+ if (code[removalEnd] === '\r' && code[removalEnd + 1] === '\n') {
238
+ removalEnd += 2;
239
+ }
240
+ else if (code[removalEnd] === '\n') {
241
+ removalEnd += 1;
242
+ }
243
+ return {
244
+ end: removalEnd,
245
+ start: removalStart,
246
+ value: '',
247
+ };
248
+ }
249
+ export const collectUnusedScopedDeclarationRemovals = (code, bindings, initialRemovableNames) => {
250
+ const removableBindingIds = collectScopedRemovableBindingIds(bindings, initialRemovableNames);
251
+ const removals = new Map();
252
+ bindings.forEach((binding) => {
253
+ if (!removableBindingIds.has(binding.id) ||
254
+ binding.kind === 'import' ||
255
+ binding.kind === 'param' ||
256
+ binding.externalReferences > 0 ||
257
+ [...binding.incomingFromBindings].some((sourceId) => !removableBindingIds.has(sourceId))) {
258
+ return;
259
+ }
260
+ if (binding.kind === 'function' &&
261
+ binding.declaration.type === 'FunctionDeclaration') {
262
+ const range = expandImportRemovalRange(code, binding.declaration.start, binding.declaration.end);
263
+ removals.set(`${range.start}:${range.end}`, range);
264
+ return;
265
+ }
266
+ if (binding.declaration.type !== 'VariableDeclaration') {
267
+ return;
268
+ }
269
+ const { declarations } = binding.declaration;
270
+ if (!Array.isArray(declarations) || declarations.length !== 1) {
271
+ return;
272
+ }
273
+ const range = expandImportRemovalRange(code, binding.declaration.start, binding.declaration.end);
274
+ removals.set(`${range.start}:${range.end}`, range);
275
+ });
276
+ return [...removals.values()];
277
+ };
278
+ export const expandImportSpecifierRemovalRange = (code, start, end) => {
279
+ let removalStart = start;
280
+ let removalEnd = end;
281
+ let whitespaceStart = removalStart;
282
+ while (whitespaceStart > 0 &&
283
+ (code[whitespaceStart - 1] === ' ' || code[whitespaceStart - 1] === '\t')) {
284
+ whitespaceStart -= 1;
285
+ }
286
+ if (code[whitespaceStart - 1] !== '{') {
287
+ removalStart = whitespaceStart;
288
+ }
289
+ while (removalEnd < code.length &&
290
+ (code[removalEnd] === ' ' || code[removalEnd] === '\t')) {
291
+ removalEnd += 1;
292
+ }
293
+ if (code[removalEnd] === ',') {
294
+ removalEnd += 1;
295
+ while (removalEnd < code.length &&
296
+ (code[removalEnd] === ' ' || code[removalEnd] === '\t')) {
297
+ removalEnd += 1;
298
+ }
299
+ }
300
+ else {
301
+ while (removalStart > 0 &&
302
+ (code[removalStart - 1] === ' ' || code[removalStart - 1] === '\t')) {
303
+ removalStart -= 1;
304
+ }
305
+ if (code[removalStart - 1] === ',') {
306
+ removalStart -= 1;
307
+ while (removalStart > 0 &&
308
+ (code[removalStart - 1] === ' ' || code[removalStart - 1] === '\t')) {
309
+ removalStart -= 1;
310
+ }
311
+ }
312
+ }
313
+ return {
314
+ end: removalEnd,
315
+ start: removalStart,
316
+ value: '',
317
+ };
318
+ };
319
+ export const mergeEmptyRemovalRanges = (removals) => {
320
+ if (removals.length <= 1) {
321
+ return removals;
322
+ }
323
+ const sorted = [...removals].sort((a, b) => a.start - b.start);
324
+ const merged = [];
325
+ sorted.forEach((removal) => {
326
+ const previous = merged[merged.length - 1];
327
+ if (previous &&
328
+ previous.value === '' &&
329
+ removal.value === '' &&
330
+ removal.start <= previous.end) {
331
+ previous.end = Math.max(previous.end, removal.end);
332
+ return;
333
+ }
334
+ merged.push({ ...removal });
335
+ });
336
+ return merged;
337
+ };
338
+ export const collectUnusedImportRemovals = (code, program, referencedNames, removableNames, preserveSideEffectImportLocals, preserveSideEffectImportOrderLocals = preserveSideEffectImportLocals) => {
339
+ const removals = [];
340
+ const importSourceByLocal = new Map();
341
+ const removedSideEffectImportRanges = [];
342
+ const keptImportRangesBySource = new Map();
343
+ program.body.forEach((statement) => {
344
+ if (statement.type !== 'ImportDeclaration') {
345
+ return;
346
+ }
347
+ const localNames = collectImportLocalNames(statement);
348
+ const source = code.slice(statement.source.start, statement.source.end);
349
+ const orderedLocalNames = localNames.filter((localName) => preserveSideEffectImportOrderLocals.has(localName));
350
+ const sideEffectLocalNames = localNames.filter((localName) => preserveSideEffectImportLocals.has(localName));
351
+ [...orderedLocalNames, ...sideEffectLocalNames].forEach((localName) => {
352
+ importSourceByLocal.set(localName, source);
353
+ });
354
+ const removableLocalNames = localNames.filter((localName) => removableNames.has(localName));
355
+ if (removableLocalNames.length > 0 &&
356
+ removableLocalNames.length === localNames.length &&
357
+ removableLocalNames.every((localName) => !referencedNames.has(localName))) {
358
+ if (removableLocalNames.some((localName) => preserveSideEffectImportLocals.has(localName))) {
359
+ removedSideEffectImportRanges.push({
360
+ end: statement.end,
361
+ start: statement.start,
362
+ });
363
+ return;
364
+ }
365
+ removals.push(expandImportRemovalRange(code, statement.start, statement.end));
366
+ return;
367
+ }
368
+ if (orderedLocalNames.length > 0 && !keptImportRangesBySource.has(source)) {
369
+ keptImportRangesBySource.set(source, {
370
+ end: statement.end,
371
+ start: statement.start,
372
+ });
373
+ }
374
+ const { specifiers } = statement;
375
+ if (!Array.isArray(specifiers) || specifiers.length <= 1) {
376
+ return;
377
+ }
378
+ specifiers.forEach((specifier) => {
379
+ if (!isOxcNode(specifier)) {
380
+ return;
381
+ }
382
+ const localName = getImportSpecifierLocalName(specifier);
383
+ if (localName &&
384
+ removableNames.has(localName) &&
385
+ !referencedNames.has(localName)) {
386
+ removals.push(expandImportSpecifierRemovalRange(code, specifier.start, specifier.end));
387
+ }
388
+ });
389
+ });
390
+ if (removedSideEffectImportRanges.length > 0) {
391
+ const seenSources = new Set();
392
+ const removedRanges = removedSideEffectImportRanges.sort((a, b) => a.start - b.start);
393
+ const [firstRemoved, ...restRemoved] = removedRanges;
394
+ const pendingImports = [];
395
+ let insertionAfterLastKept = null;
396
+ let usedFirstRemovedRange = false;
397
+ const flushBefore = (position) => {
398
+ if (pendingImports.length === 0) {
399
+ return;
400
+ }
401
+ removals.push({
402
+ end: position,
403
+ start: position,
404
+ value: `${pendingImports.join('\n')}\n`,
405
+ });
406
+ pendingImports.length = 0;
407
+ };
408
+ [...preserveSideEffectImportOrderLocals].forEach((localName) => {
409
+ const source = importSourceByLocal.get(localName);
410
+ if (!source) {
411
+ return;
412
+ }
413
+ const keptRange = keptImportRangesBySource.get(source);
414
+ if (keptRange) {
415
+ flushBefore(keptRange.start);
416
+ insertionAfterLastKept = keptRange.end;
417
+ if (preserveSideEffectImportLocals.has(localName)) {
418
+ seenSources.add(source);
419
+ }
420
+ return;
421
+ }
422
+ if (!preserveSideEffectImportLocals.has(localName) ||
423
+ seenSources.has(source)) {
424
+ return;
425
+ }
426
+ seenSources.add(source);
427
+ pendingImports.push(`import ${source};`);
428
+ });
429
+ if (pendingImports.length > 0) {
430
+ if (insertionAfterLastKept !== null) {
431
+ removals.push({
432
+ end: insertionAfterLastKept,
433
+ start: insertionAfterLastKept,
434
+ value: `\n${pendingImports.join('\n')}`,
435
+ });
436
+ }
437
+ else if (firstRemoved) {
438
+ usedFirstRemovedRange = true;
439
+ removals.push({
440
+ end: firstRemoved.end,
441
+ start: firstRemoved.start,
442
+ value: pendingImports.join('\n'),
443
+ });
444
+ }
445
+ }
446
+ removals.push(...(usedFirstRemovedRange ? restRemoved : removedRanges).map((range) => ({
447
+ ...range,
448
+ value: '',
449
+ })));
450
+ }
451
+ return removals;
452
+ };
453
+ export const collectUnusedTopLevelDeclarationRemovals = (code, program, referencedNames, removableNames) => {
454
+ const removals = [];
455
+ program.body.forEach((statement) => {
456
+ if (statement.type !== 'VariableDeclaration') {
457
+ return;
458
+ }
459
+ const localNames = [...collectTopLevelBindings(statement)];
460
+ if (localNames.length > 0 &&
461
+ localNames.every((localName) => removableNames.has(localName)) &&
462
+ localNames.every((localName) => !referencedNames.has(localName))) {
463
+ removals.push(expandImportRemovalRange(code, statement.start, statement.end));
464
+ }
465
+ });
466
+ return removals;
467
+ };
468
+ export const collectUnusedGeneratedHelperDeclarationRemovals = (code, program, referencedNames) => {
469
+ const removals = [];
470
+ program.body.forEach((statement) => {
471
+ if (statement.type !== 'VariableDeclaration') {
472
+ return;
473
+ }
474
+ const localNames = [...collectTopLevelBindings(statement)];
475
+ if (localNames.length > 0 &&
476
+ localNames.every((localName) => GENERATED_HELPER_NAME_RE.test(localName)) &&
477
+ localNames.every((localName) => !referencedNames.has(localName))) {
478
+ removals.push(expandImportRemovalRange(code, statement.start, statement.end));
479
+ }
480
+ });
481
+ return removals;
482
+ };
483
+ export const collectTopLevelExpressionStatementRemovals = (code, statements, topLevelBindings, removableExpressionRefs) => {
484
+ const removals = [];
485
+ statements.forEach((statement) => {
486
+ if (statement.node.type !== 'ExpressionStatement') {
487
+ return;
488
+ }
489
+ const { expression } = statement.node;
490
+ const isPureExpression = expression.type === 'Identifier' ||
491
+ expression.type === 'Literal' ||
492
+ expression.type === 'ObjectExpression' ||
493
+ expression.type === 'ArrayExpression' ||
494
+ expression.type === 'ArrowFunctionExpression' ||
495
+ expression.type === 'FunctionExpression' ||
496
+ (expression.type === 'TemplateLiteral' &&
497
+ expression.expressions.length === 0);
498
+ if (!isPureExpression) {
499
+ return;
500
+ }
501
+ const localReferences = [...statement.references].filter((name) => topLevelBindings.has(name));
502
+ if (localReferences.length > 0 &&
503
+ localReferences.every((name) => removableExpressionRefs.has(name))) {
504
+ removals.push(expandImportRemovalRange(code, statement.node.start, statement.node.end));
505
+ }
506
+ });
507
+ return removals;
508
+ };
509
+ export const collectEmptyTopLevelBlockRemovals = (code, program) => {
510
+ const removals = [];
511
+ program.body.forEach((statement) => {
512
+ if (statement.type !== 'BlockStatement' || statement.body.length > 0) {
513
+ return;
514
+ }
515
+ removals.push(expandImportRemovalRange(code, statement.start, statement.end));
516
+ });
517
+ return removals;
518
+ };
519
+ export const removeUnusedAfterReplacement = (code, filename, initialRemovableNames, removableExpressionRefs, preserveSideEffectImportLocals, preserveSideEffectImportOrderLocals = preserveSideEffectImportLocals) => {
520
+ let current = code;
521
+ let program = null;
522
+ const cumulativeRemovableNames = new Set(initialRemovableNames);
523
+ // Incremental cleanup loop: validate-by-parsing the next iteration's
524
+ // candidate code AND reuse that parse as the next iter's `program` input,
525
+ // instead of re-parsing at the top of the next iter. Saves one parse per
526
+ // loop revolution (N+1 parses for an N-iter loop instead of 2N).
527
+ // Also short-circuits a round earlier when no removals were collected.
528
+ for (let idx = 0; idx < 5; idx += 1) {
529
+ const previous = current;
530
+ if (program === null) {
531
+ program = parseOxc(current, filename);
532
+ }
533
+ const statements = collectTopLevelStatementInfos(program);
534
+ const removableNames = collectRemovableNamesFromStatements(statements, cumulativeRemovableNames);
535
+ removableNames.forEach((name) => cumulativeRemovableNames.add(name));
536
+ const referencedNames = collectReferencedNames(program);
537
+ const topLevelBindings = collectTopLevelBindingsFromStatements(statements);
538
+ const scopedBindings = collectScopedBindingInfos(program);
539
+ const removals = mergeEmptyRemovalRanges([
540
+ ...collectUnusedScopedDeclarationRemovals(current, scopedBindings, cumulativeRemovableNames),
541
+ ...collectUnusedTopLevelDeclarationRemovals(current, program, referencedNames, cumulativeRemovableNames),
542
+ ...collectUnusedGeneratedHelperDeclarationRemovals(current, program, referencedNames),
543
+ ...collectUnusedImportRemovals(current, program, referencedNames, cumulativeRemovableNames, preserveSideEffectImportLocals, preserveSideEffectImportOrderLocals),
544
+ ...collectTopLevelExpressionStatementRemovals(current, statements, topLevelBindings, removableExpressionRefs),
545
+ ...collectEmptyTopLevelBlockRemovals(current, program),
546
+ ]);
547
+ if (removals.length === 0) {
548
+ // Convergence: next iter would parse the same code and see the same
549
+ // removable set. Skip the round of walks + parse.
550
+ return current;
551
+ }
552
+ const next = applyOxcReplacements(current, removals);
553
+ try {
554
+ // Validate + capture the AST for the next iteration in one parse.
555
+ program = parseOxc(next, filename);
556
+ current = next;
557
+ }
558
+ catch {
559
+ // Pathological removal — drop this iteration and return prior state.
560
+ return current;
561
+ }
562
+ if (current === previous) {
563
+ return current;
564
+ }
565
+ }
566
+ return current;
567
+ };
@@ -0,0 +1,9 @@
1
+ import type { ExpressionValue } from '@wyw-in-js/shared';
2
+ import type { Node, Program } from 'oxc-parser';
3
+ import type { OxcStaticValue } from '../collectOxcTemplateDependencies';
4
+ import type { AnyNode } from './types';
5
+ export declare const getPropertyKeyName: (property: AnyNode, code: string) => string | null;
6
+ export declare const getDisplayName: (ancestors: Node[], idx: number, code: string, filename?: string | null) => string;
7
+ export declare const getTagOwner: (ancestors: Node[]) => AnyNode | null;
8
+ export declare const isTagReferenced: (program: Program, ancestors: Node[]) => boolean;
9
+ export declare const collectSameFileProcessorStaticValues: (expressionValues: Omit<ExpressionValue, "buildCodeFrameError">[], processorStaticValuesByLocal: Map<string, unknown>) => OxcStaticValue[];
@@ -0,0 +1,113 @@
1
+ /* eslint-disable no-restricted-syntax */
2
+ import { basename, dirname } from 'path';
3
+ import { ValueType } from '@wyw-in-js/shared';
4
+ import { isOxcNode, walkOxc } from '../oxc/ast';
5
+ import { isNodeReference } from './cleanupBindings';
6
+ export const getPropertyKeyName = (property, code) => {
7
+ const { key } = property;
8
+ if (!isOxcNode(key)) {
9
+ return null;
10
+ }
11
+ if (key.type === 'Identifier') {
12
+ return key.name;
13
+ }
14
+ if (key.type === 'Literal') {
15
+ return String(key.value);
16
+ }
17
+ return typeof key.start === 'number' && typeof key.end === 'number'
18
+ ? code.slice(key.start, key.end)
19
+ : null;
20
+ };
21
+ export const getDisplayName = (ancestors, idx, code, filename) => {
22
+ const owner = [...ancestors].reverse().find((node) => {
23
+ return (node.type === 'Property' ||
24
+ node.type === 'JSXOpeningElement' ||
25
+ node.type === 'VariableDeclarator');
26
+ });
27
+ if (owner?.type === 'Property') {
28
+ const keyName = getPropertyKeyName(owner, code);
29
+ if (keyName) {
30
+ return keyName;
31
+ }
32
+ }
33
+ else if (owner?.type === 'JSXOpeningElement') {
34
+ const { name } = owner;
35
+ if (isOxcNode(name) && name.type === 'JSXIdentifier') {
36
+ return name.name;
37
+ }
38
+ }
39
+ else if (owner?.type === 'VariableDeclarator') {
40
+ const { id } = owner;
41
+ if (isOxcNode(id) && id.type === 'Identifier') {
42
+ return id.name;
43
+ }
44
+ }
45
+ let displayName = basename(filename ?? 'unknown').replace(/\.[a-z\d]+$/, '');
46
+ if (filename && /^index\.[a-z\d]+$/.test(basename(filename))) {
47
+ displayName = basename(dirname(filename));
48
+ }
49
+ if (!displayName) {
50
+ throw new Error("Couldn't determine a name for the component. Ensure that it's either:\n" +
51
+ '- Assigned to a variable\n' +
52
+ '- Is an object property\n' +
53
+ '- Is a prop in a JSX element\n');
54
+ }
55
+ return `${displayName}${idx}`;
56
+ };
57
+ export const getTagOwner = (ancestors) => {
58
+ const owner = [...ancestors]
59
+ .reverse()
60
+ .find((node) => node.type === 'Property' ||
61
+ node.type === 'JSXOpeningElement' ||
62
+ node.type === 'VariableDeclarator');
63
+ return owner ?? null;
64
+ };
65
+ export const isTagReferenced = (program, ancestors) => {
66
+ const owner = getTagOwner(ancestors);
67
+ if (owner?.type !== 'VariableDeclarator') {
68
+ return true;
69
+ }
70
+ const { id } = owner;
71
+ if (!isOxcNode(id) || id.type !== 'Identifier') {
72
+ return true;
73
+ }
74
+ if (ancestors.some((node) => node.type === 'ExportNamedDeclaration')) {
75
+ return true;
76
+ }
77
+ let referenced = false;
78
+ walkOxc(program, (node, parent) => {
79
+ const referenceName = node.type === 'Identifier' || node.type === 'JSXIdentifier'
80
+ ? node.name
81
+ : null;
82
+ if (referenced ||
83
+ referenceName !== id.name ||
84
+ (node.type === 'Identifier' &&
85
+ node.start === id.start &&
86
+ node.end === id.end)) {
87
+ return;
88
+ }
89
+ referenced = isNodeReference(node, parent);
90
+ });
91
+ return referenced;
92
+ };
93
+ export const collectSameFileProcessorStaticValues = (expressionValues, processorStaticValuesByLocal) => {
94
+ const staticValues = [];
95
+ const seen = new Set();
96
+ expressionValues.forEach((value) => {
97
+ if (value.kind !== ValueType.LAZY) {
98
+ return;
99
+ }
100
+ const staticValue = processorStaticValuesByLocal.get(value.source);
101
+ if (staticValue === undefined ||
102
+ value.ex.type !== 'Identifier' ||
103
+ seen.has(value.ex.name)) {
104
+ return;
105
+ }
106
+ seen.add(value.ex.name);
107
+ staticValues.push({
108
+ name: value.ex.name,
109
+ value: staticValue,
110
+ });
111
+ });
112
+ return staticValues;
113
+ };