@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,496 @@
1
+ import { ValueType } from "@wyw-in-js/shared";
2
+ import { applyOxcReplacements } from "../oxc/replacements.js";
3
+ import { createOxcLocationLookup } from "../oxc/sourceLocations.js";
4
+ import { analyzeProgram, containsTaggedTemplateExpression, createSpanLookup, findReferences, getSourceLocation, isBindingDeclaredWithin, parseOxc, resolveBindingAt } from "./scopeAnalysis.js";
5
+ import { applyExpressionReplacements, collectIdentifierReferenceReplacements, collectStaticNamespaceMemberReferences, getConstantReplacement, replaceIdentifierReferences } from "./expressionReplacements.js";
6
+ import { cloneStaticValue, evaluateStatic, isStaticSerializableValue, literalCode } from "./staticEvaluator.js";
7
+ const allocateExpressionName = (ctx) => {
8
+ let base = "_exp";
9
+ let idx = 1;
10
+ while (ctx.usedNames.has(base)) {
11
+ idx += 1;
12
+ base = `_exp${idx}`;
13
+ }
14
+ ctx.usedNames.add(base);
15
+ return base;
16
+ };
17
+ const hoistedBindingKey = (binding) => `${binding.scope.start}:${binding.scope.end}:${binding.declaredAt}:${binding.name}`;
18
+ const allocateHoistedBindingName = (originalName, ctx) => {
19
+ const sanitized = originalName.replace(/[^A-Za-z0-9_$]/g, "_") || "hoisted";
20
+ const base = /^[A-Za-z_$]/.test(sanitized) ? `_${sanitized}` : "_hoisted";
21
+ let candidate = base;
22
+ let idx = 2;
23
+ while (ctx.usedNames.has(candidate)) {
24
+ candidate = `${base}${idx}`;
25
+ idx += 1;
26
+ }
27
+ ctx.usedNames.add(candidate);
28
+ return candidate;
29
+ };
30
+ const getHoistedBindingName = (binding, ctx) => {
31
+ const key = hoistedBindingKey(binding);
32
+ const existing = ctx.hoistedBindingNames.get(key);
33
+ if (existing) {
34
+ return existing;
35
+ }
36
+ const next = allocateHoistedBindingName(binding.name, ctx);
37
+ ctx.hoistedBindingNames.set(key, next);
38
+ return next;
39
+ };
40
+ const parenthesizeStaticReplacement = (source) => `(${source})`;
41
+ const replaceStaticLocalReferences = (expression, replacements, ctx, extraReplacements = []) => {
42
+ if (expression.type === "Identifier" && extraReplacements.length === 0) {
43
+ return replacements.get(expression.name) ?? ctx.code.slice(expression.start, expression.end);
44
+ }
45
+ const parenthesized = new Map();
46
+ replacements.forEach((value, key) => {
47
+ parenthesized.set(key, parenthesizeStaticReplacement(value));
48
+ });
49
+ return applyExpressionReplacements(expression, [...extraReplacements, ...collectIdentifierReferenceReplacements(expression, parenthesized)], ctx.code);
50
+ };
51
+ const collectStaticLocalExpression = (expression, ctx, stack = []) => {
52
+ const replacements = new Map();
53
+ const importedFrom = new Set();
54
+ const imports = [];
55
+ for (const { name, start } of findReferences(expression, ctx.referencesByNode)) {
56
+ const binding = resolveBindingAt(ctx, name, start);
57
+ if (!binding) {
58
+ return null;
59
+ }
60
+ if (binding.importedFrom) {
61
+ importedFrom.add(binding.importedFrom);
62
+ if (binding.imported && binding.imported !== "*") {
63
+ imports.push({
64
+ imported: binding.imported,
65
+ local: binding.name,
66
+ source: binding.importedFrom
67
+ });
68
+ continue;
69
+ }
70
+ return null;
71
+ }
72
+ const replacement = getConstantReplacement(binding, ctx);
73
+ if (replacement) {
74
+ replacements.set(name, replacement);
75
+ continue;
76
+ }
77
+ if (binding.kind === "param" || binding.declarationKind !== "const" || !binding.declarator?.init || binding.declarator.id.type !== "Identifier") {
78
+ return null;
79
+ }
80
+ // Processor-managed bindings (const x = css``) carry their value
81
+ // (the generated className string) via inlineConstants at candidate
82
+ // evaluation time. Walking the TaggedTemplateExpression here would
83
+ // pull the processor's tag import (e.g. `css` from '@linaria/core')
84
+ // into the candidate's static imports, where it fails to resolve.
85
+ // Leave the identifier as a free reference; the candidate-side env
86
+ // supplies the className.
87
+ if (binding.declarator.init.type === "TaggedTemplateExpression") {
88
+ continue;
89
+ }
90
+ const key = hoistedBindingKey(binding);
91
+ if (stack.includes(key)) {
92
+ return null;
93
+ }
94
+ const nested = collectStaticLocalExpression(binding.declarator.init, ctx, [...stack, key]);
95
+ if (!nested) {
96
+ return null;
97
+ }
98
+ replacements.set(name, nested.source);
99
+ nested.importedFrom.forEach((source) => importedFrom.add(source));
100
+ imports.push(...nested.imports);
101
+ }
102
+ return {
103
+ importedFrom: [...importedFrom],
104
+ imports,
105
+ source: replacements.size > 0 ? replaceStaticLocalReferences(expression, replacements, ctx) : ctx.code.slice(expression.start, expression.end)
106
+ };
107
+ };
108
+ const declarationInitCode = (init, ctx) => {
109
+ const renamedDependencies = new Map();
110
+ findReferences(init, ctx.referencesByNode).forEach(({ name, start }) => {
111
+ const dependency = resolveBindingAt(ctx, name, start);
112
+ if (!dependency || dependency.importedFrom || dependency.isRoot || dependency.declarator?.id.type !== "Identifier") {
113
+ return;
114
+ }
115
+ renamedDependencies.set(name, getHoistedBindingName(dependency, ctx));
116
+ });
117
+ return renamedDependencies.size > 0 ? replaceIdentifierReferences(init, renamedDependencies, ctx.code) : ctx.code.slice(init.start, init.end);
118
+ };
119
+ const addHoistedCode = (key, code, ctx) => {
120
+ if (ctx.hoistedDeclarations.has(key)) {
121
+ return;
122
+ }
123
+ ctx.hoistedDeclarations.set(key, code);
124
+ const declarations = ctx.hoistedDeclarationsByInsertionPoint.get(ctx.currentInsertionPoint) ?? [];
125
+ declarations.push(code);
126
+ ctx.hoistedDeclarationsByInsertionPoint.set(ctx.currentInsertionPoint, declarations);
127
+ };
128
+ const declarationCode = (binding, ctx) => {
129
+ const { declarator } = binding;
130
+ if (!declarator) {
131
+ return "";
132
+ }
133
+ const { id } = declarator;
134
+ if (id.type !== "Identifier") {
135
+ const idCode = ctx.code.slice(id.start, id.end);
136
+ if (!declarator.init) {
137
+ return `let ${idCode};`;
138
+ }
139
+ return `let ${idCode} = ${declarationInitCode(declarator.init, ctx)};`;
140
+ }
141
+ const hoistedName = getHoistedBindingName(binding, ctx);
142
+ if (!declarator.init) {
143
+ return `let ${hoistedName};`;
144
+ }
145
+ return `let ${hoistedName} = ${declarationInitCode(declarator.init, ctx)};`;
146
+ };
147
+ const assertHoistable = (binding, ctx, stack = []) => {
148
+ if (!binding.declarator?.init || binding.importedFrom || binding.isRoot) {
149
+ return;
150
+ }
151
+ if (stack.includes(binding.name)) {
152
+ return;
153
+ }
154
+ const refs = findReferences(binding.declarator.init, ctx.referencesByNode);
155
+ refs.forEach(({ name, start }) => {
156
+ const nextBinding = resolveBindingAt(ctx, name, start);
157
+ if (!nextBinding) {
158
+ return;
159
+ }
160
+ if (nextBinding.kind === "param") {
161
+ throw new Error(`This identifier cannot be used in the template, because it is a function parameter.`);
162
+ }
163
+ assertHoistable(nextBinding, ctx, [...stack, binding.name]);
164
+ });
165
+ };
166
+ const addHoistedDeclaration = (binding, ctx, stack = []) => {
167
+ if (!binding.declaration || !binding.declarator || binding.importedFrom || binding.isRoot || stack.includes(binding.name)) {
168
+ return;
169
+ }
170
+ const hoistSource = binding.declarator.init ?? binding.declarator;
171
+ findReferences(hoistSource, ctx.referencesByNode).forEach(({ name, start }) => {
172
+ const dependency = resolveBindingAt(ctx, name, start);
173
+ if (dependency) {
174
+ addHoistedDeclaration(dependency, ctx, [...stack, binding.name]);
175
+ }
176
+ });
177
+ if (!ctx.hoistedDeclarations.has(binding.name)) {
178
+ addHoistedCode(binding.name, declarationCode(binding, ctx), ctx);
179
+ }
180
+ };
181
+ const literalExpressionValue = (expression, ctx) => {
182
+ if (expression.type !== "Literal") {
183
+ return null;
184
+ }
185
+ if (expression.value !== null && typeof expression.value !== "string" && typeof expression.value !== "number" && typeof expression.value !== "boolean") {
186
+ return null;
187
+ }
188
+ let type;
189
+ if (expression.value === null) {
190
+ type = "NullLiteral";
191
+ } else if (typeof expression.value === "string") {
192
+ type = "StringLiteral";
193
+ } else if (typeof expression.value === "number") {
194
+ type = "NumericLiteral";
195
+ } else {
196
+ type = "BooleanLiteral";
197
+ }
198
+ const loc = getSourceLocation(expression.start, expression.end, ctx);
199
+ const ex = expression.value === null ? {
200
+ loc,
201
+ type
202
+ } : {
203
+ loc,
204
+ type,
205
+ value: expression.value
206
+ };
207
+ return {
208
+ ex,
209
+ kind: ValueType.CONST,
210
+ source: ctx.code.slice(expression.start, expression.end),
211
+ value: expression.value
212
+ };
213
+ };
214
+ const extractExpression = (expression, ctx, evaluate) => {
215
+ const source = ctx.code.slice(expression.start, expression.end);
216
+ // Only inline function expressions are function-valued here. A bare
217
+ // identifier that points to a local function may be a styled runtime
218
+ // component, so it has to stay as a lazy `_exp()` reference.
219
+ const isFunction = expression.type === "FunctionExpression" || expression.type === "ArrowFunctionExpression";
220
+ if (evaluate) {
221
+ const evaluated = evaluateStatic(expression, ctx);
222
+ const literal = literalCode(evaluated);
223
+ if (literal) {
224
+ findReferences(expression, ctx.referencesByNode).forEach(({ name }) => ctx.dependencyNames.add(name));
225
+ return {
226
+ expressionCode: literal,
227
+ importedFrom: [],
228
+ kind: isFunction ? ValueType.FUNCTION : ValueType.LAZY,
229
+ staticImports: [],
230
+ staticValue: isStaticSerializableValue(evaluated) ? cloneStaticValue(evaluated) : undefined
231
+ };
232
+ }
233
+ }
234
+ const identifierReplacements = new Map();
235
+ const importedFrom = [];
236
+ const namespaceStatic = collectStaticNamespaceMemberReferences(expression, ctx);
237
+ const staticIdentifierReplacements = new Map();
238
+ const staticImports = [...namespaceStatic.imports];
239
+ let hasNonStaticLocalReference = false;
240
+ let hasInlinableLocalReference = false;
241
+ findReferences(expression, ctx.referencesByNode).forEach(({ name, start }) => {
242
+ const binding = resolveBindingAt(ctx, name, start);
243
+ if (!binding) {
244
+ return;
245
+ }
246
+ if (isFunction && isBindingDeclaredWithin(binding, expression)) {
247
+ return;
248
+ }
249
+ ctx.dependencyNames.add(name);
250
+ if (binding.importedFrom) {
251
+ importedFrom.push(binding.importedFrom);
252
+ if (binding.imported && binding.imported !== "*") {
253
+ staticImports.push({
254
+ imported: binding.imported,
255
+ local: binding.name,
256
+ source: binding.importedFrom
257
+ });
258
+ } else if (binding.imported === "*" && namespaceStatic.coveredReferenceStarts.has(start)) {} else {
259
+ hasNonStaticLocalReference = true;
260
+ }
261
+ return;
262
+ }
263
+ const replacement = getConstantReplacement(binding, ctx);
264
+ if (evaluate && replacement) {
265
+ identifierReplacements.set(name, replacement);
266
+ return;
267
+ }
268
+ const init = binding.declarator?.init;
269
+ // Processor-managed bindings (`const x = css```, or object literals
270
+ // containing processor tags) carry values that only become known after
271
+ // processors run. Leave the identifier free in the candidate source so
272
+ // the resolver can supply it via inlineConstants at evaluation time.
273
+ const isProcessorManagedLocal = !!evaluate && !!init && containsTaggedTemplateExpression(init);
274
+ const staticLocalExpression = evaluate && init && !isProcessorManagedLocal ? collectStaticLocalExpression(init, ctx, [hoistedBindingKey(binding)]) : null;
275
+ if (staticLocalExpression) {
276
+ staticIdentifierReplacements.set(name, staticLocalExpression.source);
277
+ importedFrom.push(...staticLocalExpression.importedFrom);
278
+ staticImports.push(...staticLocalExpression.imports);
279
+ } else if (isProcessorManagedLocal) {
280
+ hasInlinableLocalReference = true;
281
+ } else {
282
+ hasNonStaticLocalReference = true;
283
+ }
284
+ assertHoistable(binding, ctx);
285
+ addHoistedDeclaration(binding, ctx);
286
+ if (!binding.isRoot && binding.declarator?.id.type === "Identifier") {
287
+ identifierReplacements.set(name, getHoistedBindingName(binding, ctx));
288
+ }
289
+ });
290
+ // Merge literal-const inlines (e.g. `const A = 32` -> "32") with
291
+ // local-to-imported substitutions (e.g. `const X = imp.y` -> "imp.y").
292
+ // Both must reach the candidate source so the resolver's evaluator
293
+ // can fold every Identifier in the expression; env only carries
294
+ // imported bindings, never same-file locals.
295
+ const mergedReplacements = new Map(staticIdentifierReplacements);
296
+ identifierReplacements.forEach((value, key) => {
297
+ if (!mergedReplacements.has(key)) {
298
+ mergedReplacements.set(key, value);
299
+ }
300
+ });
301
+ let staticExpressionCode;
302
+ if (mergedReplacements.size > 0) {
303
+ staticExpressionCode = replaceStaticLocalReferences(expression, mergedReplacements, ctx, namespaceStatic.replacements);
304
+ } else if (namespaceStatic.replacements.length > 0) {
305
+ staticExpressionCode = applyExpressionReplacements(expression, namespaceStatic.replacements, ctx.code);
306
+ }
307
+ return {
308
+ expressionCode: identifierReplacements.size > 0 ? replaceIdentifierReferences(expression, identifierReplacements, ctx.code) : source,
309
+ importedFrom,
310
+ kind: isFunction ? ValueType.FUNCTION : ValueType.LAZY,
311
+ staticExpressionCode,
312
+ hasInlinableLocalReference: !hasNonStaticLocalReference && hasInlinableLocalReference,
313
+ staticImports: hasNonStaticLocalReference ? [] : staticImports
314
+ };
315
+ };
316
+ const getInsertionPoints = (program, expressions) => {
317
+ if (expressions.length === 0) {
318
+ return [];
319
+ }
320
+ if (program.body.length === 0) {
321
+ return expressions.map(() => 0);
322
+ }
323
+ const insertionPoints = [];
324
+ let ownerIndex = 0;
325
+ expressions.forEach((expression) => {
326
+ while (ownerIndex < program.body.length - 1 && program.body[ownerIndex].end < expression.start) {
327
+ ownerIndex += 1;
328
+ }
329
+ let owner = program.body[ownerIndex];
330
+ if (!owner || owner.start > expression.start || owner.end < expression.end) {
331
+ owner = program.body.find((statement) => statement.start <= expression.start && statement.end >= expression.end);
332
+ }
333
+ insertionPoints.push(owner?.start ?? 0);
334
+ });
335
+ return insertionPoints;
336
+ };
337
+ const extractExpressions = (code, filename, evaluate, program, analysis, expressions, staticBindings) => {
338
+ if (expressions.length === 0) {
339
+ return {
340
+ code,
341
+ dependencyNames: [],
342
+ expressionValues: [],
343
+ staticValueCandidates: [],
344
+ staticValues: []
345
+ };
346
+ }
347
+ const insertionPoints = getInsertionPoints(program, expressions);
348
+ const ctx = {
349
+ bindingResolutionCache: new Map(),
350
+ bindingsByName: analysis.bindingsByName,
351
+ code,
352
+ currentInsertionPoint: insertionPoints[0] ?? 0,
353
+ currentExpressionStart: expressions[0].start,
354
+ dependencyNames: new Set(),
355
+ expressionValues: [],
356
+ filename,
357
+ hoistedBindingNames: new Map(),
358
+ hoistedDeclarations: new Map(),
359
+ hoistedDeclarationsByInsertionPoint: new Map(),
360
+ loc: createOxcLocationLookup(code),
361
+ referencesByNode: new WeakMap(),
362
+ replacements: [],
363
+ rootMutationsByBinding: analysis.rootMutationsByBinding,
364
+ staticBindings,
365
+ staticImportAliases: new Map(),
366
+ staticValueCandidates: [],
367
+ staticValues: [],
368
+ usedNames: new Set(analysis.usedNames)
369
+ };
370
+ expressions.forEach((expression, index) => {
371
+ ctx.currentInsertionPoint = insertionPoints[index] ?? 0;
372
+ ctx.currentExpressionStart = expression.start;
373
+ const literal = literalExpressionValue(expression, ctx);
374
+ if (literal) {
375
+ ctx.expressionValues.push(literal);
376
+ return;
377
+ }
378
+ const { expressionCode, hasInlinableLocalReference, importedFrom, kind, staticExpressionCode, staticImports, staticValue } = extractExpression(expression, ctx, evaluate);
379
+ const expName = allocateExpressionName(ctx);
380
+ addHoistedCode(expName, `const ${expName} = () => (${expressionCode});`, ctx);
381
+ if (staticValue !== undefined && kind !== ValueType.FUNCTION) {
382
+ ctx.staticValues.push({
383
+ name: expName,
384
+ value: staticValue
385
+ });
386
+ } else if ((staticImports.length > 0 || hasInlinableLocalReference || staticExpressionCode !== undefined) && kind !== ValueType.FUNCTION) {
387
+ const uniqueImports = new Map();
388
+ staticImports.forEach((item) => {
389
+ uniqueImports.set(`${item.local}\0${item.importLocal ?? ""}\0${item.source}\0${item.imported}`, item);
390
+ });
391
+ ctx.staticValueCandidates.push({
392
+ imports: [...uniqueImports.values()],
393
+ name: expName,
394
+ source: staticExpressionCode ?? expressionCode
395
+ });
396
+ }
397
+ ctx.replacements.push({
398
+ start: expression.start,
399
+ end: expression.end,
400
+ value: `${expName}()`
401
+ });
402
+ ctx.expressionValues.push({
403
+ ex: {
404
+ loc: getSourceLocation(expression.start, expression.end, ctx),
405
+ name: expName,
406
+ type: "Identifier"
407
+ },
408
+ importedFrom,
409
+ kind,
410
+ source: ctx.code.slice(expression.start, expression.end)
411
+ });
412
+ });
413
+ ctx.hoistedDeclarationsByInsertionPoint.forEach((declarations, point) => {
414
+ ctx.replacements.push({
415
+ start: point,
416
+ end: point,
417
+ value: `${declarations.join("\n")}\n`
418
+ });
419
+ });
420
+ return {
421
+ code: applyOxcReplacements(code, ctx.replacements),
422
+ dependencyNames: [...ctx.dependencyNames],
423
+ expressionValues: ctx.expressionValues,
424
+ staticValueCandidates: ctx.staticValueCandidates,
425
+ staticValues: ctx.staticValues
426
+ };
427
+ };
428
+ export const isOxcStaticSerializableValue = (value) => isStaticSerializableValue(value);
429
+ export const evaluateOxcStaticExpressionAt = (code, filename, expressionSpan, env = new Map(), staticBindings) => {
430
+ const program = parseOxc(code, filename);
431
+ const analysis = analyzeProgram(program, {
432
+ collectTargetExpressions: true,
433
+ expressionSpanLookup: createSpanLookup([expressionSpan])
434
+ });
435
+ const [expression] = analysis.targetExpressions;
436
+ if (!expression) {
437
+ return undefined;
438
+ }
439
+ const ctx = {
440
+ bindingResolutionCache: new Map(),
441
+ bindingsByName: analysis.bindingsByName,
442
+ code,
443
+ currentInsertionPoint: 0,
444
+ currentExpressionStart: expression.start,
445
+ dependencyNames: new Set(),
446
+ expressionValues: [],
447
+ filename,
448
+ hoistedBindingNames: new Map(),
449
+ hoistedDeclarations: new Map(),
450
+ hoistedDeclarationsByInsertionPoint: new Map(),
451
+ loc: createOxcLocationLookup(code),
452
+ referencesByNode: new WeakMap(),
453
+ replacements: [],
454
+ rootMutationsByBinding: analysis.rootMutationsByBinding,
455
+ staticBindings,
456
+ staticImportAliases: new Map(),
457
+ staticValueCandidates: [],
458
+ staticValues: [],
459
+ usedNames: new Set(analysis.usedNames)
460
+ };
461
+ return evaluateStatic(expression, ctx, new Map(env));
462
+ };
463
+ export const evaluateOxcStaticExpression = (source, filename, env = new Map(), staticBindings) => {
464
+ const code = `const __wyw_static_value = ${source};`;
465
+ const program = parseOxc(code, filename);
466
+ const declaration = program.body[0];
467
+ if (declaration?.type !== "VariableDeclaration") {
468
+ return undefined;
469
+ }
470
+ const [declarator] = declaration.declarations;
471
+ if (!declarator?.init) {
472
+ return undefined;
473
+ }
474
+ return evaluateOxcStaticExpressionAt(code, filename, {
475
+ end: declarator.init.end,
476
+ start: declarator.init.start
477
+ }, env, staticBindings);
478
+ };
479
+ export const collectOxcExpressionDependencies = (code, filename, evaluate = false, targetExpressionSpans, staticBindings) => {
480
+ const program = parseOxc(code, filename);
481
+ const analysis = analyzeProgram(program, {
482
+ collectTargetExpressions: true,
483
+ expressionSpanLookup: createSpanLookup(targetExpressionSpans)
484
+ });
485
+ return extractExpressions(code, filename, evaluate, program, analysis, analysis.targetExpressions, staticBindings);
486
+ };
487
+ export const collectOxcTemplateDependencies = (code, filename, evaluate = false, targetTemplateSpans) => {
488
+ const program = parseOxc(code, filename);
489
+ const analysis = analyzeProgram(program, {
490
+ collectTemplateLiterals: true,
491
+ templateSpanLookup: createSpanLookup(targetTemplateSpans)
492
+ });
493
+ const expressions = analysis.templateLiterals.flatMap((template) => template.expressions);
494
+ return extractExpressions(code, filename, evaluate, program, analysis, expressions);
495
+ };
496
+ //# sourceMappingURL=expressionExtraction.js.map
@@ -0,0 +1 @@
1
+ {"mappings":"AAGA,SAAS,iBAAiB;AAG1B,SAAS,4BAA4B;AACrC,SAAS,+BAA+B;AACxC,SACE,gBACA,kCACA,kBACA,gBACA,mBACA,yBACA,UACA,wBACK;AACP,SACE,6BACA,wCACA,wCACA,wBACA,mCACK;AACP,SACE,kBACA,gBACA,2BACA,mBACK;AAcP,MAAM,0BAA0B,QAAmC;CACjE,IAAI,OAAO;CACX,IAAI,MAAM;AACV,QAAO,IAAI,UAAU,IAAI,KAAK,EAAE;AAC9B,SAAO;AACP,SAAO,OAAO;;AAGhB,KAAI,UAAU,IAAI,KAAK;AACvB,QAAO;;AAGT,MAAM,qBAAqB,YACzB,GAAG,QAAQ,MAAM,MAAM,GAAG,QAAQ,MAAM,IAAI,GAAG,QAAQ,WAAW,GAAG,QAAQ;AAE/E,MAAM,8BACJ,cACA,QACW;CACX,MAAM,YAAY,aAAa,QAAQ,mBAAmB,IAA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uBAAuB;AAC/C,SAAO;;CAGT,MAAM,CAAC,cAAc,YAAY;AACjC,KAAI,CAAC,YAAY,MAAM;AACrB,SAAO;;AAGT,QAAO,8BACL,MACA,UACA;EACE,KAAK,WAAW,KAAK;EACrB,OAAO,WAAW,KAAK;EACxB,EACD,KACA,eACD;;AAGH,OAAO,MAAM,oCACX,MACA,UACA,WAAW,OACX,uBACA,mBAC6B;CAC7B,MAAM,UAAU,SAAS,MAAM,SAAS;CACxC,MAAM,WAAW,eAAe,SAAS;EACvC,0BAA0B;EAC1B,sBAAsB,iBAAiB,sBAAsB;EAC9D,CAAC;AAEF,QAAO,mBACL,MACA,UACA,UACA,SACA,UACA,SAAS,mBACT,eACD;;AAGH,OAAO,MAAM,kCACX,MACA,UACA,WAAW,OACX,wBAC6B;CAC7B,MAAM,UAAU,SAAS,MAAM,SAAS;CACxC,MAAM,WAAW,eAAe,SAAS;EACvC,yBAAyB;EACzB,oBAAoB,iBAAiB,oBAAoB;EAC1D,CAAC;CACF,MAAM,cAAc,SAAS,iBAAiB,SAC3C,aAAa,SAAS,YACxB;AAED,QAAO,mBACL,MACA,UACA,UACA,SACA,UACA,YACD","names":[],"sources":["../../../src/utils/collectOxcTemplateDependencies/expressionExtraction.ts"],"version":3,"sourcesContent":["/* eslint-disable no-restricted-syntax,no-continue */\n\nimport type { ExpressionValue } from '@wyw-in-js/shared';\nimport { ValueType } from '@wyw-in-js/shared';\nimport type { Expression, Program } from 'oxc-parser';\n\nimport { applyOxcReplacements } from '../oxc/replacements';\nimport { createOxcLocationLookup } from '../oxc/sourceLocations';\nimport {\n analyzeProgram,\n containsTaggedTemplateExpression,\n createSpanLookup,\n findReferences,\n getSourceLocation,\n isBindingDeclaredWithin,\n parseOxc,\n resolveBindingAt,\n} from './scopeAnalysis';\nimport {\n applyExpressionReplacements,\n collectIdentifierReferenceReplacements,\n collectStaticNamespaceMemberReferences,\n getConstantReplacement,\n replaceIdentifierReferences,\n} from './expressionReplacements';\nimport {\n cloneStaticValue,\n evaluateStatic,\n isStaticSerializableValue,\n literalCode,\n} from './staticEvaluator';\nimport type {\n Binding,\n ExtractedExpression,\n ExpressionSpan,\n ExtractionContext,\n OxcStaticImportReference,\n ProgramAnalysis,\n Replacement,\n StaticBindings,\n StaticLocalExpression,\n TemplateExtractionResult,\n} from './types';\n\nconst allocateExpressionName = (ctx: ExtractionContext): string => {\n let base = '_exp';\n let idx = 1;\n while (ctx.usedNames.has(base)) {\n idx += 1;\n base = `_exp${idx}`;\n }\n\n ctx.usedNames.add(base);\n return base;\n};\n\nconst hoistedBindingKey = (binding: Binding): string =>\n `${binding.scope.start}:${binding.scope.end}:${binding.declaredAt}:${binding.name}`;\n\nconst allocateHoistedBindingName = (\n originalName: string,\n ctx: ExtractionContext\n): string => {\n const sanitized = originalName.replace(/[^A-Za-z0-9_$]/g, '_') || 'hoisted';\n const base = /^[A-Za-z_$]/.test(sanitized) ? `_${sanitized}` : '_hoisted';\n let candidate = base;\n let idx = 2;\n\n while (ctx.usedNames.has(candidate)) {\n candidate = `${base}${idx}`;\n idx += 1;\n }\n\n ctx.usedNames.add(candidate);\n return candidate;\n};\n\nconst getHoistedBindingName = (\n binding: Binding,\n ctx: ExtractionContext\n): string => {\n const key = hoistedBindingKey(binding);\n const existing = ctx.hoistedBindingNames.get(key);\n if (existing) {\n return existing;\n }\n\n const next = allocateHoistedBindingName(binding.name, ctx);\n ctx.hoistedBindingNames.set(key, next);\n return next;\n};\n\nconst parenthesizeStaticReplacement = (source: string): string => `(${source})`;\n\nconst replaceStaticLocalReferences = (\n expression: Expression,\n replacements: Map<string, string>,\n ctx: ExtractionContext,\n extraReplacements: Replacement[] = []\n): string => {\n if (expression.type === 'Identifier' && extraReplacements.length === 0) {\n return (\n replacements.get(expression.name) ??\n ctx.code.slice(expression.start, expression.end)\n );\n }\n\n const parenthesized = new Map<string, string>();\n replacements.forEach((value, key) => {\n parenthesized.set(key, parenthesizeStaticReplacement(value));\n });\n\n return applyExpressionReplacements(\n expression,\n [\n ...extraReplacements,\n ...collectIdentifierReferenceReplacements(expression, parenthesized),\n ],\n ctx.code\n );\n};\n\nconst collectStaticLocalExpression = (\n expression: Expression,\n ctx: ExtractionContext,\n stack: string[] = []\n): StaticLocalExpression | null => {\n const replacements = new Map<string, string>();\n const importedFrom = new Set<string>();\n const imports: OxcStaticImportReference[] = [];\n\n for (const { name, start } of findReferences(\n expression,\n ctx.referencesByNode\n )) {\n const binding = resolveBindingAt(ctx, name, start);\n if (!binding) {\n return null;\n }\n\n if (binding.importedFrom) {\n importedFrom.add(binding.importedFrom);\n if (binding.imported && binding.imported !== '*') {\n imports.push({\n imported: binding.imported,\n local: binding.name,\n source: binding.importedFrom,\n });\n continue;\n }\n\n return null;\n }\n\n const replacement = getConstantReplacement(binding, ctx);\n if (replacement) {\n replacements.set(name, replacement);\n continue;\n }\n\n if (\n binding.kind === 'param' ||\n binding.declarationKind !== 'const' ||\n !binding.declarator?.init ||\n binding.declarator.id.type !== 'Identifier'\n ) {\n return null;\n }\n\n // Processor-managed bindings (const x = css``) carry their value\n // (the generated className string) via inlineConstants at candidate\n // evaluation time. Walking the TaggedTemplateExpression here would\n // pull the processor's tag import (e.g. `css` from '@linaria/core')\n // into the candidate's static imports, where it fails to resolve.\n // Leave the identifier as a free reference; the candidate-side env\n // supplies the className.\n if (binding.declarator.init.type === 'TaggedTemplateExpression') {\n continue;\n }\n\n const key = hoistedBindingKey(binding);\n if (stack.includes(key)) {\n return null;\n }\n\n const nested = collectStaticLocalExpression(binding.declarator.init, ctx, [\n ...stack,\n key,\n ]);\n if (!nested) {\n return null;\n }\n\n replacements.set(name, nested.source);\n nested.importedFrom.forEach((source) => importedFrom.add(source));\n imports.push(...nested.imports);\n }\n\n return {\n importedFrom: [...importedFrom],\n imports,\n source:\n replacements.size > 0\n ? replaceStaticLocalReferences(expression, replacements, ctx)\n : ctx.code.slice(expression.start, expression.end),\n };\n};\n\nconst declarationInitCode = (\n init: Expression,\n ctx: ExtractionContext\n): string => {\n const renamedDependencies = new Map<string, string>();\n findReferences(init, ctx.referencesByNode).forEach(({ name, start }) => {\n const dependency = resolveBindingAt(ctx, name, start);\n if (\n !dependency ||\n dependency.importedFrom ||\n dependency.isRoot ||\n dependency.declarator?.id.type !== 'Identifier'\n ) {\n return;\n }\n\n renamedDependencies.set(name, getHoistedBindingName(dependency, ctx));\n });\n\n return renamedDependencies.size > 0\n ? replaceIdentifierReferences(init, renamedDependencies, ctx.code)\n : ctx.code.slice(init.start, init.end);\n};\n\nconst addHoistedCode = (\n key: string,\n code: string,\n ctx: ExtractionContext\n): void => {\n if (ctx.hoistedDeclarations.has(key)) {\n return;\n }\n\n ctx.hoistedDeclarations.set(key, code);\n const declarations =\n ctx.hoistedDeclarationsByInsertionPoint.get(ctx.currentInsertionPoint) ??\n [];\n declarations.push(code);\n ctx.hoistedDeclarationsByInsertionPoint.set(\n ctx.currentInsertionPoint,\n declarations\n );\n};\n\nconst declarationCode = (binding: Binding, ctx: ExtractionContext): string => {\n const { declarator } = binding;\n if (!declarator) {\n return '';\n }\n\n const { id } = declarator;\n if (id.type !== 'Identifier') {\n const idCode = ctx.code.slice(id.start, id.end);\n if (!declarator.init) {\n return `let ${idCode};`;\n }\n\n return `let ${idCode} = ${declarationInitCode(declarator.init, ctx)};`;\n }\n\n const hoistedName = getHoistedBindingName(binding, ctx);\n if (!declarator.init) {\n return `let ${hoistedName};`;\n }\n\n return `let ${hoistedName} = ${declarationInitCode(declarator.init, ctx)};`;\n};\n\nconst assertHoistable = (\n binding: Binding,\n ctx: ExtractionContext,\n stack: string[] = []\n): void => {\n if (!binding.declarator?.init || binding.importedFrom || binding.isRoot) {\n return;\n }\n\n if (stack.includes(binding.name)) {\n return;\n }\n\n const refs = findReferences(binding.declarator.init, ctx.referencesByNode);\n refs.forEach(({ name, start }) => {\n const nextBinding = resolveBindingAt(ctx, name, start);\n if (!nextBinding) {\n return;\n }\n\n if (nextBinding.kind === 'param') {\n throw new Error(\n `This identifier cannot be used in the template, because it is a function parameter.`\n );\n }\n\n assertHoistable(nextBinding, ctx, [...stack, binding.name]);\n });\n};\n\nconst addHoistedDeclaration = (\n binding: Binding,\n ctx: ExtractionContext,\n stack: string[] = []\n): void => {\n if (\n !binding.declaration ||\n !binding.declarator ||\n binding.importedFrom ||\n binding.isRoot ||\n stack.includes(binding.name)\n ) {\n return;\n }\n\n const hoistSource = binding.declarator.init ?? binding.declarator;\n findReferences(hoistSource, ctx.referencesByNode).forEach(\n ({ name, start }) => {\n const dependency = resolveBindingAt(ctx, name, start);\n if (dependency) {\n addHoistedDeclaration(dependency, ctx, [...stack, binding.name]);\n }\n }\n );\n\n if (!ctx.hoistedDeclarations.has(binding.name)) {\n addHoistedCode(binding.name, declarationCode(binding, ctx), ctx);\n }\n};\n\nconst literalExpressionValue = (\n expression: Expression,\n ctx: ExtractionContext\n): Omit<ExpressionValue, 'buildCodeFrameError'> | 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 return null;\n }\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 loc = getSourceLocation(expression.start, expression.end, ctx);\n const ex =\n expression.value === null\n ? { loc, type }\n : {\n loc,\n type,\n value: expression.value,\n };\n\n return {\n ex,\n kind: ValueType.CONST,\n source: ctx.code.slice(expression.start, expression.end),\n value: expression.value,\n } as unknown as Omit<ExpressionValue, 'buildCodeFrameError'>;\n};\n\nconst extractExpression = (\n expression: Expression,\n ctx: ExtractionContext,\n evaluate: boolean\n): ExtractedExpression => {\n const source = ctx.code.slice(expression.start, expression.end);\n // Only inline function expressions are function-valued here. A bare\n // identifier that points to a local function may be a styled runtime\n // component, so it has to stay as a lazy `_exp()` reference.\n const isFunction =\n expression.type === 'FunctionExpression' ||\n expression.type === 'ArrowFunctionExpression';\n\n if (evaluate) {\n const evaluated = evaluateStatic(expression, ctx);\n const literal = literalCode(evaluated);\n if (literal) {\n findReferences(expression, ctx.referencesByNode).forEach(({ name }) =>\n ctx.dependencyNames.add(name)\n );\n return {\n expressionCode: literal,\n importedFrom: [],\n kind: isFunction ? ValueType.FUNCTION : ValueType.LAZY,\n staticImports: [],\n staticValue: isStaticSerializableValue(evaluated)\n ? cloneStaticValue(evaluated)\n : undefined,\n };\n }\n }\n\n const identifierReplacements = new Map<string, string>();\n const importedFrom: string[] = [];\n const namespaceStatic = collectStaticNamespaceMemberReferences(\n expression,\n ctx\n );\n const staticIdentifierReplacements = new Map<string, string>();\n const staticImports: OxcStaticImportReference[] = [\n ...namespaceStatic.imports,\n ];\n let hasNonStaticLocalReference = false;\n let hasInlinableLocalReference = false;\n\n findReferences(expression, ctx.referencesByNode).forEach(\n ({ name, start }) => {\n const binding = resolveBindingAt(ctx, name, start);\n if (!binding) {\n return;\n }\n\n if (isFunction && isBindingDeclaredWithin(binding, expression)) {\n return;\n }\n\n ctx.dependencyNames.add(name);\n\n if (binding.importedFrom) {\n importedFrom.push(binding.importedFrom);\n if (binding.imported && binding.imported !== '*') {\n staticImports.push({\n imported: binding.imported,\n local: binding.name,\n source: binding.importedFrom,\n });\n } else if (\n binding.imported === '*' &&\n namespaceStatic.coveredReferenceStarts.has(start)\n ) {\n // The static candidate source gets a synthetic named import alias,\n // while the eval fallback keeps the original namespace expression.\n } else {\n hasNonStaticLocalReference = true;\n }\n return;\n }\n\n const replacement = getConstantReplacement(binding, ctx);\n if (evaluate && replacement) {\n identifierReplacements.set(name, replacement);\n return;\n }\n\n const init = binding.declarator?.init;\n // Processor-managed bindings (`const x = css```, or object literals\n // containing processor tags) carry values that only become known after\n // processors run. Leave the identifier free in the candidate source so\n // the resolver can supply it via inlineConstants at evaluation time.\n const isProcessorManagedLocal =\n !!evaluate && !!init && containsTaggedTemplateExpression(init);\n const staticLocalExpression =\n evaluate && init && !isProcessorManagedLocal\n ? collectStaticLocalExpression(init, ctx, [\n hoistedBindingKey(binding),\n ])\n : null;\n if (staticLocalExpression) {\n staticIdentifierReplacements.set(name, staticLocalExpression.source);\n importedFrom.push(...staticLocalExpression.importedFrom);\n staticImports.push(...staticLocalExpression.imports);\n } else if (isProcessorManagedLocal) {\n hasInlinableLocalReference = true;\n } else {\n hasNonStaticLocalReference = true;\n }\n\n assertHoistable(binding, ctx);\n addHoistedDeclaration(binding, ctx);\n if (!binding.isRoot && binding.declarator?.id.type === 'Identifier') {\n identifierReplacements.set(name, getHoistedBindingName(binding, ctx));\n }\n }\n );\n\n // Merge literal-const inlines (e.g. `const A = 32` -> \"32\") with\n // local-to-imported substitutions (e.g. `const X = imp.y` -> \"imp.y\").\n // Both must reach the candidate source so the resolver's evaluator\n // can fold every Identifier in the expression; env only carries\n // imported bindings, never same-file locals.\n const mergedReplacements = new Map(staticIdentifierReplacements);\n identifierReplacements.forEach((value, key) => {\n if (!mergedReplacements.has(key)) {\n mergedReplacements.set(key, value);\n }\n });\n\n let staticExpressionCode: string | undefined;\n if (mergedReplacements.size > 0) {\n staticExpressionCode = replaceStaticLocalReferences(\n expression,\n mergedReplacements,\n ctx,\n namespaceStatic.replacements\n );\n } else if (namespaceStatic.replacements.length > 0) {\n staticExpressionCode = applyExpressionReplacements(\n expression,\n namespaceStatic.replacements,\n ctx.code\n );\n }\n\n return {\n expressionCode:\n identifierReplacements.size > 0\n ? replaceIdentifierReferences(\n expression,\n identifierReplacements,\n ctx.code\n )\n : source,\n importedFrom,\n kind: isFunction ? ValueType.FUNCTION : ValueType.LAZY,\n staticExpressionCode,\n hasInlinableLocalReference:\n !hasNonStaticLocalReference && hasInlinableLocalReference,\n staticImports: hasNonStaticLocalReference ? [] : staticImports,\n };\n};\n\nconst getInsertionPoints = (\n program: Program,\n expressions: Expression[]\n): number[] => {\n if (expressions.length === 0) {\n return [];\n }\n\n if (program.body.length === 0) {\n return expressions.map(() => 0);\n }\n\n const insertionPoints: number[] = [];\n let ownerIndex = 0;\n\n expressions.forEach((expression) => {\n while (\n ownerIndex < program.body.length - 1 &&\n program.body[ownerIndex]!.end < expression.start\n ) {\n ownerIndex += 1;\n }\n\n let owner: Program['body'][number] | undefined = program.body[ownerIndex];\n if (\n !owner ||\n owner.start > expression.start ||\n owner.end < expression.end\n ) {\n owner = program.body.find(\n (statement) =>\n statement.start <= expression.start && statement.end >= expression.end\n );\n }\n\n insertionPoints.push(owner?.start ?? 0);\n });\n\n return insertionPoints;\n};\n\nconst extractExpressions = (\n code: string,\n filename: string,\n evaluate: boolean,\n program: Program,\n analysis: Pick<\n ProgramAnalysis,\n 'bindingsByName' | 'rootMutationsByBinding' | 'usedNames'\n >,\n expressions: Expression[],\n staticBindings?: StaticBindings\n): TemplateExtractionResult => {\n if (expressions.length === 0) {\n return {\n code,\n dependencyNames: [],\n expressionValues: [],\n staticValueCandidates: [],\n staticValues: [],\n };\n }\n\n const insertionPoints = getInsertionPoints(program, expressions);\n const ctx: ExtractionContext = {\n bindingResolutionCache: new Map(),\n bindingsByName: analysis.bindingsByName,\n code,\n currentInsertionPoint: insertionPoints[0] ?? 0,\n currentExpressionStart: expressions[0].start,\n dependencyNames: new Set(),\n expressionValues: [],\n filename,\n hoistedBindingNames: new Map(),\n hoistedDeclarations: new Map(),\n hoistedDeclarationsByInsertionPoint: new Map(),\n loc: createOxcLocationLookup(code),\n referencesByNode: new WeakMap(),\n replacements: [],\n rootMutationsByBinding: analysis.rootMutationsByBinding,\n staticBindings,\n staticImportAliases: new Map(),\n staticValueCandidates: [],\n staticValues: [],\n usedNames: new Set(analysis.usedNames),\n };\n\n expressions.forEach((expression, index) => {\n ctx.currentInsertionPoint = insertionPoints[index] ?? 0;\n ctx.currentExpressionStart = expression.start;\n\n const literal = literalExpressionValue(expression, ctx);\n if (literal) {\n ctx.expressionValues.push(literal);\n return;\n }\n\n const {\n expressionCode,\n hasInlinableLocalReference,\n importedFrom,\n kind,\n staticExpressionCode,\n staticImports,\n staticValue,\n } = extractExpression(expression, ctx, evaluate);\n const expName = allocateExpressionName(ctx);\n\n addHoistedCode(\n expName,\n `const ${expName} = () => (${expressionCode});`,\n ctx\n );\n if (staticValue !== undefined && kind !== ValueType.FUNCTION) {\n ctx.staticValues.push({\n name: expName,\n value: staticValue,\n });\n } else if (\n (staticImports.length > 0 ||\n hasInlinableLocalReference ||\n staticExpressionCode !== undefined) &&\n kind !== ValueType.FUNCTION\n ) {\n const uniqueImports = new Map<string, OxcStaticImportReference>();\n staticImports.forEach((item) => {\n uniqueImports.set(\n `${item.local}\\0${item.importLocal ?? ''}\\0${item.source}\\0${\n item.imported\n }`,\n item\n );\n });\n ctx.staticValueCandidates.push({\n imports: [...uniqueImports.values()],\n name: expName,\n source: staticExpressionCode ?? expressionCode,\n });\n }\n ctx.replacements.push({\n start: expression.start,\n end: expression.end,\n value: `${expName}()`,\n });\n ctx.expressionValues.push({\n ex: {\n loc: getSourceLocation(expression.start, expression.end, ctx),\n name: expName,\n type: 'Identifier',\n },\n importedFrom,\n kind,\n source: ctx.code.slice(expression.start, expression.end),\n } as unknown as Omit<ExpressionValue, 'buildCodeFrameError'>);\n });\n\n ctx.hoistedDeclarationsByInsertionPoint.forEach((declarations, point) => {\n ctx.replacements.push({\n start: point,\n end: point,\n value: `${declarations.join('\\n')}\\n`,\n });\n });\n\n return {\n code: applyOxcReplacements(code, ctx.replacements),\n dependencyNames: [...ctx.dependencyNames],\n expressionValues: ctx.expressionValues,\n staticValueCandidates: ctx.staticValueCandidates,\n staticValues: ctx.staticValues,\n };\n};\n\nexport const isOxcStaticSerializableValue = (value: unknown): boolean =>\n isStaticSerializableValue(value);\n\nexport const evaluateOxcStaticExpressionAt = (\n code: string,\n filename: string,\n expressionSpan: ExpressionSpan,\n env: Map<string, unknown> = new Map(),\n staticBindings?: StaticBindings\n): unknown | undefined => {\n const program = parseOxc(code, filename);\n const analysis = analyzeProgram(program, {\n collectTargetExpressions: true,\n expressionSpanLookup: createSpanLookup([expressionSpan]),\n });\n const [expression] = analysis.targetExpressions;\n if (!expression) {\n return undefined;\n }\n\n const ctx: ExtractionContext = {\n bindingResolutionCache: new Map(),\n bindingsByName: analysis.bindingsByName,\n code,\n currentInsertionPoint: 0,\n currentExpressionStart: expression.start,\n dependencyNames: new Set(),\n expressionValues: [],\n filename,\n hoistedBindingNames: new Map(),\n hoistedDeclarations: new Map(),\n hoistedDeclarationsByInsertionPoint: new Map(),\n loc: createOxcLocationLookup(code),\n referencesByNode: new WeakMap(),\n replacements: [],\n rootMutationsByBinding: analysis.rootMutationsByBinding,\n staticBindings,\n staticImportAliases: new Map(),\n staticValueCandidates: [],\n staticValues: [],\n usedNames: new Set(analysis.usedNames),\n };\n\n return evaluateStatic(expression, ctx, new Map(env));\n};\n\nexport const evaluateOxcStaticExpression = (\n source: string,\n filename: string,\n env: Map<string, unknown> = new Map(),\n staticBindings?: StaticBindings\n): unknown | undefined => {\n const code = `const __wyw_static_value = ${source};`;\n const program = parseOxc(code, filename);\n const declaration = program.body[0];\n if (declaration?.type !== 'VariableDeclaration') {\n return undefined;\n }\n\n const [declarator] = declaration.declarations;\n if (!declarator?.init) {\n return undefined;\n }\n\n return evaluateOxcStaticExpressionAt(\n code,\n filename,\n {\n end: declarator.init.end,\n start: declarator.init.start,\n },\n env,\n staticBindings\n );\n};\n\nexport const collectOxcExpressionDependencies = (\n code: string,\n filename: string,\n evaluate = false,\n targetExpressionSpans?: ExpressionSpan[],\n staticBindings?: StaticBindings\n): TemplateExtractionResult => {\n const program = parseOxc(code, filename);\n const analysis = analyzeProgram(program, {\n collectTargetExpressions: true,\n expressionSpanLookup: createSpanLookup(targetExpressionSpans),\n });\n\n return extractExpressions(\n code,\n filename,\n evaluate,\n program,\n analysis,\n analysis.targetExpressions,\n staticBindings\n );\n};\n\nexport const collectOxcTemplateDependencies = (\n code: string,\n filename: string,\n evaluate = false,\n targetTemplateSpans?: ExpressionSpan[]\n): TemplateExtractionResult => {\n const program = parseOxc(code, filename);\n const analysis = analyzeProgram(program, {\n collectTemplateLiterals: true,\n templateSpanLookup: createSpanLookup(targetTemplateSpans),\n });\n const expressions = analysis.templateLiterals.flatMap(\n (template) => template.expressions\n );\n\n return extractExpressions(\n code,\n filename,\n evaluate,\n program,\n analysis,\n expressions\n );\n};\n"],"file":"expressionExtraction.js"}