Skip to content

Commit 90747ec

Browse files
committed
chore(ui-codemods): make local themes default
1 parent f85911b commit 90747ec

16 files changed

+724
-17
lines changed

docs/contributor-docs/codemods.md

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ export default myCodemod
4343

4444
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:
4545

46-
1. create a new test in `packages/ui-codemods/lib/__node_tests__/codemod.tests.ts`:
46+
1. Create a new test in `packages/ui-codemods/lib/__node_tests__/codemod.tests.ts`:
4747
```ts
4848
---
4949
type: code
@@ -52,11 +52,33 @@ You should write unit tests for your codemods. They are tested via test fixtures
5252
runTest(sampleCodemod)
5353
})
5454
```
55-
2. create a folder named `sampleCodemod` in `packages/ui-codemods/lib/__node_tests__/__testfixtures__`
55+
2. Create a folder named `sampleCodemod` in `packages/ui-codemods/lib/__node_tests__/__testfixtures__`
5656
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.
5757

5858
Done! Run `npm run test:vitest ui-codemods` to run your test.
5959

60+
#### Testing codemod warnings
61+
62+
Sometimes codemods need to emit warnings to inform users about manual follow-ups or tricky edge cases.
63+
We support testing these warnings using special fixtures and a console.warn spy.
64+
65+
1. In your codemod you can use the `printWarning` from `utils/codemodhelpers.ts` to create warnings.
66+
2. Create new sample input file in the `__testfixtures__` folder, filename follows the following naming convention: `[fixtureName].warning.input.[js/ts/tsx]`:
67+
```ts
68+
---
69+
type: code
70+
---
71+
<MyComponent deprecatedProp="value" />
72+
```
73+
3. Create new sample output file with the array of expected warning messages in the `__testfixtures__` folder, filename follows the following naming convention: `[fixtureName].warning.output.[js/ts/tsx]`:
74+
```ts
75+
---
76+
type: code
77+
---
78+
["Warn for deprecated prop usage.", "Warn for something else."]
79+
```
80+
4. When you run `npm run test:vitest ui-codemods`, your test will pass and ensure your codemod warns as expected.
81+
6082
Finally, you should try to run your codemod as users will do:
6183

6284
```sh
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// @ts-nocheck
2+
3+
import canvasHighContrast from '@instructure/ui-themes'
4+
5+
function asd() {
6+
canvasHighContrast.use()
7+
8+
return <div>asd</div>
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
["Found canvasHighContrast.use(). This will be deleted. Users should wrap each React root in its own InstUISettingsProvider instead."]
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// @ts-nocheck
2+
3+
import canvas from '@instructure/ui-themes'
4+
5+
function asd() {
6+
canvas.use()
7+
8+
return <div>asd</div>
9+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
["Found canvas.use(). This will be deleted. Users should wrap each React root in its own InstUISettingsProvider instead."]
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// @ts-nocheck
2+
3+
import { ThemeRegistry } from '@instructure/theme-registry'
4+
5+
function asd() {
6+
return <div>asd</div>
7+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
["ThemeRegistry has been deleted and there are no global themes anymore."]
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
// @ts-nocheck
2+
3+
import {
4+
canvasThemeLocal,
5+
canvasHighContrastThemeLocal,
6+
somethingElse
7+
} from '@instructure/ui-themes'
8+
import { canvasThemeLocal as theme } from '@instructure/ui-themes'
9+
import { canvasHighContrastThemeLocal as anotherTheme } from '@instructure/ui-themes'
10+
import * as myThemes from '@instructure/ui-themes'
11+
12+
function asd() {
13+
const a = canvasThemeLocal
14+
const b = canvasHighContrastThemeLocal
15+
const c = canvasThemeLocalCustom
16+
const d = myThemes.canvasThemeLocal
17+
const e = myThemes.canvasHighContrastThemeLocal
18+
19+
canvasThemeLocal()
20+
canvasHighContrastThemeLocal()
21+
22+
const { canvasThemeLocal } = myThemes
23+
const { canvasHighContrastThemeLocal } = myThemes
24+
25+
return (
26+
<div>
27+
<InstUISettingsProvider theme={canvasThemeLocal}>
28+
<div>asd</div>
29+
</InstUISettingsProvider>
30+
<InstUISettingsProvider
31+
theme={{
32+
...canvasThemeLocal,
33+
...{
34+
typography: { fontFamily: 'monospace' }
35+
}
36+
}}
37+
>
38+
<div>asd</div>
39+
</InstUISettingsProvider>
40+
<InstUISettingsProvider theme={canvasHighContrastThemeLocal}>
41+
<div>asd</div>
42+
</InstUISettingsProvider>
43+
<InstUISettingsProvider
44+
theme={{
45+
...canvasHighContrastThemeLocal,
46+
...{
47+
typography: { fontFamily: 'monospace' }
48+
}
49+
}}
50+
>
51+
<div>asd</div>
52+
</InstUISettingsProvider>
53+
</div>
54+
)
55+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
// @ts-nocheck
2+
3+
import {
4+
canvasTheme,
5+
canvasHighContrastTheme,
6+
somethingElse
7+
} from '@instructure/ui-themes'
8+
import { canvasTheme as theme } from '@instructure/ui-themes'
9+
import { canvasHighContrastTheme as anotherTheme } from '@instructure/ui-themes'
10+
import * as myThemes from '@instructure/ui-themes'
11+
12+
function asd() {
13+
const a = canvasTheme
14+
const b = canvasHighContrastTheme
15+
const c = canvasThemeLocalCustom
16+
const d = myThemes.canvasTheme
17+
const e = myThemes.canvasHighContrastTheme
18+
19+
canvasTheme()
20+
canvasHighContrastTheme()
21+
22+
const { canvasTheme } = myThemes
23+
const { canvasHighContrastTheme } = myThemes
24+
25+
return (
26+
<div>
27+
<InstUISettingsProvider theme={canvasTheme}>
28+
<div>asd</div>
29+
</InstUISettingsProvider>
30+
<InstUISettingsProvider
31+
theme={{
32+
...canvasTheme,
33+
...{
34+
typography: { fontFamily: 'monospace' }
35+
}
36+
}}
37+
>
38+
<div>asd</div>
39+
</InstUISettingsProvider>
40+
<InstUISettingsProvider theme={canvasHighContrastTheme}>
41+
<div>asd</div>
42+
</InstUISettingsProvider>
43+
<InstUISettingsProvider
44+
theme={{
45+
...canvasHighContrastTheme,
46+
...{
47+
typography: { fontFamily: 'monospace' }
48+
}
49+
}}
50+
>
51+
<div>asd</div>
52+
</InstUISettingsProvider>
53+
</div>
54+
)
55+
}

0 commit comments

Comments
 (0)