Skip to content

Commit 3fb9028

Browse files
feat(prefer-immutable-types)!: use suggestions instead of a fixer by default (#598)
BREAKING CHANGE: The fixer config no longer inherits as many options as before; be sure to be explicit in your configs.
1 parent 1855ebd commit 3fb9028

File tree

6 files changed

+560
-229
lines changed

6 files changed

+560
-229
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: 54 additions & 38 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,23 +205,25 @@ const defaults = {
204205
enforcement: "Immutable",
205206
ignoreClasses: false,
206207
ignoreInferredTypes: false,
207-
fixer: {
208+
fixer: false,
209+
suggestions: {
208210
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-
},
211+
[
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+
],
221226
],
222-
ReadonlyDeep: false,
223-
Immutable: false,
224227
},
225228
};
226229
```
@@ -384,20 +387,33 @@ If set to `false`, the fixer will be disabled.
384387

385388
#### `fixer.*`
386389

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

390404
Example using `ReadonlyDeep` instead of `Readonly`:
391405

392406
```jsonc
393407
{
394408
// ...
395-
"fixer": {
409+
"suggestions": {
396410
"ReadonlyDeep": [
397-
{
398-
"pattern": "^(?:Readonly<(.+)>|(.+))$",
399-
"replace": "ReadonlyDeep<$1$2>"
400-
}
411+
[
412+
{
413+
"pattern": "^(?:Readonly<(.+)>|(.+))$",
414+
"replace": "ReadonlyDeep<$1$2>"
415+
}
416+
]
401417
]
402418
}
403419
}

0 commit comments

Comments
 (0)