Skip to content

Commit

Permalink
feat(prefer-immutable-types): allow overriding options based on where…
Browse files Browse the repository at this point in the history
… the type is declared

fix #800
  • Loading branch information
RebeccaStevens committed May 3, 2024
1 parent 854b721 commit 3818253
Show file tree
Hide file tree
Showing 5 changed files with 430 additions and 207 deletions.
54 changes: 54 additions & 0 deletions docs/rules/prefer-immutable-types.md
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,37 @@ type Options = {
ReadonlyDeep?: Array<Array<{ pattern: string; replace: string }>>;
Immutable?: Array<Array<{ pattern: string; replace: string }>>;
};

overrides?: Array<{
match: Array<
| {
from: "file";
path?: string;
name?: string | string[];
pattern?: RegExp | RegExp[];
ignoreName?: string | string[];
ignorePattern?: RegExp | RegExp[];
}
| {
from: "lib";
name?: string | string[];
pattern?: RegExp | RegExp[];
ignoreName?: string | string[];
ignorePattern?: RegExp | RegExp[];
}
| {
from: "package";
package?: string;
name?: string | string[];
pattern?: RegExp | RegExp[];
ignoreName?: string | string[];
ignorePattern?: RegExp | RegExp[];
}
>;
options: Omit<Options, "overrides">;
inherit?: boolean;
disable: boolean;
}>;
};
```

Expand Down Expand Up @@ -475,3 +506,26 @@ It allows for the ability to ignore violations based on the identifier (name) of

This option takes a `RegExp` string or an array of `RegExp` strings.
It allows for the ability to ignore violations based on the type (as written, with whitespace removed) of the node in question.

### `overrides`

Allows for applying overrides to the options based on where the type is defined.
This can be used to override the settings for types coming from 3rd party libraries.

Note: Only the first matching override will be used.

#### `overrides[n].specifiers`

A specifier, or an array of specifiers to match the function type against.

#### `overrides[n].options`

The options to use when a specifiers matches.

#### `overrides[n].inherit`

Inherit the root options? Default is `true`.

#### `overrides[n].disable`

If true, when a specifier matches, this rule will not be applied to the matching node.
18 changes: 18 additions & 0 deletions src/options/overrides.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { type TSESTree } from "@typescript-eslint/utils";
import { type RuleContext } from "@typescript-eslint/utils/ts-eslint";
import { deepmerge } from "deepmerge-ts";
import typeMatchesSpecifier from "ts-declaration-location";
import { type Type, type TypeNode } from "typescript";

import { getTypeDataOfNode } from "#eslint-plugin-functional/utils/rule";
import {
Expand Down Expand Up @@ -108,6 +109,23 @@ export function getCoreOptions<
}

const [type, typeNode] = getTypeDataOfNode(node, context);
return getCoreOptionsForType(type, typeNode, context, options);
}

export function getCoreOptionsForType<
CoreOptions extends object,
Options extends Readonly<OverridableOptions<CoreOptions>>,
>(
type: Type,
typeNode: TypeNode | null,
context: Readonly<RuleContext<string, unknown[]>>,
options: Readonly<Options>,
): CoreOptions | null {
const program = context.sourceCode.parserServices?.program ?? undefined;
if (program === undefined) {
return options;
}

const found = options.overrides?.find((override) =>
(Array.isArray(override.specifiers)
? override.specifiers
Expand Down
Loading

0 comments on commit 3818253

Please sign in to comment.