@sanity/ui-codemod 1.0.0-alpha.2 → 1.0.0-alpha.4
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/dist/_chunks-es/box.mods.js +4 -2
- package/dist/_chunks-es/box.mods.js.map +1 -1
- package/dist/_chunks-es/layout-mods.js +13 -1
- package/dist/_chunks-es/layout-mods.js.map +1 -1
- package/dist/_chunks-es/transformComponent.js +51 -22
- package/dist/_chunks-es/transformComponent.js.map +1 -1
- package/dist/transforms/latest/box/box.js +133 -18
- package/dist/transforms/latest/box/box.js.map +1 -1
- package/package.json +1 -1
- package/src/constants/latest/layout-mods.ts +12 -0
- package/src/transforms/latest/box/box.test.ts +202 -4
- package/src/transforms/latest/box/box.ts +36 -10
- package/src/utils/getElementMatchNames.ts +7 -0
- package/src/utils/getMappingValue.ts +18 -21
- package/src/utils/getNamedExportInit.ts +111 -0
- package/src/utils/getStaticAttributeExpression.ts +62 -9
- package/src/utils/getStyledComponentAliases.ts +85 -1
- package/src/utils/insertTodoWarning.ts +2 -14
- package/src/utils/parseModule.ts +29 -0
- package/src/utils/replaceElement.test.ts +28 -26
- package/src/utils/replaceElement.ts +4 -5
- package/src/utils/replaceStyledComponent.test.ts +40 -14
- package/src/utils/replaceStyledComponent.ts +2 -5
- package/src/utils/resolveRelativeModulePath.ts +37 -0
- package/src/utils/shouldTransformComponent.test.ts +14 -0
- package/src/utils/shouldTransformComponent.ts +5 -0
- package/src/utils/transformAttributes.test.ts +2 -2
- package/src/utils/transformStyledComponents.test.ts +80 -14
- package/src/utils/transformStyledComponents.ts +7 -16
- package/src/utils/getComponentJsxNames.ts +0 -17
|
@@ -1,13 +1,20 @@
|
|
|
1
|
+
import {mkdtempSync, readFileSync, rmSync, writeFileSync} from 'node:fs'
|
|
2
|
+
import {tmpdir} from 'node:os'
|
|
3
|
+
import {join} from 'node:path'
|
|
4
|
+
|
|
1
5
|
import {type API, type FileInfo} from 'jscodeshift'
|
|
6
|
+
import jscodeshift from 'jscodeshift'
|
|
7
|
+
import {afterEach, describe, expect, it} from 'vitest'
|
|
2
8
|
|
|
3
9
|
import {getAttribute} from './getAttribute'
|
|
4
10
|
import {getComponentLocalNames} from './getComponentLocalNames'
|
|
5
11
|
import {getStyledComponentAliases} from './getStyledComponentAliases'
|
|
12
|
+
import {clearModuleParseCache} from './parseModule'
|
|
6
13
|
import {defineInlineTest} from './testUtils'
|
|
7
14
|
import {transformAttributes} from './transformAttributes'
|
|
8
15
|
import {transformStyledComponents} from './transformStyledComponents'
|
|
9
16
|
|
|
10
|
-
const DEFAULT_WARNING = 'Please double check styled-component migration
|
|
17
|
+
const DEFAULT_WARNING = 'Please double check styled-component migration below'
|
|
11
18
|
const CUSTOM_WARNING =
|
|
12
19
|
'Please double check styled(Card) migration — update to styled(Box) manually if needed'
|
|
13
20
|
|
|
@@ -22,7 +29,7 @@ function transform(
|
|
|
22
29
|
const j = api.jscodeshift
|
|
23
30
|
const root = j(fileInfo.source)
|
|
24
31
|
const localNames = getComponentLocalNames(j, root, 'Card')
|
|
25
|
-
const styledAliases = getStyledComponentAliases(j, root, localNames)
|
|
32
|
+
const styledAliases = getStyledComponentAliases(j, root, 'Card', fileInfo.path, localNames)
|
|
26
33
|
|
|
27
34
|
transformStyledComponents(j, root, styledAliases, (attrs) => !!getAttribute(attrs, 'margin'), {
|
|
28
35
|
warning: options?.warning,
|
|
@@ -111,13 +118,15 @@ defineInlineTest(
|
|
|
111
118
|
import {Card} from '@sanity/ui'
|
|
112
119
|
|
|
113
120
|
function Example() {
|
|
114
|
-
|
|
115
|
-
const RootCard = styled(Card)(({theme}) => ({}));
|
|
121
|
+
const RootCard = styled(Card)(({theme}) => ({}))
|
|
116
122
|
|
|
117
|
-
return
|
|
123
|
+
return (
|
|
124
|
+
// UI-CODEMOD TODO: ${DEFAULT_WARNING}
|
|
125
|
+
<RootCard padding={1} />
|
|
126
|
+
);
|
|
118
127
|
}
|
|
119
128
|
`,
|
|
120
|
-
'adds todo warning on styled
|
|
129
|
+
'adds todo warning on styled component jsx instances when filter does not match',
|
|
121
130
|
)
|
|
122
131
|
|
|
123
132
|
defineInlineTest(
|
|
@@ -136,13 +145,15 @@ defineInlineTest(
|
|
|
136
145
|
import {Card} from '@sanity/ui'
|
|
137
146
|
|
|
138
147
|
function Example() {
|
|
139
|
-
|
|
140
|
-
const RootCard = styled(Card)(({theme}) => ({}));
|
|
148
|
+
const RootCard = styled(Card)(({theme}) => ({}))
|
|
141
149
|
|
|
142
|
-
return
|
|
150
|
+
return (
|
|
151
|
+
// UI-CODEMOD TODO: ${CUSTOM_WARNING}
|
|
152
|
+
<RootCard padding={1} />
|
|
153
|
+
);
|
|
143
154
|
}
|
|
144
155
|
`,
|
|
145
|
-
'adds custom todo warning on styled
|
|
156
|
+
'adds custom todo warning on styled component jsx instances when filter does not match',
|
|
146
157
|
)
|
|
147
158
|
|
|
148
159
|
defineInlineTest(
|
|
@@ -152,21 +163,76 @@ defineInlineTest(
|
|
|
152
163
|
import {Card} from '@sanity/ui'
|
|
153
164
|
|
|
154
165
|
function Example() {
|
|
155
|
-
// UI-CODEMOD TODO: ${DEFAULT_WARNING}
|
|
156
166
|
const RootCard = styled(Card)(({theme}) => ({}))
|
|
157
167
|
|
|
158
|
-
return
|
|
168
|
+
return (
|
|
169
|
+
// UI-CODEMOD TODO: ${DEFAULT_WARNING}
|
|
170
|
+
<RootCard padding={1} />
|
|
171
|
+
);
|
|
159
172
|
}
|
|
160
173
|
`,
|
|
161
174
|
`
|
|
162
175
|
import {Card} from '@sanity/ui'
|
|
163
176
|
|
|
164
177
|
function Example() {
|
|
165
|
-
// UI-CODEMOD TODO: ${DEFAULT_WARNING}
|
|
166
178
|
const RootCard = styled(Card)(({theme}) => ({}))
|
|
167
179
|
|
|
168
|
-
return
|
|
180
|
+
return (
|
|
181
|
+
// UI-CODEMOD TODO: ${DEFAULT_WARNING}
|
|
182
|
+
<RootCard padding={1} />
|
|
183
|
+
);
|
|
169
184
|
}
|
|
170
185
|
`,
|
|
171
186
|
'does not duplicate todo warning',
|
|
172
187
|
)
|
|
188
|
+
|
|
189
|
+
describe('imported styled aliases', () => {
|
|
190
|
+
const j = jscodeshift
|
|
191
|
+
const tempDirs: string[] = []
|
|
192
|
+
|
|
193
|
+
afterEach(() => {
|
|
194
|
+
clearModuleParseCache()
|
|
195
|
+
|
|
196
|
+
for (const dir of tempDirs.splice(0)) {
|
|
197
|
+
rmSync(dir, {recursive: true, force: true})
|
|
198
|
+
}
|
|
199
|
+
})
|
|
200
|
+
|
|
201
|
+
it('adds todo warning on styled component jsx instances when filter does not match', () => {
|
|
202
|
+
const dir = mkdtempSync(join(tmpdir(), 'ui-codemod-styled-crossfile-'))
|
|
203
|
+
|
|
204
|
+
tempDirs.push(dir)
|
|
205
|
+
|
|
206
|
+
writeFileSync(
|
|
207
|
+
join(dir, 'Component.styled.tsx'),
|
|
208
|
+
`
|
|
209
|
+
import {Card} from '@sanity/ui'
|
|
210
|
+
|
|
211
|
+
export const RootCard = styled(Card)(({theme}) => ({}))
|
|
212
|
+
`,
|
|
213
|
+
)
|
|
214
|
+
|
|
215
|
+
writeFileSync(
|
|
216
|
+
join(dir, 'Component.tsx'),
|
|
217
|
+
`
|
|
218
|
+
import {RootCard} from './Component.styled'
|
|
219
|
+
|
|
220
|
+
export function Component() {
|
|
221
|
+
return <RootCard padding={1} />
|
|
222
|
+
}
|
|
223
|
+
`,
|
|
224
|
+
)
|
|
225
|
+
|
|
226
|
+
const importerPath = join(dir, 'Component.tsx')
|
|
227
|
+
const root = j(readFileSync(importerPath, 'utf8'))
|
|
228
|
+
const localNames = getComponentLocalNames(j, root, 'Card')
|
|
229
|
+
const styledAliases = getStyledComponentAliases(j, root, 'Card', importerPath, localNames)
|
|
230
|
+
|
|
231
|
+
transformStyledComponents(j, root, styledAliases, (attrs) => !!getAttribute(attrs, 'margin'), {
|
|
232
|
+
warning: DEFAULT_WARNING,
|
|
233
|
+
})
|
|
234
|
+
|
|
235
|
+
expect(root.toSource()).toContain(`UI-CODEMOD TODO: ${DEFAULT_WARNING}`)
|
|
236
|
+
expect(root.toSource()).not.toContain('const RootCard = styled(Card)')
|
|
237
|
+
})
|
|
238
|
+
})
|
|
@@ -2,11 +2,11 @@ import type {API, ASTPath, JSXAttribute, JSXOpeningElement, JSXSpreadAttribute}
|
|
|
2
2
|
|
|
3
3
|
import {insertTodoWarning} from './insertTodoWarning'
|
|
4
4
|
|
|
5
|
-
const DEFAULT_WARNING = 'Please double check styled-component migration
|
|
5
|
+
const DEFAULT_WARNING = 'Please double check styled-component migration below'
|
|
6
6
|
|
|
7
7
|
/**
|
|
8
8
|
* Runs callback on JSX styled-component if `filter` passes. Otherwise, adds
|
|
9
|
-
* a TODO on the
|
|
9
|
+
* a TODO on the component JSX instance when manual review is needed.
|
|
10
10
|
* Returns whether the AST was updated.
|
|
11
11
|
*/
|
|
12
12
|
export function transformStyledComponents(
|
|
@@ -21,7 +21,6 @@ export function transformStyledComponents(
|
|
|
21
21
|
): boolean {
|
|
22
22
|
const names = new Set(aliases)
|
|
23
23
|
const {callback, warning = DEFAULT_WARNING} = options
|
|
24
|
-
const aliasesToWarn = new Set<string>()
|
|
25
24
|
let hasChanges = false
|
|
26
25
|
|
|
27
26
|
root.find(j.JSXOpeningElement).forEach((path) => {
|
|
@@ -34,23 +33,15 @@ export function transformStyledComponents(
|
|
|
34
33
|
const attrs = path.node.attributes ?? []
|
|
35
34
|
|
|
36
35
|
if (!filter(attrs)) {
|
|
37
|
-
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
if (filter(attrs)) {
|
|
41
|
-
if (callback?.(path)) {
|
|
36
|
+
if (insertTodoWarning(j, path, warning)) {
|
|
42
37
|
hasChanges = true
|
|
43
38
|
}
|
|
44
|
-
}
|
|
45
|
-
})
|
|
46
39
|
|
|
47
|
-
|
|
48
|
-
|
|
40
|
+
return
|
|
41
|
+
}
|
|
49
42
|
|
|
50
|
-
if (
|
|
51
|
-
|
|
52
|
-
hasChanges = true
|
|
53
|
-
}
|
|
43
|
+
if (callback?.(path)) {
|
|
44
|
+
hasChanges = true
|
|
54
45
|
}
|
|
55
46
|
})
|
|
56
47
|
|
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
import type {API, Collection} from 'jscodeshift'
|
|
2
|
-
|
|
3
|
-
import type {BaseOptions} from '../types/BaseOptions'
|
|
4
|
-
import {getComponentLocalNames} from './getComponentLocalNames'
|
|
5
|
-
import {getStyledComponentAliases} from './getStyledComponentAliases'
|
|
6
|
-
|
|
7
|
-
export function getComponentJsxNames(
|
|
8
|
-
j: API['jscodeshift'],
|
|
9
|
-
root: Collection,
|
|
10
|
-
componentName: string,
|
|
11
|
-
options?: BaseOptions,
|
|
12
|
-
): Set<string> {
|
|
13
|
-
const localNames = getComponentLocalNames(j, root, componentName, options)
|
|
14
|
-
const styledAliases = getStyledComponentAliases(j, root, localNames)
|
|
15
|
-
|
|
16
|
-
return new Set([...localNames, ...styledAliases])
|
|
17
|
-
}
|