@planningcenter/tapestry-migration-cli 2.3.0-rc.11 → 2.3.0-rc.13
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 +2 -2
- package/src/components/button/transforms/convertStyleProps.test.ts +4 -5
- package/src/components/link/index.ts +27 -0
- package/src/components/link/transforms/inlineToKind.test.ts +308 -0
- package/src/components/link/transforms/inlineToKind.ts +51 -0
- package/src/components/link/transforms/toToHref.test.ts +245 -0
- package/src/components/link/transforms/toToHref.ts +14 -0
- package/src/components/shared/actions/removeAttribute.ts +9 -2
- package/src/components/shared/actions/transformElementName.ts +23 -9
- package/src/components/shared/transformFactories/attributeTransformFactory.test.ts +83 -0
- package/src/components/shared/transformFactories/attributeTransformFactory.ts +21 -14
- package/src/components/shared/transformFactories/componentTransformFactory.test.ts +85 -2
- package/src/components/shared/transformFactories/componentTransformFactory.ts +41 -22
- package/src/components/shared/transformFactories/helpers/findJSXElements.ts +37 -0
- package/src/components/shared/transformFactories/stylePropTransformFactory.ts +2 -27
- package/src/components/shared/types.ts +19 -1
- package/src/index.ts +7 -2
- package/src/jscodeshiftRunner.ts +7 -0
- package/src/reportGenerator.ts +450 -0
- package/src/shared/types.ts +1 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@planningcenter/tapestry-migration-cli",
|
|
3
|
-
"version": "2.3.0-rc.
|
|
3
|
+
"version": "2.3.0-rc.13",
|
|
4
4
|
"description": "CLI tool for Tapestry migrations",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/index.js",
|
|
@@ -51,5 +51,5 @@
|
|
|
51
51
|
"publishConfig": {
|
|
52
52
|
"access": "public"
|
|
53
53
|
},
|
|
54
|
-
"gitHead": "
|
|
54
|
+
"gitHead": "bb096c975d4d0a176233e6a335d89fb836d78b74"
|
|
55
55
|
}
|
|
@@ -110,9 +110,9 @@ describe("convertStyleProps transform", () => {
|
|
|
110
110
|
|
|
111
111
|
const result = applyTransform(source)
|
|
112
112
|
|
|
113
|
-
expect(result).not.toContain('height="40px"')
|
|
113
|
+
expect(result).not.toContain('<Button height="40px"')
|
|
114
114
|
expect(result).toContain(
|
|
115
|
-
'TODO: tapestry-migration (
|
|
115
|
+
'TODO: tapestry-migration (styleProp): height has been removed as this is covered by default styling: height="40px"'
|
|
116
116
|
)
|
|
117
117
|
expect(result).toContain('kind="primary"')
|
|
118
118
|
})
|
|
@@ -128,7 +128,7 @@ describe("convertStyleProps transform", () => {
|
|
|
128
128
|
|
|
129
129
|
const result = applyTransform(source)
|
|
130
130
|
|
|
131
|
-
expect(result).not.toContain("height={buttonHeight}")
|
|
131
|
+
expect(result).not.toContain("<Button height={buttonHeight}")
|
|
132
132
|
expect(result).toContain("TODO: tapestry-migration")
|
|
133
133
|
expect(result).toContain("buttonHeight")
|
|
134
134
|
expect(result).toContain("disabled")
|
|
@@ -219,10 +219,9 @@ describe("convertStyleProps transform", () => {
|
|
|
219
219
|
expect(result).toContain('marginLeft: "128px"')
|
|
220
220
|
expect(result).toContain("}}>")
|
|
221
221
|
expect(result).toContain(
|
|
222
|
-
'TODO: tapestry-migration (
|
|
222
|
+
'TODO: tapestry-migration (styleProp): height has been removed as this is covered by default styling: height="40px"'
|
|
223
223
|
)
|
|
224
224
|
expect(result).not.toContain("marginLeft={16}")
|
|
225
|
-
expect(result).not.toContain('height="40px"')
|
|
226
225
|
expect(result).not.toContain('distribution="center"')
|
|
227
226
|
expect(result).not.toContain('paddingTop="8px"')
|
|
228
227
|
expect(result).toContain('kind="primary"')
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { Transform } from "jscodeshift"
|
|
2
|
+
|
|
3
|
+
import inlineToKind from "./transforms/inlineToKind"
|
|
4
|
+
import toToHref from "./transforms/toToHref"
|
|
5
|
+
|
|
6
|
+
const transform: Transform = (fileInfo, api, options) => {
|
|
7
|
+
let currentSource = fileInfo.source
|
|
8
|
+
let hasAnyChanges = false
|
|
9
|
+
|
|
10
|
+
const transforms: Transform[] = [inlineToKind, toToHref]
|
|
11
|
+
|
|
12
|
+
for (const individualTransform of transforms) {
|
|
13
|
+
const result = individualTransform(
|
|
14
|
+
{ ...fileInfo, source: currentSource },
|
|
15
|
+
api,
|
|
16
|
+
options
|
|
17
|
+
)
|
|
18
|
+
if (result && result !== currentSource) {
|
|
19
|
+
currentSource = result as string
|
|
20
|
+
hasAnyChanges = true
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
return hasAnyChanges ? currentSource : null
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export default transform
|
|
@@ -0,0 +1,308 @@
|
|
|
1
|
+
import jscodeshift from "jscodeshift"
|
|
2
|
+
import { describe, expect, it } from "vitest"
|
|
3
|
+
|
|
4
|
+
import transform from "./inlineToKind"
|
|
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("inlineToKind transform", () => {
|
|
18
|
+
describe("basic transformations", () => {
|
|
19
|
+
it("should transform Link.Inline to Link with kind='inline-text'", () => {
|
|
20
|
+
const input = `
|
|
21
|
+
import { Link } from "@planningcenter/tapestry-react"
|
|
22
|
+
|
|
23
|
+
export default function Test() {
|
|
24
|
+
return <Link.Inline href="/profile">Profile</Link.Inline>
|
|
25
|
+
}
|
|
26
|
+
`.trim()
|
|
27
|
+
|
|
28
|
+
const expected = `
|
|
29
|
+
import { Link } from "@planningcenter/tapestry-react"
|
|
30
|
+
|
|
31
|
+
export default function Test() {
|
|
32
|
+
return <Link href="/profile" kind="inline-text">Profile</Link>;
|
|
33
|
+
}
|
|
34
|
+
`.trim()
|
|
35
|
+
|
|
36
|
+
const result = applyTransform(input)
|
|
37
|
+
expect(result?.trim()).toBe(expected)
|
|
38
|
+
})
|
|
39
|
+
|
|
40
|
+
it("should preserve all existing props when transforming Link.Inline", () => {
|
|
41
|
+
const input = `
|
|
42
|
+
import { Link } from "@planningcenter/tapestry-react"
|
|
43
|
+
|
|
44
|
+
export default function Test() {
|
|
45
|
+
return (
|
|
46
|
+
<Link.Inline
|
|
47
|
+
href="/dashboard"
|
|
48
|
+
className="nav-link"
|
|
49
|
+
target="_blank"
|
|
50
|
+
rel="noopener"
|
|
51
|
+
data-testid="profile-link"
|
|
52
|
+
>
|
|
53
|
+
Dashboard
|
|
54
|
+
</Link.Inline>
|
|
55
|
+
)
|
|
56
|
+
}
|
|
57
|
+
`.trim()
|
|
58
|
+
|
|
59
|
+
const result = applyTransform(input)
|
|
60
|
+
expect(result).toContain('kind="inline-text"')
|
|
61
|
+
expect(result).toContain('href="/dashboard"')
|
|
62
|
+
expect(result).toContain('className="nav-link"')
|
|
63
|
+
expect(result).toContain('target="_blank"')
|
|
64
|
+
expect(result).toContain('rel="noopener"')
|
|
65
|
+
expect(result).toContain('data-testid="profile-link"')
|
|
66
|
+
expect(result).toContain('kind="inline-text"')
|
|
67
|
+
expect(result).toContain("<Link")
|
|
68
|
+
expect(result).not.toContain("Link.Inline")
|
|
69
|
+
})
|
|
70
|
+
|
|
71
|
+
it("should handle self-closing Link.Inline tags", () => {
|
|
72
|
+
const input = `
|
|
73
|
+
import { Link } from "@planningcenter/tapestry-react"
|
|
74
|
+
|
|
75
|
+
export default function Test() {
|
|
76
|
+
return <Link.Inline href="/dashboard" />
|
|
77
|
+
}
|
|
78
|
+
`.trim()
|
|
79
|
+
|
|
80
|
+
const expected = `
|
|
81
|
+
import { Link } from "@planningcenter/tapestry-react"
|
|
82
|
+
|
|
83
|
+
export default function Test() {
|
|
84
|
+
return <Link href="/dashboard" kind="inline-text" />;
|
|
85
|
+
}
|
|
86
|
+
`.trim()
|
|
87
|
+
|
|
88
|
+
const result = applyTransform(input)
|
|
89
|
+
expect(result?.trim()).toBe(expected)
|
|
90
|
+
})
|
|
91
|
+
|
|
92
|
+
it("should handle multiple Link.Inline components", () => {
|
|
93
|
+
const input = `
|
|
94
|
+
import { Link } from "@planningcenter/tapestry-react"
|
|
95
|
+
|
|
96
|
+
export default function Test() {
|
|
97
|
+
return (
|
|
98
|
+
<div>
|
|
99
|
+
<Link.Inline href="/home">Home</Link.Inline>
|
|
100
|
+
<Link.Inline href="/about">About</Link.Inline>
|
|
101
|
+
</div>
|
|
102
|
+
)
|
|
103
|
+
}
|
|
104
|
+
`.trim()
|
|
105
|
+
|
|
106
|
+
const result = applyTransform(input)
|
|
107
|
+
expect(result).toContain(
|
|
108
|
+
'<Link href="/home" kind="inline-text">Home</Link>'
|
|
109
|
+
)
|
|
110
|
+
expect(result).toContain(
|
|
111
|
+
'<Link href="/about" kind="inline-text">About</Link>'
|
|
112
|
+
)
|
|
113
|
+
expect(result).not.toContain("Link.Inline")
|
|
114
|
+
})
|
|
115
|
+
})
|
|
116
|
+
|
|
117
|
+
describe("mixed scenarios", () => {
|
|
118
|
+
it("should only transform Link.Inline and leave regular Link unchanged", () => {
|
|
119
|
+
const input = `
|
|
120
|
+
import { Link } from "@planningcenter/tapestry-react"
|
|
121
|
+
|
|
122
|
+
export default function Test() {
|
|
123
|
+
return (
|
|
124
|
+
<nav>
|
|
125
|
+
<Link href="/home">Home</Link>
|
|
126
|
+
<Link.Inline href="/profile">Profile</Link.Inline>
|
|
127
|
+
<Link as="button" href="/dashboard">Dashboard</Link>
|
|
128
|
+
</nav>
|
|
129
|
+
)
|
|
130
|
+
}
|
|
131
|
+
`.trim()
|
|
132
|
+
|
|
133
|
+
const result = applyTransform(input)
|
|
134
|
+
expect(result).toContain('<Link href="/home">Home</Link>')
|
|
135
|
+
expect(result).toContain(
|
|
136
|
+
'<Link href="/profile" kind="inline-text">Profile</Link>'
|
|
137
|
+
)
|
|
138
|
+
expect(result).toContain(
|
|
139
|
+
'<Link as="button" href="/dashboard">Dashboard</Link>'
|
|
140
|
+
)
|
|
141
|
+
expect(result).not.toContain("Link.Inline")
|
|
142
|
+
})
|
|
143
|
+
|
|
144
|
+
it("should handle aliased Link import", () => {
|
|
145
|
+
const input = `
|
|
146
|
+
import { Link as TapestryLink } from "@planningcenter/tapestry-react"
|
|
147
|
+
|
|
148
|
+
export default function Test() {
|
|
149
|
+
return <TapestryLink.Inline href="/profile">Profile</TapestryLink.Inline>
|
|
150
|
+
}
|
|
151
|
+
`.trim()
|
|
152
|
+
|
|
153
|
+
const result = applyTransform(input)
|
|
154
|
+
expect(result).toContain(
|
|
155
|
+
'<TapestryLink href="/profile" kind="inline-text">Profile</TapestryLink>'
|
|
156
|
+
)
|
|
157
|
+
expect(result).not.toContain("TapestryLink.Inline")
|
|
158
|
+
})
|
|
159
|
+
|
|
160
|
+
it("should handle complex JSX expressions", () => {
|
|
161
|
+
const input = `
|
|
162
|
+
import { Link } from "@planningcenter/tapestry-react"
|
|
163
|
+
|
|
164
|
+
export default function Test({ items, showInline }) {
|
|
165
|
+
return (
|
|
166
|
+
<div>
|
|
167
|
+
{items.map(item => (
|
|
168
|
+
<Link.Inline key={item.id} href={item.url}>{item.name}</Link.Inline>
|
|
169
|
+
))}
|
|
170
|
+
{showInline && <Link.Inline href="/conditional">Conditional</Link.Inline>}
|
|
171
|
+
</div>
|
|
172
|
+
)
|
|
173
|
+
}
|
|
174
|
+
`.trim()
|
|
175
|
+
|
|
176
|
+
const result = applyTransform(input)
|
|
177
|
+
expect(result).toContain(
|
|
178
|
+
'<Link key={item.id} href={item.url} kind="inline-text">{item.name}</Link>'
|
|
179
|
+
)
|
|
180
|
+
expect(result).toContain(
|
|
181
|
+
'<Link href="/conditional" kind="inline-text">Conditional</Link>'
|
|
182
|
+
)
|
|
183
|
+
expect(result).not.toContain("Link.Inline")
|
|
184
|
+
})
|
|
185
|
+
})
|
|
186
|
+
|
|
187
|
+
describe("edge cases", () => {
|
|
188
|
+
it("should not transform if Link is not imported from tapestry-react", () => {
|
|
189
|
+
const input = `
|
|
190
|
+
import { Link } from "react-router-dom"
|
|
191
|
+
|
|
192
|
+
export default function Test() {
|
|
193
|
+
return <Link.Inline to="/profile">Profile</Link.Inline>
|
|
194
|
+
}
|
|
195
|
+
`.trim()
|
|
196
|
+
|
|
197
|
+
const result = applyTransform(input)
|
|
198
|
+
expect(result).toBe(null) // No changes needed
|
|
199
|
+
})
|
|
200
|
+
|
|
201
|
+
it("should handle Link.Inline with dynamic props", () => {
|
|
202
|
+
const input = `
|
|
203
|
+
import { Link } from "@planningcenter/tapestry-react"
|
|
204
|
+
|
|
205
|
+
export default function Test({ url, isExternal }) {
|
|
206
|
+
return (
|
|
207
|
+
<Link.Inline
|
|
208
|
+
href={url}
|
|
209
|
+
target={isExternal ? "_blank" : "_self"}
|
|
210
|
+
className={\`link-\${isExternal ? 'external' : 'internal'}\`}
|
|
211
|
+
>
|
|
212
|
+
Dynamic Link
|
|
213
|
+
</Link.Inline>
|
|
214
|
+
)
|
|
215
|
+
}
|
|
216
|
+
`.trim()
|
|
217
|
+
|
|
218
|
+
const result = applyTransform(input)
|
|
219
|
+
expect(result).toContain('kind="inline-text"')
|
|
220
|
+
expect(result).toContain("<Link")
|
|
221
|
+
expect(result).toContain("href={url}")
|
|
222
|
+
expect(result).toContain('target={isExternal ? "_blank" : "_self"}')
|
|
223
|
+
expect(result).toContain(
|
|
224
|
+
"className={`link-${isExternal ? 'external' : 'internal'}`}"
|
|
225
|
+
)
|
|
226
|
+
expect(result).not.toContain("Link.Inline")
|
|
227
|
+
})
|
|
228
|
+
|
|
229
|
+
it("should handle nested Link.Inline components", () => {
|
|
230
|
+
const input = `
|
|
231
|
+
import { Link } from "@planningcenter/tapestry-react"
|
|
232
|
+
|
|
233
|
+
export default function Test() {
|
|
234
|
+
return (
|
|
235
|
+
<div>
|
|
236
|
+
<p>
|
|
237
|
+
Visit our <Link.Inline href="/about">about page</Link.Inline> for more info.
|
|
238
|
+
</p>
|
|
239
|
+
<span>
|
|
240
|
+
Or check the <Link.Inline href="/help">help section</Link.Inline>.
|
|
241
|
+
</span>
|
|
242
|
+
</div>
|
|
243
|
+
)
|
|
244
|
+
}
|
|
245
|
+
`.trim()
|
|
246
|
+
|
|
247
|
+
const result = applyTransform(input)
|
|
248
|
+
expect(result).toContain(
|
|
249
|
+
'<Link href="/about" kind="inline-text">about page</Link>'
|
|
250
|
+
)
|
|
251
|
+
expect(result).toContain(
|
|
252
|
+
'<Link href="/help" kind="inline-text">help section</Link>'
|
|
253
|
+
)
|
|
254
|
+
expect(result).not.toContain("Link.Inline")
|
|
255
|
+
})
|
|
256
|
+
})
|
|
257
|
+
|
|
258
|
+
describe("no changes scenarios", () => {
|
|
259
|
+
it("should return null when no Link.Inline components exist", () => {
|
|
260
|
+
const input = `
|
|
261
|
+
import { Link } from "@planningcenter/tapestry-react"
|
|
262
|
+
|
|
263
|
+
export default function Test() {
|
|
264
|
+
return <Link href="/home">Home</Link>
|
|
265
|
+
}
|
|
266
|
+
`.trim()
|
|
267
|
+
|
|
268
|
+
const result = applyTransform(input)
|
|
269
|
+
expect(result).toBe(null)
|
|
270
|
+
})
|
|
271
|
+
|
|
272
|
+
it("should return null when no tapestry-react imports exist", () => {
|
|
273
|
+
const input = `
|
|
274
|
+
import React from "react"
|
|
275
|
+
|
|
276
|
+
export default function Test() {
|
|
277
|
+
return <div>No Links here</div>
|
|
278
|
+
}
|
|
279
|
+
`.trim()
|
|
280
|
+
|
|
281
|
+
const result = applyTransform(input)
|
|
282
|
+
expect(result).toBe(null)
|
|
283
|
+
})
|
|
284
|
+
|
|
285
|
+
it("should return null for empty file", () => {
|
|
286
|
+
const result = applyTransform("")
|
|
287
|
+
expect(result).toBe(null)
|
|
288
|
+
})
|
|
289
|
+
|
|
290
|
+
it("should return null when Link is imported but no Link.Inline used", () => {
|
|
291
|
+
const input = `
|
|
292
|
+
import { Link, Button } from "@planningcenter/tapestry-react"
|
|
293
|
+
|
|
294
|
+
export default function Test() {
|
|
295
|
+
return (
|
|
296
|
+
<div>
|
|
297
|
+
<Link href="/home">Home</Link>
|
|
298
|
+
<Button>Click me</Button>
|
|
299
|
+
</div>
|
|
300
|
+
)
|
|
301
|
+
}
|
|
302
|
+
`.trim()
|
|
303
|
+
|
|
304
|
+
const result = applyTransform(input)
|
|
305
|
+
expect(result).toBe(null)
|
|
306
|
+
})
|
|
307
|
+
})
|
|
308
|
+
})
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { Transform } from "jscodeshift"
|
|
2
|
+
|
|
3
|
+
import { addAttribute } from "../../shared/actions/addAttribute"
|
|
4
|
+
import { attributeTransformFactory } from "../../shared/transformFactories/attributeTransformFactory"
|
|
5
|
+
import { componentTransformFactory } from "../../shared/transformFactories/componentTransformFactory"
|
|
6
|
+
|
|
7
|
+
// Step 1: Add kind="inline-text" attribute to Link.Inline elements
|
|
8
|
+
const addKindAttribute = attributeTransformFactory({
|
|
9
|
+
condition: () => true, // Add to all Link.Inline elements
|
|
10
|
+
targetComponent: "Link.Inline",
|
|
11
|
+
targetPackage: "@planningcenter/tapestry-react",
|
|
12
|
+
transform: (element, { j }) => {
|
|
13
|
+
addAttribute({
|
|
14
|
+
element,
|
|
15
|
+
j,
|
|
16
|
+
name: "kind",
|
|
17
|
+
value: "inline-text",
|
|
18
|
+
})
|
|
19
|
+
return true
|
|
20
|
+
},
|
|
21
|
+
})
|
|
22
|
+
|
|
23
|
+
// Step 2: Transform Link.Inline to Link (preserving original component name)
|
|
24
|
+
const transformComponent = componentTransformFactory({
|
|
25
|
+
condition: () => true, // Transform all Link.Inline elements
|
|
26
|
+
fromComponent: "Link.Inline",
|
|
27
|
+
fromPackage: "@planningcenter/tapestry-react",
|
|
28
|
+
toComponent: "Link",
|
|
29
|
+
toPackage: "@planningcenter/tapestry-react",
|
|
30
|
+
})
|
|
31
|
+
|
|
32
|
+
// Combined transform that runs both steps
|
|
33
|
+
const transform: Transform = (fileInfo, api, options) => {
|
|
34
|
+
// Step 1: Add kind attribute to Link.Inline elements
|
|
35
|
+
const result1 = addKindAttribute(fileInfo, api, options)
|
|
36
|
+
|
|
37
|
+
if (!result1) {
|
|
38
|
+
return null
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
// Step 2: Transform Link.Inline to Link
|
|
42
|
+
const result2 = transformComponent(
|
|
43
|
+
{ ...fileInfo, source: result1 as string },
|
|
44
|
+
api,
|
|
45
|
+
options
|
|
46
|
+
)
|
|
47
|
+
|
|
48
|
+
return result2 || result1
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export default transform
|
|
@@ -0,0 +1,245 @@
|
|
|
1
|
+
import jscodeshift from "jscodeshift"
|
|
2
|
+
import { describe, expect, it } from "vitest"
|
|
3
|
+
|
|
4
|
+
import transform from "./toToHref"
|
|
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("toToHref transform", () => {
|
|
18
|
+
describe("basic transformations", () => {
|
|
19
|
+
it("should transform Link 'to' prop to 'href' prop", () => {
|
|
20
|
+
const input = `
|
|
21
|
+
import { Link } from "@planningcenter/tapestry-react"
|
|
22
|
+
|
|
23
|
+
export default function Test() {
|
|
24
|
+
return <Link to="/dashboard">Dashboard</Link>
|
|
25
|
+
}
|
|
26
|
+
`.trim()
|
|
27
|
+
|
|
28
|
+
const expected = `
|
|
29
|
+
import { Link } from "@planningcenter/tapestry-react"
|
|
30
|
+
|
|
31
|
+
export default function Test() {
|
|
32
|
+
return <Link href="/dashboard">Dashboard</Link>;
|
|
33
|
+
}
|
|
34
|
+
`.trim()
|
|
35
|
+
|
|
36
|
+
const result = applyTransform(input)
|
|
37
|
+
expect(result?.trim()).toBe(expected)
|
|
38
|
+
})
|
|
39
|
+
|
|
40
|
+
it("should handle multiple Link components with 'to' props", () => {
|
|
41
|
+
const input = `
|
|
42
|
+
import { Link } from "@planningcenter/tapestry-react"
|
|
43
|
+
|
|
44
|
+
export default function Test() {
|
|
45
|
+
return (
|
|
46
|
+
<div>
|
|
47
|
+
<Link to="/home">Home</Link>
|
|
48
|
+
<Link to="/about">About</Link>
|
|
49
|
+
</div>
|
|
50
|
+
)
|
|
51
|
+
}
|
|
52
|
+
`.trim()
|
|
53
|
+
|
|
54
|
+
const result = applyTransform(input)
|
|
55
|
+
expect(result).toContain('href="/home"')
|
|
56
|
+
expect(result).toContain('href="/about"')
|
|
57
|
+
expect(result).not.toContain('to="/home"')
|
|
58
|
+
expect(result).not.toContain('to="/about"')
|
|
59
|
+
})
|
|
60
|
+
|
|
61
|
+
it("should preserve other props while transforming 'to' to 'href'", () => {
|
|
62
|
+
const input = `
|
|
63
|
+
import { Link } from "@planningcenter/tapestry-react"
|
|
64
|
+
|
|
65
|
+
export default function Test() {
|
|
66
|
+
return (
|
|
67
|
+
<Link
|
|
68
|
+
to="/dashboard"
|
|
69
|
+
className="nav-link"
|
|
70
|
+
target="_blank"
|
|
71
|
+
rel="noopener"
|
|
72
|
+
>
|
|
73
|
+
Dashboard
|
|
74
|
+
</Link>
|
|
75
|
+
)
|
|
76
|
+
}
|
|
77
|
+
`.trim()
|
|
78
|
+
|
|
79
|
+
const result = applyTransform(input)
|
|
80
|
+
expect(result).toContain('href="/dashboard"')
|
|
81
|
+
expect(result).toContain('className="nav-link"')
|
|
82
|
+
expect(result).toContain('target="_blank"')
|
|
83
|
+
expect(result).toContain('rel="noopener"')
|
|
84
|
+
expect(result).not.toContain('to="/dashboard"')
|
|
85
|
+
})
|
|
86
|
+
|
|
87
|
+
it("should handle dynamic 'to' prop values", () => {
|
|
88
|
+
const input = `
|
|
89
|
+
import { Link } from "@planningcenter/tapestry-react"
|
|
90
|
+
|
|
91
|
+
export default function Test({ userId }) {
|
|
92
|
+
return <Link to={\`/user/\${userId}\`}>User Profile</Link>
|
|
93
|
+
}
|
|
94
|
+
`.trim()
|
|
95
|
+
|
|
96
|
+
const result = applyTransform(input)
|
|
97
|
+
expect(result).toContain("href={`/user/${userId}`}")
|
|
98
|
+
expect(result).not.toContain("to={`/user/${userId}`}")
|
|
99
|
+
})
|
|
100
|
+
})
|
|
101
|
+
|
|
102
|
+
describe("edge cases", () => {
|
|
103
|
+
it("should not transform Links without 'to' prop", () => {
|
|
104
|
+
const input = `
|
|
105
|
+
import { Link } from "@planningcenter/tapestry-react"
|
|
106
|
+
|
|
107
|
+
export default function Test() {
|
|
108
|
+
return <Link href="/already-href">Already has href</Link>
|
|
109
|
+
}
|
|
110
|
+
`.trim()
|
|
111
|
+
|
|
112
|
+
const result = applyTransform(input)
|
|
113
|
+
expect(result).toBe(null) // No changes needed
|
|
114
|
+
})
|
|
115
|
+
|
|
116
|
+
it("should not transform if Link is not imported from tapestry-react", () => {
|
|
117
|
+
const input = `
|
|
118
|
+
import { Link } from "react-router-dom"
|
|
119
|
+
|
|
120
|
+
export default function Test() {
|
|
121
|
+
return <Link to="/dashboard">Dashboard</Link>
|
|
122
|
+
}
|
|
123
|
+
`.trim()
|
|
124
|
+
|
|
125
|
+
const result = applyTransform(input)
|
|
126
|
+
expect(result).toBe(null) // No changes needed
|
|
127
|
+
})
|
|
128
|
+
|
|
129
|
+
it("should handle aliased Link import", () => {
|
|
130
|
+
const input = `
|
|
131
|
+
import { Link as TapestryLink } from "@planningcenter/tapestry-react"
|
|
132
|
+
|
|
133
|
+
export default function Test() {
|
|
134
|
+
return <TapestryLink to="/dashboard">Dashboard</TapestryLink>
|
|
135
|
+
}
|
|
136
|
+
`.trim()
|
|
137
|
+
|
|
138
|
+
const result = applyTransform(input)
|
|
139
|
+
expect(result).toContain('href="/dashboard"')
|
|
140
|
+
expect(result).not.toContain('to="/dashboard"')
|
|
141
|
+
})
|
|
142
|
+
|
|
143
|
+
it("should handle self-closing Link tags", () => {
|
|
144
|
+
const input = `
|
|
145
|
+
import { Link } from "@planningcenter/tapestry-react"
|
|
146
|
+
|
|
147
|
+
export default function Test() {
|
|
148
|
+
return <Link to="/dashboard" />
|
|
149
|
+
}
|
|
150
|
+
`.trim()
|
|
151
|
+
|
|
152
|
+
const expected = `
|
|
153
|
+
import { Link } from "@planningcenter/tapestry-react"
|
|
154
|
+
|
|
155
|
+
export default function Test() {
|
|
156
|
+
return <Link href="/dashboard" />;
|
|
157
|
+
}
|
|
158
|
+
`.trim()
|
|
159
|
+
|
|
160
|
+
const result = applyTransform(input)
|
|
161
|
+
expect(result?.trim()).toBe(expected)
|
|
162
|
+
})
|
|
163
|
+
})
|
|
164
|
+
|
|
165
|
+
describe("mixed scenarios", () => {
|
|
166
|
+
it("should handle mixed Link elements with various props", () => {
|
|
167
|
+
const input = `
|
|
168
|
+
import { Link } from "@planningcenter/tapestry-react"
|
|
169
|
+
|
|
170
|
+
export default function Test() {
|
|
171
|
+
return (
|
|
172
|
+
<nav>
|
|
173
|
+
<Link to="/home" className="home-link">Home</Link>
|
|
174
|
+
<Link to="/profile" target="_blank">Profile</Link>
|
|
175
|
+
<Link href="/already-href">Already Good</Link>
|
|
176
|
+
</nav>
|
|
177
|
+
)
|
|
178
|
+
}
|
|
179
|
+
`.trim()
|
|
180
|
+
|
|
181
|
+
const result = applyTransform(input)
|
|
182
|
+
expect(result).toContain('href="/home"')
|
|
183
|
+
expect(result).toContain('href="/profile"')
|
|
184
|
+
expect(result).toContain('href="/already-href"')
|
|
185
|
+
expect(result).not.toContain('to="/home"')
|
|
186
|
+
expect(result).not.toContain('to="/profile"')
|
|
187
|
+
})
|
|
188
|
+
|
|
189
|
+
it("should handle complex JSX expressions", () => {
|
|
190
|
+
const input = `
|
|
191
|
+
import { Link } from "@planningcenter/tapestry-react"
|
|
192
|
+
|
|
193
|
+
export default function Test({ items }) {
|
|
194
|
+
return (
|
|
195
|
+
<div>
|
|
196
|
+
{items.map(item => (
|
|
197
|
+
<Link key={item.id} to={item.url}>{item.name}</Link>
|
|
198
|
+
))}
|
|
199
|
+
{showConditional && <Link to="/conditional">Conditional</Link>}
|
|
200
|
+
</div>
|
|
201
|
+
)
|
|
202
|
+
}
|
|
203
|
+
`.trim()
|
|
204
|
+
|
|
205
|
+
const result = applyTransform(input)
|
|
206
|
+
expect(result).toContain("href={item.url}")
|
|
207
|
+
expect(result).toContain('href="/conditional"')
|
|
208
|
+
expect(result).not.toContain("to={item.url}")
|
|
209
|
+
expect(result).not.toContain('to="/conditional"')
|
|
210
|
+
})
|
|
211
|
+
})
|
|
212
|
+
|
|
213
|
+
describe("no changes scenarios", () => {
|
|
214
|
+
it("should return null when no tapestry-react Links exist", () => {
|
|
215
|
+
const input = `
|
|
216
|
+
import React from "react"
|
|
217
|
+
|
|
218
|
+
export default function Test() {
|
|
219
|
+
return <div>No Links here</div>
|
|
220
|
+
}
|
|
221
|
+
`.trim()
|
|
222
|
+
|
|
223
|
+
const result = applyTransform(input)
|
|
224
|
+
expect(result).toBe(null)
|
|
225
|
+
})
|
|
226
|
+
|
|
227
|
+
it("should return null when no Links have 'to' prop", () => {
|
|
228
|
+
const input = `
|
|
229
|
+
import { Link } from "@planningcenter/tapestry-react"
|
|
230
|
+
|
|
231
|
+
export default function Test() {
|
|
232
|
+
return <Link href="/already-good">Already Good</Link>
|
|
233
|
+
}
|
|
234
|
+
`.trim()
|
|
235
|
+
|
|
236
|
+
const result = applyTransform(input)
|
|
237
|
+
expect(result).toBe(null)
|
|
238
|
+
})
|
|
239
|
+
|
|
240
|
+
it("should return null for empty file", () => {
|
|
241
|
+
const result = applyTransform("")
|
|
242
|
+
expect(result).toBe(null)
|
|
243
|
+
})
|
|
244
|
+
})
|
|
245
|
+
})
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { transformAttributeName } from "../../shared/actions/transformAttributeName"
|
|
2
|
+
import { hasAttribute } from "../../shared/conditions/hasAttribute"
|
|
3
|
+
import { attributeTransformFactory } from "../../shared/transformFactories/attributeTransformFactory"
|
|
4
|
+
|
|
5
|
+
const transform = attributeTransformFactory({
|
|
6
|
+
condition: hasAttribute("to"),
|
|
7
|
+
targetComponent: "Link",
|
|
8
|
+
targetPackage: "@planningcenter/tapestry-react",
|
|
9
|
+
transform: (element) => {
|
|
10
|
+
return transformAttributeName("to", "href", { element })
|
|
11
|
+
},
|
|
12
|
+
})
|
|
13
|
+
|
|
14
|
+
export default transform
|