Skip to content

Commit 64db62e

Browse files
Editor page + reanalyze guide
1 parent f72c525 commit 64db62e

File tree

3 files changed

+197
-5
lines changed

3 files changed

+197
-5
lines changed

data/sidebar_manual_v1200.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,8 @@
6767
"warning-numbers"
6868
],
6969
"Guides": [
70-
"converting-from-js"
70+
"converting-from-js",
71+
"editor-code-analysis"
7172
],
7273
"Extra": [
7374
"newcomer-examples",
Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
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.
Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,52 @@
11
---
2-
title: "Editor Plugins"
3-
description: "List of ReScript editor plugins"
2+
title: "Editor"
3+
metaTitle: "Editor"
4+
description: "Documentation about ReScript editor plugins and code analysis"
45
canonical: "/docs/manual/v12.0.0/editor-plugins"
56
---
67

7-
# Editor Plugins
8+
# Editor
9+
10+
This section is about the editor plugin for ReScript. It adds syntax highlighting, autocomplete, type hints, formatting, code navigation, code analysis for `.res` and `.resi` files.
11+
12+
13+
## Plugins
814

915
- [VSCode](https://marketplace.visualstudio.com/items?itemName=chenglou92.rescript-vscode)
1016
- [Sublime Text](https://github.com/rescript-lang/rescript-sublime)
1117
- [Vim/Neovim](https://github.com/rescript-lang/vim-rescript)
1218

13-
## Community Supported
19+
### Community Supported
1420

1521
We don't officially support these; use them at your own risk!
1622

1723
- [Neovim Tree-sitter](https://github.com/nkrkv/nvim-treesitter-rescript)
1824
- [IDEA](https://github.com/reasonml-editor/reasonml-idea-plugin)
1925
- [Emacs](https://github.com/jjlee/rescript-mode)
26+
27+
28+
## Code analysis
29+
30+
The code analysis provides extra checks for your ReScript project, such as detecting dead code and unhandled exceptions. It's powered by [reanalyze](https://github.com/rescript-association/reanalyze), which is built into the extension—no separate install required.
31+
32+
### How to Use
33+
34+
- Open the command palette and run:
35+
`ReScript: Start Code Analyzer`
36+
- Warnings like dead code will show inline in the editor.
37+
- Suppression actions are available where applicable.
38+
- To stop analysis, click **Stop Code Analyzer** in the status bar.
39+
40+
### Configuration
41+
42+
Add a `reanalyze` section to your `rescript.json` to control what the analyzer checks or ignores. You’ll get autocomplete for config options in the editor.
43+
More details: [reanalyze config docs](https://github.com/rescript-association/reanalyze#configuration-via-bsconfigjson)
44+
45+
### Guide
46+
47+
Look [here](editor-code-analysis) for a more detailed guide about how to use the code analysis tool.
48+
49+
### Caveats
50+
51+
- Doesn't support cross-package dead code analysis in monorepos. Run it per package instead.
52+

0 commit comments

Comments
 (0)