@sanity/ui-codemod 1.0.0-alpha.4 → 1.0.0-alpha.6
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/addAttribute.js +1 -1
- package/dist/_chunks-es/box.mods.js +1 -1
- package/dist/_chunks-es/layout-mods.js +4 -0
- package/dist/_chunks-es/layout-mods.js.map +1 -1
- package/dist/_chunks-es/{transformComponent.js → transformStyledComponents.js} +150 -2
- package/dist/_chunks-es/transformStyledComponents.js.map +1 -0
- package/dist/transforms/latest/box/box.js +1 -147
- package/dist/transforms/latest/box/box.js.map +1 -1
- package/dist/transforms/latest/card/card.js +24 -9
- package/dist/transforms/latest/card/card.js.map +1 -1
- package/dist/transforms/latest/code/code.js +18 -7
- package/dist/transforms/latest/code/code.js.map +1 -1
- package/dist/transforms/latest/container/container.js +15 -7
- package/dist/transforms/latest/container/container.js.map +1 -1
- package/dist/transforms/latest/flex/flex.js +15 -7
- package/dist/transforms/latest/flex/flex.js.map +1 -1
- package/dist/transforms/latest/grid/grid.js +15 -7
- package/dist/transforms/latest/grid/grid.js.map +1 -1
- package/dist/transforms/latest/heading/heading.js +18 -7
- package/dist/transforms/latest/heading/heading.js.map +1 -1
- package/dist/transforms/latest/text/text.js +18 -7
- package/dist/transforms/latest/text/text.js.map +1 -1
- package/package.json +1 -1
- package/src/constants/latest/layout-mods.ts +4 -0
- package/src/transforms/latest/box/box.test.ts +86 -151
- package/src/transforms/latest/card/card.test.ts +100 -1
- package/src/transforms/latest/card/card.ts +42 -19
- package/src/transforms/latest/code/code.test.ts +84 -1
- package/src/transforms/latest/code/code.ts +47 -16
- package/src/transforms/latest/container/container.test.ts +78 -1
- package/src/transforms/latest/container/container.ts +29 -9
- package/src/transforms/latest/flex/flex.test.ts +78 -1
- package/src/transforms/latest/flex/flex.ts +29 -9
- package/src/transforms/latest/grid/grid.test.ts +78 -1
- package/src/transforms/latest/grid/grid.ts +29 -9
- package/src/transforms/latest/heading/heading.test.ts +84 -1
- package/src/transforms/latest/heading/heading.ts +47 -16
- package/src/transforms/latest/inline/inline.test.ts +78 -1
- package/src/transforms/latest/inline/inline.ts +29 -9
- package/src/transforms/latest/label/label.test.ts +84 -1
- package/src/transforms/latest/label/label.ts +36 -1
- package/src/transforms/latest/stack/stack.test.ts +100 -1
- package/src/transforms/latest/stack/stack.ts +29 -7
- package/src/transforms/latest/text/text.test.ts +84 -1
- package/src/transforms/latest/text/text.ts +58 -19
- package/src/utils/testUtils.ts +45 -0
- package/dist/_chunks-es/transformComponent.js.map +0 -1
|
@@ -3,10 +3,12 @@ import {type API, type FileInfo} from 'jscodeshift'
|
|
|
3
3
|
import type {BaseOptions} from '../../../types/BaseOptions'
|
|
4
4
|
import {addAttribute} from '../../../utils/addAttribute'
|
|
5
5
|
import {getComponentLocalNames} from '../../../utils/getComponentLocalNames'
|
|
6
|
+
import {getStyledComponentAliases} from '../../../utils/getStyledComponentAliases'
|
|
6
7
|
import {shouldTransformComponent} from '../../../utils/shouldTransformComponent'
|
|
7
8
|
import {transformAttributes} from '../../../utils/transformAttributes'
|
|
8
9
|
import {transformComponent} from '../../../utils/transformComponent'
|
|
9
10
|
import {transformImport} from '../../../utils/transformImport'
|
|
11
|
+
import {transformStyledComponents} from '../../../utils/transformStyledComponents'
|
|
10
12
|
import {CODE_MODS} from './code.mods'
|
|
11
13
|
|
|
12
14
|
const TODO_WARNING = 'Please double check the Code migration below'
|
|
@@ -21,8 +23,16 @@ export default function transform(
|
|
|
21
23
|
|
|
22
24
|
return transformComponent(fileInfo, api, ({j, root, markChanged}) => {
|
|
23
25
|
const localNames = getComponentLocalNames(j, root, 'Code', options)
|
|
26
|
+
const styledAliases = getStyledComponentAliases(
|
|
27
|
+
j,
|
|
28
|
+
root,
|
|
29
|
+
'Code',
|
|
30
|
+
fileInfo.path,
|
|
31
|
+
localNames,
|
|
32
|
+
options,
|
|
33
|
+
)
|
|
24
34
|
|
|
25
|
-
if (!shouldTransformComponent(j, root, 'Code', localNames, options)) {
|
|
35
|
+
if (!shouldTransformComponent(j, root, 'Code', localNames, options, styledAliases)) {
|
|
26
36
|
return
|
|
27
37
|
}
|
|
28
38
|
|
|
@@ -30,24 +40,45 @@ export default function transform(
|
|
|
30
40
|
markChanged()
|
|
31
41
|
}
|
|
32
42
|
|
|
33
|
-
root
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
.
|
|
38
|
-
|
|
43
|
+
root.find(j.JSXOpeningElement).forEach((path) => {
|
|
44
|
+
let changed = false
|
|
45
|
+
const name = path.node.name
|
|
46
|
+
|
|
47
|
+
if (name.type !== 'JSXIdentifier' || !localNames.has(name.name)) {
|
|
48
|
+
return
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
if (transformAttributes(j, path, CODE_MODS, TODO_WARNING)) {
|
|
52
|
+
changed = true
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
if (addAttribute(j, path.node, 'trim', true)) {
|
|
56
|
+
changed = true
|
|
57
|
+
}
|
|
39
58
|
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
59
|
+
if (changed) {
|
|
60
|
+
markChanged()
|
|
61
|
+
}
|
|
62
|
+
})
|
|
43
63
|
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
64
|
+
if (
|
|
65
|
+
transformStyledComponents(j, root, styledAliases, () => true, {
|
|
66
|
+
callback: (path) => {
|
|
67
|
+
let changed = false
|
|
47
68
|
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
69
|
+
if (transformAttributes(j, path, CODE_MODS, TODO_WARNING)) {
|
|
70
|
+
changed = true
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
if (addAttribute(j, path.node, 'trim', true)) {
|
|
74
|
+
changed = true
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
return changed
|
|
78
|
+
},
|
|
51
79
|
})
|
|
80
|
+
) {
|
|
81
|
+
markChanged()
|
|
82
|
+
}
|
|
52
83
|
})
|
|
53
84
|
}
|
|
@@ -1,4 +1,6 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {expect} from 'vitest'
|
|
2
|
+
|
|
3
|
+
import {defineCrossFileTest, defineInlineTest} from '../../../utils/testUtils'
|
|
2
4
|
import transform from './container'
|
|
3
5
|
|
|
4
6
|
defineInlineTest(
|
|
@@ -103,3 +105,78 @@ defineInlineTest(
|
|
|
103
105
|
`,
|
|
104
106
|
'moves grid props to style and updates mapped values',
|
|
105
107
|
)
|
|
108
|
+
|
|
109
|
+
defineCrossFileTest(
|
|
110
|
+
transform,
|
|
111
|
+
{},
|
|
112
|
+
`
|
|
113
|
+
import {Container} from '@sanity/ui'
|
|
114
|
+
|
|
115
|
+
export const RootContainer = styled(Container)(({theme}) => ({}))
|
|
116
|
+
`,
|
|
117
|
+
`
|
|
118
|
+
import {RootContainer} from './Component.styled'
|
|
119
|
+
|
|
120
|
+
export function Component() {
|
|
121
|
+
return <RootContainer width={2} />
|
|
122
|
+
}
|
|
123
|
+
`,
|
|
124
|
+
(output) => {
|
|
125
|
+
expect(output).toContain('<RootContainer size={2} />')
|
|
126
|
+
},
|
|
127
|
+
'transforms attributes on imported styled Container wrappers',
|
|
128
|
+
)
|
|
129
|
+
|
|
130
|
+
defineCrossFileTest(
|
|
131
|
+
transform,
|
|
132
|
+
{},
|
|
133
|
+
`
|
|
134
|
+
import {Container} from '@sanity/ui'
|
|
135
|
+
|
|
136
|
+
export const RootContainer = styled(Container)(({theme}) => ({}))
|
|
137
|
+
`,
|
|
138
|
+
`
|
|
139
|
+
import {Container} from 'another-package'
|
|
140
|
+
import {RootContainer} from './Component.styled'
|
|
141
|
+
|
|
142
|
+
export function Component() {
|
|
143
|
+
return (
|
|
144
|
+
<>
|
|
145
|
+
<RootContainer width={2} />
|
|
146
|
+
<Container width={2} />
|
|
147
|
+
</>
|
|
148
|
+
)
|
|
149
|
+
}
|
|
150
|
+
`,
|
|
151
|
+
(output) => {
|
|
152
|
+
expect(output).toContain('<RootContainer size={2} />')
|
|
153
|
+
expect(output).toContain('<Container width={2} />')
|
|
154
|
+
},
|
|
155
|
+
'does not transform attributes on unrelated Container from another package',
|
|
156
|
+
)
|
|
157
|
+
|
|
158
|
+
defineCrossFileTest(
|
|
159
|
+
transform,
|
|
160
|
+
{},
|
|
161
|
+
`
|
|
162
|
+
import {Container} from '@sanity/ui'
|
|
163
|
+
|
|
164
|
+
export const RootContainer = styled(Container)(({theme}) => ({}))
|
|
165
|
+
`,
|
|
166
|
+
`
|
|
167
|
+
import {RootContainer} from './index'
|
|
168
|
+
|
|
169
|
+
export function Component() {
|
|
170
|
+
return <RootContainer width={2} />
|
|
171
|
+
}
|
|
172
|
+
`,
|
|
173
|
+
(output) => {
|
|
174
|
+
expect(output).toContain('<RootContainer size={2} />')
|
|
175
|
+
},
|
|
176
|
+
'transforms styled Container wrappers imported through barrel re-exports',
|
|
177
|
+
{
|
|
178
|
+
extraFiles: {
|
|
179
|
+
'index.ts': `export {RootContainer} from './Component.styled'`,
|
|
180
|
+
},
|
|
181
|
+
},
|
|
182
|
+
)
|
|
@@ -2,10 +2,12 @@ import {type API, type FileInfo} from 'jscodeshift'
|
|
|
2
2
|
|
|
3
3
|
import type {BaseOptions} from '../../../types/BaseOptions'
|
|
4
4
|
import {getComponentLocalNames} from '../../../utils/getComponentLocalNames'
|
|
5
|
+
import {getStyledComponentAliases} from '../../../utils/getStyledComponentAliases'
|
|
5
6
|
import {shouldTransformComponent} from '../../../utils/shouldTransformComponent'
|
|
6
7
|
import {transformAttributes} from '../../../utils/transformAttributes'
|
|
7
8
|
import {transformComponent} from '../../../utils/transformComponent'
|
|
8
9
|
import {transformImport} from '../../../utils/transformImport'
|
|
10
|
+
import {transformStyledComponents} from '../../../utils/transformStyledComponents'
|
|
9
11
|
import {CONTAINER_MODS} from './container.mods'
|
|
10
12
|
|
|
11
13
|
const TODO_WARNING = 'Please double check the Container migration below'
|
|
@@ -20,8 +22,16 @@ export default function transform(
|
|
|
20
22
|
|
|
21
23
|
return transformComponent(fileInfo, api, ({j, root, markChanged}) => {
|
|
22
24
|
const localNames = getComponentLocalNames(j, root, 'Container', options)
|
|
25
|
+
const styledAliases = getStyledComponentAliases(
|
|
26
|
+
j,
|
|
27
|
+
root,
|
|
28
|
+
'Container',
|
|
29
|
+
fileInfo.path,
|
|
30
|
+
localNames,
|
|
31
|
+
options,
|
|
32
|
+
)
|
|
23
33
|
|
|
24
|
-
if (!shouldTransformComponent(j, root, 'Container', localNames, options)) {
|
|
34
|
+
if (!shouldTransformComponent(j, root, 'Container', localNames, options, styledAliases)) {
|
|
25
35
|
return
|
|
26
36
|
}
|
|
27
37
|
|
|
@@ -29,14 +39,24 @@ export default function transform(
|
|
|
29
39
|
markChanged()
|
|
30
40
|
}
|
|
31
41
|
|
|
32
|
-
root
|
|
33
|
-
.
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
42
|
+
root.find(j.JSXOpeningElement).forEach((path) => {
|
|
43
|
+
const name = path.node.name
|
|
44
|
+
|
|
45
|
+
if (name.type !== 'JSXIdentifier' || !localNames.has(name.name)) {
|
|
46
|
+
return
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
if (transformAttributes(j, path, CONTAINER_MODS, TODO_WARNING)) {
|
|
50
|
+
markChanged()
|
|
51
|
+
}
|
|
52
|
+
})
|
|
53
|
+
|
|
54
|
+
if (
|
|
55
|
+
transformStyledComponents(j, root, styledAliases, () => true, {
|
|
56
|
+
callback: (path) => transformAttributes(j, path, CONTAINER_MODS, TODO_WARNING),
|
|
40
57
|
})
|
|
58
|
+
) {
|
|
59
|
+
markChanged()
|
|
60
|
+
}
|
|
41
61
|
})
|
|
42
62
|
}
|
|
@@ -1,4 +1,6 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {expect} from 'vitest'
|
|
2
|
+
|
|
3
|
+
import {defineCrossFileTest, defineInlineTest} from '../../../utils/testUtils'
|
|
2
4
|
import transform from './flex'
|
|
3
5
|
|
|
4
6
|
defineInlineTest(
|
|
@@ -78,3 +80,78 @@ defineInlineTest(
|
|
|
78
80
|
`,
|
|
79
81
|
'moves grid props to style and updates mapped values',
|
|
80
82
|
)
|
|
83
|
+
|
|
84
|
+
defineCrossFileTest(
|
|
85
|
+
transform,
|
|
86
|
+
{},
|
|
87
|
+
`
|
|
88
|
+
import {Flex} from '@sanity/ui'
|
|
89
|
+
|
|
90
|
+
export const RootFlex = styled(Flex)(({theme}) => ({}))
|
|
91
|
+
`,
|
|
92
|
+
`
|
|
93
|
+
import {RootFlex} from './Component.styled'
|
|
94
|
+
|
|
95
|
+
export function Component() {
|
|
96
|
+
return <RootFlex align="center" />
|
|
97
|
+
}
|
|
98
|
+
`,
|
|
99
|
+
(output) => {
|
|
100
|
+
expect(output).toContain('<RootFlex alignItems="center" />')
|
|
101
|
+
},
|
|
102
|
+
'transforms attributes on imported styled Flex wrappers',
|
|
103
|
+
)
|
|
104
|
+
|
|
105
|
+
defineCrossFileTest(
|
|
106
|
+
transform,
|
|
107
|
+
{},
|
|
108
|
+
`
|
|
109
|
+
import {Flex} from '@sanity/ui'
|
|
110
|
+
|
|
111
|
+
export const RootFlex = styled(Flex)(({theme}) => ({}))
|
|
112
|
+
`,
|
|
113
|
+
`
|
|
114
|
+
import {Flex} from 'another-package'
|
|
115
|
+
import {RootFlex} from './Component.styled'
|
|
116
|
+
|
|
117
|
+
export function Component() {
|
|
118
|
+
return (
|
|
119
|
+
<>
|
|
120
|
+
<RootFlex align="center" />
|
|
121
|
+
<Flex direction="column" />
|
|
122
|
+
</>
|
|
123
|
+
)
|
|
124
|
+
}
|
|
125
|
+
`,
|
|
126
|
+
(output) => {
|
|
127
|
+
expect(output).toContain('<RootFlex alignItems="center" />')
|
|
128
|
+
expect(output).toContain('<Flex direction="column" />')
|
|
129
|
+
},
|
|
130
|
+
'does not transform attributes on unrelated Flex from another package',
|
|
131
|
+
)
|
|
132
|
+
|
|
133
|
+
defineCrossFileTest(
|
|
134
|
+
transform,
|
|
135
|
+
{},
|
|
136
|
+
`
|
|
137
|
+
import {Flex} from '@sanity/ui'
|
|
138
|
+
|
|
139
|
+
export const RootFlex = styled(Flex)(({theme}) => ({}))
|
|
140
|
+
`,
|
|
141
|
+
`
|
|
142
|
+
import {RootFlex} from './index'
|
|
143
|
+
|
|
144
|
+
export function Component() {
|
|
145
|
+
return <RootFlex align="center" />
|
|
146
|
+
}
|
|
147
|
+
`,
|
|
148
|
+
(output) => {
|
|
149
|
+
expect(output).toContain('<RootFlex alignItems="center" />')
|
|
150
|
+
},
|
|
151
|
+
'transforms styled Flex wrappers imported through barrel re-exports',
|
|
152
|
+
{
|
|
153
|
+
extraFiles: {
|
|
154
|
+
'index.ts': `export {RootFlex} from './Component.styled'`,
|
|
155
|
+
},
|
|
156
|
+
},
|
|
157
|
+
)
|
|
@@ -2,10 +2,12 @@ import {type API, type FileInfo} from 'jscodeshift'
|
|
|
2
2
|
|
|
3
3
|
import type {BaseOptions} from '../../../types/BaseOptions'
|
|
4
4
|
import {getComponentLocalNames} from '../../../utils/getComponentLocalNames'
|
|
5
|
+
import {getStyledComponentAliases} from '../../../utils/getStyledComponentAliases'
|
|
5
6
|
import {shouldTransformComponent} from '../../../utils/shouldTransformComponent'
|
|
6
7
|
import {transformAttributes} from '../../../utils/transformAttributes'
|
|
7
8
|
import {transformComponent} from '../../../utils/transformComponent'
|
|
8
9
|
import {transformImport} from '../../../utils/transformImport'
|
|
10
|
+
import {transformStyledComponents} from '../../../utils/transformStyledComponents'
|
|
9
11
|
import {FLEX_MODS} from './flex.mods'
|
|
10
12
|
|
|
11
13
|
const TODO_WARNING = 'Please double check the Flex migration below'
|
|
@@ -20,8 +22,16 @@ export default function transform(
|
|
|
20
22
|
|
|
21
23
|
return transformComponent(fileInfo, api, ({j, root, markChanged}) => {
|
|
22
24
|
const localNames = getComponentLocalNames(j, root, 'Flex', options)
|
|
25
|
+
const styledAliases = getStyledComponentAliases(
|
|
26
|
+
j,
|
|
27
|
+
root,
|
|
28
|
+
'Flex',
|
|
29
|
+
fileInfo.path,
|
|
30
|
+
localNames,
|
|
31
|
+
options,
|
|
32
|
+
)
|
|
23
33
|
|
|
24
|
-
if (!shouldTransformComponent(j, root, 'Flex', localNames, options)) {
|
|
34
|
+
if (!shouldTransformComponent(j, root, 'Flex', localNames, options, styledAliases)) {
|
|
25
35
|
return
|
|
26
36
|
}
|
|
27
37
|
|
|
@@ -29,14 +39,24 @@ export default function transform(
|
|
|
29
39
|
markChanged()
|
|
30
40
|
}
|
|
31
41
|
|
|
32
|
-
root
|
|
33
|
-
.
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
42
|
+
root.find(j.JSXOpeningElement).forEach((path) => {
|
|
43
|
+
const name = path.node.name
|
|
44
|
+
|
|
45
|
+
if (name.type !== 'JSXIdentifier' || !localNames.has(name.name)) {
|
|
46
|
+
return
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
if (transformAttributes(j, path, FLEX_MODS, TODO_WARNING)) {
|
|
50
|
+
markChanged()
|
|
51
|
+
}
|
|
52
|
+
})
|
|
53
|
+
|
|
54
|
+
if (
|
|
55
|
+
transformStyledComponents(j, root, styledAliases, () => true, {
|
|
56
|
+
callback: (path) => transformAttributes(j, path, FLEX_MODS, TODO_WARNING),
|
|
40
57
|
})
|
|
58
|
+
) {
|
|
59
|
+
markChanged()
|
|
60
|
+
}
|
|
41
61
|
})
|
|
42
62
|
}
|
|
@@ -1,4 +1,6 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {expect} from 'vitest'
|
|
2
|
+
|
|
3
|
+
import {defineCrossFileTest, defineInlineTest} from '../../../utils/testUtils'
|
|
2
4
|
import transform from './grid'
|
|
3
5
|
|
|
4
6
|
defineInlineTest(
|
|
@@ -95,3 +97,78 @@ defineInlineTest(
|
|
|
95
97
|
`,
|
|
96
98
|
'updates v3 grid props mapped values',
|
|
97
99
|
)
|
|
100
|
+
|
|
101
|
+
defineCrossFileTest(
|
|
102
|
+
transform,
|
|
103
|
+
{},
|
|
104
|
+
`
|
|
105
|
+
import {Grid} from '@sanity/ui'
|
|
106
|
+
|
|
107
|
+
export const RootGrid = styled(Grid)(({theme}) => ({}))
|
|
108
|
+
`,
|
|
109
|
+
`
|
|
110
|
+
import {RootGrid} from './Component.styled'
|
|
111
|
+
|
|
112
|
+
export function Component() {
|
|
113
|
+
return <RootGrid autoCols="auto" />
|
|
114
|
+
}
|
|
115
|
+
`,
|
|
116
|
+
(output) => {
|
|
117
|
+
expect(output).toContain('<RootGrid gridAutoColumns="auto" />')
|
|
118
|
+
},
|
|
119
|
+
'transforms attributes on imported styled Grid wrappers',
|
|
120
|
+
)
|
|
121
|
+
|
|
122
|
+
defineCrossFileTest(
|
|
123
|
+
transform,
|
|
124
|
+
{},
|
|
125
|
+
`
|
|
126
|
+
import {Grid} from '@sanity/ui'
|
|
127
|
+
|
|
128
|
+
export const RootGrid = styled(Grid)(({theme}) => ({}))
|
|
129
|
+
`,
|
|
130
|
+
`
|
|
131
|
+
import {Grid} from 'another-package'
|
|
132
|
+
import {RootGrid} from './Component.styled'
|
|
133
|
+
|
|
134
|
+
export function Component() {
|
|
135
|
+
return (
|
|
136
|
+
<>
|
|
137
|
+
<RootGrid autoCols="auto" />
|
|
138
|
+
<Grid autoCols="auto" />
|
|
139
|
+
</>
|
|
140
|
+
)
|
|
141
|
+
}
|
|
142
|
+
`,
|
|
143
|
+
(output) => {
|
|
144
|
+
expect(output).toContain('<RootGrid gridAutoColumns="auto" />')
|
|
145
|
+
expect(output).toContain('<Grid autoCols="auto" />')
|
|
146
|
+
},
|
|
147
|
+
'does not transform attributes on unrelated Grid from another package',
|
|
148
|
+
)
|
|
149
|
+
|
|
150
|
+
defineCrossFileTest(
|
|
151
|
+
transform,
|
|
152
|
+
{},
|
|
153
|
+
`
|
|
154
|
+
import {Grid} from '@sanity/ui'
|
|
155
|
+
|
|
156
|
+
export const RootGrid = styled(Grid)(({theme}) => ({}))
|
|
157
|
+
`,
|
|
158
|
+
`
|
|
159
|
+
import {RootGrid} from './index'
|
|
160
|
+
|
|
161
|
+
export function Component() {
|
|
162
|
+
return <RootGrid autoCols="auto" />
|
|
163
|
+
}
|
|
164
|
+
`,
|
|
165
|
+
(output) => {
|
|
166
|
+
expect(output).toContain('<RootGrid gridAutoColumns="auto" />')
|
|
167
|
+
},
|
|
168
|
+
'transforms styled Grid wrappers imported through barrel re-exports',
|
|
169
|
+
{
|
|
170
|
+
extraFiles: {
|
|
171
|
+
'index.ts': `export {RootGrid} from './Component.styled'`,
|
|
172
|
+
},
|
|
173
|
+
},
|
|
174
|
+
)
|
|
@@ -2,10 +2,12 @@ import {type API, type FileInfo} from 'jscodeshift'
|
|
|
2
2
|
|
|
3
3
|
import type {BaseOptions} from '../../../types/BaseOptions'
|
|
4
4
|
import {getComponentLocalNames} from '../../../utils/getComponentLocalNames'
|
|
5
|
+
import {getStyledComponentAliases} from '../../../utils/getStyledComponentAliases'
|
|
5
6
|
import {shouldTransformComponent} from '../../../utils/shouldTransformComponent'
|
|
6
7
|
import {transformAttributes} from '../../../utils/transformAttributes'
|
|
7
8
|
import {transformComponent} from '../../../utils/transformComponent'
|
|
8
9
|
import {transformImport} from '../../../utils/transformImport'
|
|
10
|
+
import {transformStyledComponents} from '../../../utils/transformStyledComponents'
|
|
9
11
|
import {GRID_MODS} from './grid.mods'
|
|
10
12
|
|
|
11
13
|
const TODO_WARNING = 'Please double check the Grid migration below'
|
|
@@ -20,8 +22,16 @@ export default function transform(
|
|
|
20
22
|
|
|
21
23
|
return transformComponent(fileInfo, api, ({j, root, markChanged}) => {
|
|
22
24
|
const localNames = getComponentLocalNames(j, root, 'Grid', options)
|
|
25
|
+
const styledAliases = getStyledComponentAliases(
|
|
26
|
+
j,
|
|
27
|
+
root,
|
|
28
|
+
'Grid',
|
|
29
|
+
fileInfo.path,
|
|
30
|
+
localNames,
|
|
31
|
+
options,
|
|
32
|
+
)
|
|
23
33
|
|
|
24
|
-
if (!shouldTransformComponent(j, root, 'Grid', localNames, options)) {
|
|
34
|
+
if (!shouldTransformComponent(j, root, 'Grid', localNames, options, styledAliases)) {
|
|
25
35
|
return
|
|
26
36
|
}
|
|
27
37
|
|
|
@@ -29,14 +39,24 @@ export default function transform(
|
|
|
29
39
|
markChanged()
|
|
30
40
|
}
|
|
31
41
|
|
|
32
|
-
root
|
|
33
|
-
.
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
42
|
+
root.find(j.JSXOpeningElement).forEach((path) => {
|
|
43
|
+
const name = path.node.name
|
|
44
|
+
|
|
45
|
+
if (name.type !== 'JSXIdentifier' || !localNames.has(name.name)) {
|
|
46
|
+
return
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
if (transformAttributes(j, path, GRID_MODS, TODO_WARNING)) {
|
|
50
|
+
markChanged()
|
|
51
|
+
}
|
|
52
|
+
})
|
|
53
|
+
|
|
54
|
+
if (
|
|
55
|
+
transformStyledComponents(j, root, styledAliases, () => true, {
|
|
56
|
+
callback: (path) => transformAttributes(j, path, GRID_MODS, TODO_WARNING),
|
|
40
57
|
})
|
|
58
|
+
) {
|
|
59
|
+
markChanged()
|
|
60
|
+
}
|
|
41
61
|
})
|
|
42
62
|
}
|
|
@@ -1,4 +1,6 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {expect} from 'vitest'
|
|
2
|
+
|
|
3
|
+
import {defineCrossFileTest, defineInlineTest} from '../../../utils/testUtils'
|
|
2
4
|
import transform from './heading'
|
|
3
5
|
|
|
4
6
|
defineInlineTest(
|
|
@@ -85,3 +87,84 @@ defineInlineTest(
|
|
|
85
87
|
`,
|
|
86
88
|
'warns if as props is missing',
|
|
87
89
|
)
|
|
90
|
+
|
|
91
|
+
defineCrossFileTest(
|
|
92
|
+
transform,
|
|
93
|
+
{},
|
|
94
|
+
`
|
|
95
|
+
import {Heading} from '@sanity/ui'
|
|
96
|
+
|
|
97
|
+
export const RootHeading = styled(Heading)(({theme}) => ({}))
|
|
98
|
+
`,
|
|
99
|
+
`
|
|
100
|
+
import {RootHeading} from './Component.styled'
|
|
101
|
+
|
|
102
|
+
export function RootCode() {
|
|
103
|
+
return <RootHeading flex="auto" />
|
|
104
|
+
}
|
|
105
|
+
`,
|
|
106
|
+
(output) => {
|
|
107
|
+
expect(output.replace(/\s+/g, ' ')).toContain(
|
|
108
|
+
'<RootHeading style={{ flex: "1 1 auto" }} trim={true} />',
|
|
109
|
+
)
|
|
110
|
+
},
|
|
111
|
+
'transforms attributes on imported styled Heading wrappers',
|
|
112
|
+
)
|
|
113
|
+
|
|
114
|
+
defineCrossFileTest(
|
|
115
|
+
transform,
|
|
116
|
+
{},
|
|
117
|
+
`
|
|
118
|
+
import {Heading} from '@sanity/ui'
|
|
119
|
+
|
|
120
|
+
export const RootHeading = styled(Heading)(({theme}) => ({}))
|
|
121
|
+
`,
|
|
122
|
+
`
|
|
123
|
+
import {Heading} from 'another-package'
|
|
124
|
+
import {RootHeading} from './Component.styled'
|
|
125
|
+
|
|
126
|
+
export function Component() {
|
|
127
|
+
return (
|
|
128
|
+
<>
|
|
129
|
+
<RootHeading flex="auto" />
|
|
130
|
+
<Heading flex="auto" />
|
|
131
|
+
</>
|
|
132
|
+
)
|
|
133
|
+
}
|
|
134
|
+
`,
|
|
135
|
+
(output) => {
|
|
136
|
+
expect(output.replace(/\s+/g, ' ')).toContain(
|
|
137
|
+
'<RootHeading style={{ flex: "1 1 auto" }} trim={true} />',
|
|
138
|
+
)
|
|
139
|
+
expect(output).toContain('<Heading flex="auto" />')
|
|
140
|
+
},
|
|
141
|
+
'does not transform attributes on unrelated Heading from another package',
|
|
142
|
+
)
|
|
143
|
+
|
|
144
|
+
defineCrossFileTest(
|
|
145
|
+
transform,
|
|
146
|
+
{},
|
|
147
|
+
`
|
|
148
|
+
import {Heading} from '@sanity/ui'
|
|
149
|
+
|
|
150
|
+
export const RootHeading = styled(Heading)(({theme}) => ({}))
|
|
151
|
+
`,
|
|
152
|
+
`
|
|
153
|
+
import {RootHeading} from './index'
|
|
154
|
+
|
|
155
|
+
export function Component() {
|
|
156
|
+
return <RootHeading flex="auto" />
|
|
157
|
+
}
|
|
158
|
+
`,
|
|
159
|
+
(output) => {
|
|
160
|
+
expect(output.replace(/\s+/g, ' ')).toContain(
|
|
161
|
+
'<RootHeading style={{ flex: "1 1 auto" }} trim={true} />',
|
|
162
|
+
)
|
|
163
|
+
},
|
|
164
|
+
'transforms styled Heading wrappers imported through barrel re-exports',
|
|
165
|
+
{
|
|
166
|
+
extraFiles: {
|
|
167
|
+
'index.ts': `export {RootHeading} from './Component.styled'`,
|
|
168
|
+
},
|
|
169
|
+
},
|
|
170
|
+
)
|