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

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (279) hide show
  1. package/esm/cache.js +7 -0
  2. package/esm/cache.js.map +1 -1
  3. package/esm/debug/fileReporter.js +35 -1
  4. package/esm/debug/fileReporter.js.map +1 -1
  5. package/esm/eval/broker.js +230 -89
  6. package/esm/eval/broker.js.map +1 -1
  7. package/esm/eval/protocol.js.map +1 -1
  8. package/esm/eval/runner.js +143 -24
  9. package/esm/eval/runner.js.map +1 -1
  10. package/esm/module.js +70 -43
  11. package/esm/module.js.map +1 -1
  12. package/esm/transform/Entrypoint.types.js.map +1 -1
  13. package/esm/transform/generators/collect.js +1 -0
  14. package/esm/transform/generators/collect.js.map +1 -1
  15. package/esm/transform/generators/processEntrypoint.js +35 -20
  16. package/esm/transform/generators/processEntrypoint.js.map +1 -1
  17. package/esm/transform/generators/resolveImports.js +48 -2
  18. package/esm/transform/generators/resolveImports.js.map +1 -1
  19. package/esm/transform/generators/resolveStaticOxcValues/cache.js +182 -0
  20. package/esm/transform/generators/resolveStaticOxcValues/cache.js.map +1 -0
  21. package/esm/transform/generators/resolveStaticOxcValues/candidateResolver.js +146 -0
  22. package/esm/transform/generators/resolveStaticOxcValues/candidateResolver.js.map +1 -0
  23. package/esm/transform/generators/resolveStaticOxcValues/dependencies.js +33 -0
  24. package/esm/transform/generators/resolveStaticOxcValues/dependencies.js.map +1 -0
  25. package/esm/transform/generators/resolveStaticOxcValues/environment.js +29 -0
  26. package/esm/transform/generators/resolveStaticOxcValues/environment.js.map +1 -0
  27. package/esm/transform/generators/resolveStaticOxcValues/exportResolver.js +351 -0
  28. package/esm/transform/generators/resolveStaticOxcValues/exportResolver.js.map +1 -0
  29. package/esm/transform/generators/resolveStaticOxcValues/exportTargets.js +253 -0
  30. package/esm/transform/generators/resolveStaticOxcValues/exportTargets.js.map +1 -0
  31. package/esm/transform/generators/resolveStaticOxcValues/objectAssign.js +310 -0
  32. package/esm/transform/generators/resolveStaticOxcValues/objectAssign.js.map +1 -0
  33. package/esm/transform/generators/resolveStaticOxcValues/objectAssignStaticExport.js +74 -0
  34. package/esm/transform/generators/resolveStaticOxcValues/objectAssignStaticExport.js.map +1 -0
  35. package/esm/transform/generators/resolveStaticOxcValues/opaqueRuntime.js +229 -0
  36. package/esm/transform/generators/resolveStaticOxcValues/opaqueRuntime.js.map +1 -0
  37. package/esm/transform/generators/resolveStaticOxcValues/processorStaticExport.js +172 -0
  38. package/esm/transform/generators/resolveStaticOxcValues/processorStaticExport.js.map +1 -0
  39. package/esm/transform/generators/resolveStaticOxcValues/processorStaticModel.js +476 -0
  40. package/esm/transform/generators/resolveStaticOxcValues/processorStaticModel.js.map +1 -0
  41. package/esm/transform/generators/resolveStaticOxcValues/processorTarget.js +55 -0
  42. package/esm/transform/generators/resolveStaticOxcValues/processorTarget.js.map +1 -0
  43. package/esm/transform/generators/resolveStaticOxcValues/programAnalysis.js +123 -0
  44. package/esm/transform/generators/resolveStaticOxcValues/programAnalysis.js.map +1 -0
  45. package/esm/transform/generators/resolveStaticOxcValues/prune.js +272 -0
  46. package/esm/transform/generators/resolveStaticOxcValues/prune.js.map +1 -0
  47. package/esm/transform/generators/resolveStaticOxcValues/resolveStaticOxcPreevalValues.js +184 -0
  48. package/esm/transform/generators/resolveStaticOxcValues/resolveStaticOxcPreevalValues.js.map +1 -0
  49. package/esm/transform/generators/resolveStaticOxcValues/staticExpression.js +298 -0
  50. package/esm/transform/generators/resolveStaticOxcValues/staticExpression.js.map +1 -0
  51. package/esm/transform/generators/resolveStaticOxcValues/staticExpressionDependencies.js +298 -0
  52. package/esm/transform/generators/resolveStaticOxcValues/staticExpressionDependencies.js.map +1 -0
  53. package/esm/transform/generators/resolveStaticOxcValues/types.js +3 -0
  54. package/esm/transform/generators/resolveStaticOxcValues/types.js.map +1 -0
  55. package/esm/transform/generators/resolveStaticOxcValues/zeroArgFunctionStaticExport.js +59 -0
  56. package/esm/transform/generators/resolveStaticOxcValues/zeroArgFunctionStaticExport.js.map +1 -0
  57. package/esm/transform/generators/resolveStaticOxcValues.js +1 -2910
  58. package/esm/transform/generators/resolveStaticOxcValues.js.map +1 -1
  59. package/esm/transform/generators/transform.js +58 -45
  60. package/esm/transform/generators/transform.js.map +1 -1
  61. package/esm/transform/helpers/loadWywOptions.js +33 -4
  62. package/esm/transform/helpers/loadWywOptions.js.map +1 -1
  63. package/esm/transform.js +3 -1
  64. package/esm/transform.js.map +1 -1
  65. package/esm/utils/EventEmitter.js +55 -9
  66. package/esm/utils/EventEmitter.js.map +1 -1
  67. package/esm/utils/applyOxcProcessors/applyOxcProcessors.js +160 -0
  68. package/esm/utils/applyOxcProcessors/applyOxcProcessors.js.map +1 -0
  69. package/esm/utils/applyOxcProcessors/cleanupBindings.js +157 -0
  70. package/esm/utils/applyOxcProcessors/cleanupBindings.js.map +1 -0
  71. package/esm/utils/applyOxcProcessors/cleanupRemovals.js +517 -0
  72. package/esm/utils/applyOxcProcessors/cleanupRemovals.js.map +1 -0
  73. package/esm/utils/applyOxcProcessors/displayName.js +93 -0
  74. package/esm/utils/applyOxcProcessors/displayName.js.map +1 -0
  75. package/esm/utils/applyOxcProcessors/expressionValues.js +152 -0
  76. package/esm/utils/applyOxcProcessors/expressionValues.js.map +1 -0
  77. package/esm/utils/applyOxcProcessors/processorFactory.js +62 -0
  78. package/esm/utils/applyOxcProcessors/processorFactory.js.map +1 -0
  79. package/esm/utils/applyOxcProcessors/processorUsages.js +143 -0
  80. package/esm/utils/applyOxcProcessors/processorUsages.js.map +1 -0
  81. package/esm/utils/applyOxcProcessors/sameFileStaticValues.js +207 -0
  82. package/esm/utils/applyOxcProcessors/sameFileStaticValues.js.map +1 -0
  83. package/esm/utils/applyOxcProcessors/shared.js +29 -0
  84. package/esm/utils/applyOxcProcessors/shared.js.map +1 -0
  85. package/esm/utils/applyOxcProcessors/types.js +2 -0
  86. package/esm/utils/applyOxcProcessors/types.js.map +1 -0
  87. package/esm/utils/applyOxcProcessors.js +1 -1216
  88. package/esm/utils/applyOxcProcessors.js.map +1 -1
  89. package/esm/utils/collectOxcExportsAndImports.js +8 -0
  90. package/esm/utils/collectOxcExportsAndImports.js.map +1 -1
  91. package/esm/utils/collectOxcRuntime/normalizeRuntimeCode.js +157 -0
  92. package/esm/utils/collectOxcRuntime/normalizeRuntimeCode.js.map +1 -0
  93. package/esm/utils/collectOxcRuntime/sourceMap.js +36 -0
  94. package/esm/utils/collectOxcRuntime/sourceMap.js.map +1 -0
  95. package/esm/utils/collectOxcRuntime/types.js +2 -0
  96. package/esm/utils/collectOxcRuntime/types.js.map +1 -0
  97. package/esm/utils/collectOxcRuntime.js +5 -193
  98. package/esm/utils/collectOxcRuntime.js.map +1 -1
  99. package/esm/utils/collectOxcTemplateDependencies/expressionExtraction.js +496 -0
  100. package/esm/utils/collectOxcTemplateDependencies/expressionExtraction.js.map +1 -0
  101. package/esm/utils/collectOxcTemplateDependencies/expressionReplacements.js +113 -0
  102. package/esm/utils/collectOxcTemplateDependencies/expressionReplacements.js.map +1 -0
  103. package/esm/utils/collectOxcTemplateDependencies/scopeAnalysis.js +387 -0
  104. package/esm/utils/collectOxcTemplateDependencies/scopeAnalysis.js.map +1 -0
  105. package/esm/utils/collectOxcTemplateDependencies/staticBindings.js +17 -0
  106. package/esm/utils/collectOxcTemplateDependencies/staticBindings.js.map +1 -0
  107. package/esm/utils/collectOxcTemplateDependencies/staticEvaluator.js +540 -0
  108. package/esm/utils/collectOxcTemplateDependencies/staticEvaluator.js.map +1 -0
  109. package/esm/utils/collectOxcTemplateDependencies/types.js +2 -0
  110. package/esm/utils/collectOxcTemplateDependencies/types.js.map +1 -0
  111. package/esm/utils/collectOxcTemplateDependencies.js +3 -1397
  112. package/esm/utils/collectOxcTemplateDependencies.js.map +1 -1
  113. package/esm/utils/nativeResolver.js +93 -0
  114. package/esm/utils/nativeResolver.js.map +1 -0
  115. package/esm/utils/oxc/ast.js +58 -0
  116. package/esm/utils/oxc/ast.js.map +1 -0
  117. package/esm/utils/oxc/parse.js +3 -0
  118. package/esm/utils/oxc/parse.js.map +1 -0
  119. package/esm/utils/oxc/replacements.js +14 -0
  120. package/esm/utils/oxc/replacements.js.map +1 -0
  121. package/esm/utils/oxc/sourceLocations.js +59 -0
  122. package/esm/utils/oxc/sourceLocations.js.map +1 -0
  123. package/esm/utils/oxcPreevalStage/evalStrategy.js +3 -0
  124. package/esm/utils/oxcPreevalStage/evalStrategy.js.map +1 -0
  125. package/esm/utils/oxcPreevalStage/prepareCode.js +21 -0
  126. package/esm/utils/oxcPreevalStage/prepareCode.js.map +1 -0
  127. package/esm/utils/oxcPreevalStage/prevalExport.js +23 -0
  128. package/esm/utils/oxcPreevalStage/prevalExport.js.map +1 -0
  129. package/esm/utils/oxcPreevalStage/processors.js +17 -0
  130. package/esm/utils/oxcPreevalStage/processors.js.map +1 -0
  131. package/esm/utils/oxcPreevalStage/staticOverlay.js +18 -0
  132. package/esm/utils/oxcPreevalStage/staticOverlay.js.map +1 -0
  133. package/esm/utils/oxcPreevalStage/types.js +2 -0
  134. package/esm/utils/oxcPreevalStage/types.js.map +1 -0
  135. package/esm/utils/oxcPreevalStage.js +17 -73
  136. package/esm/utils/oxcPreevalStage.js.map +1 -1
  137. package/esm/utils/oxcPreevalTransforms.js +106 -66
  138. package/esm/utils/oxcPreevalTransforms.js.map +1 -1
  139. package/esm/utils/parseOxc.js +6 -1
  140. package/esm/utils/parseOxc.js.map +1 -1
  141. package/esm/utils/processorStaticSemantics.js +157 -0
  142. package/esm/utils/processorStaticSemantics.js.map +1 -0
  143. package/esm/utils/resolveWithConditions.js +3 -3
  144. package/esm/utils/resolveWithConditions.js.map +1 -1
  145. package/package.json +6 -5
  146. package/types/cache.js +8 -0
  147. package/types/debug/fileReporter.js +46 -1
  148. package/types/eval/broker.d.ts +6 -2
  149. package/types/eval/broker.js +244 -95
  150. package/types/eval/protocol.d.ts +15 -1
  151. package/types/module.d.ts +4 -1
  152. package/types/module.js +97 -48
  153. package/types/transform/Entrypoint.types.d.ts +4 -0
  154. package/types/transform/generators/collect.js +1 -0
  155. package/types/transform/generators/resolveImports.d.ts +3 -1
  156. package/types/transform/generators/resolveImports.js +49 -2
  157. package/types/transform/generators/resolveStaticOxcValues/cache.d.ts +25 -0
  158. package/types/transform/generators/resolveStaticOxcValues/cache.js +182 -0
  159. package/types/transform/generators/resolveStaticOxcValues/candidateResolver.d.ts +5 -0
  160. package/types/transform/generators/resolveStaticOxcValues/candidateResolver.js +149 -0
  161. package/types/transform/generators/resolveStaticOxcValues/dependencies.d.ts +6 -0
  162. package/types/transform/generators/resolveStaticOxcValues/dependencies.js +37 -0
  163. package/types/transform/generators/resolveStaticOxcValues/environment.d.ts +12 -0
  164. package/types/transform/generators/resolveStaticOxcValues/environment.js +32 -0
  165. package/types/transform/generators/resolveStaticOxcValues/exportResolver.d.ts +4 -0
  166. package/types/transform/generators/resolveStaticOxcValues/exportResolver.js +358 -0
  167. package/types/transform/generators/resolveStaticOxcValues/exportTargets.d.ts +20 -0
  168. package/types/transform/generators/resolveStaticOxcValues/exportTargets.js +285 -0
  169. package/types/transform/generators/resolveStaticOxcValues/objectAssign.d.ts +41 -0
  170. package/types/transform/generators/resolveStaticOxcValues/objectAssign.js +352 -0
  171. package/types/transform/generators/resolveStaticOxcValues/objectAssignStaticExport.d.ts +6 -0
  172. package/types/transform/generators/resolveStaticOxcValues/objectAssignStaticExport.js +88 -0
  173. package/types/transform/generators/resolveStaticOxcValues/opaqueRuntime.d.ts +13 -0
  174. package/types/transform/generators/resolveStaticOxcValues/opaqueRuntime.js +263 -0
  175. package/types/transform/generators/resolveStaticOxcValues/processorStaticExport.d.ts +4 -0
  176. package/types/transform/generators/resolveStaticOxcValues/processorStaticExport.js +191 -0
  177. package/types/transform/generators/resolveStaticOxcValues/processorStaticModel.d.ts +40 -0
  178. package/types/transform/generators/resolveStaticOxcValues/processorStaticModel.js +516 -0
  179. package/types/transform/generators/resolveStaticOxcValues/processorTarget.d.ts +5 -0
  180. package/types/transform/generators/resolveStaticOxcValues/processorTarget.js +62 -0
  181. package/types/transform/generators/resolveStaticOxcValues/programAnalysis.d.ts +11 -0
  182. package/types/transform/generators/resolveStaticOxcValues/programAnalysis.js +133 -0
  183. package/types/transform/generators/resolveStaticOxcValues/prune.d.ts +15 -0
  184. package/types/transform/generators/resolveStaticOxcValues/prune.js +312 -0
  185. package/types/transform/generators/resolveStaticOxcValues/resolveStaticOxcPreevalValues.d.ts +2 -0
  186. package/types/transform/generators/resolveStaticOxcValues/resolveStaticOxcPreevalValues.js +198 -0
  187. package/types/transform/generators/resolveStaticOxcValues/staticExpression.d.ts +25 -0
  188. package/types/transform/generators/resolveStaticOxcValues/staticExpression.js +391 -0
  189. package/types/transform/generators/resolveStaticOxcValues/staticExpressionDependencies.d.ts +14 -0
  190. package/types/transform/generators/resolveStaticOxcValues/staticExpressionDependencies.js +304 -0
  191. package/types/transform/generators/resolveStaticOxcValues/types.d.ts +99 -0
  192. package/types/transform/generators/resolveStaticOxcValues/types.js +2 -0
  193. package/types/transform/generators/resolveStaticOxcValues/zeroArgFunctionStaticExport.d.ts +7 -0
  194. package/types/transform/generators/resolveStaticOxcValues/zeroArgFunctionStaticExport.js +64 -0
  195. package/types/transform/generators/resolveStaticOxcValues.d.ts +1 -2
  196. package/types/transform/generators/resolveStaticOxcValues.js +1 -3235
  197. package/types/transform/generators/transform.js +64 -49
  198. package/types/transform/helpers/loadWywOptions.js +23 -3
  199. package/types/transform.js +3 -1
  200. package/types/utils/EventEmitter.d.ts +16 -1
  201. package/types/utils/EventEmitter.js +57 -9
  202. package/types/utils/applyOxcProcessors/applyOxcProcessors.d.ts +9 -0
  203. package/types/utils/applyOxcProcessors/applyOxcProcessors.js +169 -0
  204. package/types/utils/applyOxcProcessors/cleanupBindings.d.ts +12 -0
  205. package/types/utils/applyOxcProcessors/cleanupBindings.js +189 -0
  206. package/types/utils/applyOxcProcessors/cleanupRemovals.d.ts +16 -0
  207. package/types/utils/applyOxcProcessors/cleanupRemovals.js +567 -0
  208. package/types/utils/applyOxcProcessors/displayName.d.ts +9 -0
  209. package/types/utils/applyOxcProcessors/displayName.js +113 -0
  210. package/types/utils/applyOxcProcessors/expressionValues.d.ts +11 -0
  211. package/types/utils/applyOxcProcessors/expressionValues.js +170 -0
  212. package/types/utils/applyOxcProcessors/processorFactory.d.ts +7 -0
  213. package/types/utils/applyOxcProcessors/processorFactory.js +70 -0
  214. package/types/utils/applyOxcProcessors/processorUsages.d.ts +16 -0
  215. package/types/utils/applyOxcProcessors/processorUsages.js +163 -0
  216. package/types/utils/applyOxcProcessors/sameFileStaticValues.d.ts +23 -0
  217. package/types/utils/applyOxcProcessors/sameFileStaticValues.js +211 -0
  218. package/types/utils/applyOxcProcessors/shared.d.ts +10 -0
  219. package/types/utils/applyOxcProcessors/shared.js +37 -0
  220. package/types/utils/applyOxcProcessors/types.d.ts +88 -0
  221. package/types/utils/applyOxcProcessors/types.js +1 -0
  222. package/types/utils/applyOxcProcessors.d.ts +1 -16
  223. package/types/utils/applyOxcProcessors.js +1 -1391
  224. package/types/utils/collectOxcExportsAndImports.js +8 -0
  225. package/types/utils/collectOxcRuntime/normalizeRuntimeCode.d.ts +1 -0
  226. package/types/utils/collectOxcRuntime/normalizeRuntimeCode.js +185 -0
  227. package/types/utils/collectOxcRuntime/sourceMap.d.ts +2 -0
  228. package/types/utils/collectOxcRuntime/sourceMap.js +37 -0
  229. package/types/utils/collectOxcRuntime/types.d.ts +17 -0
  230. package/types/utils/collectOxcRuntime/types.js +1 -0
  231. package/types/utils/collectOxcRuntime.d.ts +2 -12
  232. package/types/utils/collectOxcRuntime.js +5 -224
  233. package/types/utils/collectOxcTemplateDependencies/expressionExtraction.d.ts +6 -0
  234. package/types/utils/collectOxcTemplateDependencies/expressionExtraction.js +550 -0
  235. package/types/utils/collectOxcTemplateDependencies/expressionReplacements.d.ts +11 -0
  236. package/types/utils/collectOxcTemplateDependencies/expressionReplacements.js +131 -0
  237. package/types/utils/collectOxcTemplateDependencies/scopeAnalysis.d.ts +20 -0
  238. package/types/utils/collectOxcTemplateDependencies/scopeAnalysis.js +434 -0
  239. package/types/utils/collectOxcTemplateDependencies/staticBindings.d.ts +7 -0
  240. package/types/utils/collectOxcTemplateDependencies/staticBindings.js +13 -0
  241. package/types/utils/collectOxcTemplateDependencies/staticEvaluator.d.ts +13 -0
  242. package/types/utils/collectOxcTemplateDependencies/staticEvaluator.js +626 -0
  243. package/types/utils/collectOxcTemplateDependencies/types.d.ts +116 -0
  244. package/types/utils/collectOxcTemplateDependencies/types.js +1 -0
  245. package/types/utils/collectOxcTemplateDependencies.d.ts +4 -38
  246. package/types/utils/collectOxcTemplateDependencies.js +3 -1580
  247. package/types/utils/nativeResolver.d.ts +13 -0
  248. package/types/utils/nativeResolver.js +91 -0
  249. package/types/utils/oxc/ast.d.ts +4 -0
  250. package/types/utils/oxc/ast.js +61 -0
  251. package/types/utils/oxc/parse.d.ts +3 -0
  252. package/types/utils/oxc/parse.js +2 -0
  253. package/types/utils/oxc/replacements.d.ts +12 -0
  254. package/types/utils/oxc/replacements.js +18 -0
  255. package/types/utils/oxc/sourceLocations.d.ts +5 -0
  256. package/types/utils/oxc/sourceLocations.js +63 -0
  257. package/types/utils/oxcPreevalStage/evalStrategy.d.ts +3 -0
  258. package/types/utils/oxcPreevalStage/evalStrategy.js +2 -0
  259. package/types/utils/oxcPreevalStage/prepareCode.d.ts +3 -0
  260. package/types/utils/oxcPreevalStage/prepareCode.js +20 -0
  261. package/types/utils/oxcPreevalStage/prevalExport.d.ts +1 -0
  262. package/types/utils/oxcPreevalStage/prevalExport.js +22 -0
  263. package/types/utils/oxcPreevalStage/processors.d.ts +10 -0
  264. package/types/utils/oxcPreevalStage/processors.js +16 -0
  265. package/types/utils/oxcPreevalStage/staticOverlay.d.ts +3 -0
  266. package/types/utils/oxcPreevalStage/staticOverlay.js +23 -0
  267. package/types/utils/oxcPreevalStage/types.d.ts +22 -0
  268. package/types/utils/oxcPreevalStage/types.js +1 -0
  269. package/types/utils/oxcPreevalStage.d.ts +2 -18
  270. package/types/utils/oxcPreevalStage.js +17 -79
  271. package/types/utils/oxcPreevalTransforms.js +111 -75
  272. package/types/utils/parseOxc.js +6 -1
  273. package/types/utils/processorStaticSemantics.d.ts +13 -0
  274. package/types/utils/processorStaticSemantics.js +191 -0
  275. package/types/utils/resolveWithConditions.js +3 -3
  276. package/esm/eval/resolverStrategy.js +0 -51
  277. package/esm/eval/resolverStrategy.js.map +0 -1
  278. package/types/eval/resolverStrategy.d.ts +0 -13
  279. package/types/eval/resolverStrategy.js +0 -46
