@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 @@
1
+ export declare const normalizeRuntimeCode: (code: string, filename: string) => string;
@@ -0,0 +1,185 @@
1
+ /* eslint-disable no-restricted-syntax */
2
+ import { parseSync } from 'oxc-parser';
3
+ const parseOxc = (code, filename) => {
4
+ const parsed = parseSync(filename, code, {
5
+ astType: filename.endsWith('.ts') || filename.endsWith('.tsx') ? 'ts' : 'js',
6
+ range: true,
7
+ sourceType: 'module',
8
+ });
9
+ const fatalError = parsed.errors.find((error) => error.severity === 'Error');
10
+ if (fatalError) {
11
+ throw new Error(fatalError.message);
12
+ }
13
+ return parsed.program;
14
+ };
15
+ const applyReplacements = (code, replacements) => {
16
+ let result = code;
17
+ replacements
18
+ .sort((a, b) => b.start - a.start)
19
+ .forEach((replacement) => {
20
+ result =
21
+ result.slice(0, replacement.start) +
22
+ replacement.value +
23
+ result.slice(replacement.end);
24
+ });
25
+ return result;
26
+ };
27
+ const shouldTerminateWithSemicolon = (node, parent, key) => {
28
+ if (node.type === 'VariableDeclaration' && parent) {
29
+ if ((parent.type === 'ForStatement' ||
30
+ parent.type === 'ForInStatement' ||
31
+ parent.type === 'ForOfStatement') &&
32
+ key &&
33
+ ['init', 'left'].includes(key)) {
34
+ return false;
35
+ }
36
+ if (parent.type === 'ExportNamedDeclaration') {
37
+ return false;
38
+ }
39
+ }
40
+ if (node.type === 'ImportDeclaration' ||
41
+ node.type === 'ExpressionStatement' ||
42
+ node.type === 'ReturnStatement' ||
43
+ node.type === 'ThrowStatement' ||
44
+ node.type === 'VariableDeclaration') {
45
+ return true;
46
+ }
47
+ if (node.type === 'ExportNamedDeclaration') {
48
+ if (node.declaration) {
49
+ return node.declaration.type === 'VariableDeclaration';
50
+ }
51
+ return Array.isArray(node.specifiers) && node.specifiers.length > 0;
52
+ }
53
+ return false;
54
+ };
55
+ const getChildren = (node) => {
56
+ const result = [];
57
+ const record = node;
58
+ Object.keys(record).forEach((key) => {
59
+ if (key === 'type' || key === 'start' || key === 'end' || key === 'range') {
60
+ return;
61
+ }
62
+ const value = record[key];
63
+ if (value && typeof value === 'object' && 'type' in value) {
64
+ result.push({ key, node: value });
65
+ return;
66
+ }
67
+ if (Array.isArray(value)) {
68
+ value.forEach((item) => {
69
+ if (item && typeof item === 'object' && 'type' in item) {
70
+ result.push({ key, node: item });
71
+ }
72
+ });
73
+ }
74
+ });
75
+ return result;
76
+ };
77
+ const getLineIndent = (code, offset) => {
78
+ const lineStart = code.lastIndexOf('\n', offset - 1) + 1;
79
+ let idx = lineStart;
80
+ while (idx < code.length && (code[idx] === ' ' || code[idx] === '\t')) {
81
+ idx += 1;
82
+ }
83
+ return code.slice(lineStart, idx).replace(/\t/g, ' ');
84
+ };
85
+ const isPlainObjectProperty = (property) => property.type === 'Property' &&
86
+ property.kind === 'init' &&
87
+ property.method !== true;
88
+ const isFormattableObjectExpression = (node) => node.type === 'ObjectExpression' &&
89
+ node.properties.every((property) => property.type === 'SpreadElement' || isPlainObjectProperty(property));
90
+ const hasNestedObjectValue = (node) => node.type === 'ObjectExpression' &&
91
+ node.properties.some((property) => isPlainObjectProperty(property) &&
92
+ property.value.type === 'ObjectExpression');
93
+ const printFormattedObjectExpression = (node, code, baseIndent) => {
94
+ if (node.type !== 'ObjectExpression' ||
95
+ node.properties.length === 0 ||
96
+ !isFormattableObjectExpression(node)) {
97
+ return code.slice(node.start, node.end);
98
+ }
99
+ const lines = node.properties.map((property) => {
100
+ if (property.type === 'SpreadElement') {
101
+ return `${baseIndent} ...${code.slice(property.argument.start, property.argument.end)}`;
102
+ }
103
+ const keySource = property.computed
104
+ ? `[${code.slice(property.key.start, property.key.end)}]`
105
+ : code.slice(property.key.start, property.key.end);
106
+ const valueSource = property.value.type === 'ObjectExpression'
107
+ ? printFormattedObjectExpression(property.value, code, `${baseIndent} `)
108
+ : code.slice(property.value.start, property.value.end).trim();
109
+ return `${baseIndent} ${keySource}: ${valueSource}`;
110
+ });
111
+ return `{\n${lines.join(',\n')}\n${baseIndent}}`;
112
+ };
113
+ const formatRuntimeObjectLiterals = (code, filename) => {
114
+ const replacements = [];
115
+ const walk = (node, parent = null) => {
116
+ if (node.type === 'ObjectExpression') {
117
+ const shouldFormat = (parent?.type === 'VariableDeclarator' && hasNestedObjectValue(node)) ||
118
+ parent?.type === 'CallExpression';
119
+ if (shouldFormat) {
120
+ replacements.push({
121
+ end: node.end,
122
+ start: node.start,
123
+ value: printFormattedObjectExpression(node, code, getLineIndent(code, node.start)),
124
+ });
125
+ return;
126
+ }
127
+ }
128
+ getChildren(node).forEach((child) => walk(child.node, node));
129
+ };
130
+ walk(parseOxc(code, filename));
131
+ return replacements.length > 0 ? applyReplacements(code, replacements) : code;
132
+ };
133
+ const collapseRuntimeBlankLines = (code) => {
134
+ const lines = code.split('\n');
135
+ const result = [];
136
+ for (let idx = 0; idx < lines.length; idx += 1) {
137
+ const line = lines[idx];
138
+ if (line.trim() !== '') {
139
+ result.push(line);
140
+ }
141
+ else {
142
+ let nextIdx = idx;
143
+ while (nextIdx < lines.length && lines[nextIdx]?.trim() === '') {
144
+ nextIdx += 1;
145
+ }
146
+ const previousNonEmpty = [...result]
147
+ .reverse()
148
+ .find((entry) => entry.trim() !== '');
149
+ const nextNonEmpty = lines
150
+ .slice(nextIdx)
151
+ .find((entry) => entry.trim() !== '');
152
+ if (previousNonEmpty && nextNonEmpty) {
153
+ const trimmedPrevious = previousNonEmpty.trim();
154
+ if (trimmedPrevious.startsWith('//') ||
155
+ trimmedPrevious.endsWith('*/')) {
156
+ result.push('');
157
+ }
158
+ }
159
+ idx = nextIdx - 1;
160
+ }
161
+ }
162
+ return result.join('\n');
163
+ };
164
+ const ensureBlankLineAfterLeadingBlockComment = (code) => code.replace(/^(\/\*[\s\S]*?\*\/)\n(?!\n)/, '$1\n\n');
165
+ const insertMissingSemicolons = (code, filename) => {
166
+ const replacements = [];
167
+ const hasTrailingSemicolon = (node) => code.slice(node.start, node.end).trimEnd().endsWith(';');
168
+ const walk = (node, parent = null, key = null) => {
169
+ if (shouldTerminateWithSemicolon(node, parent, key) &&
170
+ !hasTrailingSemicolon(node)) {
171
+ replacements.push({
172
+ end: node.end,
173
+ start: node.end,
174
+ value: ';',
175
+ });
176
+ }
177
+ getChildren(node).forEach((child) => walk(child.node, node, child.key));
178
+ };
179
+ walk(parseOxc(code, filename));
180
+ return replacements.length > 0 ? applyReplacements(code, replacements) : code;
181
+ };
182
+ export const normalizeRuntimeCode = (code, filename) => insertMissingSemicolons(ensureBlankLineAfterLeadingBlockComment(collapseRuntimeBlankLines(formatRuntimeObjectLiterals(code
183
+ .replace(/^\n+/, '')
184
+ .replace(/\n+$/, '')
185
+ .replace(/[ \t]+\n/g, '\n'), filename))), filename);
@@ -0,0 +1,2 @@
1
+ import { type RawSourceMap } from 'source-map';
2
+ export declare const createComposedRuntimeSourceMap: (generatedCode: string, originalCode: string, filename: string, inputSourceMap?: RawSourceMap) => RawSourceMap;
@@ -0,0 +1,37 @@
1
+ import { createRequire } from 'node:module';
2
+ import { SourceMapGenerator } from 'source-map';
3
+ const remapping = createRequire(import.meta.url)('@jridgewell/remapping');
4
+ const countLines = (code) => code.split('\n').length;
5
+ const createLineSourceMap = (generatedCode, originalCode, filename) => {
6
+ const generator = new SourceMapGenerator({
7
+ file: filename,
8
+ });
9
+ const generatedLines = countLines(generatedCode);
10
+ const originalLines = countLines(originalCode);
11
+ for (let line = 1; line <= generatedLines; line += 1) {
12
+ generator.addMapping({
13
+ generated: {
14
+ column: 0,
15
+ line,
16
+ },
17
+ original: {
18
+ column: 0,
19
+ line: Math.min(line, originalLines),
20
+ },
21
+ source: filename,
22
+ });
23
+ }
24
+ generator.setSourceContent(filename, originalCode);
25
+ return generator.toJSON();
26
+ };
27
+ export const createComposedRuntimeSourceMap = (generatedCode, originalCode, filename, inputSourceMap) => {
28
+ const runtimeMap = createLineSourceMap(generatedCode, originalCode, filename);
29
+ if (!inputSourceMap) {
30
+ return runtimeMap;
31
+ }
32
+ const composed = remapping([runtimeMap, inputSourceMap], () => null);
33
+ return {
34
+ ...composed,
35
+ file: runtimeMap.file,
36
+ };
37
+ };
@@ -0,0 +1,16 @@
1
+ import type { StrictOptions } from '@wyw-in-js/shared';
2
+ import type { RawSourceMap } from 'source-map';
3
+ import type { WYWTransformMetadata } from '../TransformMetadata';
4
+ export type OxcCollectOptions = Pick<StrictOptions, 'classNameSlug' | 'displayName' | 'eval' | 'extensions' | 'tagResolver' | 'variableNameConfig'> & {
5
+ preserveSideEffectImportLocals?: Set<string>;
6
+ };
7
+ export type OxcCollectResult = {
8
+ code: string;
9
+ map: RawSourceMap;
10
+ metadata: WYWTransformMetadata | null;
11
+ };
12
+ export type RuntimeReplacement = {
13
+ end: number;
14
+ start: number;
15
+ value: string;
16
+ };
@@ -0,0 +1 @@
1
+ export {};
@@ -1,14 +1,4 @@
1
1
  import type { ValueCache } from '@wyw-in-js/processor-utils';
