Skip to content

Commit d5806df

Browse files
nzakasfasttime
andauthored
feat: Set type for RuleDefinition.defaultOptions (#173)
Co-authored-by: Francesco Trotta <[email protected]>
1 parent 6199b6e commit d5806df

File tree

2 files changed

+53
-3
lines changed

2 files changed

+53
-3
lines changed

packages/core/src/types.ts

+10-3
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@ export interface RulesMetaDocs {
141141
*/
142142
export interface RulesMeta<
143143
MessageIds extends string = string,
144+
RuleOptions = unknown[],
144145
ExtRuleDocs = unknown,
145146
> {
146147
/**
@@ -158,8 +159,10 @@ export interface RulesMeta<
158159
*/
159160
schema?: JSONSchema4 | JSONSchema4[] | false | undefined;
160161

161-
/** Any default options to be recursively merged on top of any user-provided options. */
162-
defaultOptions?: unknown[];
162+
/**
163+
* Any default options to be recursively merged on top of any user-provided options.
164+
**/
165+
defaultOptions?: RuleOptions;
163166

164167
/**
165168
* The messages that the rule can report.
@@ -561,7 +564,11 @@ export interface RuleDefinition<
561564
/**
562565
* The meta information for the rule.
563566
*/
564-
meta?: RulesMeta<Options["MessageIds"], Options["ExtRuleDocs"]>;
567+
meta?: RulesMeta<
568+
Options["MessageIds"],
569+
Options["RuleOptions"],
570+
Options["ExtRuleDocs"]
571+
>;
565572

566573
/**
567574
* Creates the visitor that ESLint uses to apply the rule during traversal.

packages/core/tests/types/types.test.ts

+43
Original file line numberDiff line numberDiff line change
@@ -290,3 +290,46 @@ const testRule: RuleDefinition<{
290290
};
291291

292292
testRule.meta satisfies RulesMeta | undefined;
293+
294+
const testRuleWithInvalidDefaultOptions: RuleDefinition<{
295+
LangOptions: TestLanguageOptions;
296+
Code: TestSourceCode;
297+
RuleOptions: [{ foo: string; bar: number }];
298+
Visitor: TestRuleVisitor;
299+
Node: TestNode;
300+
MessageIds: "badFoo" | "wrongBar";
301+
ExtRuleDocs: never;
302+
}> = {
303+
meta: {
304+
type: "problem",
305+
schema: [
306+
{
307+
type: "object",
308+
properties: {
309+
foo: {
310+
type: "string",
311+
},
312+
bar: {
313+
type: "integer",
314+
},
315+
},
316+
additionalProperties: false,
317+
},
318+
],
319+
320+
defaultOptions: [
321+
{
322+
foo: "always",
323+
bar: 5,
324+
// @ts-expect-error invalid default option "baz"
325+
baz: "invalid",
326+
},
327+
],
328+
},
329+
330+
create(): TestRuleVisitor {
331+
return {};
332+
},
333+
};
334+
335+
testRuleWithInvalidDefaultOptions.meta satisfies RulesMeta | undefined;

0 commit comments

Comments
 (0)