@cornerstonejs/codemods 5.19.3 → 6.0.0-beta.0

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 (349) hide show
  1. package/README.md +23 -0
  2. package/package.json +2 -2
  3. package/registry/cornerstone3d/6/README.md +87 -0
  4. package/registry/cornerstone3d/6/classes-and-types/README.md +113 -0
  5. package/registry/cornerstone3d/6/classes-and-types/codemod.yaml +20 -0
  6. package/registry/cornerstone3d/6/classes-and-types/scripts/classes-and-types.ts +1461 -0
  7. package/registry/cornerstone3d/6/classes-and-types/scripts/lib.ts +2386 -0
  8. package/registry/cornerstone3d/6/classes-and-types/tests/tsx/edge-namespace-and-reported/expected.tsx +27 -0
  9. package/registry/cornerstone3d/6/classes-and-types/tests/tsx/edge-namespace-and-reported/input.tsx +27 -0
  10. package/registry/cornerstone3d/6/classes-and-types/tests/tsx/edge-namespace-and-reported/metrics.json +36 -0
  11. package/registry/cornerstone3d/6/classes-and-types/tests/typescript/edge-already-6/expected.ts +15 -0
  12. package/registry/cornerstone3d/6/classes-and-types/tests/typescript/edge-already-6/input.ts +15 -0
  13. package/registry/cornerstone3d/6/classes-and-types/tests/typescript/negative-other-packages/expected.ts +11 -0
  14. package/registry/cornerstone3d/6/classes-and-types/tests/typescript/negative-other-packages/input.ts +11 -0
  15. package/registry/cornerstone3d/6/classes-and-types/tests/typescript/ohif-3d-family-of-predicate-or-cast-object/expected.ts +55 -0
  16. package/registry/cornerstone3d/6/classes-and-types/tests/typescript/ohif-3d-family-of-predicate-or-cast-object/input.ts +51 -0
  17. package/registry/cornerstone3d/6/classes-and-types/tests/typescript/ohif-3d-family-of-predicate-or-cast-object/metrics.json +36 -0
  18. package/registry/cornerstone3d/6/classes-and-types/tests/typescript/ohif-types-from-the-types-subpath/expected.ts +26 -0
  19. package/registry/cornerstone3d/6/classes-and-types/tests/typescript/ohif-types-from-the-types-subpath/input.ts +26 -0
  20. package/registry/cornerstone3d/6/classes-and-types/tests/typescript/ohif-types-from-the-types-subpath/metrics.json +52 -0
  21. package/registry/cornerstone3d/6/classes-and-types/tests/typescript/positive-3d-evidence/expected.ts +34 -0
  22. package/registry/cornerstone3d/6/classes-and-types/tests/typescript/positive-3d-evidence/input.ts +30 -0
  23. package/registry/cornerstone3d/6/classes-and-types/tests/typescript/positive-3d-evidence/metrics.json +12 -0
  24. package/registry/cornerstone3d/6/classes-and-types/tests/typescript/positive-classes/expected.ts +35 -0
  25. package/registry/cornerstone3d/6/classes-and-types/tests/typescript/positive-classes/input.ts +35 -0
  26. package/registry/cornerstone3d/6/classes-and-types/tests/typescript/positive-classes/metrics.json +28 -0
  27. package/registry/cornerstone3d/6/classes-and-types/tests/typescript/positive-types-and-casts/expected.ts +46 -0
  28. package/registry/cornerstone3d/6/classes-and-types/tests/typescript/positive-types-and-casts/input.ts +42 -0
  29. package/registry/cornerstone3d/6/classes-and-types/tests/typescript/review-class-import-cleanup-orphans-shorthand/expected.ts +4 -0
  30. package/registry/cornerstone3d/6/classes-and-types/tests/typescript/review-class-import-cleanup-orphans-shorthand/input.ts +4 -0
  31. package/registry/cornerstone3d/6/classes-and-types/tests/typescript/review-class-import-cleanup-orphans-shorthand/metrics.json +12 -0
  32. package/registry/cornerstone3d/6/classes-and-types/tests/typescript/review-instanceof-check-can-prevent-the-first-mount/expected.ts +24 -0
  33. package/registry/cornerstone3d/6/classes-and-types/tests/typescript/review-instanceof-check-can-prevent-the-first-mount/input.ts +25 -0
  34. package/registry/cornerstone3d/6/classes-and-types/tests/typescript/review-instanceof-check-can-prevent-the-first-mount/metrics.json +38 -0
  35. package/registry/cornerstone3d/6/classes-and-types/tests/typescript/review-instanceof-replacement-inverts-comparison/expected.ts +11 -0
  36. package/registry/cornerstone3d/6/classes-and-types/tests/typescript/review-instanceof-replacement-inverts-comparison/input.ts +11 -0
  37. package/registry/cornerstone3d/6/classes-and-types/tests/typescript/review-instanceof-replacement-inverts-comparison/metrics.json +20 -0
  38. package/registry/cornerstone3d/6/classes-and-types/tests/typescript/review-negative-3d-test-taken-as-3d-viewport/expected.ts +29 -0
  39. package/registry/cornerstone3d/6/classes-and-types/tests/typescript/review-negative-3d-test-taken-as-3d-viewport/input.ts +29 -0
  40. package/registry/cornerstone3d/6/classes-and-types/tests/typescript/review-negative-3d-test-taken-as-3d-viewport/metrics.json +20 -0
  41. package/registry/cornerstone3d/6/classes-and-types/tests/typescript/review-re-exports-of-removed-classes/expected.ts +6 -0
  42. package/registry/cornerstone3d/6/classes-and-types/tests/typescript/review-re-exports-of-removed-classes/input.ts +6 -0
  43. package/registry/cornerstone3d/6/classes-and-types/tests/typescript/review-re-exports-of-removed-classes/metrics.json +20 -0
  44. package/registry/cornerstone3d/6/classes-and-types/tests/typescript/review-type-only-namespace-used-as-value/expected.ts +8 -0
  45. package/registry/cornerstone3d/6/classes-and-types/tests/typescript/review-type-only-namespace-used-as-value/input.ts +8 -0
  46. package/registry/cornerstone3d/6/classes-and-types/tests/typescript/review-type-only-namespace-used-as-value/metrics.json +12 -0
  47. package/registry/cornerstone3d/6/classes-and-types/tests/typescript/review2-instanceof-mount-proof-crosses-shadowed-binding/expected.ts +19 -0
  48. package/registry/cornerstone3d/6/classes-and-types/tests/typescript/review2-instanceof-mount-proof-crosses-shadowed-binding/input.ts +19 -0
  49. package/registry/cornerstone3d/6/classes-and-types/tests/typescript/review2-instanceof-mount-proof-crosses-shadowed-binding/metrics.json +22 -0
  50. package/registry/cornerstone3d/6/classes-and-types/tests/typescript/review2-unrelated-discriminator-taken-as-3d-proof/expected.ts +40 -0
  51. package/registry/cornerstone3d/6/classes-and-types/tests/typescript/review2-unrelated-discriminator-taken-as-3d-proof/input.ts +40 -0
  52. package/registry/cornerstone3d/6/classes-and-types/tests/typescript/review2-unrelated-discriminator-taken-as-3d-proof/metrics.json +28 -0
  53. package/registry/cornerstone3d/6/classes-and-types/tests/typescript/review3-conditional-viewport-type-taken-as-3d/expected.ts +39 -0
  54. package/registry/cornerstone3d/6/classes-and-types/tests/typescript/review3-conditional-viewport-type-taken-as-3d/input.ts +38 -0
  55. package/registry/cornerstone3d/6/classes-and-types/tests/typescript/review3-conditional-viewport-type-taken-as-3d/metrics.json +20 -0
  56. package/registry/cornerstone3d/6/classes-and-types/tests/typescript/review4-spread-or-computed-key-overrides-viewport-type/expected.ts +76 -0
  57. package/registry/cornerstone3d/6/classes-and-types/tests/typescript/review4-spread-or-computed-key-overrides-viewport-type/input.ts +75 -0
  58. package/registry/cornerstone3d/6/classes-and-types/tests/typescript/review4-spread-or-computed-key-overrides-viewport-type/metrics.json +36 -0
  59. package/registry/cornerstone3d/6/classes-and-types/tests/typescript/review4-viewport-family-proved-on-another-engine/expected.ts +92 -0
  60. package/registry/cornerstone3d/6/classes-and-types/tests/typescript/review4-viewport-family-proved-on-another-engine/input.ts +91 -0
  61. package/registry/cornerstone3d/6/classes-and-types/tests/typescript/review4-viewport-family-proved-on-another-engine/metrics.json +20 -0
  62. package/registry/cornerstone3d/6/classes-and-types/workflow.yaml +29 -0
  63. package/registry/cornerstone3d/6/codemod.yaml +19 -0
  64. package/registry/cornerstone3d/6/display-sets/README.md +173 -0
  65. package/registry/cornerstone3d/6/display-sets/codemod.yaml +20 -0
  66. package/registry/cornerstone3d/6/display-sets/scripts/display-sets.ts +965 -0
  67. package/registry/cornerstone3d/6/display-sets/scripts/lib.ts +2386 -0
  68. package/registry/cornerstone3d/6/display-sets/tests/tsx/edge-reported-shapes/expected.tsx +32 -0
  69. package/registry/cornerstone3d/6/display-sets/tests/tsx/edge-reported-shapes/input.tsx +32 -0
  70. package/registry/cornerstone3d/6/display-sets/tests/tsx/edge-reported-shapes/metrics.json +84 -0
  71. package/registry/cornerstone3d/6/display-sets/tests/tsx/edge-reported-shapes-on-a-typed-engine/expected.tsx +33 -0
  72. package/registry/cornerstone3d/6/display-sets/tests/tsx/edge-reported-shapes-on-a-typed-engine/input.tsx +33 -0
  73. package/registry/cornerstone3d/6/display-sets/tests/tsx/edge-reported-shapes-on-a-typed-engine/metrics.json +84 -0
  74. package/registry/cornerstone3d/6/display-sets/tests/tsx/positive-javascript/expected.js +23 -0
  75. package/registry/cornerstone3d/6/display-sets/tests/tsx/positive-javascript/input.js +17 -0
  76. package/registry/cornerstone3d/6/display-sets/tests/tsx/review-commonjs-module-gets-es-import/expected.js +10 -0
  77. package/registry/cornerstone3d/6/display-sets/tests/tsx/review-commonjs-module-gets-es-import/input.js +10 -0
  78. package/registry/cornerstone3d/6/display-sets/tests/tsx/review-commonjs-module-gets-es-import/metrics.json +12 -0
  79. package/registry/cornerstone3d/6/display-sets/tests/typescript/negative-already-6/expected.ts +20 -0
  80. package/registry/cornerstone3d/6/display-sets/tests/typescript/negative-already-6/input.ts +20 -0
  81. package/registry/cornerstone3d/6/display-sets/tests/typescript/positive-set-volumes-for-viewports/expected.ts +27 -0
  82. package/registry/cornerstone3d/6/display-sets/tests/typescript/positive-set-volumes-for-viewports/input.ts +27 -0
  83. package/registry/cornerstone3d/6/display-sets/tests/typescript/positive-set-volumes-for-viewports/metrics.json +12 -0
  84. package/registry/cornerstone3d/6/display-sets/tests/typescript/positive-stack-and-volume/expected.ts +37 -0
  85. package/registry/cornerstone3d/6/display-sets/tests/typescript/positive-stack-and-volume/input.ts +31 -0
  86. package/registry/cornerstone3d/6/display-sets/tests/typescript/positive-stack-and-volume/metrics.json +20 -0
  87. package/registry/cornerstone3d/6/display-sets/tests/typescript/positive-video-and-ecg/expected.ts +15 -0
  88. package/registry/cornerstone3d/6/display-sets/tests/typescript/positive-video-and-ecg/input.ts +15 -0
  89. package/registry/cornerstone3d/6/display-sets/tests/typescript/review-conditional-mount-breaks-control-flow/expected.ts +26 -0
  90. package/registry/cornerstone3d/6/display-sets/tests/typescript/review-conditional-mount-breaks-control-flow/input.ts +18 -0
  91. package/registry/cornerstone3d/6/display-sets/tests/typescript/review-conditional-mount-breaks-control-flow/metrics.json +20 -0
  92. package/registry/cornerstone3d/6/display-sets/tests/typescript/review-conditional-viewport-lists-and-callback-capture/expected.ts +46 -0
  93. package/registry/cornerstone3d/6/display-sets/tests/typescript/review-conditional-viewport-lists-and-callback-capture/input.ts +21 -0
  94. package/registry/cornerstone3d/6/display-sets/tests/typescript/review-generated-references-resolve-to-shadowing-parameter/expected.ts +23 -0
  95. package/registry/cornerstone3d/6/display-sets/tests/typescript/review-generated-references-resolve-to-shadowing-parameter/input.ts +13 -0
  96. package/registry/cornerstone3d/6/display-sets/tests/typescript/review-optional-mounts-become-unconditional/expected.ts +11 -0
  97. package/registry/cornerstone3d/6/display-sets/tests/typescript/review-optional-mounts-become-unconditional/input.ts +11 -0
  98. package/registry/cornerstone3d/6/display-sets/tests/typescript/review-optional-mounts-become-unconditional/metrics.json +28 -0
  99. package/registry/cornerstone3d/6/display-sets/tests/typescript/review-regenerated-import-drops-comments/expected.ts +22 -0
  100. package/registry/cornerstone3d/6/display-sets/tests/typescript/review-regenerated-import-drops-comments/input.ts +15 -0
  101. package/registry/cornerstone3d/6/display-sets/tests/typescript/review-stack-display-set-id-reused-across-series/expected.ts +41 -0
  102. package/registry/cornerstone3d/6/display-sets/tests/typescript/review-stack-display-set-id-reused-across-series/input.ts +16 -0
  103. package/registry/cornerstone3d/6/display-sets/tests/typescript/review-viewport-like-name-rewrites-unrelated-objects/expected.ts +11 -0
  104. package/registry/cornerstone3d/6/display-sets/tests/typescript/review-viewport-like-name-rewrites-unrelated-objects/input.ts +11 -0
  105. package/registry/cornerstone3d/6/display-sets/tests/typescript/review-viewport-like-name-rewrites-unrelated-objects/metrics.json +20 -0
  106. package/registry/cornerstone3d/6/display-sets/tests/typescript/review-volume-helper-binding-still-used/expected.ts +46 -0
  107. package/registry/cornerstone3d/6/display-sets/tests/typescript/review-volume-helper-binding-still-used/input.ts +17 -0
  108. package/registry/cornerstone3d/6/display-sets/tests/typescript/review-volume-helper-binding-still-used/metrics.json +20 -0
  109. package/registry/cornerstone3d/6/display-sets/tests/typescript/review-volume-registration-references-image-list-outside-block/expected.ts +16 -0
  110. package/registry/cornerstone3d/6/display-sets/tests/typescript/review-volume-registration-references-image-list-outside-block/input.ts +16 -0
  111. package/registry/cornerstone3d/6/display-sets/tests/typescript/review-volume-registration-references-image-list-outside-block/metrics.json +12 -0
  112. package/registry/cornerstone3d/6/display-sets/tests/typescript/review2-bracketed-and-re-exported-data-names/expected.ts +19 -0
  113. package/registry/cornerstone3d/6/display-sets/tests/typescript/review2-bracketed-and-re-exported-data-names/input.ts +19 -0
  114. package/registry/cornerstone3d/6/display-sets/tests/typescript/review2-bracketed-and-re-exported-data-names/metrics.json +44 -0
  115. package/registry/cornerstone3d/6/display-sets/tests/typescript/review2-generic-container-taken-as-viewport/expected.ts +22 -0
  116. package/registry/cornerstone3d/6/display-sets/tests/typescript/review2-generic-container-taken-as-viewport/input.ts +22 -0
  117. package/registry/cornerstone3d/6/display-sets/tests/typescript/review2-generic-container-taken-as-viewport/metrics.json +36 -0
  118. package/registry/cornerstone3d/6/display-sets/tests/typescript/review2-image-list-getter-read-twice/expected.ts +62 -0
  119. package/registry/cornerstone3d/6/display-sets/tests/typescript/review2-image-list-getter-read-twice/input.ts +28 -0
  120. package/registry/cornerstone3d/6/display-sets/tests/typescript/review2-missing-target-partly-updates-layout/expected.ts +50 -0
  121. package/registry/cornerstone3d/6/display-sets/tests/typescript/review2-missing-target-partly-updates-layout/input.ts +25 -0
  122. package/registry/cornerstone3d/6/display-sets/tests/typescript/review2-missing-target-partly-updates-layout/metrics.json +12 -0
  123. package/registry/cornerstone3d/6/display-sets/tests/typescript/review2-reassigned-receiver-keeps-initializer-evidence/expected.ts +45 -0
  124. package/registry/cornerstone3d/6/display-sets/tests/typescript/review2-reassigned-receiver-keeps-initializer-evidence/input.ts +39 -0
  125. package/registry/cornerstone3d/6/display-sets/tests/typescript/review2-reassigned-receiver-keeps-initializer-evidence/metrics.json +36 -0
  126. package/registry/cornerstone3d/6/display-sets/tests/typescript/review2-stack-ids-collide-across-engines/expected.ts +24 -0
  127. package/registry/cornerstone3d/6/display-sets/tests/typescript/review2-stack-ids-collide-across-engines/input.ts +12 -0
  128. package/registry/cornerstone3d/6/display-sets/tests/typescript/review2-volume-registration-attaches-another-series/expected.ts +47 -0
  129. package/registry/cornerstone3d/6/display-sets/tests/typescript/review2-volume-registration-attaches-another-series/input.ts +42 -0
  130. package/registry/cornerstone3d/6/display-sets/tests/typescript/review2-volume-registration-attaches-another-series/metrics.json +28 -0
  131. package/registry/cornerstone3d/6/display-sets/tests/typescript/review3-computed-option-key-overrides-image-ids/expected.ts +18 -0
  132. package/registry/cornerstone3d/6/display-sets/tests/typescript/review3-computed-option-key-overrides-image-ids/input.ts +18 -0
  133. package/registry/cornerstone3d/6/display-sets/tests/typescript/review3-computed-option-key-overrides-image-ids/metrics.json +12 -0
  134. package/registry/cornerstone3d/6/display-sets/tests/typescript/review3-loop-increment-taken-as-volume-creation/expected.ts +18 -0
  135. package/registry/cornerstone3d/6/display-sets/tests/typescript/review3-loop-increment-taken-as-volume-creation/input.ts +18 -0
  136. package/registry/cornerstone3d/6/display-sets/tests/typescript/review3-loop-increment-taken-as-volume-creation/metrics.json +12 -0
  137. package/registry/cornerstone3d/6/display-sets/tests/typescript/review3-mixed-family-targets-partly-update-layout/expected.ts +42 -0
  138. package/registry/cornerstone3d/6/display-sets/tests/typescript/review3-mixed-family-targets-partly-update-layout/input.ts +18 -0
  139. package/registry/cornerstone3d/6/display-sets/tests/typescript/review3-unawaited-ecg-mount-empty-after-the-call/expected.ts +18 -0
  140. package/registry/cornerstone3d/6/display-sets/tests/typescript/review3-unawaited-ecg-mount-empty-after-the-call/input.ts +18 -0
  141. package/registry/cornerstone3d/6/display-sets/tests/typescript/review3-unawaited-ecg-mount-empty-after-the-call/metrics.json +20 -0
  142. package/registry/cornerstone3d/6/display-sets/tests/typescript/review3-unawaited-helper-blocks-async-function/expected.ts +18 -0
  143. package/registry/cornerstone3d/6/display-sets/tests/typescript/review3-unawaited-helper-blocks-async-function/input.ts +18 -0
  144. package/registry/cornerstone3d/6/display-sets/tests/typescript/review3-unawaited-helper-blocks-async-function/metrics.json +12 -0
  145. package/registry/cornerstone3d/6/display-sets/tests/typescript/review3-unawaited-stack-mount-blocks-async-function/expected.ts +19 -0
  146. package/registry/cornerstone3d/6/display-sets/tests/typescript/review3-unawaited-stack-mount-blocks-async-function/input.ts +19 -0
  147. package/registry/cornerstone3d/6/display-sets/tests/typescript/review3-unawaited-stack-mount-blocks-async-function/metrics.json +20 -0
  148. package/registry/cornerstone3d/6/display-sets/tests/typescript/review3-unawaited-video-mount-empty-after-the-call/expected.ts +18 -0
  149. package/registry/cornerstone3d/6/display-sets/tests/typescript/review3-unawaited-video-mount-empty-after-the-call/input.ts +18 -0
  150. package/registry/cornerstone3d/6/display-sets/tests/typescript/review3-unawaited-video-mount-empty-after-the-call/metrics.json +20 -0
  151. package/registry/cornerstone3d/6/display-sets/tests/typescript/review3-unawaited-volume-mount-blocks-async-function/expected.ts +23 -0
  152. package/registry/cornerstone3d/6/display-sets/tests/typescript/review3-unawaited-volume-mount-blocks-async-function/input.ts +23 -0
  153. package/registry/cornerstone3d/6/display-sets/tests/typescript/review3-unawaited-volume-mount-blocks-async-function/metrics.json +20 -0
  154. package/registry/cornerstone3d/6/display-sets/tests/typescript/review3-viewport-getter-read-three-times/expected.ts +34 -0
  155. package/registry/cornerstone3d/6/display-sets/tests/typescript/review3-viewport-getter-read-three-times/input.ts +19 -0
  156. package/registry/cornerstone3d/6/display-sets/tests/typescript/review4-argument-getter-redirects-stack-mount/expected.ts +80 -0
  157. package/registry/cornerstone3d/6/display-sets/tests/typescript/review4-argument-getter-redirects-stack-mount/input.ts +50 -0
  158. package/registry/cornerstone3d/6/display-sets/workflow.yaml +29 -0
  159. package/registry/cornerstone3d/6/events/README.md +65 -0
  160. package/registry/cornerstone3d/6/events/codemod.yaml +20 -0
  161. package/registry/cornerstone3d/6/events/scripts/events.ts +442 -0
  162. package/registry/cornerstone3d/6/events/scripts/lib.ts +2386 -0
  163. package/registry/cornerstone3d/6/events/tests/tsx/edge-reported-and-conflicts/expected.tsx +18 -0
  164. package/registry/cornerstone3d/6/events/tests/tsx/edge-reported-and-conflicts/input.tsx +18 -0
  165. package/registry/cornerstone3d/6/events/tests/tsx/edge-reported-and-conflicts/metrics.json +46 -0
  166. package/registry/cornerstone3d/6/events/tests/typescript/negative-other-events/expected.ts +10 -0
  167. package/registry/cornerstone3d/6/events/tests/typescript/negative-other-events/input.ts +10 -0
  168. package/registry/cornerstone3d/6/events/tests/typescript/ohif-core-events-export/expected.ts +22 -0
  169. package/registry/cornerstone3d/6/events/tests/typescript/ohif-core-events-export/input.ts +22 -0
  170. package/registry/cornerstone3d/6/events/tests/typescript/ohif-core-events-export/metrics.json +54 -0
  171. package/registry/cornerstone3d/6/events/tests/typescript/positive-destructured/expected.ts +20 -0
  172. package/registry/cornerstone3d/6/events/tests/typescript/positive-destructured/input.ts +20 -0
  173. package/registry/cornerstone3d/6/events/tests/typescript/positive-destructured/metrics.json +12 -0
  174. package/registry/cornerstone3d/6/events/tests/typescript/positive-members/expected.ts +16 -0
  175. package/registry/cornerstone3d/6/events/tests/typescript/positive-members/input.ts +16 -0
  176. package/registry/cornerstone3d/6/events/tests/typescript/positive-members/metrics.json +52 -0
  177. package/registry/cornerstone3d/6/events/tests/typescript/review-aliased-event-destructuring-left-silently/expected.ts +5 -0
  178. package/registry/cornerstone3d/6/events/tests/typescript/review-aliased-event-destructuring-left-silently/input.ts +5 -0
  179. package/registry/cornerstone3d/6/events/tests/typescript/review-aliased-event-destructuring-left-silently/metrics.json +12 -0
  180. package/registry/cornerstone3d/6/events/tests/typescript/review-event-destructuring-introduces-undeclared-name/expected.ts +9 -0
  181. package/registry/cornerstone3d/6/events/tests/typescript/review-event-destructuring-introduces-undeclared-name/input.ts +10 -0
  182. package/registry/cornerstone3d/6/events/tests/typescript/review-event-destructuring-introduces-undeclared-name/metrics.json +20 -0
  183. package/registry/cornerstone3d/6/events/tests/typescript/review-removed-event-strings-not-reported/expected.ts +11 -0
  184. package/registry/cornerstone3d/6/events/tests/typescript/review-removed-event-strings-not-reported/input.ts +11 -0
  185. package/registry/cornerstone3d/6/events/tests/typescript/review-removed-event-strings-not-reported/metrics.json +28 -0
  186. package/registry/cornerstone3d/6/events/tests/typescript/review2-quoted-defaulted-and-bracketed-event-keys/expected.ts +15 -0
  187. package/registry/cornerstone3d/6/events/tests/typescript/review2-quoted-defaulted-and-bracketed-event-keys/input.ts +15 -0
  188. package/registry/cornerstone3d/6/events/tests/typescript/review2-quoted-defaulted-and-bracketed-event-keys/metrics.json +62 -0
  189. package/registry/cornerstone3d/6/events/workflow.yaml +29 -0
  190. package/registry/cornerstone3d/6/settings/README.md +69 -0
  191. package/registry/cornerstone3d/6/settings/codemod.yaml +20 -0
  192. package/registry/cornerstone3d/6/settings/scripts/lib.ts +2386 -0
  193. package/registry/cornerstone3d/6/settings/scripts/settings.ts +647 -0
  194. package/registry/cornerstone3d/6/settings/tests/tsx/edge-namespace-and-reported/expected.tsx +18 -0
  195. package/registry/cornerstone3d/6/settings/tests/tsx/edge-namespace-and-reported/input.tsx +18 -0
  196. package/registry/cornerstone3d/6/settings/tests/tsx/edge-namespace-and-reported/metrics.json +46 -0
  197. package/registry/cornerstone3d/6/settings/tests/typescript/negative-other-names/expected.ts +18 -0
  198. package/registry/cornerstone3d/6/settings/tests/typescript/negative-other-names/input.ts +18 -0
  199. package/registry/cornerstone3d/6/settings/tests/typescript/positive-configuration/expected.ts +27 -0
  200. package/registry/cornerstone3d/6/settings/tests/typescript/positive-configuration/input.ts +28 -0
  201. package/registry/cornerstone3d/6/settings/tests/typescript/positive-configuration/metrics.json +46 -0
  202. package/registry/cornerstone3d/6/settings/tests/typescript/positive-functions/expected.ts +32 -0
  203. package/registry/cornerstone3d/6/settings/tests/typescript/positive-functions/input.ts +33 -0
  204. package/registry/cornerstone3d/6/settings/tests/typescript/positive-functions/metrics.json +36 -0
  205. package/registry/cornerstone3d/6/settings/tests/typescript/review-aliased-setting-imports-removed-while-used/expected.ts +10 -0
  206. package/registry/cornerstone3d/6/settings/tests/typescript/review-aliased-setting-imports-removed-while-used/input.ts +9 -0
  207. package/registry/cornerstone3d/6/settings/tests/typescript/review-aliased-setting-imports-removed-while-used/metrics.json +22 -0
  208. package/registry/cornerstone3d/6/settings/tests/typescript/review-generated-references-resolve-to-shadowing-locals/expected.ts +12 -0
  209. package/registry/cornerstone3d/6/settings/tests/typescript/review-generated-references-resolve-to-shadowing-locals/input.ts +12 -0
  210. package/registry/cornerstone3d/6/settings/tests/typescript/review-import-name-collision-calls-undefined/expected.ts +9 -0
  211. package/registry/cornerstone3d/6/settings/tests/typescript/review-import-name-collision-calls-undefined/input.ts +9 -0
  212. package/registry/cornerstone3d/6/settings/tests/typescript/review-import-name-collision-calls-undefined/metrics.json +12 -0
  213. package/registry/cornerstone3d/6/settings/tests/typescript/review-settings-deletes-unrelated-configuration/expected.ts +12 -0
  214. package/registry/cornerstone3d/6/settings/tests/typescript/review-settings-deletes-unrelated-configuration/input.ts +12 -0
  215. package/registry/cornerstone3d/6/settings/tests/typescript/review-settings-deletes-unrelated-configuration/metrics.json +28 -0
  216. package/registry/cornerstone3d/6/settings/tests/typescript/review2-adjacent-obsolete-settings/expected.ts +24 -0
  217. package/registry/cornerstone3d/6/settings/tests/typescript/review2-adjacent-obsolete-settings/input.ts +29 -0
  218. package/registry/cornerstone3d/6/settings/tests/typescript/review2-adjacent-obsolete-settings/metrics.json +12 -0
  219. package/registry/cornerstone3d/6/settings/tests/typescript/review2-nested-cpu-getter-in-rewritten-setting/expected.ts +3 -0
  220. package/registry/cornerstone3d/6/settings/tests/typescript/review2-nested-cpu-getter-in-rewritten-setting/input.ts +3 -0
  221. package/registry/cornerstone3d/6/settings/tests/typescript/review2-nested-cpu-getter-in-rewritten-setting/metrics.json +12 -0
  222. package/registry/cornerstone3d/6/settings/tests/typescript/review2-re-exported-setting-names/expected.ts +1 -0
  223. package/registry/cornerstone3d/6/settings/tests/typescript/review2-re-exported-setting-names/input.ts +1 -0
  224. package/registry/cornerstone3d/6/settings/tests/typescript/review2-re-exported-setting-names/metrics.json +20 -0
  225. package/registry/cornerstone3d/6/settings/tests/typescript/review2-spread-boolean-tuple/expected.ts +6 -0
  226. package/registry/cornerstone3d/6/settings/tests/typescript/review2-spread-boolean-tuple/input.ts +6 -0
  227. package/registry/cornerstone3d/6/settings/tests/typescript/review2-spread-boolean-tuple/metrics.json +22 -0
  228. package/registry/cornerstone3d/6/settings/tests/typescript/review3-shorthand-cpu-setting/expected.ts +13 -0
  229. package/registry/cornerstone3d/6/settings/tests/typescript/review3-shorthand-cpu-setting/input.ts +13 -0
  230. package/registry/cornerstone3d/6/settings/tests/typescript/review3-shorthand-cpu-setting/metrics.json +12 -0
  231. package/registry/cornerstone3d/6/settings/workflow.yaml +29 -0
  232. package/registry/cornerstone3d/6/tools/README.md +59 -0
  233. package/registry/cornerstone3d/6/tools/codemod.yaml +20 -0
  234. package/registry/cornerstone3d/6/tools/scripts/lib.ts +2386 -0
  235. package/registry/cornerstone3d/6/tools/scripts/tools.ts +517 -0
  236. package/registry/cornerstone3d/6/tools/tests/tsx/edge-removed-names/expected.tsx +36 -0
  237. package/registry/cornerstone3d/6/tools/tests/tsx/edge-removed-names/input.tsx +36 -0
  238. package/registry/cornerstone3d/6/tools/tests/tsx/edge-removed-names/metrics.json +60 -0
  239. package/registry/cornerstone3d/6/tools/tests/typescript/negative-other-modules/expected.ts +13 -0
  240. package/registry/cornerstone3d/6/tools/tests/typescript/negative-other-modules/input.ts +13 -0
  241. package/registry/cornerstone3d/6/tools/tests/typescript/positive-moved-modules/expected.ts +11 -0
  242. package/registry/cornerstone3d/6/tools/tests/typescript/positive-moved-modules/input.ts +11 -0
  243. package/registry/cornerstone3d/6/tools/tests/typescript/positive-viewport-capabilities-to-core/expected.ts +13 -0
  244. package/registry/cornerstone3d/6/tools/tests/typescript/positive-viewport-capabilities-to-core/input.ts +13 -0
  245. package/registry/cornerstone3d/6/tools/tests/typescript/review-moved-import-collides-with-local/expected.ts +7 -0
  246. package/registry/cornerstone3d/6/tools/tests/typescript/review-moved-import-collides-with-local/input.ts +7 -0
  247. package/registry/cornerstone3d/6/tools/tests/typescript/review-viewport-capabilities-not-plain/expected.ts +26 -0
  248. package/registry/cornerstone3d/6/tools/tests/typescript/review-viewport-capabilities-not-plain/input.ts +26 -0
  249. package/registry/cornerstone3d/6/tools/tests/typescript/review-viewport-capabilities-not-plain/metrics.json +60 -0
  250. package/registry/cornerstone3d/6/tools/tests/typescript/review2-barrel-re-export-of-moved-module/expected.ts +6 -0
  251. package/registry/cornerstone3d/6/tools/tests/typescript/review2-barrel-re-export-of-moved-module/input.ts +6 -0
  252. package/registry/cornerstone3d/6/tools/tests/typescript/review2-barrel-re-export-of-moved-module/metrics.json +28 -0
  253. package/registry/cornerstone3d/6/tools/tests/typescript/review3-side-effect-and-empty-imports-of-deleted-module/expected.ts +12 -0
  254. package/registry/cornerstone3d/6/tools/tests/typescript/review3-side-effect-and-empty-imports-of-deleted-module/input.ts +12 -0
  255. package/registry/cornerstone3d/6/tools/tests/typescript/review3-side-effect-and-empty-imports-of-deleted-module/metrics.json +36 -0
  256. package/registry/cornerstone3d/6/tools/workflow.yaml +29 -0
  257. package/registry/cornerstone3d/6/viewport-members/README.md +101 -0
  258. package/registry/cornerstone3d/6/viewport-members/codemod.yaml +20 -0
  259. package/registry/cornerstone3d/6/viewport-members/scripts/lib.ts +2386 -0
  260. package/registry/cornerstone3d/6/viewport-members/scripts/viewport-members.ts +815 -0
  261. package/registry/cornerstone3d/6/viewport-members/tests/tsx/edge-reported-members/expected.tsx +31 -0
  262. package/registry/cornerstone3d/6/viewport-members/tests/tsx/edge-reported-members/input.tsx +31 -0
  263. package/registry/cornerstone3d/6/viewport-members/tests/tsx/edge-reported-members/metrics.json +100 -0
  264. package/registry/cornerstone3d/6/viewport-members/tests/tsx/edge-reported-members-on-a-typed-viewport/expected.tsx +31 -0
  265. package/registry/cornerstone3d/6/viewport-members/tests/tsx/edge-reported-members-on-a-typed-viewport/input.tsx +31 -0
  266. package/registry/cornerstone3d/6/viewport-members/tests/tsx/edge-reported-members-on-a-typed-viewport/metrics.json +100 -0
  267. package/registry/cornerstone3d/6/viewport-members/tests/typescript/negative-other-receivers/expected.ts +24 -0
  268. package/registry/cornerstone3d/6/viewport-members/tests/typescript/negative-other-receivers/input.ts +24 -0
  269. package/registry/cornerstone3d/6/viewport-members/tests/typescript/positive-3d-presentation/expected.ts +11 -0
  270. package/registry/cornerstone3d/6/viewport-members/tests/typescript/positive-3d-presentation/input.ts +11 -0
  271. package/registry/cornerstone3d/6/viewport-members/tests/typescript/positive-3d-presentation/metrics.json +28 -0
  272. package/registry/cornerstone3d/6/viewport-members/tests/typescript/positive-engine-render-frame-of-reference/expected.ts +16 -0
  273. package/registry/cornerstone3d/6/viewport-members/tests/typescript/positive-engine-render-frame-of-reference/input.ts +16 -0
  274. package/registry/cornerstone3d/6/viewport-members/tests/typescript/positive-engine-render-frame-of-reference/metrics.json +28 -0
  275. package/registry/cornerstone3d/6/viewport-members/tests/typescript/positive-presentation-and-view/expected.ts +31 -0
  276. package/registry/cornerstone3d/6/viewport-members/tests/typescript/positive-presentation-and-view/input.ts +31 -0
  277. package/registry/cornerstone3d/6/viewport-members/tests/typescript/positive-questions-and-actors/expected.ts +31 -0
  278. package/registry/cornerstone3d/6/viewport-members/tests/typescript/positive-questions-and-actors/input.ts +26 -0
  279. package/registry/cornerstone3d/6/viewport-members/tests/typescript/positive-questions-and-actors/metrics.json +22 -0
  280. package/registry/cornerstone3d/6/viewport-members/tests/typescript/positive-video-and-ecg/expected.ts +15 -0
  281. package/registry/cornerstone3d/6/viewport-members/tests/typescript/positive-video-and-ecg/input.ts +15 -0
  282. package/registry/cornerstone3d/6/viewport-members/tests/typescript/positive-video-and-ecg/metrics.json +28 -0
  283. package/registry/cornerstone3d/6/viewport-members/tests/typescript/review-generated-imports-invalidate-file-directives/expected.ts +5 -0
  284. package/registry/cornerstone3d/6/viewport-members/tests/typescript/review-generated-imports-invalidate-file-directives/input.ts +5 -0
  285. package/registry/cornerstone3d/6/viewport-members/tests/typescript/review-generated-imports-invalidate-file-directives/metrics.json +12 -0
  286. package/registry/cornerstone3d/6/viewport-members/tests/typescript/review-quoted-presentation-key-loses-effect/expected.ts +12 -0
  287. package/registry/cornerstone3d/6/viewport-members/tests/typescript/review-quoted-presentation-key-loses-effect/input.ts +12 -0
  288. package/registry/cornerstone3d/6/viewport-members/tests/typescript/review-quoted-presentation-key-loses-effect/metrics.json +12 -0
  289. package/registry/cornerstone3d/6/viewport-members/tests/typescript/review-slice-index-rewrite-mishandles-references/expected.ts +12 -0
  290. package/registry/cornerstone3d/6/viewport-members/tests/typescript/review-slice-index-rewrite-mishandles-references/input.ts +12 -0
  291. package/registry/cornerstone3d/6/viewport-members/tests/typescript/review-slice-index-rewrite-mishandles-references/metrics.json +20 -0
  292. package/registry/cornerstone3d/6/viewport-members/tests/typescript/review-viewport-like-name-rewrites-unrelated-objects/expected.ts +9 -0
  293. package/registry/cornerstone3d/6/viewport-members/tests/typescript/review-viewport-like-name-rewrites-unrelated-objects/input.ts +9 -0
  294. package/registry/cornerstone3d/6/viewport-members/tests/typescript/review-viewport-like-name-rewrites-unrelated-objects/metrics.json +20 -0
  295. package/registry/cornerstone3d/6/viewport-members/tests/typescript/review2-bracket-access-hides-removed-methods/expected.ts +11 -0
  296. package/registry/cornerstone3d/6/viewport-members/tests/typescript/review2-bracket-access-hides-removed-methods/input.ts +11 -0
  297. package/registry/cornerstone3d/6/viewport-members/tests/typescript/review2-bracket-access-hides-removed-methods/metrics.json +36 -0
  298. package/registry/cornerstone3d/6/viewport-members/tests/typescript/review2-re-exported-core-helpers/expected.ts +5 -0
  299. package/registry/cornerstone3d/6/viewport-members/tests/typescript/review2-re-exported-core-helpers/input.ts +5 -0
  300. package/registry/cornerstone3d/6/viewport-members/tests/typescript/review2-re-exported-core-helpers/metrics.json +20 -0
  301. package/registry/cornerstone3d/6/viewport-members/tests/typescript/review3-conditional-mount-drops-presentation/expected.ts +58 -0
  302. package/registry/cornerstone3d/6/viewport-members/tests/typescript/review3-conditional-mount-drops-presentation/input.ts +58 -0
  303. package/registry/cornerstone3d/6/viewport-members/tests/typescript/review3-conditional-mount-drops-presentation/metrics.json +20 -0
  304. package/registry/cornerstone3d/6/viewport-members/tests/typescript/review4-strict-bounds-and-actor-volume-ids/expected.ts +16 -0
  305. package/registry/cornerstone3d/6/viewport-members/tests/typescript/review4-strict-bounds-and-actor-volume-ids/input.ts +16 -0
  306. package/registry/cornerstone3d/6/viewport-members/tests/typescript/review4-strict-bounds-and-actor-volume-ids/metrics.json +12 -0
  307. package/registry/cornerstone3d/6/viewport-members/workflow.yaml +29 -0
  308. package/registry/cornerstone3d/6/viewport-types/README.md +73 -0
  309. package/registry/cornerstone3d/6/viewport-types/codemod.yaml +20 -0
  310. package/registry/cornerstone3d/6/viewport-types/scripts/lib.ts +2386 -0
  311. package/registry/cornerstone3d/6/viewport-types/scripts/viewport-types.ts +714 -0
  312. package/registry/cornerstone3d/6/viewport-types/tests/tsx/edge-reported-sites/expected.tsx +57 -0
  313. package/registry/cornerstone3d/6/viewport-types/tests/tsx/edge-reported-sites/input.tsx +57 -0
  314. package/registry/cornerstone3d/6/viewport-types/tests/tsx/edge-reported-sites/metrics.json +70 -0
  315. package/registry/cornerstone3d/6/viewport-types/tests/typescript/edge-already-6/expected.ts +25 -0
  316. package/registry/cornerstone3d/6/viewport-types/tests/typescript/edge-already-6/input.ts +25 -0
  317. package/registry/cornerstone3d/6/viewport-types/tests/typescript/negative-other-names/expected.ts +22 -0
  318. package/registry/cornerstone3d/6/viewport-types/tests/typescript/negative-other-names/input.ts +22 -0
  319. package/registry/cornerstone3d/6/viewport-types/tests/typescript/positive-destructured-viewport-type/expected.ts +68 -0
  320. package/registry/cornerstone3d/6/viewport-types/tests/typescript/positive-destructured-viewport-type/input.ts +65 -0
  321. package/registry/cornerstone3d/6/viewport-types/tests/typescript/positive-destructured-viewport-type/metrics.json +46 -0
  322. package/registry/cornerstone3d/6/viewport-types/tests/typescript/positive-namespaced-and-aliased/expected.ts +20 -0
  323. package/registry/cornerstone3d/6/viewport-types/tests/typescript/positive-namespaced-and-aliased/input.ts +19 -0
  324. package/registry/cornerstone3d/6/viewport-types/tests/typescript/positive-namespaced-and-aliased/metrics.json +12 -0
  325. package/registry/cornerstone3d/6/viewport-types/tests/typescript/positive-strings/expected.ts +25 -0
  326. package/registry/cornerstone3d/6/viewport-types/tests/typescript/positive-strings/input.ts +24 -0
  327. package/registry/cornerstone3d/6/viewport-types/tests/typescript/positive-strings/metrics.json +22 -0
  328. package/registry/cornerstone3d/6/viewport-types/tests/typescript/review-destructured-viewport-type-members/expected.ts +19 -0
  329. package/registry/cornerstone3d/6/viewport-types/tests/typescript/review-destructured-viewport-type-members/input.ts +19 -0
  330. package/registry/cornerstone3d/6/viewport-types/tests/typescript/review-destructured-viewport-type-members/metrics.json +12 -0
  331. package/registry/cornerstone3d/6/viewport-types/tests/typescript/review-type-check-can-prevent-the-first-mount/expected.ts +27 -0
  332. package/registry/cornerstone3d/6/viewport-types/tests/typescript/review-type-check-can-prevent-the-first-mount/input.ts +27 -0
  333. package/registry/cornerstone3d/6/viewport-types/tests/typescript/review-type-check-can-prevent-the-first-mount/metrics.json +28 -0
  334. package/registry/cornerstone3d/6/viewport-types/tests/typescript/review-viewport-like-name-rewrites-unrelated-objects/expected.ts +9 -0
  335. package/registry/cornerstone3d/6/viewport-types/tests/typescript/review-viewport-like-name-rewrites-unrelated-objects/input.ts +9 -0
  336. package/registry/cornerstone3d/6/viewport-types/tests/typescript/review-viewport-like-name-rewrites-unrelated-objects/metrics.json +20 -0
  337. package/registry/cornerstone3d/6/viewport-types/tests/typescript/review-viewport-type-pass-changes-unrelated-discriminators/expected.ts +18 -0
  338. package/registry/cornerstone3d/6/viewport-types/tests/typescript/review-viewport-type-pass-changes-unrelated-discriminators/input.ts +18 -0
  339. package/registry/cornerstone3d/6/viewport-types/tests/typescript/review-viewport-type-pass-changes-unrelated-discriminators/metrics.json +30 -0
  340. package/registry/cornerstone3d/6/viewport-types/tests/typescript/review2-mount-proof-crosses-shadowed-binding/expected.ts +42 -0
  341. package/registry/cornerstone3d/6/viewport-types/tests/typescript/review2-mount-proof-crosses-shadowed-binding/input.ts +42 -0
  342. package/registry/cornerstone3d/6/viewport-types/tests/typescript/review2-mount-proof-crosses-shadowed-binding/metrics.json +36 -0
  343. package/registry/cornerstone3d/6/viewport-types/tests/typescript/review2-quoted-defaulted-and-bracketed-viewport-type-keys/expected.ts +10 -0
  344. package/registry/cornerstone3d/6/viewport-types/tests/typescript/review2-quoted-defaulted-and-bracketed-viewport-type-keys/input.ts +10 -0
  345. package/registry/cornerstone3d/6/viewport-types/tests/typescript/review2-quoted-defaulted-and-bracketed-viewport-type-keys/metrics.json +36 -0
  346. package/registry/cornerstone3d/6/viewport-types/workflow.yaml +29 -0
  347. package/registry/cornerstone3d/6/workflow.yaml +26 -0
  348. package/scripts/test-registry-packages.mjs +86 -0
  349. package/scripts/validate-registry-packages.mjs +43 -2
