@cornerstonejs/codemods 6.0.0-beta.0 → 6.0.0-beta.2

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 (79) hide show
  1. package/.oxlintrc.json +4 -0
  2. package/package.json +1 -1
  3. package/registry/cornerstone3d/6/README.md +46 -0
  4. package/registry/cornerstone3d/6/classes-and-types/README.md +16 -0
  5. package/registry/cornerstone3d/6/classes-and-types/scripts/classes-and-types.ts +32 -90
  6. package/registry/cornerstone3d/6/classes-and-types/scripts/lib.ts +864 -16
  7. package/registry/cornerstone3d/6/classes-and-types/tests/tsx/jsx-in-a-js-file/expected.js +31 -0
  8. package/registry/cornerstone3d/6/classes-and-types/tests/tsx/jsx-in-a-js-file/input.js +31 -0
  9. package/registry/cornerstone3d/6/classes-and-types/tests/tsx/jsx-in-a-js-file/metrics.json +46 -0
  10. package/registry/cornerstone3d/6/classes-and-types/tests/tsx/not-analysed-a-name-declared-twice/expected.tsx +34 -0
  11. package/registry/cornerstone3d/6/classes-and-types/tests/tsx/not-analysed-a-name-declared-twice/input.tsx +34 -0
  12. package/registry/cornerstone3d/6/classes-and-types/tests/tsx/not-analysed-a-name-declared-twice/metrics.json +12 -0
  13. package/registry/cornerstone3d/6/classes-and-types/tests/typescript/positive-classes/metrics.json +10 -0
  14. package/registry/cornerstone3d/6/classes-and-types/tests/typescript/renamed-classes-make-two-operands-one/expected.ts +61 -0
  15. package/registry/cornerstone3d/6/classes-and-types/tests/typescript/renamed-classes-make-two-operands-one/input.ts +65 -0
  16. package/registry/cornerstone3d/6/classes-and-types/tests/typescript/renamed-classes-make-two-operands-one/metrics.json +36 -0
  17. package/registry/cornerstone3d/6/display-sets/README.md +4 -0
  18. package/registry/cornerstone3d/6/display-sets/scripts/display-sets.ts +4 -2
  19. package/registry/cornerstone3d/6/display-sets/scripts/lib.ts +864 -16
  20. package/registry/cornerstone3d/6/display-sets/tests/tsx/jsx-in-a-js-file/expected.js +45 -0
  21. package/registry/cornerstone3d/6/display-sets/tests/tsx/jsx-in-a-js-file/input.js +39 -0
  22. package/registry/cornerstone3d/6/display-sets/tests/tsx/jsx-in-a-js-file/metrics.json +84 -0
  23. package/registry/cornerstone3d/6/display-sets/tests/tsx/not-analysed-a-name-declared-twice/expected.tsx +42 -0
  24. package/registry/cornerstone3d/6/display-sets/tests/tsx/not-analysed-a-name-declared-twice/input.tsx +42 -0
  25. package/registry/cornerstone3d/6/display-sets/tests/tsx/not-analysed-a-name-declared-twice/metrics.json +12 -0
  26. package/registry/cornerstone3d/6/events/README.md +4 -0
  27. package/registry/cornerstone3d/6/events/scripts/events.ts +5 -3
  28. package/registry/cornerstone3d/6/events/scripts/lib.ts +864 -16
  29. package/registry/cornerstone3d/6/events/tests/tsx/jsx-in-a-js-file/expected.js +18 -0
  30. package/registry/cornerstone3d/6/events/tests/tsx/jsx-in-a-js-file/input.js +18 -0
  31. package/registry/cornerstone3d/6/events/tests/tsx/jsx-in-a-js-file/metrics.json +38 -0
  32. package/registry/cornerstone3d/6/events/tests/tsx/not-analysed-a-name-declared-twice/expected.tsx +21 -0
  33. package/registry/cornerstone3d/6/events/tests/tsx/not-analysed-a-name-declared-twice/input.tsx +21 -0
  34. package/registry/cornerstone3d/6/events/tests/tsx/not-analysed-a-name-declared-twice/metrics.json +12 -0
  35. package/registry/cornerstone3d/6/settings/README.md +4 -0
  36. package/registry/cornerstone3d/6/settings/scripts/lib.ts +864 -16
  37. package/registry/cornerstone3d/6/settings/scripts/settings.ts +5 -3
  38. package/registry/cornerstone3d/6/settings/tests/tsx/jsx-in-a-js-file/expected.js +18 -0
  39. package/registry/cornerstone3d/6/settings/tests/tsx/jsx-in-a-js-file/input.js +18 -0
  40. package/registry/cornerstone3d/6/settings/tests/tsx/jsx-in-a-js-file/metrics.json +46 -0
  41. package/registry/cornerstone3d/6/settings/tests/tsx/not-analysed-a-name-declared-twice/expected.tsx +21 -0
  42. package/registry/cornerstone3d/6/settings/tests/tsx/not-analysed-a-name-declared-twice/input.tsx +21 -0
  43. package/registry/cornerstone3d/6/settings/tests/tsx/not-analysed-a-name-declared-twice/metrics.json +12 -0
  44. package/registry/cornerstone3d/6/tools/README.md +5 -0
  45. package/registry/cornerstone3d/6/tools/scripts/lib.ts +864 -16
  46. package/registry/cornerstone3d/6/tools/scripts/tools.ts +6 -3
  47. package/registry/cornerstone3d/6/tools/tests/tsx/jsx-in-a-js-file/expected.js +35 -0
  48. package/registry/cornerstone3d/6/tools/tests/tsx/jsx-in-a-js-file/input.js +35 -0
  49. package/registry/cornerstone3d/6/tools/tests/tsx/jsx-in-a-js-file/metrics.json +52 -0
  50. package/registry/cornerstone3d/6/tools/tests/tsx/not-analysed-a-name-declared-twice/expected.tsx +38 -0
  51. package/registry/cornerstone3d/6/tools/tests/tsx/not-analysed-a-name-declared-twice/input.tsx +38 -0
  52. package/registry/cornerstone3d/6/tools/tests/tsx/not-analysed-a-name-declared-twice/metrics.json +12 -0
  53. package/registry/cornerstone3d/6/viewport-members/README.md +5 -0
  54. package/registry/cornerstone3d/6/viewport-members/scripts/lib.ts +864 -16
  55. package/registry/cornerstone3d/6/viewport-members/scripts/viewport-members.ts +5 -3
  56. package/registry/cornerstone3d/6/viewport-members/tests/tsx/jsx-in-a-js-file/expected.js +41 -0
  57. package/registry/cornerstone3d/6/viewport-members/tests/tsx/jsx-in-a-js-file/input.js +41 -0
  58. package/registry/cornerstone3d/6/viewport-members/tests/tsx/jsx-in-a-js-file/metrics.json +108 -0
  59. package/registry/cornerstone3d/6/viewport-members/tests/tsx/not-analysed-a-name-declared-twice/expected.tsx +44 -0
  60. package/registry/cornerstone3d/6/viewport-members/tests/tsx/not-analysed-a-name-declared-twice/input.tsx +44 -0
  61. package/registry/cornerstone3d/6/viewport-members/tests/tsx/not-analysed-a-name-declared-twice/metrics.json +12 -0
  62. package/registry/cornerstone3d/6/viewport-types/README.md +35 -0
  63. package/registry/cornerstone3d/6/viewport-types/scripts/lib.ts +864 -16
  64. package/registry/cornerstone3d/6/viewport-types/scripts/viewport-types.ts +192 -4
  65. package/registry/cornerstone3d/6/viewport-types/tests/tsx/jsx-in-a-js-file/expected.js +76 -0
  66. package/registry/cornerstone3d/6/viewport-types/tests/tsx/jsx-in-a-js-file/input.js +76 -0
  67. package/registry/cornerstone3d/6/viewport-types/tests/tsx/jsx-in-a-js-file/metrics.json +102 -0
  68. package/registry/cornerstone3d/6/viewport-types/tests/tsx/not-analysed-a-name-declared-twice/expected.tsx +79 -0
  69. package/registry/cornerstone3d/6/viewport-types/tests/tsx/not-analysed-a-name-declared-twice/input.tsx +79 -0
  70. package/registry/cornerstone3d/6/viewport-types/tests/tsx/not-analysed-a-name-declared-twice/metrics.json +12 -0
  71. package/registry/cornerstone3d/6/viewport-types/tests/tsx/not-analysed-a-name-declared-twice-in-a-js-file/expected.js +78 -0
  72. package/registry/cornerstone3d/6/viewport-types/tests/tsx/not-analysed-a-name-declared-twice-in-a-js-file/input.js +78 -0
  73. package/registry/cornerstone3d/6/viewport-types/tests/tsx/not-analysed-a-name-declared-twice-in-a-js-file/metrics.json +12 -0
  74. package/registry/cornerstone3d/6/viewport-types/tests/typescript/ohif-returns-of-two-types-become-one/expected.ts +173 -0
  75. package/registry/cornerstone3d/6/viewport-types/tests/typescript/ohif-returns-of-two-types-become-one/input.ts +173 -0
  76. package/registry/cornerstone3d/6/viewport-types/tests/typescript/ohif-returns-of-two-types-become-one/metrics.json +166 -0
  77. package/registry/cornerstone3d/6/viewport-types/tests/typescript/ohif-two-operands-become-one/expected.ts +103 -0
  78. package/registry/cornerstone3d/6/viewport-types/tests/typescript/ohif-two-operands-become-one/input.ts +103 -0
  79. package/registry/cornerstone3d/6/viewport-types/tests/typescript/ohif-two-operands-become-one/metrics.json +78 -0
