|
| 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 | +}; |
0 commit comments