@@ -0,0 +1,93 @@
1
+ /* eslint-disable no-restricted-syntax */
2
+ import { basename, dirname } from "path";
3
+ import { ValueType } from "@wyw-in-js/shared";
4
+ import { isOxcNode, walkOxc } from "../oxc/ast.js";
5
+ import { isNodeReference } from "./cleanupBindings.js";
6
+ export const getPropertyKeyName = (property, code) => {
7
+ const { key } = property;
8
+ if (!isOxcNode(key)) {
9
+ return null;
10
+ }
11
+ if (key.type === "Identifier") {
12
+ return key.name;
13
+ }
14
+ if (key.type === "Literal") {
15
+ return String(key.value);
16
+ }
17
+ return typeof key.start === "number" && typeof key.end === "number" ? code.slice(key.start, key.end) : null;
18
+ };
19
+ export const getDisplayName = (ancestors, idx, code, filename) => {
20
+ const owner = [...ancestors].reverse().find((node) => {
21
+ return node.type === "Property" || node.type === "JSXOpeningElement" || node.type === "VariableDeclarator";
22
+ });
23
+ if (owner?.type === "Property") {
24
+ const keyName = getPropertyKeyName(owner, code);
25
+ if (keyName) {
26
+ return keyName;
27
+ }
28
+ } else if (owner?.type === "JSXOpeningElement") {
29
+ const { name } = owner;
30
+ if (isOxcNode(name) && name.type === "JSXIdentifier") {
31
+ return name.name;
32
+ }
33
+ } else if (owner?.type === "VariableDeclarator") {
34
+ const { id } = owner;
35
+ if (isOxcNode(id) && id.type === "Identifier") {
36
+ return id.name;
37
+ }
38
+ }
39
+ let displayName = basename(filename ?? "unknown").replace(/\.[a-z\d]+$/, "");
40
+ if (filename && /^index\.[a-z\d]+$/.test(basename(filename))) {
41
+ displayName = basename(dirname(filename));
42
+ }
43
+ if (!displayName) {
44
+ throw new Error("Couldn't determine a name for the component. Ensure that it's either:\n" + "- Assigned to a variable\n" + "- Is an object property\n" + "- Is a prop in a JSX element\n");
45
+ }
46
+ return `${displayName}${idx}`;
47
+ };
48
+ export const getTagOwner = (ancestors) => {
49
+ const owner = [...ancestors].reverse().find((node) => node.type === "Property" || node.type === "JSXOpeningElement" || node.type === "VariableDeclarator");
50
+ return owner ?? null;
51
+ };
52
+ export const isTagReferenced = (program, ancestors) => {
53
+ const owner = getTagOwner(ancestors);
54
+ if (owner?.type !== "VariableDeclarator") {
55
+ return true;
56
+ }
57
+ const { id } = owner;
58
+ if (!isOxcNode(id) || id.type !== "Identifier") {
59
+ return true;
60
+ }
61
+ if (ancestors.some((node) => node.type === "ExportNamedDeclaration")) {
62
+ return true;
63
+ }
64
+ let referenced = false;
65
+ walkOxc(program, (node, parent) => {
66
+ const referenceName = node.type === "Identifier" || node.type === "JSXIdentifier" ? node.name : null;
67
+ if (referenced || referenceName !== id.name || node.type === "Identifier" && node.start === id.start && node.end === id.end) {
68
+ return;
69
+ }
70
+ referenced = isNodeReference(node, parent);
71
+ });
72
+ return referenced;
73
+ };
74
+ export const collectSameFileProcessorStaticValues = (expressionValues, processorStaticValuesByLocal) => {
75
+ const staticValues = [];
76
+ const seen = new Set();
77
+ expressionValues.forEach((value) => {
78
+ if (value.kind !== ValueType.LAZY) {
79
+ return;
80
+ }
81
+ const staticValue = processorStaticValuesByLocal.get(value.source);
82
+ if (staticValue === undefined || value.ex.type !== "Identifier" || seen.has(value.ex.name)) {
83
+ return;
84
+ }
85
+ seen.add(value.ex.name);
86
+ staticValues.push({
87
+ name: value.ex.name,
88
+ value: staticValue
89
+ });
90
+ });
91
+ return staticValues;
92
+ };
93
+ //# sourceMappingURL=displayName.js.map
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B,OAAO,MAAM,eAAe,cAAsC;CAChE,MAAM,QAAQ,CAAC,GAAG,UAAU,CACzB,SAAS,CACT,MACE,SACC,KAAK,SAAS,cACd,KAAK,SAAS,uBACd,KAAK,SAAS,qBACjB;AAEH,QAAO,SAAS;;AAGlB,OAAO,MAAM,mBACX,SACA,cACY;CACZ,MAAM,QAAQ,YAAY,UAAU;AACpC,KAAI,OAAO,SAAS,sBAAsB;AACxC,SAAO;;CAGT,MAAM,EAAE,OAAO;AACf,KAAI,CAAC,UAAU,GAAG,IAAI,GAAG,SAAS,cAAc;AAC9C,SAAO;;AAGT,KAAI,UAAU,MAAM,SAAS,KAAK,SAAS,yBAAyB,EAAE;AACpE,SAAO;;CAGT,IAAI,aAAa;AACjB,SAAQ,UAAU,MAAM,WAAW;EACjC,MAAM,gBACJ,KAAK,SAAS,gBAAgB,KAAK,SAAS,kBACxC,KAAK,OACL;AAEN,MACE,cACA,kBAAkB,GAAG,QACpB,KAAK,SAAS,gBACb,KAAK,UAAU,GAAG,SAClB,KAAK,QAAQ,GAAG,KAClB;AACA;;AAGF,eAAa,gBAAgB,MAAM,OAAO;GAC1C;AAEF,QAAO;;AAGT,OAAO,MAAM,wCACX,kBACA,iCACqB;CACrB,MAAM,eAAiC,EAAE;CACzC,MAAM,OAAO,IAAI,KAAa;AAE9B,kBAAiB,SAAS,UAAU;AAClC,MAAI,MAAM,SAAS,UAAU,MAAM;AACjC;;EAGF,MAAM,cAAc,6BAA6B,IAAI,MAAM,OAAO;AAClE,MACE,gBAAgB,aAChB,MAAM,GAAG,SAAS,gBAClB,KAAK,IAAI,MAAM,GAAG,KAAK,EACvB;AACA;;AAGF,OAAK,IAAI,MAAM,GAAG,KAAK;AACvB,eAAa,KAAK;GAChB,MAAM,MAAM,GAAG;GACf,OAAO;GACR,CAAC;GACF;AAEF,QAAO","names":[],"sources":["../../../src/utils/applyOxcProcessors/displayName.ts"],"version":3,"sourcesContent":["/* eslint-disable no-restricted-syntax */\n\nimport { basename, dirname } from 'path';\n\nimport type { ExpressionValue } from '@wyw-in-js/shared';\nimport { ValueType } from '@wyw-in-js/shared';\nimport type { Node, Program } from 'oxc-parser';\n\nimport type { OxcStaticValue } from '../collectOxcTemplateDependencies';\nimport { isOxcNode, walkOxc } from '../oxc/ast';\nimport { isNodeReference } from './cleanupBindings';\nimport type { AnyNode } from './types';\n\nexport const getPropertyKeyName = (\n property: AnyNode,\n code: string\n): string | null => {\n const { key } = property;\n if (!isOxcNode(key)) {\n return null;\n }\n\n if (key.type === 'Identifier') {\n return key.name;\n }\n\n if (key.type === 'Literal') {\n return String(key.value);\n }\n\n return typeof key.start === 'number' && typeof key.end === 'number'\n ? code.slice(key.start, key.end)\n : null;\n};\n\nexport const getDisplayName = (\n ancestors: Node[],\n idx: number,\n code: string,\n filename?: string | null\n): string => {\n const owner = [...ancestors].reverse().find((node) => {\n return (\n node.type === 'Property' ||\n node.type === 'JSXOpeningElement' ||\n node.type === 'VariableDeclarator'\n );\n }) as AnyNode | undefined;\n\n if (owner?.type === 'Property') {\n const keyName = getPropertyKeyName(owner, code);\n if (keyName) {\n return keyName;\n }\n } else if (owner?.type === 'JSXOpeningElement') {\n const { name } = owner;\n if (isOxcNode(name) && name.type === 'JSXIdentifier') {\n return name.name;\n }\n } else if (owner?.type === 'VariableDeclarator') {\n const { id } = owner;\n if (isOxcNode(id) && id.type === 'Identifier') {\n return id.name;\n }\n }\n\n let displayName = basename(filename ?? 'unknown').replace(/\\.[a-z\\d]+$/, '');\n if (filename && /^index\\.[a-z\\d]+$/.test(basename(filename))) {\n displayName = basename(dirname(filename));\n }\n\n if (!displayName) {\n throw new Error(\n \"Couldn't determine a name for the component. Ensure that it's either:\\n\" +\n '- Assigned to a variable\\n' +\n '- Is an object property\\n' +\n '- Is a prop in a JSX element\\n'\n );\n }\n\n return `${displayName}${idx}`;\n};\n\nexport const getTagOwner = (ancestors: Node[]): AnyNode | null => {\n const owner = [...ancestors]\n .reverse()\n .find(\n (node) =>\n node.type === 'Property' ||\n node.type === 'JSXOpeningElement' ||\n node.type === 'VariableDeclarator'\n ) as AnyNode | undefined;\n\n return owner ?? null;\n};\n\nexport const isTagReferenced = (\n program: Program,\n ancestors: Node[]\n): boolean => {\n const owner = getTagOwner(ancestors);\n if (owner?.type !== 'VariableDeclarator') {\n return true;\n }\n\n const { id } = owner;\n if (!isOxcNode(id) || id.type !== 'Identifier') {\n return true;\n }\n\n if (ancestors.some((node) => node.type === 'ExportNamedDeclaration')) {\n return true;\n }\n\n let referenced = false;\n walkOxc(program, (node, parent) => {\n const referenceName =\n node.type === 'Identifier' || node.type === 'JSXIdentifier'\n ? node.name\n : null;\n\n if (\n referenced ||\n referenceName !== id.name ||\n (node.type === 'Identifier' &&\n node.start === id.start &&\n node.end === id.end)\n ) {\n return;\n }\n\n referenced = isNodeReference(node, parent);\n });\n\n return referenced;\n};\n\nexport const collectSameFileProcessorStaticValues = (\n expressionValues: Omit<ExpressionValue, 'buildCodeFrameError'>[],\n processorStaticValuesByLocal: Map<string, unknown>\n): OxcStaticValue[] => {\n const staticValues: OxcStaticValue[] = [];\n const seen = new Set<string>();\n\n expressionValues.forEach((value) => {\n if (value.kind !== ValueType.LAZY) {\n return;\n }\n\n const staticValue = processorStaticValuesByLocal.get(value.source);\n if (\n staticValue === undefined ||\n value.ex.type !== 'Identifier' ||\n seen.has(value.ex.name)\n ) {\n return;\n }\n\n seen.add(value.ex.name);\n staticValues.push({\n name: value.ex.name,\n value: staticValue,\n });\n });\n\n return staticValues;\n};\n"],"file":"displayName.js"}
@@ -0,0 +1,152 @@
1
+ import { ValueType } from "@wyw-in-js/shared";
2
+ import { isNotNull } from "../isNotNull.js";
3
+ import { buildOxcCodeFrameError } from "../oxc/sourceLocations.js";
4
+ import { getMemberName, unwrapQualifiedExpression } from "./processorUsages.js";
5
+ import { GENERATED_HELPER_NAME_RE, getSourceLocation } from "./shared.js";
6
+ export const literalExpressionValue = (expression, code, source, location) => {
7
+ if (expression.type !== "Literal") {
8
+ return null;
9
+ }
10
+ if (expression.value === null || typeof expression.value === "string" || typeof expression.value === "number" || typeof expression.value === "boolean") {
11
+ let type;
12
+ if (expression.value === null) {
13
+ type = "NullLiteral";
14
+ } else if (typeof expression.value === "string") {
15
+ type = "StringLiteral";
16
+ } else if (typeof expression.value === "number") {
17
+ type = "NumericLiteral";
18
+ } else {
19
+ type = "BooleanLiteral";
20
+ }
21
+ const ex = expression.value === null ? {
22
+ loc: location,
23
+ type
24
+ } : {
25
+ loc: location,
26
+ type,
27
+ value: expression.value
28
+ };
29
+ return {
30
+ buildCodeFrameError: (message) => buildOxcCodeFrameError(code, location, message),
31
+ ex,
32
+ kind: ValueType.CONST,
33
+ source,
34
+ value: expression.value
35
+ };
36
+ }
37
+ return null;
38
+ };
39
+ export const expressionValue = (expression, code, loc, filename) => {
40
+ const source = code.slice(expression.start, expression.end);
41
+ const location = getSourceLocation(expression.start, expression.end, loc, filename);
42
+ const literal = literalExpressionValue(expression, code, source, location);
43
+ if (literal) {
44
+ return literal;
45
+ }
46
+ const helperCallName = expression.type === "CallExpression" && expression.arguments.length === 0 && expression.callee.type === "Identifier" && GENERATED_HELPER_NAME_RE.test(expression.callee.name) ? expression.callee.name : null;
47
+ let ex;
48
+ if (expression.type === "Identifier") {
49
+ ex = {
50
+ loc: location,
51
+ name: expression.name,
52
+ type: "Identifier"
53
+ };
54
+ } else if (helperCallName) {
55
+ ex = {
56
+ loc: location,
57
+ name: helperCallName,
58
+ type: "Identifier"
59
+ };
60
+ } else {
61
+ ex = {
62
+ loc: location,
63
+ name: code.slice(expression.start, expression.end),
64
+ type: "Identifier"
65
+ };
66
+ }
67
+ return {
68
+ buildCodeFrameError: (message) => buildOxcCodeFrameError(code, location, message),
69
+ ex,
70
+ kind: expression.type === "ArrowFunctionExpression" || expression.type === "FunctionExpression" ? ValueType.FUNCTION : ValueType.LAZY,
71
+ source
72
+ };
73
+ };
74
+ export const withCurrentExpressionLocation = (value, expression, loc, filename) => {
75
+ const location = getSourceLocation(expression.start, expression.end, loc, filename);
76
+ if (value.kind === ValueType.CONST) {
77
+ return {
78
+ ...value,
79
+ ex: {
80
+ ...value.ex,
81
+ loc: location
82
+ }
83
+ };
84
+ }
85
+ if (value.kind === ValueType.FUNCTION) {
86
+ return {
87
+ ...value,
88
+ ex: {
89
+ ...value.ex,
90
+ loc: location
91
+ }
92
+ };
93
+ }
94
+ return {
95
+ ...value,
96
+ ex: {
97
+ ...value.ex,
98
+ loc: location
99
+ }
100
+ };
101
+ };
102
+ export const shiftExpressionValue = (expressionValues, expression, code, loc, filename) => expressionValues.length > 0 ? withCurrentExpressionLocation(expressionValues.shift(), expression, loc, filename) : expressionValue(expression, code, loc, filename);
103
+ export const zipTemplate = (template, code, loc, filename, expressionValues) => {
104
+ const parts = template.quasi.quasis.flatMap((quasi, idx) => {
105
+ const expression = template.quasi.expressions[idx];
106
+ const templateElement = {
107
+ ...quasi,
108
+ loc: getSourceLocation(quasi.start, quasi.end, loc, filename)
109
+ };
110
+ return [templateElement, expression ? shiftExpressionValue(expressionValues, expression, code, loc, filename) : null].filter(isNotNull);
111
+ });
112
+ return ["template", parts];
113
+ };
114
+ export const buildCalleeParams = (node, code, loc, filename, expressionValues, collapseQualifiedCallee = false) => {
115
+ const expression = unwrapQualifiedExpression(node);
116
+ if (collapseQualifiedCallee && (expression.type === "Identifier" || expression.type === "MemberExpression")) {
117
+ return [["callee", expression]];
118
+ }
119
+ if (expression.type === "Identifier") {
120
+ return [["callee", {
121
+ name: expression.name,
122
+ type: "Identifier"
123
+ }]];
124
+ }
125
+ if (expression.type === "MemberExpression") {
126
+ const params = buildCalleeParams(expression.object, code, loc, filename, expressionValues, collapseQualifiedCallee);
127
+ const member = getMemberName(expression);
128
+ return params && member ? [...params, ["member", member]] : null;
129
+ }
130
+ if (expression.type === "CallExpression") {
131
+ const call = expression;
132
+ const params = buildCalleeParams(call.callee, code, loc, filename, expressionValues, collapseQualifiedCallee);
133
+ if (!params) {
134
+ return null;
135
+ }
136
+ const callValues = call.arguments.filter((arg) => arg.type !== "SpreadElement").map((arg) => shiftExpressionValue(expressionValues, arg, code, loc, filename));
137
+ return [...params, ["call", ...callValues]];
138
+ }
139
+ return null;
140
+ };
141
+ export const buildParams = (usage, code, loc, filename, expressionValues, collapseQualifiedCallee) => {
142
+ const params = buildCalleeParams(usage.callee, code, loc, filename, expressionValues, collapseQualifiedCallee);
143
+ if (!params) {
144
+ return null;
145
+ }
146
+ if (usage.kind === "template") {
147
+ return [...params, zipTemplate(usage.target, code, loc, filename, expressionValues)];
148
+ }
149
+ const callValues = usage.target.arguments.filter((arg) => arg.type !== "SpreadElement").map((arg) => shiftExpressionValue(expressionValues, arg, code, loc, filename));
150
+ return [...params, ["call", ...callValues]];
151
+ };
152
+ //# sourceMappingURL=expressionValues.js.map
@@ -0,0 +1 @@
1
+ {"mappings":"AAEA,SAAS,iBAAiB;AAG1B,SAAS,iBAAiB;AAC1B,SAAS,8BAA8B;AACvC,SAAS,eAAe,iCAAiC;AACzD,SAAS,0BAA0B,yBAAyB;AAO5D,OAAO,MAAM,0BACX,YACA,MACA,QACA,aAC2B;AAC3B,KAAI,WAAW,SAAS,WAAW;AACjC,SAAO;;AAGT,KACE,WAAW,UAAU,QACrB,OAAO,WAAW,UAAU,YAC5B,OAAO,WAAW,UAAU,YAC5B,OAAO,WAAW,UAAU,WAC5B;EACA,IAAI;AAKJ,MAAI,WAAW,UAAU,MAAM;AAC7B,UAAO;aACE,OAAO,WAAW,UAAU,UAAU;AAC/C,UAAO;aACE,OAAO,WAAW,UAAU,UAAU;AAC/C,UAAO;SACF;AACL,UAAO;;EAGT,MAAM,KACJ,WAAW,UAAU,OACjB;GAAE,KAAK;GAAU;GAAM,GACvB;GACE,KAAK;GACL;GACA,OAAO,WAAW;GACnB;AAEP,SAAO;GACL,sBAAsB,YACpB,uBAAuB,MAAM,UAAU,QAAQ;GACjD;GACA,MAAM,UAAU;GAChB;GACA,OAAO,WAAW;GACnB;;AAGH,QAAO;;AAGT,OAAO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AAC,GAAG,QAAQ,CAAC,QAAQ,GAAG,WAAW,CAAC;;AAG7C,QAAO;;AAGT,OAAO,MAAM,eACX,OACA,MACA,KACA,UACA,kBACA,4BACkB;CAClB,MAAM,SAAS,kBACb,MAAM,QACN,MACA,KACA,UACA,kBACA,wBACD;AACD,KAAI,CAAC,QAAQ;AACX,SAAO;;AAGT,KAAI,MAAM,SAAS,YAAY;AAC7B,SAAO,CACL,GAAG,QACH,YAAY,MAAM,QAAQ,MAAM,KAAK,UAAU,iBAAiB,CACjE;;CAGH,MAAM,aAAa,MAAM,OAAO,UAC7B,QAAQ,QAAQ,IAAI,SAAS,gBAAgB,CAC7C,KAAK,QACJ,qBACE,kBACA,KACA,MACA,KACA,SACD,CACF;AAEH,QAAO,CAAC,GAAG,QAAQ,CAAC,QAAQ,GAAG,WAAW,CAAC","names":[],"sources":["../../../src/utils/applyOxcProcessors/expressionValues.ts"],"version":3,"sourcesContent":["import type { Param, Params, SourceLocation } from '@wyw-in-js/processor-utils';\nimport type { ExpressionValue } from '@wyw-in-js/shared';\nimport { ValueType } from '@wyw-in-js/shared';\nimport type { Expression, TaggedTemplateExpression } from 'oxc-parser';\n\nimport { isNotNull } from '../isNotNull';\nimport { buildOxcCodeFrameError } from '../oxc/sourceLocations';\nimport { getMemberName, unwrapQualifiedExpression } from './processorUsages';\nimport { GENERATED_HELPER_NAME_RE, getSourceLocation } from './shared';\nimport type {\n CallExpressionLike,\n LocationLookup,\n ProcessorUsage,\n} from './types';\n\nexport const literalExpressionValue = (\n expression: Expression,\n code: string,\n source: string,\n location: SourceLocation\n): ExpressionValue | null => {\n if (expression.type !== 'Literal') {\n return null;\n }\n\n if (\n expression.value === null ||\n typeof expression.value === 'string' ||\n typeof expression.value === 'number' ||\n typeof expression.value === 'boolean'\n ) {\n let type:\n | 'BooleanLiteral'\n | 'NullLiteral'\n | 'NumericLiteral'\n | 'StringLiteral';\n if (expression.value === null) {\n type = 'NullLiteral';\n } else if (typeof expression.value === 'string') {\n type = 'StringLiteral';\n } else if (typeof expression.value === 'number') {\n type = 'NumericLiteral';\n } else {\n type = 'BooleanLiteral';\n }\n\n const ex =\n expression.value === null\n ? { loc: location, type }\n : {\n loc: location,\n type,\n value: expression.value,\n };\n\n return {\n buildCodeFrameError: (message: string) =>\n buildOxcCodeFrameError(code, location, message),\n ex,\n kind: ValueType.CONST,\n source,\n value: expression.value,\n } as ExpressionValue;\n }\n\n return null;\n};\n\nexport const expressionValue = (\n expression: Expression,\n code: string,\n loc: LocationLookup,\n filename?: string | null\n): ExpressionValue => {\n const source = code.slice(expression.start, expression.end);\n const location = getSourceLocation(\n expression.start,\n expression.end,\n loc,\n filename\n );\n const literal = literalExpressionValue(expression, code, source, location);\n if (literal) {\n return literal;\n }\n\n const helperCallName =\n expression.type === 'CallExpression' &&\n expression.arguments.length === 0 &&\n expression.callee.type === 'Identifier' &&\n GENERATED_HELPER_NAME_RE.test(expression.callee.name)\n ? expression.callee.name\n : null;\n\n let ex: ExpressionValue['ex'];\n if (expression.type === 'Identifier') {\n ex = { loc: location, name: expression.name, type: 'Identifier' };\n } else if (helperCallName) {\n ex = { loc: location, name: helperCallName, type: 'Identifier' };\n } else {\n ex = {\n loc: location,\n name: code.slice(expression.start, expression.end),\n type: 'Identifier',\n };\n }\n\n return {\n buildCodeFrameError: (message: string) =>\n buildOxcCodeFrameError(code, location, message),\n ex,\n kind:\n expression.type === 'ArrowFunctionExpression' ||\n expression.type === 'FunctionExpression'\n ? ValueType.FUNCTION\n : ValueType.LAZY,\n source,\n } as ExpressionValue;\n};\n\nexport const withCurrentExpressionLocation = (\n value: ExpressionValue,\n expression: Expression,\n loc: LocationLookup,\n filename?: string | null\n): ExpressionValue => {\n const location = getSourceLocation(\n expression.start,\n expression.end,\n loc,\n filename\n );\n\n if (value.kind === ValueType.CONST) {\n return {\n ...value,\n ex: {\n ...value.ex,\n loc: location,\n },\n };\n }\n\n if (value.kind === ValueType.FUNCTION) {\n return {\n ...value,\n ex: {\n ...value.ex,\n loc: location,\n },\n };\n }\n\n return {\n ...value,\n ex: {\n ...value.ex,\n loc: location,\n },\n };\n};\n\nexport const shiftExpressionValue = (\n expressionValues: ExpressionValue[],\n expression: Expression,\n code: string,\n loc: LocationLookup,\n filename?: string | null\n): ExpressionValue =>\n expressionValues.length > 0\n ? withCurrentExpressionLocation(\n expressionValues.shift()!,\n expression,\n loc,\n filename\n )\n : expressionValue(expression, code, loc, filename);\n\nexport const zipTemplate = (\n template: TaggedTemplateExpression,\n code: string,\n loc: LocationLookup,\n filename: string | null | undefined,\n expressionValues: ExpressionValue[]\n): Param => {\n const parts = template.quasi.quasis.flatMap((quasi, idx) => {\n const expression = template.quasi.expressions[idx];\n const templateElement = {\n ...quasi,\n loc: getSourceLocation(quasi.start, quasi.end, loc, filename),\n };\n\n return [\n templateElement,\n expression\n ? shiftExpressionValue(\n expressionValues,\n expression as Expression,\n code,\n loc,\n filename\n )\n : null,\n ].filter(isNotNull);\n });\n\n return ['template', parts] as Param;\n};\n\nexport const buildCalleeParams = (\n node: Expression,\n code: string,\n loc: LocationLookup,\n filename: string | null | undefined,\n expressionValues: ExpressionValue[],\n collapseQualifiedCallee = false\n): Params | null => {\n const expression = unwrapQualifiedExpression(node);\n\n if (\n collapseQualifiedCallee &&\n (expression.type === 'Identifier' || expression.type === 'MemberExpression')\n ) {\n return [['callee', expression] as Param];\n }\n\n if (expression.type === 'Identifier') {\n return [['callee', { name: expression.name, type: 'Identifier' }]];\n }\n\n if (expression.type === 'MemberExpression') {\n const params = buildCalleeParams(\n expression.object,\n code,\n loc,\n filename,\n expressionValues,\n collapseQualifiedCallee\n );\n const member = getMemberName(expression);\n return params && member ? [...params, ['member', member]] : null;\n }\n\n if (expression.type === 'CallExpression') {\n const call = expression as CallExpressionLike;\n const params = buildCalleeParams(\n call.callee,\n code,\n loc,\n filename,\n expressionValues,\n collapseQualifiedCallee\n );\n if (!params) {\n return null;\n }\n\n const callValues = call.arguments\n .filter((arg) => arg.type !== 'SpreadElement')\n .map((arg) =>\n shiftExpressionValue(\n expressionValues,\n arg as Expression,\n code,\n loc,\n filename\n )\n );\n\n return [...params, ['call', ...callValues]];\n }\n\n return null;\n};\n\nexport const buildParams = (\n usage: ProcessorUsage,\n code: string,\n loc: LocationLookup,\n filename: string | null | undefined,\n expressionValues: ExpressionValue[],\n collapseQualifiedCallee: boolean\n): Params | null => {\n const params = buildCalleeParams(\n usage.callee,\n code,\n loc,\n filename,\n expressionValues,\n collapseQualifiedCallee\n );\n if (!params) {\n return null;\n }\n\n if (usage.kind === 'template') {\n return [\n ...params,\n zipTemplate(usage.target, code, loc, filename, expressionValues),\n ];\n }\n\n const callValues = usage.target.arguments\n .filter((arg) => arg.type !== 'SpreadElement')\n .map((arg) =>\n shiftExpressionValue(\n expressionValues,\n arg as Expression,\n code,\n loc,\n filename\n )\n );\n\n return [...params, ['call', ...callValues]];\n};\n"],"file":"expressionValues.js"}
@@ -0,0 +1,62 @@
1
+ import { BaseProcessor, expressionToCode } from "@wyw-in-js/processor-utils";
2
+ import { createOxcAstService } from "../oxcAstService.js";
3
+ import { buildOxcCodeFrameError } from "../oxc/sourceLocations.js";
4
+ import { getDisplayName } from "./displayName.js";
5
+ import { getRootIdentifier } from "./processorUsages.js";
6
+ import { getSourceLocation } from "./shared.js";
7
+ let didWarnSkipSymbolMismatch = false;
8
+ export const isReplacementPure = (replacement) => replacement.type === "CallExpression";
9
+ export const shouldCollectStaticExpressionValues = (options) => (options.eval?.strategy ?? "hybrid") !== "execute";
10
+ export const createProcessor = (definedProcessor, params, target, replacementTarget, ancestors, fileContext, options, code, loc, idx, isReferenced, usedNames, replacements) => {
11
+ const [Processor, tagSource] = definedProcessor;
12
+ const astService = createOxcAstService(usedNames);
13
+ const replacer = (replacement, isPure) => {
14
+ const next = typeof replacement === "function" ? replacement(target) : replacement;
15
+ const replacementCode = expressionToCode(next);
16
+ replacements.push({
17
+ start: replacementTarget.start,
18
+ end: replacementTarget.end,
19
+ value: isPure && isReplacementPure(next) ? `/*#__PURE__*/${replacementCode}` : replacementCode
20
+ });
21
+ };
22
+ try {
23
+ let displayName;
24
+ try {
25
+ displayName = getDisplayName(ancestors, idx, code, fileContext.filename);
26
+ } catch (error) {
27
+ if (error instanceof Error && error.message.startsWith("Couldn't determine a name for the component")) {
28
+ let displayNameNode = target;
29
+ if (target.type === "TaggedTemplateExpression") {
30
+ displayNameNode = target.tag;
31
+ } else if (target.type === "CallExpression") {
32
+ displayNameNode = target.callee;
33
+ }
34
+ const pointerNode = displayNameNode.type === "MemberExpression" ? getRootIdentifier(displayNameNode) ?? displayNameNode : displayNameNode;
35
+ throw buildOxcCodeFrameError(code, getSourceLocation(pointerNode.start, pointerNode.end, loc, fileContext.filename), error.message);
36
+ }
37
+ throw error;
38
+ }
39
+ return {
40
+ astService,
41
+ processor: new Processor(params, tagSource, astService, getSourceLocation(target.start, target.end, loc, fileContext.filename), replacer, displayName, isReferenced, idx, options, fileContext)
42
+ };
43
+ } catch (e) {
44
+ if (e === BaseProcessor.SKIP) {
45
+ return null;
46
+ }
47
+ if (typeof e === "symbol" && e.description === BaseProcessor.SKIP.description) {
48
+ if (!didWarnSkipSymbolMismatch) {
49
+ didWarnSkipSymbolMismatch = true;
50
+ // eslint-disable-next-line no-console
51
+ console.warn([
52
+ "[wyw-in-js] Processor threw Symbol('skip') that does not match BaseProcessor.SKIP identity.",
53
+ "This usually means duplicate copies of @wyw-in-js/processor-utils (or the processor) are bundled/installed.",
54
+ "Consider deduping dependencies to avoid subtle issues (instanceof checks, sentinels, etc)."
55
+ ].join("\n"));
56
+ }
57
+ return null;
58
+ }
59
+ throw e;
60
+ }
61
+ };
62
+ //# sourceMappingURL=processorFactory.js.map
@@ -0,0 +1 @@
1
+ {"mappings":"AAAA,SAAS,eAAe,wBAAwB;AAShD,SAAS,2BAA2B;AACpC,SAAS,8BAA8B;AACvC,SAAS,sBAAsB;AAC/B,SAAS,yBAAyB;AAClC,SAAS,yBAAyB;AAQlC,IAAI,4BAA4B;AAChC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B;AAC9B,gCAA4B;;AAE5B,YAAQ,KACN;KACE;KACA;KACA;KACD,CAAC,KAAK,KAAK,CACb;;AAGH,UAAO;;AAGT,QAAM","names":[],"sources":["../../../src/utils/applyOxcProcessors/processorFactory.ts"],"version":3,"sourcesContent":["import { BaseProcessor, expressionToCode } from '@wyw-in-js/processor-utils';\nimport type {\n Expression as ProcessorExpression,\n IFileContext,\n Params,\n} from '@wyw-in-js/processor-utils';\nimport type { StrictOptions } from '@wyw-in-js/shared';\nimport type { Expression, Node } from 'oxc-parser';\n\nimport { createOxcAstService } from '../oxcAstService';\nimport { buildOxcCodeFrameError } from '../oxc/sourceLocations';\nimport { getDisplayName } from './displayName';\nimport { getRootIdentifier } from './processorUsages';\nimport { getSourceLocation } from './shared';\nimport type {\n CreatedProcessor,\n DefinedProcessor,\n LocationLookup,\n Replacement,\n} from './types';\n\nlet didWarnSkipSymbolMismatch = false;\nexport const isReplacementPure = (replacement: ProcessorExpression): boolean =>\n replacement.type === 'CallExpression';\n\nexport const shouldCollectStaticExpressionValues = (\n options: Pick<StrictOptions, 'eval'>\n): boolean => (options.eval?.strategy ?? 'hybrid') !== 'execute';\n\nexport const createProcessor = (\n definedProcessor: DefinedProcessor,\n params: Params,\n target: Expression,\n replacementTarget: Expression,\n ancestors: Node[],\n fileContext: IFileContext,\n options: Pick<\n StrictOptions,\n 'classNameSlug' | 'displayName' | 'extensions' | 'tagResolver'\n >,\n code: string,\n loc: LocationLookup,\n idx: number,\n isReferenced: boolean,\n usedNames: Set<string>,\n replacements: Replacement[]\n): CreatedProcessor | null => {\n const [Processor, tagSource] = definedProcessor;\n const astService = createOxcAstService(usedNames);\n\n const replacer = (\n replacement:\n | ProcessorExpression\n | ((tagPath: unknown) => ProcessorExpression),\n isPure: boolean\n ) => {\n const next =\n typeof replacement === 'function' ? replacement(target) : replacement;\n const replacementCode = expressionToCode(next);\n replacements.push({\n start: replacementTarget.start,\n end: replacementTarget.end,\n value:\n isPure && isReplacementPure(next)\n ? `/*#__PURE__*/${replacementCode}`\n : replacementCode,\n });\n };\n\n try {\n let displayName: string;\n try {\n displayName = getDisplayName(ancestors, idx, code, fileContext.filename);\n } catch (error) {\n if (\n error instanceof Error &&\n error.message.startsWith(\"Couldn't determine a name for the component\")\n ) {\n let displayNameNode: Node = target;\n if (target.type === 'TaggedTemplateExpression') {\n displayNameNode = target.tag;\n } else if (target.type === 'CallExpression') {\n displayNameNode = target.callee;\n }\n const pointerNode =\n displayNameNode.type === 'MemberExpression'\n ? getRootIdentifier(displayNameNode) ?? displayNameNode\n : displayNameNode;\n throw buildOxcCodeFrameError(\n code,\n getSourceLocation(\n pointerNode.start,\n pointerNode.end,\n loc,\n fileContext.filename\n ),\n error.message\n );\n }\n throw error;\n }\n\n return {\n astService,\n processor: new Processor(\n params,\n tagSource,\n astService,\n getSourceLocation(target.start, target.end, loc, fileContext.filename),\n replacer,\n displayName,\n isReferenced,\n idx,\n options,\n fileContext\n ),\n };\n } catch (e) {\n if (e === BaseProcessor.SKIP) {\n return null;\n }\n\n if (\n typeof e === 'symbol' &&\n e.description === BaseProcessor.SKIP.description\n ) {\n if (!didWarnSkipSymbolMismatch) {\n didWarnSkipSymbolMismatch = true;\n // eslint-disable-next-line no-console\n console.warn(\n [\n \"[wyw-in-js] Processor threw Symbol('skip') that does not match BaseProcessor.SKIP identity.\",\n 'This usually means duplicate copies of @wyw-in-js/processor-utils (or the processor) are bundled/installed.',\n 'Consider deduping dependencies to avoid subtle issues (instanceof checks, sentinels, etc).',\n ].join('\\n')\n );\n }\n\n return null;\n }\n\n throw e;\n }\n};\n"],"file":"processorFactory.js"}
@@ -0,0 +1,143 @@
1
+ import { getOxcNodeChildren } from "../oxc/ast.js";
2
+ export const getMemberName = (node) => {
3
+ if (node.computed) {
4
+ return node.property.type === "Literal" && typeof node.property.value === "string" ? node.property.value : null;
5
+ }
6
+ return node.property.type === "Identifier" ? node.property.name : null;
7
+ };
8
+ export const unwrapQualifiedExpression = (node) => {
9
+ if (node.type === "TSAsExpression" || node.type === "TSSatisfiesExpression" || node.type === "TSNonNullExpression" || node.type === "TSTypeAssertion" || node.type === "ParenthesizedExpression") {
10
+ return unwrapQualifiedExpression(node.expression);
11
+ }
12
+ if (node.type === "SequenceExpression") {
13
+ const sequence = node;
14
+ return unwrapQualifiedExpression(sequence.expressions[sequence.expressions.length - 1] ?? node);
15
+ }
16
+ return node;
17
+ };
18
+ export const getRootIdentifier = (node) => {
19
+ const expression = unwrapQualifiedExpression(node);
20
+ if (expression.type === "Identifier") {
21
+ return expression;
22
+ }
23
+ if (expression.type === "MemberExpression") {
24
+ return getRootIdentifier(expression.object);
25
+ }
26
+ if (expression.type === "CallExpression") {
27
+ return getRootIdentifier(expression.callee);
28
+ }
29
+ return null;
30
+ };
31
+ export const getQualifiedName = (node) => {
32
+ const expression = unwrapQualifiedExpression(node);
33
+ if (expression.type === "Identifier") {
34
+ return expression.name;
35
+ }
36
+ if (expression.type === "MemberExpression") {
37
+ const object = getQualifiedName(expression.object);
38
+ const member = getMemberName(expression);
39
+ return object && member ? `${object}.${member}` : null;
40
+ }
41
+ if (expression.type === "CallExpression") {
42
+ return getQualifiedName(expression.callee);
43
+ }
44
+ return null;
45
+ };
46
+ export const resolveDefinedProcessor = (callee, definedProcessors) => {
47
+ const qualified = getQualifiedName(callee);
48
+ if (qualified) {
49
+ const definedProcessor = definedProcessors.get(qualified);
50
+ if (definedProcessor) {
51
+ return {
52
+ collapseQualifiedCallee: qualified.includes("."),
53
+ definedProcessor
54
+ };
55
+ }
56
+ }
57
+ const root = getRootIdentifier(callee);
58
+ if (!root) {
59
+ return null;
60
+ }
61
+ const definedProcessor = definedProcessors.get(root.name);
62
+ return definedProcessor ? {
63
+ collapseQualifiedCallee: false,
64
+ definedProcessor
65
+ } : null;
66
+ };
67
+ export const isCallTagOfTaggedTemplate = (node, parent) => parent?.type === "TaggedTemplateExpression" && parent.tag === node;
68
+ export const expandReplacementTarget = (target, ancestors) => {
69
+ let current = target;
70
+ for (let idx = ancestors.length - 1; idx >= 0; idx -= 1) {
71
+ const ancestor = ancestors[idx];
72
+ if (ancestor.type === "SequenceExpression" && ancestor.expressions[ancestor.expressions.length - 1] === current) {
73
+ current = ancestor;
74
+ } else if (ancestor.type === "ParenthesizedExpression" && ancestor.expression === current) {
75
+ current = ancestor;
76
+ } else {
77
+ break;
78
+ }
79
+ }
80
+ return current;
81
+ };
82
+ export const collectProcessorUsages = (program, definedProcessors) => {
83
+ const usages = [];
84
+ const walk = (node, ancestors, parent = null) => {
85
+ if (node.type === "TaggedTemplateExpression") {
86
+ const callee = node.tag;
87
+ const resolvedProcessor = resolveDefinedProcessor(callee, definedProcessors);
88
+ if (resolvedProcessor) {
89
+ usages.push({
90
+ ancestors,
91
+ callee,
92
+ collapseQualifiedCallee: resolvedProcessor.collapseQualifiedCallee,
93
+ definedProcessor: resolvedProcessor.definedProcessor,
94
+ kind: "template",
95
+ replacementTarget: expandReplacementTarget(node, ancestors),
96
+ target: node
97
+ });
98
+ }
99
+ } else if (node.type === "CallExpression" && !isCallTagOfTaggedTemplate(node, parent)) {
100
+ const { callee } = node;
101
+ const resolvedProcessor = resolveDefinedProcessor(callee, definedProcessors);
102
+ if (resolvedProcessor) {
103
+ usages.push({
104
+ ancestors,
105
+ callee,
106
+ collapseQualifiedCallee: resolvedProcessor.collapseQualifiedCallee,
107
+ definedProcessor: resolvedProcessor.definedProcessor,
108
+ kind: "call",
109
+ replacementTarget: expandReplacementTarget(node, ancestors),
110
+ target: node
111
+ });
112
+ }
113
+ }
114
+ getOxcNodeChildren(node).forEach((child) => walk(child, [...ancestors, node], node));
115
+ };
116
+ walk(program, []);
117
+ return usages.sort((a, b) => a.target.start - b.target.start);
118
+ };
119
+ export const expressionSpan = (expression) => ({
120
+ end: expression.end,
121
+ start: expression.start
122
+ });
123
+ export const collectCallArgumentSpans = (node) => {
124
+ const expression = unwrapQualifiedExpression(node);
125
+ if (expression.type === "CallExpression") {
126
+ const call = expression;
127
+ const calleeSpans = collectCallArgumentSpans(call.callee);
128
+ const argumentSpans = call.arguments.flatMap((arg) => arg.type === "SpreadElement" ? [] : [expressionSpan(arg)]);
129
+ return [...calleeSpans, ...argumentSpans];
130
+ }
131
+ if (expression.type === "MemberExpression") {
132
+ return collectCallArgumentSpans(expression.object);
133
+ }
134
+ return [];
135
+ };
136
+ export const collectUsageExpressionSpans = (usage) => {
137
+ const calleeSpans = collectCallArgumentSpans(usage.callee);
138
+ if (usage.kind === "template") {
139
+ return [...calleeSpans, ...usage.target.quasi.expressions.map((expression) => expressionSpan(expression))];
140
+ }
141
+ return [...calleeSpans, ...usage.target.arguments.flatMap((arg) => arg.type === "SpreadElement" ? [] : [expressionSpan(arg)])];
142
+ };
143
+ //# sourceMappingURL=processorUsages.js.map
@@ -0,0 +1 @@
1
+ {"mappings":"AAQA,SAAS,0BAA0B;AAWnC,OAAO,MAAM,iBAAiB,SAA0C;AACtE,KAAI,KAAK,UAAU;AACjB,SAAO,KAAK,SAAS,SAAS,aAC5B,OAAO,KAAK,SAAS,UAAU,WAC7B,KAAK,SAAS,QACd;;AAGN,QAAO,KAAK,SAAS,SAAS,eAAe,KAAK,SAAS,OAAO;;AAGpE,OAAO,MAAM,6BAA6B,SAAiC;AACzE,KACE,KAAK,SAAS,oBACd,KAAK,SAAS,2BACd,KAAK,SAAS,yBACd,KAAK,SAAS,qBACd,KAAK,SAAS,2BACd;AACA,SAAO,0BACJ,KAA0D,WAC5D;;AAGH,KAAI,KAAK,SAAS,sBAAsB;EACtC,MAAM,WAAW;AACjB,SAAO,0BACL,SAAS,YAAY,SAAS,YAAY,SAAS,MAAM,KAC1D;;AAGH,QAAO;;AAGT,OAAO,MAAM,qBAAqB,SAA2C;CAC3E,MAAM,aAAa,0BAA0B,KAAK;AAElD,KAAI,WAAW,SAAS,cAAc;AACpC,SAAO;;AAGT,KAAI,WAAW,SAAS,oBAAoB;AAC1C,SAAO,kBAAkB,WAAW,OAAO;;AAG7C,KAAI,WAAW,SAAS,kBAAkB;A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AO,MAAM,+BACX,UACqB;CACrB,MAAM,cAAc,yBAAyB,MAAM,OAAO;AAC1D,KAAI,MAAM,SAAS,YAAY;AAC7B,SAAO,CACL,GAAG,aACH,GAAG,MAAM,OAAO,MAAM,YAAY,KAAK,eACrC,eAAe,WAAyB,CACzC,CACF;;AAGH,QAAO,CACL,GAAG,aACH,GAAG,MAAM,OAAO,UAAU,SAAS,QACjC,IAAI,SAAS,kBAAkB,EAAE,GAAG,CAAC,eAAe,IAAkB,CAAC,CACxE,CACF","names":[],"sources":["../../../src/utils/applyOxcProcessors/processorUsages.ts"],"version":3,"sourcesContent":["import type {\n CallExpression,\n Expression,\n MemberExpression,\n Node,\n Program,\n} from 'oxc-parser';\n\nimport { getOxcNodeChildren } from '../oxc/ast';\nimport type {\n CallExpressionLike,\n DefinedProcessor,\n ExpressionSpan,\n OxcIdentifier,\n ProcessorUsage,\n QualifiedExpression,\n SequenceExpressionLike,\n} from './types';\n\nexport const getMemberName = (node: MemberExpression): string | null => {\n if (node.computed) {\n return node.property.type === 'Literal' &&\n typeof node.property.value === 'string'\n ? node.property.value\n : null;\n }\n\n return node.property.type === 'Identifier' ? node.property.name : null;\n};\n\nexport const unwrapQualifiedExpression = (node: Expression): Expression => {\n if (\n node.type === 'TSAsExpression' ||\n node.type === 'TSSatisfiesExpression' ||\n node.type === 'TSNonNullExpression' ||\n node.type === 'TSTypeAssertion' ||\n node.type === 'ParenthesizedExpression'\n ) {\n return unwrapQualifiedExpression(\n (node as QualifiedExpression & { expression: Expression }).expression\n );\n }\n\n if (node.type === 'SequenceExpression') {\n const sequence = node as SequenceExpressionLike;\n return unwrapQualifiedExpression(\n sequence.expressions[sequence.expressions.length - 1] ?? node\n );\n }\n\n return node;\n};\n\nexport const getRootIdentifier = (node: Expression): OxcIdentifier | null => {\n const expression = unwrapQualifiedExpression(node);\n\n if (expression.type === 'Identifier') {\n return expression;\n }\n\n if (expression.type === 'MemberExpression') {\n return getRootIdentifier(expression.object);\n }\n\n if (expression.type === 'CallExpression') {\n return getRootIdentifier((expression as CallExpressionLike).callee);\n }\n\n return null;\n};\n\nexport const getQualifiedName = (node: Expression): string | null => {\n const expression = unwrapQualifiedExpression(node);\n\n if (expression.type === 'Identifier') {\n return expression.name;\n }\n\n if (expression.type === 'MemberExpression') {\n const object = getQualifiedName(expression.object);\n const member = getMemberName(expression);\n return object && member ? `${object}.${member}` : null;\n }\n\n if (expression.type === 'CallExpression') {\n return getQualifiedName((expression as CallExpressionLike).callee);\n }\n\n return null;\n};\n\nexport const resolveDefinedProcessor = (\n callee: Expression,\n definedProcessors: Map<string, DefinedProcessor>\n): {\n collapseQualifiedCallee: boolean;\n definedProcessor: DefinedProcessor;\n} | null => {\n const qualified = getQualifiedName(callee);\n if (qualified) {\n const definedProcessor = definedProcessors.get(qualified);\n if (definedProcessor) {\n return {\n collapseQualifiedCallee: qualified.includes('.'),\n definedProcessor,\n };\n }\n }\n\n const root = getRootIdentifier(callee);\n if (!root) {\n return null;\n }\n\n const definedProcessor = definedProcessors.get(root.name);\n return definedProcessor\n ? {\n collapseQualifiedCallee: false,\n definedProcessor,\n }\n : null;\n};\n\nexport const isCallTagOfTaggedTemplate = (\n node: Node,\n parent: Node | null\n): boolean =>\n parent?.type === 'TaggedTemplateExpression' && parent.tag === node;\n\nexport const expandReplacementTarget = (\n target: Expression,\n ancestors: Node[]\n): Expression => {\n let current: Expression = target;\n\n for (let idx = ancestors.length - 1; idx >= 0; idx -= 1) {\n const ancestor = ancestors[idx];\n if (\n ancestor.type === 'SequenceExpression' &&\n ancestor.expressions[ancestor.expressions.length - 1] === current\n ) {\n current = ancestor as Expression;\n } else if (\n ancestor.type === 'ParenthesizedExpression' &&\n ancestor.expression === current\n ) {\n current = ancestor as Expression;\n } else {\n break;\n }\n }\n\n return current;\n};\n\nexport const collectProcessorUsages = (\n program: Program,\n definedProcessors: Map<string, DefinedProcessor>\n): ProcessorUsage[] => {\n const usages: ProcessorUsage[] = [];\n\n const walk = (\n node: Node,\n ancestors: Node[],\n parent: Node | null = null\n ): void => {\n if (node.type === 'TaggedTemplateExpression') {\n const callee = node.tag as Expression;\n const resolvedProcessor = resolveDefinedProcessor(\n callee,\n definedProcessors\n );\n if (resolvedProcessor) {\n usages.push({\n ancestors,\n callee,\n collapseQualifiedCallee: resolvedProcessor.collapseQualifiedCallee,\n definedProcessor: resolvedProcessor.definedProcessor,\n kind: 'template',\n replacementTarget: expandReplacementTarget(node, ancestors),\n target: node,\n });\n }\n } else if (\n node.type === 'CallExpression' &&\n !isCallTagOfTaggedTemplate(node, parent)\n ) {\n const { callee } = node as CallExpressionLike;\n const resolvedProcessor = resolveDefinedProcessor(\n callee,\n definedProcessors\n );\n if (resolvedProcessor) {\n usages.push({\n ancestors,\n callee,\n collapseQualifiedCallee: resolvedProcessor.collapseQualifiedCallee,\n definedProcessor: resolvedProcessor.definedProcessor,\n kind: 'call',\n replacementTarget: expandReplacementTarget(\n node as CallExpression,\n ancestors\n ),\n target: node as CallExpression,\n });\n }\n }\n\n getOxcNodeChildren(node).forEach((child) =>\n walk(child, [...ancestors, node], node)\n );\n };\n\n walk(program, []);\n\n return usages.sort((a, b) => a.target.start - b.target.start);\n};\n\nexport const expressionSpan = (expression: Expression): ExpressionSpan => ({\n end: expression.end,\n start: expression.start,\n});\n\nexport const collectCallArgumentSpans = (\n node: Expression\n): ExpressionSpan[] => {\n const expression = unwrapQualifiedExpression(node);\n\n if (expression.type === 'CallExpression') {\n const call = expression as CallExpressionLike;\n const calleeSpans = collectCallArgumentSpans(call.callee);\n const argumentSpans = call.arguments.flatMap((arg) =>\n arg.type === 'SpreadElement' ? [] : [expressionSpan(arg as Expression)]\n );\n return [...calleeSpans, ...argumentSpans];\n }\n\n if (expression.type === 'MemberExpression') {\n return collectCallArgumentSpans(expression.object);\n }\n\n return [];\n};\n\nexport const collectUsageExpressionSpans = (\n usage: ProcessorUsage\n): ExpressionSpan[] => {\n const calleeSpans = collectCallArgumentSpans(usage.callee);\n if (usage.kind === 'template') {\n return [\n ...calleeSpans,\n ...usage.target.quasi.expressions.map((expression) =>\n expressionSpan(expression as Expression)\n ),\n ];\n }\n\n return [\n ...calleeSpans,\n ...usage.target.arguments.flatMap((arg) =>\n arg.type === 'SpreadElement' ? [] : [expressionSpan(arg as Expression)]\n ),\n ];\n};\n"],"file":"processorUsages.js"}