You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Codemods are small scripts that can automate tedious code refactorings. It's like Search & replace, just on steroids, InstUI uses them so breaking changes during major version upgrades are easier for users. Common use cases are renaming imports or props of components.
10
+
11
+
To accomplish this our codemods use [jsCodeshift](https://github.com/facebook/jscodeshift). This tool parses source files to an Abstract Syntax Tree (AST) and gives us an API that can modify it.
12
+
13
+
### Codemod development
14
+
15
+
Our codemods live in the `ui-codemods` package. Each codemod is an ES module with a default export and is executed by jscodeshift. it should have the following code:
16
+
17
+
```ts
18
+
---
19
+
type: code
20
+
---
21
+
importtype { Transform } from'jscodeshift'
22
+
/**
23
+
* @paramfile the file to transform
24
+
* @paramapi jscodeshift instance
25
+
* @paramoptions
26
+
* @paramoptions.filename if `filename` is specified then emitted warnings are
27
+
* written to this file.
28
+
* @paramoptions.usePrettier if `true` the transformed code will be run through
29
+
* [Prettier](https://prettier.io/). You can customize this through a [Prettier
> You can find lots of useful helper functions in `utils/codemodhelpers.ts` and `utils/codemodTypeCheckers.ts`
41
+
42
+
#### Testing codemods
43
+
44
+
You should write unit tests for your codemods. They are tested via test fixtures (sample before transform, sample after transform). To create a unit test for say `sampleCodemod` the following steps are needed:
45
+
46
+
1, create a new test in `__node_tests__/codemod.tests.ts`:
47
+
48
+
```ts
49
+
---
50
+
type: code
51
+
---
52
+
it('tests the cool sampleCodemod', () => {
53
+
runTest(sampleCodemod)
54
+
})
55
+
```
56
+
57
+
2, create a folder named `sampleCodemod` in `__node_tests__/__testfixtures__`
58
+
3, Here create sample input-output file pairs whose filename follows the following naming convention: `[fixtureName].input.[js/ts/tsx]`, `[fixtureName].output.[js/ts/tsx]`. These should be your test cases that ensure that the codemod does the transformation correctly.
59
+
60
+
Done! Run `npm run test:vitest ui-codemods` to run your test.
61
+
62
+
Finally, you should try to run your codemod as users will do:
Copy file name to clipboardExpand all lines: packages/ui-codemods/README.md
+5-87Lines changed: 5 additions & 87 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -4,7 +4,7 @@ category: packages
4
4
5
5
## ui-codemods
6
6
7
-
The ui-codemods package should make it easier to deal with API changes when upgrading Instructure UI libraries.
7
+
The `ui-codemods` package should make it easier to deal with API changes when upgrading Instructure UI libraries.
8
8
9
9
[![npm][npm]][npm-url]
10
10
[![MIT License][license-badge]][license]
@@ -18,102 +18,20 @@ The codemod scripts can be installed via the following command:
18
18
---
19
19
type: code
20
20
---
21
-
npm install @instructure/ui-codemods
21
+
# use here the InstUI version number you are upgrading to
22
+
npm install @instructure/ui-codemods@10
22
23
```
23
24
24
-
The configuration files are located in the [instui-config](#instui-config) package.
25
-
This can be installed via the following command:
26
-
27
-
```sh
28
-
---
29
-
type: code
30
-
---
31
-
npm install @instructure/instui-config
32
-
```
33
-
34
-
### Updating Deprecated Props
35
-
36
-
This codemod helps you update your project by renaming `props` that have had names changed (e.g., `onReady` => `onOpen`).
37
-
38
-
```sh
39
-
---
40
-
type: code
41
-
---
42
-
jscodeshift -t node_modules/@instructure/ui-codemods/lib/updatePropNames.ts <path> --config=node_modules/@instructure/instui-config/codemod-configs/v<version number ex. 5 or 6>/propNames.config.json
43
-
```
44
-
45
-
### Updating Package Imports
46
-
47
-
This codemod helps you update your project by renaming `imports` that have changed (e.g., `instructure-ui` => `@instructure/<package name>`).
48
-
49
-
```sh
50
-
---
51
-
type: code
52
-
---
53
-
jscodeshift -t node_modules/@instructure/ui-codemods/lib/updateImports.ts <path> --config=node_modules/@instructure/instui-config/codemod-configs/v<version number ex. 5 or 6>/imports.config.js
54
-
```
55
-
56
-
### Updating more complex props to the InstUI v8 syntax
57
-
58
-
This codemod upgrades more complex changes like Button, also outputs any manual changes needed to the console. Run this in a InstUI v7 codebase only. This command has an optional fileName parameter, supplying this will append to the given file the warnings.
This codemod addresses breaking changes following a v9 upgrade. Notably, it updates `EmotionThemeProvider` to `InstUISettingsProvider`. Execute this in your project post-upgrade to InstUI v9. Prettier is turned on by default for output formatting, and you can also use the `usePrettier` flag. Additionally, the parser flag can specify the parser for jsx and tsx files.
88
-
89
25
### Codemod for changing the color palette to the v10 color palette
This codemod updates the `canvas` and `canvas-high-contrast` color palettes. Execute this in your project post-upgrade to InstUI v10. Prettier is turned on by default for output formatting, and you can also use the `usePrettier` flag. Additionally, the parser flag can specify the parser for jsx and tsx files.
99
-
100
-
### Codemod for adding a wrapper to ReactDOM.render()
-`fileName`: supplying this will append to the given file the warnings.
114
-
-`wrapperPath`: The import path for the wrapper, default value is '@canvas/react-root'.
115
-
-`wrapperTag`: The tag to wrap render calls in, default is 'Root'.
116
-
-`isDefaultImport`: Is the given tag a default import? Default value is `true`.
34
+
This codemod updates the `canvas` and `canvas-high-contrast` color palettes. Execute this in your project post-upgrade to InstUI v10. Prettier is turned on by default for output formatting, and you can also use the `usePrettier` flag.
0 commit comments