Skip to content
Source
Sign inSign up
TanStack/query
Open History to see the last commit

ast-utils.test.ts

53 lines1.6 KB

1import { AST_NODE_TYPES } from '@typescript-eslint/utils'
2import { describe, expect, it } from 'vitest'
3import { ExhaustiveDepsUtils } from '../rules/exhaustive-deps/exhaustive-deps.utils'
4import { ASTUtils } from '../utils/ast-utils'
5import type { TSESLint, TSESTree } from '@typescript-eslint/utils'
6​
7function createIdentifier(name: string): TSESTree.Identifier {
8 return { type: AST_NODE_TYPES.Identifier, name } as TSESTree.Identifier
9}
10​
11describe('ASTUtils', () => {
12 it('stops member traversal when a node has no parent', () => {
13 const identifier = createIdentifier('value')
14​
15 expect(ASTUtils.traverseUpMemberExpression(identifier)).toBe(identifier)
16 })
17​
18 it('handles an external reference without a parent', () => {
19 const operation = createIdentifier('operation')
20 const reference = {
21 identifier: operation,
22 isRead: () => true,
23 resolved: null,
24 } as TSESLint.Scope.Reference
25 const scope = {
26 childScopes: [],
27 references: [reference],
28 set: new Map(),
29 } as unknown as TSESLint.Scope.Scope
30 const scopeManager = {
31 acquire: () => scope,
32 } as unknown as TSESLint.Scope.ScopeManager
33 const sourceCode = {
34 getText: () => 'operation',
35 } as unknown as Readonly<TSESLint.SourceCode>
36​
37 expect(
38 ASTUtils.getExternalRefs({
39 scopeManager,
40 sourceCode,
41 node: operation,
42 }),
43 ).toEqual([reference])
44 })
45})
46​
47describe('ExhaustiveDepsUtils', () => {
48 it('does not treat a detached identifier as a function call target', () => {
49 expect(
50 ExhaustiveDepsUtils.isFunctionCallTarget(createIdentifier('fetchTodos')),
51 ).toBe(false)
52 })
53})