Skip to content

Commit

Permalink
fix: improve typing exposed for flat configs
Browse files Browse the repository at this point in the history
  • Loading branch information
RebeccaStevens committed Mar 24, 2024
1 parent a31b512 commit 6a65ac1
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 12 deletions.
11 changes: 7 additions & 4 deletions src/utils/rule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import {
type ReportDescriptor,
type RuleContext,
type RuleListener,
type RuleModule,
} from "@typescript-eslint/utils/ts-eslint";
import {
Immutability,
Expand Down Expand Up @@ -51,11 +50,15 @@ export type NamedCreateRuleCustomMeta<
*/
export type BaseOptions = ReadonlyArray<unknown>;

export type CustomRuleModule<
// eslint-disable-next-line functional/no-mixed-types
export type RuleDefinition<
MessageIds extends string,
Options extends BaseOptions,
> = Omit<RuleModule<MessageIds, Options>, "meta"> & {
> = {
readonly meta: NamedCreateRuleCustomMeta<MessageIds, Options>;
readonly create: (
context: RuleDefinition<MessageIds, Options>,
) => RuleListener;
};

/**
Expand Down Expand Up @@ -174,7 +177,7 @@ export function createRuleUsingFunction<
]),
);
},
}) as unknown as CustomRuleModule<MessageIds, Options>;
}) as unknown as RuleDefinition<MessageIds, Options>;
}

/**
Expand Down
4 changes: 2 additions & 2 deletions tests/helpers/testers.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { type CustomRuleModule } from "#eslint-plugin-functional/utils/rule";
import { type RuleDefinition } from "#eslint-plugin-functional/utils/rule";

import { getRuleTester } from "./RuleTester";
import { configs } from "./configs";
Expand All @@ -20,7 +20,7 @@ type TestFunction<
export function testRule<
TMessageIds extends string,
TOptions extends Readonly<unknown[]>,
>(ruleName: string, rule: CustomRuleModule<TMessageIds, TOptions>) {
>(ruleName: string, rule: RuleDefinition<TMessageIds, TOptions>) {
return Object.fromEntries(
[...Object.entries(configs)].map(
([configName, config]): [
Expand Down
12 changes: 6 additions & 6 deletions tests/helpers/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { type NamedCreateRuleMeta } from "@typescript-eslint/utils/eslint-utils"

import {
createRuleUsingFunction,
type CustomRuleModule,
type RuleDefinition,
type RuleFunctionsMap,
} from "#eslint-plugin-functional/utils/rule";

Expand Down Expand Up @@ -87,7 +87,7 @@ export function createDummyRule(
create: (
context: Readonly<TSESLint.RuleContext<"generic", any>>,
) => RuleFunctionsMap<any, "generic", any>,
): CustomRuleModule<string, [boolean, ...unknown[]]> {
): RuleDefinition<string, [boolean, ...unknown[]]> {
const meta: NamedCreateRuleMeta<"generic", []> = {
type: "suggestion",
docs: {
Expand Down Expand Up @@ -143,12 +143,12 @@ export function addFilename<
}

export type MessagesOf<
T extends CustomRuleModule<string, ReadonlyArray<unknown>>,
T extends RuleDefinition<string, ReadonlyArray<unknown>>,
> =
T extends CustomRuleModule<infer Messages, ReadonlyArray<unknown>>
T extends RuleDefinition<infer Messages, ReadonlyArray<unknown>>
? Messages
: never;

export type OptionsOf<
T extends CustomRuleModule<string, ReadonlyArray<unknown>>,
> = T extends CustomRuleModule<string, infer Options> ? Options : never;
T extends RuleDefinition<string, ReadonlyArray<unknown>>,
> = T extends RuleDefinition<string, infer Options> ? Options : never;

0 comments on commit 6a65ac1

Please sign in to comment.