Skip to content

fix(utils): add missing types for onCodePath* in RuleListener #6679

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

Closed
wants to merge 1 commit into from
Closed
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
49 changes: 48 additions & 1 deletion packages/utils/src/ts-eslint/Rule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ interface RuleMetaDataDocs {
*/
extendsBaseRule?: boolean | string;
}

interface RuleMetaData<TMessageIds extends string> {
/**
* True if the rule is deprecated, false otherwise
Expand Down Expand Up @@ -262,12 +263,39 @@ interface RuleContext<
report(descriptor: ReportDescriptor<TMessageIds>): void;
}

interface CodePath {
id: string;
initialSegment: CodePathSegment;
finalSegments: CodePathSegment[];
returnedSegments: CodePathSegment[];
thrownSegments: CodePathSegment[];
currentSegments: CodePathSegment[];
upper: CodePath | null;
childCodePaths: CodePath[];
}

interface CodePathSegment {
id: string;
nextSegments: CodePathSegment[];
prevSegments: CodePathSegment[];
reachable: boolean;
}

type CodePathFunction =
| ((codePath: CodePath, node: TSESTree.Node) => void)
| ((segment: CodePathSegment, node: TSESTree.Node) => void)
| ((
fromSegment: CodePathSegment,
toSegment: CodePathSegment,
node: TSESTree.Node,
) => void);

// This isn't the correct signature, but it makes it easier to do custom unions within reusable listeners
// never will break someone's code unless they specifically type the function argument
type RuleFunction<T extends TSESTree.BaseNode = never> = (node: T) => void;

interface RuleListener {
[nodeSelector: string]: RuleFunction | undefined;
[nodeSelector: string]: RuleFunction | CodePathFunction | undefined;
ArrayExpression?: RuleFunction<TSESTree.ArrayExpression>;
ArrayPattern?: RuleFunction<TSESTree.ArrayPattern>;
ArrowFunctionExpression?: RuleFunction<TSESTree.ArrowFunctionExpression>;
Expand Down Expand Up @@ -425,6 +453,22 @@ interface RuleListener {
WhileStatement?: RuleFunction<TSESTree.WhileStatement>;
WithStatement?: RuleFunction<TSESTree.WithStatement>;
YieldExpression?: RuleFunction<TSESTree.YieldExpression>;

onCodePathStart?: (codePath: CodePath, node: TSESTree.Node) => void;
onCodePathEnd?: (codePath: CodePath, node: TSESTree.Node) => void;
onCodePathSegmentStart?: (
segment: CodePathSegment,
node: TSESTree.Node,
) => void;
onCodePathSegmentEnd?: (
segment: CodePathSegment,
node: TSESTree.Node,
) => void;
onCodePathSegmentLoop?: (
fromSegment: CodePathSegment,
toSegment: CodePathSegment,
node: TSESTree.Node,
) => void;
}

interface RuleModule<
Expand Down Expand Up @@ -458,6 +502,9 @@ type RuleCreateFunction<
> = (context: Readonly<RuleContext<TMessageIds, TOptions>>) => TRuleListener;

export {
CodePath,
CodePathSegment,
CodePathFunction,
ReportDescriptor,
ReportFixFunction,
ReportSuggestionArray,
Expand Down