Skip to content

Commit

Permalink
feat(prefer-immutable-types)!: use suggestions instead of a fixer by …
Browse files Browse the repository at this point in the history
…default (#598)

BREAKING CHANGE: The fixer config no longer inherits as many options as before; be sure to be
explicit in your configs.
  • Loading branch information
RebeccaStevens committed Mar 31, 2023
1 parent 17066d4 commit ba7d855
Show file tree
Hide file tree
Showing 6 changed files with 560 additions and 229 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ The [below section](#rules) gives details on which rules are enabled by each rul
| :--------------------------------------------------------------------------- | :-------------------------------------------------------------- | :------------------------------- | :-- | :-- | :-- | :-- | :-- |
| [immutable-data](docs/rules/immutable-data.md) | Enforce treating data as immutable. | ☑️ ✅ 🔒 ![badge-no-mutations][] | | | | | |
| [no-let](docs/rules/no-let.md) | Disallow mutable variables. | ☑️ ✅ 🔒 ![badge-no-mutations][] | | | | | |
| [prefer-immutable-types](docs/rules/prefer-immutable-types.md) | Require function parameters to be typed as certain immutability | ☑️ ✅ 🔒 ![badge-no-mutations][] | | | 🔧 | | |
| [prefer-immutable-types](docs/rules/prefer-immutable-types.md) | Require function parameters to be typed as certain immutability | ☑️ ✅ 🔒 ![badge-no-mutations][] | | | 🔧 | 💡 | |
| [prefer-readonly-type](docs/rules/prefer-readonly-type.md) | Prefer readonly types over mutable types. | | | | 🔧 | ||
| [type-declaration-immutability](docs/rules/type-declaration-immutability.md) | Enforce the immutability of types based on patterns. | ☑️ ✅ 🔒 ![badge-no-mutations][] | | | 🔧 | | |

Expand Down
92 changes: 54 additions & 38 deletions docs/rules/prefer-immutable-types.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

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

🔧 This rule is automatically fixable by the [`--fix` CLI option](https://eslint.org/docs/latest/user-guide/command-line-interface#--fix).
🔧💡 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).

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

Expand Down Expand Up @@ -178,22 +178,23 @@ type Options = {
ignoreTypePattern?: string[] | string;
};

fixer?:
| {
ReadonlyShallow?:
| { pattern: string; replace: string }
| Array<{ pattern: string; replace: string }>
| false;
ReadonlyDeep?:
| { pattern: string; replace: string }
| Array<{ pattern: string; replace: string }>
| false;
Immutable?:
| { pattern: string; replace: string }
| Array<{ pattern: string; replace: string }>
| false;
}
| false;
fixer?: {
ReadonlyShallow?:
| { pattern: string; replace: string }
| Array<{ pattern: string; replace: string }>;
ReadonlyDeep?:
| { pattern: string; replace: string }
| Array<{ pattern: string; replace: string }>;
Immutable?:
| { pattern: string; replace: string }
| Array<{ pattern: string; replace: string }>;
};

suggestions?: {
ReadonlyShallow?: Array<Array<{ pattern: string; replace: string }>>;
ReadonlyDeep?: Array<Array<{ pattern: string; replace: string }>>;
Immutable?: Array<Array<{ pattern: string; replace: string }>>;
};
};
```

Expand All @@ -204,23 +205,25 @@ const defaults = {
enforcement: "Immutable",
ignoreClasses: false,
ignoreInferredTypes: false,
fixer: {
fixer: false,
suggestions: {
ReadonlyShallow: [
{
pattern: "^([_$a-zA-Z\\xA0-\\uFFFF][_$a-zA-Z0-9\\xA0-\\uFFFF]*\\[\\])$",
replace: "readonly $1",
},
{
pattern: "^(Array|Map|Set)<(.+)>$",
replace: "Readonly$1<$2>",
},
{
pattern: "^(.+)$",
replace: "Readonly<$1>",
},
[
{
pattern:
"^([_$a-zA-Z\\xA0-\\uFFFF][_$a-zA-Z0-9\\xA0-\\uFFFF]*\\[\\])$",
replace: "readonly $1",
},
{
pattern: "^(Array|Map|Set)<(.+)>$",
replace: "Readonly$1<$2>",
},
{
pattern: "^(.+)$",
replace: "Readonly<$1>",
},
],
],
ReadonlyDeep: false,
Immutable: false,
},
};
```
Expand Down Expand Up @@ -384,20 +387,33 @@ If set to `false`, the fixer will be disabled.

#### `fixer.*`

By default we only configure the fixer to correct shallow readonly violations as TypeScript itself provides a utility type for this.
Configure how the fixer should fix issue of each of the different enforcement levels.

### `suggestions`

This is the same as `fixer` but for manual suggestions instead of automatic fixers.
If set to `false`, the no suggestions will be enabled.

### `suggestions[*].*`

Configure how the suggestion should fix issue of each of the different enforcement levels.

By default we only configure the suggestions to correct shallow readonly violations as TypeScript itself provides a utility type for this.
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.

Example using `ReadonlyDeep` instead of `Readonly`:

```jsonc
{
// ...
"fixer": {
"suggestions": {
"ReadonlyDeep": [
{
"pattern": "^(?:Readonly<(.+)>|(.+))$",
"replace": "ReadonlyDeep<$1$2>"
}
[
{
"pattern": "^(?:Readonly<(.+)>|(.+))$",
"replace": "ReadonlyDeep<$1$2>"
}
]
]
}
}
Expand Down

0 comments on commit ba7d855

Please sign in to comment.