@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,20 @@
1
+ import type { SourceLocation } from '@wyw-in-js/shared';
2
+ import type { Node, Program } from 'oxc-parser';
3
+ import type { Binding, ExpressionSpan, ExtractionContext, ProgramAnalysis, ReferenceIdentifier, SpanLookup } from './types';
4
+ export declare const containsTaggedTemplateExpression: (node: Node) => boolean;
5
+ export declare const parseOxc: (code: string, filename: string) => Program;
6
+ export declare const createSpanLookup: (spans?: ExpressionSpan[]) => SpanLookup;
7
+ export declare const getSourceLocation: (start: number, end: number, ctx: Pick<ExtractionContext, "filename" | "loc">) => SourceLocation;
8
+ export declare const isInTypeContext: (ancestors: Node[]) => boolean;
9
+ export declare const isPropertyOnlyIdentifier: (node: Node, parent: Node | null) => boolean;
10
+ export declare const isObjectPropertyKey: (node: Node, parent: Node | null) => boolean;
11
+ export declare const isBindingPosition: (node: Node, parent: Node | null) => boolean;
12
+ export declare const analyzeProgram: (program: Program, { collectTargetExpressions, collectTemplateLiterals, expressionSpanLookup, templateSpanLookup, }?: {
13
+ collectTargetExpressions?: boolean;
14
+ collectTemplateLiterals?: boolean;
15
+ expressionSpanLookup?: SpanLookup;
16
+ templateSpanLookup?: SpanLookup;
17
+ }) => ProgramAnalysis;
18
+ export declare const resolveBindingAt: (ctx: Pick<ExtractionContext, "bindingResolutionCache" | "bindingsByName">, name: string, referenceStart: number) => Binding | undefined;
19
+ export declare const findReferences: (node: Node, referenceCache?: WeakMap<Node, ReferenceIdentifier[]>) => ReferenceIdentifier[];
20
+ export declare const isBindingDeclaredWithin: (binding: Binding, container: Node) => boolean;
@@ -0,0 +1,434 @@
1
+ /* eslint-disable no-restricted-syntax,no-continue,@typescript-eslint/no-use-before-define */
2
+ import { getOxcNodeChildren } from '../oxc/ast';
3
+ import { parseOxcProgram } from '../oxc/parse';
4
+ import { createOxcSourceLocation } from '../oxc/sourceLocations';
5
+ export const containsTaggedTemplateExpression = (node) => {
6
+ if (node.type === 'TaggedTemplateExpression') {
7
+ return true;
8
+ }
9
+ return getOxcNodeChildren(node).some(containsTaggedTemplateExpression);
10
+ };
11
+ export const parseOxc = (code, filename) => {
12
+ return parseOxcProgram(code, filename, 'unambiguous');
13
+ };
14
+ const toSpanKey = (start, end) => `${start}:${end}`;
15
+ export const createSpanLookup = (spans) => {
16
+ if (!spans || spans.length === 0) {
17
+ return null;
18
+ }
19
+ return new Set(spans.map((span) => toSpanKey(span.start, span.end)));
20
+ };
21
+ const matchesSpanLookup = (node, spanLookup) => !spanLookup || spanLookup.has(toSpanKey(node.start, node.end));
22
+ export const getSourceLocation = (start, end, ctx) => createOxcSourceLocation(start, end, ctx.loc, ctx.filename);
23
+ const createScope = (parent, node, root = false, functionBoundary = false) => ({
24
+ bindings: new Map(),
25
+ depth: parent ? parent.depth + 1 : 0,
26
+ end: node.end,
27
+ functionBoundary,
28
+ params: new Set(),
29
+ parent,
30
+ root,
31
+ start: node.start,
32
+ });
33
+ const normalizeDeclarationKind = (declarationKind) => {
34
+ if (declarationKind === 'var') {
35
+ return 'var';
36
+ }
37
+ if (declarationKind === 'let') {
38
+ return 'let';
39
+ }
40
+ return 'const';
41
+ };
42
+ const moduleExportName = (node) => node.type === 'Literal' ? String(node.value) : node.name;
43
+ const getImportSpecifierInfo = (statement, specifier) => {
44
+ const local = specifier.local?.name;
45
+ if (!local) {
46
+ return null;
47
+ }
48
+ if (specifier.type === 'ImportDefaultSpecifier') {
49
+ return {
50
+ imported: 'default',
51
+ local,
52
+ };
53
+ }
54
+ if (specifier.type === 'ImportNamespaceSpecifier') {
55
+ return {
56
+ imported: '*',
57
+ local,
58
+ };
59
+ }
60
+ if (statement.importKind === 'type' ||
61
+ specifier.importKind === 'type') {
62
+ return null;
63
+ }
64
+ return {
65
+ imported: moduleExportName(specifier.imported),
66
+ local,
67
+ };
68
+ };
69
+ const getDeclarationScope = (scope, declarationKind) => {
70
+ if (declarationKind !== 'var') {
71
+ return scope;
72
+ }
73
+ let current = scope;
74
+ while (current && !current.functionBoundary) {
75
+ current = current.parent;
76
+ }
77
+ return current ?? scope;
78
+ };
79
+ const collectBindingNames = (node) => {
80
+ if (node.type === 'Identifier') {
81
+ return [node.name];
82
+ }
83
+ if (node.type === 'RestElement') {
84
+ return collectBindingNames(node.argument);
85
+ }
86
+ if (node.type === 'AssignmentPattern') {
87
+ return collectBindingNames(node.left);
88
+ }
89
+ if (node.type === 'ObjectPattern') {
90
+ return node.properties.flatMap((property) => property.type === 'RestElement'
91
+ ? collectBindingNames(property.argument)
92
+ : collectBindingNames(property.value));
93
+ }
94
+ if (node.type === 'ArrayPattern') {
95
+ return node.elements.flatMap((element) => element ? collectBindingNames(element) : []);
96
+ }
97
+ if (node.type === 'TSParameterProperty') {
98
+ return collectBindingNames(node.parameter);
99
+ }
100
+ return [];
101
+ };
102
+ export const isInTypeContext = (ancestors) => ancestors.some((ancestor) => ancestor.type.startsWith('TS') || ancestor.type.startsWith('JSDoc'));
103
+ export const isPropertyOnlyIdentifier = (node, parent) => !!parent &&
104
+ parent.type === 'MemberExpression' &&
105
+ parent.property === node &&
106
+ !parent.computed;
107
+ export const isObjectPropertyKey = (node, parent) => !!parent &&
108
+ parent.type === 'Property' &&
109
+ parent.key === node &&
110
+ !parent.computed &&
111
+ parent.value !== node;
112
+ export const isBindingPosition = (node, parent) => {
113
+ if (!parent) {
114
+ return false;
115
+ }
116
+ if (parent.type === 'VariableDeclarator' && parent.id === node) {
117
+ return true;
118
+ }
119
+ if ((parent.type === 'FunctionDeclaration' ||
120
+ parent.type === 'FunctionExpression' ||
121
+ parent.type === 'ClassDeclaration' ||
122
+ parent.type === 'ClassExpression') &&
123
+ parent.id === node) {
124
+ return true;
125
+ }
126
+ if ((parent.type === 'ImportSpecifier' ||
127
+ parent.type === 'ImportDefaultSpecifier' ||
128
+ parent.type === 'ImportNamespaceSpecifier') &&
129
+ 'local' in parent &&
130
+ parent.local === node) {
131
+ return true;
132
+ }
133
+ return false;
134
+ };
135
+ const visit = (node, scope, enter, parent = null, ancestors = []) => {
136
+ const visitNode = (currentNode, currentScope, currentParent) => {
137
+ let nextScope;
138
+ if (currentNode.type === 'Program') {
139
+ nextScope = createScope(null, currentNode, true, true);
140
+ }
141
+ else if (currentNode.type === 'BlockStatement' ||
142
+ currentNode.type === 'FunctionDeclaration' ||
143
+ currentNode.type === 'FunctionExpression' ||
144
+ currentNode.type === 'ArrowFunctionExpression') {
145
+ nextScope = createScope(currentScope, currentNode, false, currentNode.type !== 'BlockStatement');
146
+ }
147
+ else if (currentScope) {
148
+ nextScope = currentScope;
149
+ }
150
+ else {
151
+ nextScope = createScope(null, currentNode, false, true);
152
+ }
153
+ if (currentNode.type === 'FunctionDeclaration' ||
154
+ currentNode.type === 'FunctionExpression' ||
155
+ currentNode.type === 'ArrowFunctionExpression') {
156
+ currentNode.params.forEach((param) => {
157
+ collectBindingNames(param).forEach((name) => {
158
+ nextScope.params.add(name);
159
+ nextScope.bindings.set(name, {
160
+ declaredAt: param.start,
161
+ declaration: null,
162
+ declarator: null,
163
+ functionNode: null,
164
+ isRoot: false,
165
+ kind: 'param',
166
+ name,
167
+ scope: nextScope,
168
+ });
169
+ });
170
+ });
171
+ }
172
+ enter(currentNode, nextScope, currentParent, ancestors);
173
+ ancestors.push(currentNode);
174
+ getOxcNodeChildren(currentNode).forEach((child) => visitNode(child, nextScope, currentNode));
175
+ ancestors.pop();
176
+ };
177
+ visitNode(node, scope, parent);
178
+ };
179
+ export const analyzeProgram = (program, { collectTargetExpressions = false, collectTemplateLiterals = false, expressionSpanLookup = null, templateSpanLookup = null, } = {}) => {
180
+ const bindings = new Map();
181
+ const usedNames = new Set();
182
+ const templateLiterals = [];
183
+ const targetExpressions = [];
184
+ const addBinding = (scope, binding) => {
185
+ scope.bindings.set(binding.name, binding);
186
+ const existing = bindings.get(binding.name) ?? [];
187
+ existing.push(binding);
188
+ bindings.set(binding.name, existing);
189
+ };
190
+ const collectTargets = (node, ancestors) => {
191
+ if (collectTemplateLiterals &&
192
+ node.type === 'TemplateLiteral' &&
193
+ node.expressions.length > 0 &&
194
+ !ancestors.some((ancestor) => ancestor.type === 'TemplateLiteral') &&
195
+ matchesSpanLookup(node, templateSpanLookup)) {
196
+ templateLiterals.push(node);
197
+ }
198
+ if (collectTargetExpressions &&
199
+ expressionSpanLookup &&
200
+ matchesSpanLookup(node, expressionSpanLookup)) {
201
+ targetExpressions.push(node);
202
+ }
203
+ };
204
+ visit(program, null, (node, scope, _parent, ancestors) => {
205
+ collectTargets(node, ancestors);
206
+ if (node.type === 'Identifier') {
207
+ usedNames.add(node.name);
208
+ }
209
+ if (isInTypeContext(ancestors)) {
210
+ return;
211
+ }
212
+ if (node.type === 'FunctionDeclaration' ||
213
+ node.type === 'FunctionExpression' ||
214
+ node.type === 'ArrowFunctionExpression') {
215
+ node.params.forEach((param) => {
216
+ collectBindingNames(param).forEach((name) => {
217
+ const binding = scope.bindings.get(name);
218
+ if (binding) {
219
+ addBinding(scope, binding);
220
+ }
221
+ });
222
+ });
223
+ if (node.type !== 'FunctionDeclaration') {
224
+ return;
225
+ }
226
+ }
227
+ if (node.type === 'ImportDeclaration') {
228
+ const source = node.source.value;
229
+ node.specifiers.forEach((specifier) => {
230
+ const importInfo = getImportSpecifierInfo(node, specifier);
231
+ if (!importInfo) {
232
+ return;
233
+ }
234
+ addBinding(scope, {
235
+ declaredAt: specifier.start,
236
+ declaration: null,
237
+ declarator: null,
238
+ functionNode: null,
239
+ imported: importInfo.imported,
240
+ importedFrom: source,
241
+ isRoot: scope.root,
242
+ kind: 'import',
243
+ name: importInfo.local,
244
+ scope,
245
+ });
246
+ });
247
+ return;
248
+ }
249
+ if (node.type !== 'VariableDeclaration') {
250
+ if (node.type === 'FunctionDeclaration' && node.id) {
251
+ const declarationScope = scope.parent ?? scope;
252
+ const binding = {
253
+ declaredAt: node.start,
254
+ declaration: null,
255
+ declarator: null,
256
+ functionNode: node,
257
+ isRoot: declarationScope.root,
258
+ kind: 'function',
259
+ name: node.id.name,
260
+ scope: declarationScope,
261
+ };
262
+ addBinding(declarationScope, binding);
263
+ }
264
+ return;
265
+ }
266
+ node.declarations.forEach((declarator) => {
267
+ collectBindingNames(declarator.id).forEach((name) => {
268
+ const declarationKind = normalizeDeclarationKind(node.kind);
269
+ const declarationScope = getDeclarationScope(scope, declarationKind);
270
+ const binding = {
271
+ declarationKind,
272
+ declaredAt: declarator.start,
273
+ declaration: node,
274
+ declarator,
275
+ functionNode: null,
276
+ isRoot: declarationScope.root,
277
+ kind: 'variable',
278
+ name,
279
+ scope: declarationScope,
280
+ };
281
+ addBinding(declarationScope, binding);
282
+ });
283
+ });
284
+ });
285
+ return {
286
+ bindingsByName: bindings,
287
+ rootMutationsByBinding: collectRootMutations(program),
288
+ targetExpressions: targetExpressions.sort((a, b) => a.start - b.start),
289
+ templateLiterals,
290
+ usedNames,
291
+ };
292
+ };
293
+ export const resolveBindingAt = (ctx, name, referenceStart) => {
294
+ const cachedBindings = ctx.bindingResolutionCache.get(name);
295
+ if (cachedBindings?.has(referenceStart)) {
296
+ return cachedBindings.get(referenceStart) ?? undefined;
297
+ }
298
+ const bindings = ctx.bindingsByName.get(name);
299
+ const bindingCache = cachedBindings ?? new Map();
300
+ if (!cachedBindings) {
301
+ ctx.bindingResolutionCache.set(name, bindingCache);
302
+ }
303
+ if (!bindings || bindings.length === 0) {
304
+ bindingCache.set(referenceStart, null);
305
+ return undefined;
306
+ }
307
+ let binding;
308
+ bindings.forEach((candidate) => {
309
+ if (candidate.scope.start > referenceStart ||
310
+ referenceStart >= candidate.scope.end) {
311
+ return;
312
+ }
313
+ if (!binding ||
314
+ candidate.scope.depth > binding.scope.depth ||
315
+ (candidate.scope.depth === binding.scope.depth &&
316
+ candidate.declaredAt > binding.declaredAt)) {
317
+ binding = candidate;
318
+ }
319
+ });
320
+ bindingCache.set(referenceStart, binding ?? null);
321
+ return binding;
322
+ };
323
+ const collectRootMutations = (program) => {
324
+ const mutations = new Map();
325
+ const getRootMutationTarget = (node) => {
326
+ if (node.type === 'Identifier') {
327
+ return {
328
+ binding: node.name,
329
+ path: [],
330
+ };
331
+ }
332
+ if (node.type !== 'MemberExpression') {
333
+ return null;
334
+ }
335
+ const parent = getRootMutationTarget(node.object);
336
+ if (!parent) {
337
+ return null;
338
+ }
339
+ let key = null;
340
+ if (node.computed &&
341
+ node.property.type === 'Literal' &&
342
+ (typeof node.property.value === 'string' ||
343
+ typeof node.property.value === 'number')) {
344
+ key = node.property.value;
345
+ }
346
+ else if (!node.computed && node.property.type === 'Identifier') {
347
+ key = node.property.name;
348
+ }
349
+ if (key === null) {
350
+ return null;
351
+ }
352
+ return {
353
+ binding: parent.binding,
354
+ path: [...parent.path, key],
355
+ };
356
+ };
357
+ program.body.forEach((statement) => {
358
+ if (statement.type !== 'ExpressionStatement') {
359
+ return;
360
+ }
361
+ const { expression } = statement;
362
+ if (expression.type === 'AssignmentExpression') {
363
+ const target = getRootMutationTarget(expression.left);
364
+ if (!target || target.path.length === 0) {
365
+ return;
366
+ }
367
+ const bucket = mutations.get(target.binding) ?? [];
368
+ bucket.push(expression);
369
+ mutations.set(target.binding, bucket);
370
+ return;
371
+ }
372
+ if (expression.type === 'UpdateExpression') {
373
+ const target = getRootMutationTarget(expression.argument);
374
+ if (!target || target.path.length === 0) {
375
+ return;
376
+ }
377
+ const bucket = mutations.get(target.binding) ?? [];
378
+ bucket.push(expression);
379
+ mutations.set(target.binding, bucket);
380
+ }
381
+ });
382
+ return mutations;
383
+ };
384
+ const hasLocalBinding = (scope, name) => {
385
+ let current = scope;
386
+ while (current) {
387
+ if (current.bindings.has(name)) {
388
+ return true;
389
+ }
390
+ current = current.parent;
391
+ }
392
+ return false;
393
+ };
394
+ const hasLocalBindingCached = (scope, name, cache) => {
395
+ const scopeCache = cache.get(scope);
396
+ if (scopeCache?.has(name)) {
397
+ return scopeCache.get(name);
398
+ }
399
+ const result = hasLocalBinding(scope, name);
400
+ const nextScopeCache = scopeCache ?? new Map();
401
+ nextScopeCache.set(name, result);
402
+ if (!scopeCache) {
403
+ cache.set(scope, nextScopeCache);
404
+ }
405
+ return result;
406
+ };
407
+ export const findReferences = (node, referenceCache) => {
408
+ const cachedReferences = referenceCache?.get(node);
409
+ if (cachedReferences) {
410
+ return cachedReferences;
411
+ }
412
+ const refs = new Map();
413
+ const localBindingCache = new WeakMap();
414
+ visit(node, null, (current, scope, parent, ancestors) => {
415
+ if (current.type !== 'Identifier' ||
416
+ isInTypeContext(ancestors) ||
417
+ isBindingPosition(current, parent) ||
418
+ isPropertyOnlyIdentifier(current, parent) ||
419
+ isObjectPropertyKey(current, parent) ||
420
+ hasLocalBindingCached(scope, current.name, localBindingCache)) {
421
+ return;
422
+ }
423
+ const key = `${current.start}:${current.end}:${current.name}`;
424
+ refs.set(key, {
425
+ end: current.end,
426
+ name: current.name,
427
+ start: current.start,
428
+ });
429
+ });
430
+ const resolvedReferences = [...refs.values()];
431
+ referenceCache?.set(node, resolvedReferences);
432
+ return resolvedReferences;
433
+ };
434
+ export const isBindingDeclaredWithin = (binding, container) => container.start <= binding.declaredAt && binding.declaredAt < container.end;
@@ -0,0 +1,7 @@
1
+ import type { StaticBindings } from './types';
2
+ export declare const lookupStaticBinding: (staticBindings: StaticBindings | undefined, source: string | undefined, imported: string | undefined) => {
3
+ found: true;
4
+ value: unknown;
5
+ } | {
6
+ found: false;
7
+ };
@@ -0,0 +1,13 @@
1
+ export const lookupStaticBinding = (staticBindings, source, imported) => {
2
+ if (!staticBindings || !source || !imported) {
3
+ return { found: false };
4
+ }
5
+ const sourceMap = staticBindings[source];
6
+ if (!sourceMap) {
7
+ return { found: false };
8
+ }
9
+ if (!Object.prototype.hasOwnProperty.call(sourceMap, imported)) {
10
+ return { found: false };
11
+ }
12
+ return { found: true, value: sourceMap[imported] };
13
+ };
@@ -0,0 +1,13 @@
1
+ import type { Expression } from 'oxc-parser';
2
+ import type { ExtractionContext } from './types';
3
+ export declare const literalCode: (value: unknown) => string | null;
4
+ export declare const isStaticSerializableValue: (value: unknown) => boolean;
5
+ export declare const cloneStaticValue: (value: unknown) => unknown;
6
+ type EvalEnv = Map<string, unknown>;
7
+ declare const oxcStaticCallableValue: unique symbol;
8
+ type OxcStaticCallableValue = {
9
+ [oxcStaticCallableValue]: unknown;
10
+ };
11
+ export declare const createOxcStaticCallableValue: (value: unknown) => OxcStaticCallableValue;
12
+ export declare const evaluateStatic: (expression: Expression, ctx: ExtractionContext, env?: EvalEnv, stack?: string[]) => unknown | undefined;
13
+ export {};