Skip to content

Commit 61c494c

Browse files
committed
Use jscodeshift
1 parent bd76edf commit 61c494c

11 files changed

Lines changed: 256 additions & 496 deletions

File tree

README.md

Lines changed: 53 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ npx @suites/codemod <codemod> <source> [options]
1414
```
1515

1616
**Example:**
17+
1718
```bash
1819
npx @suites/codemod automock/2/to-suites-v3 src/**/*.spec.ts
1920
```
@@ -66,24 +67,25 @@ describe('UserService', () => {
6667

6768
### Arguments
6869

69-
| Argument | Description | Default |
70-
|----------|-------------|---------|
71-
| `codemod` | Codemod slug to run. See available transforms below. | - |
72-
| `source` | Path to source files or directory to transform including glob patterns. | `.` |
70+
| Argument | Description | Default |
71+
| --------- | ----------------------------------------------------------------------- | ------- |
72+
| `codemod` | Codemod slug to run. See available transforms below. | - |
73+
| `source` | Path to source files or directory to transform including glob patterns. | `.` |
7374

7475
### Options
7576

76-
| Option | Description | Default |
77-
|--------|-------------|---------|
78-
| `-v, --version` | Output the current version | - |
79-
| `-d, --dry` | Dry run (no changes are made to files) | `false` |
80-
| `-f, --force` | Bypass Git safety checks and forcibly run codemods | `false` |
81-
| `-p, --print` | Print transformed files to stdout, useful for development | `false` |
82-
| `--verbose` | Show more information about the transform process | `false` |
83-
| `--parser <parser>` | Parser to use: `tsx`, `ts`, `babel` | `tsx` |
84-
| `-h, --help` | Display help message | - |
77+
| Option | Description | Default |
78+
| ------------------- | --------------------------------------------------------- | ------- |
79+
| `-v, --version` | Output the current version | - |
80+
| `-d, --dry` | Dry run (no changes are made to files) | `false` |
81+
| `-f, --force` | Bypass Git safety checks and forcibly run codemods | `false` |
82+
| `-p, --print` | Print transformed files to stdout, useful for development | `false` |
83+
| `--verbose` | Show more information about the transform process | `false` |
84+
| `--parser <parser>` | Parser to use: `tsx`, `ts`, `babel` | `tsx` |
85+
| `-h, --help` | Display help message | - |
8586

8687
**Examples:**
88+
8789
```bash
8890
# Preview changes (dry run)
8991
npx @suites/codemod automock/2/to-suites-v3 src --dry
@@ -105,6 +107,7 @@ npx @suites/codemod automock/2/to-suites-v3 src --parser babel
105107
Intelligently migrates Automock v2 test files to Suites v3 framework.
106108

107109
**What it transforms:**
110+
108111
- Import statements: `@automock/jest` -> `@suites/unit`
109112
- TestBed API: `TestBed.create()` -> `TestBed.solitary().compile()`
110113
- Mock configuration: `.using()` -> `.impl()` or `.final()`
@@ -124,6 +127,7 @@ The codemod automatically chooses between `.impl()` and `.final()`:
124127
**Validation:**
125128

126129
Built-in validation ensures:
130+
127131
- No `@automock` imports remain
128132
- `TestBed.create()` is converted to `TestBed.solitary()`
129133
- `.compile()` is called with proper `await`
@@ -139,15 +143,19 @@ Built-in validation ensures:
139143
## Troubleshooting
140144

141145
**"Working directory is not clean"**
146+
142147
- Commit your changes or use `--force` to bypass
143148

144149
**"No files found"**
150+
145151
- Check your source path and ensure it contains `.ts` or `.tsx` files
146152

147153
**Parser errors**
154+
148155
- Try the babel parser: `--parser babel`
149156

150157
**Validation failed**
158+
151159
- Run with `--verbose` for detailed logs
152160
- Review validation errors in the output
153161
- Fix the issues reported by validators
@@ -157,6 +165,7 @@ For more help, see [troubleshooting guide](https://github.com/suites-dev/codemod
157165
## How It Works
158166

159167
The codemod uses [jscodeshift](https://github.com/facebook/jscodeshift) to:
168+
160169
1. Parse TypeScript/JavaScript into an Abstract Syntax Tree (AST)
161170
2. Apply intelligent transformations (imports, TestBed API, mocks, types)
162171
3. Validate the transformed code
@@ -169,12 +178,14 @@ The codemod uses [jscodeshift](https://github.com/facebook/jscodeshift) to:
169178
This codemod follows the **Codemod Registry** pattern used by React, Next.js, and other major frameworks:
170179

171180
**Transform Naming:** `<framework>/<version>/<transform>`
181+
172182
- `automock/2/to-suites-v3` - Current migration
173183
- `automock/3/to-suites-v4` - Future migrations
174184
- Supports multiple transforms per version
175185
- Extensible to other frameworks (e.g., `jest/28/to-v29`)
176186

177187
**Directory Structure:**
188+
178189
```
179190
src/transforms/
180191
automock/ # Framework namespace
@@ -185,6 +196,7 @@ src/transforms/
185196
```
186197

187198
**Design Benefits:**
199+
188200
- No default transform - explicit selection prevents mistakes
189201
- Version-based organization supports migration chains
190202
- Framework namespacing allows multi-framework support
@@ -204,7 +216,30 @@ Contributions welcome! To contribute:
204216
### Adding New Transforms
205217

206218
1. Create transform directory: `src/transforms/<framework>/<version>/<transform-name>.ts`
207-
2. Export `applyTransform` function from your transform
219+
2. **Export a default function** that follows jscodeshift's transform signature:
220+
221+
```typescript
222+
import type { FileInfo, API, Options } from 'jscodeshift';
223+
import { transform } from '../../../transform';
224+
225+
export default transform;
226+
```
227+
228+
Or implement your own transform function:
229+
230+
```typescript
231+
import type { FileInfo, API, Options } from 'jscodeshift';
232+
233+
export default function transform(
234+
fileInfo: FileInfo,
235+
api: API,
236+
options: Options
237+
): string | null | undefined {
238+
// Your transform logic here
239+
return transformedCode;
240+
}
241+
```
242+
208243
3. Register in `src/transforms/index.ts`:
209244
```typescript
210245
{
@@ -218,9 +253,12 @@ Contributions welcome! To contribute:
218253
6. Update this README
219254

220255
**Example:**
256+
221257
```typescript
222258
// src/transforms/automock/3/to-suites-v4.ts
223-
export { applyTransform } from '../../../transform';
259+
import { transform } from '../../../transform';
260+
261+
export default transform;
224262
```
225263

226264
### Project Structure

0 commit comments

Comments
 (0)