Skip to content

Commit

Permalink
Revert "Remove legacy rule function form"
Browse files Browse the repository at this point in the history
This reverts commit e053179.
  • Loading branch information
JoshuaKGoldberg committed May 8, 2024
1 parent 6b6e249 commit bc6d611
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 14 deletions.
5 changes: 3 additions & 2 deletions packages/rule-tester/src/RuleTester.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import type * as ParserType from '@typescript-eslint/parser';
import type { TSESTree } from '@typescript-eslint/utils';
import { deepMerge } from '@typescript-eslint/utils/eslint-utils';
import type {
AnyRuleCreateFunction,
AnyRuleModule,
Parser,
ParserOptions,
Expand Down Expand Up @@ -74,7 +75,7 @@ let defaultConfig = deepMerge(

export class RuleTester extends TestFramework {
readonly #testerConfig: TesterConfigWithDefaults;
readonly #rules: Record<string, AnyRuleModule> = {};
readonly #rules: Record<string, AnyRuleCreateFunction | AnyRuleModule> = {};
readonly #linter: Linter = new Linter({ configType: 'eslintrc' });

/**
Expand Down Expand Up @@ -170,7 +171,7 @@ export class RuleTester extends TestFramework {
/**
* Define a rule for one particular run of tests.
*/
defineRule(name: string, rule: AnyRuleModule): void {
defineRule(name: string, rule: AnyRuleCreateFunction | AnyRuleModule): void {
this.#rules[name] = rule;
}

Expand Down
12 changes: 9 additions & 3 deletions packages/utils/src/eslint-utils/InferTypesFromRule.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,23 @@
import type { RuleModule } from '../ts-eslint';
import type { RuleCreateFunction, RuleModule } from '../ts-eslint';

/**
* Uses type inference to fetch the Options type from the given RuleModule
*/
type InferOptionsTypeFromRule<T> =
T extends RuleModule<infer _MessageIds, infer Options> ? Options : unknown;
T extends RuleModule<infer _MessageIds, infer Options>
? Options
: T extends RuleCreateFunction<infer _MessageIds, infer Options>
? Options
: unknown;

/**
* Uses type inference to fetch the MessageIds type from the given RuleModule
*/
type InferMessageIdsTypeFromRule<T> =
T extends RuleModule<infer MessageIds, infer _TOptions>
? MessageIds
: unknown;
: T extends RuleCreateFunction<infer MessageIds, infer _TOptions>
? MessageIds
: unknown;

export { InferOptionsTypeFromRule, InferMessageIdsTypeFromRule };
23 changes: 19 additions & 4 deletions packages/utils/src/ts-eslint/Linter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,13 @@ import { Linter as ESLintLinter } from 'eslint';
import type { ClassicConfig, FlatConfig, SharedConfig } from './Config';
import type { Parser } from './Parser';
import type { Processor as ProcessorType } from './Processor';
import type { AnyRuleModule, RuleFix, RuleModule } from './Rule';
import type {
AnyRuleCreateFunction,
AnyRuleModule,
RuleCreateFunction,
RuleFix,
RuleModule,
} from './Rule';
import type { SourceCode } from './SourceCode';

export type MinimalRuleModule<
Expand Down Expand Up @@ -35,15 +41,19 @@ declare class LinterBase {
*/
defineRule<MessageIds extends string, Options extends readonly unknown[]>(
ruleId: string,
ruleModule: MinimalRuleModule<MessageIds, Options>,
ruleModule: MinimalRuleModule<MessageIds, Options> | RuleCreateFunction,
): void;

/**
* Defines many new linting rules.
* @param rulesToDefine map from unique rule identifier to rule
*/
defineRules<MessageIds extends string, Options extends readonly unknown[]>(
rulesToDefine: Record<string, MinimalRuleModule<MessageIds, Options>>,
rulesToDefine: Record<
string,
| MinimalRuleModule<MessageIds, Options>
| RuleCreateFunction<MessageIds, Options>
>,
): void;

/**
Expand Down Expand Up @@ -266,6 +276,11 @@ namespace Linter {
parserOptions?: ParserOptions;
}

// TODO - RuleCreateFunction is no longer supported in ESLint v9
export type LegacyPluginRules = Record<
string,
AnyRuleCreateFunction | AnyRuleModule
>;
export type PluginRules = Record<string, AnyRuleModule>;

export interface Plugin {
Expand All @@ -288,7 +303,7 @@ namespace Linter {
/**
* The definition of plugin rules.
*/
rules?: PluginRules;
rules?: LegacyPluginRules;
}
}

Expand Down
20 changes: 16 additions & 4 deletions packages/utils/src/ts-eslint/Rule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -663,10 +663,13 @@ export type AnyRuleModule = RuleModule<string, readonly unknown[]>;
*
* @see {@link LooseParserModule}, {@link LooseProcessorModule}
*/
export interface LooseRuleDefinition {
meta?: object | undefined;
create: LooseRuleCreateFunction;
}
export type LooseRuleDefinition =
// TODO - ESLint v9 will remove support for RuleCreateFunction
| LooseRuleCreateFunction
| {
meta?: object | undefined;
create: LooseRuleCreateFunction;
};
/*
eslint-disable-next-line @typescript-eslint/no-explicit-any --
intentionally using `any` to allow bi-directional assignment (unknown and
Expand All @@ -682,3 +685,12 @@ export type LooseRuleCreateFunction = (context: any) => Record<
*/
Function | undefined
>;

export type RuleCreateFunction<
MessageIds extends string = never,
Options extends readonly unknown[] = unknown[],
> = (context: Readonly<RuleContext<MessageIds, Options>>) => RuleListener;
export type AnyRuleCreateFunction = RuleCreateFunction<
string,
readonly unknown[]
>;
5 changes: 4 additions & 1 deletion packages/utils/src/ts-eslint/RuleTester.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import type { Linter } from './Linter';
import type { ParserOptions } from './ParserOptions';
import type {
ReportDescriptorMessageData,
RuleCreateFunction,
RuleModule,
SharedConfigurationSettings,
} from './Rule';
Expand Down Expand Up @@ -191,7 +192,9 @@ declare class RuleTesterBase {
*/
defineRule<MessageIds extends string, Options extends Readonly<unknown[]>>(
name: string,
rule: RuleModule<MessageIds, Options>,
rule:
| RuleCreateFunction<MessageIds, Options>
| RuleModule<MessageIds, Options>,
): void;
}

Expand Down

0 comments on commit bc6d611

Please sign in to comment.