|
| 1 | +# CLAUDE.md |
| 2 | + |
| 3 | +This file provides guidance to Claude Code when working with the OpenRewrite TypeScript implementation. |
| 4 | + |
| 5 | +## Module Overview |
| 6 | + |
| 7 | +TypeScript implementation of OpenRewrite for JavaScript/TypeScript source code transformations, plus JSON and YAML support. Includes parsers, AST models, visitors, printers, and an RPC bridge for Java communication. |
| 8 | + |
| 9 | +Self-contained Node.js project, separate from the Java monorepo build system. |
| 10 | + |
| 11 | +## Project Setup |
| 12 | + |
| 13 | +From `rewrite-javascript/rewrite/`: |
| 14 | +```bash |
| 15 | +npm install |
| 16 | +``` |
| 17 | + |
| 18 | +Via Gradle (from repo root): |
| 19 | +```bash |
| 20 | +./gradlew :rewrite-javascript:npmInstall |
| 21 | +./gradlew :rewrite-javascript:npm_test |
| 22 | +./gradlew :rewrite-javascript:npm_run_build |
| 23 | +``` |
| 24 | + |
| 25 | +Requires Node.js 18+. |
| 26 | + |
| 27 | +## Running Tests |
| 28 | + |
| 29 | +```bash |
| 30 | +# Full suite (typecheck + build + test) |
| 31 | +npm test |
| 32 | + |
| 33 | +# Fast iteration (skip typecheck) |
| 34 | +npm run testhelper |
| 35 | + |
| 36 | +# Type-checking only |
| 37 | +npm run typecheck |
| 38 | + |
| 39 | +# Individual test file |
| 40 | +npm run testhelper -- test/javascript/recipes/order-imports.test.ts |
| 41 | +``` |
| 42 | + |
| 43 | +Available npm scripts: `prebuild`, `build`, `postbuild`, `typecheck`, `dev`, `test`, `testhelper`, `build:fixtures`, `ci:test`, `start`. |
| 44 | + |
| 45 | +## Directory Structure |
| 46 | + |
| 47 | +``` |
| 48 | +rewrite-javascript/rewrite/ |
| 49 | +├── src/ |
| 50 | +│ ├── index.ts # Main entry point / re-exports |
| 51 | +│ ├── tree.ts, visitor.ts, recipe.ts # Core framework |
| 52 | +│ ├── markers.ts, execution.ts # Metadata, execution context |
| 53 | +│ ├── print.ts, parser.ts # Base printer, base parser |
| 54 | +│ ├── util.ts, uuid.ts # Utilities |
| 55 | +│ ├── java/ # Java LST model |
| 56 | +│ │ ├── tree.ts # J namespace (Java AST) |
| 57 | +│ │ ├── visitor.ts # JavaVisitor |
| 58 | +│ │ ├── print.ts # Java-to-source printer |
| 59 | +│ │ ├── rpc.ts # RPC sender/receiver for Java |
| 60 | +│ │ └── type.ts, type-visitor.ts # Java type system |
| 61 | +│ ├── javascript/ # JavaScript/TypeScript |
| 62 | +│ │ ├── tree.ts # JS namespace (JavaScript AST) |
| 63 | +│ │ ├── visitor.ts # JavaScriptVisitor |
| 64 | +│ │ ├── print.ts # JS-to-source printer |
| 65 | +│ │ ├── parser.ts # JS/TS parser |
| 66 | +│ │ ├── rpc.ts # RPC sender/receiver for JS |
| 67 | +│ │ ├── assertions.ts # Test helpers: typescript(), javascript(), jsx(), tsx(), packageJson() |
| 68 | +│ │ ├── add-import.ts, remove-import.ts # Import manipulation |
| 69 | +│ │ ├── recipes/ # Built-in recipes (order-imports, change-import, add-dependency, etc.) |
| 70 | +│ │ ├── format/ # Formatting visitors |
| 71 | +│ │ ├── cleanup/ # Cleanup recipes (add-parse-int-radix, prefer-optional-chain, etc.) |
| 72 | +│ │ ├── migrate/ # Migration recipes (es6/, typescript/) |
| 73 | +│ │ ├── search/ # Search patterns |
| 74 | +│ │ └── templating/ # Template engine |
| 75 | +│ ├── json/ # JSON support (tree, visitor, print, rpc, recipes) |
| 76 | +│ ├── yaml/ # YAML support (tree, visitor, print, rpc, recipes) |
| 77 | +│ ├── search/ # Cross-language search utilities |
| 78 | +│ ├── text/ # Plain text support |
| 79 | +│ ├── rpc/ # RPC infrastructure |
| 80 | +│ │ ├── queue.ts # Message queue |
| 81 | +│ │ ├── rewrite-rpc.ts # Core RPC protocol |
| 82 | +│ │ ├── server.ts # RPC server |
| 83 | +│ │ ├── recipe.ts # Recipe RPC bridge |
| 84 | +│ │ ├── trace.ts # RPC tracing/debugging |
| 85 | +│ │ └── request/ # Request types (parse, visit, get-object, etc.) |
| 86 | +│ └── test/ # Testing infrastructure |
| 87 | +│ └── rewrite-test.ts # RecipeSpec class, rewriteRun() |
| 88 | +├── test/ # Jest tests (mirrors src/ structure) |
| 89 | +│ ├── javascript/ # JS/TS tests |
| 90 | +│ │ ├── recipes/ # Recipe tests |
| 91 | +│ │ ├── fixtures/ # Test npm projects |
| 92 | +│ │ ├── parser/, format/, cleanup/ # Category tests |
| 93 | +│ │ └── search/, templating/, migrate/ |
| 94 | +│ ├── java/ # Java model tests |
| 95 | +│ ├── json/, yaml/ # JSON/YAML tests |
| 96 | +│ └── rpc/ # RPC integration tests |
| 97 | +├── tsconfig.json |
| 98 | +├── jest.config.js |
| 99 | +└── package.json # name: @openrewrite/rewrite |
| 100 | +``` |
| 101 | + |
| 102 | +## Development Patterns |
| 103 | + |
| 104 | +### Async Visitor Pattern |
| 105 | + |
| 106 | +**All visitor methods are async.** This supports the RPC nature of the framework. |
| 107 | + |
| 108 | +```typescript |
| 109 | +export class MyVisitor extends JavaScriptVisitor<ExecutionContext> { |
| 110 | + protected async visitJsCompilationUnit( |
| 111 | + cu: JS.CompilationUnit, |
| 112 | + p: ExecutionContext |
| 113 | + ): Promise<JS.CompilationUnit> { |
| 114 | + // Transform and return |
| 115 | + return cu; |
| 116 | + } |
| 117 | +} |
| 118 | +``` |
| 119 | + |
| 120 | +Hierarchy: `TreeVisitor` → `JavaVisitor` → `JavaScriptVisitor` |
| 121 | + |
| 122 | +### Immutability and Structural Sharing |
| 123 | + |
| 124 | +LST nodes are immutable. Use `updateIfChanged()` for single-field updates: |
| 125 | + |
| 126 | +```typescript |
| 127 | +const updated = updateIfChanged(node, 'field', newValue); |
| 128 | +``` |
| 129 | + |
| 130 | +For multiple updates, use `produceAsync()` (Mutative-based draft mutations): |
| 131 | +```typescript |
| 132 | +import { produceAsync } from '../../visitor'; |
| 133 | + |
| 134 | +const updated = await produceAsync(cu, async draft => { |
| 135 | + draft.statements = newStatements; |
| 136 | +}); |
| 137 | +``` |
| 138 | + |
| 139 | +Note: The project uses [Mutative](https://github.com/unadlib/mutative) (not Immer) for immutable state management. |
| 140 | + |
| 141 | +### RPC Kind Constants |
| 142 | + |
| 143 | +LST nodes have a `kind` property that must exactly match Java FQN strings: |
| 144 | + |
| 145 | +```typescript |
| 146 | +// These must stay in sync with Java — any mismatch breaks RPC serialization |
| 147 | +CompilationUnit: "org.openrewrite.javascript.tree.JS$CompilationUnit" |
| 148 | +``` |
| 149 | + |
| 150 | +## Recipe Pattern |
| 151 | + |
| 152 | +```typescript |
| 153 | +import { Recipe, ExecutionContext, TreeVisitor } from '../../'; |
| 154 | +import { JavaScriptVisitor } from '../visitor'; |
| 155 | +import { JS } from '../tree'; |
| 156 | + |
| 157 | +export class MyRecipe extends Recipe { |
| 158 | + readonly name = 'org.openrewrite.javascript.MyRecipe'; |
| 159 | + readonly displayName = 'My Recipe'; |
| 160 | + readonly description = 'Describes what this recipe does'; |
| 161 | + |
| 162 | + async editor(): Promise<TreeVisitor<any, ExecutionContext>> { |
| 163 | + return new class extends JavaScriptVisitor<ExecutionContext> { |
| 164 | + protected async visitJsCompilationUnit(cu: JS.CompilationUnit, p: ExecutionContext) { |
| 165 | + return cu; |
| 166 | + } |
| 167 | + }; |
| 168 | + } |
| 169 | +} |
| 170 | +``` |
| 171 | + |
| 172 | +## Test Pattern |
| 173 | + |
| 174 | +Tests use relative imports. Source spec factories (`typescript()`, `javascript()`, `jsx()`, `tsx()`, `packageJson()`) are in `src/javascript/assertions.ts`. |
| 175 | + |
| 176 | +```typescript |
| 177 | +import { RecipeSpec } from "../../../src/test"; |
| 178 | +import { typescript } from "../../../src/javascript"; |
| 179 | +import { OrderImports } from "../../../src/javascript/recipes/order-imports"; |
| 180 | + |
| 181 | +describe('OrderImports', () => { |
| 182 | + test('sorts imports', () => |
| 183 | + new RecipeSpec({ recipe: new OrderImports() }).rewriteRun( |
| 184 | + typescript( |
| 185 | + `import {z} from 'zebra';\nimport {a} from 'alpha';`, |
| 186 | + `import {a} from 'alpha';\nimport {z} from 'zebra';` |
| 187 | + ) |
| 188 | + ) |
| 189 | + ); |
| 190 | +}); |
| 191 | +``` |
| 192 | + |
| 193 | +## RPC Sender/Receiver |
| 194 | + |
| 195 | +Each language module has `rpc.ts` with a Sender (visit tree → serialize to queue) and Receiver (read queue → reconstruct tree). These must stay aligned with each other AND with the Java equivalents. Any mismatch causes deadlocks or corrupted trees. |
| 196 | + |
| 197 | +## Debugging Tips |
| 198 | + |
| 199 | +### RPC Hangs |
| 200 | +1. Check that both Java and TypeScript RPC methods are implemented |
| 201 | +2. Verify Kind constants match between Java and TypeScript |
| 202 | +3. Run with `--detectOpenHandles` to find unclosed promises: `npm run testhelper -- --detectOpenHandles` |
| 203 | +4. Check `src/rpc/queue.ts` for deadlock in read/write operations |
| 204 | + |
| 205 | +### Type Checking |
| 206 | +Run `npm run typecheck` frequently to catch type mismatches early. |
0 commit comments