Skip to content

Commit 400d888

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

File tree

6 files changed

+565
-232
lines changed

6 files changed

+565
-232
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ The [below section](#rules) gives details on which rules are enabled by each rul
110110
| :--------------------------------------------------------------------------- | :-------------------------------------------------------------- | :------------------------------- | :-- | :-- | :-- | :-- | :-- |
111111
| [immutable-data](docs/rules/immutable-data.md) | Enforce treating data as immutable. | ☑️ ✅ 🔒 ![badge-no-mutations][] | | | | | |
112112
| [no-let](docs/rules/no-let.md) | Disallow mutable variables. | ☑️ ✅ 🔒 ![badge-no-mutations][] | | | | | |
113-
| [prefer-immutable-types](docs/rules/prefer-immutable-types.md) | Require function parameters to be typed as certain immutability | ☑️ ✅ 🔒 ![badge-no-mutations][] | | | 🔧 | | |
113+
| [prefer-immutable-types](docs/rules/prefer-immutable-types.md) | Require function parameters to be typed as certain immutability | ☑️ ✅ 🔒 ![badge-no-mutations][] | | | 🔧 | 💡 | |
114114
| [prefer-readonly-type](docs/rules/prefer-readonly-type.md) | Prefer readonly types over mutable types. | | | | 🔧 | ||
115115
| [type-declaration-immutability](docs/rules/type-declaration-immutability.md) | Enforce the immutability of types based on patterns. | ☑️ ✅ 🔒 ![badge-no-mutations][] | | | 🔧 | | |
116116

docs/rules/prefer-immutable-types.md

Lines changed: 59 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
💼 This rule is enabled in the following configs: ☑️ `lite`, `no-mutations`, ✅ `recommended`, 🔒 `strict`.
44

5-
🔧 This rule is automatically fixable by the [`--fix` CLI option](https://eslint.org/docs/latest/user-guide/command-line-interface#--fix).
5+
🔧💡 This rule is automatically fixable by the [`--fix` CLI option](https://eslint.org/docs/latest/user-guide/command-line-interface#--fix) and manually fixable by [editor suggestions](https://eslint.org/docs/developer-guide/working-with-rules#providing-suggestions).
66

77
<!-- end auto-generated rule header -->
88

@@ -178,22 +178,23 @@ type Options = {
178178
ignoreTypePattern?: string[] | string;
179179
};
180180

181-
fixer?:
182-
| {
183-
ReadonlyShallow?:
184-
| { pattern: string; replace: string }
185-
| Array<{ pattern: string; replace: string }>
186-
| false;
187-
ReadonlyDeep?:
188-
| { pattern: string; replace: string }
189-
| Array<{ pattern: string; replace: string }>
190-
| false;
191-
Immutable?:
192-
| { pattern: string; replace: string }
193-
| Array<{ pattern: string; replace: string }>
194-
| false;
195-
}
196-
| false;
181+
fixer?: {
182+
ReadonlyShallow?:
183+
| { pattern: string; replace: string }
184+
| Array<{ pattern: string; replace: string }>;
185+
ReadonlyDeep?:
186+
| { pattern: string; replace: string }
187+
| Array<{ pattern: string; replace: string }>;
188+
Immutable?:
189+
| { pattern: string; replace: string }
190+
| Array<{ pattern: string; replace: string }>;
191+
};
192+
193+
suggestions?: {
194+
ReadonlyShallow?: Array<Array<{ pattern: string; replace: string }>>;
195+
ReadonlyDeep?: Array<Array<{ pattern: string; replace: string }>>;
196+
Immutable?: Array<Array<{ pattern: string; replace: string }>>;
197+
};
197198
};
198199
```
199200

@@ -204,24 +205,28 @@ const defaults = {
204205
enforcement: "Immutable",
205206
ignoreClasses: false,
206207
ignoreInferredTypes: false,
207-
fixer: {
208-
ReadonlyShallow: [
209-
{
210-
pattern: "^([_$a-zA-Z\\xA0-\\uFFFF][_$a-zA-Z0-9\\xA0-\\uFFFF]*\\[\\])$",
211-
replace: "readonly $1",
212-
},
213-
{
214-
pattern: "^(Array|Map|Set)<(.+)>$",
215-
replace: "Readonly$1<$2>",
216-
},
217-
{
218-
pattern: "^(.+)$",
219-
replace: "Readonly<$1>",
220-
},
221-
],
222-
ReadonlyDeep: false,
223-
Immutable: false,
224-
},
208+
fixer: false,
209+
suggestions: [
210+
{
211+
ReadonlyShallow: [
212+
{
213+
pattern:
214+
"^([_$a-zA-Z\\xA0-\\uFFFF][_$a-zA-Z0-9\\xA0-\\uFFFF]*\\[\\])$",
215+
replace: "readonly $1",
216+
},
217+
{
218+
pattern: "^(Array|Map|Set)<(.+)>$",
219+
replace: "Readonly$1<$2>",
220+
},
221+
{
222+
pattern: "^(.+)$",
223+
replace: "Readonly<$1>",
224+
},
225+
],
226+
ReadonlyDeep: false,
227+
Immutable: false,
228+
},
229+
],
225230
};
226231
```
227232

@@ -384,20 +389,33 @@ If set to `false`, the fixer will be disabled.
384389

385390
#### `fixer.*`
386391

387-
By default we only configure the fixer to correct shallow readonly violations as TypeScript itself provides a utility type for this.
392+
Configure how the fixer should fix issue of each of the different enforcement levels.
393+
394+
### `suggestions`
395+
396+
This is the same as `fixer` but for manual suggestions instead of automatic fixers.
397+
If set to `false`, the no suggestions will be enabled.
398+
399+
### `suggestions[*].*`
400+
401+
Configure how the suggestion should fix issue of each of the different enforcement levels.
402+
403+
By default we only configure the suggestions to correct shallow readonly violations as TypeScript itself provides a utility type for this.
388404
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.
389405

390406
Example using `ReadonlyDeep` instead of `Readonly`:
391407

392408
```jsonc
393409
{
394410
// ...
395-
"fixer": {
411+
"suggestions": {
396412
"ReadonlyDeep": [
397-
{
398-
"pattern": "^(?:Readonly<(.+)>|(.+))$",
399-
"replace": "ReadonlyDeep<$1$2>"
400-
}
413+
[
414+
{
415+
"pattern": "^(?:Readonly<(.+)>|(.+))$",
416+
"replace": "ReadonlyDeep<$1$2>"
417+
}
418+
]
401419
]
402420
}
403421
}

0 commit comments

Comments
 (0)