Skip to content
Merged
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
23 changes: 15 additions & 8 deletions packages/core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -459,6 +459,15 @@ export interface RuleTextEdit {

// #endregion

/**
* Fixes a violation.
* @param fixer The text editor to apply the fix.
* @returns The fix(es) for the violation.
*/
type RuleFixer = (
fixer: RuleTextEditor,
) => RuleTextEdit | Iterable<RuleTextEdit> | null;

interface ViolationReportBase {
/**
* The type of node that the violation is for.
Expand All @@ -473,22 +482,22 @@ interface ViolationReportBase {

/**
* The fix to be applied for the violation.
* @param fixer The text editor to apply the fix.
* @returns The fix(es) for the violation.
*/
fix?(fixer: RuleTextEditor): RuleTextEdit | Iterable<RuleTextEdit> | null;
fix?: RuleFixer | null | undefined;

/**
* An array of suggested fixes for the problem. These fixes may change the
* behavior of the code, so they are not applied automatically.
*/
suggest?: SuggestedEdit[];
suggest?: SuggestedEdit[] | null | undefined;
}

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

export type ViolationReport<
Node = unknown,
Expand All @@ -507,10 +516,8 @@ interface SuggestedEditBase {

/**
* The fix to be applied for the suggestion.
* @param fixer The text editor to apply the fix.
* @returns The fix for the suggestion.
*/
fix?(fixer: RuleTextEditor): RuleTextEdit | Iterable<RuleTextEdit> | null;
fix?: RuleFixer | null | undefined;
}

type SuggestionMessage = { desc: string } | { messageId: string };
Expand Down
11 changes: 11 additions & 0 deletions packages/core/tests/types/types.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,7 @@ const testRule: RuleDefinition<{
: "👎",
);
},
suggest: undefined,
});
}
},
Expand All @@ -268,10 +269,20 @@ const testRule: RuleDefinition<{
suggest: [
{
messageId: "Bar",
fix: null,
},
],
});
},
Baz(node: TestNode) {
// node.type === "Baz"
context.report({
message: "This baz is foobar",
loc: { line: node.start, column: 1 },
fix: null,
suggest: null,
});
},
};
},
};
Expand Down