|
| 1 | +--- |
| 2 | +title: "Editor Overview" |
| 3 | +metaTitle: "Editor Overview" |
| 4 | +description: "Documentation about ReScript editor plugins and code analysis" |
| 5 | +canonical: "/docs/manual/v12.0.0/editor-plugins" |
| 6 | +--- |
| 7 | + |
| 8 | +# Guide: Dead Code Analysis in ReScript |
| 9 | + |
| 10 | +This guide provides a detailed walkthrough on how to leverage ReScript’s powerful dead code analysis tools to maintain a clean, efficient, and distraction-free codebase. |
| 11 | + |
| 12 | +Dead code refers to code that's present in your codebase but is never executed. It can lead to: |
| 13 | + |
| 14 | +- Increased compilation times |
| 15 | +- Confusion during development |
| 16 | +- Misleading assumptions about functionality |
| 17 | + |
| 18 | +ReScript’s language design allows for accurate and efficient dead code analysis using the **ReScript Code Analyzer**, available via the official VSCode extension. |
| 19 | + |
| 20 | +### Prerequisites |
| 21 | + |
| 22 | +- ReScript VSCode extension (v1.8.2 or higher) |
| 23 | + |
| 24 | +### Activation |
| 25 | + |
| 26 | +1. Open the Command Palette: `Cmd/Ctrl + P` |
| 27 | +2. Run: `> ReScript: Start Code Analyzer` |
| 28 | + |
| 29 | +### Deactivation |
| 30 | + |
| 31 | +- Run: `> ReScript: Stop Code Analyzer` |
| 32 | +- Or click “Stop Code Analyzer” in the status bar |
| 33 | + |
| 34 | +### Result |
| 35 | + |
| 36 | +- The “Problems” pane populates with dead code warnings and suggestions. |
| 37 | + |
| 38 | +## Real-World Use Cases |
| 39 | + |
| 40 | +### 1. **Unused Record Fields** |
| 41 | + |
| 42 | +```rescript |
| 43 | +type useReturn = { |
| 44 | + items: array<item>, |
| 45 | + toggleItemChecked: string => unit, // ← Never used |
| 46 | + setCheckedOnItem: (string, bool) => unit, |
| 47 | + checkAll: unit => unit, |
| 48 | + uncheckAll: unit => unit, |
| 49 | +} |
| 50 | +``` |
| 51 | + |
| 52 | +Remove unused fields to simplify code. |
| 53 | + |
| 54 | +### 2. **Unused Variant Cases** |
| 55 | + |
| 56 | +```rescript |
| 57 | +type textType = |
| 58 | + | Text(string) |
| 59 | + | TextWithIcon({icon: React.element, text: string}) |
| 60 | + | Render(React.element) // ← Never constructed |
| 61 | +``` |
| 62 | + |
| 63 | +Removing unused variants allows simplifying rendering logic. |
| 64 | + |
| 65 | +### 3. **Unused Parts of State** |
| 66 | + |
| 67 | +```rescript |
| 68 | +type validationState = Idle | Invalid | Valid |
| 69 | +
|
| 70 | +type state = { |
| 71 | + oldPassword: string, |
| 72 | + newPassword: string, |
| 73 | + newPasswordRepeated: string, |
| 74 | + validationState: validationState, // ← Never read |
| 75 | +} |
| 76 | +``` |
| 77 | + |
| 78 | +Old validation logic might remain after refactors—clean it up. |
| 79 | + |
| 80 | +### 4. **Unnecessary Interface Exposure** |
| 81 | + |
| 82 | +```rescript |
| 83 | +// DrilldownTarget.resi |
| 84 | +MetricParam.parse // ← Never used |
| 85 | +MetricParam.serialize // ← Never used |
| 86 | +``` |
| 87 | + |
| 88 | +Keep interfaces minimal by removing unused exports. |
| 89 | + |
| 90 | +### 5. **Unused Functions** |
| 91 | + |
| 92 | +```rescript |
| 93 | +let routerUrlToPath = ... // ← Never used |
| 94 | +let routeUrlStartsWith = ... // ← Never used |
| 95 | +``` |
| 96 | + |
| 97 | +Removing these often uncovers further unused logic. |
| 98 | + |
| 99 | +### 6. **Unused Components** |
| 100 | + |
| 101 | +Components never referenced in production should be removed, unless explicitly preserved. |
| 102 | + |
| 103 | + |
| 104 | +## Keeping Some Dead Code |
| 105 | + |
| 106 | +### Use `@dead` and `@live` |
| 107 | + |
| 108 | +#### `@dead` |
| 109 | + |
| 110 | +Suppresses warnings but notifies if code becomes alive again. |
| 111 | + |
| 112 | +```rescript |
| 113 | +type user = { |
| 114 | + name: string, |
| 115 | + @dead age: int, |
| 116 | +} |
| 117 | +``` |
| 118 | + |
| 119 | +#### `@live` |
| 120 | + |
| 121 | +Permanently marks code as alive (no future warnings). |
| 122 | + |
| 123 | +```rescript |
| 124 | +@live |
| 125 | +let getUserName = user => user.name |
| 126 | +``` |
| 127 | + |
| 128 | +## Configuration |
| 129 | + |
| 130 | +Add to your `rescript.json`: |
| 131 | + |
| 132 | +```json |
| 133 | +"reanalyze": { |
| 134 | + "analysis": ["dce"], |
| 135 | + "suppress": ["src/bindings", "src/stories", "src/routes"], |
| 136 | + "unsuppress": [], |
| 137 | + "transitive": false |
| 138 | +} |
| 139 | +``` |
| 140 | + |
| 141 | +### Options: |
| 142 | + |
| 143 | +- **analysis**: Enables dead code analysis (`"dce"`) |
| 144 | +- **suppress**: Silences reporting for paths (still analyzes) |
| 145 | +- **unsuppress**: Re-enables reports within suppressed paths |
| 146 | +- **transitive**: Controls reporting of indirectly dead code |
| 147 | + |
| 148 | +**Recommendation:** Set `transitive: false` for incremental cleanup. |
| 149 | + |
| 150 | +## Summary |
| 151 | + |
| 152 | +ReScript’s dead code analyzer helps you: |
| 153 | + |
| 154 | +- Incrementally clean up your codebase |
| 155 | +- Avoid confusion and complexity |
| 156 | +- Improve long-term maintainability |
| 157 | + |
| 158 | +Use it regularly for the best results. |
0 commit comments