|
1 | 1 | --- |
2 | 2 | name: reduce-similarities |
3 | | -description: Detects duplicate JavaScript and TypeScript code with similarity-ts. Use when checking .js, .jsx, .ts, or .tsx files for refactoring opportunities. |
| 3 | +description: Detect duplicate Rust code using AST-based similarity analysis. Use when working with .rs files and looking for code duplication or refactoring opportunities. |
4 | 4 | argument-hint: '[path] [--threshold 0.85] [--print]' |
5 | | -allowed-tools: Bash(similarity-ts *) Read Grep Glob |
6 | | -paths: '**/*.ts,**/*.tsx,**/*.js,**/*.jsx' |
| 5 | +allowed-tools: Bash(similarity-rs *) Read Grep Glob |
| 6 | +paths: '**/*.rs' |
7 | 7 | --- |
8 | 8 |
|
9 | | -# TypeScript/JavaScript Code Similarity Detection |
| 9 | +# Rust Code Similarity Detection |
10 | 10 |
|
11 | 11 | ## What to do |
12 | 12 |
|
13 | | -Run `similarity-ts` on the target project to detect duplicate functions and types, then analyze results and propose refactoring. |
| 13 | +Run `similarity-rs` on the target Rust project to detect duplicate functions, struct/enum definitions, and impl methods, then analyze results and propose refactoring. |
14 | 14 |
|
15 | | -similarity-ts is installed via nix. check out flake.nix for details. |
| 15 | +If `similarity-rs` is not installed: |
| 16 | + |
| 17 | +```bash |
| 18 | +similarity-rs |
| 19 | + |
| 20 | +# or |
| 21 | + |
| 22 | +direnv exec . similarity-rs |
| 23 | +``` |
16 | 24 |
|
17 | 25 | ## Step 1: Run similarity analysis |
18 | 26 |
|
19 | 27 | ```bash |
20 | | -similarity-ts $ARGUMENTS |
| 28 | +similarity-rs $ARGUMENTS |
21 | 29 | ``` |
22 | 30 |
|
23 | 31 | If no arguments given: |
24 | 32 |
|
25 | 33 | ```bash |
26 | | -similarity-ts . --threshold 0.85 --min-tokens 25 |
| 34 | +similarity-rs . --threshold 0.85 --min-lines 5 |
27 | 35 | ``` |
28 | 36 |
|
29 | | -For type-level duplicates (interfaces, type aliases): |
| 37 | +For struct/enum similarity: |
30 | 38 |
|
31 | 39 | ```bash |
32 | | -similarity-ts . --threshold 0.85 --experimental-types |
| 40 | +similarity-rs . --threshold 0.85 --experimental-types |
33 | 41 | ``` |
34 | 42 |
|
35 | 43 | ## Step 2: Analyze results |
36 | 44 |
|
37 | 45 | ### High-priority |
38 | 46 |
|
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 |
| 47 | +- **100% similarity**: Exact duplicates -> extract shared function or use generics |
| 48 | +- **95-100%**: Same algorithm on different types -> generic function with trait bounds |
| 49 | +- **Duplicate impl methods**: Same logic across types -> trait with default implementation |
42 | 50 |
|
43 | 51 | ### Medium-priority |
44 | 52 |
|
45 | | -- **85-95%**: Extract common pattern, especially for API handlers and data processing |
46 | | -- **Similar type literals**: Shared interface with optional fields |
| 53 | +- **85-95%**: Similar match arms or error handling -> macro or shared helper |
| 54 | +- **Parallel struct definitions**: Identical fields -> shared base or generic struct |
47 | 55 |
|
48 | 56 | ### Acceptable |
49 | 57 |
|
50 | | -- **Short utility functions** (< 5 lines) that naturally share structure |
51 | | -- **Overloaded variants** that differ by type parameter |
| 58 | +- **Short `new()` constructors** with field initialization |
| 59 | +- **Simple `From`/`Into` implementations** |
| 60 | +- **Derive-equivalent implementations** |
52 | 61 |
|
53 | 62 | ## Step 3: Propose refactoring |
54 | 63 |
|
55 | | -For each high-priority pair, show before/after code with concrete implementation. |
| 64 | +For each high-priority pair, show before/after code with Rust idioms. |
56 | 65 |
|
57 | 66 | ## Key Options |
58 | 67 |
|
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 |
| 68 | +| Option | Description | |
| 69 | +| -------------------------- | ------------------------------------------------ | |
| 70 | +| `--threshold <0-1>` | Similarity threshold (default: 0.85) | |
| 71 | +| `--min-lines <n>` | Skip functions shorter than n lines (default: 3) | |
| 72 | +| `--min-tokens <n>` | Skip functions with fewer AST nodes | |
| 73 | +| `--print` | Show actual code snippets | |
| 74 | +| `--experimental-types` | Enable struct/enum similarity detection | |
| 75 | +| `--filter-function <name>` | Filter by function name | |
| 76 | +| `--fail-on-duplicates` | Exit code 1 if duplicates found | |
| 77 | + |
| 78 | +## Common Rust refactoring patterns |
| 79 | + |
| 80 | +- **Type-specific functions** -> generic `fn<T: Trait>` with trait bounds |
| 81 | +- **Duplicate impl blocks** across types -> trait with default methods |
| 82 | +- **Repeated match patterns** -> macro_rules! or helper function |
| 83 | +- **Parallel X/Y/Z functions** -> enum parameter or tuple-based abstraction |
| 84 | +- **Similar error handling** -> shared Result combinator or `?` chain |
0 commit comments