Skip to content

Commit c3e4754

Browse files
authored
fix: detect class/function type more reliably (#882)
* fix: detect class/function type more reliably * remove unused import
1 parent ba77927 commit c3e4754

4 files changed

Lines changed: 55 additions & 18 deletions

File tree

src/utils/types.ts

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { TSESTree } from '@typescript-eslint/utils'
22
import type ts from 'typescript'
3-
import { require } from './require'
43

54
export enum UtilName {
65
vi = 'vi',
@@ -86,23 +85,8 @@ interface TypeAssertionChain<
8685
}
8786

8887
export function isClassOrFunctionType(type: ts.Type): boolean {
89-
if (type.getCallSignatures().length > 0) return true
90-
91-
const ts = require('typescript')
92-
9388
return (
94-
type
95-
.getSymbol()
96-
?.getDeclarations()
97-
?.some(
98-
(declaration) =>
99-
ts.isArrowFunction(declaration) ||
100-
ts.isClassDeclaration(declaration) ||
101-
ts.isClassExpression(declaration) ||
102-
ts.isFunctionDeclaration(declaration) ||
103-
ts.isFunctionExpression(declaration) ||
104-
ts.isMethodDeclaration(declaration) ||
105-
ts.isFunctionTypeNode(declaration),
106-
) ?? false
89+
type.getCallSignatures().length > 0 ||
90+
type.getConstructSignatures().length > 0
10791
)
10892
}

tests/fixture/myFunction.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,7 @@
11
export function myFunction() {}
2+
3+
type ValidatorFn = () => boolean
4+
export const validatorFunction: ValidatorFn = () => true
5+
6+
class Foo {}
7+
export const foo = new Foo()

tests/prefer-describe-function-title.test.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,18 @@ ruleTester.run(rule.name, rule, {
101101
},
102102
},
103103
},
104+
{
105+
code: `
106+
import { foo } from "./myFunction"
107+
describe("foo", () => {})
108+
`,
109+
filename: 'myFunction.test.ts',
110+
settings: {
111+
vitest: {
112+
typecheck: true,
113+
},
114+
},
115+
},
104116
],
105117
invalid: [
106118
{
@@ -185,5 +197,28 @@ ruleTester.run(rule.name, rule, {
185197
},
186198
},
187199
},
200+
{
201+
code: `
202+
import { validatorFunction } from "./myFunction"
203+
describe("validatorFunction", () => {})
204+
`,
205+
errors: [
206+
{
207+
column: 18,
208+
line: 3,
209+
messageId: 'preferFunction',
210+
},
211+
],
212+
filename: 'myFunction.test.ts',
213+
output: `
214+
import { validatorFunction } from "./myFunction"
215+
describe(validatorFunction, () => {})
216+
`,
217+
settings: {
218+
vitest: {
219+
typecheck: true,
220+
},
221+
},
222+
},
188223
],
189224
})

tests/valid-title.test.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,18 @@ ruleTester.run(rule.name, rule, {
106106
});`,
107107
settings: { vitest: { typecheck: true } },
108108
},
109+
{
110+
code: `
111+
import { validatorFunction } from "./myFunction"
112+
describe(validatorFunction, () => {
113+
test('item', () => {
114+
expect(0).toBe(0)
115+
})
116+
})
117+
`,
118+
filename: 'myFunction.test.ts',
119+
settings: { vitest: { typecheck: true } },
120+
},
109121
],
110122
invalid: [
111123
{

0 commit comments

Comments
 (0)