Skip to content

Commit

Permalink
fix: Types to align with older ESLint types (#155)
Browse files Browse the repository at this point in the history
Co-authored-by: Francesco Trotta <[email protected]>
  • Loading branch information
nzakas and fasttime authored Feb 21, 2025
1 parent d4a04b9 commit 664740a
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 8 deletions.
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

0 comments on commit 664740a

Please sign in to comment.