@sanity/ui-codemod 1.0.0-alpha.5 → 1.0.0-alpha.7
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 -100
- package/dist/_chunks-es/box.mods.js.map +1 -1
- package/dist/_chunks-es/flex.mods.js +1 -150
- package/dist/_chunks-es/flex.mods.js.map +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/replaceElement.js +102 -0
- package/dist/_chunks-es/replaceElement.js.map +1 -0
- package/dist/_chunks-es/{transformComponent.js → transformStyledComponents.js} +150 -2
- package/dist/_chunks-es/transformStyledComponents.js.map +1 -0
- package/dist/commands/latest/inline.d.ts +47 -0
- package/dist/commands/latest/inline.js +17 -0
- package/dist/commands/latest/inline.js.map +1 -0
- package/dist/commands/latest/label.d.ts +47 -0
- package/dist/commands/latest/label.js +17 -0
- package/dist/commands/latest/label.js.map +1 -0
- package/dist/commands/latest/stack.d.ts +47 -0
- package/dist/commands/latest/stack.js +17 -0
- package/dist/commands/latest/stack.js.map +1 -0
- package/dist/transforms/latest/box/box.js +4 -3
- package/dist/transforms/latest/box/box.js.map +1 -1
- package/dist/transforms/latest/card/card.js +26 -10
- 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 +2 -2
- 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/inline/inline.d.ts +18 -0
- package/dist/transforms/latest/inline/inline.js +80 -0
- package/dist/transforms/latest/inline/inline.js.map +1 -0
- package/dist/transforms/latest/label/label.d.ts +18 -0
- package/dist/transforms/latest/label/label.js +111 -0
- package/dist/transforms/latest/label/label.js.map +1 -0
- package/dist/transforms/latest/stack/stack.d.ts +18 -0
- package/dist/transforms/latest/stack/stack.js +67 -0
- package/dist/transforms/latest/stack/stack.js.map +1 -0
- package/dist/transforms/latest/text/text.js +18 -7
- package/dist/transforms/latest/text/text.js.map +1 -1
- package/package.json +7 -1
- package/src/constants/latest/layout-mods.ts +4 -0
- package/src/transforms/latest/box/box.test.ts +3 -8
- 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/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/dist/_chunks-es/transformComponent.js.map +0 -1
|
@@ -1,4 +1,6 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {expect} from 'vitest'
|
|
2
|
+
|
|
3
|
+
import {defineCrossFileTest, defineInlineTest} from '../../../utils/testUtils'
|
|
2
4
|
import transform from './code'
|
|
3
5
|
|
|
4
6
|
defineInlineTest(
|
|
@@ -59,3 +61,84 @@ defineInlineTest(
|
|
|
59
61
|
`,
|
|
60
62
|
'moves width props to style and uppdates mapped values',
|
|
61
63
|
)
|
|
64
|
+
|
|
65
|
+
defineCrossFileTest(
|
|
66
|
+
transform,
|
|
67
|
+
{},
|
|
68
|
+
`
|
|
69
|
+
import {Code} from '@sanity/ui'
|
|
70
|
+
|
|
71
|
+
export const RootCode = styled(Code)(({theme}) => ({}))
|
|
72
|
+
`,
|
|
73
|
+
`
|
|
74
|
+
import {RootCode} from './Component.styled'
|
|
75
|
+
|
|
76
|
+
export function RootCode() {
|
|
77
|
+
return <RootCode flex="auto" />
|
|
78
|
+
}
|
|
79
|
+
`,
|
|
80
|
+
(output) => {
|
|
81
|
+
expect(output.replace(/\s+/g, ' ')).toContain(
|
|
82
|
+
'<RootCode style={{ flex: "1 1 auto" }} trim={true} />',
|
|
83
|
+
)
|
|
84
|
+
},
|
|
85
|
+
'transforms attributes on imported styled Code wrappers',
|
|
86
|
+
)
|
|
87
|
+
|
|
88
|
+
defineCrossFileTest(
|
|
89
|
+
transform,
|
|
90
|
+
{},
|
|
91
|
+
`
|
|
92
|
+
import {Code} from '@sanity/ui'
|
|
93
|
+
|
|
94
|
+
export const RootCode = styled(Code)(({theme}) => ({}))
|
|
95
|
+
`,
|
|
96
|
+
`
|
|
97
|
+
import {Code} from 'another-package'
|
|
98
|
+
import {RootCode} from './Component.styled'
|
|
99
|
+
|
|
100
|
+
export function Component() {
|
|
101
|
+
return (
|
|
102
|
+
<>
|
|
103
|
+
<RootCode flex="auto" />
|
|
104
|
+
<Code flex="auto" />
|
|
105
|
+
</>
|
|
106
|
+
)
|
|
107
|
+
}
|
|
108
|
+
`,
|
|
109
|
+
(output) => {
|
|
110
|
+
expect(output.replace(/\s+/g, ' ')).toContain(
|
|
111
|
+
'<RootCode style={{ flex: "1 1 auto" }} trim={true} />',
|
|
112
|
+
)
|
|
113
|
+
expect(output).toContain('<Code flex="auto" />')
|
|
114
|
+
},
|
|
115
|
+
'does not transform attributes on unrelated Code from another package',
|
|
116
|
+
)
|
|
117
|
+
|
|
118
|
+
defineCrossFileTest(
|
|
119
|
+
transform,
|
|
120
|
+
{},
|
|
121
|
+
`
|
|
122
|
+
import {Code} from '@sanity/ui'
|
|
123
|
+
|
|
124
|
+
export const RootCode = styled(Code)(({theme}) => ({}))
|
|
125
|
+
`,
|
|
126
|
+
`
|
|
127
|
+
import {RootCode} from './index'
|
|
128
|
+
|
|
129
|
+
export function Component() {
|
|
130
|
+
return <RootCode flex="auto" />
|
|
131
|
+
}
|
|
132
|
+
`,
|
|
133
|
+
(output) => {
|
|
134
|
+
expect(output.replace(/\s+/g, ' ')).toContain(
|
|
135
|
+
'<RootCode style={{ flex: "1 1 auto" }} trim={true} />',
|
|
136
|
+
)
|
|
137
|
+
},
|
|
138
|
+
'transforms styled Code wrappers imported through barrel re-exports',
|
|
139
|
+
{
|
|
140
|
+
extraFiles: {
|
|
141
|
+
'index.ts': `export {RootCode} from './Component.styled'`,
|
|
142
|
+
},
|
|
143
|
+
},
|
|
144
|
+
)
|
|
@@ -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 './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
|
+
)
|
|
@@ -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 {HEADING_MODS} from './heading.mods'
|
|
11
13
|
|
|
12
14
|
const TODO_WARNING = 'Please double check the Heading 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, 'Heading', options)
|
|
26
|
+
const styledAliases = getStyledComponentAliases(
|
|
27
|
+
j,
|
|
28
|
+
root,
|
|
29
|
+
'Heading',
|
|
30
|
+
fileInfo.path,
|
|
31
|
+
localNames,
|
|
32
|
+
options,
|
|
33
|
+
)
|
|
24
34
|
|
|
25
|
-
if (!shouldTransformComponent(j, root, 'Heading', localNames, options)) {
|
|
35
|
+
if (!shouldTransformComponent(j, root, 'Heading', 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, HEADING_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, HEADING_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
|
}
|