1
- import { AST_NODE_TYPES } from '@typescript-eslint/utils' ;
1
+ import { AST_NODE_TYPES , type TSESTree } from '@typescript-eslint/utils' ;
2
+ import jestFnNames from '../globals.json' ;
2
3
import {
3
4
type ParsedJestFnCall ,
4
5
createRule ,
@@ -31,11 +32,32 @@ export default createRule({
31
32
} ,
32
33
defaultOptions : [ ] ,
33
34
create ( context ) {
35
+ const jestFunctionNames : string [ ] = Object . keys ( jestFnNames ) ;
34
36
const importedJestFunctions : string [ ] = [ ] ;
35
37
const usedJestFunctions : ParsedJestFnCall [ ] = [ ] ;
36
38
37
39
return {
38
- CallExpression ( node ) {
40
+ CallExpression ( node : TSESTree . CallExpression ) {
41
+ if (
42
+ node . type === AST_NODE_TYPES . CallExpression &&
43
+ node . callee . type === AST_NODE_TYPES . Identifier &&
44
+ jestFunctionNames . includes ( node . callee . name )
45
+ ) {
46
+ const jestFnCall : ParsedJestFnCall = {
47
+ head : {
48
+ type : 'import' ,
49
+ node : node . callee ,
50
+ original : node . callee . name ,
51
+ local : node . callee . name ,
52
+ } ,
53
+ name : node . callee . name ,
54
+ type : 'jest' ,
55
+ members : [ ] ,
56
+ } ;
57
+
58
+ usedJestFunctions . push ( jestFnCall ) ;
59
+ }
60
+
39
61
const jestFnCall = parseJestFnCall ( node , context ) ;
40
62
41
63
if ( ! jestFnCall ) {
@@ -48,6 +70,38 @@ export default createRule({
48
70
49
71
usedJestFunctions . push ( jestFnCall ) ;
50
72
} ,
73
+ // if (
74
+ // node.init &&
75
+ // node.init.type === AST_NODE_TYPES.CallExpression &&
76
+ // node.init.callee.type === AST_NODE_TYPES.Identifier &&
77
+ // node.init.callee.name === 'require' &&
78
+ // node.init.arguments[0]?.type === 'Literal' &&
79
+ // node.init.arguments[0]?.value === '@jest/globals'
80
+ // ) {
81
+ // if (node.id.type === AST_NODE_TYPES.Identifier) {
82
+ // importedJestFunctions.push(node.id.name);
83
+ // } else if (node.id.type === AST_NODE_TYPES.ObjectPattern) {
84
+ // node.id.properties.forEach(property => {
85
+ // if (
86
+ // property.type === AST_NODE_TYPES.Property &&
87
+ // property.key.type === AST_NODE_TYPES.Identifier
88
+ // ) {
89
+ // importedJestFunctions.push(property.key.name);
90
+ // }
91
+ // });
92
+ // }
93
+ // }
94
+
95
+ // // if (node.id.name) {
96
+ // // usedJestFunctions.push({
97
+ // // name: node.id.name,
98
+ // // type: 'jest',
99
+ // // // Assign a valid value of type 'ResolvedJestFnWithNode' here
100
+ // // head: { type: 'jest', node: node },
101
+ // // members: [],
102
+ // // });
103
+ // // }
104
+ // },
51
105
'Program:exit' ( ) {
52
106
const jestFunctionsToReport = usedJestFunctions . filter (
53
107
jestFunction => ! importedJestFunctions . includes ( jestFunction . name ) ,
@@ -56,8 +110,8 @@ export default createRule({
56
110
if ( ! jestFunctionsToReport . length ) {
57
111
return ;
58
112
}
59
- const jestFunctionsToImport = jestFunctionsToReport . map (
60
- jestFunction => jestFunction . name ,
113
+ const jestFunctionsToImport = Array . from (
114
+ new Set ( jestFunctionsToReport . map ( jestFunction => jestFunction . name ) ) ,
61
115
) ;
62
116
const reportingNode = jestFunctionsToReport [ 0 ] . head . node ;
63
117
0 commit comments