Skip to content

Commit a0befc6

Browse files
feat(prefer-immutable-types): add support for suggestions
1 parent fcaaeb8 commit a0befc6

File tree

5 files changed

+536
-203
lines changed

5 files changed

+536
-203
lines changed

docs/rules/prefer-immutable-types.md

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -182,18 +182,24 @@ type Options = {
182182
| {
183183
ReadonlyShallow?:
184184
| { pattern: string; replace: string }
185-
| Array<{ pattern: string; replace: string }>
186-
| false;
185+
| Array<{ pattern: string; replace: string }>;
187186
ReadonlyDeep?:
188187
| { pattern: string; replace: string }
189-
| Array<{ pattern: string; replace: string }>
190-
| false;
188+
| Array<{ pattern: string; replace: string }>;
191189
Immutable?:
192190
| { pattern: string; replace: string }
193-
| Array<{ pattern: string; replace: string }>
194-
| false;
195-
}
196-
| false;
191+
| Array<{ pattern: string; replace: string }>;
192+
};
193+
194+
suggestions?:
195+
| {
196+
ReadonlyShallow?:
197+
| Array<Array<{ pattern: string; replace: string }>>;
198+
ReadonlyDeep?:
199+
| Array<Array<{ pattern: string; replace: string }>>;
200+
Immutable?:
201+
| Array<Array<{ pattern: string; replace: string }>>;
202+
};
197203
};
198204
```
199205

@@ -204,7 +210,8 @@ const defaults = {
204210
enforcement: "Immutable",
205211
ignoreClasses: false,
206212
ignoreInferredTypes: false,
207-
fixer: {
213+
fixer: false,
214+
suggestions: [{
208215
ReadonlyShallow: [
209216
{
210217
pattern: "^([_$a-zA-Z\\xA0-\\uFFFF][_$a-zA-Z0-9\\xA0-\\uFFFF]*\\[\\])$",
@@ -221,7 +228,7 @@ const defaults = {
221228
],
222229
ReadonlyDeep: false,
223230
Immutable: false,
224-
},
231+
}],
225232
};
226233
```
227234

@@ -384,22 +391,33 @@ If set to `false`, the fixer will be disabled.
384391

385392
#### `fixer.*`
386393

387-
By default we only configure the fixer to correct shallow readonly violations as TypeScript itself provides a utility type for this.
394+
Configure how the fixer should fix issue of each of the different enforcement levels.
395+
396+
### `suggestions`
397+
398+
This is the same as `fixer` but for manual suggestions instead of automatic fixers.
399+
If set to `false`, the no suggestions will be enabled.
400+
401+
### `suggestions[*].*`
402+
403+
Configure how the suggestion should fix issue of each of the different enforcement levels.
404+
405+
By default we only configure the suggestions to correct shallow readonly violations as TypeScript itself provides a utility type for this.
388406
If you have access to other utility types (such as [type-fest's `ReadonlyDeep`](https://github.com/sindresorhus/type-fest#:~:text=set%20to%20optional.-,ReadonlyDeep,-%2D%20Create%20a%20deeply)), you can configure the fixer to use them with this option.
389407

390408
Example using `ReadonlyDeep` instead of `Readonly`:
391409

392410
```jsonc
393411
{
394412
// ...
395-
"fixer": {
413+
"suggestions": [{
396414
"ReadonlyDeep": [
397415
{
398416
"pattern": "^(?:Readonly<(.+)>|(.+))$",
399417
"replace": "ReadonlyDeep<$1$2>"
400418
}
401419
]
402-
}
420+
}]
403421
}
404422
```
405423

0 commit comments

Comments
 (0)