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

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 (270) 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/processEntrypoint.js +35 -20
  14. package/esm/transform/generators/processEntrypoint.js.map +1 -1
  15. package/esm/transform/generators/resolveImports.js +48 -2
  16. package/esm/transform/generators/resolveImports.js.map +1 -1
  17. package/esm/transform/generators/resolveStaticOxcValues/cache.js +182 -0
  18. package/esm/transform/generators/resolveStaticOxcValues/cache.js.map +1 -0
  19. package/esm/transform/generators/resolveStaticOxcValues/candidateResolver.js +146 -0
  20. package/esm/transform/generators/resolveStaticOxcValues/candidateResolver.js.map +1 -0
  21. package/esm/transform/generators/resolveStaticOxcValues/dependencies.js +33 -0
  22. package/esm/transform/generators/resolveStaticOxcValues/dependencies.js.map +1 -0
  23. package/esm/transform/generators/resolveStaticOxcValues/environment.js +29 -0
  24. package/esm/transform/generators/resolveStaticOxcValues/environment.js.map +1 -0
  25. package/esm/transform/generators/resolveStaticOxcValues/exportResolver.js +351 -0
  26. package/esm/transform/generators/resolveStaticOxcValues/exportResolver.js.map +1 -0
  27. package/esm/transform/generators/resolveStaticOxcValues/exportTargets.js +253 -0
  28. package/esm/transform/generators/resolveStaticOxcValues/exportTargets.js.map +1 -0
  29. package/esm/transform/generators/resolveStaticOxcValues/objectAssign.js +310 -0
  30. package/esm/transform/generators/resolveStaticOxcValues/objectAssign.js.map +1 -0
  31. package/esm/transform/generators/resolveStaticOxcValues/objectAssignStaticExport.js +74 -0
  32. package/esm/transform/generators/resolveStaticOxcValues/objectAssignStaticExport.js.map +1 -0
  33. package/esm/transform/generators/resolveStaticOxcValues/opaqueRuntime.js +229 -0
  34. package/esm/transform/generators/resolveStaticOxcValues/opaqueRuntime.js.map +1 -0
  35. package/esm/transform/generators/resolveStaticOxcValues/processorStaticExport.js +172 -0
  36. package/esm/transform/generators/resolveStaticOxcValues/processorStaticExport.js.map +1 -0
  37. package/esm/transform/generators/resolveStaticOxcValues/processorStaticModel.js +476 -0
  38. package/esm/transform/generators/resolveStaticOxcValues/processorStaticModel.js.map +1 -0
  39. package/esm/transform/generators/resolveStaticOxcValues/processorTarget.js +55 -0
  40. package/esm/transform/generators/resolveStaticOxcValues/processorTarget.js.map +1 -0
  41. package/esm/transform/generators/resolveStaticOxcValues/programAnalysis.js +123 -0
  42. package/esm/transform/generators/resolveStaticOxcValues/programAnalysis.js.map +1 -0
  43. package/esm/transform/generators/resolveStaticOxcValues/prune.js +186 -0
  44. package/esm/transform/generators/resolveStaticOxcValues/prune.js.map +1 -0
  45. package/esm/transform/generators/resolveStaticOxcValues/resolveStaticOxcPreevalValues.js +183 -0
  46. package/esm/transform/generators/resolveStaticOxcValues/resolveStaticOxcPreevalValues.js.map +1 -0
  47. package/esm/transform/generators/resolveStaticOxcValues/staticExpression.js +298 -0
  48. package/esm/transform/generators/resolveStaticOxcValues/staticExpression.js.map +1 -0
  49. package/esm/transform/generators/resolveStaticOxcValues/staticExpressionDependencies.js +298 -0
  50. package/esm/transform/generators/resolveStaticOxcValues/staticExpressionDependencies.js.map +1 -0
  51. package/esm/transform/generators/resolveStaticOxcValues/types.js +3 -0
  52. package/esm/transform/generators/resolveStaticOxcValues/types.js.map +1 -0
  53. package/esm/transform/generators/resolveStaticOxcValues/zeroArgFunctionStaticExport.js +59 -0
  54. package/esm/transform/generators/resolveStaticOxcValues/zeroArgFunctionStaticExport.js.map +1 -0
  55. package/esm/transform/generators/resolveStaticOxcValues.js +1 -2910
  56. package/esm/transform/generators/resolveStaticOxcValues.js.map +1 -1
  57. package/esm/transform/generators/transform.js +57 -45
  58. package/esm/transform/generators/transform.js.map +1 -1
  59. package/esm/transform/helpers/loadWywOptions.js +33 -4
  60. package/esm/transform/helpers/loadWywOptions.js.map +1 -1
  61. package/esm/transform.js +3 -1
  62. package/esm/transform.js.map +1 -1
  63. package/esm/utils/EventEmitter.js +42 -9
  64. package/esm/utils/EventEmitter.js.map +1 -1
  65. package/esm/utils/applyOxcProcessors/applyOxcProcessors.js +160 -0
  66. package/esm/utils/applyOxcProcessors/applyOxcProcessors.js.map +1 -0
  67. package/esm/utils/applyOxcProcessors/cleanupBindings.js +157 -0
  68. package/esm/utils/applyOxcProcessors/cleanupBindings.js.map +1 -0
  69. package/esm/utils/applyOxcProcessors/cleanupRemovals.js +431 -0
  70. package/esm/utils/applyOxcProcessors/cleanupRemovals.js.map +1 -0
  71. package/esm/utils/applyOxcProcessors/displayName.js +93 -0
  72. package/esm/utils/applyOxcProcessors/displayName.js.map +1 -0
  73. package/esm/utils/applyOxcProcessors/expressionValues.js +152 -0
  74. package/esm/utils/applyOxcProcessors/expressionValues.js.map +1 -0
  75. package/esm/utils/applyOxcProcessors/processorFactory.js +62 -0
  76. package/esm/utils/applyOxcProcessors/processorFactory.js.map +1 -0
  77. package/esm/utils/applyOxcProcessors/processorUsages.js +143 -0
  78. package/esm/utils/applyOxcProcessors/processorUsages.js.map +1 -0
  79. package/esm/utils/applyOxcProcessors/sameFileStaticValues.js +207 -0
  80. package/esm/utils/applyOxcProcessors/sameFileStaticValues.js.map +1 -0
  81. package/esm/utils/applyOxcProcessors/shared.js +29 -0
  82. package/esm/utils/applyOxcProcessors/shared.js.map +1 -0
  83. package/esm/utils/applyOxcProcessors/types.js +2 -0
  84. package/esm/utils/applyOxcProcessors/types.js.map +1 -0
  85. package/esm/utils/applyOxcProcessors.js +1 -1216
  86. package/esm/utils/applyOxcProcessors.js.map +1 -1
  87. package/esm/utils/collectOxcRuntime/normalizeRuntimeCode.js +157 -0
  88. package/esm/utils/collectOxcRuntime/normalizeRuntimeCode.js.map +1 -0
  89. package/esm/utils/collectOxcRuntime/sourceMap.js +36 -0
  90. package/esm/utils/collectOxcRuntime/sourceMap.js.map +1 -0
  91. package/esm/utils/collectOxcRuntime/types.js +2 -0
  92. package/esm/utils/collectOxcRuntime/types.js.map +1 -0
  93. package/esm/utils/collectOxcRuntime.js +5 -193
  94. package/esm/utils/collectOxcRuntime.js.map +1 -1
  95. package/esm/utils/collectOxcTemplateDependencies/expressionExtraction.js +496 -0
  96. package/esm/utils/collectOxcTemplateDependencies/expressionExtraction.js.map +1 -0
  97. package/esm/utils/collectOxcTemplateDependencies/expressionReplacements.js +113 -0
  98. package/esm/utils/collectOxcTemplateDependencies/expressionReplacements.js.map +1 -0
  99. package/esm/utils/collectOxcTemplateDependencies/scopeAnalysis.js +387 -0
  100. package/esm/utils/collectOxcTemplateDependencies/scopeAnalysis.js.map +1 -0
  101. package/esm/utils/collectOxcTemplateDependencies/staticBindings.js +17 -0
  102. package/esm/utils/collectOxcTemplateDependencies/staticBindings.js.map +1 -0
  103. package/esm/utils/collectOxcTemplateDependencies/staticEvaluator.js +540 -0
  104. package/esm/utils/collectOxcTemplateDependencies/staticEvaluator.js.map +1 -0
  105. package/esm/utils/collectOxcTemplateDependencies/types.js +2 -0
  106. package/esm/utils/collectOxcTemplateDependencies/types.js.map +1 -0
  107. package/esm/utils/collectOxcTemplateDependencies.js +3 -1397
  108. package/esm/utils/collectOxcTemplateDependencies.js.map +1 -1
  109. package/esm/utils/nativeResolver.js +93 -0
  110. package/esm/utils/nativeResolver.js.map +1 -0
  111. package/esm/utils/oxc/ast.js +28 -0
  112. package/esm/utils/oxc/ast.js.map +1 -0
  113. package/esm/utils/oxc/parse.js +3 -0
  114. package/esm/utils/oxc/parse.js.map +1 -0
  115. package/esm/utils/oxc/replacements.js +14 -0
  116. package/esm/utils/oxc/replacements.js.map +1 -0
  117. package/esm/utils/oxc/sourceLocations.js +59 -0
  118. package/esm/utils/oxc/sourceLocations.js.map +1 -0
  119. package/esm/utils/oxcPreevalStage/evalStrategy.js +3 -0
  120. package/esm/utils/oxcPreevalStage/evalStrategy.js.map +1 -0
  121. package/esm/utils/oxcPreevalStage/prepareCode.js +21 -0
  122. package/esm/utils/oxcPreevalStage/prepareCode.js.map +1 -0
  123. package/esm/utils/oxcPreevalStage/prevalExport.js +23 -0
  124. package/esm/utils/oxcPreevalStage/prevalExport.js.map +1 -0
  125. package/esm/utils/oxcPreevalStage/processors.js +17 -0
  126. package/esm/utils/oxcPreevalStage/processors.js.map +1 -0
  127. package/esm/utils/oxcPreevalStage/staticOverlay.js +18 -0
  128. package/esm/utils/oxcPreevalStage/staticOverlay.js.map +1 -0
  129. package/esm/utils/oxcPreevalStage/types.js +2 -0
  130. package/esm/utils/oxcPreevalStage/types.js.map +1 -0
  131. package/esm/utils/oxcPreevalStage.js +17 -73
  132. package/esm/utils/oxcPreevalStage.js.map +1 -1
  133. package/esm/utils/oxcPreevalTransforms.js +12 -1
  134. package/esm/utils/oxcPreevalTransforms.js.map +1 -1
  135. package/esm/utils/processorStaticSemantics.js +157 -0
  136. package/esm/utils/processorStaticSemantics.js.map +1 -0
  137. package/esm/utils/resolveWithConditions.js +3 -3
  138. package/esm/utils/resolveWithConditions.js.map +1 -1
  139. package/package.json +4 -3
  140. package/types/cache.js +8 -0
  141. package/types/debug/fileReporter.js +46 -1
  142. package/types/eval/broker.d.ts +6 -2
  143. package/types/eval/broker.js +244 -95
  144. package/types/eval/protocol.d.ts +15 -1
  145. package/types/module.d.ts +4 -1
  146. package/types/module.js +97 -48
  147. package/types/transform/Entrypoint.types.d.ts +3 -0
  148. package/types/transform/generators/resolveImports.d.ts +3 -1
  149. package/types/transform/generators/resolveImports.js +49 -2
  150. package/types/transform/generators/resolveStaticOxcValues/cache.d.ts +25 -0
  151. package/types/transform/generators/resolveStaticOxcValues/cache.js +182 -0
  152. package/types/transform/generators/resolveStaticOxcValues/candidateResolver.d.ts +5 -0
  153. package/types/transform/generators/resolveStaticOxcValues/candidateResolver.js +149 -0
  154. package/types/transform/generators/resolveStaticOxcValues/dependencies.d.ts +6 -0
  155. package/types/transform/generators/resolveStaticOxcValues/dependencies.js +37 -0
  156. package/types/transform/generators/resolveStaticOxcValues/environment.d.ts +12 -0
  157. package/types/transform/generators/resolveStaticOxcValues/environment.js +32 -0
  158. package/types/transform/generators/resolveStaticOxcValues/exportResolver.d.ts +4 -0
  159. package/types/transform/generators/resolveStaticOxcValues/exportResolver.js +358 -0
  160. package/types/transform/generators/resolveStaticOxcValues/exportTargets.d.ts +20 -0
  161. package/types/transform/generators/resolveStaticOxcValues/exportTargets.js +285 -0
  162. package/types/transform/generators/resolveStaticOxcValues/objectAssign.d.ts +41 -0
  163. package/types/transform/generators/resolveStaticOxcValues/objectAssign.js +352 -0
  164. package/types/transform/generators/resolveStaticOxcValues/objectAssignStaticExport.d.ts +6 -0
  165. package/types/transform/generators/resolveStaticOxcValues/objectAssignStaticExport.js +88 -0
  166. package/types/transform/generators/resolveStaticOxcValues/opaqueRuntime.d.ts +13 -0
  167. package/types/transform/generators/resolveStaticOxcValues/opaqueRuntime.js +263 -0
  168. package/types/transform/generators/resolveStaticOxcValues/processorStaticExport.d.ts +4 -0
  169. package/types/transform/generators/resolveStaticOxcValues/processorStaticExport.js +191 -0
  170. package/types/transform/generators/resolveStaticOxcValues/processorStaticModel.d.ts +40 -0
  171. package/types/transform/generators/resolveStaticOxcValues/processorStaticModel.js +516 -0
  172. package/types/transform/generators/resolveStaticOxcValues/processorTarget.d.ts +5 -0
  173. package/types/transform/generators/resolveStaticOxcValues/processorTarget.js +62 -0
  174. package/types/transform/generators/resolveStaticOxcValues/programAnalysis.d.ts +11 -0
  175. package/types/transform/generators/resolveStaticOxcValues/programAnalysis.js +133 -0
  176. package/types/transform/generators/resolveStaticOxcValues/prune.d.ts +15 -0
  177. package/types/transform/generators/resolveStaticOxcValues/prune.js +220 -0
  178. package/types/transform/generators/resolveStaticOxcValues/resolveStaticOxcPreevalValues.d.ts +2 -0
  179. package/types/transform/generators/resolveStaticOxcValues/resolveStaticOxcPreevalValues.js +197 -0
  180. package/types/transform/generators/resolveStaticOxcValues/staticExpression.d.ts +25 -0
  181. package/types/transform/generators/resolveStaticOxcValues/staticExpression.js +391 -0
  182. package/types/transform/generators/resolveStaticOxcValues/staticExpressionDependencies.d.ts +14 -0
  183. package/types/transform/generators/resolveStaticOxcValues/staticExpressionDependencies.js +304 -0
  184. package/types/transform/generators/resolveStaticOxcValues/types.d.ts +99 -0
  185. package/types/transform/generators/resolveStaticOxcValues/types.js +2 -0
  186. package/types/transform/generators/resolveStaticOxcValues/zeroArgFunctionStaticExport.d.ts +7 -0
  187. package/types/transform/generators/resolveStaticOxcValues/zeroArgFunctionStaticExport.js +64 -0
  188. package/types/transform/generators/resolveStaticOxcValues.d.ts +1 -2
  189. package/types/transform/generators/resolveStaticOxcValues.js +1 -3235
  190. package/types/transform/generators/transform.js +63 -49
  191. package/types/transform/helpers/loadWywOptions.js +23 -3
  192. package/types/transform.js +3 -1
  193. package/types/utils/EventEmitter.d.ts +16 -1
  194. package/types/utils/EventEmitter.js +44 -9
  195. package/types/utils/applyOxcProcessors/applyOxcProcessors.d.ts +8 -0
  196. package/types/utils/applyOxcProcessors/applyOxcProcessors.js +167 -0
  197. package/types/utils/applyOxcProcessors/cleanupBindings.d.ts +12 -0
  198. package/types/utils/applyOxcProcessors/cleanupBindings.js +189 -0
  199. package/types/utils/applyOxcProcessors/cleanupRemovals.d.ts +16 -0
  200. package/types/utils/applyOxcProcessors/cleanupRemovals.js +482 -0
  201. package/types/utils/applyOxcProcessors/displayName.d.ts +9 -0
  202. package/types/utils/applyOxcProcessors/displayName.js +113 -0
  203. package/types/utils/applyOxcProcessors/expressionValues.d.ts +11 -0
  204. package/types/utils/applyOxcProcessors/expressionValues.js +170 -0
  205. package/types/utils/applyOxcProcessors/processorFactory.d.ts +7 -0
  206. package/types/utils/applyOxcProcessors/processorFactory.js +70 -0
  207. package/types/utils/applyOxcProcessors/processorUsages.d.ts +16 -0
  208. package/types/utils/applyOxcProcessors/processorUsages.js +163 -0
  209. package/types/utils/applyOxcProcessors/sameFileStaticValues.d.ts +23 -0
  210. package/types/utils/applyOxcProcessors/sameFileStaticValues.js +211 -0
  211. package/types/utils/applyOxcProcessors/shared.d.ts +10 -0
  212. package/types/utils/applyOxcProcessors/shared.js +37 -0
  213. package/types/utils/applyOxcProcessors/types.d.ts +88 -0
  214. package/types/utils/applyOxcProcessors/types.js +1 -0
  215. package/types/utils/applyOxcProcessors.d.ts +1 -16
  216. package/types/utils/applyOxcProcessors.js +1 -1391
  217. package/types/utils/collectOxcRuntime/normalizeRuntimeCode.d.ts +1 -0
  218. package/types/utils/collectOxcRuntime/normalizeRuntimeCode.js +185 -0
  219. package/types/utils/collectOxcRuntime/sourceMap.d.ts +2 -0
  220. package/types/utils/collectOxcRuntime/sourceMap.js +37 -0
  221. package/types/utils/collectOxcRuntime/types.d.ts +16 -0
  222. package/types/utils/collectOxcRuntime/types.js +1 -0
  223. package/types/utils/collectOxcRuntime.d.ts +2 -12
  224. package/types/utils/collectOxcRuntime.js +5 -224
  225. package/types/utils/collectOxcTemplateDependencies/expressionExtraction.d.ts +6 -0
  226. package/types/utils/collectOxcTemplateDependencies/expressionExtraction.js +550 -0
  227. package/types/utils/collectOxcTemplateDependencies/expressionReplacements.d.ts +11 -0
  228. package/types/utils/collectOxcTemplateDependencies/expressionReplacements.js +131 -0
  229. package/types/utils/collectOxcTemplateDependencies/scopeAnalysis.d.ts +20 -0
  230. package/types/utils/collectOxcTemplateDependencies/scopeAnalysis.js +434 -0
  231. package/types/utils/collectOxcTemplateDependencies/staticBindings.d.ts +7 -0
  232. package/types/utils/collectOxcTemplateDependencies/staticBindings.js +13 -0
  233. package/types/utils/collectOxcTemplateDependencies/staticEvaluator.d.ts +13 -0
  234. package/types/utils/collectOxcTemplateDependencies/staticEvaluator.js +626 -0
  235. package/types/utils/collectOxcTemplateDependencies/types.d.ts +116 -0
  236. package/types/utils/collectOxcTemplateDependencies/types.js +1 -0
  237. package/types/utils/collectOxcTemplateDependencies.d.ts +4 -38
  238. package/types/utils/collectOxcTemplateDependencies.js +3 -1580
  239. package/types/utils/nativeResolver.d.ts +13 -0
  240. package/types/utils/nativeResolver.js +91 -0
  241. package/types/utils/oxc/ast.d.ts +4 -0
  242. package/types/utils/oxc/ast.js +37 -0
  243. package/types/utils/oxc/parse.d.ts +3 -0
  244. package/types/utils/oxc/parse.js +2 -0
  245. package/types/utils/oxc/replacements.d.ts +12 -0
  246. package/types/utils/oxc/replacements.js +18 -0
  247. package/types/utils/oxc/sourceLocations.d.ts +5 -0
  248. package/types/utils/oxc/sourceLocations.js +63 -0
  249. package/types/utils/oxcPreevalStage/evalStrategy.d.ts +3 -0
  250. package/types/utils/oxcPreevalStage/evalStrategy.js +2 -0
  251. package/types/utils/oxcPreevalStage/prepareCode.d.ts +3 -0
  252. package/types/utils/oxcPreevalStage/prepareCode.js +20 -0
  253. package/types/utils/oxcPreevalStage/prevalExport.d.ts +1 -0
  254. package/types/utils/oxcPreevalStage/prevalExport.js +22 -0
  255. package/types/utils/oxcPreevalStage/processors.d.ts +10 -0
  256. package/types/utils/oxcPreevalStage/processors.js +16 -0
  257. package/types/utils/oxcPreevalStage/staticOverlay.d.ts +3 -0
  258. package/types/utils/oxcPreevalStage/staticOverlay.js +23 -0
  259. package/types/utils/oxcPreevalStage/types.d.ts +22 -0
  260. package/types/utils/oxcPreevalStage/types.js +1 -0
  261. package/types/utils/oxcPreevalStage.d.ts +2 -18
  262. package/types/utils/oxcPreevalStage.js +17 -79
  263. package/types/utils/oxcPreevalTransforms.js +14 -1
  264. package/types/utils/processorStaticSemantics.d.ts +13 -0
  265. package/types/utils/processorStaticSemantics.js +191 -0
  266. package/types/utils/resolveWithConditions.js +3 -3
  267. package/esm/eval/resolverStrategy.js +0 -51
  268. package/esm/eval/resolverStrategy.js.map +0 -1
  269. package/types/eval/resolverStrategy.d.ts +0 -13
  270. package/types/eval/resolverStrategy.js +0 -46
