|
| 1 | +--- |
| 2 | +name: check-similarity-ts |
| 3 | +description: Detect duplicate TypeScript/JavaScript code using AST-based similarity analysis. Use when working with .ts/.tsx/.js/.jsx files and looking for code duplication or refactoring opportunities. |
| 4 | +argument-hint: '[path] [--threshold 0.85] [--print]' |
| 5 | +allowed-tools: Bash(similarity-ts *) Read Grep Glob |
| 6 | +paths: '**/*.ts,**/*.tsx,**/*.js,**/*.jsx' |
| 7 | +--- |
| 8 | + |
| 9 | +# TypeScript/JavaScript Code Similarity Detection |
| 10 | + |
| 11 | +## What to do |
| 12 | + |
| 13 | +Run `similarity-ts` on the target project to detect duplicate functions and types, then analyze results and propose refactoring. |
| 14 | + |
| 15 | +similarity-ts is installed via nix. check out flake.nix for details. |
| 16 | + |
| 17 | +## Step 1: Run similarity analysis |
| 18 | + |
| 19 | +```bash |
| 20 | +similarity-ts $ARGUMENTS |
| 21 | +``` |
| 22 | + |
| 23 | +If no arguments given: |
| 24 | + |
| 25 | +```bash |
| 26 | +similarity-ts . --threshold 0.85 --min-tokens 25 |
| 27 | +``` |
| 28 | + |
| 29 | +For type-level duplicates (interfaces, type aliases): |
| 30 | + |
| 31 | +```bash |
| 32 | +similarity-ts . --threshold 0.85 --experimental-types |
| 33 | +``` |
| 34 | + |
| 35 | +## Step 2: Analyze results |
| 36 | + |
| 37 | +### High-priority |
| 38 | + |
| 39 | +- **100% similarity**: Extract shared function |
| 40 | +- **95-100%**: Parameterize the small difference |
| 41 | +- **Duplicate types/interfaces**: Consolidate into a single definition and re-export |
| 42 | + |
| 43 | +### Medium-priority |
| 44 | + |
| 45 | +- **85-95%**: Extract common pattern, especially for API handlers and data processing |
| 46 | +- **Similar type literals**: Shared interface with optional fields |
| 47 | + |
| 48 | +### Acceptable |
| 49 | + |
| 50 | +- **Short utility functions** (< 5 lines) that naturally share structure |
| 51 | +- **Overloaded variants** that differ by type parameter |
| 52 | + |
| 53 | +## Step 3: Propose refactoring |
| 54 | + |
| 55 | +For each high-priority pair, show before/after code with concrete implementation. |
| 56 | + |
| 57 | +## Key Options |
| 58 | + |
| 59 | +| Option | Description | |
| 60 | +| ------------------------------- | -------------------------------------------------------- | |
| 61 | +| `--threshold <0-1>` | Similarity threshold (default: 0.85) | |
| 62 | +| `--min-tokens <n>` | Skip functions with fewer AST nodes (recommended: 20-30) | |
| 63 | +| `--print` | Show actual code snippets | |
| 64 | +| `--experimental-types` | Enable type/interface similarity detection | |
| 65 | +| `--experimental-overlap` | Enable partial overlap detection | |
| 66 | +| `--extensions <ext>` | File extensions to check (comma-separated) | |
| 67 | +| `--filter-function <name>` | Filter by function name | |
| 68 | +| `--filter-function-body <text>` | Filter by function body content | |
| 69 | +| `--fail-on-duplicates` | Exit code 1 if duplicates found | |
| 70 | + |
| 71 | +## Common TS/JS refactoring patterns |
| 72 | + |
| 73 | +- **Data processing loops** with different field names -> generic mapper |
| 74 | +- **API handlers** with similar request/response logic -> shared middleware |
| 75 | +- **Validation functions** -> schema-based validation |
| 76 | +- **Duplicate interfaces** -> shared base interface with extensions |
| 77 | +- **Similar type literals** -> extract named type |
0 commit comments