@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,113 @@
1
+ import { getOxcNodeChildren } from "../oxc/ast.js";
2
+ import { isBindingPosition, isInTypeContext, isObjectPropertyKey, isPropertyOnlyIdentifier, resolveBindingAt } from "./scopeAnalysis.js";
3
+ import { evaluateStatic, literalCode } from "./staticEvaluator.js";
4
+ export const getConstantReplacement = (binding, ctx) => {
5
+ const init = binding?.declarator?.init;
6
+ if (!init) {
7
+ return null;
8
+ }
9
+ if (init.type === "Literal") {
10
+ return literalCode(init.value);
11
+ }
12
+ if (init.type === "ObjectExpression" && binding?.isRoot && binding.declarator?.id.type === "Identifier") {
13
+ const evaluated = evaluateStatic(binding.declarator.id, ctx);
14
+ return literalCode(evaluated);
15
+ }
16
+ return null;
17
+ };
18
+ export const collectIdentifierReferenceReplacements = (expression, replacements) => {
19
+ const localReplacements = [];
20
+ const ancestors = [];
21
+ const walk = (current, parent) => {
22
+ if (current.type === "Identifier" && replacements.has(current.name) && !isInTypeContext(ancestors) && !isBindingPosition(current, parent) && !isPropertyOnlyIdentifier(current, parent) && !isObjectPropertyKey(current, parent)) {
23
+ const replacement = replacements.get(current.name);
24
+ // Shorthand property `{ width }` → `{ width: 500 }` when the
25
+ // identifier is the value side of a shorthand ObjectProperty.
26
+ const isShorthandValue = !!parent && parent.type === "Property" && parent.shorthand && parent.value === current;
27
+ localReplacements.push({
28
+ start: isShorthandValue ? parent.start : current.start,
29
+ end: current.end,
30
+ value: isShorthandValue ? `${current.name}: ${replacement}` : replacement
31
+ });
32
+ }
33
+ ancestors.push(current);
34
+ getOxcNodeChildren(current).forEach((child) => walk(child, current));
35
+ ancestors.pop();
36
+ };
37
+ walk(expression, null);
38
+ return localReplacements;
39
+ };
40
+ export const applyExpressionReplacements = (expression, replacements, code) => {
41
+ let result = code.slice(expression.start, expression.end);
42
+ replacements.sort((a, b) => b.start - a.start).forEach((replacement) => {
43
+ const start = replacement.start - expression.start;
44
+ const end = replacement.end - expression.start;
45
+ result = result.slice(0, start) + replacement.value + result.slice(end);
46
+ });
47
+ return result;
48
+ };
49
+ export const replaceIdentifierReferences = (expression, replacements, code) => {
50
+ return applyExpressionReplacements(expression, collectIdentifierReferenceReplacements(expression, replacements), code);
51
+ };
52
+ const staticImportAliasPart = (value) => value.replace(/[^A-Za-z0-9_$]/g, "_") || "value";
53
+ const allocateStaticImportAlias = (binding, imported, ctx) => {
54
+ const key = `${binding.importedFrom ?? ""}\0${binding.name}\0${imported}`;
55
+ const existing = ctx.staticImportAliases.get(key);
56
+ if (existing) {
57
+ return existing;
58
+ }
59
+ const namespacePart = staticImportAliasPart(binding.name);
60
+ const importedPart = staticImportAliasPart(imported);
61
+ let alias = `_wyw_static_${namespacePart}_${importedPart}`;
62
+ let idx = 1;
63
+ while (ctx.usedNames.has(alias)) {
64
+ idx += 1;
65
+ alias = `_wyw_static_${namespacePart}_${importedPart}_${idx}`;
66
+ }
67
+ ctx.usedNames.add(alias);
68
+ ctx.staticImportAliases.set(key, alias);
69
+ return alias;
70
+ };
71
+ const staticMemberPropertyName = (expression) => {
72
+ if (!expression.computed && expression.property.type === "Identifier") {
73
+ return expression.property.name;
74
+ }
75
+ if (expression.computed && expression.property.type === "Literal" && typeof expression.property.value === "string") {
76
+ return expression.property.value;
77
+ }
78
+ return null;
79
+ };
80
+ export const collectStaticNamespaceMemberReferences = (expression, ctx) => {
81
+ const coveredReferenceStarts = new Set();
82
+ const imports = new Map();
83
+ const replacements = [];
84
+ const walk = (node) => {
85
+ if (node.type === "MemberExpression" && node.object.type === "Identifier") {
86
+ const binding = resolveBindingAt(ctx, node.object.name, node.object.start);
87
+ const imported = staticMemberPropertyName(node);
88
+ if (binding?.importedFrom && binding.imported === "*" && imported !== null) {
89
+ const alias = allocateStaticImportAlias(binding, imported, ctx);
90
+ imports.set(`${binding.importedFrom}\0${imported}\0${alias}`, {
91
+ imported,
92
+ importLocal: binding.name,
93
+ local: alias,
94
+ source: binding.importedFrom
95
+ });
96
+ replacements.push({
97
+ end: node.end,
98
+ start: node.start,
99
+ value: alias
100
+ });
101
+ coveredReferenceStarts.add(node.object.start);
102
+ }
103
+ }
104
+ getOxcNodeChildren(node).forEach(walk);
105
+ };
106
+ walk(expression);
107
+ return {
108
+ coveredReferenceStarts,
109
+ imports: [...imports.values()],
110
+ replacements
111
+ };
112
+ };
113
+ //# sourceMappingURL=expressionReplacements.js.map
@@ -0,0 +1 @@
1
+ {"mappings":"AAIA,SAAS,0BAA0B;AACnC,SACE,mBACA,iBACA,qBACA,0BACA,wBACK;AACP,SAAS,gBAAgB,mBAAmB;AAQ5C,OAAO,MAAM,0BACX,SACA,QACkB;CAClB,MAAM,OAAO,SAAS,YAAY;AAClC,KAAI,CAAC,MAAM;AACT,SAAO;;AAGT,KAAI,KAAK,SAAS,WAAW;AAC3B,SAAO,YAAY,KAAK,MAAM;;AAGhC,KACE,KAAK,SAAS,sBACd,SAAS,UACT,QAAQ,YAAY,GAAG,SAAS,cAChC;EACA,MAAM,YAAY,eAAe,QAAQ,WAAW,IAAI,IAAI;AAC5D,SAAO,YAAY,UAAU;;AAG/B,QAAO;;AAGT,OAAO,MAAM,0CACX,YACA,iBACkB;CAClB,MAAM,oBAAmC,EAAE;CAC3C,MAAM,YAAoB,EAAE;CAE5B,MAAM,QAAQ,SAAe,WAAwB;AACnD,MACE,QAAQ,SAAS,gBACjB,aAAa,IAAI,QAAQ,KAAK,IAC9B,CAAC,gBAAgB,UAAU,IAC3B,CAAC,kBAAkB,SAAS,OAAO,IACnC,CAAC,yBAAyB,SAAS,OAAO,IAC1C,CAAC,oBAAoB,SAAS,OAAO,EACrC;GACA,MAAM,cAAc,aAAa,IAAI,QAAQ,KAAK;;;GAGlD,MAAM,mBACJ,CAAC,CAAC,UACF,OAAO,SAAS,cACf,OAA8C,aAC/C,OAAO,UAAU;AACnB,qBAAkB,K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KAAK,MAAM;AACvC,QAAO;;AAGT,MAAM,4BACJ,eACkB;AAClB,KAAI,CAAC,WAAW,YAAY,WAAW,SAAS,SAAS,cAAc;AACrE,SAAO,WAAW,SAAS;;AAG7B,KACE,WAAW,YACX,WAAW,SAAS,SAAS,aAC7B,OAAO,WAAW,SAAS,UAAU,UACrC;AACA,SAAO,WAAW,SAAS;;AAG7B,QAAO;;AAGT,OAAO,MAAM,0CACX,YACA,QAKG;CACH,MAAM,yBAAyB,IAAI,KAAa;CAChD,MAAM,UAAU,IAAI,KAAuC;CAC3D,MAAM,eAA8B,EAAE;CAEtC,MAAM,QAAQ,SAAqB;AACjC,MAAI,KAAK,SAAS,sBAAsB,KAAK,OAAO,SAAS,cAAc;GACzE,MAAM,UAAU,iBACd,KACA,KAAK,OAAO,MACZ,KAAK,OAAO,MACb;GACD,MAAM,WAAW,yBAAyB,KAAK;AAC/C,OACE,SAAS,gBACT,QAAQ,aAAa,OACrB,aAAa,MACb;IACA,MAAM,QAAQ,0BAA0B,SAAS,UAAU,IAAI;AAC/D,YAAQ,IAAI,GAAG,QAAQ,aAAa,IAAI,SAAS,IAAI,SAAS;KAC5D;KACA,aAAa,QAAQ;KACrB,OAAO;KACP,QAAQ,QAAQ;KACjB,CAAC;AACF,iBAAa,KAAK;KAChB,KAAK,KAAK;KACV,OAAO,KAAK;KACZ,OAAO;KACR,CAAC;AACF,2BAAuB,IAAI,KAAK,OAAO,MAAM;;;AAIjD,qBAAmB,KAAK,CAAC,QAAQ,KAAK;;AAGxC,MAAK,WAAW;AAEhB,QAAO;EACL;EACA,SAAS,CAAC,GAAG,QAAQ,QAAQ,CAAC;EAC9B;EACD","names":[],"sources":["../../../src/utils/collectOxcTemplateDependencies/expressionReplacements.ts"],"version":3,"sourcesContent":["/* eslint-disable no-restricted-syntax */\n\nimport type { Expression, MemberExpression, Node } from 'oxc-parser';\n\nimport { getOxcNodeChildren } from '../oxc/ast';\nimport {\n isBindingPosition,\n isInTypeContext,\n isObjectPropertyKey,\n isPropertyOnlyIdentifier,\n resolveBindingAt,\n} from './scopeAnalysis';\nimport { evaluateStatic, literalCode } from './staticEvaluator';\nimport type {\n Binding,\n ExtractionContext,\n OxcStaticImportReference,\n Replacement,\n} from './types';\n\nexport const getConstantReplacement = (\n binding: Binding | undefined,\n ctx: ExtractionContext\n): string | null => {\n const init = binding?.declarator?.init;\n if (!init) {\n return null;\n }\n\n if (init.type === 'Literal') {\n return literalCode(init.value);\n }\n\n if (\n init.type === 'ObjectExpression' &&\n binding?.isRoot &&\n binding.declarator?.id.type === 'Identifier'\n ) {\n const evaluated = evaluateStatic(binding.declarator.id, ctx);\n return literalCode(evaluated);\n }\n\n return null;\n};\n\nexport const collectIdentifierReferenceReplacements = (\n expression: Expression,\n replacements: Map<string, string>\n): Replacement[] => {\n const localReplacements: Replacement[] = [];\n const ancestors: Node[] = [];\n\n const walk = (current: Node, parent: Node | null) => {\n if (\n current.type === 'Identifier' &&\n replacements.has(current.name) &&\n !isInTypeContext(ancestors) &&\n !isBindingPosition(current, parent) &&\n !isPropertyOnlyIdentifier(current, parent) &&\n !isObjectPropertyKey(current, parent)\n ) {\n const replacement = replacements.get(current.name)!;\n // Shorthand property `{ width }` → `{ width: 500 }` when the\n // identifier is the value side of a shorthand ObjectProperty.\n const isShorthandValue =\n !!parent &&\n parent.type === 'Property' &&\n (parent as unknown as { shorthand?: boolean }).shorthand &&\n parent.value === current;\n localReplacements.push({\n start: isShorthandValue ? parent.start : current.start,\n end: current.end,\n value: isShorthandValue\n ? `${current.name}: ${replacement}`\n : replacement,\n });\n }\n\n ancestors.push(current);\n getOxcNodeChildren(current).forEach((child) => walk(child, current));\n ancestors.pop();\n };\n\n walk(expression, null);\n return localReplacements;\n};\n\nexport const applyExpressionReplacements = (\n expression: Expression,\n replacements: Replacement[],\n code: string\n): string => {\n let result = code.slice(expression.start, expression.end);\n replacements\n .sort((a, b) => b.start - a.start)\n .forEach((replacement) => {\n const start = replacement.start - expression.start;\n const end = replacement.end - expression.start;\n result = result.slice(0, start) + replacement.value + result.slice(end);\n });\n return result;\n};\n\nexport const replaceIdentifierReferences = (\n expression: Expression,\n replacements: Map<string, string>,\n code: string\n): string => {\n return applyExpressionReplacements(\n expression,\n collectIdentifierReferenceReplacements(expression, replacements),\n code\n );\n};\n\nconst staticImportAliasPart = (value: string): string =>\n value.replace(/[^A-Za-z0-9_$]/g, '_') || 'value';\n\nconst allocateStaticImportAlias = (\n binding: Binding,\n imported: string,\n ctx: ExtractionContext\n): string => {\n const key = `${binding.importedFrom ?? ''}\\0${binding.name}\\0${imported}`;\n const existing = ctx.staticImportAliases.get(key);\n if (existing) {\n return existing;\n }\n\n const namespacePart = staticImportAliasPart(binding.name);\n const importedPart = staticImportAliasPart(imported);\n let alias = `_wyw_static_${namespacePart}_${importedPart}`;\n let idx = 1;\n while (ctx.usedNames.has(alias)) {\n idx += 1;\n alias = `_wyw_static_${namespacePart}_${importedPart}_${idx}`;\n }\n\n ctx.usedNames.add(alias);\n ctx.staticImportAliases.set(key, alias);\n return alias;\n};\n\nconst staticMemberPropertyName = (\n expression: MemberExpression\n): string | null => {\n if (!expression.computed && expression.property.type === 'Identifier') {\n return expression.property.name;\n }\n\n if (\n expression.computed &&\n expression.property.type === 'Literal' &&\n typeof expression.property.value === 'string'\n ) {\n return expression.property.value;\n }\n\n return null;\n};\n\nexport const collectStaticNamespaceMemberReferences = (\n expression: Expression,\n ctx: ExtractionContext\n): {\n coveredReferenceStarts: Set<number>;\n imports: OxcStaticImportReference[];\n replacements: Replacement[];\n} => {\n const coveredReferenceStarts = new Set<number>();\n const imports = new Map<string, OxcStaticImportReference>();\n const replacements: Replacement[] = [];\n\n const walk = (node: Node): void => {\n if (node.type === 'MemberExpression' && node.object.type === 'Identifier') {\n const binding = resolveBindingAt(\n ctx,\n node.object.name,\n node.object.start\n );\n const imported = staticMemberPropertyName(node);\n if (\n binding?.importedFrom &&\n binding.imported === '*' &&\n imported !== null\n ) {\n const alias = allocateStaticImportAlias(binding, imported, ctx);\n imports.set(`${binding.importedFrom}\\0${imported}\\0${alias}`, {\n imported,\n importLocal: binding.name,\n local: alias,\n source: binding.importedFrom,\n });\n replacements.push({\n end: node.end,\n start: node.start,\n value: alias,\n });\n coveredReferenceStarts.add(node.object.start);\n }\n }\n\n getOxcNodeChildren(node).forEach(walk);\n };\n\n walk(expression);\n\n return {\n coveredReferenceStarts,\n imports: [...imports.values()],\n replacements,\n };\n};\n"],"file":"expressionReplacements.js"}
@@ -0,0 +1,387 @@
1
+ import { getOxcNodeChildren } from "../oxc/ast.js";
2
+ import { parseOxcProgram } from "../oxc/parse.js";
3
+ import { createOxcSourceLocation } from "../oxc/sourceLocations.js";
4
+ export const containsTaggedTemplateExpression = (node) => {
5
+ if (node.type === "TaggedTemplateExpression") {
6
+ return true;
7
+ }
8
+ return getOxcNodeChildren(node).some(containsTaggedTemplateExpression);
9
+ };
10
+ export const parseOxc = (code, filename) => {
11
+ return parseOxcProgram(code, filename, "unambiguous");
12
+ };
13
+ const toSpanKey = (start, end) => `${start}:${end}`;
14
+ export const createSpanLookup = (spans) => {
15
+ if (!spans || spans.length === 0) {
16
+ return null;
17
+ }
18
+ return new Set(spans.map((span) => toSpanKey(span.start, span.end)));
19
+ };
20
+ const matchesSpanLookup = (node, spanLookup) => !spanLookup || spanLookup.has(toSpanKey(node.start, node.end));
21
+ export const getSourceLocation = (start, end, ctx) => createOxcSourceLocation(start, end, ctx.loc, ctx.filename);
22
+ const createScope = (parent, node, root = false, functionBoundary = false) => ({
23
+ bindings: new Map(),
24
+ depth: parent ? parent.depth + 1 : 0,
25
+ end: node.end,
26
+ functionBoundary,
27
+ params: new Set(),
28
+ parent,
29
+ root,
30
+ start: node.start
31
+ });
32
+ const normalizeDeclarationKind = (declarationKind) => {
33
+ if (declarationKind === "var") {
34
+ return "var";
35
+ }
36
+ if (declarationKind === "let") {
37
+ return "let";
38
+ }
39
+ return "const";
40
+ };
41
+ const moduleExportName = (node) => node.type === "Literal" ? String(node.value) : node.name;
42
+ const getImportSpecifierInfo = (statement, specifier) => {
43
+ const local = specifier.local?.name;
44
+ if (!local) {
45
+ return null;
46
+ }
47
+ if (specifier.type === "ImportDefaultSpecifier") {
48
+ return {
49
+ imported: "default",
50
+ local
51
+ };
52
+ }
53
+ if (specifier.type === "ImportNamespaceSpecifier") {
54
+ return {
55
+ imported: "*",
56
+ local
57
+ };
58
+ }
59
+ if (statement.importKind === "type" || specifier.importKind === "type") {
60
+ return null;
61
+ }
62
+ return {
63
+ imported: moduleExportName(specifier.imported),
64
+ local
65
+ };
66
+ };
67
+ const getDeclarationScope = (scope, declarationKind) => {
68
+ if (declarationKind !== "var") {
69
+ return scope;
70
+ }
71
+ let current = scope;
72
+ while (current && !current.functionBoundary) {
73
+ current = current.parent;
74
+ }
75
+ return current ?? scope;
76
+ };
77
+ const collectBindingNames = (node) => {
78
+ if (node.type === "Identifier") {
79
+ return [node.name];
80
+ }
81
+ if (node.type === "RestElement") {
82
+ return collectBindingNames(node.argument);
83
+ }
84
+ if (node.type === "AssignmentPattern") {
85
+ return collectBindingNames(node.left);
86
+ }
87
+ if (node.type === "ObjectPattern") {
88
+ return node.properties.flatMap((property) => property.type === "RestElement" ? collectBindingNames(property.argument) : collectBindingNames(property.value));
89
+ }
90
+ if (node.type === "ArrayPattern") {
91
+ return node.elements.flatMap((element) => element ? collectBindingNames(element) : []);
92
+ }
93
+ if (node.type === "TSParameterProperty") {
94
+ return collectBindingNames(node.parameter);
95
+ }
96
+ return [];
97
+ };
98
+ export const isInTypeContext = (ancestors) => ancestors.some((ancestor) => ancestor.type.startsWith("TS") || ancestor.type.startsWith("JSDoc"));
99
+ export const isPropertyOnlyIdentifier = (node, parent) => !!parent && parent.type === "MemberExpression" && parent.property === node && !parent.computed;
100
+ export const isObjectPropertyKey = (node, parent) => !!parent && parent.type === "Property" && parent.key === node && !parent.computed && parent.value !== node;
101
+ export const isBindingPosition = (node, parent) => {
102
+ if (!parent) {
103
+ return false;
104
+ }
105
+ if (parent.type === "VariableDeclarator" && parent.id === node) {
106
+ return true;
107
+ }
108
+ if ((parent.type === "FunctionDeclaration" || parent.type === "FunctionExpression" || parent.type === "ClassDeclaration" || parent.type === "ClassExpression") && parent.id === node) {
109
+ return true;
110
+ }
111
+ if ((parent.type === "ImportSpecifier" || parent.type === "ImportDefaultSpecifier" || parent.type === "ImportNamespaceSpecifier") && "local" in parent && parent.local === node) {
112
+ return true;
113
+ }
114
+ return false;
115
+ };
116
+ const visit = (node, scope, enter, parent = null, ancestors = []) => {
117
+ const visitNode = (currentNode, currentScope, currentParent) => {
118
+ let nextScope;
119
+ if (currentNode.type === "Program") {
120
+ nextScope = createScope(null, currentNode, true, true);
121
+ } else if (currentNode.type === "BlockStatement" || currentNode.type === "FunctionDeclaration" || currentNode.type === "FunctionExpression" || currentNode.type === "ArrowFunctionExpression") {
122
+ nextScope = createScope(currentScope, currentNode, false, currentNode.type !== "BlockStatement");
123
+ } else if (currentScope) {
124
+ nextScope = currentScope;
125
+ } else {
126
+ nextScope = createScope(null, currentNode, false, true);
127
+ }
128
+ if (currentNode.type === "FunctionDeclaration" || currentNode.type === "FunctionExpression" || currentNode.type === "ArrowFunctionExpression") {
129
+ currentNode.params.forEach((param) => {
130
+ collectBindingNames(param).forEach((name) => {
131
+ nextScope.params.add(name);
132
+ nextScope.bindings.set(name, {
133
+ declaredAt: param.start,
134
+ declaration: null,
135
+ declarator: null,
136
+ functionNode: null,
137
+ isRoot: false,
138
+ kind: "param",
139
+ name,
140
+ scope: nextScope
141
+ });
142
+ });
143
+ });
144
+ }
145
+ enter(currentNode, nextScope, currentParent, ancestors);
146
+ ancestors.push(currentNode);
147
+ getOxcNodeChildren(currentNode).forEach((child) => visitNode(child, nextScope, currentNode));
148
+ ancestors.pop();
149
+ };
150
+ visitNode(node, scope, parent);
151
+ };
152
+ export const analyzeProgram = (program, { collectTargetExpressions = false, collectTemplateLiterals = false, expressionSpanLookup = null, templateSpanLookup = null } = {}) => {
153
+ const bindings = new Map();
154
+ const usedNames = new Set();
155
+ const templateLiterals = [];
156
+ const targetExpressions = [];
157
+ const addBinding = (scope, binding) => {
158
+ scope.bindings.set(binding.name, binding);
159
+ const existing = bindings.get(binding.name) ?? [];
160
+ existing.push(binding);
161
+ bindings.set(binding.name, existing);
162
+ };
163
+ const collectTargets = (node, ancestors) => {
164
+ if (collectTemplateLiterals && node.type === "TemplateLiteral" && node.expressions.length > 0 && !ancestors.some((ancestor) => ancestor.type === "TemplateLiteral") && matchesSpanLookup(node, templateSpanLookup)) {
165
+ templateLiterals.push(node);
166
+ }
167
+ if (collectTargetExpressions && expressionSpanLookup && matchesSpanLookup(node, expressionSpanLookup)) {
168
+ targetExpressions.push(node);
169
+ }
170
+ };
171
+ visit(program, null, (node, scope, _parent, ancestors) => {
172
+ collectTargets(node, ancestors);
173
+ if (node.type === "Identifier") {
174
+ usedNames.add(node.name);
175
+ }
176
+ if (isInTypeContext(ancestors)) {
177
+ return;
178
+ }
179
+ if (node.type === "FunctionDeclaration" || node.type === "FunctionExpression" || node.type === "ArrowFunctionExpression") {
180
+ node.params.forEach((param) => {
181
+ collectBindingNames(param).forEach((name) => {
182
+ const binding = scope.bindings.get(name);
183
+ if (binding) {
184
+ addBinding(scope, binding);
185
+ }
186
+ });
187
+ });
188
+ if (node.type !== "FunctionDeclaration") {
189
+ return;
190
+ }
191
+ }
192
+ if (node.type === "ImportDeclaration") {
193
+ const source = node.source.value;
194
+ node.specifiers.forEach((specifier) => {
195
+ const importInfo = getImportSpecifierInfo(node, specifier);
196
+ if (!importInfo) {
197
+ return;
198
+ }
199
+ addBinding(scope, {
200
+ declaredAt: specifier.start,
201
+ declaration: null,
202
+ declarator: null,
203
+ functionNode: null,
204
+ imported: importInfo.imported,
205
+ importedFrom: source,
206
+ isRoot: scope.root,
207
+ kind: "import",
208
+ name: importInfo.local,
209
+ scope
210
+ });
211
+ });
212
+ return;
213
+ }
214
+ if (node.type !== "VariableDeclaration") {
215
+ if (node.type === "FunctionDeclaration" && node.id) {
216
+ const declarationScope = scope.parent ?? scope;
217
+ const binding = {
218
+ declaredAt: node.start,
219
+ declaration: null,
220
+ declarator: null,
221
+ functionNode: node,
222
+ isRoot: declarationScope.root,
223
+ kind: "function",
224
+ name: node.id.name,
225
+ scope: declarationScope
226
+ };
227
+ addBinding(declarationScope, binding);
228
+ }
229
+ return;
230
+ }
231
+ node.declarations.forEach((declarator) => {
232
+ collectBindingNames(declarator.id).forEach((name) => {
233
+ const declarationKind = normalizeDeclarationKind(node.kind);
234
+ const declarationScope = getDeclarationScope(scope, declarationKind);
235
+ const binding = {
236
+ declarationKind,
237
+ declaredAt: declarator.start,
238
+ declaration: node,
239
+ declarator,
240
+ functionNode: null,
241
+ isRoot: declarationScope.root,
242
+ kind: "variable",
243
+ name,
244
+ scope: declarationScope
245
+ };
246
+ addBinding(declarationScope, binding);
247
+ });
248
+ });
249
+ });
250
+ return {
251
+ bindingsByName: bindings,
252
+ rootMutationsByBinding: collectRootMutations(program),
253
+ targetExpressions: targetExpressions.sort((a, b) => a.start - b.start),
254
+ templateLiterals,
255
+ usedNames
256
+ };
257
+ };
258
+ export const resolveBindingAt = (ctx, name, referenceStart) => {
259
+ const cachedBindings = ctx.bindingResolutionCache.get(name);
260
+ if (cachedBindings?.has(referenceStart)) {
261
+ return cachedBindings.get(referenceStart) ?? undefined;
262
+ }
263
+ const bindings = ctx.bindingsByName.get(name);
264
+ const bindingCache = cachedBindings ?? new Map();
265
+ if (!cachedBindings) {
266
+ ctx.bindingResolutionCache.set(name, bindingCache);
267
+ }
268
+ if (!bindings || bindings.length === 0) {
269
+ bindingCache.set(referenceStart, null);
270
+ return undefined;
271
+ }
272
+ let binding;
273
+ bindings.forEach((candidate) => {
274
+ if (candidate.scope.start > referenceStart || referenceStart >= candidate.scope.end) {
275
+ return;
276
+ }
277
+ if (!binding || candidate.scope.depth > binding.scope.depth || candidate.scope.depth === binding.scope.depth && candidate.declaredAt > binding.declaredAt) {
278
+ binding = candidate;
279
+ }
280
+ });
281
+ bindingCache.set(referenceStart, binding ?? null);
282
+ return binding;
283
+ };
284
+ const collectRootMutations = (program) => {
285
+ const mutations = new Map();
286
+ const getRootMutationTarget = (node) => {
287
+ if (node.type === "Identifier") {
288
+ return {
289
+ binding: node.name,
290
+ path: []
291
+ };
292
+ }
293
+ if (node.type !== "MemberExpression") {
294
+ return null;
295
+ }
296
+ const parent = getRootMutationTarget(node.object);
297
+ if (!parent) {
298
+ return null;
299
+ }
300
+ let key = null;
301
+ if (node.computed && node.property.type === "Literal" && (typeof node.property.value === "string" || typeof node.property.value === "number")) {
302
+ key = node.property.value;
303
+ } else if (!node.computed && node.property.type === "Identifier") {
304
+ key = node.property.name;
305
+ }
306
+ if (key === null) {
307
+ return null;
308
+ }
309
+ return {
310
+ binding: parent.binding,
311
+ path: [...parent.path, key]
312
+ };
313
+ };
314
+ program.body.forEach((statement) => {
315
+ if (statement.type !== "ExpressionStatement") {
316
+ return;
317
+ }
318
+ const { expression } = statement;
319
+ if (expression.type === "AssignmentExpression") {
320
+ const target = getRootMutationTarget(expression.left);
321
+ if (!target || target.path.length === 0) {
322
+ return;
323
+ }
324
+ const bucket = mutations.get(target.binding) ?? [];
325
+ bucket.push(expression);
326
+ mutations.set(target.binding, bucket);
327
+ return;
328
+ }
329
+ if (expression.type === "UpdateExpression") {
330
+ const target = getRootMutationTarget(expression.argument);
331
+ if (!target || target.path.length === 0) {
332
+ return;
333
+ }
334
+ const bucket = mutations.get(target.binding) ?? [];
335
+ bucket.push(expression);
336
+ mutations.set(target.binding, bucket);
337
+ }
338
+ });
339
+ return mutations;
340
+ };
341
+ const hasLocalBinding = (scope, name) => {
342
+ let current = scope;
343
+ while (current) {
344
+ if (current.bindings.has(name)) {
345
+ return true;
346
+ }
347
+ current = current.parent;
348
+ }
349
+ return false;
350
+ };
351
+ const hasLocalBindingCached = (scope, name, cache) => {
352
+ const scopeCache = cache.get(scope);
353
+ if (scopeCache?.has(name)) {
354
+ return scopeCache.get(name);
355
+ }
356
+ const result = hasLocalBinding(scope, name);
357
+ const nextScopeCache = scopeCache ?? new Map();
358
+ nextScopeCache.set(name, result);
359
+ if (!scopeCache) {
360
+ cache.set(scope, nextScopeCache);
361
+ }
362
+ return result;
363
+ };
364
+ export const findReferences = (node, referenceCache) => {
365
+ const cachedReferences = referenceCache?.get(node);
366
+ if (cachedReferences) {
367
+ return cachedReferences;
368
+ }
369
+ const refs = new Map();
370
+ const localBindingCache = new WeakMap();
371
+ visit(node, null, (current, scope, parent, ancestors) => {
372
+ if (current.type !== "Identifier" || isInTypeContext(ancestors) || isBindingPosition(current, parent) || isPropertyOnlyIdentifier(current, parent) || isObjectPropertyKey(current, parent) || hasLocalBindingCached(scope, current.name, localBindingCache)) {
373
+ return;
374
+ }
375
+ const key = `${current.start}:${current.end}:${current.name}`;
376
+ refs.set(key, {
377
+ end: current.end,
378
+ name: current.name,
379
+ start: current.start
380
+ });
381
+ });
382
+ const resolvedReferences = [...refs.values()];
383
+ referenceCache?.set(node, resolvedReferences);
384
+ return resolvedReferences;
385
+ };
386
+ export const isBindingDeclaredWithin = (binding, container) => container.start <= binding.declaredAt && binding.declaredAt < container.end;
387
+ //# sourceMappingURL=scopeAnalysis.js.map
@@ -0,0 +1 @@
1
+ {"mappings":"AAgBA,SAAS,0BAA0B;AACnC,SAAS,uBAAuB;AAChC,SAAS,+BAA+B;AAYxC,OAAO,MAAM,oCAAoC,SAAwB;AACvE,KAAI,KAAK,SAAS,4BAA4B;AAC5C,SAAO;;AAGT,QAAO,mBAAmB,KAAK,CAAC,KAAK,iCAAiC;;AAGxE,OAAO,MAAM,YAAY,MAAc,aAA8B;AACnE,QAAO,gBAAgB,MAAM,UAAU,cAAc;;AAGvD,MAAM,aAAa,OAAe,QAAwB,GAAG,MAAM,GAAG;AAEtE,OAAO,MAAM,oBAAoB,UAAyC;AACxE,KAAI,CAAC,SAAS,MAAM,WAAW,GAAG;AAChC,SAAO;;AAGT,QAAO,IAAI,IAAI,MAAM,KAAK,SAA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AAI,MAAM,mBAAmB;AAC7C,QAAO;;AAGT,OAAO,MAAM,2BACX,SACA,cAEA,UAAU,SAAS,QAAQ,cAAc,QAAQ,aAAa,UAAU","names":[],"sources":["../../../src/utils/collectOxcTemplateDependencies/scopeAnalysis.ts"],"version":3,"sourcesContent":["/* eslint-disable no-restricted-syntax,no-continue,@typescript-eslint/no-use-before-define */\n\nimport type { SourceLocation } from '@wyw-in-js/shared';\nimport type {\n AssignmentExpression,\n Expression,\n ImportDeclaration,\n ImportSpecifier,\n ModuleExportName,\n Node,\n Program,\n TemplateLiteral,\n UpdateExpression,\n VariableDeclaration,\n} from 'oxc-parser';\n\nimport { getOxcNodeChildren } from '../oxc/ast';\nimport { parseOxcProgram } from '../oxc/parse';\nimport { createOxcSourceLocation } from '../oxc/sourceLocations';\nimport type {\n Binding,\n ExpressionSpan,\n ExtractionContext,\n ProgramAnalysis,\n ReferenceIdentifier,\n Scope,\n ScopedDeclarationKind,\n SpanLookup,\n} from './types';\n\nexport const containsTaggedTemplateExpression = (node: Node): boolean => {\n if (node.type === 'TaggedTemplateExpression') {\n return true;\n }\n\n return getOxcNodeChildren(node).some(containsTaggedTemplateExpression);\n};\n\nexport const parseOxc = (code: string, filename: string): Program => {\n return parseOxcProgram(code, filename, 'unambiguous');\n};\n\nconst toSpanKey = (start: number, end: number): string => `${start}:${end}`;\n\nexport const createSpanLookup = (spans?: ExpressionSpan[]): SpanLookup => {\n if (!spans || spans.length === 0) {\n return null;\n }\n\n return new Set(spans.map((span) => toSpanKey(span.start, span.end)));\n};\n\nconst matchesSpanLookup = (\n node: Pick<Node, 'start' | 'end'>,\n spanLookup: SpanLookup\n): boolean => !spanLookup || spanLookup.has(toSpanKey(node.start, node.end));\n\nexport const getSourceLocation = (\n start: number,\n end: number,\n ctx: Pick<ExtractionContext, 'filename' | 'loc'>\n): SourceLocation => createOxcSourceLocation(start, end, ctx.loc, ctx.filename);\n\nconst createScope = (\n parent: Scope | null,\n node: Pick<Node, 'start' | 'end'>,\n root = false,\n functionBoundary = false\n): Scope => ({\n bindings: new Map(),\n depth: parent ? parent.depth + 1 : 0,\n end: node.end,\n functionBoundary,\n params: new Set(),\n parent,\n root,\n start: node.start,\n});\n\nconst normalizeDeclarationKind = (\n declarationKind: VariableDeclaration['kind']\n): ScopedDeclarationKind => {\n if (declarationKind === 'var') {\n return 'var';\n }\n\n if (declarationKind === 'let') {\n return 'let';\n }\n\n return 'const';\n};\n\nconst moduleExportName = (node: ModuleExportName): string =>\n node.type === 'Literal' ? String(node.value) : node.name;\n\nconst getImportSpecifierInfo = (\n statement: ImportDeclaration,\n specifier: ImportDeclaration['specifiers'][number]\n): { imported: 'default' | '*' | string; local: string } | null => {\n const local = specifier.local?.name;\n if (!local) {\n return null;\n }\n\n if (specifier.type === 'ImportDefaultSpecifier') {\n return {\n imported: 'default',\n local,\n };\n }\n\n if (specifier.type === 'ImportNamespaceSpecifier') {\n return {\n imported: '*',\n local,\n };\n }\n\n if (\n statement.importKind === 'type' ||\n (specifier as ImportSpecifier).importKind === 'type'\n ) {\n return null;\n }\n\n return {\n imported: moduleExportName((specifier as ImportSpecifier).imported),\n local,\n };\n};\n\nconst getDeclarationScope = (\n scope: Scope,\n declarationKind: ScopedDeclarationKind\n): Scope => {\n if (declarationKind !== 'var') {\n return scope;\n }\n\n let current: Scope | null = scope;\n while (current && !current.functionBoundary) {\n current = current.parent;\n }\n\n return current ?? scope;\n};\n\nconst collectBindingNames = (node: Node): string[] => {\n if (node.type === 'Identifier') {\n return [node.name];\n }\n\n if (node.type === 'RestElement') {\n return collectBindingNames(node.argument);\n }\n\n if (node.type === 'AssignmentPattern') {\n return collectBindingNames(node.left);\n }\n\n if (node.type === 'ObjectPattern') {\n return node.properties.flatMap((property) =>\n property.type === 'RestElement'\n ? collectBindingNames(property.argument)\n : collectBindingNames(property.value)\n );\n }\n\n if (node.type === 'ArrayPattern') {\n return node.elements.flatMap((element) =>\n element ? collectBindingNames(element) : []\n );\n }\n\n if (node.type === 'TSParameterProperty') {\n return collectBindingNames(node.parameter);\n }\n\n return [];\n};\n\nexport const isInTypeContext = (ancestors: Node[]): boolean =>\n ancestors.some(\n (ancestor) =>\n ancestor.type.startsWith('TS') || ancestor.type.startsWith('JSDoc')\n );\n\nexport const isPropertyOnlyIdentifier = (\n node: Node,\n parent: Node | null\n): boolean =>\n !!parent &&\n parent.type === 'MemberExpression' &&\n parent.property === node &&\n !parent.computed;\n\nexport const isObjectPropertyKey = (node: Node, parent: Node | null): boolean =>\n !!parent &&\n parent.type === 'Property' &&\n parent.key === node &&\n !parent.computed &&\n parent.value !== node;\n\nexport const isBindingPosition = (node: Node, parent: Node | null): boolean => {\n if (!parent) {\n return false;\n }\n\n if (parent.type === 'VariableDeclarator' && parent.id === node) {\n return true;\n }\n\n if (\n (parent.type === 'FunctionDeclaration' ||\n parent.type === 'FunctionExpression' ||\n parent.type === 'ClassDeclaration' ||\n parent.type === 'ClassExpression') &&\n parent.id === node\n ) {\n return true;\n }\n\n if (\n (parent.type === 'ImportSpecifier' ||\n parent.type === 'ImportDefaultSpecifier' ||\n parent.type === 'ImportNamespaceSpecifier') &&\n 'local' in parent &&\n parent.local === node\n ) {\n return true;\n }\n\n return false;\n};\n\nconst visit = (\n node: Node,\n scope: Scope | null,\n enter: (\n node: Node,\n scope: Scope,\n parent: Node | null,\n ancestors: Node[]\n ) => void,\n parent: Node | null = null,\n ancestors: Node[] = []\n): void => {\n const visitNode = (\n currentNode: Node,\n currentScope: Scope | null,\n currentParent: Node | null\n ): void => {\n let nextScope: Scope;\n if (currentNode.type === 'Program') {\n nextScope = createScope(null, currentNode, true, true);\n } else if (\n currentNode.type === 'BlockStatement' ||\n currentNode.type === 'FunctionDeclaration' ||\n currentNode.type === 'FunctionExpression' ||\n currentNode.type === 'ArrowFunctionExpression'\n ) {\n nextScope = createScope(\n currentScope,\n currentNode,\n false,\n currentNode.type !== 'BlockStatement'\n );\n } else if (currentScope) {\n nextScope = currentScope;\n } else {\n nextScope = createScope(null, currentNode, false, true);\n }\n\n if (\n currentNode.type === 'FunctionDeclaration' ||\n currentNode.type === 'FunctionExpression' ||\n currentNode.type === 'ArrowFunctionExpression'\n ) {\n currentNode.params.forEach((param) => {\n collectBindingNames(param).forEach((name) => {\n nextScope.params.add(name);\n nextScope.bindings.set(name, {\n declaredAt: param.start,\n declaration: null,\n declarator: null,\n functionNode: null,\n isRoot: false,\n kind: 'param',\n name,\n scope: nextScope,\n });\n });\n });\n }\n\n enter(currentNode, nextScope, currentParent, ancestors);\n\n ancestors.push(currentNode);\n getOxcNodeChildren(currentNode).forEach((child) =>\n visitNode(child, nextScope, currentNode)\n );\n ancestors.pop();\n };\n\n visitNode(node, scope, parent);\n};\n\nexport const analyzeProgram = (\n program: Program,\n {\n collectTargetExpressions = false,\n collectTemplateLiterals = false,\n expressionSpanLookup = null,\n templateSpanLookup = null,\n }: {\n collectTargetExpressions?: boolean;\n collectTemplateLiterals?: boolean;\n expressionSpanLookup?: SpanLookup;\n templateSpanLookup?: SpanLookup;\n } = {}\n): ProgramAnalysis => {\n const bindings = new Map<string, Binding[]>();\n const usedNames = new Set<string>();\n const templateLiterals: TemplateLiteral[] = [];\n const targetExpressions: Expression[] = [];\n\n const addBinding = (scope: Scope, binding: Binding): void => {\n scope.bindings.set(binding.name, binding);\n const existing = bindings.get(binding.name) ?? [];\n existing.push(binding);\n bindings.set(binding.name, existing);\n };\n\n const collectTargets = (node: Node, ancestors: Node[]): void => {\n if (\n collectTemplateLiterals &&\n node.type === 'TemplateLiteral' &&\n node.expressions.length > 0 &&\n !ancestors.some((ancestor) => ancestor.type === 'TemplateLiteral') &&\n matchesSpanLookup(node, templateSpanLookup)\n ) {\n templateLiterals.push(node);\n }\n\n if (\n collectTargetExpressions &&\n expressionSpanLookup &&\n matchesSpanLookup(node, expressionSpanLookup)\n ) {\n targetExpressions.push(node as Expression);\n }\n };\n\n visit(program, null, (node, scope, _parent, ancestors) => {\n collectTargets(node, ancestors);\n\n if (node.type === 'Identifier') {\n usedNames.add(node.name);\n }\n\n if (isInTypeContext(ancestors)) {\n return;\n }\n\n if (\n node.type === 'FunctionDeclaration' ||\n node.type === 'FunctionExpression' ||\n node.type === 'ArrowFunctionExpression'\n ) {\n node.params.forEach((param) => {\n collectBindingNames(param).forEach((name) => {\n const binding = scope.bindings.get(name);\n if (binding) {\n addBinding(scope, binding);\n }\n });\n });\n\n if (node.type !== 'FunctionDeclaration') {\n return;\n }\n }\n\n if (node.type === 'ImportDeclaration') {\n const source = node.source.value;\n node.specifiers.forEach((specifier) => {\n const importInfo = getImportSpecifierInfo(node, specifier);\n if (!importInfo) {\n return;\n }\n\n addBinding(scope, {\n declaredAt: specifier.start,\n declaration: null,\n declarator: null,\n functionNode: null,\n imported: importInfo.imported,\n importedFrom: source,\n isRoot: scope.root,\n kind: 'import',\n name: importInfo.local,\n scope,\n });\n });\n return;\n }\n\n if (node.type !== 'VariableDeclaration') {\n if (node.type === 'FunctionDeclaration' && node.id) {\n const declarationScope = scope.parent ?? scope;\n const binding: Binding = {\n declaredAt: node.start,\n declaration: null,\n declarator: null,\n functionNode: node,\n isRoot: declarationScope.root,\n kind: 'function',\n name: node.id.name,\n scope: declarationScope,\n };\n addBinding(declarationScope, binding);\n }\n\n return;\n }\n\n node.declarations.forEach((declarator) => {\n collectBindingNames(declarator.id).forEach((name) => {\n const declarationKind = normalizeDeclarationKind(node.kind);\n const declarationScope = getDeclarationScope(scope, declarationKind);\n const binding: Binding = {\n declarationKind,\n declaredAt: declarator.start,\n declaration: node,\n declarator,\n functionNode: null,\n isRoot: declarationScope.root,\n kind: 'variable',\n name,\n scope: declarationScope,\n };\n addBinding(declarationScope, binding);\n });\n });\n });\n\n return {\n bindingsByName: bindings,\n rootMutationsByBinding: collectRootMutations(program),\n targetExpressions: targetExpressions.sort((a, b) => a.start - b.start),\n templateLiterals,\n usedNames,\n };\n};\n\nexport const resolveBindingAt = (\n ctx: Pick<ExtractionContext, 'bindingResolutionCache' | 'bindingsByName'>,\n name: string,\n referenceStart: number\n): Binding | undefined => {\n const cachedBindings = ctx.bindingResolutionCache.get(name);\n if (cachedBindings?.has(referenceStart)) {\n return cachedBindings.get(referenceStart) ?? undefined;\n }\n\n const bindings = ctx.bindingsByName.get(name);\n const bindingCache = cachedBindings ?? new Map<number, Binding | null>();\n if (!cachedBindings) {\n ctx.bindingResolutionCache.set(name, bindingCache);\n }\n\n if (!bindings || bindings.length === 0) {\n bindingCache.set(referenceStart, null);\n return undefined;\n }\n\n let binding: Binding | undefined;\n bindings.forEach((candidate) => {\n if (\n candidate.scope.start > referenceStart ||\n referenceStart >= candidate.scope.end\n ) {\n return;\n }\n\n if (\n !binding ||\n candidate.scope.depth > binding.scope.depth ||\n (candidate.scope.depth === binding.scope.depth &&\n candidate.declaredAt > binding.declaredAt)\n ) {\n binding = candidate;\n }\n });\n\n bindingCache.set(referenceStart, binding ?? null);\n return binding;\n};\n\nconst collectRootMutations = (\n program: Program\n): Map<string, Array<AssignmentExpression | UpdateExpression>> => {\n const mutations = new Map<\n string,\n Array<AssignmentExpression | UpdateExpression>\n >();\n\n const getRootMutationTarget = (\n node: Node\n ): { binding: string; path: Array<string | number> } | null => {\n if (node.type === 'Identifier') {\n return {\n binding: node.name,\n path: [],\n };\n }\n\n if (node.type !== 'MemberExpression') {\n return null;\n }\n\n const parent = getRootMutationTarget(node.object);\n if (!parent) {\n return null;\n }\n\n let key: string | number | null = null;\n if (\n node.computed &&\n node.property.type === 'Literal' &&\n (typeof node.property.value === 'string' ||\n typeof node.property.value === 'number')\n ) {\n key = node.property.value;\n } else if (!node.computed && node.property.type === 'Identifier') {\n key = node.property.name;\n }\n if (key === null) {\n return null;\n }\n\n return {\n binding: parent.binding,\n path: [...parent.path, key],\n };\n };\n\n program.body.forEach((statement) => {\n if (statement.type !== 'ExpressionStatement') {\n return;\n }\n\n const { expression } = statement;\n if (expression.type === 'AssignmentExpression') {\n const target = getRootMutationTarget(expression.left);\n if (!target || target.path.length === 0) {\n return;\n }\n\n const bucket = mutations.get(target.binding) ?? [];\n bucket.push(expression);\n mutations.set(target.binding, bucket);\n return;\n }\n\n if (expression.type === 'UpdateExpression') {\n const target = getRootMutationTarget(expression.argument);\n if (!target || target.path.length === 0) {\n return;\n }\n\n const bucket = mutations.get(target.binding) ?? [];\n bucket.push(expression);\n mutations.set(target.binding, bucket);\n }\n });\n\n return mutations;\n};\n\nconst hasLocalBinding = (scope: Scope, name: string): boolean => {\n let current: Scope | null = scope;\n\n while (current) {\n if (current.bindings.has(name)) {\n return true;\n }\n\n current = current.parent;\n }\n\n return false;\n};\n\nconst hasLocalBindingCached = (\n scope: Scope,\n name: string,\n cache: WeakMap<Scope, Map<string, boolean>>\n): boolean => {\n const scopeCache = cache.get(scope);\n if (scopeCache?.has(name)) {\n return scopeCache.get(name)!;\n }\n\n const result = hasLocalBinding(scope, name);\n const nextScopeCache = scopeCache ?? new Map<string, boolean>();\n nextScopeCache.set(name, result);\n if (!scopeCache) {\n cache.set(scope, nextScopeCache);\n }\n\n return result;\n};\n\nexport const findReferences = (\n node: Node,\n referenceCache?: WeakMap<Node, ReferenceIdentifier[]>\n): ReferenceIdentifier[] => {\n const cachedReferences = referenceCache?.get(node);\n if (cachedReferences) {\n return cachedReferences;\n }\n\n const refs = new Map<string, ReferenceIdentifier>();\n const localBindingCache = new WeakMap<Scope, Map<string, boolean>>();\n\n visit(node, null, (current, scope, parent, ancestors) => {\n if (\n current.type !== 'Identifier' ||\n isInTypeContext(ancestors) ||\n isBindingPosition(current, parent) ||\n isPropertyOnlyIdentifier(current, parent) ||\n isObjectPropertyKey(current, parent) ||\n hasLocalBindingCached(scope, current.name, localBindingCache)\n ) {\n return;\n }\n\n const key = `${current.start}:${current.end}:${current.name}`;\n refs.set(key, {\n end: current.end,\n name: current.name,\n start: current.start,\n });\n });\n\n const resolvedReferences = [...refs.values()];\n referenceCache?.set(node, resolvedReferences);\n return resolvedReferences;\n};\n\nexport const isBindingDeclaredWithin = (\n binding: Binding,\n container: Node\n): boolean =>\n container.start <= binding.declaredAt && binding.declaredAt < container.end;\n"],"file":"scopeAnalysis.js"}
@@ -0,0 +1,17 @@
1
+ export const lookupStaticBinding = (staticBindings, source, imported) => {
2
+ if (!staticBindings || !source || !imported) {
3
+ return { found: false };
4
+ }
5
+ const sourceMap = staticBindings[source];
6
+ if (!sourceMap) {
7
+ return { found: false };
8
+ }
9
+ if (!Object.prototype.hasOwnProperty.call(sourceMap, imported)) {
10
+ return { found: false };
11
+ }
12
+ return {
13
+ found: true,
14
+ value: sourceMap[imported]
15
+ };
16
+ };
17
+ //# sourceMappingURL=staticBindings.js.map
@@ -0,0 +1 @@
1
+ {"mappings":"AAEA,OAAO,MAAM,uBACX,gBACA,QACA,aACuD;AACvD,KAAI,CAAC,kBAAkB,CAAC,UAAU,CAAC,UAAU;AAC3C,SAAO,EAAE,OAAO,OAAO;;CAEzB,MAAM,YAAY,eAAe;AACjC,KAAI,CAAC,WAAW;AACd,SAAO,EAAE,OAAO,OAAO;;AAEzB,KAAI,CAAC,OAAO,UAAU,eAAe,KAAK,WAAW,SAAS,EAAE;AAC9D,SAAO,EAAE,OAAO,OAAO;;AAEzB,QAAO;EAAE,OAAO;EAAM,OAAO,UAAU;EAAW","names":[],"sources":["../../../src/utils/collectOxcTemplateDependencies/staticBindings.ts"],"version":3,"sourcesContent":["import type { StaticBindings } from './types';\n\nexport const lookupStaticBinding = (\n staticBindings: StaticBindings | undefined,\n source: string | undefined,\n imported: string | undefined\n): { found: true; value: unknown } | { found: false } => {\n if (!staticBindings || !source || !imported) {\n return { found: false };\n }\n const sourceMap = staticBindings[source];\n if (!sourceMap) {\n return { found: false };\n }\n if (!Object.prototype.hasOwnProperty.call(sourceMap, imported)) {\n return { found: false };\n }\n return { found: true, value: sourceMap[imported] };\n};\n"],"file":"staticBindings.js"}