Skip to content

feat(perfer-tacit): allow specifying which type need binds #807

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ The [below section](#rules) gives details on which rules are enabled by each rul

| Name | Description | 💼 | ⚠️ | 🚫 | 🔧 | 💡 | 💭 | ❌ |
| :----------------------------------------------------------- | :----------------------------- | :-------------------------- | :- | :- | :- | :- | :- | :- |
| [functional-parameters](docs/rules/functional-parameters.md) | Enforce functional parameters. | ☑️ ✅ 🔒 ![badge-currying][] | | | | | | |
| [functional-parameters](docs/rules/functional-parameters.md) | Enforce functional parameters. | ☑️ ✅ 🔒 ![badge-currying][] | | | | | 💭 | |

### No Exceptions

Expand Down
42 changes: 42 additions & 0 deletions docs/rules/functional-parameters.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@

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

💭 This rule requires [type information](https://typescript-eslint.io/linting/typed-linting).

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

Disallow use of rest parameters, the `arguments` keyword and enforces that functions take at least 1 parameter.

Note: type information is only required when using the [overrides](#overrides) option.

## Rule Details

In functions, `arguments` is a special variable that is implicitly available.
Expand Down Expand Up @@ -67,6 +71,23 @@ type Options = {
};
ignoreIdentifierPattern?: string[] | string;
ignorePrefixSelector?: string[] | string;
overrides?: Array<{
match:
| {
from: "file";
path?: string;
}
| {
from: "lib";
}
| {
from: "package";
package?: string;
}
| TypeDeclarationSpecifier[];
options: Omit<Options, "overrides">;
disable: boolean;
}>;
};
```

Expand Down Expand Up @@ -196,3 +217,24 @@ const sum = [1, 2, 3].reduce((carry, current) => current, 0);

This option takes a RegExp string or an array of RegExp strings.
It allows for the ability to ignore violations based on a function's name.

### `overrides`

_Using this option requires type infomation._

Allows for applying overrides to the options based on where the function's 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].disable`

If true, when a specifier matches, this rule will not be applied to the matching node.
37 changes: 37 additions & 0 deletions docs/rules/prefer-immutable-types.md
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,24 @@ type Options = {
ReadonlyDeep?: Array<Array<{ pattern: string; replace: string }>>;
Immutable?: Array<Array<{ pattern: string; replace: string }>>;
};

overrides?: Array<{
match:
| {
from: "file";
path?: string;
}
| {
from: "lib";
}
| {
from: "package";
package?: string;
}
| TypeDeclarationSpecifier[];
options: Omit<Options, "overrides">;
disable: boolean;
}>;
};
```

Expand Down Expand Up @@ -475,3 +493,22 @@ 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].disable`

If true, when a specifier matches, this rule will not be applied to the matching node.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,8 @@
"escape-string-regexp": "^4.0.0",
"is-immutable-type": "^3.1.0",
"semver": "^7.6.0",
"ts-api-utils": "^1.3.0"
"ts-api-utils": "^1.3.0",
"ts-declaration-location": "1.0.0"
},
"devDependencies": {
"@babel/eslint-parser": "7.24.1",
Expand Down
3 changes: 3 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions src/options/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from "./ignore";
export * from "./overrides";
68 changes: 68 additions & 0 deletions src/options/overrides.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import { type TSESTree } from "@typescript-eslint/utils";
import { type RuleContext } from "@typescript-eslint/utils/ts-eslint";
import typeMatchesSpecifier, {
type TypeDeclarationSpecifier,
} from "ts-declaration-location";

import { getTypeOfNode } from "../utils/rule";

/**
* Options that can be overridden.
*/
export type OverridableOptions<CoreOptions> = CoreOptions & {
overrides?: Array<
{
specifiers: TypeDeclarationSpecifier | TypeDeclarationSpecifier[];
} & (
| {
options: CoreOptions;
disable?: false;
}
| {
disable: true;
}
)
>;
};

/**
* Get the core options to use, taking into account overrides.
*
* @throws when there is a configuration error.
*/
export function getCoreOptions<
CoreOptions extends object,
Options extends readonly [Readonly<OverridableOptions<CoreOptions>>],
>(
node: TSESTree.Node,
context: Readonly<RuleContext<string, Options>>,
options: Readonly<Options>,
): CoreOptions | null {
const [optionsObject] = options;

const program = context.sourceCode.parserServices?.program ?? undefined;
if (program === undefined) {
return optionsObject;
}

const type = getTypeOfNode(node, context);
const found = optionsObject.overrides?.find((override) =>
(Array.isArray(override.specifiers)
? override.specifiers
: [override.specifiers]
).some((specifier) => typeMatchesSpecifier(program, specifier, type)),
);

if (found !== undefined) {
if (found.disable === true) {
return null;
}
if (found.options === undefined) {
// eslint-disable-next-line functional/no-throw-statements
throw new Error("Configuration error: No options found for override.");
}
return found.options;
}

return optionsObject;
}
Loading