@goast/core 0.6.0 → 0.7.0-beta.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 (342) hide show
  1. package/README.md +0 -4
  2. package/dist/mod.d.ts +31 -0
  3. package/{script → dist}/src/ast/node.d.ts +3 -4
  4. package/{esm → dist}/src/ast/node.js +3 -9
  5. package/{esm → dist}/src/codegen/config.d.ts +2 -3
  6. package/{script → dist}/src/codegen/endpoints-generator.d.ts +4 -5
  7. package/{esm → dist}/src/codegen/endpoints-generator.js +1 -1
  8. package/{script → dist}/src/codegen/generator.d.ts +7 -8
  9. package/{esm → dist}/src/codegen/generator.js +4 -21
  10. package/{esm → dist}/src/codegen/internal-utils.d.ts +0 -1
  11. package/{esm → dist}/src/codegen/internal-utils.js +0 -1
  12. package/{script → dist}/src/codegen/schemas-generator.d.ts +4 -5
  13. package/{esm → dist}/src/codegen/schemas-generator.js +1 -2
  14. package/{esm → dist}/src/codegen/services-generator.d.ts +4 -5
  15. package/{esm → dist}/src/codegen/services-generator.js +1 -1
  16. package/{script → dist}/src/codegen/types.d.ts +3 -4
  17. package/dist/src/collect/collector.d.ts +4 -0
  18. package/{esm → dist}/src/collect/collector.js +6 -8
  19. package/{esm → dist}/src/collect/helpers.d.ts +2 -3
  20. package/{script → dist}/src/collect/types.d.ts +2 -3
  21. package/{esm → dist}/src/internal-utils.d.ts +0 -1
  22. package/dist/src/parse/deref-proxy.d.ts +3 -0
  23. package/{esm → dist}/src/parse/deref-proxy.js +7 -21
  24. package/{esm → dist}/src/parse/openapi-types.d.ts +0 -1
  25. package/{esm → dist}/src/parse/parser.d.ts +3 -4
  26. package/{esm → dist}/src/parse/parser.js +7 -20
  27. package/{script → dist}/src/parse/types.d.ts +3 -4
  28. package/{script → dist}/src/transform/api-types.d.ts +3 -4
  29. package/{esm → dist}/src/transform/helpers.d.ts +4 -5
  30. package/{esm → dist}/src/transform/helpers.js +5 -15
  31. package/dist/src/transform/transform-document.d.ts +4 -0
  32. package/{esm → dist}/src/transform/transform-document.js +2 -4
  33. package/{esm → dist}/src/transform/transform-endpoint.d.ts +3 -4
  34. package/{esm → dist}/src/transform/transform-endpoint.js +10 -17
  35. package/dist/src/transform/transform-schema.d.ts +5 -0
  36. package/{esm → dist}/src/transform/transform-schema.js +8 -12
  37. package/dist/src/transform/transformer.d.ts +4 -0
  38. package/{esm → dist}/src/transform/types.d.ts +4 -5
  39. package/{script → dist}/src/utils/action-provider.d.ts +2 -3
  40. package/{esm → dist}/src/utils/action-provider.js +3 -7
  41. package/{esm → dist}/src/utils/api-data.utils.d.ts +1 -2
  42. package/{esm → dist}/src/utils/array.utils.d.ts +0 -1
  43. package/{script → dist}/src/utils/asset-manager.d.ts +0 -1
  44. package/{esm → dist}/src/utils/asset-manager.js +2 -8
  45. package/{esm → dist}/src/utils/class.utils.d.ts +0 -1
  46. package/{esm → dist}/src/utils/class.utils.js +0 -1
  47. package/{script → dist}/src/utils/common.utils.d.ts +1 -2
  48. package/{esm → dist}/src/utils/condition.d.ts +0 -1
  49. package/dist/src/utils/endpoint.utils.d.ts +2 -0
  50. package/{esm → dist}/src/utils/error.utils.d.ts +0 -1
  51. package/{esm → dist}/src/utils/factory.d.ts +2 -3
  52. package/{esm → dist}/src/utils/factory.js +3 -7
  53. package/{esm → dist}/src/utils/file-system.utils.d.ts +2 -3
  54. package/{esm → dist}/src/utils/generator.utils.d.ts +0 -1
  55. package/{esm → dist}/src/utils/object.utils.d.ts +0 -1
  56. package/{esm → dist}/src/utils/object.utils.js +2 -11
  57. package/dist/src/utils/schema-factory.d.ts +41 -0
  58. package/dist/src/utils/schema-factory.js +132 -0
  59. package/{esm → dist}/src/utils/schema.utils.d.ts +2 -3
  60. package/{esm → dist}/src/utils/schema.utils.js +1 -5
  61. package/{esm → dist}/src/utils/source-builder.d.ts +5 -6
  62. package/{esm → dist}/src/utils/source-builder.js +19 -54
  63. package/{script → dist}/src/utils/string-builder/options.d.ts +0 -1
  64. package/{script → dist}/src/utils/string-builder/string-builder.d.ts +2 -3
  65. package/{esm → dist}/src/utils/string-builder/string-builder.js +2 -12
  66. package/{esm → dist}/src/utils/string-builder/utils.d.ts +2 -3
  67. package/{esm → dist}/src/utils/string-builder/utils.js +4 -6
  68. package/{script → dist}/src/utils/string.utils.d.ts +2 -3
  69. package/{esm → dist}/src/utils/string.utils.js +9 -15
  70. package/{esm → dist}/src/utils/string.utils.types.d.ts +0 -1
  71. package/{esm → dist}/src/utils/type.utils.d.ts +0 -1
  72. package/{script → dist}/src/utils/yaml-info.d.ts +0 -1
  73. package/{esm → dist}/src/utils/yaml-info.js +9 -6
  74. package/package.json +23 -16
  75. package/esm/_dnt.shims.d.ts +0 -11
  76. package/esm/_dnt.shims.d.ts.map +0 -1
  77. package/esm/_dnt.shims.js +0 -66
  78. package/esm/mod.d.ts +0 -32
  79. package/esm/mod.d.ts.map +0 -1
  80. package/esm/package.json +0 -3
  81. package/esm/src/ast/node.d.ts +0 -17
  82. package/esm/src/ast/node.d.ts.map +0 -1
  83. package/esm/src/codegen/config.d.ts.map +0 -1
  84. package/esm/src/codegen/endpoints-generator.d.ts +0 -18
  85. package/esm/src/codegen/endpoints-generator.d.ts.map +0 -1
  86. package/esm/src/codegen/generator.d.ts +0 -39
  87. package/esm/src/codegen/generator.d.ts.map +0 -1
  88. package/esm/src/codegen/internal-utils.d.ts.map +0 -1
  89. package/esm/src/codegen/schemas-generator.d.ts +0 -19
  90. package/esm/src/codegen/schemas-generator.d.ts.map +0 -1
  91. package/esm/src/codegen/services-generator.d.ts.map +0 -1
  92. package/esm/src/codegen/types.d.ts +0 -19
  93. package/esm/src/codegen/types.d.ts.map +0 -1
  94. package/esm/src/collect/collector.d.ts +0 -5
  95. package/esm/src/collect/collector.d.ts.map +0 -1
  96. package/esm/src/collect/helpers.d.ts.map +0 -1
  97. package/esm/src/collect/types.d.ts +0 -14
  98. package/esm/src/collect/types.d.ts.map +0 -1
  99. package/esm/src/internal-utils.d.ts.map +0 -1
  100. package/esm/src/parse/deref-proxy.d.ts +0 -4
  101. package/esm/src/parse/deref-proxy.d.ts.map +0 -1
  102. package/esm/src/parse/openapi-types.d.ts.map +0 -1
  103. package/esm/src/parse/parser.d.ts.map +0 -1
  104. package/esm/src/parse/types.d.ts +0 -28
  105. package/esm/src/parse/types.d.ts.map +0 -1
  106. package/esm/src/transform/api-types.d.ts +0 -211
  107. package/esm/src/transform/api-types.d.ts.map +0 -1
  108. package/esm/src/transform/helpers.d.ts.map +0 -1
  109. package/esm/src/transform/transform-document.d.ts +0 -5
  110. package/esm/src/transform/transform-document.d.ts.map +0 -1
  111. package/esm/src/transform/transform-endpoint.d.ts.map +0 -1
  112. package/esm/src/transform/transform-schema.d.ts +0 -6
  113. package/esm/src/transform/transform-schema.d.ts.map +0 -1
  114. package/esm/src/transform/transformer.d.ts +0 -5
  115. package/esm/src/transform/transformer.d.ts.map +0 -1
  116. package/esm/src/transform/types.d.ts.map +0 -1
  117. package/esm/src/utils/action-provider.d.ts +0 -29
  118. package/esm/src/utils/action-provider.d.ts.map +0 -1
  119. package/esm/src/utils/api-data.utils.d.ts.map +0 -1
  120. package/esm/src/utils/array.utils.d.ts.map +0 -1
  121. package/esm/src/utils/asset-manager.d.ts +0 -9
  122. package/esm/src/utils/asset-manager.d.ts.map +0 -1
  123. package/esm/src/utils/class.utils.d.ts.map +0 -1
  124. package/esm/src/utils/common.utils.d.ts +0 -16
  125. package/esm/src/utils/common.utils.d.ts.map +0 -1
  126. package/esm/src/utils/condition.d.ts.map +0 -1
  127. package/esm/src/utils/endpoint.utils.d.ts +0 -3
  128. package/esm/src/utils/endpoint.utils.d.ts.map +0 -1
  129. package/esm/src/utils/error.utils.d.ts.map +0 -1
  130. package/esm/src/utils/factory.d.ts.map +0 -1
  131. package/esm/src/utils/file-system.utils.d.ts.map +0 -1
  132. package/esm/src/utils/generator.utils.d.ts.map +0 -1
  133. package/esm/src/utils/object.utils.d.ts.map +0 -1
  134. package/esm/src/utils/schema.utils.d.ts.map +0 -1
  135. package/esm/src/utils/source-builder.d.ts.map +0 -1
  136. package/esm/src/utils/string-builder/options.d.ts +0 -15
  137. package/esm/src/utils/string-builder/options.d.ts.map +0 -1
  138. package/esm/src/utils/string-builder/string-builder.d.ts +0 -73
  139. package/esm/src/utils/string-builder/string-builder.d.ts.map +0 -1
  140. package/esm/src/utils/string-builder/utils.d.ts.map +0 -1
  141. package/esm/src/utils/string.utils.d.ts +0 -149
  142. package/esm/src/utils/string.utils.d.ts.map +0 -1
  143. package/esm/src/utils/string.utils.types.d.ts.map +0 -1
  144. package/esm/src/utils/type.utils.d.ts.map +0 -1
  145. package/esm/src/utils/yaml-info.d.ts +0 -13
  146. package/esm/src/utils/yaml-info.d.ts.map +0 -1
  147. package/script/_dnt.shims.d.ts +0 -11
  148. package/script/_dnt.shims.d.ts.map +0 -1
  149. package/script/_dnt.shims.js +0 -75
  150. package/script/mod.d.ts +0 -32
  151. package/script/mod.d.ts.map +0 -1
  152. package/script/mod.js +0 -36
  153. package/script/package.json +0 -3
  154. package/script/src/ast/node.d.ts.map +0 -1
  155. package/script/src/ast/node.js +0 -33
  156. package/script/src/codegen/config.d.ts +0 -36
  157. package/script/src/codegen/config.d.ts.map +0 -1
  158. package/script/src/codegen/config.js +0 -12
  159. package/script/src/codegen/endpoints-generator.d.ts.map +0 -1
  160. package/script/src/codegen/endpoints-generator.js +0 -35
  161. package/script/src/codegen/generator.d.ts.map +0 -1
  162. package/script/src/codegen/generator.js +0 -134
  163. package/script/src/codegen/internal-utils.d.ts +0 -2
  164. package/script/src/codegen/internal-utils.d.ts.map +0 -1
  165. package/script/src/codegen/internal-utils.js +0 -9
  166. package/script/src/codegen/schemas-generator.d.ts.map +0 -1
  167. package/script/src/codegen/schemas-generator.js +0 -49
  168. package/script/src/codegen/services-generator.d.ts +0 -18
  169. package/script/src/codegen/services-generator.d.ts.map +0 -1
  170. package/script/src/codegen/services-generator.js +0 -35
  171. package/script/src/codegen/types.d.ts.map +0 -1
  172. package/script/src/codegen/types.js +0 -2
  173. package/script/src/collect/collector.d.ts +0 -5
  174. package/script/src/collect/collector.d.ts.map +0 -1
  175. package/script/src/collect/collector.js +0 -122
  176. package/script/src/collect/helpers.d.ts +0 -5
  177. package/script/src/collect/helpers.d.ts.map +0 -1
  178. package/script/src/collect/helpers.js +0 -37
  179. package/script/src/collect/types.d.ts.map +0 -1
  180. package/script/src/collect/types.js +0 -2
  181. package/script/src/internal-utils.d.ts +0 -2
  182. package/script/src/internal-utils.d.ts.map +0 -1
  183. package/script/src/internal-utils.js +0 -6
  184. package/script/src/parse/deref-proxy.d.ts +0 -4
  185. package/script/src/parse/deref-proxy.d.ts.map +0 -1
  186. package/script/src/parse/deref-proxy.js +0 -71
  187. package/script/src/parse/openapi-types.d.ts +0 -902
  188. package/script/src/parse/openapi-types.d.ts.map +0 -1
  189. package/script/src/parse/openapi-types.js +0 -2
  190. package/script/src/parse/parser.d.ts +0 -18
  191. package/script/src/parse/parser.d.ts.map +0 -1
  192. package/script/src/parse/parser.js +0 -183
  193. package/script/src/parse/types.d.ts.map +0 -1
  194. package/script/src/parse/types.js +0 -5
  195. package/script/src/transform/api-types.d.ts.map +0 -1
  196. package/script/src/transform/api-types.js +0 -2
  197. package/script/src/transform/helpers.d.ts +0 -58
  198. package/script/src/transform/helpers.d.ts.map +0 -1
  199. package/script/src/transform/helpers.js +0 -211
  200. package/script/src/transform/transform-document.d.ts +0 -5
  201. package/script/src/transform/transform-document.d.ts.map +0 -1
  202. package/script/src/transform/transform-document.js +0 -34
  203. package/script/src/transform/transform-endpoint.d.ts +0 -5
  204. package/script/src/transform/transform-endpoint.d.ts.map +0 -1
  205. package/script/src/transform/transform-endpoint.js +0 -273
  206. package/script/src/transform/transform-schema.d.ts +0 -6
  207. package/script/src/transform/transform-schema.d.ts.map +0 -1
  208. package/script/src/transform/transform-schema.js +0 -196
  209. package/script/src/transform/transformer.d.ts +0 -5
  210. package/script/src/transform/transformer.d.ts.map +0 -1
  211. package/script/src/transform/transformer.js +0 -45
  212. package/script/src/transform/types.d.ts +0 -36
  213. package/script/src/transform/types.d.ts.map +0 -1
  214. package/script/src/transform/types.js +0 -6
  215. package/script/src/utils/action-provider.d.ts.map +0 -1
  216. package/script/src/utils/action-provider.js +0 -33
  217. package/script/src/utils/api-data.utils.d.ts +0 -3
  218. package/script/src/utils/api-data.utils.d.ts.map +0 -1
  219. package/script/src/utils/api-data.utils.js +0 -42
  220. package/script/src/utils/array.utils.d.ts +0 -2
  221. package/script/src/utils/array.utils.d.ts.map +0 -1
  222. package/script/src/utils/array.utils.js +0 -10
  223. package/script/src/utils/asset-manager.d.ts.map +0 -1
  224. package/script/src/utils/asset-manager.js +0 -55
  225. package/script/src/utils/class.utils.d.ts +0 -8
  226. package/script/src/utils/class.utils.d.ts.map +0 -1
  227. package/script/src/utils/class.utils.js +0 -33
  228. package/script/src/utils/common.utils.d.ts.map +0 -1
  229. package/script/src/utils/common.utils.js +0 -84
  230. package/script/src/utils/condition.d.ts +0 -4
  231. package/script/src/utils/condition.d.ts.map +0 -1
  232. package/script/src/utils/condition.js +0 -7
  233. package/script/src/utils/endpoint.utils.d.ts +0 -3
  234. package/script/src/utils/endpoint.utils.d.ts.map +0 -1
  235. package/script/src/utils/endpoint.utils.js +0 -11
  236. package/script/src/utils/error.utils.d.ts +0 -2
  237. package/script/src/utils/error.utils.d.ts.map +0 -1
  238. package/script/src/utils/error.utils.js +0 -6
  239. package/script/src/utils/factory.d.ts +0 -21
  240. package/script/src/utils/factory.d.ts.map +0 -1
  241. package/script/src/utils/factory.js +0 -33
  242. package/script/src/utils/file-system.utils.d.ts +0 -11
  243. package/script/src/utils/file-system.utils.d.ts.map +0 -1
  244. package/script/src/utils/file-system.utils.js +0 -79
  245. package/script/src/utils/generator.utils.d.ts +0 -2
  246. package/script/src/utils/generator.utils.d.ts.map +0 -1
  247. package/script/src/utils/generator.utils.js +0 -9
  248. package/script/src/utils/object.utils.d.ts +0 -3
  249. package/script/src/utils/object.utils.d.ts.map +0 -1
  250. package/script/src/utils/object.utils.js +0 -40
  251. package/script/src/utils/schema.utils.d.ts +0 -12
  252. package/script/src/utils/schema.utils.d.ts.map +0 -1
  253. package/script/src/utils/schema.utils.js +0 -206
  254. package/script/src/utils/source-builder.d.ts +0 -236
  255. package/script/src/utils/source-builder.d.ts.map +0 -1
  256. package/script/src/utils/source-builder.js +0 -458
  257. package/script/src/utils/string-builder/options.d.ts.map +0 -1
  258. package/script/src/utils/string-builder/options.js +0 -10
  259. package/script/src/utils/string-builder/string-builder.d.ts.map +0 -1
  260. package/script/src/utils/string-builder/string-builder.js +0 -153
  261. package/script/src/utils/string-builder/utils.d.ts +0 -22
  262. package/script/src/utils/string-builder/utils.d.ts.map +0 -1
  263. package/script/src/utils/string-builder/utils.js +0 -37
  264. package/script/src/utils/string.utils.d.ts.map +0 -1
  265. package/script/src/utils/string.utils.js +0 -382
  266. package/script/src/utils/string.utils.types.d.ts +0 -150
  267. package/script/src/utils/string.utils.types.d.ts.map +0 -1
  268. package/script/src/utils/string.utils.types.js +0 -2
  269. package/script/src/utils/type.utils.d.ts +0 -57
  270. package/script/src/utils/type.utils.d.ts.map +0 -1
  271. package/script/src/utils/type.utils.js +0 -10
  272. package/script/src/utils/yaml-info.d.ts.map +0 -1
  273. package/script/src/utils/yaml-info.js +0 -38
  274. package/src/_dnt.shims.ts +0 -69
  275. package/src/mod.ts +0 -35
  276. package/src/src/ast/node.ts +0 -46
  277. package/src/src/codegen/config.ts +0 -50
  278. package/src/src/codegen/endpoints-generator.ts +0 -72
  279. package/src/src/codegen/generator.ts +0 -192
  280. package/src/src/codegen/internal-utils.ts +0 -6
  281. package/src/src/codegen/schemas-generator.ts +0 -87
  282. package/src/src/codegen/services-generator.ts +0 -77
  283. package/src/src/codegen/types.ts +0 -37
  284. package/src/src/collect/collector.ts +0 -142
  285. package/src/src/collect/helpers.ts +0 -36
  286. package/src/src/collect/types.ts +0 -21
  287. package/src/src/internal-utils.ts +0 -5
  288. package/src/src/parse/deref-proxy.ts +0 -70
  289. package/src/src/parse/openapi-types.ts +0 -953
  290. package/src/src/parse/parser.ts +0 -206
  291. package/src/src/parse/types.ts +0 -28
  292. package/src/src/transform/api-types.ts +0 -259
  293. package/src/src/transform/helpers.ts +0 -251
  294. package/src/src/transform/transform-document.ts +0 -35
  295. package/src/src/transform/transform-endpoint.ts +0 -332
  296. package/src/src/transform/transform-schema.ts +0 -232
  297. package/src/src/transform/transformer.ts +0 -53
  298. package/src/src/transform/types.ts +0 -52
  299. package/src/src/utils/action-provider.ts +0 -58
  300. package/src/src/utils/api-data.utils.ts +0 -48
  301. package/src/src/utils/array.utils.ts +0 -7
  302. package/src/src/utils/asset-manager.ts +0 -51
  303. package/src/src/utils/class.utils.ts +0 -40
  304. package/src/src/utils/common.utils.ts +0 -86
  305. package/src/src/utils/condition.ts +0 -7
  306. package/src/src/utils/endpoint.utils.ts +0 -10
  307. package/src/src/utils/error.utils.ts +0 -3
  308. package/src/src/utils/factory.ts +0 -42
  309. package/src/src/utils/file-system.utils.ts +0 -97
  310. package/src/src/utils/generator.utils.ts +0 -6
  311. package/src/src/utils/object.utils.ts +0 -32
  312. package/src/src/utils/schema.utils.ts +0 -240
  313. package/src/src/utils/source-builder.ts +0 -597
  314. package/src/src/utils/string-builder/options.ts +0 -19
  315. package/src/src/utils/string-builder/string-builder.ts +0 -165
  316. package/src/src/utils/string-builder/utils.ts +0 -69
  317. package/src/src/utils/string.utils.ts +0 -394
  318. package/src/src/utils/string.utils.types.ts +0 -179
  319. package/src/src/utils/type.utils.ts +0 -93
  320. package/src/src/utils/yaml-info.ts +0 -48
  321. /package/{esm → dist}/mod.js +0 -0
  322. /package/{esm → dist}/src/codegen/config.js +0 -0
  323. /package/{esm → dist}/src/codegen/types.js +0 -0
  324. /package/{esm → dist}/src/collect/helpers.js +0 -0
  325. /package/{esm → dist}/src/collect/types.js +0 -0
  326. /package/{esm → dist}/src/internal-utils.js +0 -0
  327. /package/{esm → dist}/src/parse/openapi-types.js +0 -0
  328. /package/{esm → dist}/src/parse/types.js +0 -0
  329. /package/{esm → dist}/src/transform/api-types.js +0 -0
  330. /package/{esm → dist}/src/transform/transformer.js +0 -0
  331. /package/{esm → dist}/src/transform/types.js +0 -0
  332. /package/{esm → dist}/src/utils/api-data.utils.js +0 -0
  333. /package/{esm → dist}/src/utils/array.utils.js +0 -0
  334. /package/{esm → dist}/src/utils/common.utils.js +0 -0
  335. /package/{esm → dist}/src/utils/condition.js +0 -0
  336. /package/{esm → dist}/src/utils/endpoint.utils.js +0 -0
  337. /package/{esm → dist}/src/utils/error.utils.js +0 -0
  338. /package/{esm → dist}/src/utils/file-system.utils.js +0 -0
  339. /package/{esm → dist}/src/utils/generator.utils.js +0 -0
  340. /package/{esm → dist}/src/utils/string-builder/options.js +0 -0
  341. /package/{esm → dist}/src/utils/string.utils.types.js +0 -0
  342. /package/{esm → dist}/src/utils/type.utils.js +0 -0
