@cornerstonejs/codemods 5.0.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.
- package/CHANGELOG.md +20 -0
- package/LICENSE +21 -0
- package/README.md +92 -0
- package/package.json +40 -0
- package/registry/cornerstone3d/5/README.md +28 -0
- package/registry/cornerstone3d/5/codemod.yaml +19 -0
- package/registry/cornerstone3d/5/generic-viewport/README.md +39 -0
- package/registry/cornerstone3d/5/generic-viewport/codemod.yaml +21 -0
- package/registry/cornerstone3d/5/generic-viewport/scripts/rendering-engine-viewport-accessors.ts +234 -0
- package/registry/cornerstone3d/5/generic-viewport/tests/edge-existing-utilities-import/expected.ts +8 -0
- package/registry/cornerstone3d/5/generic-viewport/tests/edge-existing-utilities-import/input.ts +6 -0
- package/registry/cornerstone3d/5/generic-viewport/tests/negative-no-matching-accessors/expected.ts +8 -0
- package/registry/cornerstone3d/5/generic-viewport/tests/negative-no-matching-accessors/input.ts +8 -0
- package/registry/cornerstone3d/5/generic-viewport/tests/nested-stack-viewport/expected.ts +4 -0
- package/registry/cornerstone3d/5/generic-viewport/tests/nested-stack-viewport/input.ts +4 -0
- package/registry/cornerstone3d/5/generic-viewport/tests/positive-viewport-accessors/expected.ts +17 -0
- package/registry/cornerstone3d/5/generic-viewport/tests/positive-viewport-accessors/input.ts +10 -0
- package/registry/cornerstone3d/5/generic-viewport/tests/viewport-accessors/expected.ts +16 -0
- package/registry/cornerstone3d/5/generic-viewport/tests/viewport-accessors/input.ts +9 -0
- package/registry/cornerstone3d/5/generic-viewport/workflow.yaml +16 -0
- package/registry/cornerstone3d/5/workflow.yaml +8 -0
- package/scripts/validate-registry-packages.mjs +49 -0
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
# Change Log
|
|
2
|
+
|
|
3
|
+
All notable changes to this project will be documented in this file.
|
|
4
|
+
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
|
|
5
|
+
|
|
6
|
+
## [5.0.2](https://github.com/cornerstonejs/cornerstone3D/compare/v5.0.1...v5.0.2) (2026-06-11)
|
|
7
|
+
|
|
8
|
+
### Bug Fixes
|
|
9
|
+
|
|
10
|
+
- codemod publish and docs 5x ([#2759](https://github.com/cornerstonejs/cornerstone3D/issues/2759)) ([0a4064d](https://github.com/cornerstonejs/cornerstone3D/commit/0a4064dc81a2f36c04ec3fc7e68c6aebbdc7ccdb)), closes [#2612](https://github.com/cornerstonejs/cornerstone3D/issues/2612)
|
|
11
|
+
|
|
12
|
+
## [5.0.1](https://github.com/cornerstonejs/cornerstone3D/compare/v5.0.0...v5.0.1) (2026-06-10)
|
|
13
|
+
|
|
14
|
+
**Note:** Version bump only for package @cornerstonejs/codemods
|
|
15
|
+
|
|
16
|
+
# [5.0.0](https://github.com/cornerstonejs/cornerstone3D/compare/v4.22.13...v5.0.0) (2026-06-09)
|
|
17
|
+
|
|
18
|
+
### Features
|
|
19
|
+
|
|
20
|
+
- Cornerstone3D 5.0 ([#2748](https://github.com/cornerstonejs/cornerstone3D/issues/2748)) ([aa68c90](https://github.com/cornerstonejs/cornerstone3D/commit/aa68c904b8ba6abfcbf05fbb510bb10a17ea7028)), closes [#2638](https://github.com/cornerstonejs/cornerstone3D/issues/2638) [#2639](https://github.com/cornerstonejs/cornerstone3D/issues/2639) [#2625](https://github.com/cornerstonejs/cornerstone3D/issues/2625) [#2666](https://github.com/cornerstonejs/cornerstone3D/issues/2666) [#2635](https://github.com/cornerstonejs/cornerstone3D/issues/2635)
|
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2019 Open Health Imaging Foundation
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
# Cornerstone3D Codemods
|
|
2
|
+
|
|
3
|
+
Codemod registry packages for Cornerstone3D migrations. These are published to
|
|
4
|
+
the Codemod registry so users run them with `npx codemod`.
|
|
5
|
+
|
|
6
|
+
## Usage
|
|
7
|
+
|
|
8
|
+
Run the full Cornerstone3D v5 migration:
|
|
9
|
+
|
|
10
|
+
```sh
|
|
11
|
+
npx codemod @cornerstonejs/cornerstone3d-5
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
Run only the Generic Viewport migration:
|
|
15
|
+
|
|
16
|
+
```sh
|
|
17
|
+
npx codemod @cornerstonejs/cornerstone3d-5-generic-viewport
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
## Registry Packages
|
|
21
|
+
|
|
22
|
+
This workspace currently includes:
|
|
23
|
+
|
|
24
|
+
- `@cornerstonejs/cornerstone3d-5`: runs all available Cornerstone3D v5
|
|
25
|
+
migration packages in the recommended order.
|
|
26
|
+
- `@cornerstonejs/cornerstone3d-5-generic-viewport`: migrates removed
|
|
27
|
+
RenderingEngine viewport accessor APIs.
|
|
28
|
+
|
|
29
|
+
The Generic Viewport migration changes:
|
|
30
|
+
|
|
31
|
+
- `getStackViewports()` -> `getViewports().filter(utilities.viewportSupportsStackCompatibility)`
|
|
32
|
+
- `getVolumeViewports()` -> `getViewports().filter(utilities.viewportSupportsVolumeCompatibility)`
|
|
33
|
+
- `getStackViewport(id)` -> `getViewport(id)`
|
|
34
|
+
|
|
35
|
+
For simple variable declarations, `getStackViewport(id)` also adds a runtime
|
|
36
|
+
capability guard.
|
|
37
|
+
|
|
38
|
+
## Development
|
|
39
|
+
|
|
40
|
+
Validate all registry packages and run fixture tests:
|
|
41
|
+
|
|
42
|
+
```sh
|
|
43
|
+
cd packages/codemods
|
|
44
|
+
yarn validate
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
Validate only registry workflow metadata:
|
|
48
|
+
|
|
49
|
+
```sh
|
|
50
|
+
cd packages/codemods
|
|
51
|
+
yarn validate:registry
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
Run only the JSSG fixture tests:
|
|
55
|
+
|
|
56
|
+
```sh
|
|
57
|
+
cd packages/codemods
|
|
58
|
+
yarn test
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
Run a local registry package against a target checkout:
|
|
62
|
+
|
|
63
|
+
```sh
|
|
64
|
+
cd packages/codemods
|
|
65
|
+
npx codemod workflow run -w registry/cornerstone3d/5/generic-viewport -t /path/to/project --dry-run
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
## Registry Publishing
|
|
69
|
+
|
|
70
|
+
The package is intentionally private to npm. Publishing happens through the
|
|
71
|
+
Codemod registry packages under `packages/codemods/registry`.
|
|
72
|
+
|
|
73
|
+
Publish migration packages before aggregate packages that reference them.
|
|
74
|
+
|
|
75
|
+
To publish `cornerstone3d/5/generic-viewport`:
|
|
76
|
+
|
|
77
|
+
```sh
|
|
78
|
+
git tag cornerstone3d/5/generic-viewport@v0.1.0
|
|
79
|
+
git push origin cornerstone3d/5/generic-viewport@v0.1.0
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
To publish `cornerstone3d/5`:
|
|
83
|
+
|
|
84
|
+
```sh
|
|
85
|
+
git tag cornerstone3d/5@v0.1.0
|
|
86
|
+
git push origin cornerstone3d/5@v0.1.0
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
`Publish Codemod Registry Entry` resolves those tags to directories under
|
|
90
|
+
`packages/codemods/registry/` and publishes them with
|
|
91
|
+
`codemod/publish-action@v1`. `Validate Codemod Registry Entries` checks pull
|
|
92
|
+
requests and pushes to `main`.
|
package/package.json
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@cornerstonejs/codemods",
|
|
3
|
+
"version": "5.0.2",
|
|
4
|
+
"description": "Codemod registry packages for Cornerstone3D migrations",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"cornerstone3d",
|
|
7
|
+
"codemod",
|
|
8
|
+
"migration"
|
|
9
|
+
],
|
|
10
|
+
"publishConfig": {
|
|
11
|
+
"access": "public"
|
|
12
|
+
},
|
|
13
|
+
"repository": "https://github.com/cornerstonejs/cornerstone3D",
|
|
14
|
+
"sideEffects": false,
|
|
15
|
+
"scripts": {
|
|
16
|
+
"build": "node -e \"console.log('codemods package does not require a build step')\"",
|
|
17
|
+
"build:esm": "pnpm run build",
|
|
18
|
+
"build:esm:watch": "pnpm run build",
|
|
19
|
+
"build:all": "pnpm run build",
|
|
20
|
+
"clean": "shx rm -rf dist",
|
|
21
|
+
"clean:deep": "pnpm run clean && shx rm -rf node_modules",
|
|
22
|
+
"dev": "pnpm run test",
|
|
23
|
+
"test": "npx codemod jssg test -l tsx registry/cornerstone3d/5/generic-viewport/scripts/rendering-engine-viewport-accessors.ts registry/cornerstone3d/5/generic-viewport/tests",
|
|
24
|
+
"validate": "pnpm run validate:registry && pnpm run test",
|
|
25
|
+
"validate:registry": "node scripts/validate-registry-packages.mjs",
|
|
26
|
+
"lint": "oxlint ."
|
|
27
|
+
},
|
|
28
|
+
"contributors": [
|
|
29
|
+
{
|
|
30
|
+
"name": "Cornerstone.js Contributors",
|
|
31
|
+
"url": "https://github.com/orgs/cornerstonejs/people"
|
|
32
|
+
}
|
|
33
|
+
],
|
|
34
|
+
"license": "MIT",
|
|
35
|
+
"funding": {
|
|
36
|
+
"type": "individual",
|
|
37
|
+
"url": "https://ohif.org/donate"
|
|
38
|
+
},
|
|
39
|
+
"gitHead": "b1f35afb1ff6a75fc487ecabf71fec4851d7d339"
|
|
40
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
# Cornerstone3D v5 Migration
|
|
2
|
+
|
|
3
|
+
Runs all available Cornerstone3D v5 migration codemods in the recommended
|
|
4
|
+
order.
|
|
5
|
+
|
|
6
|
+
## Usage
|
|
7
|
+
|
|
8
|
+
```sh
|
|
9
|
+
npx codemod @cornerstonejs/cornerstone3d-5
|
|
10
|
+
```
|
|
11
|
+
|
|
12
|
+
To preview changes before writing files:
|
|
13
|
+
|
|
14
|
+
```sh
|
|
15
|
+
npx codemod @cornerstonejs/cornerstone3d-5 --dry-run
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
## Included Migrations
|
|
19
|
+
|
|
20
|
+
- `@cornerstonejs/cornerstone3d-5-generic-viewport`: replaces removed
|
|
21
|
+
RenderingEngine viewport accessor APIs with Cornerstone3D v5 compatible
|
|
22
|
+
viewport APIs.
|
|
23
|
+
|
|
24
|
+
## Publishing
|
|
25
|
+
|
|
26
|
+
Publish child migration packages before publishing this aggregate package. The
|
|
27
|
+
workflow resolves `@cornerstonejs/cornerstone3d-5-generic-viewport` from the
|
|
28
|
+
Codemod registry at runtime.
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
schema_version: '1.0'
|
|
2
|
+
name: '@cornerstonejs/cornerstone3d-5'
|
|
3
|
+
version: '0.1.0'
|
|
4
|
+
description: 'Apply all available Cornerstone3D v5 migration transforms in the recommended order.'
|
|
5
|
+
author: 'Cornerstone.js Contributors'
|
|
6
|
+
license: 'MIT'
|
|
7
|
+
workflow: 'workflow.yaml'
|
|
8
|
+
repository: 'https://github.com/cornerstonejs/cornerstone3D'
|
|
9
|
+
category: 'migration'
|
|
10
|
+
targets:
|
|
11
|
+
languages: ['typescript', 'tsx', 'javascript', 'jsx']
|
|
12
|
+
keywords:
|
|
13
|
+
- cornerstone3d
|
|
14
|
+
- migration
|
|
15
|
+
- upgrade
|
|
16
|
+
- v5
|
|
17
|
+
registry:
|
|
18
|
+
access: 'public'
|
|
19
|
+
visibility: 'public'
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
# Cornerstone3D v5 Generic Viewport Migration
|
|
2
|
+
|
|
3
|
+
Replaces removed RenderingEngine viewport accessor APIs with Cornerstone3D v5
|
|
4
|
+
compatible viewport APIs.
|
|
5
|
+
|
|
6
|
+
## Usage
|
|
7
|
+
|
|
8
|
+
```sh
|
|
9
|
+
npx codemod @cornerstonejs/cornerstone3d-5-generic-viewport
|
|
10
|
+
```
|
|
11
|
+
|
|
12
|
+
To preview changes before writing files:
|
|
13
|
+
|
|
14
|
+
```sh
|
|
15
|
+
npx codemod @cornerstonejs/cornerstone3d-5-generic-viewport --dry-run
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
## Changes
|
|
19
|
+
|
|
20
|
+
- `getStackViewports()` becomes
|
|
21
|
+
`getViewports().filter(utilities.viewportSupportsStackCompatibility)`.
|
|
22
|
+
- `getVolumeViewports()` becomes
|
|
23
|
+
`getViewports().filter(utilities.viewportSupportsVolumeCompatibility)`.
|
|
24
|
+
- `getStackViewport(id)` becomes `getViewport(id)`.
|
|
25
|
+
- Simple `const viewport = renderingEngine.getStackViewport(id)` declarations
|
|
26
|
+
receive a runtime `viewportSupportsStackCompatibility` guard before stack
|
|
27
|
+
APIs are used.
|
|
28
|
+
|
|
29
|
+
When a file needs compatibility guards, the transform adds `utilities` to an
|
|
30
|
+
existing `@cornerstonejs/core` named import or inserts a new named import.
|
|
31
|
+
|
|
32
|
+
## Development
|
|
33
|
+
|
|
34
|
+
From `packages/codemods`:
|
|
35
|
+
|
|
36
|
+
```sh
|
|
37
|
+
yarn test
|
|
38
|
+
npx codemod workflow validate -w registry/cornerstone3d/5/generic-viewport
|
|
39
|
+
```
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
schema_version: '1.0'
|
|
2
|
+
name: '@cornerstonejs/cornerstone3d-5-generic-viewport'
|
|
3
|
+
version: '0.1.0'
|
|
4
|
+
description: 'Replace removed RenderingEngine viewport accessors with Cornerstone3D v5 compatible viewport APIs.'
|
|
5
|
+
author: 'Cornerstone.js Contributors'
|
|
6
|
+
license: 'MIT'
|
|
7
|
+
workflow: 'workflow.yaml'
|
|
8
|
+
repository: 'https://github.com/cornerstonejs/cornerstone3D'
|
|
9
|
+
category: 'migration'
|
|
10
|
+
targets:
|
|
11
|
+
languages: ['typescript', 'tsx', 'javascript', 'jsx']
|
|
12
|
+
keywords:
|
|
13
|
+
- cornerstone3d
|
|
14
|
+
- migration
|
|
15
|
+
- upgrade
|
|
16
|
+
- v5
|
|
17
|
+
- generic-viewport
|
|
18
|
+
- rendering-engine
|
|
19
|
+
registry:
|
|
20
|
+
access: 'public'
|
|
21
|
+
visibility: 'public'
|
package/registry/cornerstone3d/5/generic-viewport/scripts/rendering-engine-viewport-accessors.ts
ADDED
|
@@ -0,0 +1,234 @@
|
|
|
1
|
+
import type { Codemod, Edit } from 'codemod:ast-grep';
|
|
2
|
+
import type { SgNode } from 'codemod:ast-grep';
|
|
3
|
+
import type TSX from 'codemod:ast-grep/langs/tsx';
|
|
4
|
+
|
|
5
|
+
const STACK_GUARD = 'viewportSupportsStackCompatibility';
|
|
6
|
+
const VOLUME_GUARD = 'viewportSupportsVolumeCompatibility';
|
|
7
|
+
const CORE_PACKAGE = '@cornerstonejs/core';
|
|
8
|
+
|
|
9
|
+
function getMethodCallParts(node: SgNode<TSX>) {
|
|
10
|
+
if (node.kind() !== 'call_expression') {
|
|
11
|
+
return null;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
const memberExpression = node.field('function');
|
|
15
|
+
|
|
16
|
+
if (!memberExpression || memberExpression.kind() !== 'member_expression') {
|
|
17
|
+
return null;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
const object = memberExpression.field('object');
|
|
21
|
+
const property = memberExpression.field('property');
|
|
22
|
+
|
|
23
|
+
if (!object || !property) {
|
|
24
|
+
return null;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
return {
|
|
28
|
+
object,
|
|
29
|
+
property,
|
|
30
|
+
methodName: property.text(),
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function createFilterReplacement(objectText: string, guardName: string) {
|
|
35
|
+
return `${objectText}\n .getViewports()\n .filter(utilities.${guardName})`;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
function findCoreNamedImport(rootNode: SgNode<TSX>) {
|
|
39
|
+
const coreImports = rootNode.findAll({
|
|
40
|
+
rule: {
|
|
41
|
+
kind: 'import_statement',
|
|
42
|
+
},
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
return coreImports.find((importNode) => {
|
|
46
|
+
const text = importNode.text();
|
|
47
|
+
|
|
48
|
+
return (
|
|
49
|
+
!/^import\s+type\b/.test(text) &&
|
|
50
|
+
text.includes('{') &&
|
|
51
|
+
text.includes('}') &&
|
|
52
|
+
/from\s+(['"])@cornerstonejs\/core\1/.test(text)
|
|
53
|
+
);
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
function hasUtilitiesImport(rootNode: SgNode<TSX>) {
|
|
58
|
+
const coreImport = findCoreNamedImport(rootNode);
|
|
59
|
+
|
|
60
|
+
if (!coreImport) {
|
|
61
|
+
return false;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
const importClause = coreImport.text().match(/\{([\s\S]*?)\}/)?.[1] ?? '';
|
|
65
|
+
|
|
66
|
+
return importClause
|
|
67
|
+
.split(',')
|
|
68
|
+
.map((specifier) => specifier.trim())
|
|
69
|
+
.some((specifier) => specifier === 'utilities');
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
function createUtilitiesImportEdit(rootNode: SgNode<TSX>): Edit | null {
|
|
73
|
+
if (hasUtilitiesImport(rootNode)) {
|
|
74
|
+
return null;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
const coreImport = findCoreNamedImport(rootNode);
|
|
78
|
+
|
|
79
|
+
if (coreImport) {
|
|
80
|
+
const text = coreImport.text();
|
|
81
|
+
const specifierText = text.match(/\{([\s\S]*?)\}/)?.[1];
|
|
82
|
+
const quote = text.includes(`from "${CORE_PACKAGE}"`) ? '"' : "'";
|
|
83
|
+
|
|
84
|
+
if (!specifierText) {
|
|
85
|
+
return null;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
const specifiers = specifierText
|
|
89
|
+
.split(',')
|
|
90
|
+
.map((specifier) => specifier.trim())
|
|
91
|
+
.filter(Boolean);
|
|
92
|
+
|
|
93
|
+
specifiers.push('utilities');
|
|
94
|
+
|
|
95
|
+
return coreImport.replace(
|
|
96
|
+
`import { ${specifiers.join(', ')} } from ${quote}${CORE_PACKAGE}${quote};`
|
|
97
|
+
);
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
const firstImport = rootNode.find({
|
|
101
|
+
rule: {
|
|
102
|
+
kind: 'import_statement',
|
|
103
|
+
},
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
const insertedText = `import { utilities } from '${CORE_PACKAGE}';\n`;
|
|
107
|
+
|
|
108
|
+
if (firstImport) {
|
|
109
|
+
return {
|
|
110
|
+
startPos: firstImport.range().start.index,
|
|
111
|
+
endPos: firstImport.range().start.index,
|
|
112
|
+
insertedText,
|
|
113
|
+
};
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
return {
|
|
117
|
+
startPos: 0,
|
|
118
|
+
endPos: 0,
|
|
119
|
+
insertedText,
|
|
120
|
+
};
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
function statementForNode(node: SgNode<TSX>) {
|
|
124
|
+
return node.ancestors().find((ancestor) => {
|
|
125
|
+
const parent = ancestor.parent();
|
|
126
|
+
|
|
127
|
+
if (!parent) {
|
|
128
|
+
return false;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
return (
|
|
132
|
+
parent.kind() === 'program' ||
|
|
133
|
+
parent.kind() === 'statement_block' ||
|
|
134
|
+
parent.kind() === 'else_clause'
|
|
135
|
+
);
|
|
136
|
+
});
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
function createStackGuardEdit(callNode: SgNode<TSX>): Edit | null {
|
|
140
|
+
const variableDeclarator = callNode
|
|
141
|
+
.ancestors()
|
|
142
|
+
.find((ancestor) => ancestor.kind() === 'variable_declarator');
|
|
143
|
+
|
|
144
|
+
if (!variableDeclarator) {
|
|
145
|
+
return null;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
const variableName = variableDeclarator.field('name')?.text();
|
|
149
|
+
const value = variableDeclarator.field('value');
|
|
150
|
+
|
|
151
|
+
if (!variableName || value?.id() !== callNode.id()) {
|
|
152
|
+
return null;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
const statement = statementForNode(variableDeclarator);
|
|
156
|
+
|
|
157
|
+
if (!statement) {
|
|
158
|
+
return null;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
return {
|
|
162
|
+
startPos: statement.range().end.index,
|
|
163
|
+
endPos: statement.range().end.index,
|
|
164
|
+
insertedText: `\nif (!utilities.${STACK_GUARD}(${variableName})) {\n throw new Error('Viewport does not support setStack');\n}`,
|
|
165
|
+
};
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
const codemod: Codemod<TSX> = (root) => {
|
|
169
|
+
const rootNode = root.root();
|
|
170
|
+
const edits: Edit[] = [];
|
|
171
|
+
let needsUtilities = false;
|
|
172
|
+
|
|
173
|
+
const calls = rootNode.findAll({
|
|
174
|
+
rule: {
|
|
175
|
+
kind: 'call_expression',
|
|
176
|
+
any: [
|
|
177
|
+
{ pattern: '$RE.getStackViewports()' },
|
|
178
|
+
{ pattern: '$RE.getVolumeViewports()' },
|
|
179
|
+
{ pattern: '$RE.getStackViewport($$$ARGS)' },
|
|
180
|
+
],
|
|
181
|
+
},
|
|
182
|
+
});
|
|
183
|
+
|
|
184
|
+
for (const call of calls) {
|
|
185
|
+
const parts = getMethodCallParts(call);
|
|
186
|
+
|
|
187
|
+
if (!parts) {
|
|
188
|
+
continue;
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
if (parts.methodName === 'getStackViewports') {
|
|
192
|
+
edits.push(
|
|
193
|
+
call.replace(createFilterReplacement(parts.object.text(), STACK_GUARD))
|
|
194
|
+
);
|
|
195
|
+
needsUtilities = true;
|
|
196
|
+
continue;
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
if (parts.methodName === 'getVolumeViewports') {
|
|
200
|
+
edits.push(
|
|
201
|
+
call.replace(createFilterReplacement(parts.object.text(), VOLUME_GUARD))
|
|
202
|
+
);
|
|
203
|
+
needsUtilities = true;
|
|
204
|
+
continue;
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
if (parts.methodName === 'getStackViewport') {
|
|
208
|
+
edits.push(parts.property.replace('getViewport'));
|
|
209
|
+
|
|
210
|
+
const guardEdit = createStackGuardEdit(call);
|
|
211
|
+
|
|
212
|
+
if (guardEdit) {
|
|
213
|
+
edits.push(guardEdit);
|
|
214
|
+
needsUtilities = true;
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
if (!edits.length) {
|
|
220
|
+
return null;
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
if (needsUtilities) {
|
|
224
|
+
const importEdit = createUtilitiesImportEdit(rootNode);
|
|
225
|
+
|
|
226
|
+
if (importEdit) {
|
|
227
|
+
edits.push(importEdit);
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
return rootNode.commitEdits(edits);
|
|
232
|
+
};
|
|
233
|
+
|
|
234
|
+
export default codemod;
|
package/registry/cornerstone3d/5/generic-viewport/tests/edge-existing-utilities-import/expected.ts
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { RenderingEngine, utilities } from '@cornerstonejs/core';
|
|
2
|
+
|
|
3
|
+
const renderingEngine = new RenderingEngine('default');
|
|
4
|
+
const stackViewports = renderingEngine
|
|
5
|
+
.getViewports()
|
|
6
|
+
.filter(utilities.viewportSupportsStackCompatibility);
|
|
7
|
+
|
|
8
|
+
console.log(utilities, stackViewports);
|
package/registry/cornerstone3d/5/generic-viewport/tests/negative-no-matching-accessors/expected.ts
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { RenderingEngine } from '@cornerstonejs/core';
|
|
2
|
+
|
|
3
|
+
const renderingEngine = new RenderingEngine('default');
|
|
4
|
+
|
|
5
|
+
const viewport = renderingEngine.getViewport('viewport-1');
|
|
6
|
+
const viewports = renderingEngine.getViewports();
|
|
7
|
+
|
|
8
|
+
console.log(viewport, viewports);
|
package/registry/cornerstone3d/5/generic-viewport/tests/negative-no-matching-accessors/input.ts
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { RenderingEngine } from '@cornerstonejs/core';
|
|
2
|
+
|
|
3
|
+
const renderingEngine = new RenderingEngine('default');
|
|
4
|
+
|
|
5
|
+
const viewport = renderingEngine.getViewport('viewport-1');
|
|
6
|
+
const viewports = renderingEngine.getViewports();
|
|
7
|
+
|
|
8
|
+
console.log(viewport, viewports);
|
package/registry/cornerstone3d/5/generic-viewport/tests/positive-viewport-accessors/expected.ts
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { RenderingEngine, utilities } from '@cornerstonejs/core';
|
|
2
|
+
|
|
3
|
+
const renderingEngine = new RenderingEngine('default');
|
|
4
|
+
|
|
5
|
+
const stackViewports = renderingEngine
|
|
6
|
+
.getViewports()
|
|
7
|
+
.filter(utilities.viewportSupportsStackCompatibility);
|
|
8
|
+
const volumeViewports = renderingEngine
|
|
9
|
+
.getViewports()
|
|
10
|
+
.filter(utilities.viewportSupportsVolumeCompatibility);
|
|
11
|
+
const viewport = renderingEngine.getViewport('viewport-1');
|
|
12
|
+
if (!utilities.viewportSupportsStackCompatibility(viewport)) {
|
|
13
|
+
throw new Error('Viewport does not support setStack');
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
viewport.setStack(['image-1']);
|
|
17
|
+
console.log(stackViewports, volumeViewports);
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { RenderingEngine } from '@cornerstonejs/core';
|
|
2
|
+
|
|
3
|
+
const renderingEngine = new RenderingEngine('default');
|
|
4
|
+
|
|
5
|
+
const stackViewports = renderingEngine.getStackViewports();
|
|
6
|
+
const volumeViewports = renderingEngine.getVolumeViewports();
|
|
7
|
+
const viewport = renderingEngine.getStackViewport('viewport-1');
|
|
8
|
+
|
|
9
|
+
viewport.setStack(['image-1']);
|
|
10
|
+
console.log(stackViewports, volumeViewports);
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { RenderingEngine, utilities } from '@cornerstonejs/core';
|
|
2
|
+
|
|
3
|
+
const renderingEngine = new RenderingEngine('default');
|
|
4
|
+
|
|
5
|
+
const _stackViewports = renderingEngine
|
|
6
|
+
.getViewports()
|
|
7
|
+
.filter(utilities.viewportSupportsStackCompatibility);
|
|
8
|
+
const _volumeViewports = renderingEngine
|
|
9
|
+
.getViewports()
|
|
10
|
+
.filter(utilities.viewportSupportsVolumeCompatibility);
|
|
11
|
+
const viewport = renderingEngine.getViewport('viewport-1');
|
|
12
|
+
if (!utilities.viewportSupportsStackCompatibility(viewport)) {
|
|
13
|
+
throw new Error('Viewport does not support setStack');
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
viewport.setStack(['image-1']);
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { RenderingEngine } from '@cornerstonejs/core';
|
|
2
|
+
|
|
3
|
+
const renderingEngine = new RenderingEngine('default');
|
|
4
|
+
|
|
5
|
+
const _stackViewports = renderingEngine.getStackViewports();
|
|
6
|
+
const _volumeViewports = renderingEngine.getVolumeViewports();
|
|
7
|
+
const viewport = renderingEngine.getStackViewport('viewport-1');
|
|
8
|
+
|
|
9
|
+
viewport.setStack(['image-1']);
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
version: '1'
|
|
2
|
+
nodes:
|
|
3
|
+
- id: rendering-engine-viewport-accessors
|
|
4
|
+
name: RenderingEngine viewport accessor migration
|
|
5
|
+
steps:
|
|
6
|
+
- name: Apply RenderingEngine viewport accessor transform
|
|
7
|
+
js-ast-grep:
|
|
8
|
+
js_file: scripts/rendering-engine-viewport-accessors.ts
|
|
9
|
+
language: tsx
|
|
10
|
+
base_path: .
|
|
11
|
+
include:
|
|
12
|
+
- '**/*.{js,jsx,ts,tsx}'
|
|
13
|
+
exclude:
|
|
14
|
+
- '**/node_modules/**'
|
|
15
|
+
- '**/dist/**'
|
|
16
|
+
- '**/build/**'
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { spawnSync } from 'node:child_process';
|
|
2
|
+
import { readdirSync, statSync } from 'node:fs';
|
|
3
|
+
import { dirname, relative, resolve } from 'node:path';
|
|
4
|
+
import { fileURLToPath } from 'node:url';
|
|
5
|
+
|
|
6
|
+
const packageRoot = resolve(dirname(fileURLToPath(import.meta.url)), '..');
|
|
7
|
+
const registryRoot = resolve(packageRoot, 'registry');
|
|
8
|
+
const registryPackageDirs = [];
|
|
9
|
+
|
|
10
|
+
function walk(directory) {
|
|
11
|
+
for (const entry of readdirSync(directory)) {
|
|
12
|
+
const entryPath = resolve(directory, entry);
|
|
13
|
+
const stat = statSync(entryPath);
|
|
14
|
+
|
|
15
|
+
if (stat.isDirectory()) {
|
|
16
|
+
walk(entryPath);
|
|
17
|
+
continue;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
if (entry === 'codemod.yaml') {
|
|
21
|
+
registryPackageDirs.push(dirname(entryPath));
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
walk(registryRoot);
|
|
27
|
+
|
|
28
|
+
if (!registryPackageDirs.length) {
|
|
29
|
+
console.error('No codemod.yaml files found under packages/codemods/registry');
|
|
30
|
+
process.exit(1);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
for (const directory of registryPackageDirs.sort()) {
|
|
34
|
+
const displayPath = relative(packageRoot, directory);
|
|
35
|
+
console.log(`Validating ${displayPath}`);
|
|
36
|
+
|
|
37
|
+
const result = spawnSync(
|
|
38
|
+
'npx',
|
|
39
|
+
['codemod', 'workflow', 'validate', '-w', directory],
|
|
40
|
+
{
|
|
41
|
+
cwd: packageRoot,
|
|
42
|
+
stdio: 'inherit',
|
|
43
|
+
}
|
|
44
|
+
);
|
|
45
|
+
|
|
46
|
+
if (result.status !== 0) {
|
|
47
|
+
process.exit(result.status || 1);
|
|
48
|
+
}
|
|
49
|
+
}
|