@@ -0,0 +1,2386 @@
1
+ import type { Edit, SgNode, SgRoot } from 'codemod:ast-grep';
2
+ import type TSX from 'codemod:ast-grep/langs/tsx';
3
+
4
+ /**
5
+ * Helpers shared by the Cornerstone3D 6.0 transforms. Each registry package is
6
+ * published on its own, so each keeps a copy of this file;
7
+ * `scripts/validate-registry-packages.mjs` checks that the copies are equal.
8
+ */
9
+
10
+ /** A node of a TypeScript or JavaScript file. */
11
+ export type Node = SgNode<TSX>;
12
+
13
+ /** The package whose names the transforms rewrite. */
14
+ export const CORE_PACKAGE = '@cornerstonejs/core';
15
+
16
+ /** The longest line that a regenerated import statement keeps on one line. */
17
+ const IMPORT_LINE_LENGTH = 80;
18
+
19
+ /** A name of a viewport variable: `viewport`, `stackViewport`, `viewport2`. */
20
+ const VIEWPORT_NAME = /viewport\d*$/i;
21
+
22
+ /** A name of an array of viewports. */
23
+ const VIEWPORTS_NAME = /viewports$/i;
24
+
25
+ /** The family of each viewport type name, legacy and 6.0. */
26
+ const FAMILY_OF_TYPE: Record<string, ViewportFamily> = {
27
+ PlanarViewport: 'planar',
28
+ IStackViewport: 'planar',
29
+ IVolumeViewport: 'planar',
30
+ IBaseVolumeViewport: 'planar',
31
+ StackViewport: 'planar',
32
+ VolumeViewport: 'planar',
33
+ BaseVolumeViewport: 'planar',
34
+ VolumeViewport3D: '3d',
35
+ GenericVolumeViewport3D: '3d',
36
+ LegacyVolumeViewport3D: '3d',
37
+ VideoViewport: 'video',
38
+ IVideoViewport: 'video',
39
+ VideoGenericViewport: 'video',
40
+ WSIViewport: 'wsi',
41
+ IWSIViewport: 'wsi',
42
+ WSIGenericViewport: 'wsi',
43
+ ECGViewport: 'ecg',
44
+ IECGViewport: 'ecg',
45
+ ECGGenericViewport: 'ecg',
46
+ };
47
+
48
+ /** The core types of a viewport of any family. */
49
+ const ANY_VIEWPORT_TYPES = new Set([
50
+ 'IViewport',
51
+ 'IGenericViewport',
52
+ 'GenericViewport',
53
+ 'Viewport',
54
+ ]);
55
+
56
+ /** The core classes and types of a rendering engine. */
57
+ const ENGINE_TYPES = [
58
+ 'RenderingEngine',
59
+ 'IRenderingEngine',
60
+ 'BaseRenderingEngine',
61
+ 'TiledRenderingEngine',
62
+ 'ContextPoolRenderingEngine',
63
+ ];
64
+
65
+ /** The core functions that return an enabled element. */
66
+ const ENABLED_ELEMENT_GETTERS = [
67
+ 'getEnabledElement',
68
+ 'getEnabledElementByViewportId',
69
+ 'getEnabledElementByIds',
70
+ ];
71
+
72
+ /** The viewport families of 6.0. */
73
+ export type ViewportFamily = 'planar' | '3d' | 'video' | 'wsi' | 'ecg';
74
+
75
+ /**
76
+ * What shows that an expression is a Cornerstone viewport: `core` for a value
77
+ * reached through Cornerstone or a Cornerstone type, `name` for a name alone.
78
+ */
79
+ export type ViewportEvidence = 'core' | 'name';
80
+
81
+ /** The node kinds that start a function scope. */
82
+ const FUNCTION_KINDS = new Set([
83
+ 'function_declaration',
84
+ 'function_expression',
85
+ 'function',
86
+ 'arrow_function',
87
+ 'method_definition',
88
+ 'generator_function_declaration',
89
+ 'generator_function',
90
+ ]);
91
+
92
+ /** The node kinds that bound the scope of a `var` or a parameter. */
93
+ const FUNCTION_SCOPE_KINDS = new Set([...FUNCTION_KINDS, 'program']);
94
+
95
+ /** The node kinds that bound the scope of a `let`, a `const`, a function or a class. */
96
+ const BLOCK_KINDS = new Set([
97
+ 'statement_block',
98
+ 'program',
99
+ 'switch_body',
100
+ 'for_statement',
101
+ 'for_in_statement',
102
+ 'class_body',
103
+ ]);
104
+
105
+ /** The node kinds of a destructuring pattern. */
106
+ const PATTERN_KINDS = new Set([
107
+ 'object_pattern',
108
+ 'array_pattern',
109
+ 'pair_pattern',
110
+ 'assignment_pattern',
111
+ 'object_assignment_pattern',
112
+ 'rest_pattern',
113
+ ]);
114
+
115
+ /** The declarations whose name lives in the enclosing block. */
116
+ const BLOCK_DECLARATION_KINDS = new Set([
117
+ 'function_declaration',
118
+ 'generator_function_declaration',
119
+ 'class_declaration',
120
+ ]);
121
+
122
+ /** The expressions whose name lives in the expression itself. */
123
+ const NAMED_EXPRESSION_KINDS = new Set([
124
+ 'function_expression',
125
+ 'function',
126
+ 'generator_function',
127
+ 'class',
128
+ ]);
129
+
130
+ /** The node kinds of a literal value. */
131
+ const LITERAL_KINDS = new Set([
132
+ 'string',
133
+ 'number',
134
+ 'true',
135
+ 'false',
136
+ 'null',
137
+ 'undefined',
138
+ ]);
139
+
140
+ /** The node kinds that take a member access without parentheses. */
141
+ const PRIMARY_KINDS = new Set([
142
+ 'identifier',
143
+ 'this',
144
+ 'member_expression',
145
+ 'subscript_expression',
146
+ 'call_expression',
147
+ 'parenthesized_expression',
148
+ 'non_null_expression',
149
+ 'array',
150
+ 'string',
151
+ 'template_string',
152
+ ]);
153
+
154
+ /** The calls that mount data on a viewport, in 5.x and in 6.0. */
155
+ const MOUNT_METHODS = new Set([
156
+ 'setStack',
157
+ 'setVolumes',
158
+ 'setVideo',
159
+ 'setEcg',
160
+ 'setWSI',
161
+ 'setDataIds',
162
+ 'setDataList',
163
+ 'setDisplaySets',
164
+ ]);
165
+
166
+ /** The parents in which a comparison needs no parentheses. */
167
+ const LOOSE_PARENTS = new Set([
168
+ 'variable_declarator',
169
+ 'arguments',
170
+ 'parenthesized_expression',
171
+ 'return_statement',
172
+ 'pair',
173
+ 'array',
174
+ 'template_substitution',
175
+ 'expression_statement',
176
+ 'assignment_expression',
177
+ 'arrow_function',
178
+ 'ternary_expression',
179
+ ]);
180
+
181
+ /** One named specifier of an import statement. */
182
+ interface ImportSpecifierInfo {
183
+ name: string;
184
+ local: string;
185
+ isType: boolean;
186
+ node?: Node;
187
+ }
188
+
189
+ /** A named import statement of the core package and its pending changes. */
190
+ interface NamedImport {
191
+ node: Node;
192
+ namedImports: Node;
193
+ typeOnly: boolean;
194
+ multiline: boolean;
195
+ hasComments: boolean;
196
+ quote: string;
197
+ specifiers: ImportSpecifierInfo[];
198
+ originals: ImportSpecifierInfo[];
199
+ changed: boolean;
200
+ }
201
+
202
+ /** A name that a transform asks the core import to bring in. */
203
+ interface ImportAddition {
204
+ name: string;
205
+ local: string;
206
+ isType: boolean;
207
+ }
208
+
209
+ /** A declared name and the node that bounds its scope. */
210
+ interface Declaration {
211
+ node: Node;
212
+ scope: Node;
213
+ }
214
+
215
+ /**
216
+ * Returns the module that an import statement reads from.
217
+ */
218
+ export function importSource(statement: Node): string | undefined {
219
+ const source = statement.field('source');
220
+
221
+ return source ? source.text().slice(1, -1) : undefined;
222
+ }
223
+
224
+ /**
225
+ * Returns the information of a named import specifier.
226
+ */
227
+ function specifierInfo(
228
+ specifier: Node,
229
+ typeOnly: boolean
230
+ ): ImportSpecifierInfo {
231
+ const name = specifier.field('name')?.text() ?? '';
232
+ const alias = specifier.field('alias')?.text();
233
+
234
+ return {
235
+ name,
236
+ local: alias ?? name,
237
+ isType:
238
+ typeOnly || specifier.children().some((child) => child.kind() === 'type'),
239
+ node: specifier,
240
+ };
241
+ }
242
+
243
+ /**
244
+ * Formats one named import specifier.
245
+ */
246
+ function formatSpecifier(
247
+ specifier: ImportSpecifierInfo,
248
+ typeOnly: boolean
249
+ ): string {
250
+ const prefix = specifier.isType && !typeOnly ? 'type ' : '';
251
+ const alias =
252
+ specifier.local !== specifier.name ? ` as ${specifier.local}` : '';
253
+
254
+ return `${prefix}${specifier.name}${alias}`;
255
+ }
256
+
257
+ /**
258
+ * The imports of `@cornerstonejs/core` in one file. A transform asks it for the
259
+ * local name of a core name, asks it to bring a name in, to rename one or to
260
+ * drop one, and takes its edits at the end. It edits each import statement
261
+ * once, so other edits must not touch the import statements. It also gives
262
+ * out the fresh names of a run, so that no two of them collide.
263
+ */
264
+ export class CoreImports {
265
+ private readonly named: NamedImport[] = [];
266
+ private readonly additions: ImportAddition[] = [];
267
+ private readonly reserved = new Set<string>();
268
+ private readonly lastImport: Node | undefined;
269
+ private readonly firstCoreImport: Node | undefined;
270
+ private readonly rootNode: Node;
271
+ private readonly commonJs: boolean;
272
+ readonly namespace: string | undefined;
273
+ readonly typeNamespace: string | undefined;
274
+
275
+ constructor(rootNode: Node) {
276
+ this.rootNode = rootNode;
277
+
278
+ const statements = rootNode
279
+ .children()
280
+ .filter((child) => child.kind() === 'import_statement');
281
+ let namespace: string | undefined;
282
+ let typeNamespace: string | undefined;
283
+
284
+ this.lastImport = statements[statements.length - 1];
285
+ this.commonJs =
286
+ !statements.length &&
287
+ !rootNode
288
+ .children()
289
+ .some((child) => child.kind() === 'export_statement') &&
290
+ Boolean(
291
+ rootNode.find({
292
+ rule: {
293
+ kind: 'call_expression',
294
+ has: { field: 'function', regex: '^require$' },
295
+ },
296
+ })
297
+ );
298
+
299
+ for (const statement of statements) {
300
+ if (importSource(statement) !== CORE_PACKAGE) {
301
+ continue;
302
+ }
303
+
304
+ const typeOnly = statement
305
+ .children()
306
+ .some((child) => child.kind() === 'type');
307
+ const clause = statement
308
+ .children()
309
+ .find((child) => child.kind() === 'import_clause');
310
+ const namespaceImport = clause
311
+ ?.children()
312
+ .find((child) => child.kind() === 'namespace_import');
313
+
314
+ if (namespaceImport) {
315
+ const name = namespaceImport
316
+ .children()
317
+ .find((child) => child.kind() === 'identifier')
318
+ ?.text();
319
+
320
+ typeNamespace ??= name;
321
+
322
+ if (!typeOnly) {
323
+ namespace ??= name;
324
+ }
325
+
326
+ continue;
327
+ }
328
+
329
+ const namedImports = clause
330
+ ?.children()
331
+ .find((child) => child.kind() === 'named_imports');
332
+
333
+ if (!namedImports) {
334
+ continue;
335
+ }
336
+
337
+ const sourceText =
338
+ statement.field('source')?.text() ?? `'${CORE_PACKAGE}'`;
339
+ const specifiers = namedImports
340
+ .children()
341
+ .filter((child) => child.kind() === 'import_specifier')
342
+ .map((specifier) => specifierInfo(specifier, typeOnly));
343
+
344
+ this.firstCoreImport ??= statement;
345
+ this.named.push({
346
+ node: statement,
347
+ namedImports,
348
+ typeOnly,
349
+ multiline: namedImports.text().includes('\n'),
350
+ hasComments: namedImports
351
+ .children()
352
+ .some((child) => child.kind() === 'comment'),
353
+ quote: sourceText[0],
354
+ specifiers,
355
+ originals: [...specifiers],
356
+ changed: false,
357
+ });
358
+ }
359
+
360
+ this.namespace = namespace;
361
+ this.typeNamespace = typeNamespace;
362
+ }
363
+
364
+ /**
365
+ * Returns a name that no identifier of the file uses and that this run has
366
+ * not given out: `base`, or `base2`, `base3` and so on.
367
+ */
368
+ fresh(base: string): string {
369
+ for (let index = 1; ; index += 1) {
370
+ const candidate = index === 1 ? base : `${base}${index}`;
371
+
372
+ if (
373
+ !this.reserved.has(candidate) &&
374
+ !isNameTaken(this.rootNode, candidate)
375
+ ) {
376
+ this.reserved.add(candidate);
377
+
378
+ return candidate;
379
+ }
380
+ }
381
+ }
382
+
383
+ /**
384
+ * Returns the expressions that name a core export in this file, in the
385
+ * order to try them.
386
+ */
387
+ private candidates(name: string, type: boolean): string[] {
388
+ const found: string[] = [];
389
+
390
+ for (const entry of this.named) {
391
+ for (const specifier of entry.specifiers) {
392
+ if (specifier.name === name && (type || !specifier.isType)) {
393
+ found.push(specifier.local);
394
+ }
395
+ }
396
+ }
397
+
398
+ for (const addition of this.additions) {
399
+ if (addition.name === name && (type || !addition.isType)) {
400
+ found.push(addition.local);
401
+ }
402
+ }
403
+
404
+ const namespace = type ? this.typeNamespace : this.namespace;
405
+
406
+ if (namespace) {
407
+ found.push(`${namespace}.${name}`);
408
+ }
409
+
410
+ return found;
411
+ }
412
+
413
+ /**
414
+ * Returns the expression that names a core export in this file: its local
415
+ * name, or `namespace.name`, or undefined when the file has no access to it.
416
+ * A value use needs a specifier that is not `type` and a namespace that is
417
+ * not `import type`; with `at`, a name that a local declaration hides at
418
+ * that node does not count.
419
+ */
420
+ ref(
421
+ name: string,
422
+ options: { type?: boolean; at?: Node } = {}
423
+ ): string | undefined {
424
+ return this.candidates(name, Boolean(options.type)).find(
425
+ (candidate) =>
426
+ options.type ||
427
+ !options.at ||
428
+ !isShadowedAt(this.rootNode, candidate.split('.')[0], options.at)
429
+ );
430
+ }
431
+
432
+ /**
433
+ * Returns the expression that names a core export at a node, and brings
434
+ * the name in when the file has no access to it there. A name that the file
435
+ * uses already comes in under a fresh alias such as `coreUtilities`.
436
+ * Returns undefined in a CommonJS module, which cannot take an `import`.
437
+ */
438
+ ensure(
439
+ name: string,
440
+ options: { type?: boolean; at?: Node } = {}
441
+ ): string | undefined {
442
+ const existing = this.ref(name, options);
443
+
444
+ if (existing || this.commonJs) {
445
+ return existing;
446
+ }
447
+
448
+ for (const entry of this.named) {
449
+ const specifier = entry.typeOnly
450
+ ? undefined
451
+ : entry.specifiers.find(
452
+ (candidate) =>
453
+ candidate.name === name &&
454
+ candidate.isType &&
455
+ (!options.at ||
456
+ !isShadowedAt(this.rootNode, candidate.local, options.at))
457
+ );
458
+
459
+ if (specifier) {
460
+ specifier.isType = Boolean(options.type);
461
+ entry.changed = true;
462
+
463
+ return specifier.local;
464
+ }
465
+ }
466
+
467
+ const local =
468
+ !this.reserved.has(name) && !isNameTaken(this.rootNode, name)
469
+ ? name
470
+ : this.fresh(`core${name[0].toUpperCase()}${name.slice(1)}`);
471
+
472
+ this.reserved.add(local);
473
+ this.additions.push({ name, local, isType: Boolean(options.type) });
474
+
475
+ return local;
476
+ }
477
+
478
+ /**
479
+ * Returns the local name of a named core import, or undefined.
480
+ */
481
+ local(name: string): string | undefined {
482
+ for (const entry of this.named) {
483
+ const specifier = entry.specifiers.find(
484
+ (candidate) => candidate.name === name
485
+ );
486
+
487
+ if (specifier) {
488
+ return specifier.local;
489
+ }
490
+ }
491
+
492
+ return undefined;
493
+ }
494
+
495
+ /**
496
+ * Renames the imported name `name` to `newName`. An aliased specifier keeps
497
+ * its local name. When `newName` is imported already, the old specifier
498
+ * goes. Returns the local name that the references use afterwards.
499
+ */
500
+ rename(name: string, newName: string): string | undefined {
501
+ let result: string | undefined;
502
+
503
+ for (const entry of this.named) {
504
+ const index = entry.specifiers.findIndex(
505
+ (specifier) => specifier.name === name
506
+ );
507
+
508
+ if (index === -1) {
509
+ continue;
510
+ }
511
+
512
+ const specifier = entry.specifiers[index];
513
+ const existing = this.named
514
+ .flatMap((candidate) => candidate.specifiers)
515
+ .find(
516
+ (candidate) =>
517
+ candidate.name === newName && candidate.local === newName
518
+ );
519
+ const keepLocal =
520
+ specifier.local !== name ||
521
+ (!existing &&
522
+ (this.reserved.has(newName) || isNameTaken(this.rootNode, newName)));
523
+
524
+ entry.changed = true;
525
+
526
+ if (existing && !keepLocal) {
527
+ existing.isType &&= specifier.isType;
528
+ entry.specifiers.splice(index, 1);
529
+ result = existing.local;
530
+ continue;
531
+ }
532
+
533
+ result = keepLocal ? specifier.local : newName;
534
+ specifier.local = result;
535
+ specifier.name = newName;
536
+ }
537
+
538
+ return result;
539
+ }
540
+
541
+ /**
542
+ * Drops the specifiers of `name` from the core imports, or only the one
543
+ * with the local name `local`.
544
+ */
545
+ remove(name: string, local?: string): void {
546
+ for (const entry of this.named) {
547
+ const before = entry.specifiers.length;
548
+
549
+ entry.specifiers = entry.specifiers.filter(
550
+ (specifier) =>
551
+ specifier.name !== name ||
552
+ (local !== undefined && specifier.local !== local)
553
+ );
554
+ entry.changed ||= entry.specifiers.length !== before;
555
+ }
556
+ }
557
+
558
+ /**
559
+ * Returns the edits of an import statement that holds comments: each
560
+ * specifier is edited in place, and new ones go in after `{`, so the
561
+ * comments stay where they are.
562
+ */
563
+ private editsInPlace(entry: NamedImport): Edit[] {
564
+ const edits: Edit[] = [];
565
+
566
+ for (const original of entry.originals) {
567
+ if (!entry.specifiers.includes(original)) {
568
+ edits.push(removeProperty(original.node));
569
+ continue;
570
+ }
571
+
572
+ const text = formatSpecifier(original, entry.typeOnly);
573
+
574
+ if (text !== original.node.text()) {
575
+ edits.push(original.node.replace(text));
576
+ }
577
+ }
578
+
579
+ const added = entry.specifiers.filter(
580
+ (specifier) => !entry.originals.includes(specifier)
581
+ );
582
+
583
+ if (added.length) {
584
+ const brace = entry.namedImports.children()[0].range().end.index;
585
+ const separator = entry.multiline ? '\n ' : ' ';
586
+
587
+ edits.push({
588
+ startPos: brace,
589
+ endPos: brace,
590
+ insertedText: added
591
+ .map(
592
+ (specifier) =>
593
+ `${separator}${formatSpecifier(specifier, entry.typeOnly)},`
594
+ )
595
+ .join(''),
596
+ });
597
+ }
598
+
599
+ return edits;
600
+ }
601
+
602
+ /**
603
+ * Returns the edits of the import statements.
604
+ */
605
+ edits(): Edit[] {
606
+ const edits: Edit[] = [];
607
+ const valueAdditions = this.additions.filter(
608
+ (addition) => !addition.isType
609
+ );
610
+ const typeAdditions = this.additions.filter((addition) => addition.isType);
611
+ const valueTarget = this.named.find((entry) => !entry.typeOnly);
612
+ const typeTarget = this.named.find((entry) => entry.typeOnly);
613
+ const newStatements: string[] = [];
614
+
615
+ if (valueAdditions.length) {
616
+ if (valueTarget) {
617
+ valueTarget.specifiers.push(...valueAdditions);
618
+ valueTarget.changed = true;
619
+ } else {
620
+ newStatements.push(formatImport(false, valueAdditions, false, "'"));
621
+ }
622
+ }
623
+
624
+ if (typeAdditions.length) {
625
+ if (typeTarget) {
626
+ typeTarget.specifiers.push(
627
+ ...typeAdditions.filter(
628
+ (addition) =>
629
+ !typeTarget.specifiers.some(
630
+ (specifier) =>
631
+ specifier.name === addition.name &&
632
+ specifier.local === addition.local
633
+ )
634
+ )
635
+ );
636
+ typeTarget.changed = true;
637
+ } else {
638
+ newStatements.push(formatImport(true, typeAdditions, false, "'"));
639
+ }
640
+ }
641
+
642
+ const prefix =
643
+ newStatements.length && this.firstCoreImport
644
+ ? `${newStatements.join('\n')}\n`
645
+ : '';
646
+
647
+ for (const entry of this.named) {
648
+ const isAnchor = entry.node.id() === this.firstCoreImport?.id();
649
+
650
+ if (!entry.changed && !(isAnchor && prefix)) {
651
+ continue;
652
+ }
653
+
654
+ if (entry.hasComments) {
655
+ if (isAnchor && prefix) {
656
+ const start = entry.node.range().start.index;
657
+
658
+ edits.push({ startPos: start, endPos: start, insertedText: prefix });
659
+ }
660
+
661
+ if (entry.changed) {
662
+ edits.push(...this.editsInPlace(entry));
663
+ }
664
+
665
+ continue;
666
+ }
667
+
668
+ if (!entry.specifiers.length) {
669
+ edits.push(
670
+ prefix && isAnchor
671
+ ? entry.node.replace(prefix.trimEnd())
672
+ : removeStatement(this.rootNode, entry.node)
673
+ );
674
+ continue;
675
+ }
676
+
677
+ const statement = entry.changed
678
+ ? formatImport(
679
+ entry.typeOnly,
680
+ entry.specifiers,
681
+ entry.multiline,
682
+ entry.quote
683
+ )
684
+ : entry.node.text();
685
+
686
+ edits.push(entry.node.replace(`${isAnchor ? prefix : ''}${statement}`));
687
+ }
688
+
689
+ if (newStatements.length && !this.firstCoreImport) {
690
+ const end = this.lastImport?.range().end.index ?? this.prologueEnd();
691
+ const text = newStatements.join('\n');
692
+
693
+ edits.push(
694
+ end
695
+ ? { startPos: end, endPos: end, insertedText: `\n${text}` }
696
+ : { startPos: 0, endPos: 0, insertedText: `${text}\n` }
697
+ );
698
+ }
699
+
700
+ return edits;
701
+ }
702
+
703
+ /**
704
+ * Returns where the prologue of the file ends: after a shebang and the
705
+ * directives such as `'use client'`, which must stay first. 0 when the file
706
+ * has neither.
707
+ */
708
+ private prologueEnd(): number {
709
+ let end = 0;
710
+
711
+ for (const child of this.rootNode.children()) {
712
+ const expression =
713
+ child.kind() === 'expression_statement'
714
+ ? child.children().find((part) => part.isNamed())
715
+ : undefined;
716
+
717
+ if (
718
+ child.kind() === 'hash_bang_line' ||
719
+ expression?.kind() === 'string'
720
+ ) {
721
+ end = child.range().end.index;
722
+ } else if (child.kind() !== 'comment') {
723
+ break;
724
+ }
725
+ }
726
+
727
+ return end;
728
+ }
729
+ }
730
+
731
+ /**
732
+ * Formats an import statement of the core package in the style of prettier.
733
+ */
734
+ function formatImport(
735
+ typeOnly: boolean,
736
+ specifiers: ImportSpecifierInfo[],
737
+ multiline: boolean,
738
+ quote: string
739
+ ): string {
740
+ const parts = specifiers.map((specifier) =>
741
+ formatSpecifier(specifier, typeOnly)
742
+ );
743
+ const head = typeOnly ? 'import type ' : 'import ';
744
+ const source = `${quote}${CORE_PACKAGE}${quote}`;
745
+ const oneLine = `${head}{ ${parts.join(', ')} } from ${source};`;
746
+
747
+ if (!multiline && oneLine.length <= IMPORT_LINE_LENGTH) {
748
+ return oneLine;
749
+ }
750
+
751
+ return `${head}{\n${parts.map((part) => ` ${part},`).join('\n')}\n} from ${source};`;
752
+ }
753
+
754
+ /**
755
+ * Returns an edit that removes a whole statement with its line break.
756
+ */
757
+ export function removeStatement(rootNode: Node, statement: Node): Edit {
758
+ const source = rootNode.text();
759
+ const { start, end } = statement.range();
760
+ let endIndex = end.index;
761
+
762
+ if (source[endIndex] === '\n') {
763
+ endIndex += 1;
764
+ }
765
+
766
+ return { startPos: start.index, endPos: endIndex, insertedText: '' };
767
+ }
768
+
769
+ /**
770
+ * Returns a name with the characters of a regular expression escaped.
771
+ */
772
+ function escapeName(name: string): string {
773
+ return name.replace(/[$]/g, '\\$');
774
+ }
775
+
776
+ /**
777
+ * Tells whether a name is used anywhere in the file as an identifier.
778
+ */
779
+ export function isNameTaken(rootNode: Node, name: string): boolean {
780
+ return Boolean(
781
+ rootNode.find({
782
+ rule: {
783
+ any: [
784
+ { kind: 'identifier' },
785
+ { kind: 'type_identifier' },
786
+ { kind: 'shorthand_property_identifier_pattern' },
787
+ { kind: 'shorthand_property_identifier' },
788
+ ],
789
+ regex: `^${escapeName(name)}$`,
790
+ },
791
+ })
792
+ );
793
+ }
794
+
795
+ /**
796
+ * Tells whether a node lies inside another node, or is that node.
797
+ */
798
+ function contains(outer: Node, node: Node): boolean {
799
+ const outerRange = outer.range();
800
+ const range = node.range();
801
+
802
+ return (
803
+ outerRange.start.index <= range.start.index &&
804
+ range.end.index <= outerRange.end.index
805
+ );
806
+ }
807
+
808
+ /**
809
+ * Returns the nearest ancestor of a node of one of the kinds, or the root.
810
+ */
811
+ function nearestOf(node: Node, kinds: Set<string>): Node | undefined {
812
+ const ancestors = node.ancestors();
813
+
814
+ return (
815
+ ancestors.find((ancestor) => kinds.has(ancestor.kind())) ??
816
+ ancestors[ancestors.length - 1]
817
+ );
818
+ }
819
+
820
+ /**
821
+ * Returns the node that bounds the scope of a name that a node declares: the
822
+ * function of a parameter or a `var`, the block of a `let`, a `const`, a
823
+ * function or a class, the module of an import. Returns undefined when the
824
+ * node does not declare a name.
825
+ */
826
+ export function declarationScope(node: Node): Node | undefined {
827
+ let child = node;
828
+ let parent = node.parent();
829
+
830
+ while (parent && PATTERN_KINDS.has(parent.kind())) {
831
+ const isKey =
832
+ parent.kind() === 'pair_pattern' &&
833
+ parent.field('key')?.id() === child.id();
834
+ const isDefault =
835
+ parent.kind() !== 'pair_pattern' &&
836
+ parent.field('right')?.id() === child.id();
837
+
838
+ if (isKey || isDefault) {
839
+ return undefined;
840
+ }
841
+
842
+ child = parent;
843
+ parent = parent.parent();
844
+ }
845
+
846
+ if (!parent) {
847
+ return undefined;
848
+ }
849
+
850
+ const kind = parent.kind();
851
+ const holder = parent;
852
+ const isField = (field: string) => holder.field(field)?.id() === child.id();
853
+
854
+ if (kind === 'variable_declarator') {
855
+ const declaration = parent.parent();
856
+
857
+ if (!isField('name') || !declaration) {
858
+ return undefined;
859
+ }
860
+
861
+ return nearestOf(
862
+ declaration,
863
+ declaration.kind() === 'variable_declaration'
864
+ ? FUNCTION_SCOPE_KINDS
865
+ : BLOCK_KINDS
866
+ );
867
+ }
868
+
869
+ if (kind === 'required_parameter' || kind === 'optional_parameter') {
870
+ return isField('pattern') ? enclosingFunction(parent) : undefined;
871
+ }
872
+
873
+ if (kind === 'formal_parameters') {
874
+ return parent.parent() ?? undefined;
875
+ }
876
+
877
+ if (kind === 'arrow_function' || kind === 'catch_clause') {
878
+ return isField('parameter') ? parent : undefined;
879
+ }
880
+
881
+ if (kind === 'for_in_statement') {
882
+ return isField('left') ? parent : undefined;
883
+ }
884
+
885
+ if (BLOCK_DECLARATION_KINDS.has(kind)) {
886
+ return isField('name') ? nearestOf(parent, BLOCK_KINDS) : undefined;
887
+ }
888
+
889
+ if (NAMED_EXPRESSION_KINDS.has(kind)) {
890
+ return isField('name') ? parent : undefined;
891
+ }
892
+
893
+ if (kind === 'import_specifier') {
894
+ return isField(parent.field('alias') ? 'alias' : 'name')
895
+ ? nearestOf(parent, new Set(['program']))
896
+ : undefined;
897
+ }
898
+
899
+ if (kind === 'import_clause' || kind === 'namespace_import') {
900
+ return nearestOf(parent, new Set(['program']));
901
+ }
902
+
903
+ return undefined;
904
+ }
905
+
906
+ /**
907
+ * Returns the declarations of a name in a file, each with its scope.
908
+ */
909
+ export function declarationsOf(rootNode: Node, name: string): Declaration[] {
910
+ const found: Declaration[] = [];
911
+
912
+ for (const node of rootNode.findAll({
913
+ rule: {
914
+ any: [
915
+ { kind: 'identifier' },
916
+ { kind: 'shorthand_property_identifier_pattern' },
917
+ ],
918
+ regex: `^${escapeName(name)}$`,
919
+ },
920
+ })) {
921
+ const scope = declarationScope(node);
922
+
923
+ if (scope) {
924
+ found.push({ node, scope });
925
+ }
926
+ }
927
+
928
+ return found;
929
+ }
930
+
931
+ /**
932
+ * Tells whether a local declaration hides a module-level name at a node.
933
+ */
934
+ export function isShadowedAt(rootNode: Node, name: string, at: Node): boolean {
935
+ return declarationsOf(rootNode, name).some(
936
+ ({ scope }) => scope.kind() !== 'program' && contains(scope, at)
937
+ );
938
+ }
939
+
940
+ /**
941
+ * Tells whether the name that a declaration node declares refers to that
942
+ * declaration at another node: its scope holds the node, and no declaration
943
+ * of the same name inside its scope holds the node too.
944
+ */
945
+ export function bindsAt(rootNode: Node, declaration: Node, at: Node): boolean {
946
+ const scope = declarationScope(declaration);
947
+
948
+ if (!scope || !contains(scope, at)) {
949
+ return false;
950
+ }
951
+
952
+ return !declarationsOf(rootNode, declaration.text()).some(
953
+ (other) =>
954
+ other.node.id() !== declaration.id() &&
955
+ contains(other.scope, at) &&
956
+ contains(scope, other.scope)
957
+ );
958
+ }
959
+
960
+ /**
961
+ * Returns the definition of an identifier, with the kind of the definition.
962
+ * A declarator with a type annotation comes back as its name, as one without.
963
+ */
964
+ function definitionOf(
965
+ identifier: Node
966
+ ): { kind: string; node: Node } | undefined {
967
+ const definition = identifier.definition();
968
+
969
+ if (!definition) {
970
+ return undefined;
971
+ }
972
+
973
+ const name =
974
+ definition.node.kind() === 'variable_declarator'
975
+ ? definition.node.field('name')
976
+ : undefined;
977
+
978
+ return { kind: definition.kind, node: name ?? definition.node };
979
+ }
980
+
981
+ /**
982
+ * Returns the node that declares the binding an identifier refers to: the
983
+ * name of a variable, a parameter, a destructured key or an import.
984
+ */
985
+ export function declarationNodeOf(identifier: Node): Node | undefined {
986
+ const target = definitionOf(identifier)?.node;
987
+
988
+ if (!target) {
989
+ return undefined;
990
+ }
991
+
992
+ const kind = target.kind();
993
+
994
+ if (kind === 'required_parameter' || kind === 'optional_parameter') {
995
+ return target.field('pattern') ?? undefined;
996
+ }
997
+
998
+ if (kind === 'import_specifier') {
999
+ return target.field('alias') ?? target.field('name') ?? undefined;
1000
+ }
1001
+
1002
+ return target;
1003
+ }
1004
+
1005
+ /**
1006
+ * Returns the nodes of the file that refer to a declared name: identifiers,
1007
+ * shorthand properties and export specifiers, without the declaration and
1008
+ * the import statements.
1009
+ */
1010
+ export function referencesOf(declaration: Node): Node[] {
1011
+ const file = declaration.getRoot().filename();
1012
+ const start = declaration.range().start.index;
1013
+
1014
+ return declaration
1015
+ .references()
1016
+ .filter((entry) => entry.root.filename() === file)
1017
+ .flatMap((entry) => entry.nodes)
1018
+ .filter(
1019
+ (node) =>
1020
+ node.range().start.index !== start &&
1021
+ !node
1022
+ .ancestors()
1023
+ .some((ancestor) => ancestor.kind() === 'import_statement')
1024
+ );
1025
+ }
1026
+
1027
+ /**
1028
+ * Returns the named import specifiers of `@cornerstonejs/core` in a file, each
1029
+ * with the imported name and the node of its local name.
1030
+ */
1031
+ export function coreSpecifiers(
1032
+ rootNode: Node
1033
+ ): Array<{ specifier: Node; name: string; local: Node }> {
1034
+ return rootNode
1035
+ .children()
1036
+ .filter(
1037
+ (child) =>
1038
+ child.kind() === 'import_statement' &&
1039
+ importSource(child) === CORE_PACKAGE
1040
+ )
1041
+ .flatMap((statement) =>
1042
+ statement.findAll({ rule: { kind: 'import_specifier' } })
1043
+ )
1044
+ .map((specifier) => ({
1045
+ specifier,
1046
+ name: specifier.field('name')?.text() ?? '',
1047
+ local: specifier.field('alias') ?? specifier.field('name'),
1048
+ }))
1049
+ .filter((entry) => Boolean(entry.local));
1050
+ }
1051
+
1052
+ /**
1053
+ * Returns the specifiers of the statements that re-export names of
1054
+ * `@cornerstonejs/core` (`export { name } from '@cornerstonejs/core'`), each
1055
+ * with the name that it takes from the core.
1056
+ */
1057
+ export function coreReExports(
1058
+ rootNode: Node
1059
+ ): Array<{ specifier: Node; name: string }> {
1060
+ return rootNode
1061
+ .children()
1062
+ .filter(
1063
+ (child) =>
1064
+ child.kind() === 'export_statement' &&
1065
+ importSource(child) === CORE_PACKAGE
1066
+ )
1067
+ .flatMap((statement) =>
1068
+ statement.findAll({ rule: { kind: 'export_specifier' } })
1069
+ )
1070
+ .map((specifier) => ({
1071
+ specifier,
1072
+ name: specifier.field('name')?.text() ?? '',
1073
+ }));
1074
+ }
1075
+
1076
+ /**
1077
+ * Returns the name of a key that does not change: a name, a string, or a
1078
+ * string in brackets. Returns undefined for a key computed from code.
1079
+ */
1080
+ export function staticKeyName(
1081
+ key: Node | null | undefined
1082
+ ): string | undefined {
1083
+ const kind = key?.kind() ?? '';
1084
+
1085
+ if (kind === 'string') {
1086
+ return key.text().slice(1, -1);
1087
+ }
1088
+
1089
+ if (kind === 'computed_property_name') {
1090
+ const inner = key.children().find((child) => child.isNamed());
1091
+
1092
+ return inner?.kind() === 'string' ? inner.text().slice(1, -1) : undefined;
1093
+ }
1094
+
1095
+ return /identifier/.test(kind) ? key.text() : undefined;
1096
+ }
1097
+
1098
+ /**
1099
+ * Returns the string key of a bracket access such as `object['name']`, or
1100
+ * undefined.
1101
+ */
1102
+ export function bracketKey(node: Node): string | undefined {
1103
+ const index =
1104
+ node.kind() === 'subscript_expression' ? node.field('index') : undefined;
1105
+
1106
+ return index?.kind() === 'string' ? index.text().slice(1, -1) : undefined;
1107
+ }
1108
+
1109
+ /**
1110
+ * Returns the import specifier that an identifier refers to, when it is a
1111
+ * named import of `@cornerstonejs/core` (or of a path under it when
1112
+ * `subpaths` is true), with the imported name.
1113
+ */
1114
+ export function coreImportOf(
1115
+ identifier: Node,
1116
+ subpaths = false
1117
+ ): { name: string; specifier: Node } | undefined {
1118
+ const definition = identifier.definition();
1119
+ const target = definition?.node;
1120
+ const specifier = target?.kind() === 'identifier' ? target.parent() : target;
1121
+
1122
+ if (!specifier || specifier.kind() !== 'import_specifier') {
1123
+ return undefined;
1124
+ }
1125
+
1126
+ const statement = specifier
1127
+ .ancestors()
1128
+ .find((ancestor) => ancestor.kind() === 'import_statement');
1129
+ const source = statement ? importSource(statement) : undefined;
1130
+
1131
+ if (
1132
+ !source ||
1133
+ (source !== CORE_PACKAGE &&
1134
+ !(subpaths && source.startsWith(`${CORE_PACKAGE}/`)))
1135
+ ) {
1136
+ return undefined;
1137
+ }
1138
+
1139
+ return {
1140
+ name: specifier.field('name')?.text() ?? '',
1141
+ specifier,
1142
+ };
1143
+ }
1144
+
1145
+ /**
1146
+ * Tells whether an identifier is the namespace import of `@cornerstonejs/core`.
1147
+ */
1148
+ export function isCoreNamespace(identifier: Node): boolean {
1149
+ const definition = identifier.definition();
1150
+
1151
+ if (!definition) {
1152
+ return false;
1153
+ }
1154
+
1155
+ const statement = definition.node
1156
+ .ancestors()
1157
+ .find((ancestor) => ancestor.kind() === 'import_statement');
1158
+
1159
+ return (
1160
+ Boolean(
1161
+ statement &&
1162
+ importSource(statement) === CORE_PACKAGE &&
1163
+ definition.node
1164
+ .ancestors()
1165
+ .some((ancestor) => ancestor.kind() === 'namespace_import')
1166
+ ) ||
1167
+ Boolean(
1168
+ statement &&
1169
+ importSource(statement) === CORE_PACKAGE &&
1170
+ definition.node.kind() === 'import_clause' &&
1171
+ definition.node.text().startsWith('*')
1172
+ )
1173
+ );
1174
+ }
1175
+
1176
+ /**
1177
+ * Tells whether an expression names the core export `name`: an identifier
1178
+ * bound to its import, or `namespace.name` of the core namespace.
1179
+ */
1180
+ export function isCoreExport(node: Node, name: string): boolean {
1181
+ if (node.kind() === 'identifier') {
1182
+ return coreImportOf(node)?.name === name;
1183
+ }
1184
+
1185
+ if (node.kind() === 'member_expression') {
1186
+ const object = node.field('object');
1187
+ const property = node.field('property');
1188
+
1189
+ return Boolean(
1190
+ object?.kind() === 'identifier' &&
1191
+ property?.text() === name &&
1192
+ isCoreNamespace(object)
1193
+ );
1194
+ }
1195
+
1196
+ return false;
1197
+ }
1198
+
1199
+ /**
1200
+ * Tells whether an expression names a member of the core `Enums`:
1201
+ * `Enums.member`, `cs.Enums.member`, or an identifier bound to it through
1202
+ * `const { member } = Enums` or `const member = Enums.member`.
1203
+ */
1204
+ export function isCoreEnum(node: Node, member: string): boolean {
1205
+ if (node.kind() === 'member_expression') {
1206
+ return (
1207
+ node.field('property')?.text() === member &&
1208
+ isCoreExport(node.field('object'), 'Enums')
1209
+ );
1210
+ }
1211
+
1212
+ if (node.kind() !== 'identifier') {
1213
+ return false;
1214
+ }
1215
+
1216
+ if (coreImportOf(node, true)?.name === member) {
1217
+ return true;
1218
+ }
1219
+
1220
+ const definition = definitionOf(node);
1221
+
1222
+ if (
1223
+ !definition ||
1224
+ definition.kind !== 'local' ||
1225
+ !isStableBinding(definition.node)
1226
+ ) {
1227
+ return false;
1228
+ }
1229
+
1230
+ const target = definition.node;
1231
+ const declarator = target
1232
+ .ancestors()
1233
+ .find((ancestor) => ancestor.kind() === 'variable_declarator');
1234
+ const value = declarator?.field('value');
1235
+
1236
+ if (!declarator || !value) {
1237
+ return false;
1238
+ }
1239
+
1240
+ if (declarator.field('name')?.id() === target.id()) {
1241
+ return isCoreEnum(value, member);
1242
+ }
1243
+
1244
+ const parent = target.parent();
1245
+
1246
+ if (target.kind() === 'shorthand_property_identifier_pattern') {
1247
+ return target.text() === member && isCoreExport(value, 'Enums');
1248
+ }
1249
+
1250
+ if (
1251
+ parent?.kind() === 'pair_pattern' &&
1252
+ parent.field('value')?.id() === target.id()
1253
+ ) {
1254
+ return (
1255
+ parent.field('key')?.text() === member && isCoreExport(value, 'Enums')
1256
+ );
1257
+ }
1258
+
1259
+ return false;
1260
+ }
1261
+
1262
+ /**
1263
+ * Returns the expression inside parentheses, casts and non-null assertions.
1264
+ */
1265
+ export function unwrap(node: Node): Node {
1266
+ let current = node;
1267
+
1268
+ for (;;) {
1269
+ const kind = current.kind();
1270
+
1271
+ if (kind === 'parenthesized_expression' || kind === 'non_null_expression') {
1272
+ const inner = current.children().find((child) => child.isNamed());
1273
+
1274
+ if (!inner) {
1275
+ return current;
1276
+ }
1277
+
1278
+ current = inner;
1279
+ continue;
1280
+ }
1281
+
1282
+ if (kind === 'as_expression' || kind === 'satisfies_expression') {
1283
+ current = current.children().find((child) => child.isNamed()) ?? current;
1284
+ continue;
1285
+ }
1286
+
1287
+ if (kind === 'type_assertion') {
1288
+ const named = current.children().filter((child) => child.isNamed());
1289
+
1290
+ current = named[named.length - 1] ?? current;
1291
+ continue;
1292
+ }
1293
+
1294
+ return current;
1295
+ }
1296
+ }
1297
+
1298
+ /**
1299
+ * Tells whether a type identifier names a type of the core: imported from it,
1300
+ * or read from its `Types` or its namespace.
1301
+ */
1302
+ function isCoreType(identifier: Node): boolean {
1303
+ const parent = identifier.parent();
1304
+
1305
+ if (parent?.kind() !== 'nested_type_identifier') {
1306
+ return Boolean(coreImportOf(identifier, true));
1307
+ }
1308
+
1309
+ const named = parent.children().filter((child) => child.isNamed());
1310
+ const module = named[0];
1311
+
1312
+ if (!module || named[named.length - 1]?.id() !== identifier.id()) {
1313
+ return false;
1314
+ }
1315
+
1316
+ if (module.kind() === 'identifier') {
1317
+ return (
1318
+ coreImportOf(module, true)?.name === 'Types' || isCoreNamespace(module)
1319
+ );
1320
+ }
1321
+
1322
+ const parts = module.children().filter((child) => child.isNamed());
1323
+
1324
+ return (
1325
+ module.kind() === 'nested_identifier' &&
1326
+ parts.length === 2 &&
1327
+ parts[0].kind() === 'identifier' &&
1328
+ parts[1].text() === 'Types' &&
1329
+ isCoreNamespace(parts[0])
1330
+ );
1331
+ }
1332
+
1333
+ /**
1334
+ * Returns the names of the core types that a type node names at its top: the
1335
+ * type itself, or each member of a union of core types with `null` and
1336
+ * `undefined`. A type argument of a generic type, an array type or an object
1337
+ * type names none, as the value is a container of the type, not the type.
1338
+ */
1339
+ export function topLevelCoreTypes(typeNode: Node | null | undefined): string[] {
1340
+ const kind = typeNode?.kind() ?? '';
1341
+
1342
+ if (kind === 'type_annotation' || kind === 'parenthesized_type') {
1343
+ return topLevelCoreTypes(
1344
+ typeNode.children().find((child) => child.isNamed())
1345
+ );
1346
+ }
1347
+
1348
+ if (kind === 'type_identifier') {
1349
+ return isCoreType(typeNode) ? [typeNode.text()] : [];
1350
+ }
1351
+
1352
+ if (kind === 'nested_type_identifier') {
1353
+ const named = typeNode.children().filter((child) => child.isNamed());
1354
+ const last = named[named.length - 1];
1355
+
1356
+ return last?.kind() === 'type_identifier' && isCoreType(last)
1357
+ ? [last.text()]
1358
+ : [];
1359
+ }
1360
+
1361
+ if (kind !== 'union_type') {
1362
+ return [];
1363
+ }
1364
+
1365
+ const members = typeNode
1366
+ .children()
1367
+ .filter(
1368
+ (child) => child.isNamed() && !/^(null|undefined)$/.test(child.text())
1369
+ );
1370
+ const names = members.map((member) => topLevelCoreTypes(member));
1371
+
1372
+ return names.every((found) => found.length) ? names.flat() : [];
1373
+ }
1374
+
1375
+ /**
1376
+ * Returns the names of the core types that a cast puts on an expression at
1377
+ * its top, without following variables.
1378
+ */
1379
+ function castCoreTypes(node: Node | null | undefined): string[] {
1380
+ const names: string[] = [];
1381
+ let current: Node | null | undefined = node;
1382
+
1383
+ while (current) {
1384
+ const kind = current.kind();
1385
+
1386
+ if (
1387
+ kind === 'as_expression' ||
1388
+ kind === 'satisfies_expression' ||
1389
+ kind === 'type_assertion'
1390
+ ) {
1391
+ const typeNode = current
1392
+ .children()
1393
+ .find((child) => child.isNamed() && /type/.test(child.kind()));
1394
+ const typed =
1395
+ typeNode?.kind() === 'type_arguments'
1396
+ ? typeNode.children().find((child) => child.isNamed())
1397
+ : typeNode;
1398
+
1399
+ names.push(...topLevelCoreTypes(typed));
1400
+ }
1401
+
1402
+ if (
1403
+ kind === 'parenthesized_expression' ||
1404
+ kind === 'non_null_expression' ||
1405
+ kind === 'as_expression' ||
1406
+ kind === 'satisfies_expression'
1407
+ ) {
1408
+ current = current.children().find((child) => child.isNamed()) ?? null;
1409
+ continue;
1410
+ }
1411
+
1412
+ if (kind === 'type_assertion') {
1413
+ const named = current.children().filter((child) => child.isNamed());
1414
+
1415
+ current = named[named.length - 1] ?? null;
1416
+ continue;
1417
+ }
1418
+
1419
+ current = null;
1420
+ }
1421
+
1422
+ return names;
1423
+ }
1424
+
1425
+ /**
1426
+ * Returns the node that holds a declared name through its destructuring
1427
+ * patterns: a declarator, a parameter, an import specifier.
1428
+ */
1429
+ function bindingHolder(declaration: Node): Node | undefined {
1430
+ let parent = declaration.parent();
1431
+
1432
+ while (parent && PATTERN_KINDS.has(parent.kind())) {
1433
+ parent = parent.parent();
1434
+ }
1435
+
1436
+ return parent ?? undefined;
1437
+ }
1438
+
1439
+ /**
1440
+ * Tells whether a reference to a name writes it: the left side of `=`, of an
1441
+ * operator assignment or of a `for in`/`for of` without a declaration, a
1442
+ * destructuring target of those, or `++` and `--`.
1443
+ */
1444
+ function isWrite(reference: Node): boolean {
1445
+ let child = reference;
1446
+ let parent = reference.parent();
1447
+
1448
+ while (
1449
+ parent &&
1450
+ (PATTERN_KINDS.has(parent.kind()) ||
1451
+ parent.kind() === 'parenthesized_expression')
1452
+ ) {
1453
+ child = parent;
1454
+ parent = parent.parent();
1455
+ }
1456
+
1457
+ const kind = parent?.kind() ?? '';
1458
+
1459
+ if (kind === 'update_expression') {
1460
+ return true;
1461
+ }
1462
+
1463
+ return (
1464
+ (kind === 'assignment_expression' ||
1465
+ kind === 'augmented_assignment_expression' ||
1466
+ kind === 'for_in_statement') &&
1467
+ parent.field('left')?.id() === child.id()
1468
+ );
1469
+ }
1470
+
1471
+ /**
1472
+ * Tells whether a declared name holds one value for its whole scope: a
1473
+ * `const`, an import, or a parameter that the file never writes. A `let`, a
1474
+ * `var` or a written parameter can hold another value at a later node.
1475
+ */
1476
+ export function isStableBinding(declaration: Node): boolean {
1477
+ const holder = bindingHolder(declaration);
1478
+ const kind = holder?.kind() ?? '';
1479
+
1480
+ if (kind === 'variable_declarator') {
1481
+ const statement = holder.parent();
1482
+
1483
+ return (
1484
+ statement?.kind() === 'lexical_declaration' &&
1485
+ statement.children()[0]?.text() === 'const'
1486
+ );
1487
+ }
1488
+
1489
+ if (
1490
+ kind === 'import_specifier' ||
1491
+ kind === 'import_clause' ||
1492
+ kind === 'namespace_import'
1493
+ ) {
1494
+ return true;
1495
+ }
1496
+
1497
+ if (
1498
+ kind === 'required_parameter' ||
1499
+ kind === 'optional_parameter' ||
1500
+ kind === 'formal_parameters' ||
1501
+ kind === 'arrow_function'
1502
+ ) {
1503
+ return !referencesOf(declaration).some(isWrite);
1504
+ }
1505
+
1506
+ return false;
1507
+ }
1508
+
1509
+ /**
1510
+ * Returns the declaration of the binding that an identifier refers to when
1511
+ * that binding holds one value for its whole scope, or undefined.
1512
+ */
1513
+ export function stableBinding(identifier: Node): Node | undefined {
1514
+ const declaration = declarationNodeOf(identifier);
1515
+
1516
+ return declaration && isStableBinding(declaration) ? declaration : undefined;
1517
+ }
1518
+
1519
+ /**
1520
+ * Tells whether an expression is a name that refers to a declaration.
1521
+ */
1522
+ export function isBoundTo(node: Node, declaration: Node): boolean {
1523
+ const kind = node.kind();
1524
+
1525
+ return (
1526
+ (kind === 'identifier' || kind === 'shorthand_property_identifier') &&
1527
+ declarationNodeOf(node)?.range().start.index ===
1528
+ declaration.range().start.index
1529
+ );
1530
+ }
1531
+
1532
+ /**
1533
+ * Returns what a declaration gives to the binding that an identifier refers
1534
+ * to, when that binding holds one value for its whole scope: the core types
1535
+ * at the top of its annotation, the value it starts with, and for a
1536
+ * destructured name its key and the object it comes from. A `let`, a `var`,
1537
+ * a written parameter and an assignment give nothing.
1538
+ */
1539
+ function declarationOf(identifier: Node):
1540
+ | {
1541
+ typeNames: string[];
1542
+ value: Node | undefined;
1543
+ key?: string;
1544
+ source?: Node;
1545
+ }
1546
+ | undefined {
1547
+ const definition = definitionOf(identifier);
1548
+
1549
+ if (!definition || definition.kind !== 'local') {
1550
+ return undefined;
1551
+ }
1552
+
1553
+ const target = definition.node;
1554
+
1555
+ if (
1556
+ target.kind() === 'required_parameter' ||
1557
+ target.kind() === 'optional_parameter'
1558
+ ) {
1559
+ const pattern = target.field('pattern');
1560
+ const annotation = target
1561
+ .children()
1562
+ .find((child) => child.kind() === 'type_annotation');
1563
+
1564
+ if (pattern?.kind() !== 'identifier' || !isStableBinding(pattern)) {
1565
+ return undefined;
1566
+ }
1567
+
1568
+ return {
1569
+ typeNames: topLevelCoreTypes(annotation),
1570
+ value: undefined,
1571
+ };
1572
+ }
1573
+
1574
+ if (!isStableBinding(target)) {
1575
+ return undefined;
1576
+ }
1577
+
1578
+ const parent = target.parent();
1579
+
1580
+ if (
1581
+ target.kind() === 'shorthand_property_identifier_pattern' ||
1582
+ (parent?.kind() === 'pair_pattern' &&
1583
+ parent.field('value')?.id() === target.id())
1584
+ ) {
1585
+ const pattern = target.kind() === 'pair_pattern' ? target : parent;
1586
+ const objectPattern =
1587
+ pattern?.kind() === 'pair_pattern' ? pattern.parent() : pattern;
1588
+ const declarator = objectPattern?.parent();
1589
+
1590
+ if (
1591
+ objectPattern?.kind() !== 'object_pattern' ||
1592
+ declarator?.kind() !== 'variable_declarator' ||
1593
+ declarator.field('name')?.id() !== objectPattern.id()
1594
+ ) {
1595
+ return undefined;
1596
+ }
1597
+
1598
+ return {
1599
+ typeNames: [],
1600
+ value: undefined,
1601
+ key:
1602
+ target.kind() === 'shorthand_property_identifier_pattern'
1603
+ ? target.text()
1604
+ : parent.field('key')?.text(),
1605
+ source: declarator.field('value') ?? undefined,
1606
+ };
1607
+ }
1608
+
1609
+ if (
1610
+ parent?.kind() !== 'variable_declarator' ||
1611
+ parent.field('name')?.id() !== target.id()
1612
+ ) {
1613
+ return undefined;
1614
+ }
1615
+
1616
+ const annotation = parent
1617
+ .children()
1618
+ .find((child) => child.kind() === 'type_annotation');
1619
+
1620
+ return {
1621
+ typeNames: topLevelCoreTypes(annotation),
1622
+ value: parent.field('value') ?? undefined,
1623
+ };
1624
+ }
1625
+
1626
+ /**
1627
+ * Tells whether a type name of the core is the type of a viewport.
1628
+ */
1629
+ function isViewportTypeName(name: string): boolean {
1630
+ return Boolean(FAMILY_OF_TYPE[name]) || ANY_VIEWPORT_TYPES.has(name);
1631
+ }
1632
+
1633
+ /**
1634
+ * Tells whether an expression is an enabled element of the core: a call of
1635
+ * `getEnabledElement` and its kin, or a variable that holds one or is typed
1636
+ * `Types.IEnabledElement`.
1637
+ */
1638
+ function isEnabledElement(
1639
+ node: Node | null | undefined,
1640
+ depth: number
1641
+ ): boolean {
1642
+ if (!node || depth > 4) {
1643
+ return false;
1644
+ }
1645
+
1646
+ if (castCoreTypes(node).includes('IEnabledElement')) {
1647
+ return true;
1648
+ }
1649
+
1650
+ const inner = unwrap(node);
1651
+
1652
+ if (inner.kind() === 'call_expression') {
1653
+ const callee = inner.field('function');
1654
+
1655
+ return Boolean(
1656
+ callee &&
1657
+ ENABLED_ELEMENT_GETTERS.some((name) => isCoreExport(callee, name))
1658
+ );
1659
+ }
1660
+
1661
+ if (inner.kind() !== 'identifier') {
1662
+ return false;
1663
+ }
1664
+
1665
+ const declaration = declarationOf(inner);
1666
+
1667
+ return Boolean(
1668
+ declaration &&
1669
+ (declaration.typeNames.includes('IEnabledElement') ||
1670
+ (declaration.value && isEnabledElement(declaration.value, depth + 1)))
1671
+ );
1672
+ }
1673
+
1674
+ /**
1675
+ * Tells whether an expression is a rendering engine of the core: made with
1676
+ * `new RenderingEngine`, got with `getRenderingEngine`, read from an enabled
1677
+ * element, or a `const` or a parameter that the file never writes that holds
1678
+ * one or is typed as one.
1679
+ */
1680
+ export function isRenderingEngine(
1681
+ node: Node | null | undefined,
1682
+ depth = 0
1683
+ ): boolean {
1684
+ if (!node || depth > 4) {
1685
+ return false;
1686
+ }
1687
+
1688
+ if (castCoreTypes(node).some((name) => ENGINE_TYPES.includes(name))) {
1689
+ return true;
1690
+ }
1691
+
1692
+ const inner = unwrap(node);
1693
+ const kind = inner.kind();
1694
+
1695
+ if (kind === 'new_expression') {
1696
+ const constructor = inner.field('constructor');
1697
+
1698
+ return Boolean(
1699
+ constructor &&
1700
+ ENGINE_TYPES.some((name) => isCoreExport(constructor, name))
1701
+ );
1702
+ }
1703
+
1704
+ if (kind === 'call_expression') {
1705
+ const callee = inner.field('function');
1706
+
1707
+ if (!callee) {
1708
+ return false;
1709
+ }
1710
+
1711
+ return (
1712
+ isCoreExport(callee, 'getRenderingEngine') ||
1713
+ (callee.kind() === 'member_expression' &&
1714
+ callee.field('property')?.text() === 'getRenderingEngine' &&
1715
+ viewportEvidence(callee.field('object'), depth + 1) === 'core')
1716
+ );
1717
+ }
1718
+
1719
+ if (kind === 'member_expression') {
1720
+ return (
1721
+ inner.field('property')?.text() === 'renderingEngine' &&
1722
+ isEnabledElement(inner.field('object'), depth + 1)
1723
+ );
1724
+ }
1725
+
1726
+ if (kind !== 'identifier') {
1727
+ return false;
1728
+ }
1729
+
1730
+ const declaration = declarationOf(inner);
1731
+
1732
+ if (!declaration) {
1733
+ return false;
1734
+ }
1735
+
1736
+ if (declaration.typeNames.some((name) => ENGINE_TYPES.includes(name))) {
1737
+ return true;
1738
+ }
1739
+
1740
+ if (declaration.key === 'renderingEngine') {
1741
+ return isEnabledElement(declaration.source, depth + 1);
1742
+ }
1743
+
1744
+ return isRenderingEngine(declaration.value, depth + 1);
1745
+ }
1746
+
1747
+ /**
1748
+ * Tells whether an expression is the list of the viewports of a rendering
1749
+ * engine: `engine.getViewports()`, or a variable that holds it.
1750
+ */
1751
+ function isViewportList(node: Node | null | undefined, depth: number): boolean {
1752
+ if (!node || depth > 4) {
1753
+ return false;
1754
+ }
1755
+
1756
+ const inner = unwrap(node);
1757
+
1758
+ if (inner.kind() === 'call_expression') {
1759
+ const callee = inner.field('function');
1760
+
1761
+ return Boolean(
1762
+ callee?.kind() === 'member_expression' &&
1763
+ callee.field('property')?.text() === 'getViewports' &&
1764
+ isRenderingEngine(callee.field('object'), depth + 1)
1765
+ );
1766
+ }
1767
+
1768
+ if (inner.kind() !== 'identifier') {
1769
+ return false;
1770
+ }
1771
+
1772
+ return isViewportList(declarationOf(inner)?.value, depth + 1);
1773
+ }
1774
+
1775
+ /**
1776
+ * Returns what shows that an expression is a Cornerstone viewport: `core` for
1777
+ * a cast or an annotation whose top type is a core viewport type, a call of
1778
+ * `getViewport` on a rendering engine of the core, the `viewport` of an
1779
+ * enabled element, an entry of `engine.getViewports()`, or a `const` or a
1780
+ * parameter that the file never writes that holds one of these; `name` for a
1781
+ * name such as `viewport` or `ctViewport` alone (a `let`, a `var` or a
1782
+ * written parameter among them), or `getViewport` on something else;
1783
+ * undefined otherwise. A type argument of another type is no evidence.
1784
+ */
1785
+ export function viewportEvidence(
1786
+ node: Node | null | undefined,
1787
+ depth = 0
1788
+ ): ViewportEvidence | undefined {
1789
+ if (!node || depth > 4) {
1790
+ return undefined;
1791
+ }
1792
+
1793
+ if (castCoreTypes(node).some(isViewportTypeName)) {
1794
+ return 'core';
1795
+ }
1796
+
1797
+ const inner = unwrap(node);
1798
+ const kind = inner.kind();
1799
+
1800
+ if (kind === 'identifier') {
1801
+ const declaration = declarationOf(inner);
1802
+ const fromValue = declaration?.value
1803
+ ? viewportEvidence(declaration.value, depth + 1)
1804
+ : undefined;
1805
+ const isCore =
1806
+ fromValue === 'core' ||
1807
+ Boolean(declaration?.typeNames.some(isViewportTypeName)) ||
1808
+ (declaration?.key === 'viewport' &&
1809
+ isEnabledElement(declaration.source, depth + 1));
1810
+
1811
+ if (isCore) {
1812
+ return 'core';
1813
+ }
1814
+
1815
+ return VIEWPORT_NAME.test(inner.text()) || fromValue === 'name'
1816
+ ? 'name'
1817
+ : undefined;
1818
+ }
1819
+
1820
+ if (kind === 'member_expression') {
1821
+ const property = inner.field('property')?.text() ?? '';
1822
+
1823
+ if (
1824
+ property === 'viewport' &&
1825
+ isEnabledElement(inner.field('object'), depth + 1)
1826
+ ) {
1827
+ return 'core';
1828
+ }
1829
+
1830
+ return VIEWPORT_NAME.test(property) ? 'name' : undefined;
1831
+ }
1832
+
1833
+ if (kind === 'subscript_expression') {
1834
+ const object = inner.field('object');
1835
+
1836
+ if (isViewportList(object, depth + 1)) {
1837
+ return 'core';
1838
+ }
1839
+
1840
+ return object &&
1841
+ VIEWPORTS_NAME.test(unwrap(object).text().split('.').pop() ?? '')
1842
+ ? 'name'
1843
+ : undefined;
1844
+ }
1845
+
1846
+ if (kind === 'call_expression') {
1847
+ const callee = inner.field('function');
1848
+
1849
+ if (
1850
+ callee?.kind() !== 'member_expression' ||
1851
+ callee.field('property')?.text() !== 'getViewport'
1852
+ ) {
1853
+ return undefined;
1854
+ }
1855
+
1856
+ return isRenderingEngine(callee.field('object'), depth + 1)
1857
+ ? 'core'
1858
+ : 'name';
1859
+ }
1860
+
1861
+ return undefined;
1862
+ }
1863
+
1864
+ /**
1865
+ * Returns the family that core type names give together, when they give one.
1866
+ */
1867
+ function familyOfTypes(names: string[]): ViewportFamily | undefined {
1868
+ const families = new Set(
1869
+ names.map((name) => FAMILY_OF_TYPE[name]).filter(Boolean)
1870
+ );
1871
+
1872
+ return families.size === 1 ? [...families][0] : undefined;
1873
+ }
1874
+
1875
+ /**
1876
+ * Returns the family of a viewport expression when the top type of a cast or
1877
+ * an annotation names it, or the type argument of `getViewport` on a core
1878
+ * rendering engine, or undefined.
1879
+ */
1880
+ export function viewportFamily(
1881
+ node: Node,
1882
+ depth = 0
1883
+ ): ViewportFamily | undefined {
1884
+ if (depth > 4) {
1885
+ return undefined;
1886
+ }
1887
+
1888
+ const fromCast = familyOfTypes(castCoreTypes(node));
1889
+
1890
+ if (fromCast) {
1891
+ return fromCast;
1892
+ }
1893
+
1894
+ const inner = unwrap(node);
1895
+ const call = methodCall(inner);
1896
+ const typeArguments = call ? inner.field('type_arguments') : undefined;
1897
+
1898
+ if (
1899
+ call?.method === 'getViewport' &&
1900
+ typeArguments &&
1901
+ isRenderingEngine(call.object, depth + 1)
1902
+ ) {
1903
+ const argument = typeArguments
1904
+ .children()
1905
+ .filter((child) => child.isNamed());
1906
+
1907
+ return argument.length === 1
1908
+ ? familyOfTypes(topLevelCoreTypes(argument[0]))
1909
+ : undefined;
1910
+ }
1911
+
1912
+ if (inner.kind() !== 'identifier') {
1913
+ return undefined;
1914
+ }
1915
+
1916
+ const declaration = declarationOf(inner);
1917
+
1918
+ if (!declaration) {
1919
+ return undefined;
1920
+ }
1921
+
1922
+ const fromType = familyOfTypes(declaration.typeNames);
1923
+
1924
+ if (fromType) {
1925
+ return fromType;
1926
+ }
1927
+
1928
+ return declaration.value
1929
+ ? viewportFamily(declaration.value, depth + 1)
1930
+ : undefined;
1931
+ }
1932
+
1933
+ /**
1934
+ * Tells whether an expression can be written twice without a change of
1935
+ * meaning: a name, `this`, or a chain of property reads of those.
1936
+ */
1937
+ export function isRepeatable(node: Node): boolean {
1938
+ const kind = node.kind();
1939
+
1940
+ if (kind === 'identifier' || kind === 'this') {
1941
+ return true;
1942
+ }
1943
+
1944
+ if (kind === 'member_expression') {
1945
+ return (
1946
+ node.field('property')?.kind() === 'property_identifier' &&
1947
+ isRepeatable(node.field('object'))
1948
+ );
1949
+ }
1950
+
1951
+ if (
1952
+ kind === 'parenthesized_expression' ||
1953
+ kind === 'non_null_expression' ||
1954
+ kind === 'as_expression' ||
1955
+ kind === 'type_assertion'
1956
+ ) {
1957
+ return isRepeatable(unwrap(node));
1958
+ }
1959
+
1960
+ return false;
1961
+ }
1962
+
1963
+ /**
1964
+ * Tells whether an expression runs no code of its own: a literal, a
1965
+ * repeatable expression, an index of those, or an array or a template of
1966
+ * those.
1967
+ */
1968
+ export function isPure(node: Node): boolean {
1969
+ const kind = node.kind();
1970
+
1971
+ if (LITERAL_KINDS.has(kind)) {
1972
+ return true;
1973
+ }
1974
+
1975
+ if (kind === 'template_string') {
1976
+ return node
1977
+ .children()
1978
+ .filter((child) => child.kind() === 'template_substitution')
1979
+ .every((substitution) => {
1980
+ const inner = substitution.children().find((child) => child.isNamed());
1981
+
1982
+ return Boolean(inner && isPure(inner));
1983
+ });
1984
+ }
1985
+
1986
+ if (kind === 'array') {
1987
+ return node
1988
+ .children()
1989
+ .filter((child) => child.isNamed() && child.kind() !== 'comment')
1990
+ .every((child) => isPure(child));
1991
+ }
1992
+
1993
+ if (kind === 'subscript_expression') {
1994
+ const object = node.field('object');
1995
+ const index = node.field('index');
1996
+
1997
+ return Boolean(object && index && isPure(object) && isPure(index));
1998
+ }
1999
+
2000
+ return isRepeatable(node);
2001
+ }
2002
+
2003
+ /**
2004
+ * Tells whether an expression gives the same value when it is read twice in
2005
+ * a row: a name, a literal, a template without substitutions, or an array
2006
+ * literal of those. A property read, a call, a spread or a conditional can
2007
+ * give another value or run code, so a rewrite reads it once into a `const`.
2008
+ */
2009
+ export function isPlainValue(node: Node): boolean {
2010
+ const kind = node.kind();
2011
+
2012
+ if (kind === 'identifier' || LITERAL_KINDS.has(kind)) {
2013
+ return true;
2014
+ }
2015
+
2016
+ if (kind === 'template_string') {
2017
+ return !node
2018
+ .children()
2019
+ .some((child) => child.kind() === 'template_substitution');
2020
+ }
2021
+
2022
+ return (
2023
+ kind === 'array' &&
2024
+ node
2025
+ .children()
2026
+ .filter((child) => child.isNamed() && child.kind() !== 'comment')
2027
+ .every((child) => isPlainValue(child))
2028
+ );
2029
+ }
2030
+
2031
+ /**
2032
+ * Returns the text of an expression, in parentheses when a member access
2033
+ * after it would bind to a part of it.
2034
+ */
2035
+ export function asPrimary(node: Node): string {
2036
+ return PRIMARY_KINDS.has(node.kind()) ? node.text() : `(${node.text()})`;
2037
+ }
2038
+
2039
+ /**
2040
+ * Returns the text of a comparison that replaces a node, in parentheses
2041
+ * unless the parent of the node keeps it whole.
2042
+ */
2043
+ export function comparisonAt(node: Node, text: string): string {
2044
+ const parent = node.parent();
2045
+ const kind = parent?.kind() ?? '';
2046
+ const loose =
2047
+ LOOSE_PARENTS.has(kind) ||
2048
+ (kind === 'binary_expression' &&
2049
+ /^(&&|\|\||\?\?)$/.test(parent.field('operator')?.text() ?? ''));
2050
+
2051
+ return loose ? text : `(${text})`;
2052
+ }
2053
+
2054
+ /**
2055
+ * Tells whether a call has an optional link: `a?.b()`, `a.b?.()` or an
2056
+ * optional chain in its receiver.
2057
+ */
2058
+ export function isOptionalCall(call: Node): boolean {
2059
+ const callee = call.field('function');
2060
+
2061
+ return (
2062
+ call
2063
+ .children()
2064
+ .some(
2065
+ (child) => child.kind() === 'optional_chain' || child.text() === '?.'
2066
+ ) || Boolean(callee?.find({ rule: { kind: 'optional_chain' } }))
2067
+ );
2068
+ }
2069
+
2070
+ /**
2071
+ * Returns the parts of a method call `object.method(args)`, or undefined.
2072
+ */
2073
+ export function methodCall(call: Node):
2074
+ | {
2075
+ object: Node;
2076
+ property: Node;
2077
+ method: string;
2078
+ args: Node[];
2079
+ argsNode: Node;
2080
+ }
2081
+ | undefined {
2082
+ if (call.kind() !== 'call_expression') {
2083
+ return undefined;
2084
+ }
2085
+
2086
+ const callee = call.field('function');
2087
+ const argsNode = call.field('arguments');
2088
+
2089
+ if (!callee || callee.kind() !== 'member_expression' || !argsNode) {
2090
+ return undefined;
2091
+ }
2092
+
2093
+ const object = callee.field('object');
2094
+ const property = callee.field('property');
2095
+
2096
+ if (!object || !property) {
2097
+ return undefined;
2098
+ }
2099
+
2100
+ return {
2101
+ object,
2102
+ property,
2103
+ method: property.text(),
2104
+ args: argsNode
2105
+ .children()
2106
+ .filter((child) => child.isNamed() && child.kind() !== 'comment'),
2107
+ argsNode,
2108
+ };
2109
+ }
2110
+
2111
+ /**
2112
+ * Returns the nearest function that contains a node, or undefined at the top
2113
+ * of the module.
2114
+ */
2115
+ export function enclosingFunction(node: Node): Node | undefined {
2116
+ return node
2117
+ .ancestors()
2118
+ .find((ancestor) => FUNCTION_KINDS.has(ancestor.kind()));
2119
+ }
2120
+
2121
+ /**
2122
+ * Tells whether a statement awaits a mount call on a receiver that refers to
2123
+ * a declaration: `await viewport.setStack(...)` and its kin.
2124
+ */
2125
+ function isAwaitedMount(statement: Node, binding: Node): boolean {
2126
+ const expression =
2127
+ statement.kind() === 'expression_statement'
2128
+ ? statement.children().find((child) => child.isNamed())
2129
+ : undefined;
2130
+ const call =
2131
+ expression?.kind() === 'await_expression'
2132
+ ? expression.children().find((child) => child.isNamed())
2133
+ : undefined;
2134
+ const parts = call ? methodCall(call) : undefined;
2135
+
2136
+ return Boolean(
2137
+ parts &&
2138
+ MOUNT_METHODS.has(parts.method) &&
2139
+ isBoundTo(unwrap(parts.object), binding)
2140
+ );
2141
+ }
2142
+
2143
+ /**
2144
+ * Returns the statements that run before a node on every path to it in its
2145
+ * function: the statements before it in each block of the function that
2146
+ * holds it.
2147
+ */
2148
+ export function statementsBefore(node: Node): Node[] {
2149
+ const fn = enclosingFunction(node);
2150
+ const start = node.range().start.index;
2151
+
2152
+ return node
2153
+ .ancestors()
2154
+ .filter(
2155
+ (ancestor) =>
2156
+ (ancestor.kind() === 'statement_block' ||
2157
+ ancestor.kind() === 'program') &&
2158
+ (!fn || contains(fn, ancestor))
2159
+ )
2160
+ .flatMap((block) =>
2161
+ block
2162
+ .children()
2163
+ .filter((statement) => statement.range().end.index <= start)
2164
+ );
2165
+ }
2166
+
2167
+ /**
2168
+ * Tells whether a viewport shows data at a node, so that its content mode
2169
+ * answers what its 5.x type or class did: the receiver is a name whose
2170
+ * binding holds one value, and a statement that awaits a mount call on the
2171
+ * same binding runs before the node in its function. Before its first mount
2172
+ * a planar viewport is in the mode `empty`.
2173
+ */
2174
+ export function isMountedBefore(node: Node, receiver: Node): boolean {
2175
+ const target = unwrap(receiver);
2176
+ const binding =
2177
+ target.kind() === 'identifier' ? stableBinding(target) : undefined;
2178
+
2179
+ return Boolean(
2180
+ binding &&
2181
+ statementsBefore(node).some((statement) =>
2182
+ isAwaitedMount(statement, binding)
2183
+ )
2184
+ );
2185
+ }
2186
+
2187
+ /**
2188
+ * Tells whether a function node is declared `async`.
2189
+ */
2190
+ export function isAsyncFunction(fn: Node | undefined): boolean {
2191
+ return Boolean(fn?.children().some((child) => child.kind() === 'async'));
2192
+ }
2193
+
2194
+ /**
2195
+ * Returns the expression statement whose expression is the call, or the call
2196
+ * under `await`, with whether it awaits.
2197
+ */
2198
+ export function statementOfCall(
2199
+ call: Node
2200
+ ): { statement: Node; awaited: boolean } | undefined {
2201
+ const parent = call.parent();
2202
+
2203
+ if (parent?.kind() === 'expression_statement') {
2204
+ return { statement: parent, awaited: false };
2205
+ }
2206
+
2207
+ if (
2208
+ parent?.kind() === 'await_expression' &&
2209
+ parent.parent()?.kind() === 'expression_statement'
2210
+ ) {
2211
+ return { statement: parent.parent(), awaited: true };
2212
+ }
2213
+
2214
+ return undefined;
2215
+ }
2216
+
2217
+ /**
2218
+ * Returns the white space before a node on its line.
2219
+ */
2220
+ export function indentOf(rootNode: Node, node: Node): string {
2221
+ const source = rootNode.text();
2222
+ const start = node.range().start.index;
2223
+ const lineStart = source.lastIndexOf('\n', start - 1) + 1;
2224
+ const prefix = source.slice(lineStart, start);
2225
+
2226
+ return /^\s*$/.test(prefix) ? prefix : (prefix.match(/^\s*/)?.[0] ?? '');
2227
+ }
2228
+
2229
+ /**
2230
+ * Returns an edit that replaces a statement with other statements. They go
2231
+ * in a block when the statement is the single-statement body of an `if`, an
2232
+ * `else`, a loop, a label or a `case`. Each entry of `build` is one
2233
+ * statement whose later lines carry the indent it is given; an empty entry
2234
+ * is a blank line.
2235
+ */
2236
+ export function replaceStatement(
2237
+ rootNode: Node,
2238
+ statement: Node,
2239
+ build: (indent: string) => string[]
2240
+ ): Edit {
2241
+ const indent = indentOf(rootNode, statement);
2242
+ const parentKind = statement.parent()?.kind() ?? '';
2243
+ const inBlock = parentKind === 'statement_block' || parentKind === 'program';
2244
+ const inner = inBlock ? indent : `${indent} `;
2245
+ const parts = build(inner);
2246
+ const body = parts
2247
+ .map((part, index) => (index === 0 || !part ? part : `${inner}${part}`))
2248
+ .join('\n');
2249
+
2250
+ return statement.replace(inBlock ? body : `{\n${inner}${body}\n${indent}}`);
2251
+ }
2252
+
2253
+ /**
2254
+ * Returns the properties of an object literal by key, with the shorthand and
2255
+ * spread entries.
2256
+ */
2257
+ export function objectEntries(objectNode: Node): {
2258
+ pairs: Map<string, Node>;
2259
+ shorthand: Set<string>;
2260
+ hasSpread: boolean;
2261
+ keys: string[];
2262
+ } {
2263
+ const pairs = new Map<string, Node>();
2264
+ const shorthand = new Set<string>();
2265
+ const keys: string[] = [];
2266
+ let hasSpread = false;
2267
+
2268
+ for (const child of objectNode.children()) {
2269
+ if (child.kind() === 'pair') {
2270
+ const key = child.field('key');
2271
+ const name =
2272
+ key?.kind() === 'string'
2273
+ ? key.text().slice(1, -1)
2274
+ : (key?.text() ?? '');
2275
+
2276
+ pairs.set(name, child);
2277
+ keys.push(name);
2278
+ } else if (child.kind() === 'shorthand_property_identifier') {
2279
+ shorthand.add(child.text());
2280
+ keys.push(child.text());
2281
+ } else if (
2282
+ child.kind() === 'spread_element' ||
2283
+ child.kind() === 'method_definition'
2284
+ ) {
2285
+ hasSpread = true;
2286
+ }
2287
+ }
2288
+
2289
+ return { pairs, shorthand, hasSpread, keys };
2290
+ }
2291
+
2292
+ /**
2293
+ * Returns an edit that removes one property of an object literal, or one
2294
+ * specifier of a list, with its comma.
2295
+ */
2296
+ export function removeProperty(property: Node): Edit {
2297
+ const next = property.next();
2298
+ const prev = property.prev();
2299
+ const start = property.range().start.index;
2300
+
2301
+ if (next?.kind() === ',') {
2302
+ const after = next.next();
2303
+ const end = after ? after.range().start.index : next.range().end.index;
2304
+
2305
+ return { startPos: start, endPos: end, insertedText: '' };
2306
+ }
2307
+
2308
+ if (prev?.kind() === ',') {
2309
+ return {
2310
+ startPos: prev.range().start.index,
2311
+ endPos: property.range().end.index,
2312
+ insertedText: '',
2313
+ };
2314
+ }
2315
+
2316
+ return {
2317
+ startPos: start,
2318
+ endPos: property.range().end.index,
2319
+ insertedText: '',
2320
+ };
2321
+ }
2322
+
2323
+ /**
2324
+ * The cardinality of a reported site: the rule, the file and the line.
2325
+ */
2326
+ export function site(
2327
+ root: SgRoot<TSX>,
2328
+ rule: string,
2329
+ node: Node
2330
+ ): Record<string, string> {
2331
+ return {
2332
+ rule,
2333
+ file: root.relativeFilename?.() || root.filename(),
2334
+ line: String(node.range().start.line + 1),
2335
+ };
2336
+ }
2337
+
2338
+ /**
2339
+ * Keeps edits that do not overlap, so a transform can skip a site that
2340
+ * another rewrite of the same run touches.
2341
+ */
2342
+ export class EditSet {
2343
+ private readonly list: Edit[] = [];
2344
+
2345
+ /**
2346
+ * Adds the edits when none of them overlaps an edit already kept.
2347
+ */
2348
+ add(...edits: Edit[]): boolean {
2349
+ for (const edit of edits) {
2350
+ for (const kept of this.list) {
2351
+ const overlaps =
2352
+ edit.startPos < kept.endPos && kept.startPos < edit.endPos;
2353
+ const sameInsert =
2354
+ edit.startPos === edit.endPos &&
2355
+ kept.startPos === kept.endPos &&
2356
+ edit.startPos === kept.startPos;
2357
+
2358
+ if (overlaps || sameInsert) {
2359
+ return false;
2360
+ }
2361
+ }
2362
+ }
2363
+
2364
+ this.list.push(...edits);
2365
+
2366
+ return true;
2367
+ }
2368
+
2369
+ /**
2370
+ * Tells whether a node lies inside an edit already kept.
2371
+ */
2372
+ covers(node: Node): boolean {
2373
+ const { start, end } = node.range();
2374
+
2375
+ return this.list.some(
2376
+ (kept) => start.index < kept.endPos && kept.startPos < end.index
2377
+ );
2378
+ }
2379
+
2380
+ /**
2381
+ * Returns the kept edits.
2382
+ */
2383
+ all(): Edit[] {
2384
+ return this.list;
2385
+ }
2386
+ }