Skip to content

Commit 8b0b60b

Browse files
committed
fix: introduce utilities for v9 compatability
1 parent 4dc7caa commit 8b0b60b

File tree

2 files changed

+85
-0
lines changed

2 files changed

+85
-0
lines changed

lib/utils/compat.ts

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
import { type TSESLint, type TSESTree } from '@typescript-eslint/utils';
2+
3+
declare module '@typescript-eslint/utils/dist/ts-eslint/Rule' {
4+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
5+
export interface RuleContext<TMessageIds extends string, TOptions extends readonly unknown[]> {
6+
/**
7+
* The filename associated with the source.
8+
*/
9+
filename: string;
10+
11+
/**
12+
* A SourceCode object that you can use to work with the source that
13+
* was passed to ESLint.
14+
*/
15+
sourceCode: Readonly<TSESLint.SourceCode>;
16+
}
17+
}
18+
19+
declare module '@typescript-eslint/utils/dist/ts-eslint/SourceCode' {
20+
export interface SourceCode {
21+
/**
22+
* Returns the scope of the given node.
23+
* This information can be used track references to variables.
24+
* @since 8.37.0
25+
*/
26+
getScope(node: TSESTree.Node): TSESLint.Scope.Scope;
27+
/**
28+
* Returns an array of the ancestors of the given node, starting at
29+
* the root of the AST and continuing through the direct parent of the current node.
30+
* This array does not include the currently-traversed node itself.
31+
* @since 8.38.0
32+
*/
33+
getAncestors(node: TSESTree.Node): TSESTree.Node[];
34+
/**
35+
* Returns a list of variables declared by the given node.
36+
* This information can be used to track references to variables.
37+
* @since 8.38.0
38+
*/
39+
getDeclaredVariables(
40+
node: TSESTree.Node,
41+
): readonly TSESLint.Scope.Variable[];
42+
}
43+
}
44+
45+
/* istanbul ignore next */
46+
export const getFilename = (
47+
context: TSESLint.RuleContext<string, unknown[]>,
48+
) => {
49+
return context.filename ?? context.getFilename();
50+
};
51+
52+
/* istanbul ignore next */
53+
export const getSourceCode = (
54+
context: TSESLint.RuleContext<string, unknown[]>,
55+
) => {
56+
return context.sourceCode ?? context.getSourceCode();
57+
};
58+
59+
/* istanbul ignore next */
60+
export const getScope = (
61+
context: TSESLint.RuleContext<string, unknown[]>,
62+
node: TSESTree.Node,
63+
) => {
64+
return getSourceCode(context).getScope?.(node) ?? context.getScope();
65+
};
66+
67+
/* istanbul ignore next */
68+
export const getAncestors = (
69+
context: TSESLint.RuleContext<string, unknown[]>,
70+
node: TSESTree.Node,
71+
) => {
72+
return getSourceCode(context).getAncestors?.(node) ?? context.getAncestors();
73+
};
74+
75+
/* istanbul ignore next */
76+
export const getDeclaredVariables = (
77+
context: TSESLint.RuleContext<string, unknown[]>,
78+
node: TSESTree.Node,
79+
) => {
80+
return (
81+
getSourceCode(context).getDeclaredVariables?.(node) ??
82+
context.getDeclaredVariables(node)
83+
);
84+
};

lib/utils/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
export * from './compat';
12
export * from './file-import';
23
export * from './types';
34

0 commit comments

Comments
 (0)