@@ -1,597 +0,0 @@
1
- import { isNullish } from './common.utils.js';
2
- import { type Condition, evalCondition } from './condition.js';
3
- import { defaultStringBuilderOptions, type StringBuilderOptions } from './string-builder/options.js';
4
- import { type AppendParam, type BuilderFn, StringBuilder } from './string-builder/string-builder.js';
5
- import type { AppendValue } from './string-builder/utils.js';
6
- import { removeStr, trimEnd } from './string.utils.js';
7
- import type { Nullable } from './type.utils.js';
8
-
9
- export type IndentOptions = { readonly type: 'tabs' } | { readonly type: 'spaces'; readonly count: number };
10
-
11
- /**
12
- * Options for the `SourceBuilder` class.
13
- */
14
- export type SourceBuilderOptions = StringBuilderOptions & {
15
- /**
16
- * The type of indentation to use.
17
- * @default { type: 'spaces', count: 2 }
18
- */
19
- indent: IndentOptions;
20
- };
21
-
22
- /**
23
- * Default options for the `SourceBuilder` class.
24
- */
25
- export const defaultSourceBuilderOptions: SourceBuilderOptions = {
26
- ...defaultStringBuilderOptions,
27
-
28
- indent: { type: 'spaces', count: 2 },
29
- };
30
-
31
- type KnownParatheses = '()' | '[]' | '{}' | '<>';
32
- export type Paratheses<TBuilder extends StringBuilder> =
33
- | KnownParatheses
34
- | Omit<string, KnownParatheses>
35
- | [open: Nullable<AppendValue<TBuilder>>, close: Nullable<AppendValue<TBuilder>>];
36
-
37
- export type SeparatorBuidlerFn<TBuilder, TItem> = (
38
- builder: TBuilder,
39
- previousItem: TItem,
40
- nextItem: TItem,
41
- previousItemIndex: number,
42
- nextItemIndex: number,
43
- ) => void;
44
- export type Separator<TBuilder, TItem> = string | SeparatorBuidlerFn<TBuilder, TItem>;
45
-
46
- /**
47
- * Options for the `parenthesize` methods of the `SourceBuilder`.
48
- * @see SourceBuilder.parenthesize
49
- * @see SourceBuilder.parenthesizeIf
50
- */
51
- export type ParenthesizeOptions = {
52
- /**
53
- * Whether to add line breaks after start and before end of the parentheses.
54
- * @default false
55
- */
56
- readonly multiline?: boolean;
57
- /**
58
- * Whether to indent the content inside parentheses.
59
- * @default true
60
- */
61
- readonly indent?: boolean;
62
- /**
63
- * Whether to pretend that the current line and the one before is empty or consists only of whitespace characters.
64
- * This is useful when the parentheses are used as a block and `ensurePreviousLineEmpty` is used.
65
- * @default true
66
- */
67
- readonly pretendEmpty?: boolean;
68
- };
69
-
70
- /**
71
- * Options for the `forEach` methods of the `SourceBuilder`.
72
- * @see SourceBuilder.forEach
73
- * @see SourceBuilder.forEachIf
74
- */
75
- export type ForEachOptions<TBuilder, TItem> = {
76
- /**
77
- * The condition to check before adding content for each item. Defaults to `true`.
78
- */
79
- readonly condition?: Condition<TItem>;
80
- /**
81
- * The seperator to add between items. Default to `undefined`.
82
- */
83
- readonly separator?: Separator<TBuilder, TItem>;
84
- };
85
-
86
- type SwitchCase<TValue, TBuilder extends StringBuilder> = {
87
- case: TValue;
88
- value: AppendValue<TBuilder>;
89
- };
90
-
91
- type LinePrefixNode = {
92
- kind: 'indent' | 'prefix';
93
- chars: string;
94
- };
95
-
96
- export function getIndentString(options: IndentOptions): string {
97
- return options.type === 'tabs' ? '\t' : ' '.repeat(options.count);
98
- }
99
-
100
- /**
101
- * Represents an in-memory source file.
102
- */
103
- export class SourceBuilder<TAdditionalAppends = never> extends StringBuilder<TAdditionalAppends> {
104
- private readonly __options: SourceBuilderOptions;
105
- private readonly _indentString: string;
106
-
107
- private _isLineIndented: boolean = false;
108
- private _isLastLineEmpty: boolean = true;
109
- private _isCurrentLineEmpty: boolean = true;
110
- private _linePrefixNodes: LinePrefixNode[] = [];
111
-
112
- /**
113
- * Gets the options used by this instance.
114
- */
115
- public override get options(): SourceBuilderOptions {
116
- return this.__options;
117
- }
118
-
119
- public get isCurrentLineEmpty(): boolean {
120
- return this._isCurrentLineEmpty;
121
- }
122
-
123
- /**
124
- * Initializes a new instance of the SourceBuilder class.
125
- * @param options The options to use for this instance.
126
- */
127
- constructor(options?: Partial<SourceBuilderOptions>) {
128
- super(options);
129
- this.__options = { ...defaultSourceBuilderOptions, ...options };
130
- this._indentString = getIndentString(this.__options.indent);
131
- }
132
-
133
- /**
134
- * Creates a new instance of the SourceBuilder class from a string.
135
- * @param str The string to use as the initial content of the builder.
136
- * @param options The options to use for this instance.
137
- * @returns A new instance of the SourceBuilder class.
138
- */
139
- public static override fromString(str: string, options?: Partial<SourceBuilderOptions>): SourceBuilder {
140
- const builder = new SourceBuilder(options);
141
- builder.append(str);
142
- return builder;
143
- }
144
-
145
- /**
146
- * Builds a string using a callback function that receives a `SourceBuilder` instance.
147
- * @param buildAction The callback function that receives a `SourceBuilder` instance to build the string.
148
- * @param options The options to use for the `SourceBuilder` instance.
149
- * @returns The string built by the `SourceBuilder` instance.
150
- */
151
- public static override build(
152
- buildAction: AppendParam<SourceBuilder, never>,
153
- options?: Partial<SourceBuilderOptions>,
154
- ): string {
155
- const builder = new SourceBuilder(options);
156
- builder.append(buildAction);
157
- return builder.toString();
158
- }
159
-
160
- protected override appendSingle(value: AppendParam<this, TAdditionalAppends>) {
161
- if (isNullish(value)) return;
162
- if (Array.isArray(value)) {
163
- for (const part of value) {
164
- this.appendSingle(part);
165
- }
166
- return;
167
- }
168
- if (typeof value === 'function') {
169
- (value as (builder: this) => void)(this);
170
- return;
171
- }
172
- if (typeof value === 'number' || typeof value === 'boolean') {
173
- value = value.toString();
174
- }
175
- if (typeof value === 'object' && value !== null && '__type' in value && value.__type === 'append-value-group') {
176
- this.forEach(value.values, (builder, part) => builder.append(part), { separator: value.separator ?? undefined });
177
- return;
178
- }
179
- if (typeof value !== 'string') {
180
- return;
181
- }
182
-
183
- let lineStartIndex = 0;
184
- for (let i = 0; i < value.length; i++) {
185
- if (value[i] === '\r') continue;
186
-
187
- if (value[i] === '\n') {
188
- let lineLength = i - lineStartIndex;
189
- for (let j = i; j > 0 && value[j - 1] === '\r'; j--) {
190
- lineLength--;
191
- }
192
-
193
- if (lineLength > 0) {
194
- if (!this._isLineIndented) {
195
- this.appendIndent();
196
- }
197
- this.str += removeStr(value.substring(lineStartIndex, i), '\r');
198
- }
199
-
200
- this._isLastLineEmpty = this._isCurrentLineEmpty;
201
- if (this._isCurrentLineEmpty && this._linePrefixNodes.some((x) => x.kind !== 'indent')) {
202
- this.appendIndent();
203
- }
204
- this.str = trimEnd(this.str, false) + '\n';
205
- this._isLineIndented = false;
206
- this._isCurrentLineEmpty = true;
207
-
208
- lineStartIndex = i + 1;
209
- }
210
-
211
- if (value[i] !== '\r' && value[i] !== '\n' && value[i] !== ' ' && value[i] !== '\t') {
212
- this._isCurrentLineEmpty = false;
213
- }
214
- }
215
-
216
- if (!this._isLineIndented && lineStartIndex < value.length) {
217
- this.appendIndent();
218
- this._isLineIndented = true;
219
- }
220
-
221
- this.str += removeStr(value.substring(lineStartIndex), '\r');
222
- }
223
-
224
- /**
225
- * Appends one or more strings to the end of the current SourceBuilder if the specified condition is true.
226
- * @param condition The condition to check before appending the specified value.
227
- * @param value The string(s) to append.
228
- * @returns The current SourceBuilder.
229
- */
230
- public appendIf(condition: Condition, ...value: AppendParam<this, TAdditionalAppends>[]): this {
231
- if (evalCondition(condition)) {
232
- return this.append(...value);
233
- }
234
- return this;
235
- }
236
-
237
- /**
238
- * Appends one or more strings to the end of the current SourceBuilder, followed by a line terminator.
239
- * @param value The string(s) to append.
240
- * @returns The current SourceBuilder.
241
- */
242
- public override appendLine(...value: AppendParam<this, TAdditionalAppends>[]): this {
243
- return this.append(...value, '\n');
244
- }
245
-
246
- /**
247
- * Appends one or more strings to the end of the current SourceBuilder, followed by a line terminator, if the specified condition is true.
248
- * @param condition The condition to check before appending the specified value.
249
- * @param value The string(s) to append.
250
- * @returns The current SourceBuilder.
251
- */
252
- public appendLineIf(condition: Condition, ...value: AppendParam<this, TAdditionalAppends>[]): this {
253
- if (evalCondition(condition)) {
254
- return this.appendLine(...value);
255
- }
256
- return this;
257
- }
258
-
259
- /**
260
- * Appends the specified values to this source builder with the specified line prefix.
261
- * @param {string} prefix - The line prefix to use.
262
- * @param {...(string|null|undefined)} value - The values to append.
263
- * @returns {SourceBuilder} This source builder instance.
264
- */
265
- public appendWithLinePrefix(prefix: string, ...value: AppendParam<this, TAdditionalAppends>[]): this {
266
- this._linePrefixNodes.push({ chars: prefix, kind: 'prefix' });
267
- try {
268
- this.append(...value);
269
- } finally {
270
- this._linePrefixNodes.pop();
271
- }
272
-
273
- return this;
274
- }
275
-
276
- /**
277
- * Appends the specified values to this source builder as a new line with the specified line prefix.
278
- * @param {string} prefix - The line prefix to use.
279
- * @param {...(string|null|undefined)} value - The values to append.
280
- * @returns {SourceBuilder} This source builder instance.
281
- */
282
- public appendLineWithLinePrefix(prefix: string, ...value: AppendParam<this, TAdditionalAppends>[]): this {
283
- return this.appendWithLinePrefix(prefix, ...value, '\n');
284
- }
285
-
286
- /**
287
- * Prepends one or more strings to the beginning of the current SourceBuilder if the specified condition is true.
288
- * @param condition The condition to check before prepending the specified value.
289
- * @param value The string(s) to prepend.
290
- * @returns The current SourceBuilder.
291
- */
292
- public prependIf(condition: Condition, ...value: AppendParam<StringBuilder, never>[]): this {
293
- if (evalCondition(condition)) {
294
- this.prepend(...value);
295
- }
296
- return this;
297
- }
298
-
299
- /**
300
- * Prepends one or more strings to the beginning of the current SourceBuilder, followed by a line terminator, if the specified condition is true.
301
- * @param condition The condition to check before appending the specified value.
302
- * @param value The string(s) to prepend.
303
- * @returns The current SourceBuilder.
304
- */
305
- public prependLineIf(condition: Condition, ...value: AppendParam<StringBuilder, never>[]): this {
306
- if (evalCondition(condition)) {
307
- this.prependLine(...value);
308
- }
309
- return this;
310
- }
311
-
312
- /**
313
- * Ensures that the current line and the one before is empty or consists only of whitespace characters.
314
- * @returns A reference to this instance.
315
- */
316
- public ensurePreviousLineEmpty(): this {
317
- if (!this._isCurrentLineEmpty) {
318
- this.appendLine();
319
- }
320
- if (!this._isLastLineEmpty) {
321
- this.appendLine();
322
- }
323
-
324
- return this;
325
- }
326
-
327
- /**
328
- * Ensures that the current line is empty or consists only of whitespace characters.
329
- * @returns A reference to this instance.
330
- */
331
- public ensureCurrentLineEmpty(): this {
332
- if (!this._isCurrentLineEmpty) {
333
- this.appendLine();
334
- }
335
-
336
- return this;
337
- }
338
-
339
- /**
340
- * Pretends that the current line and the one before is empty or consists only of whitespace characters.
341
- * @returns A reference to this instance.
342
- */
343
- public pretendPreviousLineEmpty(): this {
344
- this._isLastLineEmpty = true;
345
- this._isCurrentLineEmpty = true;
346
- return this;
347
- }
348
-
349
- /**
350
- * Pretends that the current line is empty or consists only of whitespace characters.
351
- * @returns A reference to this instance.
352
- */
353
- public pretendCurrentLineEmpty(): this {
354
- this._isCurrentLineEmpty = true;
355
- return this;
356
- }
357
-
358
- /**
359
- * Inserts specified content if the specified condition is true.
360
- * @param condition The condition to check before inserting content.
361
- * @param builderFn The function to add content if the condition is true.
362
- * @param elseBuilderFn The function to add content if the condition is false.
363
- * @returns A reference to this instance.
364
- */
365
- public if(
366
- condition: Condition,
367
- builderFn: AppendParam<this, TAdditionalAppends>,
368
- elseBuilderFn?: AppendParam<this, TAdditionalAppends>,
369
- ): this {
370
- return evalCondition(condition) ? this.append(builderFn) : this.append(elseBuilderFn);
371
- }
372
-
373
- public switch<T extends string | number>(
374
- value: T,
375
- cases: Record<T, AppendParam<this, TAdditionalAppends>>,
376
- defaultBuilderFn?: AppendParam<this, TAdditionalAppends>,
377
- ): this;
378
- public switch<T>(
379
- value: T,
380
- cases: SwitchCase<T, this>[],
381
- defaultBuilderFn?: AppendParam<this, TAdditionalAppends>,
382
- equals?: (a: T, b: T) => boolean,
383
- ): this;
384
- public switch<T>(
385
- value: T,
386
- cases: Record<string | number, AppendParam<this, TAdditionalAppends>> | SwitchCase<T, this>[],
387
- defaultBuilderFn?: AppendParam<this, TAdditionalAppends>,
388
- equals?: (a: T, b: T) => boolean,
389
- ): this {
390
- if ((typeof value === 'string' || typeof value === 'number') && !Array.isArray(cases)) {
391
- return this.append(cases[value] ?? defaultBuilderFn);
392
- } else if (Array.isArray(cases)) {
393
- for (const c of cases) {
394
- if (equals ? equals(value, c.case) : value === c.case) {
395
- return this.append(c.value);
396
- }
397
- }
398
- return this.append(defaultBuilderFn);
399
- }
400
-
401
- return this;
402
- }
403
-
404
- /**
405
- * Adds one indentation level. If the current line already contains characters, only subsequent lines are affected.
406
- * @param builderFn The function to add indented content or the content itself.
407
- * @param condition The condition to check before adding indentation.
408
- * @returns A reference to this instance.
409
- */
410
- public indent(value: AppendParam<this, TAdditionalAppends>): this {
411
- this._linePrefixNodes.push({ chars: this._indentString, kind: 'indent' });
412
- try {
413
- this.append(value);
414
- } finally {
415
- this._linePrefixNodes.pop();
416
- }
417
-
418
- return this;
419
- }
420
-
421
- /**
422
- * Adds one indentation level if the specified condition is true. If the current line already contains characters, only subsequent lines are affected.
423
- * @param builderFn The function to add indented content or the content itself.
424
- * @param condition The condition to check before adding indentation.
425
- * @returns A reference to this instance.
426
- */
427
- public indentIf(condition: Condition, value: AppendParam<this, TAdditionalAppends>): this {
428
- return evalCondition(condition) ? this.indent(value) : this.append(value);
429
- }
430
-
431
- /**
432
- * Adds parentheses around the specified content.
433
- * @param brackets The brackets to use.
434
- * @param value The function to add content inside parentheses or one string that should be parenthesized.
435
- * @param options Options for the parentheses.
436
- * @returns A reference to this instance.
437
- */
438
- public parenthesize(
439
- brackets: Paratheses<this>,
440
- value: AppendParam<this, TAdditionalAppends>,
441
- options?: ParenthesizeOptions,
442
- ): this {
443
- const multiline = options?.multiline ?? false;
444
- const indent = options?.indent ?? true;
445
- const pretendEmpty = options?.pretendEmpty ?? true;
446
-
447
- if (multiline) {
448
- this.appendLine(brackets[0] ?? '');
449
- } else {
450
- this.append(brackets[0] ?? '');
451
- }
452
-
453
- if (pretendEmpty) this.pretendPreviousLineEmpty();
454
-
455
- if (indent !== false) {
456
- this.indent(value);
457
- } else {
458
- this.append(value);
459
- }
460
-
461
- if (multiline) this.ensureCurrentLineEmpty();
462
- this.append(brackets[1] ?? '');
463
-
464
- return this;
465
- }
466
-
467
- /**
468
- * Adds parentheses around the specified content if the specified condition is true.
469
- * @param condition The condition to check before adding parentheses.
470
- * @param brackets The brackets to use.
471
- * @param value The function to add content inside parentheses or one string that should be parenthesized.
472
- * @param options Options for the parentheses.
473
- * @returns A reference to this instance.
474
- */
475
- public parenthesizeIf(
476
- condition: Condition,
477
- brackets: Paratheses<this>,
478
- value: AppendParam<this, TAdditionalAppends>,
479
- options?: ParenthesizeOptions,
480
- ): this {
481
- if (evalCondition(condition)) {
482
- return this.parenthesize(brackets, value, options);
483
- } else if (options?.indent) {
484
- return this.indent(value);
485
- } else {
486
- return this.append(value);
487
- }
488
- }
489
-
490
- /**
491
- * Adds content for each item in the specified array.
492
- * @param items The items to add content for.
493
- * @param builderFn The function to add content for each item.
494
- * @param options The options for the loop.
495
- * @returns A reference to this instance.
496
- */
497
- public forEach<T>(
498
- items: Iterable<T>,
499
- builderFn: (builder: this, item: T, index: number) => void,
500
- options?: ForEachOptions<this, T>,
501
- ): this {
502
- const condition = options?.condition ?? true;
503
- if (options?.condition === false) return this;
504
-
505
- const seperator = options?.separator;
506
- let previousIndex: number = -1;
507
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
508
- let previousItem: T = undefined!;
509
- let index = 0;
510
- for (const item of items) {
511
- if (evalCondition(condition, item)) {
512
- if (previousIndex >= 0 && seperator) {
513
- if (typeof seperator === 'string') {
514
- this.append(seperator);
515
- } else {
516
- seperator(this, previousItem, item, previousIndex, index);
517
- }
518
- }
519
-
520
- builderFn(this, item, index);
521
- previousIndex = index;
522
- previousItem = item;
523
- }
524
-
525
- index++;
526
- }
527
-
528
- return this;
529
- }
530
-
531
- public forEachIf<T>(
532
- condition: Condition,
533
- items: Iterable<T>,
534
- builderFn: (builder: this, item: T, index: number) => void,
535
- options?: ForEachOptions<this, T>,
536
- ): this {
537
- if (evalCondition(condition)) {
538
- return this.forEach(items, builderFn, options);
539
- }
540
- return this;
541
- }
542
-
543
- public appendSeparated(items: Iterable<AppendParam<this, TAdditionalAppends>>, separator: string): this {
544
- return this.forEach(items, (builder, item) => builder.append(item), { separator });
545
- }
546
-
547
- public appendSeparatedIf(
548
- condition: Condition,
549
- items: Iterable<AppendParam<this, TAdditionalAppends>>,
550
- separator: string,
551
- ): this {
552
- if (evalCondition(condition)) {
553
- return this.appendSeparated(items, separator);
554
- }
555
- return this;
556
- }
557
-
558
- private appendIndent(): void {
559
- this.str += this._linePrefixNodes.map((n) => n.chars).join('');
560
- }
561
- }
562
-
563
- function _builderTemplate<T extends SourceBuilder>(
564
- template: readonly string[] | ArrayLike<string>,
565
- ...substitutions: AppendValue<T>[]
566
- ): BuilderFn<T> {
567
- return (b) => {
568
- let lastLine: string | undefined = undefined;
569
- for (let i = template.length - 1; i >= 0; i--) {
570
- if (template[i].includes('\n')) {
571
- lastLine = template[i];
572
- break;
573
- }
574
- }
575
- const indent = lastLine?.match(/\n( *).*$/)?.[1] ?? '';
576
- const regex = new RegExp(`\\n {0,${indent.length}}`, 'g');
577
-
578
- for (let i = 0; i < template.length; i++) {
579
- b.append(template[i].replace(regex, '\n'));
580
- if (i < substitutions.length) b.append(substitutions[i]);
581
- }
582
- };
583
- }
584
-
585
- const _builderTemplateExtensions = {
586
- indent: <T extends SourceBuilder>(
587
- template: readonly string[] | ArrayLike<string>,
588
- ...substitutions: AppendValue<T>[]
589
- ): BuilderFn<T> => {
590
- return (b) => b.indent(_builderTemplate(template, ...substitutions));
591
- },
592
- } as const;
593
-
594
- export const builderTemplate: typeof _builderTemplate & typeof _builderTemplateExtensions = Object.assign(
595
- _builderTemplate,
596
- _builderTemplateExtensions,
597
- );
@@ -1,19 +0,0 @@
1
- import { EOL } from 'node:os';
2
-
3
- /**
4
- * Options for the `StringBuilder` class.
5
- */
6
- export type StringBuilderOptions = {
7
- /**
8
- * The string to use for new lines.
9
- * @default os.EOL
10
- */
11
- readonly newLine: string;
12
- };
13
-
14
- /**
15
- * Default options for the `StringBuilder` class.
16
- */
17
- export const defaultStringBuilderOptions: StringBuilderOptions = {
18
- newLine: EOL,
19
- };