package/.oxlintrc.json ADDED
@@ -0,0 +1,4 @@
1
+ {
2
+ "extends": ["../../.oxlintrc.json"],
3
+ "ignorePatterns": ["registry/**/tests/**/not-analysed-*/**"]
4
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cornerstonejs/codemods",
3
- "version": "6.0.0-beta.0",
3
+ "version": "6.0.0-beta.2",
4
4
  "description": "Codemod registry packages for Cornerstone3D migrations",
5
5
  "keywords": [
6
6
  "cornerstone3d",
@@ -60,6 +60,52 @@ file and a line.
60
60
  `NEW_IMAGE` for the slices of a volume too, or an `ORTHOGRAPHIC` viewport
61
61
  that got the 5.x default orientation `AXIAL` written out.
62
62
 
63
+ Two review rules are about a rewrite that is right name by name and still
64
+ changes what the code says:
65
+
66
+ - A rule that starts with
67
+ `ViewportType.PLANAR: one function returned STACK and ORTHOGRAPHIC`, at
68
+ the line of the function. It returned `STACK` on one path and
69
+ `ORTHOGRAPHIC` on another, and returns `PLANAR` on both now. Read its
70
+ callers. Where a caller passes the answer to a viewport input, there is
71
+ nothing to do. Where the application stores or compares it to tell a stack
72
+ from a volume, the answer is lost: give the application its own values for
73
+ the shape of its data, and ask `viewport.getCurrentMode()` for a live
74
+ viewport.
75
+ - A rule such as
76
+ `ViewportType.VOLUME_3D: two operands of one || are the same now`, at the
77
+ line of the second operand; also for `&&`, for two `case` labels of one
78
+ `switch`, for two entries of a list used with `includes`, and for a
79
+ renamed class such as `VolumeViewport3D`.
80
+ `type === VOLUME_3D || type === VOLUME_3D_NEXT` is one test after the
81
+ rename. Where the code meant "any 3D viewport", delete one of the two.
82
+ Where it told a legacy viewport from a next one, the test is true for
83
+ every viewport of that type in 6.0: delete the branch for the legacy
84
+ viewport, and read the other tests of that type near it. Of two equal
85
+ `case` labels only the first runs.
86
+
87
+ ## JavaScript files, and a file that is not analysed
88
+
89
+ To know what a name refers to, the codemods ask the semantic analysis of the
90
+ Codemod tool. It reads JSX only in `.jsx` and `.tsx` files. Where it cannot
91
+ read a `.js`, `.mjs` or `.cjs` file (JSX is the common reason), the codemods
92
+ look the names up themselves on the syntax tree: imports, variables,
93
+ parameters, functions, classes and `catch` clauses, each with its scope. Such
94
+ a file is rewritten and reported as the same code in a `.jsx` file is.
95
+
96
+ A step never stops on a file. A file that cannot be analysed stays as it is
97
+ and has one entry in `cornerstone3d-6-manual`, at line 1. Its rule starts
98
+ with `the file was not analysed:` and gives the reason:
99
+
100
+ - a TypeScript file that the tool cannot read, for example
101
+ ``the file was not analysed: Identifier `x` has already been declared``;
102
+ - a JavaScript file with JSX where a name that the codemod looks up is
103
+ declared twice in one scope:
104
+ `the file was not analysed: x is declared twice in one scope`.
105
+
106
+ The step reports nothing else in that file. Fix the file and run the codemod
107
+ again, or do the file by hand with the guide.
108
+
63
109
  ## Included Migrations
64
110
 
65
111
  In the order in which they run:
@@ -71,6 +71,18 @@ npx codemod @cornerstonejs/cornerstone3d-6-classes-and-types
71
71
  ## Reported
72
72
 
73
73
  - Review: each `instanceof` rewrite.
74
+ - Review: two operands that a class rename makes the same expression
75
+ (`VolumeViewport3D: two operands of one || are the same now, read what each tested in 5.x`,
76
+ at the line of the second one): in a chain of `||` or of `&&`, as `case`
77
+ labels of one `switch`, or as entries of an array literal used with
78
+ `includes`, directly or through a `const`.
79
+ `viewport instanceof VolumeViewport3D || viewport instanceof GenericVolumeViewport3D`
80
+ told a legacy viewport from a next one in 5.x and is one test after the
81
+ rename: delete one operand, or the branch for the legacy viewport. Of two
82
+ equal `case` labels only the first runs. Operands are compared without
83
+ comments, casts and white space, so two casts of one value
84
+ (`(viewport as Types.IStackViewport).id || (viewport as Types.IVolumeViewport).id`)
85
+ have no entry.
74
86
  - Manual: an `instanceof` before a mount
75
87
  (`the viewport may not show its data yet`), a legacy class used as a value
76
88
  (`new StackViewport(...)`, a shorthand property, an export or a re-export
@@ -98,6 +110,10 @@ npx codemod @cornerstonejs/cornerstone3d-6-classes-and-types
98
110
  its `Types` name: the rewrites read `Types.X` only), and
99
111
  `utilities.isGenericViewport(...)`
100
112
  (deprecated: drop the check and keep its true branch).
113
+ - Manual, at line 1: `the file was not analysed:` with the reason, for a
114
+ file whose names cannot be looked up (the tool cannot read it, or a name
115
+ is declared twice in one scope). The file stays as it is. See the README
116
+ of `@cornerstonejs/cornerstone3d-6`.
101
117
 
102
118
  ## Development
103
119
 
@@ -5,10 +5,13 @@ import {
5
5
  CORE_PACKAGE,
6
6
  CoreImports,
7
7
  EditSet,
8
+ HeldMetric,
9
+ analysed,
8
10
  bracketKey,
9
11
  comparisonAt,
10
12
  coreImportOf,
11
13
  declarationNodeOf,
14
+ definitionAt,
12
15
  enclosingFunction,
13
16
  importSource,
14
17
  isCoreEnum,
@@ -16,20 +19,23 @@ import {
16
19
  isCoreNamespace,
17
20
  isMountedBefore,
18
21
  isStableBinding,
22
+ keptValue,
23
+ mergedOperands,
19
24
  methodCall,
20
25
  referencesOf as declarationReferences,
21
26
  site,
22
27
  stableBinding,
23
28
  staticKeyName,
24
29
  unwrap,
30
+ writtenValues,
25
31
  } from './lib.ts';
26
32
  import type { Node } from './lib.ts';
27
33
 
28
34
  /** Sites that the reader changes. */
29
- const manual = useMetricAtom('cornerstone3d-6-manual');
35
+ const manual = new HeldMetric(useMetricAtom('cornerstone3d-6-manual'));
30
36
 
31
37
  /** Rewrites at a decision of the migration guide. */
32
- const review = useMetricAtom('cornerstone3d-6-review');
38
+ const review = new HeldMetric(useMetricAtom('cornerstone3d-6-review'));
33
39
 
34
40
  /** The legacy classes and the class that types the same viewport in 6.0. */
35
41
  const LEGACY_CLASS_TYPE: Record<string, string> = {
@@ -131,35 +137,6 @@ const INPUT_METHOD: Record<string, string> = {
131
137
  array: 'setViewports',
132
138
  };
133
139
 
134
- /**
135
- * The expressions around a value that keep it: parentheses, casts and
136
- * non-null assertions.
137
- */
138
- const KEEPING_KINDS = new Set([
139
- 'parenthesized_expression',
140
- 'as_expression',
141
- 'satisfies_expression',
142
- 'non_null_expression',
143
- 'type_assertion',
144
- ]);
145
-
146
- /**
147
- * The parents of a name that write it in another way than `name = value`:
148
- * an operator assignment, `++` or `--`, a `for in` or `for of`, or a
149
- * destructuring target.
150
- */
151
- const OTHER_WRITES = new Set([
152
- 'augmented_assignment_expression',
153
- 'update_expression',
154
- 'for_in_statement',
155
- 'object_pattern',
156
- 'array_pattern',
157
- 'pair_pattern',
158
- 'assignment_pattern',
159
- 'object_assignment_pattern',
160
- 'rest_pattern',
161
- ]);
162
-
163
140
  /**
164
141
  * What a test of the viewport type tells about the family of a node;
165
142
  * `mixed` when the viewport inputs of its id give a 3D type with another, or
@@ -214,7 +191,7 @@ function isTypedEngine(node: Node): boolean {
214
191
  return false;
215
192
  }
216
193
 
217
- const definition = inner.definition();
194
+ const definition = definitionAt(inner);
218
195
  const target = definition?.node;
219
196
 
220
197
  if (!target || definition.kind !== 'local') {
@@ -663,62 +640,6 @@ function inputEntries(objectNode: Node): {
663
640
  };
664
641
  }
665
642
 
666
- /**
667
- * Returns the outermost expression that keeps the value of a node through
668
- * parentheses, casts and non-null assertions.
669
- */
670
- function keptValue(node: Node): Node {
671
- let current = node;
672
- let parent = node.parent();
673
-
674
- while (parent && KEEPING_KINDS.has(parent.kind())) {
675
- current = parent;
676
- parent = parent.parent();
677
- }
678
-
679
- return current;
680
- }
681
-
682
- /**
683
- * Returns the values that a declared name gets: the value of its declaration
684
- * and the value of each `name = value`. Undefined when the file writes the
685
- * name in another way, such as `+=` or a destructuring.
686
- */
687
- function writtenValues(declaration: Node): Node[] | undefined {
688
- const holder = declaration.parent();
689
- const initial =
690
- holder?.kind() === 'variable_declarator' &&
691
- holder.field('name')?.id() === declaration.id()
692
- ? holder.field('value')
693
- : undefined;
694
- const values = initial ? [initial] : [];
695
-
696
- for (const reference of declarationReferences(declaration)) {
697
- let child = reference;
698
- let parent = reference.parent();
699
-
700
- while (parent?.kind() === 'parenthesized_expression') {
701
- child = parent;
702
- parent = parent.parent();
703
- }
704
-
705
- const kind = parent?.kind() ?? '';
706
- const right =
707
- kind === 'assignment_expression' &&
708
- parent.field('left')?.id() === child.id()
709
- ? parent.field('right')
710
- : undefined;
711
-
712
- if (right) {
713
- values.push(right);
714
- } else if (OTHER_WRITES.has(kind)) {
715
- return undefined;
716
- }
717
- }
718
-
719
- return values;
720
- }
721
-
722
643
  /**
723
644
  * Returns what identifies the rendering engine that an expression gives:
724
645
  * `engine:` with the id of `new RenderingEngine(id)` or
@@ -1402,13 +1323,33 @@ function reportTypesSubpath(run: Run): void {
1402
1323
  }
1403
1324
  }
1404
1325
 
1326
+ /**
1327
+ * Reports two operands that the renames make the same expression: in a
1328
+ * chain of `||` or of `&&`, as `case` labels of one `switch`, or as entries
1329
+ * of a list used with `includes`. An `instanceof VolumeViewport3D` beside an
1330
+ * `instanceof GenericVolumeViewport3D` told a legacy viewport from a next
1331
+ * one in 5.x, and the two are one test in 6.0.
1332
+ */
1333
+ function reportMergedOperands(run: Run): void {
1334
+ for (const merged of mergedOperands(run.rootNode, run.edits.all())) {
1335
+ const name = merged.inserted.find((text) =>
1336
+ /^[A-Za-z_$][\w$]*$/.test(text)
1337
+ );
1338
+
1339
+ if (name) {
1340
+ review.increment(site(run.root, `${name}: ${merged.note}`, merged.node));
1341
+ }
1342
+ }
1343
+ }
1344
+
1405
1345
  /**
1406
1346
  * Rewrites the classes and the TypeScript types that Cornerstone3D 6.0
1407
1347
  * removes or renames: the legacy viewport classes become `PlanarViewport` in
1408
1348
  * a type and a test of the content mode after `instanceof`, the deprecated
1409
1349
  * class aliases get their plain names, `Types.IStackViewport` and its kin
1410
1350
  * become the class of their family, and a cast of `getViewport` becomes its
1411
- * type argument.
1351
+ * type argument. Two operands that the renames make the same are reported
1352
+ * for review.
1412
1353
  */
1413
1354
  const codemod: Codemod<TSX> = (root) => {
1414
1355
  const rootNode = root.root();
@@ -1448,6 +1389,7 @@ const codemod: Codemod<TSX> = (root) => {
1448
1389
  reportBracketClasses(run);
1449
1390
  reportIsGenericViewport(run);
1450
1391
  reportTypesSubpath(run);
1392
+ reportMergedOperands(run);
1451
1393
 
1452
1394
  const importEdits = run.imports.edits();
1453
1395
 
@@ -1458,4 +1400,4 @@ const codemod: Codemod<TSX> = (root) => {
1458
1400
  return rootNode.commitEdits([...run.edits.all(), ...importEdits]);
1459
1401
  };
1460
1402
 
1461
- export default codemod;
1403
+ export default analysed(codemod, manual, review);