2
- import type { StrictOptions } from '@wyw-in-js/shared';
3
- import { type RawSourceMap } from 'source-map';
4
- import type { WYWTransformMetadata } from './TransformMetadata';
5
- type OxcCollectOptions = Pick<StrictOptions, 'classNameSlug' | 'displayName' | 'evaluate' | 'extensions' | 'tagResolver' | 'variableNameConfig'> & {
6
- preserveSideEffectImportLocals?: Set<string>;
7
- };
8
- type OxcCollectResult = {
9
- code: string;
10
- map: RawSourceMap;
11
- metadata: WYWTransformMetadata | null;
12
- };
2
+ import type { RawSourceMap } from 'source-map';
3
+ import type { OxcCollectOptions, OxcCollectResult } from './collectOxcRuntime/types';
13
4
  export declare const collectOxcRuntime: (code: string, filename: string, root: string, options: OxcCollectOptions, values: ValueCache, inputSourceMap?: RawSourceMap) => OxcCollectResult;
14
- export {};
@@ -1,226 +1,6 @@
1
- /* eslint-disable no-restricted-syntax */
2
- import { createRequire } from 'node:module';
3
- import { parseSync } from 'oxc-parser';
4
- import { SourceMapGenerator } from 'source-map';
5
1
  import { applyOxcProcessors } from './applyOxcProcessors';
