1- import {
2- ESLintUtils ,
3- TSESTree ,
4- ASTUtils ,
5- } from '@typescript-eslint/experimental-utils' ;
6- import {
7- getDocsUrl ,
8- ALL_QUERIES_METHODS ,
9- PRESENCE_MATCHERS ,
10- ABSENCE_MATCHERS ,
11- } from '../utils' ;
1+ import { TSESTree , ASTUtils } from '@typescript-eslint/experimental-utils' ;
2+ import { PRESENCE_MATCHERS , ABSENCE_MATCHERS } from '../utils' ;
123import { findClosestCallNode , isMemberExpression } from '../node-utils' ;
134
5+ import { createTestingLibraryRule } from '../create-testing-library-rule' ;
6+
147export const RULE_NAME = 'prefer-explicit-assert' ;
158export type MessageIds =
169 | 'preferExplicitAssert'
1710 | 'preferExplicitAssertAssertion' ;
1811type Options = [
1912 {
2013 assertion ?: string ;
21- customQueryNames ?: string [ ] ;
2214 }
2315] ;
2416
25- const ALL_GET_BY_QUERIES = ALL_QUERIES_METHODS . map (
26- ( queryMethod ) => `get${ queryMethod } `
27- ) ;
28-
29- const isValidQuery = ( node : TSESTree . Identifier , customQueryNames : string [ ] ) =>
30- ALL_GET_BY_QUERIES . includes ( node . name ) ||
31- customQueryNames . includes ( node . name ) ;
32-
3317const isAtTopLevel = ( node : TSESTree . Node ) =>
3418 node . parent . parent . type === 'ExpressionStatement' ;
3519
36- export default ESLintUtils . RuleCreator ( getDocsUrl ) < Options , MessageIds > ( {
20+ export default createTestingLibraryRule < Options , MessageIds > ( {
3721 name : RULE_NAME ,
3822 meta : {
3923 type : 'suggestion' ,
@@ -59,26 +43,18 @@ export default ESLintUtils.RuleCreator(getDocsUrl)<Options, MessageIds>({
5943 type : 'string' ,
6044 enum : PRESENCE_MATCHERS ,
6145 } ,
62- customQueryNames : {
63- type : 'array' ,
64- } ,
6546 } ,
6647 } ,
6748 ] ,
6849 } ,
69- defaultOptions : [
70- {
71- customQueryNames : [ ] ,
72- } ,
73- ] ,
74-
75- create : function ( context , [ options ] ) {
76- const { customQueryNames, assertion } = options ;
50+ defaultOptions : [ { } ] ,
51+ create ( context , [ options ] , helpers ) {
52+ const { assertion } = options ;
7753 const getQueryCalls : TSESTree . Identifier [ ] = [ ] ;
7854
7955 return {
8056 'CallExpression Identifier' ( node : TSESTree . Identifier ) {
81- if ( isValidQuery ( node , customQueryNames ) ) {
57+ if ( helpers . isGetByQuery ( node ) ) {
8258 getQueryCalls . push ( node ) ;
8359 }
8460 } ,
@@ -93,7 +69,9 @@ export default ESLintUtils.RuleCreator(getDocsUrl)<Options, MessageIds>({
9369 node : queryCall ,
9470 messageId : 'preferExplicitAssert' ,
9571 } ) ;
96- } else if ( assertion ) {
72+ }
73+
74+ if ( assertion ) {
9775 const expectCallNode = findClosestCallNode ( node , 'expect' ) ;
9876 if ( ! expectCallNode ) return ;
9977
0 commit comments