@smoothbricks/nx-plugin 0.4.25 → 0.4.27

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 (348) hide show
  1. package/README.md +1078 -98
  2. package/dist/bin/smoo-nx-bound-failures.d.ts +3 -0
  3. package/dist/bin/smoo-nx-bound-failures.d.ts.map +1 -0
  4. package/dist/bin/smoo-nx-bound-failures.js +31 -0
  5. package/dist/bin/smoo-nx-cargo-hash.js +28 -3
  6. package/dist/bin/smoo-nx-exec.js +16 -85
  7. package/dist/bin/smoo-nx-failures.d.ts +3 -0
  8. package/dist/bin/smoo-nx-failures.d.ts.map +1 -0
  9. package/dist/bin/smoo-nx-failures.js +3 -0
  10. package/dist/bin/smoo-nx-nextest-extract.js +3 -2
  11. package/dist/bin/smoo-nx-test-walks.d.ts +3 -0
  12. package/dist/bin/smoo-nx-test-walks.d.ts.map +1 -0
  13. package/dist/bin/smoo-nx-test-walks.js +34 -0
  14. package/dist/bounded-test-policy.d.ts +7 -1
  15. package/dist/bounded-test-policy.d.ts.map +1 -1
  16. package/dist/bounded-test-policy.js +101 -40
  17. package/dist/build-state-output-policy.d.ts +25 -0
  18. package/dist/build-state-output-policy.d.ts.map +1 -0
  19. package/dist/build-state-output-policy.js +64 -0
  20. package/dist/cargo-closure-input.d.ts +16 -17
  21. package/dist/cargo-closure-input.d.ts.map +1 -1
  22. package/dist/cargo-closure-input.js +37 -44
  23. package/dist/cargo-dev-profile.d.ts +37 -0
  24. package/dist/cargo-dev-profile.d.ts.map +1 -0
  25. package/dist/cargo-dev-profile.js +108 -0
  26. package/dist/cargo-source-hash.d.ts +50 -8
  27. package/dist/cargo-source-hash.d.ts.map +1 -1
  28. package/dist/cargo-source-hash.js +691 -71
  29. package/dist/cargo-toolchain-policy.d.ts +4 -0
  30. package/dist/cargo-toolchain-policy.d.ts.map +1 -1
  31. package/dist/cargo-toolchain-policy.js +5 -0
  32. package/dist/cargo-workspace.d.ts +108 -12
  33. package/dist/cargo-workspace.d.ts.map +1 -1
  34. package/dist/cargo-workspace.js +299 -18
  35. package/dist/cross-check-policy.d.ts +21 -3
  36. package/dist/cross-check-policy.d.ts.map +1 -1
  37. package/dist/cross-check-policy.js +24 -6
  38. package/dist/disk-command-policy.d.ts +7 -0
  39. package/dist/disk-command-policy.d.ts.map +1 -0
  40. package/dist/disk-command-policy.js +98 -0
  41. package/dist/ensure-built-receipt.d.ts +25 -0
  42. package/dist/ensure-built-receipt.d.ts.map +1 -0
  43. package/dist/ensure-built-receipt.js +261 -0
  44. package/dist/ensure-built-witness.d.ts +57 -0
  45. package/dist/ensure-built-witness.d.ts.map +1 -0
  46. package/dist/ensure-built-witness.js +412 -0
  47. package/dist/ensure-built.d.ts +213 -12
  48. package/dist/ensure-built.d.ts.map +1 -1
  49. package/dist/ensure-built.js +624 -177
  50. package/dist/eslint-file-set.d.ts +7 -0
  51. package/dist/eslint-file-set.d.ts.map +1 -0
  52. package/dist/eslint-file-set.js +12 -0
  53. package/dist/executors/bounded-exec/cpu-pool.d.ts +63 -0
  54. package/dist/executors/bounded-exec/cpu-pool.d.ts.map +1 -0
  55. package/dist/executors/bounded-exec/cpu-pool.js +379 -0
  56. package/dist/executors/bounded-exec/cpu-tokens.d.ts +82 -0
  57. package/dist/executors/bounded-exec/cpu-tokens.d.ts.map +1 -0
  58. package/dist/executors/bounded-exec/cpu-tokens.js +114 -0
  59. package/dist/executors/bounded-exec/disk-lease.d.ts +61 -0
  60. package/dist/executors/bounded-exec/disk-lease.d.ts.map +1 -0
  61. package/dist/executors/bounded-exec/disk-lease.js +91 -0
  62. package/dist/executors/bounded-exec/executor.d.ts +45 -2
  63. package/dist/executors/bounded-exec/executor.d.ts.map +1 -1
  64. package/dist/executors/bounded-exec/executor.js +447 -52
  65. package/dist/executors/bounded-exec/gateway-lease.d.ts +52 -0
  66. package/dist/executors/bounded-exec/gateway-lease.d.ts.map +1 -0
  67. package/dist/executors/bounded-exec/gateway-lease.js +143 -0
  68. package/dist/executors/bounded-exec/hang-diagnostics.d.ts +35 -0
  69. package/dist/executors/bounded-exec/hang-diagnostics.d.ts.map +1 -0
  70. package/dist/executors/bounded-exec/hang-diagnostics.js +231 -0
  71. package/dist/executors/bounded-exec/progress.d.ts +35 -0
  72. package/dist/executors/bounded-exec/progress.d.ts.map +1 -0
  73. package/dist/executors/bounded-exec/progress.js +79 -0
  74. package/dist/executors/bounded-exec/ram-temp-fixtures.d.ts +3 -0
  75. package/dist/executors/bounded-exec/ram-temp-fixtures.d.ts.map +1 -0
  76. package/dist/executors/bounded-exec/ram-temp-fixtures.js +12 -0
  77. package/dist/executors/bounded-exec/ram-temp-held.d.ts +91 -0
  78. package/dist/executors/bounded-exec/ram-temp-held.d.ts.map +1 -0
  79. package/dist/executors/bounded-exec/ram-temp-held.js +152 -0
  80. package/dist/executors/bounded-exec/ram-temp.d.ts +223 -0
  81. package/dist/executors/bounded-exec/ram-temp.d.ts.map +1 -0
  82. package/dist/executors/bounded-exec/ram-temp.js +1528 -0
  83. package/dist/executors/bounded-exec/schema.d.ts +29 -11
  84. package/dist/executors/bounded-exec/schema.d.ts.map +1 -1
  85. package/dist/executors/bounded-exec/verdict.d.ts +162 -0
  86. package/dist/executors/bounded-exec/verdict.d.ts.map +1 -0
  87. package/dist/executors/bounded-exec/verdict.js +318 -0
  88. package/dist/executors/shard-timings/executor.d.ts +14 -0
  89. package/dist/executors/shard-timings/executor.d.ts.map +1 -0
  90. package/dist/executors/shard-timings/executor.js +25 -0
  91. package/dist/executors/shard-timings/schema.d.ts +4 -0
  92. package/dist/executors/shard-timings/schema.d.ts.map +1 -0
  93. package/dist/executors/shard-timings/schema.js +1 -0
  94. package/dist/failure-summary.d.ts +5 -0
  95. package/dist/failure-summary.d.ts.map +1 -0
  96. package/dist/failure-summary.js +118 -0
  97. package/dist/generators/create-package/generator.d.ts.map +1 -1
  98. package/dist/generators/create-package/generator.js +6 -2
  99. package/dist/index.d.ts +14 -2
  100. package/dist/index.d.ts.map +1 -1
  101. package/dist/index.js +435 -230
  102. package/dist/managed-assets.d.ts +6 -0
  103. package/dist/managed-assets.d.ts.map +1 -1
  104. package/dist/managed-files/ci-workflow.d.ts +13 -0
  105. package/dist/managed-files/ci-workflow.d.ts.map +1 -1
  106. package/dist/managed-files/ci-workflow.js +44 -13
  107. package/dist/managed-files/files.d.ts +7 -1
  108. package/dist/managed-files/files.d.ts.map +1 -1
  109. package/dist/managed-files/files.js +62 -8
  110. package/dist/managed-files/json-edit.d.ts +21 -0
  111. package/dist/managed-files/json-edit.d.ts.map +1 -0
  112. package/dist/managed-files/json-edit.js +295 -0
  113. package/dist/managed-files/managed-format.d.ts +33 -1
  114. package/dist/managed-files/managed-format.d.ts.map +1 -1
  115. package/dist/managed-files/managed-format.js +82 -2
  116. package/dist/managed-files/pr-preview-cleanup-workflow.d.ts +7 -0
  117. package/dist/managed-files/pr-preview-cleanup-workflow.d.ts.map +1 -1
  118. package/dist/managed-files/pr-preview-cleanup-workflow.js +68 -4
  119. package/dist/managed-files/publish-workflow.d.ts.map +1 -1
  120. package/dist/managed-files/publish-workflow.js +231 -48
  121. package/dist/managed-files/tree.d.ts +5 -0
  122. package/dist/managed-files/tree.d.ts.map +1 -1
  123. package/dist/managed-files/tree.js +5 -3
  124. package/dist/manifest-hash.js +3 -2
  125. package/dist/nextest-code-bound.d.ts +62 -0
  126. package/dist/nextest-code-bound.d.ts.map +1 -0
  127. package/dist/nextest-code-bound.js +154 -0
  128. package/dist/nextest-extraction.d.ts +24 -8
  129. package/dist/nextest-extraction.d.ts.map +1 -1
  130. package/dist/nextest-extraction.js +42 -7
  131. package/dist/package-target-policy.d.ts.map +1 -1
  132. package/dist/package-target-policy.js +3 -5
  133. package/dist/release-config-policy.d.ts.map +1 -1
  134. package/dist/release-config-policy.js +3 -5
  135. package/dist/runtime-input.d.ts +14 -0
  136. package/dist/runtime-input.d.ts.map +1 -0
  137. package/dist/runtime-input.js +15 -0
  138. package/dist/rust-tokens.d.ts +11 -0
  139. package/dist/rust-tokens.d.ts.map +1 -0
  140. package/dist/rust-tokens.js +81 -0
  141. package/dist/task-results.d.ts +6 -0
  142. package/dist/task-results.d.ts.map +1 -0
  143. package/dist/task-results.js +61 -0
  144. package/dist/test-program.d.ts +54 -0
  145. package/dist/test-program.d.ts.map +1 -0
  146. package/dist/test-program.js +292 -0
  147. package/dist/test-timings.d.ts +71 -0
  148. package/dist/test-timings.d.ts.map +1 -0
  149. package/dist/test-timings.js +393 -0
  150. package/dist/test-walk-policy.d.ts +103 -0
  151. package/dist/test-walk-policy.d.ts.map +1 -0
  152. package/dist/test-walk-policy.js +657 -0
  153. package/dist/testing.d.ts +177 -0
  154. package/dist/testing.d.ts.map +1 -0
  155. package/dist/testing.js +824 -0
  156. package/dist/type-guards.d.ts +3 -0
  157. package/dist/type-guards.d.ts.map +1 -0
  158. package/dist/type-guards.js +4 -0
  159. package/dist/typecheck-test-policy.d.ts +8 -5
  160. package/dist/typecheck-test-policy.d.ts.map +1 -1
  161. package/dist/typecheck-test-policy.js +113 -100
  162. package/dist/workspace-config-policy.d.ts +8 -0
  163. package/dist/workspace-config-policy.d.ts.map +1 -1
  164. package/dist/workspace-config-policy.js +175 -63
  165. package/dist/workspace-index.d.ts +12 -0
  166. package/dist/workspace-index.d.ts.map +1 -0
  167. package/dist/workspace-index.js +17 -0
  168. package/dist/workspace-manifest.d.ts +37 -1
  169. package/dist/workspace-manifest.d.ts.map +1 -1
  170. package/executors.json +5 -0
  171. package/loader.js +13 -11
  172. package/managed/raw/envrc +33 -62
  173. package/managed/raw/git-format-staged.yml +30 -35
  174. package/managed/raw/patches/nx@23.2.1.patch +3484 -0
  175. package/managed/raw/tooling/direnv/apple-developer.sh +83 -0
  176. package/managed/raw/tooling/direnv/developer-links.ts +607 -17
  177. package/managed/raw/tooling/direnv/devenv.smoo.nix +314 -92
  178. package/managed/raw/tooling/direnv/github-actions-bootstrap.sh +20 -11
  179. package/managed/raw/tooling/direnv/linked-snapshots.ts +845 -0
  180. package/managed/raw/tooling/direnv/nx-socket-dir.sh +82 -0
  181. package/managed/raw/tooling/direnv/repo-path +14 -2
  182. package/managed/raw/tooling/direnv/setup-environment.ts +1009 -118
  183. package/managed/raw/tooling/direnv/toolchain-stamp.ts +45 -3
  184. package/managed/raw/tooling/direnv/ttsc-env.sh +39 -0
  185. package/managed/raw/tooling/git-hooks/commit-msg.sh +5 -6
  186. package/managed/raw/tooling/git-hooks/pre-commit.sh +7 -6
  187. package/managed/raw/tooling/git-hooks/pre-push.sh +20 -15
  188. package/managed/templates/github/actions/cache-nix-devenv/action.yml +6 -6
  189. package/managed/templates/github/actions/cache-node-modules/action.yml +6 -4
  190. package/managed/templates/github/actions/cache-nx/action.yml +1 -1
  191. package/managed/templates/github/actions/cache-ttsc-plugins/action.yml +1 -1
  192. package/managed/templates/github/actions/forgejo-artifact.cjs +99 -0
  193. package/managed/templates/github/actions/forgejo-download-artifact/action.yml +49 -0
  194. package/managed/templates/github/actions/forgejo-download-artifact/index.cjs +7 -0
  195. package/managed/templates/github/actions/forgejo-upload-artifact/action.yml +65 -0
  196. package/managed/templates/github/actions/forgejo-upload-artifact/index.cjs +7 -0
  197. package/managed/templates/github/actions/setup-devenv/action.yml +21 -3
  198. package/managed/templates/github/workflows/managed-files.yml +1 -2
  199. package/nextest.toml +8 -75
  200. package/package.json +75 -13
  201. package/runtime-input.sh +81 -0
  202. package/src/__tests__/counted-cargo-process.ts +286 -0
  203. package/src/__tests__/counted-cargo.ts +730 -0
  204. package/src/__tests__/fixture-cargo-home.ts +36 -0
  205. package/src/__tests__/fixture-nx-env.ts +157 -2
  206. package/src/__tests__/progress-clock.ts +43 -0
  207. package/src/__tests__/runtime-input.ts +23 -0
  208. package/src/bin/smoo-nx-bound-failures.test.ts +127 -0
  209. package/src/bin/smoo-nx-bound-failures.ts +33 -0
  210. package/src/bin/smoo-nx-cargo-hash.ts +28 -3
  211. package/src/bin/smoo-nx-exec.ts +17 -86
  212. package/src/bin/smoo-nx-failures.ts +4 -0
  213. package/src/bin/smoo-nx-nextest-extract.ts +3 -2
  214. package/src/bin/smoo-nx-test-walks.ts +42 -0
  215. package/src/bounded-test-policy.test.ts +268 -22
  216. package/src/bounded-test-policy.ts +114 -42
  217. package/src/build-state-output-policy.test.ts +115 -0
  218. package/src/build-state-output-policy.ts +90 -0
  219. package/src/bun-bootstrap.test.ts +57 -3
  220. package/src/bun-file-packages.test.ts +47 -0
  221. package/src/cargo-closure-input.test.ts +432 -62
  222. package/src/cargo-closure-input.ts +51 -57
  223. package/src/cargo-dev-profile.ts +137 -0
  224. package/src/cargo-resolve-cache.test.ts +1044 -0
  225. package/src/cargo-source-hash.test.ts +203 -93
  226. package/src/cargo-source-hash.ts +762 -75
  227. package/src/cargo-toolchain-policy.ts +6 -0
  228. package/src/cargo-workspace.test.ts +320 -0
  229. package/src/cargo-workspace.ts +344 -18
  230. package/src/cross-check-policy.test.ts +59 -1
  231. package/src/cross-check-policy.ts +25 -6
  232. package/src/disk-command-policy.test.ts +61 -0
  233. package/src/disk-command-policy.ts +107 -0
  234. package/src/ensure-built-receipt.test.ts +184 -0
  235. package/src/ensure-built-receipt.ts +263 -0
  236. package/src/ensure-built-witness.test.ts +344 -0
  237. package/src/ensure-built-witness.ts +252 -0
  238. package/src/ensure-built.test.ts +1043 -483
  239. package/src/ensure-built.ts +870 -197
  240. package/src/eslint-file-set.ts +9 -0
  241. package/src/executors/bounded-exec/cpu-pool.test.ts +454 -0
  242. package/src/executors/bounded-exec/cpu-pool.ts +417 -0
  243. package/src/executors/bounded-exec/cpu-tokens.test.ts +269 -0
  244. package/src/executors/bounded-exec/cpu-tokens.ts +177 -0
  245. package/src/executors/bounded-exec/disk-lease.test.ts +185 -0
  246. package/src/executors/bounded-exec/disk-lease.ts +139 -0
  247. package/src/executors/bounded-exec/executor-host.test.ts +778 -0
  248. package/src/executors/bounded-exec/executor.test.ts +1116 -63
  249. package/src/executors/bounded-exec/executor.ts +565 -67
  250. package/src/executors/bounded-exec/gateway-lease.ts +176 -0
  251. package/src/executors/bounded-exec/hang-diagnostics.test.ts +285 -0
  252. package/src/executors/bounded-exec/hang-diagnostics.ts +283 -0
  253. package/src/executors/bounded-exec/progress.test.ts +79 -0
  254. package/src/executors/bounded-exec/progress.ts +118 -0
  255. package/src/executors/bounded-exec/ram-temp-fixtures.ts +12 -0
  256. package/src/executors/bounded-exec/ram-temp-held.test.ts +264 -0
  257. package/src/executors/bounded-exec/ram-temp-held.ts +242 -0
  258. package/src/executors/bounded-exec/ram-temp-host.test.ts +605 -0
  259. package/src/executors/bounded-exec/ram-temp.test.ts +674 -0
  260. package/src/executors/bounded-exec/ram-temp.ts +1918 -0
  261. package/src/executors/bounded-exec/schema.json +11 -5
  262. package/src/executors/bounded-exec/schema.ts +27 -11
  263. package/src/executors/bounded-exec/verdict.test.ts +440 -0
  264. package/src/executors/bounded-exec/verdict.ts +433 -0
  265. package/src/executors/shard-timings/executor.ts +36 -0
  266. package/src/executors/shard-timings/schema.json +21 -0
  267. package/src/executors/shard-timings/schema.ts +4 -0
  268. package/src/failure-summary.ts +119 -0
  269. package/src/generators/bounded-test-targets/generator.test.ts +4 -4
  270. package/src/generators/bun-test-tracing/generator.test.ts +2 -2
  271. package/src/generators/create-package/generator.test.ts +6 -0
  272. package/src/generators/create-package/generator.ts +6 -2
  273. package/src/host-controller-wiring.test.ts +31 -0
  274. package/src/index.test.ts +1070 -360
  275. package/src/index.ts +524 -255
  276. package/src/linked-snapshots.nx.test.ts +172 -0
  277. package/src/managed-assets.ts +12 -0
  278. package/src/managed-files/ci-workflow.ts +54 -13
  279. package/src/managed-files/files.ts +65 -9
  280. package/src/managed-files/forgejo-artifact.test.ts +395 -0
  281. package/src/managed-files/generator.test.ts +60 -1
  282. package/src/managed-files/json-edit.test.ts +216 -0
  283. package/src/managed-files/json-edit.ts +290 -0
  284. package/src/managed-files/managed-format-json.test.ts +308 -0
  285. package/src/managed-files/managed-format.ts +90 -2
  286. package/src/managed-files/pr-preview-cleanup-workflow.ts +69 -3
  287. package/src/managed-files/publish-workflow.ts +285 -67
  288. package/src/managed-files/repo-path.test.ts +129 -0
  289. package/src/managed-files/trace-db-artifact.test.ts +7 -0
  290. package/src/managed-files/tree.ts +11 -3
  291. package/src/manifest-hash.ts +3 -2
  292. package/src/nextest-code-bound.test.ts +155 -0
  293. package/src/nextest-code-bound.ts +195 -0
  294. package/src/nextest-extraction.test.ts +37 -5
  295. package/src/nextest-extraction.ts +51 -9
  296. package/src/nextest-partitions.test.ts +495 -0
  297. package/src/nx-cache-bound.test.ts +109 -0
  298. package/src/nx-cache-restore-mtime.test.ts +170 -0
  299. package/src/nx-daemon-claim.test.ts +277 -0
  300. package/src/nx-daemon-idle-cpu.test.ts +81 -0
  301. package/src/nx-daemon-idle-policy.test.ts +51 -0
  302. package/src/nx-daemon-ignore-files.test.ts +210 -0
  303. package/src/nx-daemon-root-normalization.test.ts +105 -0
  304. package/src/nx-daemon-validity.test.ts +925 -0
  305. package/src/nx-dot-input-policy.test.ts +353 -0
  306. package/src/nx-failure-life-cycles.test.ts +207 -0
  307. package/src/nx-files-archive-racy.test.ts +97 -0
  308. package/src/nx-foreign-workspace-env.test.ts +329 -0
  309. package/src/nx-incomplete-run-summary.test.ts +70 -0
  310. package/src/nx-plugin-load-errors.test.ts +120 -0
  311. package/src/nx-project-resolution-cache.test.ts +176 -0
  312. package/src/nx-raw-inputs.test.ts +166 -0
  313. package/src/nx-resident-graph-workers.test.ts +112 -0
  314. package/src/nx-run-information.test.ts +118 -0
  315. package/src/nx-skipped-task-exit.test.ts +180 -0
  316. package/src/nx-state-location.test.ts +63 -0
  317. package/src/nx-store-resolution.test.ts +99 -0
  318. package/src/nx-task-graph-cycles.test.ts +148 -0
  319. package/src/nx-task-output-lease.test.ts +414 -0
  320. package/src/nx-use-local.test.ts +103 -0
  321. package/src/package-target-policy.ts +3 -6
  322. package/src/release-config-policy.ts +3 -6
  323. package/src/runtime-input.test.ts +215 -0
  324. package/src/runtime-input.ts +17 -0
  325. package/src/rust-tokens.ts +78 -0
  326. package/src/serial-group-wiring.test.ts +326 -0
  327. package/src/task-history-namespace.test.ts +181 -0
  328. package/src/task-results.test.ts +226 -0
  329. package/src/task-results.ts +75 -0
  330. package/src/test-program.test.ts +303 -0
  331. package/src/test-program.ts +341 -0
  332. package/src/test-timings.test.ts +240 -0
  333. package/src/test-timings.ts +311 -0
  334. package/src/test-walk-policy.test.ts +585 -0
  335. package/src/test-walk-policy.ts +731 -0
  336. package/src/testing.test.ts +509 -0
  337. package/src/testing.ts +922 -0
  338. package/src/type-guards.ts +4 -0
  339. package/src/typecheck-test-policy.test.ts +496 -11
  340. package/src/typecheck-test-policy.ts +137 -103
  341. package/src/workspace-config-policy.test.ts +212 -13
  342. package/src/workspace-config-policy.ts +101 -73
  343. package/src/workspace-index.ts +18 -0
  344. package/src/workspace-manifest.ts +41 -1
  345. package/test-timings.json +85 -28
  346. package/managed/raw/tooling/direnv/inherited-devenv.ts +0 -615
  347. package/managed/raw/tooling/direnv/shared-caches.sh +0 -53
  348. package/src/__tests__/in-process-nx-index.ts +0 -9