@@ -0,0 +1,157 @@
1
+ import { isNotNull } from "../isNotNull.js";
2
+ import { getOxcNodeChildren, isOxcNode, walkOxc } from "../oxc/ast.js";
3
+ export const collectUsedNames = (program) => {
4
+ const names = new Set();
5
+ walkOxc(program, (node) => {
6
+ if (node.type === "Identifier") {
7
+ names.add(node.name);
8
+ }
9
+ });
10
+ return names;
11
+ };
12
+ export const isNodeReference = (node, parent) => {
13
+ if (node.type === "Identifier") {
14
+ const parentRecord = parent;
15
+ if (!parentRecord) {
16
+ return true;
17
+ }
18
+ if (parent?.type === "ImportDeclaration" || parent?.type === "ImportSpecifier" || parent?.type === "ImportDefaultSpecifier" || parent?.type === "ImportNamespaceSpecifier") {
19
+ return false;
20
+ }
21
+ if (parent?.type === "MemberExpression" && parentRecord.property === node && !parentRecord.computed) {
22
+ return false;
23
+ }
24
+ if ((parent?.type === "VariableDeclarator" || parent?.type === "FunctionDeclaration" || parent?.type === "ClassDeclaration" || parent?.type === "ClassExpression") && parentRecord.id === node) {
25
+ return false;
26
+ }
27
+ if ((parent?.type === "PropertyDefinition" || parent?.type === "MethodDefinition") && parentRecord.key === node && !parentRecord.computed) {
28
+ return false;
29
+ }
30
+ if (parent?.type === "Property" && parentRecord.key === node && parentRecord.value !== node && !parentRecord.computed) {
31
+ return false;
32
+ }
33
+ return true;
34
+ }
35
+ if (node.type === "JSXIdentifier") {
36
+ const parentRecord = parent;
37
+ if (parent?.type === "JSXAttribute" && parentRecord?.name === node) {
38
+ return false;
39
+ }
40
+ return true;
41
+ }
42
+ return false;
43
+ };
44
+ export const collectReferencedNames = (root) => {
45
+ const names = new Set();
46
+ const walk = (node, parent = null) => {
47
+ if (node.type === "ImportDeclaration") {
48
+ return;
49
+ }
50
+ if (isNodeReference(node, parent) && "name" in node && typeof node.name === "string") {
51
+ names.add(node.name);
52
+ }
53
+ getOxcNodeChildren(node).forEach((child) => walk(child, node));
54
+ };
55
+ walk(root);
56
+ return names;
57
+ };
58
+ export const collectImportLocalNames = (node) => {
59
+ if (node.type !== "ImportDeclaration") {
60
+ return [];
61
+ }
62
+ const { specifiers } = node;
63
+ if (!Array.isArray(specifiers)) {
64
+ return [];
65
+ }
66
+ return specifiers.map((specifier) => {
67
+ const { local } = specifier;
68
+ return isOxcNode(local) && "name" in local && typeof local.name === "string" ? local.name : null;
69
+ }).filter(isNotNull);
70
+ };
71
+ export const getImportSpecifierLocalName = (node) => {
72
+ const { local } = node;
73
+ return isOxcNode(local) && "name" in local && typeof local.name === "string" ? local.name : null;
74
+ };
75
+ export const collectDeclaredNames = (node) => {
76
+ if (node.type === "Identifier") {
77
+ return [node.name];
78
+ }
79
+ if (node.type === "RestElement") {
80
+ return collectDeclaredNames(node.argument);
81
+ }
82
+ if (node.type === "AssignmentPattern") {
83
+ return collectDeclaredNames(node.left);
84
+ }
85
+ if (node.type === "ObjectPattern") {
86
+ return node.properties.flatMap((property) => property.type === "RestElement" ? collectDeclaredNames(property.argument) : collectDeclaredNames(property.value));
87
+ }
88
+ if (node.type === "ArrayPattern") {
89
+ return node.elements.flatMap((element) => element ? collectDeclaredNames(element) : []);
90
+ }
91
+ if (node.type === "TSParameterProperty") {
92
+ return collectDeclaredNames(node.parameter);
93
+ }
94
+ return [];
95
+ };
96
+ export const collectTopLevelBindings = (statement) => {
97
+ const bindings = new Set();
98
+ if (statement.type === "ImportDeclaration") {
99
+ collectImportLocalNames(statement).forEach((name) => bindings.add(name));
100
+ return bindings;
101
+ }
102
+ if (statement.type === "VariableDeclaration") {
103
+ const { declarations } = statement;
104
+ if (!Array.isArray(declarations)) {
105
+ return bindings;
106
+ }
107
+ declarations.forEach((declarator) => {
108
+ const { id } = declarator;
109
+ if (isOxcNode(id)) {
110
+ collectDeclaredNames(id).forEach((name) => bindings.add(name));
111
+ }
112
+ });
113
+ return bindings;
114
+ }
115
+ if ((statement.type === "FunctionDeclaration" || statement.type === "ClassDeclaration" || statement.type === "TSEnumDeclaration") && "id" in statement) {
116
+ const { id } = statement;
117
+ if (isOxcNode(id) && id.type === "Identifier") {
118
+ bindings.add(id.name);
119
+ }
120
+ return bindings;
121
+ }
122
+ if (statement.type === "ExportNamedDeclaration") {
123
+ const { declaration } = statement;
124
+ return isOxcNode(declaration) ? collectTopLevelBindings(declaration) : bindings;
125
+ }
126
+ return bindings;
127
+ };
128
+ export const collectTopLevelStatementInfos = (program) => program.body.map((statement) => ({
129
+ bindings: collectTopLevelBindings(statement),
130
+ node: statement,
131
+ references: collectReferencedNames(statement)
132
+ }));
133
+ export const collectTopLevelBindingsFromStatements = (statements) => new Set(statements.flatMap((statement) => [...statement.bindings]));
134
+ export const collectRemovableNamesFromStatements = (statements, initialNames) => {
135
+ const removable = new Set(initialNames);
136
+ const bindingToStatement = new Map();
137
+ statements.forEach((statement) => {
138
+ statement.bindings.forEach((name) => {
139
+ bindingToStatement.set(name, statement);
140
+ });
141
+ });
142
+ const queue = [...removable];
143
+ while (queue.length > 0) {
144
+ const name = queue.shift();
145
+ const statement = bindingToStatement.get(name);
146
+ if (statement) {
147
+ statement.references.forEach((reference) => {
148
+ if (bindingToStatement.has(reference) && !removable.has(reference)) {
149
+ removable.add(reference);
150
+ queue.push(reference);
151
+ }
152
+ });
153
+ }
154
+ }
155
+ return removable;
156
+ };
157
+ //# sourceMappingURL=cleanupBindings.js.map
@@ -0,0 +1 @@
1
+ {"mappings":"AAIA,SAAS,iBAAiB;AAC1B,SAAS,oBAAoB,WAAW,eAAe;AAGvD,OAAO,MAAM,oBAAoB,YAAkC;CACjE,MAAM,QAAQ,IAAI,KAAa;AAC/B,SAAQ,UAAU,SAAS;AACzB,MAAI,KAAK,SAAS,cAAc;AAC9B,SAAM,IAAI,KAAK,KAAK;;GAEtB;AAEF,QAAO;;AAGT,OAAO,MAAM,mBAAmB,MAAY,WAAiC;AAC3E,KAAI,K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],"version":3,"sourcesContent":["/* eslint-disable no-restricted-syntax */\n\nimport type { Node, Program } from 'oxc-parser';\n\nimport { isNotNull } from '../isNotNull';\nimport { getOxcNodeChildren, isOxcNode, walkOxc } from '../oxc/ast';\nimport type { AnyNode, TopLevelStatementInfo } from './types';\n\nexport const collectUsedNames = (program: Program): Set<string> => {\n const names = new Set<string>();\n walkOxc(program, (node) => {\n if (node.type === 'Identifier') {\n names.add(node.name);\n }\n });\n\n return names;\n};\n\nexport const isNodeReference = (node: Node, parent: Node | null): boolean => {\n if (node.type === 'Identifier') {\n const parentRecord = parent as AnyNode | null;\n\n if (!parentRecord) {\n return true;\n }\n\n if (\n parent?.type === 'ImportDeclaration' ||\n parent?.type === 'ImportSpecifier' ||\n parent?.type === 'ImportDefaultSpecifier' ||\n parent?.type === 'ImportNamespaceSpecifier'\n ) {\n return false;\n }\n\n if (\n parent?.type === 'MemberExpression' &&\n parentRecord.property === node &&\n !parentRecord.computed\n ) {\n return false;\n }\n\n if (\n (parent?.type === 'VariableDeclarator' ||\n parent?.type === 'FunctionDeclaration' ||\n parent?.type === 'ClassDeclaration' ||\n parent?.type === 'ClassExpression') &&\n parentRecord.id === node\n ) {\n return false;\n }\n\n if (\n (parent?.type === 'PropertyDefinition' ||\n parent?.type === 'MethodDefinition') &&\n parentRecord.key === node &&\n !parentRecord.computed\n ) {\n return false;\n }\n\n if (\n parent?.type === 'Property' &&\n parentRecord.key === node &&\n parentRecord.value !== node &&\n !parentRecord.computed\n ) {\n return false;\n }\n\n return true;\n }\n\n if (node.type === 'JSXIdentifier') {\n const parentRecord = parent as AnyNode | null;\n if (parent?.type === 'JSXAttribute' && parentRecord?.name === node) {\n return false;\n }\n\n return true;\n }\n\n return false;\n};\n\nexport const collectReferencedNames = (root: Node): Set<string> => {\n const names = new Set<string>();\n\n const walk = (node: Node, parent: Node | null = null): void => {\n if (node.type === 'ImportDeclaration') {\n return;\n }\n\n if (\n isNodeReference(node, parent) &&\n 'name' in node &&\n typeof node.name === 'string'\n ) {\n names.add(node.name);\n }\n\n getOxcNodeChildren(node).forEach((child) => walk(child, node));\n };\n\n walk(root);\n return names;\n};\n\nexport const collectImportLocalNames = (node: Node): string[] => {\n if (node.type !== 'ImportDeclaration') {\n return [];\n }\n\n const { specifiers } = node as AnyNode;\n if (!Array.isArray(specifiers)) {\n return [];\n }\n\n return specifiers\n .map((specifier) => {\n const { local } = specifier as AnyNode;\n return isOxcNode(local) &&\n 'name' in local &&\n typeof local.name === 'string'\n ? local.name\n : null;\n })\n .filter(isNotNull);\n};\n\nexport const getImportSpecifierLocalName = (node: Node): string | null => {\n const { local } = node as AnyNode;\n return isOxcNode(local) && 'name' in local && typeof local.name === 'string'\n ? local.name\n : null;\n};\n\nexport const collectDeclaredNames = (node: Node): string[] => {\n if (node.type === 'Identifier') {\n return [node.name];\n }\n\n if (node.type === 'RestElement') {\n return collectDeclaredNames(node.argument);\n }\n\n if (node.type === 'AssignmentPattern') {\n return collectDeclaredNames(node.left);\n }\n\n if (node.type === 'ObjectPattern') {\n return node.properties.flatMap((property) =>\n property.type === 'RestElement'\n ? collectDeclaredNames(property.argument)\n : collectDeclaredNames(property.value)\n );\n }\n\n if (node.type === 'ArrayPattern') {\n return node.elements.flatMap((element) =>\n element ? collectDeclaredNames(element) : []\n );\n }\n\n if (node.type === 'TSParameterProperty') {\n return collectDeclaredNames(node.parameter);\n }\n\n return [];\n};\n\nexport const collectTopLevelBindings = (statement: Node): Set<string> => {\n const bindings = new Set<string>();\n\n if (statement.type === 'ImportDeclaration') {\n collectImportLocalNames(statement).forEach((name) => bindings.add(name));\n return bindings;\n }\n\n if (statement.type === 'VariableDeclaration') {\n const { declarations } = statement as AnyNode;\n if (!Array.isArray(declarations)) {\n return bindings;\n }\n\n declarations.forEach((declarator) => {\n const { id } = declarator as AnyNode;\n if (isOxcNode(id)) {\n collectDeclaredNames(id).forEach((name) => bindings.add(name));\n }\n });\n return bindings;\n }\n\n if (\n (statement.type === 'FunctionDeclaration' ||\n statement.type === 'ClassDeclaration' ||\n statement.type === 'TSEnumDeclaration') &&\n 'id' in statement\n ) {\n const { id } = statement as AnyNode;\n if (isOxcNode(id) && id.type === 'Identifier') {\n bindings.add(id.name);\n }\n return bindings;\n }\n\n if (statement.type === 'ExportNamedDeclaration') {\n const { declaration } = statement as AnyNode;\n return isOxcNode(declaration)\n ? collectTopLevelBindings(declaration)\n : bindings;\n }\n\n return bindings;\n};\n\nexport const collectTopLevelStatementInfos = (\n program: Program\n): TopLevelStatementInfo[] =>\n program.body.map((statement) => ({\n bindings: collectTopLevelBindings(statement),\n node: statement,\n references: collectReferencedNames(statement),\n }));\n\nexport const collectTopLevelBindingsFromStatements = (\n statements: TopLevelStatementInfo[]\n): Set<string> =>\n new Set(statements.flatMap((statement) => [...statement.bindings]));\n\nexport const collectRemovableNamesFromStatements = (\n statements: TopLevelStatementInfo[],\n initialNames: Set<string>\n): Set<string> => {\n const removable = new Set(initialNames);\n const bindingToStatement = new Map<string, TopLevelStatementInfo>();\n\n statements.forEach((statement) => {\n statement.bindings.forEach((name) => {\n bindingToStatement.set(name, statement);\n });\n });\n\n const queue = [...removable];\n while (queue.length > 0) {\n const name = queue.shift()!;\n const statement = bindingToStatement.get(name);\n if (statement) {\n statement.references.forEach((reference) => {\n if (bindingToStatement.has(reference) && !removable.has(reference)) {\n removable.add(reference);\n queue.push(reference);\n }\n });\n }\n }\n\n return removable;\n};\n"],"file":"cleanupBindings.js"}
@@ -0,0 +1,431 @@
1
+ import { getOxcNodeChildren, isOxcNode } from "../oxc/ast.js";
2
+ import { applyOxcReplacements } from "../oxc/replacements.js";
3
+ import { collectDeclaredNames, collectImportLocalNames, collectReferencedNames, collectRemovableNamesFromStatements, collectTopLevelBindings, collectTopLevelBindingsFromStatements, collectTopLevelStatementInfos, getImportSpecifierLocalName, isNodeReference } from "./cleanupBindings.js";
4
+ import { GENERATED_HELPER_NAME_RE, parseOxc } from "./shared.js";
5
+ export const createScopedCleanupScope = (parent) => ({
6
+ bindings: new Map(),
7
+ parent
8
+ });
9
+ export const resolveScopedBinding = (scope, name) => {
10
+ let current = scope;
11
+ while (current) {
12
+ const bindingId = current.bindings.get(name);
13
+ if (bindingId) {
14
+ return bindingId;
15
+ }
16
+ current = current.parent;
17
+ }
18
+ return null;
19
+ };
20
+ export const collectScopedBindingInfos = (program) => {
21
+ const bindings = new Map();
22
+ let sequence = 0;
23
+ const addBinding = (scope, name, kind, declaration) => {
24
+ const id = `${kind}:${name}:${declaration.start}:${sequence}`;
25
+ sequence += 1;
26
+ bindings.set(id, {
27
+ declaration,
28
+ dependencies: new Set(),
29
+ externalReferences: 0,
30
+ id,
31
+ incomingFromBindings: new Set(),
32
+ kind,
33
+ name
34
+ });
35
+ scope.bindings.set(name, id);
36
+ return id;
37
+ };
38
+ const addPatternBindings = (scope, pattern, kind, declaration) => {
39
+ collectDeclaredNames(pattern).forEach((name) => {
40
+ addBinding(scope, name, kind, declaration);
41
+ });
42
+ };
43
+ const recordReference = (scope, name, ownerBindingId) => {
44
+ const targetId = resolveScopedBinding(scope, name);
45
+ if (!targetId) {
46
+ return;
47
+ }
48
+ const target = bindings.get(targetId);
49
+ if (!target) {
50
+ return;
51
+ }
52
+ if (ownerBindingId && ownerBindingId !== targetId) {
53
+ target.incomingFromBindings.add(ownerBindingId);
54
+ bindings.get(ownerBindingId)?.dependencies.add(targetId);
55
+ return;
56
+ }
57
+ target.externalReferences += 1;
58
+ };
59
+ let walk;
60
+ function walkPatternReferenceSubexpressions(node, scope, ownerBindingId) {
61
+ if (node.type === "Identifier") {
62
+ return;
63
+ }
64
+ if (node.type === "TSParameterProperty") {
65
+ walkPatternReferenceSubexpressions(node.parameter, scope, ownerBindingId);
66
+ return;
67
+ }
68
+ if (node.type === "RestElement") {
69
+ walkPatternReferenceSubexpressions(node.argument, scope, ownerBindingId);
70
+ return;
71
+ }
72
+ if (node.type === "AssignmentPattern") {
73
+ walkPatternReferenceSubexpressions(node.left, scope, ownerBindingId);
74
+ walk(node.right, scope, node, ownerBindingId);
75
+ return;
76
+ }
77
+ if (node.type === "ObjectPattern") {
78
+ node.properties.forEach((property) => {
79
+ if (property.type === "RestElement") {
80
+ walkPatternReferenceSubexpressions(property.argument, scope, ownerBindingId);
81
+ return;
82
+ }
83
+ if (property.computed && isOxcNode(property.key)) {
84
+ walk(property.key, scope, property, ownerBindingId);
85
+ }
86
+ walkPatternReferenceSubexpressions(property.value, scope, ownerBindingId);
87
+ });
88
+ return;
89
+ }
90
+ if (node.type === "ArrayPattern") {
91
+ node.elements.forEach((element) => {
92
+ if (element && isOxcNode(element)) {
93
+ walkPatternReferenceSubexpressions(element, scope, ownerBindingId);
94
+ }
95
+ });
96
+ }
97
+ }
98
+ walk = (node, scope, parent = null, ownerBindingId = null) => {
99
+ if (node.type === "ImportDeclaration") {
100
+ const { specifiers } = node;
101
+ if (Array.isArray(specifiers)) {
102
+ specifiers.forEach((specifier) => {
103
+ const { local } = specifier;
104
+ if (isOxcNode(local) && local.type === "Identifier" && typeof local.name === "string") {
105
+ addBinding(scope, local.name, "import", node);
106
+ }
107
+ });
108
+ }
109
+ return;
110
+ }
111
+ if (node.type === "ExportNamedDeclaration" && node.declaration) {
112
+ walk(node.declaration, scope, node, ownerBindingId);
113
+ collectTopLevelBindings(node).forEach((name) => {
114
+ recordReference(scope, name, ownerBindingId);
115
+ });
116
+ return;
117
+ }
118
+ if (node.type === "ExportDefaultDeclaration") {
119
+ const { declaration } = node;
120
+ if (isOxcNode(declaration)) {
121
+ walk(declaration, scope, node, ownerBindingId);
122
+ if ((declaration.type === "FunctionDeclaration" || declaration.type === "ClassDeclaration") && declaration.id) {
123
+ recordReference(scope, declaration.id.name, ownerBindingId);
124
+ }
125
+ }
126
+ return;
127
+ }
128
+ if (node.type === "VariableDeclaration") {
129
+ const { declarations } = node;
130
+ if (!Array.isArray(declarations)) {
131
+ return;
132
+ }
133
+ declarations.forEach((declarator) => {
134
+ const { id } = declarator;
135
+ if (isOxcNode(id)) {
136
+ addPatternBindings(scope, id, "variable", node);
137
+ }
138
+ });
139
+ declarations.forEach((declarator) => {
140
+ const { id } = declarator;
141
+ const { init } = declarator;
142
+ if (!isOxcNode(id) || !isOxcNode(init)) {
143
+ return;
144
+ }
145
+ const ownerName = collectDeclaredNames(id)[0] ?? null;
146
+ const nextOwner = ownerName !== null ? resolveScopedBinding(scope, ownerName) : null;
147
+ walk(init, scope, declarator, nextOwner);
148
+ });
149
+ return;
150
+ }
151
+ if (node.type === "FunctionDeclaration" && node.id) {
152
+ const functionBindingId = addBinding(scope, node.id.name, "function", node);
153
+ const fnScope = createScopedCleanupScope(scope);
154
+ node.params.forEach((param) => {
155
+ addPatternBindings(fnScope, param, "param", param);
156
+ walkPatternReferenceSubexpressions(param, fnScope, functionBindingId);
157
+ });
158
+ if (node.body) {
159
+ walk(node.body, fnScope, node, functionBindingId);
160
+ }
161
+ return;
162
+ }
163
+ if (node.type === "FunctionExpression" || node.type === "ArrowFunctionExpression") {
164
+ const fnScope = createScopedCleanupScope(scope);
165
+ if (node.type === "FunctionExpression" && node.id) {
166
+ addBinding(fnScope, node.id.name, "function", node);
167
+ }
168
+ node.params.forEach((param) => {
169
+ addPatternBindings(fnScope, param, "param", param);
170
+ walkPatternReferenceSubexpressions(param, fnScope, ownerBindingId);
171
+ });
172
+ if (node.body) {
173
+ walk(node.body, fnScope, node, ownerBindingId);
174
+ }
175
+ return;
176
+ }
177
+ if (node.type === "BlockStatement") {
178
+ const blockScope = createScopedCleanupScope(scope);
179
+ getOxcNodeChildren(node).forEach((child) => walk(child, blockScope, node, ownerBindingId));
180
+ return;
181
+ }
182
+ if (isNodeReference(node, parent) && "name" in node && typeof node.name === "string") {
183
+ recordReference(scope, node.name, ownerBindingId);
184
+ }
185
+ getOxcNodeChildren(node).forEach((child) => walk(child, scope, node, ownerBindingId));
186
+ };
187
+ walk(program, createScopedCleanupScope(null));
188
+ return bindings;
189
+ };
190
+ export const collectScopedRemovableBindingIds = (bindings, initialNames) => {
191
+ const removable = new Set();
192
+ let changed = true;
193
+ while (changed) {
194
+ changed = false;
195
+ for (const binding of bindings.values()) {
196
+ if (!removable.has(binding.id) && binding.kind !== "import" && binding.kind !== "param" && binding.externalReferences === 0) {
197
+ const seededByName = initialNames.has(binding.name) || GENERATED_HELPER_NAME_RE.test(binding.name) && binding.incomingFromBindings.size === 0;
198
+ const allIncomingRemovable = binding.incomingFromBindings.size > 0 && [...binding.incomingFromBindings].every((sourceId) => removable.has(sourceId));
199
+ if (seededByName && binding.incomingFromBindings.size === 0 || allIncomingRemovable) {
200
+ removable.add(binding.id);
201
+ changed = true;
202
+ }
203
+ }
204
+ }
205
+ }
206
+ return removable;
207
+ };
208
+ export function expandImportRemovalRange(code, start, end) {
209
+ let removalStart = start;
210
+ while (removalStart > 0 && (code[removalStart - 1] === " " || code[removalStart - 1] === " ")) {
211
+ removalStart -= 1;
212
+ }
213
+ let removalEnd = end;
214
+ if (code[removalEnd] === ";") {
215
+ removalEnd += 1;
216
+ }
217
+ while (removalEnd < code.length && (code[removalEnd] === " " || code[removalEnd] === " ")) {
218
+ removalEnd += 1;
219
+ }
220
+ if (code[removalEnd] === "\r" && code[removalEnd + 1] === "\n") {
221
+ removalEnd += 2;
222
+ } else if (code[removalEnd] === "\n") {
223
+ removalEnd += 1;
224
+ }
225
+ return {
226
+ end: removalEnd,
227
+ start: removalStart,
228
+ value: ""
229
+ };
230
+ }
231
+ export const collectUnusedScopedDeclarationRemovals = (code, bindings, initialRemovableNames) => {
232
+ const removableBindingIds = collectScopedRemovableBindingIds(bindings, initialRemovableNames);
233
+ const removals = new Map();
234
+ bindings.forEach((binding) => {
235
+ if (!removableBindingIds.has(binding.id) || binding.kind === "import" || binding.kind === "param" || binding.externalReferences > 0 || [...binding.incomingFromBindings].some((sourceId) => !removableBindingIds.has(sourceId))) {
236
+ return;
237
+ }
238
+ if (binding.kind === "function" && binding.declaration.type === "FunctionDeclaration") {
239
+ const range = expandImportRemovalRange(code, binding.declaration.start, binding.declaration.end);
240
+ removals.set(`${range.start}:${range.end}`, range);
241
+ return;
242
+ }
243
+ if (binding.declaration.type !== "VariableDeclaration") {
244
+ return;
245
+ }
246
+ const { declarations } = binding.declaration;
247
+ if (!Array.isArray(declarations) || declarations.length !== 1) {
248
+ return;
249
+ }
250
+ const range = expandImportRemovalRange(code, binding.declaration.start, binding.declaration.end);
251
+ removals.set(`${range.start}:${range.end}`, range);
252
+ });
253
+ return [...removals.values()];
254
+ };
255
+ export const expandImportSpecifierRemovalRange = (code, start, end) => {
256
+ let removalStart = start;
257
+ let removalEnd = end;
258
+ let whitespaceStart = removalStart;
259
+ while (whitespaceStart > 0 && (code[whitespaceStart - 1] === " " || code[whitespaceStart - 1] === " ")) {
260
+ whitespaceStart -= 1;
261
+ }
262
+ if (code[whitespaceStart - 1] !== "{") {
263
+ removalStart = whitespaceStart;
264
+ }
265
+ while (removalEnd < code.length && (code[removalEnd] === " " || code[removalEnd] === " ")) {
266
+ removalEnd += 1;
267
+ }
268
+ if (code[removalEnd] === ",") {
269
+ removalEnd += 1;
270
+ while (removalEnd < code.length && (code[removalEnd] === " " || code[removalEnd] === " ")) {
271
+ removalEnd += 1;
272
+ }
273
+ } else {
274
+ while (removalStart > 0 && (code[removalStart - 1] === " " || code[removalStart - 1] === " ")) {
275
+ removalStart -= 1;
276
+ }
277
+ if (code[removalStart - 1] === ",") {
278
+ removalStart -= 1;
279
+ while (removalStart > 0 && (code[removalStart - 1] === " " || code[removalStart - 1] === " ")) {
280
+ removalStart -= 1;
281
+ }
282
+ }
283
+ }
284
+ return {
285
+ end: removalEnd,
286
+ start: removalStart,
287
+ value: ""
288
+ };
289
+ };
290
+ export const mergeEmptyRemovalRanges = (removals) => {
291
+ if (removals.length <= 1) {
292
+ return removals;
293
+ }
294
+ const sorted = [...removals].sort((a, b) => a.start - b.start);
295
+ const merged = [];
296
+ sorted.forEach((removal) => {
297
+ const previous = merged[merged.length - 1];
298
+ if (previous && previous.value === "" && removal.value === "" && removal.start <= previous.end) {
299
+ previous.end = Math.max(previous.end, removal.end);
300
+ return;
301
+ }
302
+ merged.push({ ...removal });
303
+ });
304
+ return merged;
305
+ };
306
+ export const collectUnusedImportRemovals = (code, program, referencedNames, removableNames, preserveSideEffectImportLocals) => {
307
+ const removals = [];
308
+ program.body.forEach((statement) => {
309
+ if (statement.type !== "ImportDeclaration") {
310
+ return;
311
+ }
312
+ const localNames = collectImportLocalNames(statement);
313
+ const removableLocalNames = localNames.filter((localName) => removableNames.has(localName));
314
+ if (removableLocalNames.length > 0 && removableLocalNames.length === localNames.length && removableLocalNames.every((localName) => !referencedNames.has(localName))) {
315
+ if (removableLocalNames.some((localName) => preserveSideEffectImportLocals.has(localName))) {
316
+ removals.push({
317
+ end: statement.end,
318
+ start: statement.start,
319
+ value: `import ${code.slice(statement.source.start, statement.source.end)};`
320
+ });
321
+ return;
322
+ }
323
+ removals.push(expandImportRemovalRange(code, statement.start, statement.end));
324
+ return;
325
+ }
326
+ const { specifiers } = statement;
327
+ if (!Array.isArray(specifiers) || specifiers.length <= 1) {
328
+ return;
329
+ }
330
+ specifiers.forEach((specifier) => {
331
+ if (!isOxcNode(specifier)) {
332
+ return;
333
+ }
334
+ const localName = getImportSpecifierLocalName(specifier);
335
+ if (localName && removableNames.has(localName) && !referencedNames.has(localName)) {
336
+ removals.push(expandImportSpecifierRemovalRange(code, specifier.start, specifier.end));
337
+ }
338
+ });
339
+ });
340
+ return removals;
341
+ };
342
+ export const collectUnusedTopLevelDeclarationRemovals = (code, program, referencedNames, removableNames) => {
343
+ const removals = [];
344
+ program.body.forEach((statement) => {
345
+ if (statement.type !== "VariableDeclaration") {
346
+ return;
347
+ }
348
+ const localNames = [...collectTopLevelBindings(statement)];
349
+ if (localNames.length > 0 && localNames.every((localName) => removableNames.has(localName)) && localNames.every((localName) => !referencedNames.has(localName))) {
350
+ removals.push(expandImportRemovalRange(code, statement.start, statement.end));
351
+ }
352
+ });
353
+ return removals;
354
+ };
355
+ export const collectUnusedGeneratedHelperDeclarationRemovals = (code, program, referencedNames) => {
356
+ const removals = [];
357
+ program.body.forEach((statement) => {
358
+ if (statement.type !== "VariableDeclaration") {
359
+ return;
360
+ }
361
+ const localNames = [...collectTopLevelBindings(statement)];
362
+ if (localNames.length > 0 && localNames.every((localName) => GENERATED_HELPER_NAME_RE.test(localName)) && localNames.every((localName) => !referencedNames.has(localName))) {
363
+ removals.push(expandImportRemovalRange(code, statement.start, statement.end));
364
+ }
365
+ });
366
+ return removals;
367
+ };
368
+ export const collectTopLevelExpressionStatementRemovals = (code, statements, topLevelBindings, removableExpressionRefs) => {
369
+ const removals = [];
370
+ statements.forEach((statement) => {
371
+ if (statement.node.type !== "ExpressionStatement") {
372
+ return;
373
+ }
374
+ const { expression } = statement.node;
375
+ const isPureExpression = expression.type === "Identifier" || expression.type === "Literal" || expression.type === "ObjectExpression" || expression.type === "ArrayExpression" || expression.type === "ArrowFunctionExpression" || expression.type === "FunctionExpression" || expression.type === "TemplateLiteral" && expression.expressions.length === 0;
376
+ if (!isPureExpression) {
377
+ return;
378
+ }
379
+ const localReferences = [...statement.references].filter((name) => topLevelBindings.has(name));
380
+ if (localReferences.length > 0 && localReferences.every((name) => removableExpressionRefs.has(name))) {
381
+ removals.push(expandImportRemovalRange(code, statement.node.start, statement.node.end));
382
+ }
383
+ });
384
+ return removals;
385
+ };
386
+ export const collectEmptyTopLevelBlockRemovals = (code, program) => {
387
+ const removals = [];
388
+ program.body.forEach((statement) => {
389
+ if (statement.type !== "BlockStatement" || statement.body.length > 0) {
390
+ return;
391
+ }
392
+ removals.push(expandImportRemovalRange(code, statement.start, statement.end));
393
+ });
394
+ return removals;
395
+ };
396
+ export const removeUnusedAfterReplacement = (code, filename, initialRemovableNames, removableExpressionRefs, preserveSideEffectImportLocals) => {
397
+ let current = code;
398
+ const cumulativeRemovableNames = new Set(initialRemovableNames);
399
+ const applyIfParsable = (next) => {
400
+ try {
401
+ parseOxc(next, filename);
402
+ return next;
403
+ } catch {
404
+ return current;
405
+ }
406
+ };
407
+ for (let idx = 0; idx < 5; idx += 1) {
408
+ const previous = current;
409
+ const program = parseOxc(current, filename);
410
+ const statements = collectTopLevelStatementInfos(program);
411
+ const removableNames = collectRemovableNamesFromStatements(statements, cumulativeRemovableNames);
412
+ removableNames.forEach((name) => cumulativeRemovableNames.add(name));
413
+ const referencedNames = collectReferencedNames(program);
414
+ const topLevelBindings = collectTopLevelBindingsFromStatements(statements);
415
+ const scopedBindings = collectScopedBindingInfos(program);
416
+ const removals = mergeEmptyRemovalRanges([
417
+ ...collectUnusedScopedDeclarationRemovals(current, scopedBindings, cumulativeRemovableNames),
418
+ ...collectUnusedTopLevelDeclarationRemovals(current, program, referencedNames, cumulativeRemovableNames),
419
+ ...collectUnusedGeneratedHelperDeclarationRemovals(current, program, referencedNames),
420
+ ...collectUnusedImportRemovals(current, program, referencedNames, cumulativeRemovableNames, preserveSideEffectImportLocals),
421
+ ...collectTopLevelExpressionStatementRemovals(current, statements, topLevelBindings, removableExpressionRefs),
422
+ ...collectEmptyTopLevelBlockRemovals(current, program)
423
+ ]);
424
+ current = removals.length > 0 ? applyIfParsable(applyOxcReplacements(current, removals)) : current;
425
+ if (current === previous) {
426
+ return current;
427
+ }
428
+ }
429
+ return current;
430
+ };
431
+ //# sourceMappingURL=cleanupRemovals.js.map