@planningcenter/tapestry-migration-cli 3.5.0 → 3.6.1-qa-1035.0
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/README.md +37 -0
- package/dist/tapestry-react-shim.cjs +195 -11
- package/package.json +3 -3
- package/src/availableComponents.ts +1 -0
- package/src/components/button/transforms/tooltipToWrapper.test.ts +50 -3
- package/src/components/button/transforms/tooltipToWrapper.ts +18 -0
- package/src/components/flex/index.test.ts +291 -0
- package/src/components/flex/index.ts +54 -0
- package/src/components/flex/transforms/alignmentToAlign.test.ts +185 -0
- package/src/components/flex/transforms/alignmentToAlign.ts +73 -0
- package/src/components/flex/transforms/auditSpreadProps.test.ts +93 -0
- package/src/components/flex/transforms/auditSpreadProps.ts +6 -0
- package/src/components/flex/transforms/axisToDirection.test.ts +155 -0
- package/src/components/flex/transforms/axisToDirection.ts +77 -0
- package/src/components/flex/transforms/convertStyleProps.test.ts +147 -0
- package/src/components/flex/transforms/convertStyleProps.ts +23 -0
- package/src/components/flex/transforms/cssFlexAliasesToProps.test.ts +202 -0
- package/src/components/flex/transforms/cssFlexAliasesToProps.ts +110 -0
- package/src/components/flex/transforms/distributionToJustify.test.ts +214 -0
- package/src/components/flex/transforms/distributionToJustify.ts +76 -0
- package/src/components/flex/transforms/flexPropToExpand.test.ts +156 -0
- package/src/components/flex/transforms/flexPropToExpand.ts +100 -0
- package/src/components/flex/transforms/innerRefToRef.test.ts +100 -0
- package/src/components/flex/transforms/innerRefToRef.ts +14 -0
- package/src/components/flex/transforms/mediaQueriesToResponsive.test.ts +714 -0
- package/src/components/flex/transforms/mediaQueriesToResponsive.ts +523 -0
- package/src/components/flex/transforms/moveFlexImport.test.ts +202 -0
- package/src/components/flex/transforms/moveFlexImport.ts +14 -0
- package/src/components/flex/transforms/paddingPropsToFlex.test.ts +252 -0
- package/src/components/flex/transforms/paddingPropsToFlex.ts +150 -0
- package/src/components/flex/transforms/setDefaultAxis.test.ts +126 -0
- package/src/components/flex/transforms/setDefaultAxis.ts +30 -0
- package/src/components/flex/transforms/spacingToGap.test.ts +176 -0
- package/src/components/flex/transforms/spacingToGap.ts +49 -0
- package/src/components/flex/transforms/unsupportedProps.test.ts +141 -0
- package/src/components/flex/transforms/unsupportedProps.ts +17 -0
- package/src/components/select/index.ts +2 -0
- package/src/components/select/transforms/onChangeSignature.test.ts +224 -0
- package/src/components/select/transforms/onChangeSignature.ts +172 -0
- package/src/components/shared/actions/resolveConstantAttributeValue.test.ts +122 -0
- package/src/components/shared/actions/resolveConstantAttributeValue.ts +31 -0
- package/src/components/shared/helpers/unsupportedPropsHelpers.ts +41 -0
- package/src/components/shared/transformFactories/stylePropTransformFactory.test.ts +47 -0
- package/src/components/shared/transformFactories/stylePropTransformFactory.ts +16 -0
- package/src/components/toggle-switch/index.ts +2 -0
- package/src/components/toggle-switch/transforms/onClickToOnChange.test.ts +210 -0
- package/src/components/toggle-switch/transforms/onClickToOnChange.ts +71 -0
- package/src/index.test.ts +79 -0
- package/src/index.ts +29 -0
- package/src/migrationList.ts +22 -0
- package/src/utils/componentLabel.test.ts +61 -0
- package/src/utils/componentLabel.ts +15 -0
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
import jscodeshift from "jscodeshift"
|
|
2
|
+
import { describe, expect, it } from "vitest"
|
|
3
|
+
|
|
4
|
+
import transform from "./moveFlexImport"
|
|
5
|
+
|
|
6
|
+
const j = jscodeshift.withParser("tsx")
|
|
7
|
+
|
|
8
|
+
function applyTransform(source: string): string | null {
|
|
9
|
+
const fileInfo = { path: "test.tsx", source }
|
|
10
|
+
return transform(
|
|
11
|
+
fileInfo,
|
|
12
|
+
{ j, jscodeshift: j, report: () => {}, stats: () => {} },
|
|
13
|
+
{}
|
|
14
|
+
) as string | null
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
describe("moveFlexImport transform", () => {
|
|
18
|
+
describe("basic transformations", () => {
|
|
19
|
+
it("renames StackView to Flex and moves the import to tapestry", () => {
|
|
20
|
+
const input = `
|
|
21
|
+
import { StackView } from "@planningcenter/tapestry-react"
|
|
22
|
+
|
|
23
|
+
export default function Test() {
|
|
24
|
+
return <StackView>Test</StackView>
|
|
25
|
+
}
|
|
26
|
+
`.trim()
|
|
27
|
+
|
|
28
|
+
const result = applyTransform(input)
|
|
29
|
+
expect(result).toContain(
|
|
30
|
+
'import { Flex } from "@planningcenter/tapestry"'
|
|
31
|
+
)
|
|
32
|
+
expect(result).toContain("<Flex>Test</Flex>")
|
|
33
|
+
expect(result).not.toContain("StackView")
|
|
34
|
+
expect(result).not.toContain('from "@planningcenter/tapestry-react"')
|
|
35
|
+
})
|
|
36
|
+
|
|
37
|
+
it("renames multiple StackView usages", () => {
|
|
38
|
+
const input = `
|
|
39
|
+
import { StackView } from "@planningcenter/tapestry-react"
|
|
40
|
+
|
|
41
|
+
export default function Test() {
|
|
42
|
+
return (
|
|
43
|
+
<StackView>
|
|
44
|
+
<StackView>Inner</StackView>
|
|
45
|
+
</StackView>
|
|
46
|
+
)
|
|
47
|
+
}
|
|
48
|
+
`.trim()
|
|
49
|
+
|
|
50
|
+
const result = applyTransform(input)
|
|
51
|
+
expect(result).toContain(
|
|
52
|
+
'import { Flex } from "@planningcenter/tapestry"'
|
|
53
|
+
)
|
|
54
|
+
expect(result).toContain("<Flex>")
|
|
55
|
+
expect(result).not.toContain("StackView")
|
|
56
|
+
})
|
|
57
|
+
|
|
58
|
+
it("preserves other imports from tapestry-react", () => {
|
|
59
|
+
const input = `
|
|
60
|
+
import { StackView, Button, Input } from "@planningcenter/tapestry-react"
|
|
61
|
+
|
|
62
|
+
export default function Test() {
|
|
63
|
+
return <StackView>Test</StackView>
|
|
64
|
+
}
|
|
65
|
+
`.trim()
|
|
66
|
+
|
|
67
|
+
const result = applyTransform(input)
|
|
68
|
+
expect(result).toContain(
|
|
69
|
+
'import { Button, Input } from "@planningcenter/tapestry-react"'
|
|
70
|
+
)
|
|
71
|
+
expect(result).toContain(
|
|
72
|
+
'import { Flex } from "@planningcenter/tapestry"'
|
|
73
|
+
)
|
|
74
|
+
expect(result).toContain("<Flex>Test</Flex>")
|
|
75
|
+
})
|
|
76
|
+
|
|
77
|
+
it("handles self-closing and propped StackView elements", () => {
|
|
78
|
+
const input = `
|
|
79
|
+
import { StackView } from "@planningcenter/tapestry-react"
|
|
80
|
+
|
|
81
|
+
export default function Test() {
|
|
82
|
+
return <StackView axis="vertical" spacing={2} />
|
|
83
|
+
}
|
|
84
|
+
`.trim()
|
|
85
|
+
|
|
86
|
+
const result = applyTransform(input)
|
|
87
|
+
expect(result).toContain(
|
|
88
|
+
'import { Flex } from "@planningcenter/tapestry"'
|
|
89
|
+
)
|
|
90
|
+
expect(result).toContain('<Flex axis="vertical" spacing={2} />')
|
|
91
|
+
expect(result).not.toContain('from "@planningcenter/tapestry-react"')
|
|
92
|
+
})
|
|
93
|
+
})
|
|
94
|
+
|
|
95
|
+
describe("aliased imports", () => {
|
|
96
|
+
it("normalizes an aliased StackView import to Flex", () => {
|
|
97
|
+
const input = `
|
|
98
|
+
import { StackView as Stack } from "@planningcenter/tapestry-react"
|
|
99
|
+
|
|
100
|
+
export default function Test() {
|
|
101
|
+
return <Stack>Test</Stack>
|
|
102
|
+
}
|
|
103
|
+
`.trim()
|
|
104
|
+
|
|
105
|
+
const result = applyTransform(input)
|
|
106
|
+
expect(result).toContain(
|
|
107
|
+
'import { Flex } from "@planningcenter/tapestry"'
|
|
108
|
+
)
|
|
109
|
+
expect(result).toContain("<Flex>Test</Flex>")
|
|
110
|
+
expect(result).not.toContain("Stack")
|
|
111
|
+
expect(result).not.toContain('from "@planningcenter/tapestry-react"')
|
|
112
|
+
})
|
|
113
|
+
})
|
|
114
|
+
|
|
115
|
+
describe("import conflict handling", () => {
|
|
116
|
+
it("aliases to TFlex when a Flex from another package is in scope", () => {
|
|
117
|
+
const input = `
|
|
118
|
+
import { Flex } from "some-other-lib"
|
|
119
|
+
import { StackView } from "@planningcenter/tapestry-react"
|
|
120
|
+
|
|
121
|
+
export default function Test() {
|
|
122
|
+
return <StackView>Test</StackView>
|
|
123
|
+
}
|
|
124
|
+
`.trim()
|
|
125
|
+
|
|
126
|
+
const result = applyTransform(input)
|
|
127
|
+
expect(result).toContain(
|
|
128
|
+
'import { Flex as TFlex } from "@planningcenter/tapestry"'
|
|
129
|
+
)
|
|
130
|
+
expect(result).toContain('import { Flex } from "some-other-lib"')
|
|
131
|
+
expect(result).toContain("<TFlex>Test</TFlex>")
|
|
132
|
+
expect(result).not.toContain('from "@planningcenter/tapestry-react"')
|
|
133
|
+
})
|
|
134
|
+
|
|
135
|
+
it("merges with an existing Flex import from the same tapestry package", () => {
|
|
136
|
+
const input = `
|
|
137
|
+
import { Flex } from "@planningcenter/tapestry"
|
|
138
|
+
import { StackView } from "@planningcenter/tapestry-react"
|
|
139
|
+
|
|
140
|
+
export default function Test() {
|
|
141
|
+
return <StackView>Test</StackView>
|
|
142
|
+
}
|
|
143
|
+
`.trim()
|
|
144
|
+
|
|
145
|
+
const result = applyTransform(input)
|
|
146
|
+
expect(result).toContain(
|
|
147
|
+
'import { Flex } from "@planningcenter/tapestry"'
|
|
148
|
+
)
|
|
149
|
+
expect(result).toContain("<Flex>Test</Flex>")
|
|
150
|
+
expect(result).not.toContain("TFlex")
|
|
151
|
+
expect(result).not.toContain('from "@planningcenter/tapestry-react"')
|
|
152
|
+
})
|
|
153
|
+
|
|
154
|
+
it("merges into an existing tapestry import that has other components", () => {
|
|
155
|
+
const input = `
|
|
156
|
+
import { Button, Input } from "@planningcenter/tapestry"
|
|
157
|
+
import { StackView } from "@planningcenter/tapestry-react"
|
|
158
|
+
|
|
159
|
+
export default function Test() {
|
|
160
|
+
return <StackView>Test</StackView>
|
|
161
|
+
}
|
|
162
|
+
`.trim()
|
|
163
|
+
|
|
164
|
+
const result = applyTransform(input)
|
|
165
|
+
expect(result).toContain(
|
|
166
|
+
'import { Button, Input, Flex } from "@planningcenter/tapestry"'
|
|
167
|
+
)
|
|
168
|
+
expect(result).toContain("<Flex>Test</Flex>")
|
|
169
|
+
expect(result).not.toContain('from "@planningcenter/tapestry-react"')
|
|
170
|
+
})
|
|
171
|
+
})
|
|
172
|
+
|
|
173
|
+
describe("no changes scenarios", () => {
|
|
174
|
+
it("returns null when StackView is not imported from tapestry-react", () => {
|
|
175
|
+
const input = `
|
|
176
|
+
import { Button } from "@planningcenter/tapestry-react"
|
|
177
|
+
|
|
178
|
+
export default function Test() {
|
|
179
|
+
return <Button>Test</Button>
|
|
180
|
+
}
|
|
181
|
+
`.trim()
|
|
182
|
+
|
|
183
|
+
expect(applyTransform(input)).toBe(null)
|
|
184
|
+
})
|
|
185
|
+
|
|
186
|
+
it("returns null when StackView comes from a different package", () => {
|
|
187
|
+
const input = `
|
|
188
|
+
import { StackView } from "some-other-package"
|
|
189
|
+
|
|
190
|
+
export default function Test() {
|
|
191
|
+
return <StackView>Test</StackView>
|
|
192
|
+
}
|
|
193
|
+
`.trim()
|
|
194
|
+
|
|
195
|
+
expect(applyTransform(input)).toBe(null)
|
|
196
|
+
})
|
|
197
|
+
|
|
198
|
+
it("returns null for an empty file", () => {
|
|
199
|
+
expect(applyTransform("")).toBe(null)
|
|
200
|
+
})
|
|
201
|
+
})
|
|
202
|
+
})
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { Transform } from "jscodeshift"
|
|
2
|
+
|
|
3
|
+
import { componentTransformFactory } from "../../shared/transformFactories/componentTransformFactory"
|
|
4
|
+
|
|
5
|
+
const transform: Transform = componentTransformFactory({
|
|
6
|
+
condition: () => true,
|
|
7
|
+
conflictAlias: "TFlex",
|
|
8
|
+
fromComponent: "StackView",
|
|
9
|
+
fromPackage: "@planningcenter/tapestry-react",
|
|
10
|
+
toComponent: "Flex",
|
|
11
|
+
toPackage: "@planningcenter/tapestry",
|
|
12
|
+
})
|
|
13
|
+
|
|
14
|
+
export default transform
|
|
@@ -0,0 +1,252 @@
|
|
|
1
|
+
import jscodeshift from "jscodeshift"
|
|
2
|
+
import { describe, expect, it } from "vitest"
|
|
3
|
+
|
|
4
|
+
import transform from "./paddingPropsToFlex"
|
|
5
|
+
|
|
6
|
+
const j = jscodeshift.withParser("tsx")
|
|
7
|
+
|
|
8
|
+
function applyTransform(source: string): string | null {
|
|
9
|
+
const fileInfo = { path: "test.tsx", source }
|
|
10
|
+
return transform(
|
|
11
|
+
fileInfo,
|
|
12
|
+
{ j, jscodeshift: j, report: () => {}, stats: () => {} },
|
|
13
|
+
{}
|
|
14
|
+
) as string | null
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
describe("paddingPropsToFlex transform", () => {
|
|
18
|
+
it("leaves a valid scale-number padding unchanged", () => {
|
|
19
|
+
const input = `
|
|
20
|
+
import { StackView } from "@planningcenter/tapestry-react"
|
|
21
|
+
export default function Test() {
|
|
22
|
+
return <StackView padding={2}>Test</StackView>
|
|
23
|
+
}
|
|
24
|
+
`.trim()
|
|
25
|
+
|
|
26
|
+
expect(applyTransform(input)).toBe(null)
|
|
27
|
+
})
|
|
28
|
+
|
|
29
|
+
it('converts padding="0" to padding={0}', () => {
|
|
30
|
+
const input = `
|
|
31
|
+
import { StackView } from "@planningcenter/tapestry-react"
|
|
32
|
+
export default function Test() {
|
|
33
|
+
return <StackView padding="0">Test</StackView>
|
|
34
|
+
}
|
|
35
|
+
`.trim()
|
|
36
|
+
|
|
37
|
+
const result = applyTransform(input)
|
|
38
|
+
expect(result).toContain("padding={0}")
|
|
39
|
+
expect(result).not.toContain('padding="0"')
|
|
40
|
+
expect(result).not.toContain("TODO")
|
|
41
|
+
})
|
|
42
|
+
|
|
43
|
+
it("converts a pixel string to the corresponding spacing token", () => {
|
|
44
|
+
const input = `
|
|
45
|
+
import { StackView } from "@planningcenter/tapestry-react"
|
|
46
|
+
export default function Test() {
|
|
47
|
+
return <StackView padding="8px">Test</StackView>
|
|
48
|
+
}
|
|
49
|
+
`.trim()
|
|
50
|
+
|
|
51
|
+
const result = applyTransform(input)
|
|
52
|
+
expect(result).toContain("padding={1}")
|
|
53
|
+
expect(result).not.toContain('padding="8px"')
|
|
54
|
+
expect(result).not.toContain("TODO")
|
|
55
|
+
})
|
|
56
|
+
|
|
57
|
+
it("converts all pixel values in the scale", () => {
|
|
58
|
+
const cases: Array<[string, number]> = [
|
|
59
|
+
["0px", 0],
|
|
60
|
+
["2px", 0.25],
|
|
61
|
+
["4px", 0.5],
|
|
62
|
+
["8px", 1],
|
|
63
|
+
["12px", 1.5],
|
|
64
|
+
["16px", 2],
|
|
65
|
+
["24px", 3],
|
|
66
|
+
["32px", 4],
|
|
67
|
+
["40px", 5],
|
|
68
|
+
["48px", 6],
|
|
69
|
+
["56px", 7],
|
|
70
|
+
]
|
|
71
|
+
for (const [px, token] of cases) {
|
|
72
|
+
const input = `
|
|
73
|
+
import { StackView } from "@planningcenter/tapestry-react"
|
|
74
|
+
export default function Test() {
|
|
75
|
+
return <StackView padding="${px}">Test</StackView>
|
|
76
|
+
}
|
|
77
|
+
`.trim()
|
|
78
|
+
const result = applyTransform(input)
|
|
79
|
+
expect(result, `${px} should map to ${token}`).toContain(
|
|
80
|
+
`padding={${token}}`
|
|
81
|
+
)
|
|
82
|
+
}
|
|
83
|
+
})
|
|
84
|
+
|
|
85
|
+
it("adds a TODO on the padding attribute for a value that cannot be mapped", () => {
|
|
86
|
+
const input = `
|
|
87
|
+
import { StackView } from "@planningcenter/tapestry-react"
|
|
88
|
+
export default function Test() {
|
|
89
|
+
return <StackView padding="10px">Test</StackView>
|
|
90
|
+
}
|
|
91
|
+
`.trim()
|
|
92
|
+
|
|
93
|
+
const result = applyTransform(input)
|
|
94
|
+
expect(result).toMatch(/\/\*.*TODO.*\*\/\s*padding="10px"/)
|
|
95
|
+
})
|
|
96
|
+
|
|
97
|
+
it("renames paddingHorizontal to paddingInline for a valid scale number", () => {
|
|
98
|
+
const input = `
|
|
99
|
+
import { StackView } from "@planningcenter/tapestry-react"
|
|
100
|
+
export default function Test() {
|
|
101
|
+
return <StackView paddingHorizontal={2}>Test</StackView>
|
|
102
|
+
}
|
|
103
|
+
`.trim()
|
|
104
|
+
|
|
105
|
+
const result = applyTransform(input)
|
|
106
|
+
expect(result).toContain("paddingInline={2}")
|
|
107
|
+
expect(result).not.toContain("paddingHorizontal")
|
|
108
|
+
expect(result).not.toContain("TODO")
|
|
109
|
+
})
|
|
110
|
+
|
|
111
|
+
it("renames paddingHorizontal and converts a pixel string", () => {
|
|
112
|
+
const input = `
|
|
113
|
+
import { StackView } from "@planningcenter/tapestry-react"
|
|
114
|
+
export default function Test() {
|
|
115
|
+
return <StackView paddingHorizontal="16px">Test</StackView>
|
|
116
|
+
}
|
|
117
|
+
`.trim()
|
|
118
|
+
|
|
119
|
+
const result = applyTransform(input)
|
|
120
|
+
expect(result).toContain("paddingInline={2}")
|
|
121
|
+
expect(result).not.toContain("paddingHorizontal")
|
|
122
|
+
expect(result).not.toContain("TODO")
|
|
123
|
+
})
|
|
124
|
+
|
|
125
|
+
it("renames paddingHorizontal to paddingInline and adds a TODO for an unmappable value", () => {
|
|
126
|
+
const input = `
|
|
127
|
+
import { StackView } from "@planningcenter/tapestry-react"
|
|
128
|
+
export default function Test() {
|
|
129
|
+
return <StackView paddingHorizontal="1rem">Test</StackView>
|
|
130
|
+
}
|
|
131
|
+
`.trim()
|
|
132
|
+
|
|
133
|
+
const result = applyTransform(input)
|
|
134
|
+
expect(result).toContain("paddingInline")
|
|
135
|
+
expect(result).not.toContain("paddingHorizontal")
|
|
136
|
+
expect(result).toMatch(/TODO/)
|
|
137
|
+
})
|
|
138
|
+
|
|
139
|
+
it("renames paddingHorizontal to paddingInline without a TODO when both ternary branches are valid scale numbers", () => {
|
|
140
|
+
const input = `
|
|
141
|
+
import { StackView } from "@planningcenter/tapestry-react"
|
|
142
|
+
export default function Test() {
|
|
143
|
+
return <StackView paddingHorizontal={isNarrow ? 2 : 6}>Test</StackView>
|
|
144
|
+
}
|
|
145
|
+
`.trim()
|
|
146
|
+
|
|
147
|
+
const result = applyTransform(input)
|
|
148
|
+
expect(result).toContain("paddingInline")
|
|
149
|
+
expect(result).not.toContain("paddingHorizontal")
|
|
150
|
+
expect(result).not.toMatch(/TODO/)
|
|
151
|
+
})
|
|
152
|
+
|
|
153
|
+
it("renames paddingHorizontal to paddingInline without a TODO when one ternary branch is null", () => {
|
|
154
|
+
const input = `
|
|
155
|
+
import { StackView } from "@planningcenter/tapestry-react"
|
|
156
|
+
export default function Test() {
|
|
157
|
+
return <StackView paddingHorizontal={isRoom ? null : 2}>Test</StackView>
|
|
158
|
+
}
|
|
159
|
+
`.trim()
|
|
160
|
+
|
|
161
|
+
const result = applyTransform(input)
|
|
162
|
+
expect(result).toContain("paddingInline")
|
|
163
|
+
expect(result).not.toContain("paddingHorizontal")
|
|
164
|
+
expect(result).not.toMatch(/TODO/)
|
|
165
|
+
})
|
|
166
|
+
|
|
167
|
+
it("renames paddingVertical to paddingBlock for a valid scale number", () => {
|
|
168
|
+
const input = `
|
|
169
|
+
import { StackView } from "@planningcenter/tapestry-react"
|
|
170
|
+
export default function Test() {
|
|
171
|
+
return <StackView paddingVertical={1}>Test</StackView>
|
|
172
|
+
}
|
|
173
|
+
`.trim()
|
|
174
|
+
|
|
175
|
+
const result = applyTransform(input)
|
|
176
|
+
expect(result).toContain("paddingBlock={1}")
|
|
177
|
+
expect(result).not.toContain("paddingVertical")
|
|
178
|
+
expect(result).not.toContain("TODO")
|
|
179
|
+
})
|
|
180
|
+
|
|
181
|
+
it("renames paddingVertical and converts a pixel string", () => {
|
|
182
|
+
const input = `
|
|
183
|
+
import { StackView } from "@planningcenter/tapestry-react"
|
|
184
|
+
export default function Test() {
|
|
185
|
+
return <StackView paddingVertical="8px">Test</StackView>
|
|
186
|
+
}
|
|
187
|
+
`.trim()
|
|
188
|
+
|
|
189
|
+
const result = applyTransform(input)
|
|
190
|
+
expect(result).toContain("paddingBlock={1}")
|
|
191
|
+
expect(result).not.toContain("paddingVertical")
|
|
192
|
+
expect(result).not.toContain("TODO")
|
|
193
|
+
})
|
|
194
|
+
|
|
195
|
+
it("renames paddingVertical to paddingBlock without a TODO when both ternary branches are valid scale numbers", () => {
|
|
196
|
+
const input = `
|
|
197
|
+
import { StackView } from "@planningcenter/tapestry-react"
|
|
198
|
+
export default function Test() {
|
|
199
|
+
return <StackView paddingVertical={isNarrow ? 1 : 3}>Test</StackView>
|
|
200
|
+
}
|
|
201
|
+
`.trim()
|
|
202
|
+
|
|
203
|
+
const result = applyTransform(input)
|
|
204
|
+
expect(result).toContain("paddingBlock")
|
|
205
|
+
expect(result).not.toContain("paddingVertical")
|
|
206
|
+
expect(result).not.toMatch(/TODO/)
|
|
207
|
+
})
|
|
208
|
+
|
|
209
|
+
it("leaves padding with a valid-scale ternary unchanged", () => {
|
|
210
|
+
const input = `
|
|
211
|
+
import { StackView } from "@planningcenter/tapestry-react"
|
|
212
|
+
export default function Test() {
|
|
213
|
+
return <StackView padding={isNarrow ? 2 : 6}>Test</StackView>
|
|
214
|
+
}
|
|
215
|
+
`.trim()
|
|
216
|
+
|
|
217
|
+
expect(applyTransform(input)).toBe(null)
|
|
218
|
+
})
|
|
219
|
+
|
|
220
|
+
it("leaves padding with a null ternary branch unchanged", () => {
|
|
221
|
+
const input = `
|
|
222
|
+
import { StackView } from "@planningcenter/tapestry-react"
|
|
223
|
+
export default function Test() {
|
|
224
|
+
return <StackView padding={isRoom ? null : 2}>Test</StackView>
|
|
225
|
+
}
|
|
226
|
+
`.trim()
|
|
227
|
+
|
|
228
|
+
expect(applyTransform(input)).toBe(null)
|
|
229
|
+
})
|
|
230
|
+
|
|
231
|
+
it("returns null when StackView has no padding props", () => {
|
|
232
|
+
const input = `
|
|
233
|
+
import { StackView } from "@planningcenter/tapestry-react"
|
|
234
|
+
export default function Test() {
|
|
235
|
+
return <StackView axis="horizontal">Test</StackView>
|
|
236
|
+
}
|
|
237
|
+
`.trim()
|
|
238
|
+
|
|
239
|
+
expect(applyTransform(input)).toBe(null)
|
|
240
|
+
})
|
|
241
|
+
|
|
242
|
+
it("returns null when StackView is not imported from tapestry-react", () => {
|
|
243
|
+
const input = `
|
|
244
|
+
import { StackView } from "some-other-package"
|
|
245
|
+
export default function Test() {
|
|
246
|
+
return <StackView padding="8px">Test</StackView>
|
|
247
|
+
}
|
|
248
|
+
`.trim()
|
|
249
|
+
|
|
250
|
+
expect(applyTransform(input)).toBe(null)
|
|
251
|
+
})
|
|
252
|
+
})
|
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
import { JSXAttribute } from "jscodeshift"
|
|
2
|
+
|
|
3
|
+
import { addCommentToAttribute } from "../../shared/actions/addCommentToAttribute"
|
|
4
|
+
import { getAttribute } from "../../shared/actions/getAttribute"
|
|
5
|
+
import { hasAttribute } from "../../shared/conditions/hasAttribute"
|
|
6
|
+
import { orConditions } from "../../shared/conditions/orConditions"
|
|
7
|
+
import { attributeTransformFactory } from "../../shared/transformFactories/attributeTransformFactory"
|
|
8
|
+
|
|
9
|
+
const PADDING_SCALE = new Set([0, 0.25, 0.5, 1, 1.5, 2, 3, 4, 5, 6, 7])
|
|
10
|
+
|
|
11
|
+
const PIXEL_TO_TOKEN: Record<string, number> = {
|
|
12
|
+
"0": 0,
|
|
13
|
+
"0px": 0,
|
|
14
|
+
"2px": 0.25,
|
|
15
|
+
"4px": 0.5,
|
|
16
|
+
"8px": 1,
|
|
17
|
+
"12px": 1.5,
|
|
18
|
+
"16px": 2,
|
|
19
|
+
"24px": 3,
|
|
20
|
+
"32px": 4,
|
|
21
|
+
"40px": 5,
|
|
22
|
+
"48px": 6,
|
|
23
|
+
"56px": 7,
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
function isScaleNumber(node: unknown): boolean {
|
|
27
|
+
return (
|
|
28
|
+
typeof node === "object" &&
|
|
29
|
+
node !== null &&
|
|
30
|
+
(node as { type: string }).type === "NumericLiteral" &&
|
|
31
|
+
PADDING_SCALE.has((node as { value: number }).value)
|
|
32
|
+
)
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
function isNullLiteral(node: unknown): boolean {
|
|
36
|
+
return (
|
|
37
|
+
typeof node === "object" &&
|
|
38
|
+
node !== null &&
|
|
39
|
+
(node as { type: string }).type === "NullLiteral"
|
|
40
|
+
)
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
function resolveToSpacingToken(attr: JSXAttribute): number | null {
|
|
44
|
+
const value = attr.value
|
|
45
|
+
if (!value) return null
|
|
46
|
+
if (value.type === "JSXExpressionContainer") {
|
|
47
|
+
const expr = value.expression
|
|
48
|
+
if (
|
|
49
|
+
expr.type === "NumericLiteral" &&
|
|
50
|
+
PADDING_SCALE.has(expr.value as number)
|
|
51
|
+
) {
|
|
52
|
+
return expr.value as number
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
if (value.type === "StringLiteral") {
|
|
56
|
+
const mapped = PIXEL_TO_TOKEN[value.value as string]
|
|
57
|
+
if (mapped !== undefined) return mapped
|
|
58
|
+
}
|
|
59
|
+
return null
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
function isAlreadyValidScaleValue(attr: JSXAttribute): boolean {
|
|
63
|
+
const value = attr.value
|
|
64
|
+
if (value?.type !== "JSXExpressionContainer") return false
|
|
65
|
+
const expr = value.expression
|
|
66
|
+
if (isScaleNumber(expr)) return true
|
|
67
|
+
if (expr.type === "ConditionalExpression") {
|
|
68
|
+
const validBranch = (n: unknown) => isScaleNumber(n) || isNullLiteral(n)
|
|
69
|
+
return validBranch(expr.consequent) && validBranch(expr.alternate)
|
|
70
|
+
}
|
|
71
|
+
return false
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
const PADDING_SCALE_LIST = [...PADDING_SCALE].join(", ")
|
|
75
|
+
|
|
76
|
+
const TODO_TEXT =
|
|
77
|
+
"could not migrate padding automatically; " +
|
|
78
|
+
`use a Tapestry spacing scale number (${PADDING_SCALE_LIST}) ` +
|
|
79
|
+
"for Flex padding, or use style={{ padding: ... }} for arbitrary CSS values"
|
|
80
|
+
|
|
81
|
+
export default attributeTransformFactory({
|
|
82
|
+
condition: orConditions(
|
|
83
|
+
hasAttribute("padding"),
|
|
84
|
+
hasAttribute("paddingHorizontal"),
|
|
85
|
+
hasAttribute("paddingVertical")
|
|
86
|
+
),
|
|
87
|
+
targetComponent: "StackView",
|
|
88
|
+
targetPackage: "@planningcenter/tapestry-react",
|
|
89
|
+
transform: (element, { j }) => {
|
|
90
|
+
let changed = false
|
|
91
|
+
|
|
92
|
+
const paddingAttr = getAttribute({ element, name: "padding" })
|
|
93
|
+
if (paddingAttr) {
|
|
94
|
+
const token = resolveToSpacingToken(paddingAttr)
|
|
95
|
+
if (token !== null) {
|
|
96
|
+
const alreadyCorrect =
|
|
97
|
+
paddingAttr.value?.type === "JSXExpressionContainer" &&
|
|
98
|
+
paddingAttr.value.expression.type === "NumericLiteral" &&
|
|
99
|
+
(paddingAttr.value.expression as { value: number }).value === token
|
|
100
|
+
if (!alreadyCorrect) {
|
|
101
|
+
paddingAttr.value = j.jsxExpressionContainer(j.numericLiteral(token))
|
|
102
|
+
changed = true
|
|
103
|
+
}
|
|
104
|
+
} else if (!isAlreadyValidScaleValue(paddingAttr)) {
|
|
105
|
+
addCommentToAttribute({ attribute: paddingAttr, j, text: TODO_TEXT })
|
|
106
|
+
changed = true
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
const horizontalAttr = getAttribute({ element, name: "paddingHorizontal" })
|
|
111
|
+
if (horizontalAttr) {
|
|
112
|
+
horizontalAttr.name.name = "paddingInline"
|
|
113
|
+
const token = resolveToSpacingToken(horizontalAttr)
|
|
114
|
+
if (token !== null) {
|
|
115
|
+
const alreadyCorrect =
|
|
116
|
+
horizontalAttr.value?.type === "JSXExpressionContainer" &&
|
|
117
|
+
horizontalAttr.value.expression.type === "NumericLiteral" &&
|
|
118
|
+
(horizontalAttr.value.expression as { value: number }).value === token
|
|
119
|
+
if (!alreadyCorrect) {
|
|
120
|
+
horizontalAttr.value = j.jsxExpressionContainer(
|
|
121
|
+
j.numericLiteral(token)
|
|
122
|
+
)
|
|
123
|
+
}
|
|
124
|
+
} else if (!isAlreadyValidScaleValue(horizontalAttr)) {
|
|
125
|
+
addCommentToAttribute({ attribute: horizontalAttr, j, text: TODO_TEXT })
|
|
126
|
+
}
|
|
127
|
+
changed = true
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
const verticalAttr = getAttribute({ element, name: "paddingVertical" })
|
|
131
|
+
if (verticalAttr) {
|
|
132
|
+
verticalAttr.name.name = "paddingBlock"
|
|
133
|
+
const token = resolveToSpacingToken(verticalAttr)
|
|
134
|
+
if (token !== null) {
|
|
135
|
+
const alreadyCorrect =
|
|
136
|
+
verticalAttr.value?.type === "JSXExpressionContainer" &&
|
|
137
|
+
verticalAttr.value.expression.type === "NumericLiteral" &&
|
|
138
|
+
(verticalAttr.value.expression as { value: number }).value === token
|
|
139
|
+
if (!alreadyCorrect) {
|
|
140
|
+
verticalAttr.value = j.jsxExpressionContainer(j.numericLiteral(token))
|
|
141
|
+
}
|
|
142
|
+
} else if (!isAlreadyValidScaleValue(verticalAttr)) {
|
|
143
|
+
addCommentToAttribute({ attribute: verticalAttr, j, text: TODO_TEXT })
|
|
144
|
+
}
|
|
145
|
+
changed = true
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
return changed
|
|
149
|
+
},
|
|
150
|
+
})
|