@@ -0,0 +1,216 @@
1
+ import { describe, expect, it } from 'bun:test';
2
+ import { parseJson } from 'nx/src/devkit-exports.js';
3
+ import { editJsonText } from './json-edit.js';
4
+
5
+ /** The document `documented` parses to. A test states the document it wants, and the edit is the difference. */
6
+ const document = {
7
+ extends: './tsconfig.json',
8
+ compilerOptions: { noEmit: true, types: ['bun'] },
9
+ include: ['scripts/**/*.test.ts'],
10
+ references: [{ path: './a.json' }, { path: './b.json' }, { path: './c.json' }],
11
+ };
12
+
13
+ const documented = [
14
+ '{',
15
+ ' // Why it extends this: a Bun program.',
16
+ ' "extends": "./tsconfig.json",',
17
+ ' "compilerOptions": {',
18
+ ' "noEmit": true, // required',
19
+ ' /* the runtime types */',
20
+ ' "types": ["bun"],',
21
+ ' },',
22
+ ' "include": ["scripts/**/*.test.ts"],',
23
+ ' "references": [{ "path": "./a.json" }, { "path": "./b.json" }, { "path": "./c.json" }],',
24
+ '}',
25
+ '',
26
+ ].join('\n');
27
+
28
+ describe('editing JSON text to parse as another document', () => {
29
+ it('returns the text itself when nothing differs', () => {
30
+ expect(editJsonText(documented, document)).toBe(documented);
31
+ });
32
+
33
+ it('adds a key and keeps every comment', () => {
34
+ const edited = editJsonText(documented, { ...document, exclude: ['dist'] });
35
+
36
+ expect(edited).toContain('// Why it extends this: a Bun program.');
37
+ expect(edited).toContain('// required');
38
+ expect(edited).toContain('/* the runtime types */');
39
+ expect(parseJson(edited)).toEqual({ ...document, exclude: ['dist'] });
40
+ });
41
+
42
+ it('changes a scalar where it stands, nested', () => {
43
+ const edited = editJsonText(documented, { ...document, compilerOptions: { noEmit: false, types: ['bun'] } });
44
+
45
+ expect(edited).toContain('"noEmit": false, // required');
46
+ expect(parseJson(edited)).toEqual({ ...document, compilerOptions: { noEmit: false, types: ['bun'] } });
47
+ });
48
+
49
+ it('adds a nested key and removes one, leaving the comment that is not about it', () => {
50
+ const edited = editJsonText(documented, { ...document, compilerOptions: { noEmit: true, lib: ['es2024'] } });
51
+
52
+ expect(parseJson(edited)).toEqual({ ...document, compilerOptions: { noEmit: true, lib: ['es2024'] } });
53
+ expect(edited).toContain('// required');
54
+ });
55
+
56
+ it('removes a key', () => {
57
+ const { include: _removed, ...without } = document;
58
+
59
+ const edited = editJsonText(documented, without);
60
+
61
+ expect(parseJson(edited)).toEqual(without);
62
+ expect(edited).toContain('// Why it extends this: a Bun program.');
63
+ });
64
+
65
+ it('appends the elements an array gained, after the ones it had', () => {
66
+ const include = ['scripts/**/*.test.ts', 'lib/**/*.test.ts', 'tests/**/*.ts'];
67
+
68
+ const edited = editJsonText(documented, { ...document, include });
69
+
70
+ expect(parseJson(edited)).toEqual({ ...document, include });
71
+ expect(edited).toContain('/* the runtime types */');
72
+ });
73
+
74
+ it('creates a key in an empty object and an element in an empty array', () => {
75
+ expect(editJsonText('{}\n', { a: 1 })).toBe('{"a": 1}\n');
76
+ expect(editJsonText('{ "a": [] }\n', { a: [1] })).toBe('{ "a": [1] }\n');
77
+ expect(editJsonText('{ /* nothing yet */ }\n', { a: 1 })).toBe('{"a": 1 /* nothing yet */ }\n');
78
+ });
79
+
80
+ it('removes the elements an array lost, wherever they were', () => {
81
+ const edited = editJsonText(documented, { ...document, references: [{ path: './b.json' }] });
82
+
83
+ expect(parseJson(edited)).toEqual({ ...document, references: [{ path: './b.json' }] });
84
+ expect(edited).toContain('// required');
85
+ });
86
+
87
+ it('removes only the elements that are gone when equal elements remain', () => {
88
+ expect(parseJson(editJsonText('{ "a": [1, 2, 1, 3] }\n', { a: [2, 3] }))).toEqual({ a: [2, 3] });
89
+ expect(parseJson(editJsonText('{ "a": [1, 2, 1, 3] }\n', { a: [1, 1] }))).toEqual({ a: [1, 1] });
90
+ });
91
+
92
+ it('takes the last element out of a one-line array whole (a naive delete leaves `13`)', () => {
93
+ // The space before the closer is the formatter's to tidy; what matters is the separator went with the element.
94
+ expect(editJsonText('{ "a": [1, 2, 1, 3] }\n', { a: [1, 2, 1] })).toBe('{ "a": [1, 2, 1 ] }\n');
95
+ });
96
+
97
+ it('replaces an array it neither grew nor shrank', () => {
98
+ const include = ['src/**/*.test.ts', 'scripts/**/*.test.ts'];
99
+
100
+ const edited = editJsonText(documented, { ...document, include });
101
+
102
+ expect(parseJson(edited)).toEqual({ ...document, include });
103
+ expect(edited).toContain('// Why it extends this: a Bun program.');
104
+ });
105
+
106
+ it('replaces a value that changed type', () => {
107
+ expect(parseJson(editJsonText('{ "extends": "./a.json" }\n', { extends: ['./a.json', './b.json'] }))).toEqual({
108
+ extends: ['./a.json', './b.json'],
109
+ });
110
+ expect(parseJson(editJsonText('{ "a": { "b": 1 } }\n', { a: 2 }))).toEqual({ a: 2 });
111
+ });
112
+
113
+ it('keeps a trailing comma and a block comment where it found them', () => {
114
+ const edited = editJsonText('{\n /* lead */\n "a": 1,\n}\n', { a: 1, b: 2 });
115
+
116
+ expect(edited).toBe('{\n /* lead */\n "a": 1,\n"b": 2,\n}\n');
117
+ expect(parseJson(edited)).toEqual({ a: 1, b: 2 });
118
+ });
119
+
120
+ it('is a fixed point: editing to the document the text already is changes nothing', () => {
121
+ const include = ['scripts/**/*.test.ts', 'lib/**/*.test.ts'];
122
+ const edited = editJsonText(documented, { ...document, include });
123
+
124
+ expect(editJsonText(edited, { ...document, include })).toBe(edited);
125
+ });
126
+
127
+ it('is never fooled by a key that means nothing: undefined is no key', () => {
128
+ expect(editJsonText('{ "a": 1 }\n', { a: 1, b: undefined })).toBe('{ "a": 1 }\n');
129
+ });
130
+ });
131
+
132
+ describe('a comment that trails a member on its line belongs to that member', () => {
133
+ it('stays with the key it follows when a key is added after it', () => {
134
+ expect(editJsonText('{\n "a": [1] // about a\n}\n', { a: [1], b: 2 })).toBe(
135
+ '{\n "a": [1], // about a\n"b": 2\n}\n',
136
+ );
137
+ });
138
+
139
+ it('stays with the element it follows when an element is appended after it', () => {
140
+ expect(editJsonText('{\n "include": [\n "a" // about a\n ]\n}\n', { include: ['a', 'b'] })).toBe(
141
+ '{\n "include": [\n "a", // about a\n"b"\n ]\n}\n',
142
+ );
143
+ });
144
+
145
+ it('is not given to the new member when the author already wrote a trailing comma', () => {
146
+ expect(editJsonText('{\n "a": 1, // about a\n}\n', { a: 1, b: 2 })).toBe('{\n "a": 1, // about a\n"b": 2,\n}\n');
147
+ });
148
+
149
+ it('goes with the element that is removed', () => {
150
+ expect(editJsonText('{\n "include": [\n "x", // about x\n "y"\n ]\n}\n', { include: ['y'] })).toBe(
151
+ '{\n "include": [\n "y"\n ]\n}\n',
152
+ );
153
+ });
154
+
155
+ it('goes with the last element that is removed, leaving the previous one and its own comment', () => {
156
+ const text = '{\n "include": [\n "x", // about x\n "y" // about y\n ]\n}\n';
157
+
158
+ expect(editJsonText(text, { include: ['x'] })).toBe('{\n "include": [\n "x" // about x\n ]\n}\n');
159
+ });
160
+
161
+ it('goes with the key that is removed, while a comment on a line of its own stays', () => {
162
+ const text = '{\n // about the lib\n "lib": ["es2024"], // pinned\n "noEmit": true\n}\n';
163
+
164
+ expect(editJsonText(text, { noEmit: true })).toBe('{\n // about the lib\n "noEmit": true\n}\n');
165
+ });
166
+
167
+ it('is not confused by a comma inside a comment between members', () => {
168
+ expect(editJsonText('{ "a": [1 /* a, b */, 2] }\n', { a: [2] })).toBe('{ "a": [ 2] }\n');
169
+ expect(editJsonText('{ "a": [1, 2 // x, y\n] }\n', { a: [1] })).toBe('{ "a": [1 \n] }\n');
170
+ expect(editJsonText('{ "a": [1 // x, y\n, 2] }\n', { a: [2] })).toBe('{ "a": [\n 2] }\n');
171
+ });
172
+
173
+ it('ends a line comment at a lone carriage return, as the parser does', () => {
174
+ const edited = editJsonText('{"a": [1 // x\r, 2]}', { a: [2] });
175
+
176
+ expect(parseJson(edited)).toEqual({ a: [2] });
177
+ });
178
+
179
+ it('is kept through CRLF and tab-indented text', () => {
180
+ const crlf = '{\r\n\t"a": [\r\n\t\t"x", // about x\r\n\t\t"y"\r\n\t]\r\n}\r\n';
181
+
182
+ expect(editJsonText(crlf, { a: ['y'] })).toBe('{\r\n\t"a": [\r\n\t\t"y"\r\n\t]\r\n}\r\n');
183
+ });
184
+
185
+ it('removes the only member and the only element', () => {
186
+ expect(editJsonText('{ "a": 1 }\n', {})).toBe('{ }\n');
187
+ expect(editJsonText('{ "a": [1] }\n', { a: [] })).toBe('{ "a": [] }\n');
188
+ expect(editJsonText('{ "a": [1,] }\n', { a: [] })).toBe('{ "a": [] }\n');
189
+ });
190
+
191
+ it('keeps a comment that leads the next member when the member before it is removed', () => {
192
+ expect(editJsonText('{"a": 1, /* why b */ "b": 2}', { b: 2 })).toBe('{ /* why b */ "b": 2}');
193
+ expect(editJsonText('{ "a": [1, /* why 2 */ 2] }', { a: [2] })).toBe('{ "a": [ /* why 2 */ 2] }');
194
+ expect(editJsonText('{ "a": [1, /* x */ /* y */ 2] }', { a: [2] })).toBe('{ "a": [ /* x */ /* y */ 2] }');
195
+ });
196
+
197
+ it('still takes a block comment that trails the member after its comma, with nothing behind it', () => {
198
+ expect(editJsonText('{\n "a": 1, /* about a */\n "b": 2\n}\n', { b: 2 })).toBe('{\n "b": 2\n}\n');
199
+ });
200
+ });
201
+
202
+ describe('a document the edit cannot honor faithfully', () => {
203
+ it('is edited at the key a parser would read, when a key is repeated', () => {
204
+ const edited = editJsonText('{ "a": { "x": 1 }, "a": { "x": 2 } }\n', { a: { x: 3 } });
205
+
206
+ expect(parseJson(edited)).toEqual({ a: { x: 3 } });
207
+ });
208
+
209
+ it('removes every property of a repeated key, since one removal leaves the other in the document', () => {
210
+ expect(parseJson(editJsonText('{ "a": 1, "b": 0, "a": 2 }\n', { b: 0 }))).toEqual({ b: 0 });
211
+ });
212
+
213
+ it('is refused rather than written when the text is not JSON', () => {
214
+ expect(() => editJsonText('{ "a": ', { a: 1 })).toThrow('not valid JSON');
215
+ });
216
+ });
@@ -0,0 +1,290 @@
1
+ import { isDeepStrictEqual } from 'node:util';
2
+ import { applyEdits, type Edit, type JSONPath, type Node, type ParseError, parse, parseTree } from 'jsonc-parser';
3
+ import { isRecord } from '../type-guards.js';
4
+
5
+ /**
6
+ * One structural difference between two documents. `add-key` and `append` name the object or array they add to;
7
+ * `replace` and `remove` name the value they change.
8
+ */
9
+ type Change =
10
+ | { readonly kind: 'replace'; readonly path: JSONPath; readonly value: unknown }
11
+ | { readonly kind: 'add-key'; readonly path: JSONPath; readonly key: string; readonly value: unknown }
12
+ | { readonly kind: 'append'; readonly path: JSONPath; readonly value: unknown }
13
+ | { readonly kind: 'remove'; readonly path: JSONPath };
14
+
15
+ /**
16
+ * `text` edited to parse as `after`, with every byte the difference does not touch left alone.
17
+ *
18
+ * A managed file that people also write (a tsconfig explaining why it extends what it extends) cannot be
19
+ * regenerated from its parsed value: serializing drops every comment. The generator owns a handful of fields, so it
20
+ * edits those, and the rest of the file is the author's.
21
+ *
22
+ * - A scalar or an object that changed is replaced where it stands; an array that only grew gains its new elements
23
+ * at the end, one that only shrank loses those elements, and any other array is replaced whole (comments inside
24
+ * it go with it: no generator step reorders an array it owns).
25
+ * - A comment that trails an element on its own line belongs to that element: removing the element removes the
26
+ * comment with it, and an element added after it goes after the comment, never between the two. A comment on a
27
+ * line of its own stays wherever it is, including above a key that was removed.
28
+ * - Inserted values are written compactly on a line of their own; their layout is the caller's formatter's to
29
+ * decide, not this function's.
30
+ *
31
+ * The result is checked: text that does not parse to exactly `after` is a fault in this function, not something to
32
+ * write.
33
+ */
34
+ export function editJsonText(text: string, after: unknown): string {
35
+ // What the document means, not how a caller built it: a key set to undefined is no key.
36
+ const target: unknown = JSON.parse(JSON.stringify(after));
37
+ let edited = text;
38
+ for (const change of changesBetween(parseDocument(text), target, [])) {
39
+ edited = applyEdits(edited, editsFor(edited, change));
40
+ // A repeated key is every one of its properties: one removal leaves the other still in the document.
41
+ while (change.kind === 'remove' && typeof change.path.at(-1) === 'string' && isPresent(edited, change.path)) {
42
+ edited = applyEdits(edited, editsFor(edited, change));
43
+ }
44
+ }
45
+ if (!isDeepStrictEqual(parseDocument(edited), target)) {
46
+ throw new Error('the edited JSON text does not parse to the document the policy produced, so it is not written');
47
+ }
48
+ return edited;
49
+ }
50
+
51
+ /** Whether `text` still has a member at `path`. */
52
+ function isPresent(text: string, path: JSONPath): boolean {
53
+ const root = parseTree(text, undefined, { allowTrailingComma: true });
54
+ return root !== undefined && memberAt(root, path) !== undefined;
55
+ }
56
+
57
+ function parseDocument(text: string): unknown {
58
+ const errors: ParseError[] = [];
59
+ const document: unknown = parse(text, errors, { allowTrailingComma: true });
60
+ const [first] = errors;
61
+ if (first !== undefined) throw new Error(`not valid JSON/JSONC (error ${first.error} at offset ${first.offset})`);
62
+ return document;
63
+ }
64
+
65
+ function* changesBetween(before: unknown, after: unknown, path: JSONPath): Generator<Change> {
66
+ if (isRecord(before) && isRecord(after)) {
67
+ for (const key of Object.keys(before)) {
68
+ if (!Object.hasOwn(after, key)) yield { kind: 'remove', path: [...path, key] };
69
+ }
70
+ for (const [key, value] of Object.entries(after)) {
71
+ if (Object.hasOwn(before, key)) yield* changesBetween(before[key], value, [...path, key]);
72
+ else yield { kind: 'add-key', path, key, value };
73
+ }
74
+ } else if (Array.isArray(before) && Array.isArray(after)) {
75
+ yield* arrayChanges(before, after, path);
76
+ } else if (!isDeepStrictEqual(before, after)) {
77
+ yield { kind: 'replace', path, value: after };
78
+ }
79
+ }
80
+
81
+ function* arrayChanges(before: readonly unknown[], after: readonly unknown[], path: JSONPath): Generator<Change> {
82
+ if (isDeepStrictEqual(before, after)) return;
83
+ if (before.every((element, index) => isDeepStrictEqual(element, after[index]))) {
84
+ for (const element of after.slice(before.length)) yield { kind: 'append', path, value: element };
85
+ return;
86
+ }
87
+ const removed = removedIndexes(before, after);
88
+ if (removed !== null) {
89
+ // Last first, so an index still names the element it did when the changes were derived.
90
+ for (const index of [...removed].reverse()) yield { kind: 'remove', path: [...path, index] };
91
+ return;
92
+ }
93
+ yield { kind: 'replace', path, value: after };
94
+ }
95
+
96
+ /** The indexes of `before` that `after` leaves out, when `after` is `before` with only removals; else null. */
97
+ function removedIndexes(before: readonly unknown[], after: readonly unknown[]): number[] | null {
98
+ const removed: number[] = [];
99
+ let kept = 0;
100
+ before.forEach((element, index) => {
101
+ if (kept < after.length && isDeepStrictEqual(element, after[kept])) kept += 1;
102
+ else removed.push(index);
103
+ });
104
+ return kept === after.length ? removed : null;
105
+ }
106
+
107
+ /** The edits that make `change` to `text`, found by walking the text's own syntax tree. */
108
+ function editsFor(text: string, change: Change): Edit[] {
109
+ const root = parseTree(text, undefined, { allowTrailingComma: true });
110
+ const member = root === undefined ? undefined : memberAt(root, change.path);
111
+ if (member === undefined) throw new Error(`no ${JSON.stringify(change.path)} in the JSON text to edit`);
112
+ switch (change.kind) {
113
+ case 'replace': {
114
+ const value = valueNodeOf(member);
115
+ return [{ offset: value.offset, length: value.length, content: JSON.stringify(change.value) }];
116
+ }
117
+ case 'remove':
118
+ return removal(text, member);
119
+ case 'add-key':
120
+ return insertion(text, valueNodeOf(member), `${JSON.stringify(change.key)}: ${JSON.stringify(change.value)}`);
121
+ case 'append':
122
+ return insertion(text, valueNodeOf(member), JSON.stringify(change.value));
123
+ }
124
+ }
125
+
126
+ /** The node a path names: the property for a key, the element for an index, the root for no path. */
127
+ function memberAt(root: Node, path: JSONPath): Node | undefined {
128
+ let member: Node | undefined = root;
129
+ for (const segment of path) {
130
+ if (member === undefined) return undefined;
131
+ const container = valueNodeOf(member);
132
+ member = typeof segment === 'number' ? container.children?.[segment] : lastProperty(container, segment);
133
+ }
134
+ return member;
135
+ }
136
+
137
+ /** The value a member holds: a property's value, or an element itself. */
138
+ function valueNodeOf(member: Node): Node {
139
+ return member.type === 'property' ? (member.children?.[1] ?? member) : member;
140
+ }
141
+
142
+ /** The last property of an object with this key: a parser keeps the last of duplicate keys, so that is the one that means something. */
143
+ function lastProperty(object: Node, key: string): Node | undefined {
144
+ if (object.type !== 'object') return undefined;
145
+ const properties = object.children ?? [];
146
+ for (let index = properties.length - 1; index >= 0; index--) {
147
+ const property = properties[index];
148
+ if (property?.children?.[0]?.value === key) return property;
149
+ }
150
+ return undefined;
151
+ }
152
+
153
+ /** The edits that add `entry` after the last member of `container`, or inside it when it is empty. */
154
+ function insertion(text: string, container: Node, entry: string): Edit[] {
155
+ if (container.type !== 'object' && container.type !== 'array')
156
+ throw new Error('only an object or an array can gain a member');
157
+ const last = container.children?.at(-1);
158
+ if (last === undefined) return [{ offset: container.offset + 1, length: 0, content: entry }];
159
+ const lastEnd = last.offset + last.length;
160
+ const closer = container.offset + container.length - 1;
161
+ const separator = separatorAfter(text, lastEnd, closer);
162
+ // After the comments on the last member's line, and after its separator when that is on a later line.
163
+ const at = Math.max(sameLineEnd(text, lastEnd, closer), separator === null ? 0 : separator + 1);
164
+ // A trailing comma the author wrote stays, one comma further along.
165
+ const added = `\n${entry}${separator === null ? '' : ','}`;
166
+ if (separator !== null) return [{ offset: at, length: 0, content: added }];
167
+ // No separator yet: it goes straight after the member, ahead of its comments.
168
+ return at === lastEnd
169
+ ? [{ offset: lastEnd, length: 0, content: `,${added}` }]
170
+ : [
171
+ { offset: lastEnd, length: 0, content: ',' },
172
+ { offset: at, length: 0, content: added },
173
+ ];
174
+ }
175
+
176
+ /**
177
+ * The edits that delete a member of an object or array: its text, its separator, and the comments that trail it on
178
+ * its line, plus the whole line when it has the line to itself. The member before a last one loses its separator so
179
+ * no comma is left dangling.
180
+ */
181
+ function removal(text: string, member: Node): Edit[] {
182
+ const container = member.parent;
183
+ const siblings = container?.children;
184
+ if (container === undefined || siblings === undefined)
185
+ throw new Error('a JSON value with no container cannot be removed');
186
+ const index = siblings.indexOf(member);
187
+ const previous = siblings[index - 1];
188
+ const memberEnd = member.offset + member.length;
189
+ const limit = siblings[index + 1]?.offset ?? container.offset + container.length - 1;
190
+ const separator = separatorAfter(text, memberEnd, limit);
191
+ const { from, to } = wholeLines(text, member.offset, sameLineEnd(text, memberEnd, limit));
192
+ const edits: Edit[] = [{ offset: from, length: to - from, content: '' }];
193
+ // A separator on a later line (a leading-comma style) is not part of the member's line.
194
+ if (separator !== null && separator >= to) edits.push({ offset: separator, length: 1, content: '' });
195
+ if (separator === null && previous !== undefined) {
196
+ const own = separatorAfter(text, previous.offset + previous.length, member.offset);
197
+ if (own !== null) edits.push({ offset: own, length: 1, content: '' });
198
+ }
199
+ return edits;
200
+ }
201
+
202
+ /** The span `start`..`end` widened to its whole lines, when nothing else is on them. */
203
+ function wholeLines(text: string, start: number, end: number): { from: number; to: number } {
204
+ let from = start;
205
+ while (from > 0 && isHorizontalSpace(text[from - 1])) from -= 1;
206
+ let to = end;
207
+ while (to < text.length && isHorizontalSpace(text[to])) to += 1;
208
+ const alone = from === 0 || isLineBreak(text[from - 1]);
209
+ const lineBreak = text.startsWith('\r\n', to) ? 2 : isLineBreak(text[to]) ? 1 : 0;
210
+ return alone && lineBreak > 0 ? { from, to: to + lineBreak } : { from: start, to: end };
211
+ }
212
+
213
+ /** The offset of the separating comma in `text[from, limit)`, skipping comments; null when there is none. */
214
+ function separatorAfter(text: string, from: number, limit: number): number | null {
215
+ let index = from;
216
+ while (index < limit) {
217
+ const char = text[index];
218
+ if (char === ',') return index;
219
+ if (text.startsWith('//', index)) index = lineEnd(text, index, limit);
220
+ else if (text.startsWith('/*', index)) index = blockEnd(text, index, limit);
221
+ else if (isHorizontalSpace(char) || isLineBreak(char)) index += 1;
222
+ else return null;
223
+ }
224
+ return null;
225
+ }
226
+
227
+ /**
228
+ * The end of what trails a member on its line: its comma if that is on the line, and the comments after it. A comment
229
+ * after the comma with a value behind it on the line leads that value, so it is not the member's to take.
230
+ */
231
+ function sameLineEnd(text: string, from: number, limit: number): number {
232
+ let end = from;
233
+ let index = from;
234
+ let sawComma = false;
235
+ while (index < limit) {
236
+ const char = text[index];
237
+ if (isHorizontalSpace(char)) {
238
+ index += 1;
239
+ } else if (char === ',' && !sawComma) {
240
+ sawComma = true;
241
+ index += 1;
242
+ end = index;
243
+ } else if (text.startsWith('/*', index)) {
244
+ const close = blockEnd(text, index, limit);
245
+ if (sawComma && !onlyTriviaToLineEnd(text, close, limit)) break;
246
+ index = end = close;
247
+ } else if (text.startsWith('//', index)) {
248
+ return lineEnd(text, index, limit);
249
+ } else {
250
+ break;
251
+ }
252
+ }
253
+ return end;
254
+ }
255
+
256
+ /**
257
+ * Whether nothing but whitespace and comments is left on the line from `from`. `limit` is where the next member
258
+ * starts, or the container's closer: reaching a member on the line is not "nothing left".
259
+ */
260
+ function onlyTriviaToLineEnd(text: string, from: number, limit: number): boolean {
261
+ let index = from;
262
+ while (index < limit) {
263
+ const char = text[index];
264
+ if (isHorizontalSpace(char)) index += 1;
265
+ else if (text.startsWith('/*', index)) index = blockEnd(text, index, limit);
266
+ else return text.startsWith('//', index) || isLineBreak(char);
267
+ }
268
+ const atLimit = text[limit];
269
+ return atLimit === '}' || atLimit === ']' || atLimit === undefined;
270
+ }
271
+
272
+ /** Where a `//` comment ends: at the line break, which jsonc-parser takes as `\n` or `\r`. */
273
+ function lineEnd(text: string, from: number, limit: number): number {
274
+ let index = from;
275
+ while (index < limit && !isLineBreak(text[index])) index += 1;
276
+ return index;
277
+ }
278
+
279
+ function blockEnd(text: string, from: number, limit: number): number {
280
+ const close = text.indexOf('*/', from + 2);
281
+ return close === -1 ? limit : Math.min(close + 2, limit);
282
+ }
283
+
284
+ function isHorizontalSpace(char: string | undefined): boolean {
285
+ return char === ' ' || char === '\t';
286
+ }
287
+
288
+ function isLineBreak(char: string | undefined): boolean {
289
+ return char === '\n' || char === '\r';
290
+ }