Skip to content

Commit 1c9ca4d

Browse files
authored
feat: Check messageIds in context.report() (#140)
1 parent 43416a1 commit 1c9ca4d

File tree

2 files changed

+17
-10
lines changed

2 files changed

+17
-10
lines changed

packages/core/src/types.ts

+14-10
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,7 @@ export interface RuleContextTypeOptions {
187187
Code: SourceCode;
188188
RuleOptions: unknown[];
189189
Node: unknown;
190+
MessageIds: string;
190191
}
191192

192193
/**
@@ -195,12 +196,7 @@ export interface RuleContextTypeOptions {
195196
* view into the outside world.
196197
*/
197198
export interface RuleContext<
198-
Options extends RuleContextTypeOptions = {
199-
LangOptions: LanguageOptions;
200-
Code: SourceCode;
201-
RuleOptions: unknown[];
202-
Node: unknown;
203-
},
199+
Options extends RuleContextTypeOptions = RuleContextTypeOptions,
204200
> {
205201
/**
206202
* The current working directory for the session.
@@ -282,7 +278,9 @@ export interface RuleContext<
282278
* The report function that the rule should use to report problems.
283279
* @param violation The violation to report.
284280
*/
285-
report(violation: ViolationReport<Options["Node"]>): void;
281+
report(
282+
violation: ViolationReport<Options["Node"], Options["MessageIds"]>,
283+
): void;
286284
}
287285

288286
// #region Rule Fixing
@@ -402,11 +400,16 @@ interface ViolationReportBase {
402400
suggest?: SuggestedEdit[];
403401
}
404402

405-
type ViolationMessage = { message: string } | { messageId: string };
403+
type ViolationMessage<MessageIds = string> =
404+
| { message: string }
405+
| { messageId: MessageIds };
406406
type ViolationLocation<Node> = { loc: SourceLocation } | { node: Node };
407407

408-
export type ViolationReport<Node = unknown> = ViolationReportBase &
409-
ViolationMessage &
408+
export type ViolationReport<
409+
Node = unknown,
410+
MessageIds = string,
411+
> = ViolationReportBase &
412+
ViolationMessage<MessageIds> &
410413
ViolationLocation<Node>;
411414

412415
// #region Suggestions
@@ -469,6 +472,7 @@ export interface RuleDefinition<
469472
Code: Options["Code"];
470473
RuleOptions: Options["RuleOptions"];
471474
Node: Options["Node"];
475+
MessageIds: Options["MessageIds"];
472476
}>,
473477
): Options["Visitor"];
474478
}

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

+3
Original file line numberDiff line numberDiff line change
@@ -177,11 +177,14 @@ interface TestRuleVisitor extends RuleVisitor {
177177
Node?: (node: TestNode) => void;
178178
}
179179

180+
type TestMessageIds = "badFoo" | "wrongBar";
181+
180182
type TestRuleContext = RuleContext<{
181183
LangOptions: TestLanguageOptions;
182184
Code: TestSourceCode;
183185
RuleOptions: [{ foo: string; bar: number }];
184186
Node: TestNode;
187+
MessageIds: TestMessageIds;
185188
}>;
186189

187190
const testRule: RuleDefinition<{

0 commit comments

Comments
 (0)