6
- const remapping = createRequire(import.meta.url)('@jridgewell/remapping');
7
- const countLines = (code) => code.split('\n').length;
8
- const parseOxc = (code, filename) => {
9
- const parsed = parseSync(filename, code, {
10
- astType: filename.endsWith('.ts') || filename.endsWith('.tsx') ? 'ts' : 'js',
11
- range: true,
12
- sourceType: 'module',
13
- });
14
- const fatalError = parsed.errors.find((error) => error.severity === 'Error');
15
- if (fatalError) {
16
- throw new Error(fatalError.message);
17
- }
18
- return parsed.program;
19
- };
20
- const applyReplacements = (code, replacements) => {
21
- let result = code;
22
- replacements
23
- .sort((a, b) => b.start - a.start)
24
- .forEach((replacement) => {
25
- result =
26
- result.slice(0, replacement.start) +
27
- replacement.value +
28
- result.slice(replacement.end);
29
- });
30
- return result;
31
- };
32
- const shouldTerminateWithSemicolon = (node, parent, key) => {
33
- if (node.type === 'VariableDeclaration' && parent) {
34
- if ((parent.type === 'ForStatement' ||
35
- parent.type === 'ForInStatement' ||
36
- parent.type === 'ForOfStatement') &&
37
- key &&
38
- ['init', 'left'].includes(key)) {
39
- return false;
40
- }
41
- if (parent.type === 'ExportNamedDeclaration') {
42
- return false;
43
- }
44
- }
45
- if (node.type === 'ImportDeclaration' ||
46
- node.type === 'ExpressionStatement' ||
47
- node.type === 'ReturnStatement' ||
48
- node.type === 'ThrowStatement' ||
49
- node.type === 'VariableDeclaration') {
50
- return true;
51
- }
52
- if (node.type === 'ExportNamedDeclaration') {
53
- if (node.declaration) {
54
- return node.declaration.type === 'VariableDeclaration';
55
- }
56
- return Array.isArray(node.specifiers) && node.specifiers.length > 0;
57
- }
58
- return false;
59
- };
60
- const getChildren = (node) => {
61
- const result = [];
62
- const record = node;
63
- Object.keys(record).forEach((key) => {
64
- if (key === 'type' || key === 'start' || key === 'end' || key === 'range') {
65
- return;
66
- }
67
- const value = record[key];
68
- if (value && typeof value === 'object' && 'type' in value) {
69
- result.push({ key, node: value });
70
- return;
71
- }
72
- if (Array.isArray(value)) {
73
- value.forEach((item) => {
74
- if (item && typeof item === 'object' && 'type' in item) {
75
- result.push({ key, node: item });
76
- }
77
- });
78
- }
79
- });
80
- return result;
81
- };
82
- const getLineIndent = (code, offset) => {
83
- const lineStart = code.lastIndexOf('\n', offset - 1) + 1;
84
- let idx = lineStart;
85
- while (idx < code.length && (code[idx] === ' ' || code[idx] === '\t')) {
86
- idx += 1;
87
- }
88
- return code.slice(lineStart, idx).replace(/\t/g, ' ');
89
- };
90
- const isPlainObjectProperty = (property) => property.type === 'Property' &&
91
- property.kind === 'init' &&
92
- property.method !== true;
93
- const isFormattableObjectExpression = (node) => node.type === 'ObjectExpression' &&
94
- node.properties.every((property) => property.type === 'SpreadElement' || isPlainObjectProperty(property));
95
- const hasNestedObjectValue = (node) => node.type === 'ObjectExpression' &&
96
- node.properties.some((property) => isPlainObjectProperty(property) &&
97
- property.value.type === 'ObjectExpression');
98
- const printFormattedObjectExpression = (node, code, baseIndent) => {
99
- if (node.type !== 'ObjectExpression' ||
100
- node.properties.length === 0 ||
101
- !isFormattableObjectExpression(node)) {
102
- return code.slice(node.start, node.end);
103
- }
104
- const lines = node.properties.map((property) => {
105
- if (property.type === 'SpreadElement') {
106
- return `${baseIndent} ...${code.slice(property.argument.start, property.argument.end)}`;
107
- }
108
- const keySource = property.computed
109
- ? `[${code.slice(property.key.start, property.key.end)}]`
110
- : code.slice(property.key.start, property.key.end);
111
- const valueSource = property.value.type === 'ObjectExpression'
112
- ? printFormattedObjectExpression(property.value, code, `${baseIndent} `)
113
- : code.slice(property.value.start, property.value.end).trim();
114
- return `${baseIndent} ${keySource}: ${valueSource}`;
115
- });
116
- return `{\n${lines.join(',\n')}\n${baseIndent}}`;
117
- };
118
- const formatRuntimeObjectLiterals = (code, filename) => {
119
- const replacements = [];
120
- const walk = (node, parent = null) => {
121
- if (node.type === 'ObjectExpression') {
122
- const shouldFormat = (parent?.type === 'VariableDeclarator' && hasNestedObjectValue(node)) ||
123
- parent?.type === 'CallExpression';
124
- if (shouldFormat) {
125
- replacements.push({
126
- end: node.end,
127
- start: node.start,
128
- value: printFormattedObjectExpression(node, code, getLineIndent(code, node.start)),
129
- });
130
- return;
131
- }
132
- }
133
- getChildren(node).forEach((child) => walk(child.node, node));
134
- };
135
- walk(parseOxc(code, filename));
136
- return replacements.length > 0 ? applyReplacements(code, replacements) : code;
137
- };
138
- const collapseRuntimeBlankLines = (code) => {
139
- const lines = code.split('\n');
140
- const result = [];
141
- for (let idx = 0; idx < lines.length; idx += 1) {
142
- const line = lines[idx];
143
- if (line.trim() !== '') {
144
- result.push(line);
145
- }
146
- else {
147
- let nextIdx = idx;
148
- while (nextIdx < lines.length && lines[nextIdx]?.trim() === '') {
149
- nextIdx += 1;
150
- }
151
- const previousNonEmpty = [...result]
152
- .reverse()
153
- .find((entry) => entry.trim() !== '');
154
- const nextNonEmpty = lines
155
- .slice(nextIdx)
156
- .find((entry) => entry.trim() !== '');
157
- if (previousNonEmpty && nextNonEmpty) {
158
- const trimmedPrevious = previousNonEmpty.trim();
159
- if (trimmedPrevious.startsWith('//') ||
160
- trimmedPrevious.endsWith('*/')) {
161
- result.push('');
162
- }
163
- }
164
- idx = nextIdx - 1;
165
- }
166
- }
167
- return result.join('\n');
168
- };
169
- const ensureBlankLineAfterLeadingBlockComment = (code) => code.replace(/^(\/\*[\s\S]*?\*\/)\n(?!\n)/, '$1\n\n');
170
- const insertMissingSemicolons = (code, filename) => {
171
- const replacements = [];
172
- const hasTrailingSemicolon = (node) => code.slice(node.start, node.end).trimEnd().endsWith(';');
173
- const walk = (node, parent = null, key = null) => {
174
- if (shouldTerminateWithSemicolon(node, parent, key) &&
175
- !hasTrailingSemicolon(node)) {
176
- replacements.push({
177
- end: node.end,
178
- start: node.end,
179
- value: ';',
180
- });
181
- }
182
- getChildren(node).forEach((child) => walk(child.node, node, child.key));
183
- };
184
- walk(parseOxc(code, filename));
185
- return replacements.length > 0 ? applyReplacements(code, replacements) : code;
186
- };
187
- const createLineSourceMap = (generatedCode, originalCode, filename) => {
188
- const generator = new SourceMapGenerator({
189
- file: filename,
190
- });
191
- const generatedLines = countLines(generatedCode);
192
- const originalLines = countLines(originalCode);
193
- for (let line = 1; line <= generatedLines; line += 1) {
194
- generator.addMapping({
195
- generated: {
196
- column: 0,
197
- line,
198
- },
199
- original: {
200
- column: 0,
201
- line: Math.min(line, originalLines),
202
- },
203
- source: filename,
204
- });
205
- }
206
- generator.setSourceContent(filename, originalCode);
207
- return generator.toJSON();
208
- };
209
- const createComposedRuntimeSourceMap = (generatedCode, originalCode, filename, inputSourceMap) => {
210
- const runtimeMap = createLineSourceMap(generatedCode, originalCode, filename);
211
- if (!inputSourceMap) {
212
- return runtimeMap;
213
- }
214
- const composed = remapping([runtimeMap, inputSourceMap], () => null);
215
- return {
216
- ...composed,
217
- file: runtimeMap.file,
218
- };
219
- };
220
- const normalizeRuntimeCode = (code, filename) => insertMissingSemicolons(ensureBlankLineAfterLeadingBlockComment(collapseRuntimeBlankLines(formatRuntimeObjectLiterals(code
221
- .replace(/^\n+/, '')
222
- .replace(/\n+$/, '')
223
- .replace(/[ \t]+\n/g, '\n'), filename))), filename);
2
+ import { normalizeRuntimeCode } from './collectOxcRuntime/normalizeRuntimeCode';
3
+ import { createComposedRuntimeSourceMap } from './collectOxcRuntime/sourceMap';
224
4
  export const collectOxcRuntime = (code, filename, root, options, values, inputSourceMap) => {
225
5
  const result = applyOxcProcessors(code, {
226
6
  filename,
@@ -230,16 +10,17 @@ export const collectOxcRuntime = (code, filename, root, options, values, inputSo
230
10
  processor.doRuntimeReplacement();
231
11
  }, true);
232
12
  const normalizedCode = normalizeRuntimeCode(result.code, filename);
13
+ const map = createComposedRuntimeSourceMap(normalizedCode, code, filename, inputSourceMap);
233
14
  if (result.processors.length === 0) {
234
15
  return {
235
16
  code: normalizedCode,
236
- map: createComposedRuntimeSourceMap(normalizedCode, code, filename, inputSourceMap),
17
+ map,
237
18
  metadata: null,
238
19
  };
239
20
  }
240
21
  return {
241
22
  code: normalizedCode,
242
- map: createComposedRuntimeSourceMap(normalizedCode, code, filename, inputSourceMap),
23
+ map,
243
24
  metadata: {
244
25
  dependencies: [],
245
26
  processors: result.processors,
@@ -0,0 +1,6 @@
1
+ import type { ExpressionSpan, StaticBindings, TemplateExtractionResult } from './types';
2
+ export declare const isOxcStaticSerializableValue: (value: unknown) => boolean;
3
+ export declare const evaluateOxcStaticExpressionAt: (code: string, filename: string, expressionSpan: ExpressionSpan, env?: Map<string, unknown>, staticBindings?: StaticBindings) => unknown | undefined;
4
+ export declare const evaluateOxcStaticExpression: (source: string, filename: string, env?: Map<string, unknown>, staticBindings?: StaticBindings) => unknown | undefined;
5
+ export declare const collectOxcExpressionDependencies: (code: string, filename: string, evaluate?: boolean, targetExpressionSpans?: ExpressionSpan[], staticBindings?: StaticBindings) => TemplateExtractionResult;
6
+ export declare const collectOxcTemplateDependencies: (code: string, filename: string, evaluate?: boolean, targetTemplateSpans?: ExpressionSpan[]) => TemplateExtractionResult;