Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Check messageIds in context.report() #140

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading