forked from equinor/design-system
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtextfield-to-textarea.codemod.js
More file actions
163 lines (144 loc) · 4.71 KB
/
Copy pathtextfield-to-textarea.codemod.js
File metadata and controls
163 lines (144 loc) · 4.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/**
* Codemod to migrate TextField with multiline prop to Textarea component
*
* Usage:
* npx jscodeshift -t textfield-to-textarea.codemod.js --parser=tsx src/
*
* This codemod:
* 1. Finds TextField components with multiline={true} or multiline prop
* 2. Replaces TextField with Textarea
* 3. Removes the multiline prop
* 4. Renames textareaRef to ref
* 5. Updates imports
*/
module.exports = function transformer(file, api) {
const j = api.jscodeshift
const root = j(file.source)
let hasChanges = false
// Track if we need to update imports
let hasTextFieldSingleLine = false
let changeCount = 0
let textareaRefRenames = 0
// Find TextField components with multiline prop
const textFieldsWithMultiline = root.find(j.JSXElement).filter((path) => {
const openingElement = path.value.openingElement
const elementName = openingElement.name
// Check if it's a TextField
if (
elementName.type === 'JSXIdentifier' &&
elementName.name === 'TextField'
) {
// Check if it has multiline prop
const hasMultiline = openingElement.attributes.some((attr) => {
if (attr.type === 'JSXAttribute' && attr.name.name === 'multiline') {
// multiline={true} or just multiline
if (
attr.value === null ||
(attr.value.type === 'JSXExpressionContainer' &&
attr.value.expression.type === 'BooleanLiteral' &&
attr.value.expression.value === true) ||
(attr.value.type === 'JSXExpressionContainer' &&
attr.value.expression.type === 'Literal' &&
attr.value.expression.value === true)
) {
return true
}
}
return false
})
if (!hasMultiline) {
hasTextFieldSingleLine = true
}
return hasMultiline
}
return false
})
textFieldsWithMultiline.forEach((path) => {
const openingElement = path.value.openingElement
const closingElement = path.value.closingElement
// Change component name from TextField to Textarea
openingElement.name.name = 'Textarea'
if (closingElement) {
closingElement.name.name = 'Textarea'
}
// Remove multiline prop
openingElement.attributes = openingElement.attributes.filter((attr) => {
if (attr.type === 'JSXAttribute' && attr.name.name === 'multiline') {
return false
}
return true
})
// Rename textareaRef to ref
openingElement.attributes = openingElement.attributes.map((attr) => {
if (attr.type === 'JSXAttribute' && attr.name.name === 'textareaRef') {
attr.name.name = 'ref'
textareaRefRenames++
}
return attr
})
changeCount++
hasChanges = true
})
// Update imports
if (hasChanges) {
// Find the import declaration for TextField
root
.find(j.ImportDeclaration)
.filter((path) => {
const specifiers = path.value.specifiers || []
return specifiers.some(
(spec) =>
spec.type === 'ImportSpecifier' &&
spec.imported.name === 'TextField',
)
})
.forEach((path) => {
const specifiers = path.value.specifiers || []
// Check if Textarea is already imported
const hasTextarea = specifiers.some(
(spec) =>
spec.type === 'ImportSpecifier' &&
spec.imported.name === 'Textarea',
)
if (!hasTextarea) {
// Add Textarea to imports
path.value.specifiers.push(
j.importSpecifier(j.identifier('Textarea')),
)
}
// Remove TextField if no single-line usage
if (!hasTextFieldSingleLine) {
path.value.specifiers = specifiers.filter(
(spec) =>
!(
spec.type === 'ImportSpecifier' &&
spec.imported.name === 'TextField'
),
)
// If no specifiers left, remove the entire import
if (path.value.specifiers.length === 0) {
j(path).remove()
}
}
})
// Log the changes made
console.log(`✓ ${file.path}`)
console.log(
` - Converted ${changeCount} TextField${changeCount !== 1 ? 's' : ''} with multiline prop to Textarea`,
)
if (textareaRefRenames > 0) {
console.log(
` - Renamed ${textareaRefRenames} textareaRef prop${textareaRefRenames !== 1 ? 's' : ''} to ref`,
)
}
if (!hasTextFieldSingleLine) {
console.log(` - Removed TextField from imports (no single-line usage)`)
} else {
console.log(
` - Kept TextField import (still used for single-line fields)`,
)
}
}
return hasChanges ? root.toSource({ quote: 'single' }) : null
}
module.exports.parser = 'tsx'