11import { AST_NODE_TYPES , TSESTree } from '@typescript-eslint/typescript-estree' ;
22import {
33 FunctionExpression ,
4+ JestFunctionCallExpression ,
45 createRule ,
56 isDescribe ,
67 isFunction ,
@@ -25,6 +26,11 @@ const paramsLocation = (
2526 } ;
2627} ;
2728
29+ const isDescribeEach = ( node : JestFunctionCallExpression ) =>
30+ node . callee . type === AST_NODE_TYPES . MemberExpression &&
31+ node . callee . property . type === AST_NODE_TYPES . Identifier &&
32+ node . callee . property . name === 'each' ;
33+
2834export default createRule ( {
2935 name : __filename ,
3036 meta : {
@@ -50,63 +56,64 @@ export default createRule({
5056 create ( context ) {
5157 return {
5258 CallExpression ( node ) {
53- if ( isDescribe ( node ) ) {
54- if ( node . arguments . length === 0 ) {
55- return context . report ( {
56- messageId : 'nameAndCallback' ,
57- loc : node . loc ,
58- } ) ;
59- }
59+ if ( ! isDescribe ( node ) || isDescribeEach ( node ) ) {
60+ return ;
61+ }
62+
63+ if ( node . arguments . length === 0 ) {
64+ return context . report ( {
65+ messageId : 'nameAndCallback' ,
66+ loc : node . loc ,
67+ } ) ;
68+ }
69+ const [ name ] = node . arguments ;
70+ const [ , callbackFunction ] = node . arguments ;
71+ if ( ! isString ( name ) ) {
72+ context . report ( {
73+ messageId : 'firstArgumentMustBeName' ,
74+ loc : paramsLocation ( node . arguments ) ,
75+ } ) ;
76+ }
77+ if ( ! callbackFunction ) {
78+ context . report ( {
79+ messageId : 'nameAndCallback' ,
80+ loc : paramsLocation ( node . arguments ) ,
81+ } ) ;
6082
61- const [ name ] = node . arguments ;
62- const [ , callbackFunction ] = node . arguments ;
63- if ( ! isString ( name ) ) {
83+ return ;
84+ }
85+ if ( isFunction ( callbackFunction ) ) {
86+ if ( isAsync ( callbackFunction ) ) {
6487 context . report ( {
65- messageId : 'firstArgumentMustBeName ' ,
66- loc : paramsLocation ( node . arguments ) ,
88+ messageId : 'noAsyncDescribeCallback ' ,
89+ node : callbackFunction ,
6790 } ) ;
6891 }
69- if ( ! callbackFunction ) {
92+ if ( hasParams ( callbackFunction ) ) {
7093 context . report ( {
71- messageId : 'nameAndCallback ' ,
72- loc : paramsLocation ( node . arguments ) ,
94+ messageId : 'unexpectedDescribeArgument ' ,
95+ loc : paramsLocation ( callbackFunction . params ) ,
7396 } ) ;
74-
75- return ;
7697 }
77- if ( isFunction ( callbackFunction ) ) {
78- if ( isAsync ( callbackFunction ) ) {
79- context . report ( {
80- messageId : 'noAsyncDescribeCallback' ,
81- node : callbackFunction ,
82- } ) ;
83- }
84- if ( hasParams ( callbackFunction ) ) {
85- context . report ( {
86- messageId : 'unexpectedDescribeArgument' ,
87- loc : paramsLocation ( callbackFunction . params ) ,
88- } ) ;
89- }
90- if (
91- callbackFunction . body &&
92- callbackFunction . body . type === AST_NODE_TYPES . BlockStatement
93- ) {
94- callbackFunction . body . body . forEach ( node => {
95- if ( node . type === 'ReturnStatement' ) {
96- context . report ( {
97- messageId : 'unexpectedReturnInDescribe' ,
98- node,
99- } ) ;
100- }
101- } ) ;
102- }
103- } else {
104- context . report ( {
105- messageId : 'secondArgumentMustBeFunction' ,
106- loc : paramsLocation ( node . arguments ) ,
98+ if (
99+ callbackFunction . body &&
100+ callbackFunction . body . type === AST_NODE_TYPES . BlockStatement
101+ ) {
102+ callbackFunction . body . body . forEach ( node => {
103+ if ( node . type === 'ReturnStatement' ) {
104+ context . report ( {
105+ messageId : 'unexpectedReturnInDescribe' ,
106+ node,
107+ } ) ;
108+ }
107109 } ) ;
108- return ;
109110 }
111+ } else {
112+ context . report ( {
113+ messageId : 'secondArgumentMustBeFunction' ,
114+ loc : paramsLocation ( node . arguments ) ,
115+ } ) ;
116+ return ;
110117 }
111118 } ,
112119 } ;
0 commit comments