Skip to content

Commit

Permalink
feat(prefer-immutable-types): add support for suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
RebeccaStevens committed Mar 22, 2023
1 parent fcaaeb8 commit a0befc6
Show file tree
Hide file tree
Showing 5 changed files with 536 additions and 203 deletions.
44 changes: 31 additions & 13 deletions docs/rules/prefer-immutable-types.md
Original file line number Diff line number Diff line change
Expand Up @@ -182,18 +182,24 @@ type Options = {
| {
ReadonlyShallow?:
| { pattern: string; replace: string }
| Array<{ pattern: string; replace: string }>
| false;
| Array<{ pattern: string; replace: string }>;
ReadonlyDeep?:
| { pattern: string; replace: string }
| Array<{ pattern: string; replace: string }>
| false;
| Array<{ pattern: string; replace: string }>;
Immutable?:
| { pattern: string; replace: string }
| Array<{ pattern: string; replace: string }>
| false;
}
| false;
| 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,7 +210,8 @@ 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]*\\[\\])$",
Expand All @@ -221,7 +228,7 @@ const defaults = {
],
ReadonlyDeep: false,
Immutable: false,
},
}],
};
```

Expand Down Expand Up @@ -384,22 +391,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>"
}
]
}
}]
}
```

Expand Down

0 comments on commit a0befc6

Please sign in to comment.