@wise/wds-codemods 0.0.1-experimental-82259a8 โ 0.0.1-experimental-0d8d466
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/.changeset/quick-mails-joke.md +128 -0
- package/DEVELOPER.md +783 -0
- package/README.md +157 -14
- package/package.json +1 -1
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
---
|
|
2
|
+
'@wise/wds-codemods': major
|
|
3
|
+
---
|
|
4
|
+
|
|
5
|
+
# ๐ @wise/wds-codemods@1.0.0 - Initial Release
|
|
6
|
+
|
|
7
|
+
Welcome to the first major release of WDS Codemods! This comprehensive toolkit provides automated migration support for Wise Design System components, making it easy to upgrade your codebase to the latest component versions.
|
|
8
|
+
|
|
9
|
+
## โจ Core Features
|
|
10
|
+
|
|
11
|
+
### ๐ง Transform Engine
|
|
12
|
+
|
|
13
|
+
- **Interactive CLI**: Choose transforms and configure options with an intuitive command-line interface
|
|
14
|
+
- **Dry Run Mode**: Preview changes before applying them to your codebase
|
|
15
|
+
- **Monorepo Support**: Built-in support for monorepo package structures with automatic package detection
|
|
16
|
+
- **Smart Package Detection**: Automatically detects package versions in both `package.json` dependencies and `node_modules`
|
|
17
|
+
- **Comprehensive Reporting**: Generates detailed reports for manual review items that require developer attention
|
|
18
|
+
|
|
19
|
+
### ๐ฏ Button Component Transform
|
|
20
|
+
|
|
21
|
+
- **ActionButton Migration**: Automatically converts deprecated `ActionButton` components to modern `Button` with `v2` prop
|
|
22
|
+
- **Legacy Prop Migration**: Transforms legacy props (`priority`, `size`, `type`, `htmlType`, `sentiment`) to new API
|
|
23
|
+
- **Enum Value Conversion**: Converts enum values (e.g., `Priority.PRIMARY`, `Size.LARGE`) to string literals
|
|
24
|
+
- **Icon Integration**: Intelligently processes icon children and converts them to `addonStart`/`addonEnd` props
|
|
25
|
+
- **Link Button Handling**: Properly migrates `as="a"` usage with automatic `href` management
|
|
26
|
+
- **Smart Size Mapping**: Maps legacy size values to new design tokens (`xs`, `sm`, `md`, `lg`, `xl`)
|
|
27
|
+
|
|
28
|
+
### ๐ก๏ธ Robust Validation & Safety
|
|
29
|
+
|
|
30
|
+
- **Package Version Validation**: Ensures transforms only run when target packages are present and meet version requirements
|
|
31
|
+
- **TypeScript & JSX Support**: Full support for TypeScript and JSX syntax parsing
|
|
32
|
+
- **Expression Analysis**: Handles complex expressions, conditionals, and dynamic values with appropriate fallbacks
|
|
33
|
+
- **Gitignore Integration**: Respects `.gitignore` patterns to avoid processing unnecessary files
|
|
34
|
+
- **Caching System**: Intelligent caching for package version checks to improve performance
|
|
35
|
+
|
|
36
|
+
### ๐ Advanced Reporting
|
|
37
|
+
|
|
38
|
+
- **Manual Review Detection**: Identifies code patterns that require human attention
|
|
39
|
+
- **Line-by-Line Reporting**: Precise location reporting for manual review items
|
|
40
|
+
- **Issue Classification**: Categorizes issues by type (deprecated props, unsupported values, ambiguous expressions)
|
|
41
|
+
- **Batch Processing**: Handles multiple files and provides comprehensive summary reports
|
|
42
|
+
|
|
43
|
+
## ๐ Usage Examples
|
|
44
|
+
|
|
45
|
+
### Basic Usage
|
|
46
|
+
|
|
47
|
+
```bash
|
|
48
|
+
npx @wise/wds-codemods
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
### Advanced Usage
|
|
52
|
+
|
|
53
|
+
```bash
|
|
54
|
+
# Run specific transform on a directory
|
|
55
|
+
npx @wise/wds-codemods button ./src --dry
|
|
56
|
+
|
|
57
|
+
# Target monorepo packages
|
|
58
|
+
npx @wise/wds-codemods button ./packages --monorepo
|
|
59
|
+
|
|
60
|
+
# Custom ignore patterns
|
|
61
|
+
npx @wise/wds-codemods button ./src --ignore-pattern "**/*.test.tsx,**/stories/**"
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
## ๐ Supported Transforms
|
|
65
|
+
|
|
66
|
+
### Button Transform (`button`)
|
|
67
|
+
|
|
68
|
+
Migrates Button and ActionButton components from `@transferwise/components` v46.5.0+
|
|
69
|
+
|
|
70
|
+
**Transformations include:**
|
|
71
|
+
|
|
72
|
+
- โ
`ActionButton` โ `Button` with `v2` prop
|
|
73
|
+
- โ
Legacy prop migrations (`priority`, `size`, `type`, etc.)
|
|
74
|
+
- โ
Enum value conversions
|
|
75
|
+
- โ
Icon children processing
|
|
76
|
+
- โ
Link button handling (`as="a"` โ proper href management)
|
|
77
|
+
- โ
Sentiment mapping (`type="negative"` โ `sentiment="negative"`)
|
|
78
|
+
|
|
79
|
+
## ๐๏ธ Architecture Highlights
|
|
80
|
+
|
|
81
|
+
### Modular Design
|
|
82
|
+
|
|
83
|
+
- **Transform System**: Extensible architecture for adding new component transforms
|
|
84
|
+
- **Helper Utilities**: Reusable utilities for JSX manipulation, reporting, and validation
|
|
85
|
+
- **Package Detection**: Multi-strategy package version detection (dependencies + node_modules)
|
|
86
|
+
- **Test Infrastructure**: Comprehensive testing utilities for transform development
|
|
87
|
+
|
|
88
|
+
### Developer Experience
|
|
89
|
+
|
|
90
|
+
- **Type Safety**: Full TypeScript support with proper type definitions
|
|
91
|
+
- **Error Handling**: Graceful error handling with helpful error messages
|
|
92
|
+
- **Debug Support**: Extensive debug logging for troubleshooting
|
|
93
|
+
- **Extensible**: Easy to add new transforms and extend existing functionality
|
|
94
|
+
|
|
95
|
+
## ๐งช Quality Assurance
|
|
96
|
+
|
|
97
|
+
This release includes:
|
|
98
|
+
|
|
99
|
+
- โ
**100% Test Coverage** for core utilities and transforms
|
|
100
|
+
- โ
**End-to-End Testing** with real-world code examples
|
|
101
|
+
- โ
**Edge Case Handling** for complex expressions and unusual patterns
|
|
102
|
+
- โ
**Performance Optimization** with intelligent caching and batching
|
|
103
|
+
- โ
**Memory Management** for large codebases
|
|
104
|
+
|
|
105
|
+
## ๐ฎ What's Next?
|
|
106
|
+
|
|
107
|
+
This foundation enables:
|
|
108
|
+
|
|
109
|
+
- ๐ฏ Additional component transforms (Forms, Navigation, etc.)
|
|
110
|
+
- ๐ Enhanced reporting and analytics
|
|
111
|
+
- ๐ง IDE integrations and tooling
|
|
112
|
+
- ๐ฑ Support for additional package managers
|
|
113
|
+
- ๐ Community contributions and extensibility
|
|
114
|
+
|
|
115
|
+
## ๐ Getting Started
|
|
116
|
+
|
|
117
|
+
1. Install the package: `npm install -D @wise/wds-codemods`
|
|
118
|
+
2. Run your first transform: `npx @wise/wds-codemods`
|
|
119
|
+
3. Review the generated `codemod-report.txt` for any manual review items
|
|
120
|
+
4. Commit your changes and enjoy your modernized codebase!
|
|
121
|
+
|
|
122
|
+
---
|
|
123
|
+
|
|
124
|
+
**Breaking Changes**: This is the initial release, so no breaking changes from previous versions.
|
|
125
|
+
|
|
126
|
+
**Migration Guide**: See our documentation for detailed migration examples and best practices.
|
|
127
|
+
|
|
128
|
+
Ready to modernize your Wise Design System components? Let's go! ๐
|
package/DEVELOPER.md
ADDED
|
@@ -0,0 +1,783 @@
|
|
|
1
|
+
# Developer Documentation
|
|
2
|
+
|
|
3
|
+
A comprehensive guide for developing codemods for the Wise Design System.
|
|
4
|
+
|
|
5
|
+
## Table of Contents
|
|
6
|
+
|
|
7
|
+
- [Getting Started](#getting-started)
|
|
8
|
+
- [Transform Structure](#transform-structure)
|
|
9
|
+
- [Helper Functions](#helper-functions)
|
|
10
|
+
- [Testing](#testing)
|
|
11
|
+
- [Package Requirements](#package-requirements)
|
|
12
|
+
- [Manual Review System](#manual-review-system)
|
|
13
|
+
- [Best Practices](#best-practices)
|
|
14
|
+
- [Examples](#examples)
|
|
15
|
+
|
|
16
|
+
## Getting Started
|
|
17
|
+
|
|
18
|
+
### Prerequisites
|
|
19
|
+
|
|
20
|
+
- Node.js (version specified in `.nvmrc`)
|
|
21
|
+
- pnpm 9.15.4
|
|
22
|
+
- Understanding of AST (Abstract Syntax Trees)
|
|
23
|
+
- Basic knowledge of jscodeshift
|
|
24
|
+
|
|
25
|
+
### Development Setup
|
|
26
|
+
|
|
27
|
+
```bash
|
|
28
|
+
# Install dependencies
|
|
29
|
+
pnpm install
|
|
30
|
+
|
|
31
|
+
# Build the project
|
|
32
|
+
pnpm run build
|
|
33
|
+
|
|
34
|
+
# Run tests
|
|
35
|
+
pnpm test
|
|
36
|
+
|
|
37
|
+
# Run tests in watch mode
|
|
38
|
+
pnpm test:watch
|
|
39
|
+
|
|
40
|
+
# Run with coverage
|
|
41
|
+
pnpm test:coverage
|
|
42
|
+
|
|
43
|
+
# Lint code
|
|
44
|
+
pnpm run lint
|
|
45
|
+
|
|
46
|
+
# Auto-fix linting issues
|
|
47
|
+
pnpm run lint:fix
|
|
48
|
+
|
|
49
|
+
# Type checking
|
|
50
|
+
pnpm run lint:types
|
|
51
|
+
|
|
52
|
+
# Create a changeset for releases
|
|
53
|
+
pnpm run changeset
|
|
54
|
+
|
|
55
|
+
# Publish releases
|
|
56
|
+
pnpm run release
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
## Transform Structure
|
|
60
|
+
|
|
61
|
+
Every transform follows this basic structure:
|
|
62
|
+
|
|
63
|
+
```typescript
|
|
64
|
+
// src/transforms/my-transform/my-transform.ts
|
|
65
|
+
import type { API, FileInfo, JSCodeshift, Options } from 'jscodeshift';
|
|
66
|
+
import { validatePackageRequirements, createReporter, hasImport } from '../helpers';
|
|
67
|
+
import reportManualReview from '../../utils/reportManualReview';
|
|
68
|
+
|
|
69
|
+
export const parser = 'tsx'; // Always use tsx for TypeScript + JSX support
|
|
70
|
+
export const packageRequirements = [{ name: '@transferwise/components', version: '>=46.5.0' }];
|
|
71
|
+
|
|
72
|
+
const transformer = (file: FileInfo, api: API, options: Options) => {
|
|
73
|
+
// 1. Validate package requirements
|
|
74
|
+
if (!validatePackageRequirements(options, packageRequirements)) {
|
|
75
|
+
return file.source;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
// 2. Set up jscodeshift and reporter
|
|
79
|
+
const j: JSCodeshift = api.jscodeshift;
|
|
80
|
+
const root = j(file.source);
|
|
81
|
+
const manualReviewIssues: string[] = [];
|
|
82
|
+
const reporter = createReporter(j, manualReviewIssues);
|
|
83
|
+
|
|
84
|
+
// 3. Check for relevant imports
|
|
85
|
+
const { exists: hasComponentImport } = hasImport(
|
|
86
|
+
root,
|
|
87
|
+
'@transferwise/components',
|
|
88
|
+
'MyComponent',
|
|
89
|
+
j,
|
|
90
|
+
);
|
|
91
|
+
|
|
92
|
+
if (!hasComponentImport) {
|
|
93
|
+
return file.source; // No relevant imports, skip file
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
// 4. Perform transformations
|
|
97
|
+
root.findJSXElements('MyComponent').forEach((path) => {
|
|
98
|
+
// Transform logic here
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
// 5. Report manual review issues
|
|
102
|
+
if (manualReviewIssues.length > 0) {
|
|
103
|
+
manualReviewIssues.forEach(async (issue) => {
|
|
104
|
+
await reportManualReview(file.path, issue);
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
return root.toSource();
|
|
109
|
+
};
|
|
110
|
+
|
|
111
|
+
export default transformer;
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
## Helper Functions
|
|
115
|
+
|
|
116
|
+
### Import Management
|
|
117
|
+
|
|
118
|
+
#### `hasImport(root, moduleName, importName, j)`
|
|
119
|
+
|
|
120
|
+
Checks if a specific import exists and provides utilities to remove it.
|
|
121
|
+
|
|
122
|
+
```typescript
|
|
123
|
+
import { hasImport } from '../helpers';
|
|
124
|
+
|
|
125
|
+
// Check for named import
|
|
126
|
+
const { exists: hasComponentImport, remove: removeComponentImport } = hasImport(
|
|
127
|
+
root,
|
|
128
|
+
'@transferwise/components',
|
|
129
|
+
'MyComponent',
|
|
130
|
+
j,
|
|
131
|
+
);
|
|
132
|
+
|
|
133
|
+
// Check for legacy component import with removal capability
|
|
134
|
+
const { exists: hasLegacyImport, remove: removeLegacyImport } = hasImport(
|
|
135
|
+
root,
|
|
136
|
+
'@transferwise/components',
|
|
137
|
+
'LegacyComponent',
|
|
138
|
+
j,
|
|
139
|
+
);
|
|
140
|
+
|
|
141
|
+
if (hasComponentImport) {
|
|
142
|
+
// Transform MyComponent elements
|
|
143
|
+
root.findJSXElements('MyComponent').forEach(transformComponent);
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
if (hasLegacyImport) {
|
|
147
|
+
// Transform LegacyComponent to MyComponent
|
|
148
|
+
root.findJSXElements('LegacyComponent').forEach(transformLegacyComponent);
|
|
149
|
+
|
|
150
|
+
// Remove the LegacyComponent import after transformation
|
|
151
|
+
removeLegacyImport();
|
|
152
|
+
}
|
|
153
|
+
```
|
|
154
|
+
|
|
155
|
+
**Returns:**
|
|
156
|
+
|
|
157
|
+
- `exists: boolean` - Whether the import exists
|
|
158
|
+
- `remove: () => void` - Function to remove the import
|
|
159
|
+
|
|
160
|
+
### JSX Element Utilities
|
|
161
|
+
|
|
162
|
+
#### `setNameIfJSXIdentifier(elementName, newName)`
|
|
163
|
+
|
|
164
|
+
Safely renames JSX elements, only if they are simple identifiers (not member expressions).
|
|
165
|
+
|
|
166
|
+
```typescript
|
|
167
|
+
import { setNameIfJSXIdentifier } from '../helpers';
|
|
168
|
+
|
|
169
|
+
root.findJSXElements('LegacyComponent').forEach((path) => {
|
|
170
|
+
const { openingElement, closingElement } = path.node;
|
|
171
|
+
|
|
172
|
+
// Rename opening tag from LegacyComponent to NewComponent
|
|
173
|
+
openingElement.name = setNameIfJSXIdentifier(openingElement.name, 'NewComponent')!;
|
|
174
|
+
|
|
175
|
+
// Rename closing tag if it exists
|
|
176
|
+
if (closingElement) {
|
|
177
|
+
closingElement.name = setNameIfJSXIdentifier(closingElement.name, 'NewComponent')!;
|
|
178
|
+
}
|
|
179
|
+
});
|
|
180
|
+
```
|
|
181
|
+
|
|
182
|
+
#### `hasAttributeOnElement(openingElement, attributeName)`
|
|
183
|
+
|
|
184
|
+
Checks if a JSX element has a specific attribute.
|
|
185
|
+
|
|
186
|
+
```typescript
|
|
187
|
+
import { hasAttributeOnElement } from '../helpers';
|
|
188
|
+
|
|
189
|
+
root.findJSXElements('MyComponent').forEach((path) => {
|
|
190
|
+
const { openingElement } = path.node;
|
|
191
|
+
|
|
192
|
+
// Skip if already has v2 prop
|
|
193
|
+
if (hasAttributeOnElement(openingElement, 'v2')) {
|
|
194
|
+
return;
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
// Add v2 prop
|
|
198
|
+
addAttributesIfMissing(j, openingElement, [
|
|
199
|
+
{ attribute: j.jsxAttribute(j.jsxIdentifier('v2')), name: 'v2' },
|
|
200
|
+
]);
|
|
201
|
+
});
|
|
202
|
+
```
|
|
203
|
+
|
|
204
|
+
#### `addAttributesIfMissing(j, openingElement, attributes)`
|
|
205
|
+
|
|
206
|
+
Adds attributes to a JSX element only if they don't already exist.
|
|
207
|
+
|
|
208
|
+
```typescript
|
|
209
|
+
import { addAttributesIfMissing } from '../helpers';
|
|
210
|
+
|
|
211
|
+
// Add multiple attributes if missing
|
|
212
|
+
addAttributesIfMissing(j, openingElement, [
|
|
213
|
+
{
|
|
214
|
+
attribute: j.jsxAttribute(j.jsxIdentifier('v2')),
|
|
215
|
+
name: 'v2',
|
|
216
|
+
},
|
|
217
|
+
{
|
|
218
|
+
attribute: j.jsxAttribute(j.jsxIdentifier('variant'), j.literal('primary')),
|
|
219
|
+
name: 'variant',
|
|
220
|
+
},
|
|
221
|
+
]);
|
|
222
|
+
```
|
|
223
|
+
|
|
224
|
+
**Parameters:**
|
|
225
|
+
|
|
226
|
+
- `j` - jscodeshift instance
|
|
227
|
+
- `openingElement` - JSX opening element
|
|
228
|
+
- `attributes` - Array of `{ attribute: JSXAttribute, name: string }`
|
|
229
|
+
|
|
230
|
+
### Icon Processing
|
|
231
|
+
|
|
232
|
+
#### `processIconChildren(j, children, iconImports, openingElement)`
|
|
233
|
+
|
|
234
|
+
Automatically processes icon children and converts them to appropriate addon props.
|
|
235
|
+
|
|
236
|
+
```typescript
|
|
237
|
+
import { processIconChildren } from '../helpers';
|
|
238
|
+
|
|
239
|
+
// First, collect icon imports
|
|
240
|
+
const iconImports = new Set<string>();
|
|
241
|
+
root.find(j.ImportDeclaration, { source: { value: '@transferwise/icons' } }).forEach((path) => {
|
|
242
|
+
path.node.specifiers?.forEach((specifier) => {
|
|
243
|
+
if (specifier.local) {
|
|
244
|
+
const localName = (specifier.local as { name: string }).name;
|
|
245
|
+
iconImports.add(localName);
|
|
246
|
+
}
|
|
247
|
+
});
|
|
248
|
+
});
|
|
249
|
+
|
|
250
|
+
// Then process icons in components
|
|
251
|
+
root.findJSXElements('MyComponent').forEach((path) => {
|
|
252
|
+
processIconChildren(j, path.node.children, iconImports, path.node.openingElement);
|
|
253
|
+
});
|
|
254
|
+
```
|
|
255
|
+
|
|
256
|
+
This function:
|
|
257
|
+
|
|
258
|
+
- Detects icon components in children
|
|
259
|
+
- Converts them to addon object props: `addonStart={{ type: 'icon', value: <IconComponent /> }}`
|
|
260
|
+
- Removes the icon children from the component
|
|
261
|
+
- Handles complex icon expressions that need manual review
|
|
262
|
+
|
|
263
|
+
**Example transformation:**
|
|
264
|
+
|
|
265
|
+
```typescript
|
|
266
|
+
// Before
|
|
267
|
+
<MyComponent>
|
|
268
|
+
<CheckIcon />
|
|
269
|
+
Content here
|
|
270
|
+
</MyComponent>
|
|
271
|
+
|
|
272
|
+
// After
|
|
273
|
+
<MyComponent addonStart={{ type: 'icon', value: <CheckIcon /> }}>
|
|
274
|
+
Content here
|
|
275
|
+
</MyComponent>
|
|
276
|
+
```
|
|
277
|
+
|
|
278
|
+
### Reporting System
|
|
279
|
+
|
|
280
|
+
#### `createReporter(j, issues)`
|
|
281
|
+
|
|
282
|
+
Creates a reporter instance for collecting manual review issues.
|
|
283
|
+
|
|
284
|
+
```typescript
|
|
285
|
+
import { createReporter } from '../helpers';
|
|
286
|
+
|
|
287
|
+
const manualReviewIssues: string[] = [];
|
|
288
|
+
const reporter = createReporter(j, manualReviewIssues);
|
|
289
|
+
|
|
290
|
+
// The reporter provides many methods for different scenarios
|
|
291
|
+
```
|
|
292
|
+
|
|
293
|
+
#### Reporter Methods
|
|
294
|
+
|
|
295
|
+
**Element-level reporting:**
|
|
296
|
+
|
|
297
|
+
```typescript
|
|
298
|
+
// Report general element issues
|
|
299
|
+
reporter.reportElement(componentElement, 'has complex configuration that needs review');
|
|
300
|
+
|
|
301
|
+
// Report spread props
|
|
302
|
+
reporter.reportSpreadProps(componentElement);
|
|
303
|
+
|
|
304
|
+
// Report attribute issues automatically
|
|
305
|
+
reporter.reportAttributeIssues(componentElement);
|
|
306
|
+
```
|
|
307
|
+
|
|
308
|
+
**Prop-specific reporting:**
|
|
309
|
+
|
|
310
|
+
```typescript
|
|
311
|
+
// Report specific prop issues
|
|
312
|
+
reporter.reportProp(element, 'variant', 'has unsupported value');
|
|
313
|
+
|
|
314
|
+
// Report attribute with custom reason
|
|
315
|
+
reporter.reportAttribute(attr, element, 'contains dynamic expression');
|
|
316
|
+
|
|
317
|
+
// Report unsupported values
|
|
318
|
+
reporter.reportUnsupportedValue(element, 'theme', 'custom-theme');
|
|
319
|
+
|
|
320
|
+
// Report ambiguous expressions
|
|
321
|
+
reporter.reportAmbiguousExpression(element, 'onClick');
|
|
322
|
+
|
|
323
|
+
// Report conflicting props and children
|
|
324
|
+
reporter.reportPropWithChildren(element, 'label');
|
|
325
|
+
|
|
326
|
+
// Report deprecated props
|
|
327
|
+
reporter.reportDeprecatedProp(element, 'legacy', 'use "variant" instead');
|
|
328
|
+
|
|
329
|
+
// Report missing required props
|
|
330
|
+
reporter.reportMissingRequiredProp(element, 'children');
|
|
331
|
+
|
|
332
|
+
// Report conflicting props
|
|
333
|
+
reporter.reportConflictingProps(element, ['variant', 'theme']);
|
|
334
|
+
```
|
|
335
|
+
|
|
336
|
+
**Children-specific reporting:**
|
|
337
|
+
|
|
338
|
+
```typescript
|
|
339
|
+
// Report ambiguous children (icons, conditional rendering, etc.)
|
|
340
|
+
reporter.reportAmbiguousChildren(element, 'icon');
|
|
341
|
+
```
|
|
342
|
+
|
|
343
|
+
### Package Validation
|
|
344
|
+
|
|
345
|
+
#### `validatePackageRequirements(options, packageRequirements)`
|
|
346
|
+
|
|
347
|
+
Validates that required packages are available before running the transform.
|
|
348
|
+
|
|
349
|
+
```typescript
|
|
350
|
+
import { validatePackageRequirements } from '../helpers';
|
|
351
|
+
|
|
352
|
+
export const packageRequirements = [
|
|
353
|
+
{ name: '@transferwise/components', version: '>=46.5.0' },
|
|
354
|
+
{ name: '@transferwise/icons', version: '>=1.0.0' },
|
|
355
|
+
];
|
|
356
|
+
|
|
357
|
+
const transformer = (file: FileInfo, api: API, options: Options) => {
|
|
358
|
+
// This will return false if packages aren't found/compatible
|
|
359
|
+
if (!validatePackageRequirements(options, packageRequirements)) {
|
|
360
|
+
return file.source; // Skip transformation
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
// Continue with transformation...
|
|
364
|
+
};
|
|
365
|
+
```
|
|
366
|
+
|
|
367
|
+
## Testing
|
|
368
|
+
|
|
369
|
+
### Basic Test Setup
|
|
370
|
+
|
|
371
|
+
```typescript
|
|
372
|
+
// src/transforms/my-transform/__tests__/my-transform.test.ts
|
|
373
|
+
import { createTestTransform } from '../../helpers/createTestTransform';
|
|
374
|
+
import transform from '../my-transform';
|
|
375
|
+
|
|
376
|
+
// Create test transform with package requirements
|
|
377
|
+
const testTransform = createTestTransform(transform, [
|
|
378
|
+
{ name: '@transferwise/components', version: '46.5.0' },
|
|
379
|
+
]);
|
|
380
|
+
|
|
381
|
+
describe('my-transform', () => {
|
|
382
|
+
it('should transform basic component', () => {
|
|
383
|
+
const input = `
|
|
384
|
+
import { OldComponent } from '@transferwise/components';
|
|
385
|
+
|
|
386
|
+
<OldComponent prop="value" />
|
|
387
|
+
`;
|
|
388
|
+
|
|
389
|
+
const expected = `
|
|
390
|
+
import { NewComponent } from '@transferwise/components';
|
|
391
|
+
|
|
392
|
+
<NewComponent newProp="value" />
|
|
393
|
+
`;
|
|
394
|
+
|
|
395
|
+
expect(testTransform({ source: input }).trim()).toBe(expected.trim());
|
|
396
|
+
});
|
|
397
|
+
});
|
|
398
|
+
```
|
|
399
|
+
|
|
400
|
+
### Testing with Different Package Scenarios
|
|
401
|
+
|
|
402
|
+
```typescript
|
|
403
|
+
import {
|
|
404
|
+
createTestTransform,
|
|
405
|
+
createTestTransformWithPackage,
|
|
406
|
+
createTestTransformWithoutPackage,
|
|
407
|
+
} from '../../helpers/createTestTransform';
|
|
408
|
+
|
|
409
|
+
describe('package requirement scenarios', () => {
|
|
410
|
+
it('should transform when package is available', () => {
|
|
411
|
+
const testTransform = createTestTransformWithPackage(
|
|
412
|
+
transform,
|
|
413
|
+
'@transferwise/components',
|
|
414
|
+
'46.5.0',
|
|
415
|
+
);
|
|
416
|
+
|
|
417
|
+
const result = testTransform({ source: input });
|
|
418
|
+
expect(result).toBe(expectedOutput);
|
|
419
|
+
});
|
|
420
|
+
|
|
421
|
+
it('should skip transformation when package is missing', () => {
|
|
422
|
+
const testTransform = createTestTransformWithoutPackage(
|
|
423
|
+
transform,
|
|
424
|
+
'@transferwise/components',
|
|
425
|
+
'46.5.0',
|
|
426
|
+
);
|
|
427
|
+
|
|
428
|
+
const result = testTransform({ source: input });
|
|
429
|
+
expect(result).toBe(input); // Should remain unchanged
|
|
430
|
+
});
|
|
431
|
+
});
|
|
432
|
+
```
|
|
433
|
+
|
|
434
|
+
### Testing Manual Review Cases
|
|
435
|
+
|
|
436
|
+
```typescript
|
|
437
|
+
describe('manual review cases', () => {
|
|
438
|
+
it('should handle spread props', () => {
|
|
439
|
+
const input = `<MyComponent {...props} />`;
|
|
440
|
+
const result = testTransform({ source: input });
|
|
441
|
+
|
|
442
|
+
// The transform should still run but flag for manual review
|
|
443
|
+
expect(result).toContain('v2');
|
|
444
|
+
// Manual review issues would be captured in a real scenario
|
|
445
|
+
});
|
|
446
|
+
|
|
447
|
+
it('should handle complex expressions', () => {
|
|
448
|
+
const input = `<MyComponent variant={computeVariant()} />`;
|
|
449
|
+
const result = testTransform({ source: input });
|
|
450
|
+
|
|
451
|
+
// Should preserve the expression but flag for review
|
|
452
|
+
expect(result).toContain('computeVariant()');
|
|
453
|
+
});
|
|
454
|
+
});
|
|
455
|
+
```
|
|
456
|
+
|
|
457
|
+
### Testing Icon Processing
|
|
458
|
+
|
|
459
|
+
```typescript
|
|
460
|
+
describe('icon processing', () => {
|
|
461
|
+
it('should convert leading icons to addon props', () => {
|
|
462
|
+
const input = `
|
|
463
|
+
import { MyComponent } from '@transferwise/components';
|
|
464
|
+
import { CheckIcon } from '@transferwise/icons';
|
|
465
|
+
|
|
466
|
+
<MyComponent>
|
|
467
|
+
<CheckIcon />
|
|
468
|
+
Content text
|
|
469
|
+
</MyComponent>
|
|
470
|
+
`;
|
|
471
|
+
|
|
472
|
+
const expected = `
|
|
473
|
+
import { MyComponent } from '@transferwise/components';
|
|
474
|
+
import { CheckIcon } from '@transferwise/icons';
|
|
475
|
+
|
|
476
|
+
<MyComponent v2 addonStart={{ type: 'icon', value: <CheckIcon /> }}>
|
|
477
|
+
Content text
|
|
478
|
+
</MyComponent>
|
|
479
|
+
`;
|
|
480
|
+
|
|
481
|
+
expect(testTransform({ source: input })).toBe(expected);
|
|
482
|
+
});
|
|
483
|
+
});
|
|
484
|
+
```
|
|
485
|
+
|
|
486
|
+
## Package Requirements
|
|
487
|
+
|
|
488
|
+
### Defining Requirements
|
|
489
|
+
|
|
490
|
+
```typescript
|
|
491
|
+
export const packageRequirements = [
|
|
492
|
+
{
|
|
493
|
+
name: '@transferwise/components',
|
|
494
|
+
version: '>=46.5.0', // Semantic version range
|
|
495
|
+
},
|
|
496
|
+
{
|
|
497
|
+
name: '@transferwise/icons',
|
|
498
|
+
version: '^1.0.0', // Caret range
|
|
499
|
+
},
|
|
500
|
+
{
|
|
501
|
+
name: '@transferwise/tokens',
|
|
502
|
+
version: '~1.5.0', // Tilde range
|
|
503
|
+
},
|
|
504
|
+
];
|
|
505
|
+
```
|
|
506
|
+
|
|
507
|
+
### How Package Validation Works
|
|
508
|
+
|
|
509
|
+
The system checks for packages in this order:
|
|
510
|
+
|
|
511
|
+
1. **Direct dependencies** - `package.json` files
|
|
512
|
+
2. **Lockfiles** - `package-lock.json`, `pnpm-lock.yaml`, `yarn.lock`
|
|
513
|
+
3. **Node modules** - Installed packages in `node_modules/`
|
|
514
|
+
|
|
515
|
+
For monorepos, it checks across all workspace packages and provides a summary.
|
|
516
|
+
|
|
517
|
+
## Manual Review System
|
|
518
|
+
|
|
519
|
+
### When to Use Manual Review
|
|
520
|
+
|
|
521
|
+
Use manual review reporting when:
|
|
522
|
+
|
|
523
|
+
- **Spread props** are present: `<MyComponent {...props} />`
|
|
524
|
+
- **Dynamic expressions** can't be safely transformed: `<MyComponent variant={calculateVariant()} />`
|
|
525
|
+
- **Complex children** need human review: `<MyComponent>{condition ? <Icon1 /> : <Icon2 />}</MyComponent>`
|
|
526
|
+
- **Unsupported prop values** are found: `<MyComponent theme="custom" />`
|
|
527
|
+
- **Conflicting props** are present: `<MyComponent label="Click" children="Also click" />`
|
|
528
|
+
|
|
529
|
+
### Best Practices for Reporting
|
|
530
|
+
|
|
531
|
+
```typescript
|
|
532
|
+
// โ
Good - Specific and actionable
|
|
533
|
+
reporter.reportUnsupportedValue(element, 'variant', 'custom');
|
|
534
|
+
// "prop "variant" on <MyComponent> at line 42 has unsupported value "custom""
|
|
535
|
+
|
|
536
|
+
// โ
Good - Explains the conflict
|
|
537
|
+
reporter.reportPropWithChildren(element, 'label');
|
|
538
|
+
// "prop "label" on <MyComponent> at line 42 conflicts with children"
|
|
539
|
+
|
|
540
|
+
// โ Avoid - Too vague
|
|
541
|
+
reporter.reportElement(element, 'needs review');
|
|
542
|
+
```
|
|
543
|
+
|
|
544
|
+
## Best Practices
|
|
545
|
+
|
|
546
|
+
### Transform Design
|
|
547
|
+
|
|
548
|
+
1. **Start small** - Begin with simple transformations and gradually add complexity
|
|
549
|
+
2. **Fail safely** - When in doubt, preserve existing code and report for manual review
|
|
550
|
+
3. **Be specific** - Provide clear, actionable manual review messages
|
|
551
|
+
4. **Test edge cases** - Include tests for complex scenarios and error conditions
|
|
552
|
+
|
|
553
|
+
### Code Organisation
|
|
554
|
+
|
|
555
|
+
```typescript
|
|
556
|
+
// Group related transformations
|
|
557
|
+
const transformComponent = (path: ASTPath<JSXElement>) => {
|
|
558
|
+
// Component-specific logic
|
|
559
|
+
};
|
|
560
|
+
|
|
561
|
+
const transformLegacyComponent = (path: ASTPath<JSXElement>) => {
|
|
562
|
+
// Legacy component-specific logic
|
|
563
|
+
};
|
|
564
|
+
|
|
565
|
+
// Main transformer delegates to specific functions
|
|
566
|
+
const transformer = (file: FileInfo, api: API, options: Options) => {
|
|
567
|
+
// Setup...
|
|
568
|
+
|
|
569
|
+
if (hasComponentImport) {
|
|
570
|
+
root.findJSXElements('MyComponent').forEach(transformComponent);
|
|
571
|
+
}
|
|
572
|
+
|
|
573
|
+
if (hasLegacyImport) {
|
|
574
|
+
root.findJSXElements('LegacyComponent').forEach(transformLegacyComponent);
|
|
575
|
+
removeLegacyImport();
|
|
576
|
+
}
|
|
577
|
+
|
|
578
|
+
// Cleanup...
|
|
579
|
+
};
|
|
580
|
+
```
|
|
581
|
+
|
|
582
|
+
### Error Handling
|
|
583
|
+
|
|
584
|
+
```typescript
|
|
585
|
+
// Wrap risky operations in try-catch
|
|
586
|
+
openingElement.attributes?.forEach((attr) => {
|
|
587
|
+
try {
|
|
588
|
+
if (attr.type === 'JSXAttribute' && attr.name.type === 'JSXIdentifier') {
|
|
589
|
+
// Safe to access attr.name.name
|
|
590
|
+
const propName = attr.name.name;
|
|
591
|
+
// Process attribute...
|
|
592
|
+
}
|
|
593
|
+
} catch (error) {
|
|
594
|
+
reporter.reportAttribute(attr, path, 'could not be processed automatically');
|
|
595
|
+
}
|
|
596
|
+
});
|
|
597
|
+
```
|
|
598
|
+
|
|
599
|
+
### Performance Considerations
|
|
600
|
+
|
|
601
|
+
1. **Early returns** - Skip files without relevant imports
|
|
602
|
+
2. **Efficient queries** - Use specific selectors instead of broad searches
|
|
603
|
+
3. **Minimise DOM traversal** - Cache frequently accessed nodes
|
|
604
|
+
|
|
605
|
+
```typescript
|
|
606
|
+
// โ
Good - Early return if no relevant imports
|
|
607
|
+
if (!hasComponentImport && !hasLegacyImport) {
|
|
608
|
+
return file.source;
|
|
609
|
+
}
|
|
610
|
+
|
|
611
|
+
// โ
Good - Specific query
|
|
612
|
+
root.findJSXElements('MyComponent').forEach(transformComponent);
|
|
613
|
+
|
|
614
|
+
// โ Avoid - Broad search
|
|
615
|
+
root.find(j.JSXElement).forEach((path) => {
|
|
616
|
+
if (
|
|
617
|
+
path.node.openingElement.name.type === 'JSXIdentifier' &&
|
|
618
|
+
path.node.openingElement.name.name === 'MyComponent'
|
|
619
|
+
) {
|
|
620
|
+
transformComponent(path);
|
|
621
|
+
}
|
|
622
|
+
});
|
|
623
|
+
```
|
|
624
|
+
|
|
625
|
+
## Examples
|
|
626
|
+
|
|
627
|
+
### Complete Transform Example (Simplified)
|
|
628
|
+
|
|
629
|
+
```typescript
|
|
630
|
+
// src/transforms/my-component-v2/my-component-v2.ts
|
|
631
|
+
import type { API, FileInfo, JSCodeshift, Options } from 'jscodeshift';
|
|
632
|
+
import reportManualReview from '../../utils/reportManualReview';
|
|
633
|
+
import {
|
|
634
|
+
addAttributesIfMissing,
|
|
635
|
+
createReporter,
|
|
636
|
+
hasAttributeOnElement,
|
|
637
|
+
hasImport,
|
|
638
|
+
validatePackageRequirements,
|
|
639
|
+
} from '../helpers';
|
|
640
|
+
|
|
641
|
+
export const parser = 'tsx';
|
|
642
|
+
export const packageRequirements = [{ name: '@transferwise/components', version: '>=46.5.0' }];
|
|
643
|
+
|
|
644
|
+
const transformer = (file: FileInfo, api: API, options: Options) => {
|
|
645
|
+
// 1. Validate package requirements
|
|
646
|
+
if (!validatePackageRequirements(options, packageRequirements)) {
|
|
647
|
+
return file.source;
|
|
648
|
+
}
|
|
649
|
+
|
|
650
|
+
// 2. Setup
|
|
651
|
+
const j: JSCodeshift = api.jscodeshift;
|
|
652
|
+
const root = j(file.source);
|
|
653
|
+
const manualReviewIssues: string[] = [];
|
|
654
|
+
const reporter = createReporter(j, manualReviewIssues);
|
|
655
|
+
|
|
656
|
+
// 3. Check for component import
|
|
657
|
+
const { exists: hasComponentImport } = hasImport(
|
|
658
|
+
root,
|
|
659
|
+
'@transferwise/components',
|
|
660
|
+
'MyComponent',
|
|
661
|
+
j,
|
|
662
|
+
);
|
|
663
|
+
|
|
664
|
+
if (!hasComponentImport) {
|
|
665
|
+
return file.source;
|
|
666
|
+
}
|
|
667
|
+
|
|
668
|
+
// 4. Transform components
|
|
669
|
+
root.findJSXElements('MyComponent').forEach((path) => {
|
|
670
|
+
const { openingElement } = path.node;
|
|
671
|
+
|
|
672
|
+
// Skip if already has v2 prop
|
|
673
|
+
if (hasAttributeOnElement(openingElement, 'v2')) {
|
|
674
|
+
return;
|
|
675
|
+
}
|
|
676
|
+
|
|
677
|
+
// Add v2 prop
|
|
678
|
+
addAttributesIfMissing(j, openingElement, [
|
|
679
|
+
{ attribute: j.jsxAttribute(j.jsxIdentifier('v2')), name: 'v2' },
|
|
680
|
+
]);
|
|
681
|
+
|
|
682
|
+
// Handle variant prop transformation
|
|
683
|
+
const variantAttr = openingElement.attributes?.find(
|
|
684
|
+
(attr) =>
|
|
685
|
+
attr.type === 'JSXAttribute' &&
|
|
686
|
+
attr.name.type === 'JSXIdentifier' &&
|
|
687
|
+
attr.name.name === 'variant',
|
|
688
|
+
);
|
|
689
|
+
|
|
690
|
+
if (
|
|
691
|
+
variantAttr &&
|
|
692
|
+
variantAttr.type === 'JSXAttribute' &&
|
|
693
|
+
variantAttr.value?.type === 'StringLiteral'
|
|
694
|
+
) {
|
|
695
|
+
const oldVariant = variantAttr.value.value;
|
|
696
|
+
const newVariant =
|
|
697
|
+
oldVariant === 'accent' ? 'primary' : oldVariant === 'subtle' ? 'secondary' : oldVariant;
|
|
698
|
+
variantAttr.value.value = newVariant;
|
|
699
|
+
}
|
|
700
|
+
|
|
701
|
+
// Report spread props for manual review
|
|
702
|
+
if (openingElement.attributes?.some((attr) => attr.type === 'JSXSpreadAttribute')) {
|
|
703
|
+
reporter.reportSpreadProps(path);
|
|
704
|
+
}
|
|
705
|
+
});
|
|
706
|
+
|
|
707
|
+
// 5. Report manual review issues
|
|
708
|
+
if (manualReviewIssues.length > 0) {
|
|
709
|
+
manualReviewIssues.forEach(async (issue) => {
|
|
710
|
+
await reportManualReview(file.path, issue);
|
|
711
|
+
});
|
|
712
|
+
}
|
|
713
|
+
|
|
714
|
+
return root.toSource();
|
|
715
|
+
};
|
|
716
|
+
|
|
717
|
+
export default transformer;
|
|
718
|
+
```
|
|
719
|
+
|
|
720
|
+
### Complete Test Example
|
|
721
|
+
|
|
722
|
+
```typescript
|
|
723
|
+
// src/transforms/my-component-v2/__tests__/my-component-v2.test.ts
|
|
724
|
+
import { createTestTransform } from '../../helpers/createTestTransform';
|
|
725
|
+
import transform from '../my-component-v2';
|
|
726
|
+
|
|
727
|
+
const testTransform = createTestTransform(transform, [
|
|
728
|
+
{ name: '@transferwise/components', version: '46.5.0' },
|
|
729
|
+
]);
|
|
730
|
+
|
|
731
|
+
describe('my-component-v2 transform', () => {
|
|
732
|
+
it('should add v2 prop to MyComponent instances', () => {
|
|
733
|
+
const input = `
|
|
734
|
+
import { MyComponent } from '@transferwise/components';
|
|
735
|
+
|
|
736
|
+
<MyComponent variant="accent">Content</MyComponent>
|
|
737
|
+
`;
|
|
738
|
+
|
|
739
|
+
const expected = `
|
|
740
|
+
import { MyComponent } from '@transferwise/components';
|
|
741
|
+
|
|
742
|
+
<MyComponent v2 variant="primary">Content</MyComponent>
|
|
743
|
+
`;
|
|
744
|
+
|
|
745
|
+
expect(testTransform({ source: input })).toBe(expected);
|
|
746
|
+
});
|
|
747
|
+
|
|
748
|
+
it('should skip components that already have v2 prop', () => {
|
|
749
|
+
const input = `
|
|
750
|
+
import { MyComponent } from '@transferwise/components';
|
|
751
|
+
|
|
752
|
+
<MyComponent v2 variant="primary">Already upgraded</MyComponent>
|
|
753
|
+
`;
|
|
754
|
+
|
|
755
|
+
expect(testTransform({ source: input })).toBe(input);
|
|
756
|
+
});
|
|
757
|
+
|
|
758
|
+
it('should skip files without component imports', () => {
|
|
759
|
+
const input = `
|
|
760
|
+
import { OtherComponent } from '@transferwise/components';
|
|
761
|
+
|
|
762
|
+
<OtherComponent>Content</OtherComponent>
|
|
763
|
+
`;
|
|
764
|
+
|
|
765
|
+
expect(testTransform({ source: input })).toBe(input);
|
|
766
|
+
});
|
|
767
|
+
|
|
768
|
+
it('should handle spread props', () => {
|
|
769
|
+
const input = `
|
|
770
|
+
import { MyComponent } from '@transferwise/components';
|
|
771
|
+
|
|
772
|
+
<MyComponent {...props}>Content</MyComponent>
|
|
773
|
+
`;
|
|
774
|
+
|
|
775
|
+
const result = testTransform({ source: input });
|
|
776
|
+
|
|
777
|
+
// Should still add v2 prop but flag for manual review
|
|
778
|
+
expect(result).toContain('v2');
|
|
779
|
+
});
|
|
780
|
+
});
|
|
781
|
+
```
|
|
782
|
+
|
|
783
|
+
This comprehensive guide should help developers understand how to create effective, robust, and well-tested codemods for the Wise Design System.
|
package/README.md
CHANGED
|
@@ -12,15 +12,19 @@
|
|
|
12
12
|
- [The Repository](#-the-repository)
|
|
13
13
|
- [Getting started](#-getting-started)
|
|
14
14
|
- [Commands](#commands)
|
|
15
|
+
- [Key Features](#-key-features)
|
|
16
|
+
- [Available Transforms](#-available-transforms)
|
|
15
17
|
- [Working with the Project Locally](#-working-with-the-project-locally)
|
|
16
18
|
- [Writing Codemod Transforms](#-writing-codemod-transforms)
|
|
19
|
+
- [Developer Documentation](#-developer-documentation)
|
|
17
20
|
- [Notes on Key Tools](#-notes-on-key-tools)
|
|
18
21
|
- [Feedback](#-feedback)
|
|
19
22
|
|
|
20
23
|
## ๐จโ๐ป The Repository
|
|
21
24
|
|
|
22
25
|
The project provides a flexible CLI interface that allows you to run codemods either interactively
|
|
23
|
-
via prompts or directly through command-line arguments.
|
|
26
|
+
via prompts or directly through command-line arguments. It includes intelligent package validation,
|
|
27
|
+
monorepo support, and comprehensive reporting for manual review cases.
|
|
24
28
|
|
|
25
29
|
## ๐ Getting started
|
|
26
30
|
|
|
@@ -30,7 +34,27 @@ arguments. Here's how to do both:
|
|
|
30
34
|
### To get started, install the package
|
|
31
35
|
|
|
32
36
|
```bash
|
|
37
|
+
# Using npm
|
|
33
38
|
npm i -g @wise/wds-codemods
|
|
39
|
+
|
|
40
|
+
# Using pnpm
|
|
41
|
+
pnpm add -g @wise/wds-codemods
|
|
42
|
+
|
|
43
|
+
# Using yarn
|
|
44
|
+
yarn global add @wise/wds-codemods
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
Or, if you prefer, you can run it directly without installing globally:
|
|
48
|
+
|
|
49
|
+
```bash
|
|
50
|
+
# Using npx
|
|
51
|
+
npx @wise/wds-codemods
|
|
52
|
+
|
|
53
|
+
# Using pnpm
|
|
54
|
+
pnpm dlx @wise/wds-codemods
|
|
55
|
+
|
|
56
|
+
# Using yarn
|
|
57
|
+
yarn dlx @wise/wds-codemods
|
|
34
58
|
```
|
|
35
59
|
|
|
36
60
|
### Using Interactive Prompts
|
|
@@ -53,36 +77,83 @@ You will be prompted to:
|
|
|
53
77
|
- Enter the target directory or file path to apply the codemod.
|
|
54
78
|
- Choose whether to run in dry mode (no files are modified).
|
|
55
79
|
- Choose whether to print the transformed source code to the console.
|
|
80
|
+
- Configure monorepo detection (automatically detected in most cases).
|
|
56
81
|
|
|
57
82
|
### Using CLI Arguments
|
|
58
83
|
|
|
59
84
|
You can also run codemods directly by providing arguments:
|
|
60
85
|
|
|
61
86
|
```bash
|
|
62
|
-
wds-codemods <
|
|
87
|
+
wds-codemods <transform> <targetPath> [--dry] [--print] [--monorepo]
|
|
63
88
|
```
|
|
64
89
|
|
|
65
|
-
Or using
|
|
90
|
+
Or using package runners:
|
|
66
91
|
|
|
67
92
|
```bash
|
|
68
|
-
npx wds-codemods <
|
|
93
|
+
npx @wise/wds-codemods <transform> <targetPath> [--dry] [--print] [--monorepo]
|
|
69
94
|
```
|
|
70
95
|
|
|
71
96
|
## Commands
|
|
72
97
|
|
|
73
98
|
- `npx wds-codemods <transform> <targetPath>`: Run a specific codemod transform on the target path.
|
|
74
|
-
- `--dry` or `--dry-run`: Run in dry mode without writing changes to files.
|
|
99
|
+
- `--dry` or `--dry-run`: Run in dry mode without writing changes to files. This is useful for previewing what changes would be made before actually applying them, allowing you to review the transformations safely.
|
|
75
100
|
- `--print`: Print transformed source to the console.
|
|
76
|
-
- `--ignore-pattern=GLOB`: Ignore files matching the provided glob pattern(s). Multiple patterns can be comma separated.
|
|
101
|
+
- `--ignore-pattern=GLOB`: Ignore files matching the provided [glob pattern(s)](https://code.visualstudio.com/docs/editor/glob-patterns). Multiple patterns can be comma separated.
|
|
77
102
|
- `--gitignore`: Respect `.gitignore` files to ignore files/folders during codemod runs.
|
|
78
103
|
- `--no-gitignore`: Do not respect `.gitignore` files.
|
|
104
|
+
- `--monorepo`: Enable monorepo package checking across multiple workspace folders.
|
|
79
105
|
|
|
80
|
-
|
|
106
|
+
Examples:
|
|
81
107
|
|
|
82
108
|
```bash
|
|
83
|
-
|
|
109
|
+
# Basic transform with dry run
|
|
110
|
+
wds-codemods button ./src --dry
|
|
111
|
+
|
|
112
|
+
# Transform with pattern exclusions
|
|
113
|
+
wds-codemods button ./src --ignore-pattern="*.test.ts,*.stories.ts"
|
|
114
|
+
|
|
115
|
+
# Ignore multiple directories and file types
|
|
116
|
+
wds-codemods button ./src --ignore-pattern="**/node_modules/**,**/*.test.ts,**/stories/**"
|
|
117
|
+
|
|
118
|
+
# Ignore specific directories
|
|
119
|
+
wds-codemods button ./src --ignore-pattern="dist/**,build/**,coverage/**"
|
|
120
|
+
|
|
121
|
+
# Monorepo transformation
|
|
122
|
+
wds-codemods button ./packages --monorepo
|
|
123
|
+
|
|
124
|
+
# Print output without writing files
|
|
125
|
+
wds-codemods button ./src --print --dry
|
|
84
126
|
```
|
|
85
127
|
|
|
128
|
+
## ๐ง Key Features
|
|
129
|
+
|
|
130
|
+
### Package Requirements Validation
|
|
131
|
+
|
|
132
|
+
- **Automatic Dependency Checking**: Validates required packages and versions before running transforms
|
|
133
|
+
- **Multi-Package Manager Support**: Works with npm, pnpm, and yarn
|
|
134
|
+
- **Smart Detection**: Checks package.json, lockfiles, and node_modules directories
|
|
135
|
+
- **Comprehensive Reporting**: Clear feedback when dependencies are missing or incompatible
|
|
136
|
+
|
|
137
|
+
### Monorepo Support
|
|
138
|
+
|
|
139
|
+
- **Auto-Detection**: Automatically identifies monorepo structures (packages/, apps/, libs/, etc.)
|
|
140
|
+
- **Cross-Package Validation**: Checks dependencies across all workspace packages
|
|
141
|
+
- **Summary Reports**: Provides detailed breakdown of which packages have required dependencies
|
|
142
|
+
- **Flexible Configuration**: Manual monorepo mode for custom structures
|
|
143
|
+
|
|
144
|
+
### Manual Review Reports
|
|
145
|
+
|
|
146
|
+
- **Automated Report Generation**: Creates `codemod-report.txt` for issues requiring manual attention
|
|
147
|
+
- **Detailed Context**: Includes file paths, line numbers, and specific issue descriptions
|
|
148
|
+
- **Smart Cleanup**: Automatically removes old reports and provides fresh summaries
|
|
149
|
+
- **Issue Categories**: Organised reporting for spread props, dynamic expressions, and unsupported values
|
|
150
|
+
|
|
151
|
+
### Intelligent Processing
|
|
152
|
+
|
|
153
|
+
- **Selective Execution**: Only runs transforms on projects with compatible dependencies
|
|
154
|
+
- **Performance Optimisation**: Caching and efficient directory traversal
|
|
155
|
+
- **Robust Error Handling**: Graceful handling of edge cases and invalid configurations
|
|
156
|
+
|
|
86
157
|
---
|
|
87
158
|
|
|
88
159
|
## ๐จโ๐ป Working with the Project Locally
|
|
@@ -126,9 +197,18 @@ standalone TypeScript file exporting a default function that follows the [jscode
|
|
|
126
197
|
### Example: Simple Rename Transform
|
|
127
198
|
|
|
128
199
|
```ts
|
|
129
|
-
import type { API, FileInfo } from 'jscodeshift';
|
|
200
|
+
import type { API, FileInfo, Options } from 'jscodeshift';
|
|
201
|
+
import { validatePackageRequirements } from '../helpers';
|
|
202
|
+
|
|
203
|
+
// Define package requirements for this transform
|
|
204
|
+
export const packageRequirements = [{ name: '@wise/components', version: '>=2.0.0' }];
|
|
205
|
+
|
|
206
|
+
const transformer = (file: FileInfo, api: API, options: Options) => {
|
|
207
|
+
// Validate package requirements before running
|
|
208
|
+
if (!validatePackageRequirements(options, packageRequirements)) {
|
|
209
|
+
return file.source;
|
|
210
|
+
}
|
|
130
211
|
|
|
131
|
-
const transformer = (file: FileInfo, api: API) => {
|
|
132
212
|
const j = api.jscodeshift;
|
|
133
213
|
const root = j(file.source);
|
|
134
214
|
|
|
@@ -144,15 +224,68 @@ export default transformer;
|
|
|
144
224
|
### Adding a New Transform
|
|
145
225
|
|
|
146
226
|
1. Create a new `.ts` file in `src/transforms/your-transform-name/`.
|
|
147
|
-
2. Export a
|
|
148
|
-
3.
|
|
149
|
-
4.
|
|
150
|
-
5.
|
|
227
|
+
2. Export a `packageRequirements` array defining required dependencies.
|
|
228
|
+
3. Export a default function following the jscodeshift transformer signature.
|
|
229
|
+
4. Use helper utilities from `src/transforms/helpers/` for common operations.
|
|
230
|
+
5. Write unit tests for your transform using the `createTestTransform` utility.
|
|
231
|
+
6. Build the project to compile your transform.
|
|
232
|
+
7. Run the codemod runner and select your new transform.
|
|
233
|
+
|
|
234
|
+
### Package Requirements
|
|
235
|
+
|
|
236
|
+
Each transform should export a `packageRequirements` array that specifies which packages and versions are required:
|
|
237
|
+
|
|
238
|
+
```ts
|
|
239
|
+
export const packageRequirements = [
|
|
240
|
+
{ name: '@wise/components', version: '>=2.0.0' },
|
|
241
|
+
{ name: '@wise/icons', version: '>=1.0.0' },
|
|
242
|
+
];
|
|
243
|
+
```
|
|
244
|
+
|
|
245
|
+
The system will automatically validate these requirements before running the transform, supporting:
|
|
246
|
+
|
|
247
|
+
- Semantic version ranges (`>=`, `^`, `~`, etc.)
|
|
248
|
+
- Multiple package managers (npm, pnpm, yarn)
|
|
249
|
+
- Monorepo structures
|
|
250
|
+
- Comprehensive dependency checking (dependencies, devDependencies, peerDependencies)
|
|
251
|
+
|
|
252
|
+
### Helper Utilities
|
|
253
|
+
|
|
254
|
+
The codemod provides several helper utilities in `src/transforms/helpers/`:
|
|
255
|
+
|
|
256
|
+
- **`hasImport`** - Check for and manipulate import statements
|
|
257
|
+
- **`processIconChildren`** - Handle icon component transformations
|
|
258
|
+
- **`createReporter`** - Generate manual review reports with detailed context
|
|
259
|
+
- **`validatePackageRequirements`** - Check package dependencies
|
|
260
|
+
- **JSX Element Utils** - Utilities for manipulating JSX elements and attributes
|
|
261
|
+
- **JSX Reporting Utils** - Standardised reporting for common manual review scenarios
|
|
262
|
+
|
|
263
|
+
Additional utilities are available in `src/utils/` for common operations like file handling, AST manipulation, and reporting. As this project evolves, we encourage contributors to add new utilities that can benefit the broader codemod ecosystem.
|
|
151
264
|
|
|
152
265
|
#### Writing Unit Tests for Transforms
|
|
153
266
|
|
|
154
267
|
It is important that all codemod transforms have corresponding unit tests to ensure correctness and prevent regressions. Use the `createTestTransform` utility to simplify writing tests for your transforms. This utility helps set up the testing environment and provides helpers to run your transform against sample input and verify the output.
|
|
155
268
|
|
|
269
|
+
**Basic test setup:**
|
|
270
|
+
|
|
271
|
+
```ts
|
|
272
|
+
import { createTestTransform } from '../../helpers/createTestTransform';
|
|
273
|
+
import transform from '../my-transform';
|
|
274
|
+
|
|
275
|
+
const testTransform = createTestTransform(transform, [
|
|
276
|
+
{ name: '@wise/components', version: '2.0.0' },
|
|
277
|
+
]);
|
|
278
|
+
|
|
279
|
+
describe('my-transform', () => {
|
|
280
|
+
it('should transform component', () => {
|
|
281
|
+
const input = `<OldComponent prop="value" />`;
|
|
282
|
+
const expected = `<NewComponent newProp="value" />`;
|
|
283
|
+
|
|
284
|
+
expect(testTransform({ source: input })).toBe(expected);
|
|
285
|
+
});
|
|
286
|
+
});
|
|
287
|
+
```
|
|
288
|
+
|
|
156
289
|
Example usage of `createTestTransform` can be found in the existing tests under `src/transforms/simple-rename/__tests__/simple-rename.test.ts`.
|
|
157
290
|
|
|
158
291
|
Make sure to run your tests regularly using:
|
|
@@ -163,6 +296,12 @@ pnpm test
|
|
|
163
296
|
|
|
164
297
|
---
|
|
165
298
|
|
|
299
|
+
## ๐ Developer Documentation
|
|
300
|
+
|
|
301
|
+
For comprehensive development details, including transform architecture, helper function documentation, testing patterns, and best practices, see our [Developer Documentation](./DEVELOPER.md).
|
|
302
|
+
|
|
303
|
+
---
|
|
304
|
+
|
|
166
305
|
## ๐ Notes on Key Tools
|
|
167
306
|
|
|
168
307
|
### jscodeshift
|
|
@@ -177,6 +316,10 @@ This project uses jscodeshift as the core engine to perform code transformations
|
|
|
177
316
|
|
|
178
317
|
This project uses @inquirer/prompts to provide a user-friendly interactive experience when running codemods without CLI arguments, allowing you to select transforms and options easily.
|
|
179
318
|
|
|
319
|
+
### semver
|
|
320
|
+
|
|
321
|
+
[semver](https://github.com/npm/node-semver) is used for semantic version parsing and comparison when validating package requirements. This ensures accurate dependency checking across different version specification formats.
|
|
322
|
+
|
|
180
323
|
---
|
|
181
324
|
|
|
182
325
|
## โ๏ธ Feedback
|