Skip to content

Commit

Permalink
feat: Check messageIds in context.report() (#140)
Browse files Browse the repository at this point in the history
  • Loading branch information
nzakas authored Jan 3, 2025
1 parent 43416a1 commit 1c9ca4d
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 10 deletions.
24 changes: 14 additions & 10 deletions packages/core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@ export interface RuleContextTypeOptions {
Code: SourceCode;
RuleOptions: unknown[];
Node: unknown;
MessageIds: string;
}

/**
Expand All @@ -195,12 +196,7 @@ export interface RuleContextTypeOptions {
* view into the outside world.
*/
export interface RuleContext<
Options extends RuleContextTypeOptions = {
LangOptions: LanguageOptions;
Code: SourceCode;
RuleOptions: unknown[];
Node: unknown;
},
Options extends RuleContextTypeOptions = RuleContextTypeOptions,
> {
/**
* The current working directory for the session.
Expand Down Expand Up @@ -282,7 +278,9 @@ export interface RuleContext<
* The report function that the rule should use to report problems.
* @param violation The violation to report.
*/
report(violation: ViolationReport<Options["Node"]>): void;
report(
violation: ViolationReport<Options["Node"], Options["MessageIds"]>,
): void;
}

// #region Rule Fixing
Expand Down Expand Up @@ -402,11 +400,16 @@ interface ViolationReportBase {
suggest?: SuggestedEdit[];
}

type ViolationMessage = { message: string } | { messageId: string };
type ViolationMessage<MessageIds = string> =
| { message: string }
| { messageId: MessageIds };
type ViolationLocation<Node> = { loc: SourceLocation } | { node: Node };

export type ViolationReport<Node = unknown> = ViolationReportBase &
ViolationMessage &
export type ViolationReport<
Node = unknown,
MessageIds = string,
> = ViolationReportBase &
ViolationMessage<MessageIds> &
ViolationLocation<Node>;

// #region Suggestions
Expand Down Expand Up @@ -469,6 +472,7 @@ export interface RuleDefinition<
Code: Options["Code"];
RuleOptions: Options["RuleOptions"];
Node: Options["Node"];
MessageIds: Options["MessageIds"];
}>,
): Options["Visitor"];
}
Expand Down
3 changes: 3 additions & 0 deletions packages/core/tests/types/types.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,11 +177,14 @@ interface TestRuleVisitor extends RuleVisitor {
Node?: (node: TestNode) => void;
}

type TestMessageIds = "badFoo" | "wrongBar";

type TestRuleContext = RuleContext<{
LangOptions: TestLanguageOptions;
Code: TestSourceCode;
RuleOptions: [{ foo: string; bar: number }];
Node: TestNode;
MessageIds: TestMessageIds;
}>;

const testRule: RuleDefinition<{
Expand Down

0 comments on commit 1c9ca4d

Please sign in to comment.