@planningcenter/tapestry-migration-cli 3.1.0-rc.6 → 3.1.0-rc.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/package.json +3 -3
- package/src/components/button/transforms/convertStyleProps.test.ts +97 -0
- package/src/components/button/transforms/removeTypeButton.test.ts +0 -1
- package/src/components/checkbox/transforms/moveCheckboxImport.test.ts +3 -0
- package/src/components/input/index.ts +66 -0
- package/src/components/input/transformableInput.ts +8 -0
- package/src/components/input/transforms/auditSpreadProps.test.ts +192 -0
- package/src/components/input/transforms/auditSpreadProps.ts +26 -0
- package/src/components/input/transforms/autoWidthTransform.test.ts +172 -0
- package/src/components/input/transforms/autoWidthTransform.ts +41 -0
- package/src/components/input/transforms/convertStyleProps.test.ts +128 -0
- package/src/components/input/transforms/convertStyleProps.ts +12 -0
- package/src/components/input/transforms/highlightOnInteractionToSelectTextOnFocus.test.ts +186 -0
- package/src/components/input/transforms/highlightOnInteractionToSelectTextOnFocus.ts +27 -0
- package/src/components/input/transforms/inputLabelToLabelProp.test.ts +319 -0
- package/src/components/input/transforms/inputLabelToLabelProp.ts +203 -0
- package/src/components/input/transforms/mergeFieldIntoInput.test.ts +391 -0
- package/src/components/input/transforms/mergeFieldIntoInput.ts +213 -0
- package/src/components/input/transforms/mergeInputLabel.test.ts +458 -0
- package/src/components/input/transforms/mergeInputLabel.ts +204 -0
- package/src/components/input/transforms/moveInputImport.test.ts +166 -0
- package/src/components/input/transforms/moveInputImport.ts +14 -0
- package/src/components/input/transforms/numberFieldAddTypeNumber.test.ts +92 -0
- package/src/components/input/transforms/numberFieldAddTypeNumber.ts +14 -0
- package/src/components/input/transforms/numberFieldRenameToInput.test.ts +126 -0
- package/src/components/input/transforms/numberFieldRenameToInput.ts +9 -0
- package/src/components/input/transforms/removeAsInput.test.ts +139 -0
- package/src/components/input/transforms/removeAsInput.ts +20 -0
- package/src/components/input/transforms/removeDuplicateKeys.test.ts +302 -0
- package/src/components/input/transforms/removeDuplicateKeys.ts +10 -0
- package/src/components/input/transforms/removeInputBox.test.ts +352 -0
- package/src/components/input/transforms/removeInputBox.ts +109 -0
- package/src/components/input/transforms/removeRedundantAriaLabel.test.ts +128 -0
- package/src/components/input/transforms/removeRedundantAriaLabel.ts +21 -0
- package/src/components/input/transforms/removeTypeText.test.ts +160 -0
- package/src/components/input/transforms/removeTypeText.ts +18 -0
- package/src/components/input/transforms/sizeMapping.test.ts +198 -0
- package/src/components/input/transforms/sizeMapping.ts +17 -0
- package/src/components/input/transforms/skipRenderSideProps.test.ts +236 -0
- package/src/components/input/transforms/skipRenderSideProps.ts +27 -0
- package/src/components/input/transforms/stateToInvalid.test.ts +208 -0
- package/src/components/input/transforms/stateToInvalid.ts +59 -0
- package/src/components/input/transforms/stateToInvalidTernary.test.ts +159 -0
- package/src/components/input/transforms/stateToInvalidTernary.ts +13 -0
- package/src/components/input/transforms/unsupportedProps.test.ts +566 -0
- package/src/components/input/transforms/unsupportedProps.ts +84 -0
- package/src/components/link/transforms/reviewStyles.test.ts +0 -1
- package/src/components/shared/helpers/unsupportedPropsHelpers.ts +52 -0
- package/src/components/shared/transformFactories/helpers/manageImports.ts +14 -12
- package/src/components/shared/transformFactories/sizeMappingFactory.ts +9 -2
- package/src/components/shared/transformFactories/stylePropTransformFactory.ts +54 -16
- package/src/components/shared/transformFactories/ternaryConditionalToPropFactory.ts +65 -0
|
@@ -0,0 +1,213 @@
|
|
|
1
|
+
import { JSXElement, JSXText, Transform } from "jscodeshift"
|
|
2
|
+
|
|
3
|
+
import { addComment } from "../../shared/actions/addComment"
|
|
4
|
+
import { hasAttribute } from "../../shared/conditions/hasAttribute"
|
|
5
|
+
import { orConditions } from "../../shared/conditions/orConditions"
|
|
6
|
+
import {
|
|
7
|
+
getImportName,
|
|
8
|
+
removeImportFromDeclaration,
|
|
9
|
+
} from "../../shared/transformFactories/helpers/manageImports"
|
|
10
|
+
|
|
11
|
+
const SCOPE = "mergeFieldIntoInput"
|
|
12
|
+
|
|
13
|
+
const transform: Transform = (fileInfo, api) => {
|
|
14
|
+
const j = api.jscodeshift
|
|
15
|
+
const source = j(fileInfo.source)
|
|
16
|
+
|
|
17
|
+
const fieldLocalName = getImportName(
|
|
18
|
+
"Field",
|
|
19
|
+
"@planningcenter/tapestry-react",
|
|
20
|
+
{ j, source }
|
|
21
|
+
)
|
|
22
|
+
if (!fieldLocalName) return null
|
|
23
|
+
|
|
24
|
+
const inputLocalName = getImportName(
|
|
25
|
+
"Input",
|
|
26
|
+
"@planningcenter/tapestry-react",
|
|
27
|
+
{ j, source }
|
|
28
|
+
)
|
|
29
|
+
if (!inputLocalName) return null
|
|
30
|
+
|
|
31
|
+
let hasChanges = false
|
|
32
|
+
let anyFieldRemoved = false
|
|
33
|
+
|
|
34
|
+
source.find(j.JSXElement).forEach((path) => {
|
|
35
|
+
const el = path.value
|
|
36
|
+
const opening = el.openingElement
|
|
37
|
+
|
|
38
|
+
if (opening.name.type !== "JSXIdentifier") return
|
|
39
|
+
if (opening.name.name !== fieldLocalName) return
|
|
40
|
+
|
|
41
|
+
const elementChildren = (el.children || []).filter(
|
|
42
|
+
(child) =>
|
|
43
|
+
child.type !== "JSXText" || (child as JSXText).value.trim() !== ""
|
|
44
|
+
)
|
|
45
|
+
|
|
46
|
+
const inputChildren = elementChildren.filter((child) => {
|
|
47
|
+
if (child.type !== "JSXElement") return false
|
|
48
|
+
const childOpening = (child as JSXElement).openingElement
|
|
49
|
+
return (
|
|
50
|
+
childOpening.name.type === "JSXIdentifier" &&
|
|
51
|
+
childOpening.name.name === inputLocalName
|
|
52
|
+
)
|
|
53
|
+
}) as JSXElement[]
|
|
54
|
+
|
|
55
|
+
const hasRenderSide = orConditions(
|
|
56
|
+
hasAttribute("renderLeft"),
|
|
57
|
+
hasAttribute("renderRight")
|
|
58
|
+
)
|
|
59
|
+
|
|
60
|
+
// Case: exactly 1 child and it is an Input — merge props and unwrap
|
|
61
|
+
if (elementChildren.length === 1 && inputChildren.length === 1) {
|
|
62
|
+
const inputEl = inputChildren[0]
|
|
63
|
+
|
|
64
|
+
// Skip inputs with renderLeft/renderRight — they are not transformable
|
|
65
|
+
if (hasRenderSide(inputEl)) return
|
|
66
|
+
|
|
67
|
+
const fieldAttrs = opening.attributes || []
|
|
68
|
+
|
|
69
|
+
for (const attr of fieldAttrs) {
|
|
70
|
+
if (attr.type !== "JSXAttribute") continue
|
|
71
|
+
if (attr.name.type !== "JSXIdentifier") continue
|
|
72
|
+
|
|
73
|
+
const attrName = attr.name.name
|
|
74
|
+
const inputAttrs = inputEl.openingElement.attributes || []
|
|
75
|
+
|
|
76
|
+
if (attrName === "label") {
|
|
77
|
+
const hasLabel = inputAttrs.some(
|
|
78
|
+
(a) =>
|
|
79
|
+
a.type === "JSXAttribute" &&
|
|
80
|
+
a.name?.type === "JSXIdentifier" &&
|
|
81
|
+
a.name.name === "label"
|
|
82
|
+
)
|
|
83
|
+
if (hasLabel) {
|
|
84
|
+
addComment({
|
|
85
|
+
element: inputEl,
|
|
86
|
+
j,
|
|
87
|
+
scope: SCOPE,
|
|
88
|
+
source,
|
|
89
|
+
text: "Field had label prop but Input already has label. Please migrate manually.",
|
|
90
|
+
})
|
|
91
|
+
} else {
|
|
92
|
+
inputEl.openingElement.attributes.push(attr)
|
|
93
|
+
}
|
|
94
|
+
} else if (attrName === "feedbackText") {
|
|
95
|
+
const hasDescription = inputAttrs.some(
|
|
96
|
+
(a) =>
|
|
97
|
+
a.type === "JSXAttribute" &&
|
|
98
|
+
a.name?.type === "JSXIdentifier" &&
|
|
99
|
+
a.name.name === "description"
|
|
100
|
+
)
|
|
101
|
+
if (hasDescription) {
|
|
102
|
+
addComment({
|
|
103
|
+
element: inputEl,
|
|
104
|
+
j,
|
|
105
|
+
scope: SCOPE,
|
|
106
|
+
source,
|
|
107
|
+
text: "Field had feedbackText prop but Input already has description. Please migrate manually.",
|
|
108
|
+
})
|
|
109
|
+
} else {
|
|
110
|
+
const newAttr = j.jsxAttribute(
|
|
111
|
+
j.jsxIdentifier("description"),
|
|
112
|
+
attr.value
|
|
113
|
+
)
|
|
114
|
+
inputEl.openingElement.attributes.push(newAttr)
|
|
115
|
+
}
|
|
116
|
+
} else if (attrName === "state") {
|
|
117
|
+
const hasState = inputAttrs.some(
|
|
118
|
+
(a) =>
|
|
119
|
+
a.type === "JSXAttribute" &&
|
|
120
|
+
a.name?.type === "JSXIdentifier" &&
|
|
121
|
+
a.name.name === "state"
|
|
122
|
+
)
|
|
123
|
+
if (hasState) {
|
|
124
|
+
addComment({
|
|
125
|
+
element: inputEl,
|
|
126
|
+
j,
|
|
127
|
+
scope: SCOPE,
|
|
128
|
+
source,
|
|
129
|
+
text: "Field had state prop but Input already has state. Please migrate manually.",
|
|
130
|
+
})
|
|
131
|
+
} else {
|
|
132
|
+
inputEl.openingElement.attributes.push(attr)
|
|
133
|
+
}
|
|
134
|
+
} else {
|
|
135
|
+
addComment({
|
|
136
|
+
element: inputEl,
|
|
137
|
+
j,
|
|
138
|
+
scope: SCOPE,
|
|
139
|
+
source,
|
|
140
|
+
text: `Field prop '${attrName}' is not supported by Input. Please migrate manually.`,
|
|
141
|
+
})
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
const parent = path.parent?.value
|
|
146
|
+
if (parent?.children) {
|
|
147
|
+
const idx = parent.children.indexOf(el)
|
|
148
|
+
if (idx === -1) return
|
|
149
|
+
parent.children.splice(idx, 1, ...(el.children || []))
|
|
150
|
+
} else {
|
|
151
|
+
// Root JSX (e.g. directly inside return parens) — use path.replace
|
|
152
|
+
const nonWsChildren = (el.children || []).filter(
|
|
153
|
+
(child) =>
|
|
154
|
+
child.type !== "JSXText" || (child as JSXText).value.trim() !== ""
|
|
155
|
+
)
|
|
156
|
+
if (nonWsChildren.length === 1) {
|
|
157
|
+
path.replace(nonWsChildren[0])
|
|
158
|
+
} else {
|
|
159
|
+
path.replace(
|
|
160
|
+
j.jsxFragment(
|
|
161
|
+
j.jsxOpeningFragment(),
|
|
162
|
+
j.jsxClosingFragment(),
|
|
163
|
+
el.children || []
|
|
164
|
+
)
|
|
165
|
+
)
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
hasChanges = true
|
|
169
|
+
anyFieldRemoved = true
|
|
170
|
+
return
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
// Case: more than 1 non-whitespace child — comment each Input child, leave Field
|
|
174
|
+
if (elementChildren.length > 1) {
|
|
175
|
+
for (const child of inputChildren) {
|
|
176
|
+
// Skip inputs with renderLeft/renderRight — they are not transformable
|
|
177
|
+
if (hasRenderSide(child)) continue
|
|
178
|
+
addComment({
|
|
179
|
+
element: child,
|
|
180
|
+
j,
|
|
181
|
+
scope: SCOPE,
|
|
182
|
+
source,
|
|
183
|
+
text: "Field has multiple children and cannot be auto-merged into Input. Please migrate manually.",
|
|
184
|
+
})
|
|
185
|
+
hasChanges = true
|
|
186
|
+
}
|
|
187
|
+
return
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
// Case: exactly 1 child but not an Input — skip without comment
|
|
191
|
+
})
|
|
192
|
+
|
|
193
|
+
// Remove Field from imports only if all Field usages were converted
|
|
194
|
+
if (anyFieldRemoved) {
|
|
195
|
+
const stillUsesField =
|
|
196
|
+
source.find(j.JSXOpeningElement, {
|
|
197
|
+
name: { name: fieldLocalName },
|
|
198
|
+
}).length > 0
|
|
199
|
+
|
|
200
|
+
if (!stillUsesField) {
|
|
201
|
+
const fieldImports = source.find(j.ImportDeclaration, {
|
|
202
|
+
source: { value: "@planningcenter/tapestry-react" },
|
|
203
|
+
})
|
|
204
|
+
for (let i = 0; i < fieldImports.length; i++) {
|
|
205
|
+
removeImportFromDeclaration(fieldImports.at(i), "Field")
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
return hasChanges ? source.toSource() : null
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
export default transform
|
|
@@ -0,0 +1,458 @@
|
|
|
1
|
+
import jscodeshift from "jscodeshift"
|
|
2
|
+
import { describe, expect, it } from "vitest"
|
|
3
|
+
|
|
4
|
+
import transform from "./mergeInputLabel"
|
|
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("mergeInputLabel transform", () => {
|
|
18
|
+
describe("basic merge", () => {
|
|
19
|
+
it("merges InputLabel with simple text into Input label prop inside Box", () => {
|
|
20
|
+
const input = `
|
|
21
|
+
import { Box, Input } from "@planningcenter/tapestry-react"
|
|
22
|
+
|
|
23
|
+
function Test() {
|
|
24
|
+
return (
|
|
25
|
+
<Box grow={1}>
|
|
26
|
+
<Input.InputLabel controls="input-address">Address</Input.InputLabel>
|
|
27
|
+
<Input id="input-address" disabled={isEditing} value={address} />
|
|
28
|
+
</Box>
|
|
29
|
+
)
|
|
30
|
+
}
|
|
31
|
+
`.trim()
|
|
32
|
+
|
|
33
|
+
const result = applyTransform(input)
|
|
34
|
+
expect(result).not.toBeNull()
|
|
35
|
+
expect(result).toContain('label="Address"')
|
|
36
|
+
expect(result).not.toContain("<Input.InputLabel")
|
|
37
|
+
})
|
|
38
|
+
|
|
39
|
+
it("merges InputLabel with simple text into Input label prop inside StackView", () => {
|
|
40
|
+
const input = `
|
|
41
|
+
import { Input, StackView } from "@planningcenter/tapestry-react"
|
|
42
|
+
|
|
43
|
+
function Test() {
|
|
44
|
+
return (
|
|
45
|
+
<StackView>
|
|
46
|
+
<Input.InputLabel controls="input-name">Name</Input.InputLabel>
|
|
47
|
+
<Input id="input-name" value={name} />
|
|
48
|
+
</StackView>
|
|
49
|
+
)
|
|
50
|
+
}
|
|
51
|
+
`.trim()
|
|
52
|
+
|
|
53
|
+
const result = applyTransform(input)
|
|
54
|
+
expect(result).not.toBeNull()
|
|
55
|
+
expect(result).toContain('label="Name"')
|
|
56
|
+
expect(result).not.toContain("<Input.InputLabel")
|
|
57
|
+
})
|
|
58
|
+
})
|
|
59
|
+
|
|
60
|
+
describe("StackView axis", () => {
|
|
61
|
+
it("skips horizontal StackView and returns null", () => {
|
|
62
|
+
const input = `
|
|
63
|
+
import { Input, StackView } from "@planningcenter/tapestry-react"
|
|
64
|
+
|
|
65
|
+
function Test() {
|
|
66
|
+
return (
|
|
67
|
+
<StackView axis="horizontal">
|
|
68
|
+
<Input.InputLabel controls="input-name">Name</Input.InputLabel>
|
|
69
|
+
<Input id="input-name" value={name} />
|
|
70
|
+
</StackView>
|
|
71
|
+
)
|
|
72
|
+
}
|
|
73
|
+
`.trim()
|
|
74
|
+
|
|
75
|
+
const result = applyTransform(input)
|
|
76
|
+
expect(result).toBeNull()
|
|
77
|
+
})
|
|
78
|
+
|
|
79
|
+
it('skips horizontal StackView with axis={"horizontal"} expression container', () => {
|
|
80
|
+
const input = `
|
|
81
|
+
import { Input, StackView } from "@planningcenter/tapestry-react"
|
|
82
|
+
|
|
83
|
+
function Test() {
|
|
84
|
+
return (
|
|
85
|
+
<StackView axis={"horizontal"}>
|
|
86
|
+
<Input.InputLabel controls="input-name">Name</Input.InputLabel>
|
|
87
|
+
<Input id="input-name" value={name} />
|
|
88
|
+
</StackView>
|
|
89
|
+
)
|
|
90
|
+
}
|
|
91
|
+
`.trim()
|
|
92
|
+
|
|
93
|
+
const result = applyTransform(input)
|
|
94
|
+
expect(result).toBeNull()
|
|
95
|
+
})
|
|
96
|
+
|
|
97
|
+
it("processes vertical StackView (no axis prop)", () => {
|
|
98
|
+
const input = `
|
|
99
|
+
import { Input, StackView } from "@planningcenter/tapestry-react"
|
|
100
|
+
|
|
101
|
+
function Test() {
|
|
102
|
+
return (
|
|
103
|
+
<StackView axis="vertical">
|
|
104
|
+
<Input.InputLabel controls="input-name">Name</Input.InputLabel>
|
|
105
|
+
<Input id="input-name" value={name} />
|
|
106
|
+
</StackView>
|
|
107
|
+
)
|
|
108
|
+
}
|
|
109
|
+
`.trim()
|
|
110
|
+
|
|
111
|
+
const result = applyTransform(input)
|
|
112
|
+
expect(result).not.toBeNull()
|
|
113
|
+
expect(result).toContain('label="Name"')
|
|
114
|
+
})
|
|
115
|
+
})
|
|
116
|
+
|
|
117
|
+
describe("controls/id matching", () => {
|
|
118
|
+
it("still merges when controls and id are different strings", () => {
|
|
119
|
+
const input = `
|
|
120
|
+
import { Box, Input } from "@planningcenter/tapestry-react"
|
|
121
|
+
|
|
122
|
+
function Test() {
|
|
123
|
+
return (
|
|
124
|
+
<Box>
|
|
125
|
+
<Input.InputLabel controls="label-id">Email</Input.InputLabel>
|
|
126
|
+
<Input id="input-id" value={email} />
|
|
127
|
+
</Box>
|
|
128
|
+
)
|
|
129
|
+
}
|
|
130
|
+
`.trim()
|
|
131
|
+
|
|
132
|
+
const result = applyTransform(input)
|
|
133
|
+
expect(result).not.toBeNull()
|
|
134
|
+
expect(result).toContain('label="Email"')
|
|
135
|
+
})
|
|
136
|
+
|
|
137
|
+
it("still merges when controls and id are dynamic expressions", () => {
|
|
138
|
+
const input = `
|
|
139
|
+
import { Box, Input } from "@planningcenter/tapestry-react"
|
|
140
|
+
|
|
141
|
+
function Test() {
|
|
142
|
+
return (
|
|
143
|
+
<Box>
|
|
144
|
+
<Input.InputLabel controls={inputId}>Email</Input.InputLabel>
|
|
145
|
+
<Input id={inputId} value={email} />
|
|
146
|
+
</Box>
|
|
147
|
+
)
|
|
148
|
+
}
|
|
149
|
+
`.trim()
|
|
150
|
+
|
|
151
|
+
const result = applyTransform(input)
|
|
152
|
+
expect(result).not.toBeNull()
|
|
153
|
+
expect(result).toContain('label="Email"')
|
|
154
|
+
})
|
|
155
|
+
})
|
|
156
|
+
|
|
157
|
+
describe("complex InputLabel children", () => {
|
|
158
|
+
it("adds comment to Input and removes InputLabel when children are complex JSX", () => {
|
|
159
|
+
const input = `
|
|
160
|
+
import { Box, Input } from "@planningcenter/tapestry-react"
|
|
161
|
+
|
|
162
|
+
function Test() {
|
|
163
|
+
return (
|
|
164
|
+
<Box>
|
|
165
|
+
<Input.InputLabel controls="input-id">
|
|
166
|
+
<span>Address <em>*</em></span>
|
|
167
|
+
</Input.InputLabel>
|
|
168
|
+
<Input id="input-id" value={address} />
|
|
169
|
+
</Box>
|
|
170
|
+
)
|
|
171
|
+
}
|
|
172
|
+
`.trim()
|
|
173
|
+
|
|
174
|
+
const result = applyTransform(input)
|
|
175
|
+
expect(result).not.toBeNull()
|
|
176
|
+
expect(result).not.toContain("<Input.InputLabel")
|
|
177
|
+
expect(result).toContain("TODO: tapestry-migration (mergeInputLabel)")
|
|
178
|
+
expect(result).toContain("complex and cannot be auto-converted")
|
|
179
|
+
expect(result).not.toContain('label="')
|
|
180
|
+
})
|
|
181
|
+
|
|
182
|
+
it("adds comment when children are dynamic expressions", () => {
|
|
183
|
+
const input = `
|
|
184
|
+
import { Box, Input } from "@planningcenter/tapestry-react"
|
|
185
|
+
|
|
186
|
+
function Test() {
|
|
187
|
+
return (
|
|
188
|
+
<Box>
|
|
189
|
+
<Input.InputLabel controls="input-id">{labelText}</Input.InputLabel>
|
|
190
|
+
<Input id="input-id" value={val} />
|
|
191
|
+
</Box>
|
|
192
|
+
)
|
|
193
|
+
}
|
|
194
|
+
`.trim()
|
|
195
|
+
|
|
196
|
+
const result = applyTransform(input)
|
|
197
|
+
expect(result).not.toBeNull()
|
|
198
|
+
expect(result).not.toContain("<Input.InputLabel")
|
|
199
|
+
expect(result).toContain("TODO: tapestry-migration (mergeInputLabel)")
|
|
200
|
+
expect(result).toContain("complex and cannot be auto-converted")
|
|
201
|
+
})
|
|
202
|
+
})
|
|
203
|
+
|
|
204
|
+
describe("InputLabel extra props", () => {
|
|
205
|
+
it("adds comment listing removed props when InputLabel has non-controls props", () => {
|
|
206
|
+
const input = `
|
|
207
|
+
import { Box, Input } from "@planningcenter/tapestry-react"
|
|
208
|
+
|
|
209
|
+
function Test() {
|
|
210
|
+
return (
|
|
211
|
+
<Box>
|
|
212
|
+
<Input.InputLabel controls="input-address" color={token("--t-text-color")}>Address</Input.InputLabel>
|
|
213
|
+
<Input id="input-address" value={address} />
|
|
214
|
+
</Box>
|
|
215
|
+
)
|
|
216
|
+
}
|
|
217
|
+
`.trim()
|
|
218
|
+
|
|
219
|
+
const result = applyTransform(input)
|
|
220
|
+
expect(result).not.toBeNull()
|
|
221
|
+
expect(result).toContain('label="Address"')
|
|
222
|
+
expect(result).toContain(
|
|
223
|
+
"The following props from Input.InputLabel were removed: color"
|
|
224
|
+
)
|
|
225
|
+
expect(result).not.toContain("<Input.InputLabel")
|
|
226
|
+
})
|
|
227
|
+
|
|
228
|
+
it("does not add removed props comment when only controls prop is present", () => {
|
|
229
|
+
const input = `
|
|
230
|
+
import { Box, Input } from "@planningcenter/tapestry-react"
|
|
231
|
+
|
|
232
|
+
function Test() {
|
|
233
|
+
return (
|
|
234
|
+
<Box>
|
|
235
|
+
<Input.InputLabel controls="input-id">Name</Input.InputLabel>
|
|
236
|
+
<Input id="input-id" value={name} />
|
|
237
|
+
</Box>
|
|
238
|
+
)
|
|
239
|
+
}
|
|
240
|
+
`.trim()
|
|
241
|
+
|
|
242
|
+
const result = applyTransform(input)
|
|
243
|
+
expect(result).not.toBeNull()
|
|
244
|
+
expect(result).toContain('label="Name"')
|
|
245
|
+
expect(result).not.toContain("were removed")
|
|
246
|
+
})
|
|
247
|
+
})
|
|
248
|
+
|
|
249
|
+
describe("whitespace handling", () => {
|
|
250
|
+
it("handles whitespace text nodes between InputLabel and Input", () => {
|
|
251
|
+
const input = `
|
|
252
|
+
import { Box, Input } from "@planningcenter/tapestry-react"
|
|
253
|
+
|
|
254
|
+
function Test() {
|
|
255
|
+
return (
|
|
256
|
+
<Box>
|
|
257
|
+
<Input.InputLabel controls="input-id">Phone</Input.InputLabel>
|
|
258
|
+
|
|
259
|
+
<Input id="input-id" value={phone} />
|
|
260
|
+
</Box>
|
|
261
|
+
)
|
|
262
|
+
}
|
|
263
|
+
`.trim()
|
|
264
|
+
|
|
265
|
+
const result = applyTransform(input)
|
|
266
|
+
expect(result).not.toBeNull()
|
|
267
|
+
expect(result).toContain('label="Phone"')
|
|
268
|
+
expect(result).not.toContain("<Input.InputLabel")
|
|
269
|
+
})
|
|
270
|
+
})
|
|
271
|
+
|
|
272
|
+
describe("existing label prop on Input", () => {
|
|
273
|
+
it("adds comment and does not duplicate label when Input already has label prop", () => {
|
|
274
|
+
const input = `
|
|
275
|
+
import { Box, Input } from "@planningcenter/tapestry-react"
|
|
276
|
+
|
|
277
|
+
function Test() {
|
|
278
|
+
return (
|
|
279
|
+
<Box>
|
|
280
|
+
<Input.InputLabel controls="input-id">New Label</Input.InputLabel>
|
|
281
|
+
<Input id="input-id" label="Existing Label" value={val} />
|
|
282
|
+
</Box>
|
|
283
|
+
)
|
|
284
|
+
}
|
|
285
|
+
`.trim()
|
|
286
|
+
|
|
287
|
+
const result = applyTransform(input)
|
|
288
|
+
expect(result).not.toBeNull()
|
|
289
|
+
expect(result).not.toContain("<Input.InputLabel")
|
|
290
|
+
expect(result).toContain('label="Existing Label"')
|
|
291
|
+
expect(result).toContain("TODO: tapestry-migration (mergeInputLabel)")
|
|
292
|
+
expect(result).toContain("already has a label prop")
|
|
293
|
+
expect(result).toContain("New Label")
|
|
294
|
+
// Should not add a second label attribute
|
|
295
|
+
expect(result?.match(/label=/g)?.length).toBe(1)
|
|
296
|
+
})
|
|
297
|
+
})
|
|
298
|
+
|
|
299
|
+
describe("multiple pairs", () => {
|
|
300
|
+
it("merges multiple InputLabel+Input pairs in the same parent", () => {
|
|
301
|
+
const input = `
|
|
302
|
+
import { Box, Input } from "@planningcenter/tapestry-react"
|
|
303
|
+
|
|
304
|
+
function Test() {
|
|
305
|
+
return (
|
|
306
|
+
<Box>
|
|
307
|
+
<Input.InputLabel controls="input-first">First Name</Input.InputLabel>
|
|
308
|
+
<Input id="input-first" value={firstName} />
|
|
309
|
+
<Input.InputLabel controls="input-last">Last Name</Input.InputLabel>
|
|
310
|
+
<Input id="input-last" value={lastName} />
|
|
311
|
+
</Box>
|
|
312
|
+
)
|
|
313
|
+
}
|
|
314
|
+
`.trim()
|
|
315
|
+
|
|
316
|
+
const result = applyTransform(input)
|
|
317
|
+
expect(result).not.toBeNull()
|
|
318
|
+
expect(result).toContain('label="First Name"')
|
|
319
|
+
expect(result).toContain('label="Last Name"')
|
|
320
|
+
expect(result).not.toContain("<Input.InputLabel")
|
|
321
|
+
})
|
|
322
|
+
})
|
|
323
|
+
|
|
324
|
+
describe("no Input import", () => {
|
|
325
|
+
it("returns null when Input is not imported from tapestry-react", () => {
|
|
326
|
+
const input = `
|
|
327
|
+
import { Input } from "@planningcenter/tapestry"
|
|
328
|
+
|
|
329
|
+
function Test() {
|
|
330
|
+
return (
|
|
331
|
+
<Box>
|
|
332
|
+
<Input id="input-id" value={val} />
|
|
333
|
+
</Box>
|
|
334
|
+
)
|
|
335
|
+
}
|
|
336
|
+
`.trim()
|
|
337
|
+
|
|
338
|
+
const result = applyTransform(input)
|
|
339
|
+
expect(result).toBeNull()
|
|
340
|
+
})
|
|
341
|
+
|
|
342
|
+
it("returns null when no Input import exists", () => {
|
|
343
|
+
const input = `
|
|
344
|
+
import { Button } from "@planningcenter/tapestry-react"
|
|
345
|
+
|
|
346
|
+
function Test() {
|
|
347
|
+
return <Button label="Click" />
|
|
348
|
+
}
|
|
349
|
+
`.trim()
|
|
350
|
+
|
|
351
|
+
const result = applyTransform(input)
|
|
352
|
+
expect(result).toBeNull()
|
|
353
|
+
})
|
|
354
|
+
})
|
|
355
|
+
|
|
356
|
+
describe("aliased imports", () => {
|
|
357
|
+
it("handles aliased Input imports", () => {
|
|
358
|
+
const input = `
|
|
359
|
+
import { Box, Input as TapestryInput } from "@planningcenter/tapestry-react"
|
|
360
|
+
|
|
361
|
+
function Test() {
|
|
362
|
+
return (
|
|
363
|
+
<Box>
|
|
364
|
+
<TapestryInput.InputLabel controls="input-id">City</TapestryInput.InputLabel>
|
|
365
|
+
<TapestryInput id="input-id" value={city} />
|
|
366
|
+
</Box>
|
|
367
|
+
)
|
|
368
|
+
}
|
|
369
|
+
`.trim()
|
|
370
|
+
|
|
371
|
+
const result = applyTransform(input)
|
|
372
|
+
expect(result).not.toBeNull()
|
|
373
|
+
expect(result).toContain('label="City"')
|
|
374
|
+
expect(result).not.toContain("<Input.InputLabel")
|
|
375
|
+
})
|
|
376
|
+
})
|
|
377
|
+
|
|
378
|
+
describe("non-Box/StackView parents", () => {
|
|
379
|
+
it("skips InputLabel+Input pairs inside non-Box/StackView parents", () => {
|
|
380
|
+
const input = `
|
|
381
|
+
import { Box, Input } from "@planningcenter/tapestry-react"
|
|
382
|
+
|
|
383
|
+
function Test() {
|
|
384
|
+
return (
|
|
385
|
+
<div>
|
|
386
|
+
<Input.InputLabel controls="input-id">Name</Input.InputLabel>
|
|
387
|
+
<Input id="input-id" value={name} />
|
|
388
|
+
</div>
|
|
389
|
+
)
|
|
390
|
+
}
|
|
391
|
+
`.trim()
|
|
392
|
+
|
|
393
|
+
const result = applyTransform(input)
|
|
394
|
+
expect(result).toBeNull()
|
|
395
|
+
})
|
|
396
|
+
|
|
397
|
+
it("skips Box/StackView not imported from @planningcenter/tapestry-react", () => {
|
|
398
|
+
const input = `
|
|
399
|
+
import { Input } from "@planningcenter/tapestry-react"
|
|
400
|
+
import { Box } from "./my-components"
|
|
401
|
+
|
|
402
|
+
function Test() {
|
|
403
|
+
return (
|
|
404
|
+
<Box>
|
|
405
|
+
<Input.InputLabel controls="input-id">Name</Input.InputLabel>
|
|
406
|
+
<Input id="input-id" value={name} />
|
|
407
|
+
</Box>
|
|
408
|
+
)
|
|
409
|
+
}
|
|
410
|
+
`.trim()
|
|
411
|
+
|
|
412
|
+
const result = applyTransform(input)
|
|
413
|
+
expect(result).toBeNull()
|
|
414
|
+
})
|
|
415
|
+
})
|
|
416
|
+
|
|
417
|
+
describe("aliased Box/StackView imports", () => {
|
|
418
|
+
it("handles aliased Box import", () => {
|
|
419
|
+
const input = `
|
|
420
|
+
import { Box as TapestryBox, Input } from "@planningcenter/tapestry-react"
|
|
421
|
+
|
|
422
|
+
function Test() {
|
|
423
|
+
return (
|
|
424
|
+
<TapestryBox>
|
|
425
|
+
<Input.InputLabel controls="input-id">Name</Input.InputLabel>
|
|
426
|
+
<Input id="input-id" value={name} />
|
|
427
|
+
</TapestryBox>
|
|
428
|
+
)
|
|
429
|
+
}
|
|
430
|
+
`.trim()
|
|
431
|
+
|
|
432
|
+
const result = applyTransform(input)
|
|
433
|
+
expect(result).not.toBeNull()
|
|
434
|
+
expect(result).toContain('label="Name"')
|
|
435
|
+
expect(result).not.toContain("<Input.InputLabel")
|
|
436
|
+
})
|
|
437
|
+
|
|
438
|
+
it("handles aliased StackView import", () => {
|
|
439
|
+
const input = `
|
|
440
|
+
import { Input, StackView as Stack } from "@planningcenter/tapestry-react"
|
|
441
|
+
|
|
442
|
+
function Test() {
|
|
443
|
+
return (
|
|
444
|
+
<Stack>
|
|
445
|
+
<Input.InputLabel controls="input-id">Name</Input.InputLabel>
|
|
446
|
+
<Input id="input-id" value={name} />
|
|
447
|
+
</Stack>
|
|
448
|
+
)
|
|
449
|
+
}
|
|
450
|
+
`.trim()
|
|
451
|
+
|
|
452
|
+
const result = applyTransform(input)
|
|
453
|
+
expect(result).not.toBeNull()
|
|
454
|
+
expect(result).toContain('label="Name"')
|
|
455
|
+
expect(result).not.toContain("<Input.InputLabel")
|
|
456
|
+
})
|
|
457
|
+
})
|
|
458
|
+
})
|