22// Use of this source code is governed by a BSD-style license that can be
33// found in the LICENSE file.
44
5+ import type { TSESTree } from '@typescript-eslint/utils' ;
6+
57import { createRule } from './utils/ruleCreator.ts' ;
68
79const FALSY_ASSERTIONS = new Set ( [ 'isFalse' , 'isNotOk' , 'isNotTrue' , 'notOk' ] ) ;
810const TRUTHY_ASSERTIONS = new Set ( [ 'isNotFalse' , 'isOk' , 'isTrue' , 'ok' ] ) ;
911
10- export default createRule ( {
12+ type MessageIds =
13+ 'useAssertIsOk' | 'useAssertIsOkInsteadOfNegation' | 'useAssertIsNotOk' | 'useAssertIsNotOkInsteadOfNegation' ;
14+
15+ function isAssertOk ( node : TSESTree . Expression ) {
16+ return node . type === 'MemberExpression' && node . object . type === 'Identifier' && node . object . name === 'assert' &&
17+ node . property . type === 'Identifier' && node . property . name === 'ok' ;
18+ }
19+
20+ function isAssertNotOk ( node : TSESTree . Expression ) {
21+ return node . type === 'MemberExpression' && node . object . type === 'Identifier' && node . object . name === 'assert' &&
22+ node . property . type === 'Identifier' && node . property . name === 'notOk' ;
23+ }
24+
25+ function isTruthyAssertion ( node : TSESTree . Expression ) {
26+ if ( node . type === 'Identifier' && node . name === 'assert' ) {
27+ return true ;
28+ }
29+ return node . type === 'MemberExpression' && node . object . type === 'Identifier' && node . object . name === 'assert' &&
30+ node . property . type === 'Identifier' && TRUTHY_ASSERTIONS . has ( node . property . name ) ;
31+ }
32+
33+ function isFalsyAssertion ( node : TSESTree . Expression ) {
34+ return node . type === 'MemberExpression' && node . object . type === 'Identifier' && node . object . name === 'assert' &&
35+ node . property . type === 'Identifier' && FALSY_ASSERTIONS . has ( node . property . name ) ;
36+ }
37+
38+ export default createRule < unknown [ ] , MessageIds > ( {
1139 name : 'prefer-assert-is-ok' ,
1240 meta : {
1341 type : 'suggestion' ,
@@ -26,34 +54,12 @@ export default createRule({
2654 } ,
2755 defaultOptions : [ ] ,
2856 create : function ( context ) {
29- function isAssertOk ( calleeNode ) {
30- return calleeNode . type === 'MemberExpression' && calleeNode . object . type === 'Identifier' &&
31- calleeNode . object . name === 'assert' && calleeNode . property . type === 'Identifier' &&
32- calleeNode . property . name === 'ok' ;
33- }
34-
35- function isAssertNotOk ( calleeNode ) {
36- return calleeNode . type === 'MemberExpression' && calleeNode . object . type === 'Identifier' &&
37- calleeNode . object . name === 'assert' && calleeNode . property . type === 'Identifier' &&
38- calleeNode . property . name === 'notOk' ;
39- }
40-
41- function isTruthyAssertion ( calleeNode ) {
42- if ( calleeNode . type === 'Identifier' && calleeNode . name === 'assert' ) {
43- return true ;
44- }
45- return calleeNode . type === 'MemberExpression' && calleeNode . object . type === 'Identifier' &&
46- calleeNode . object . name === 'assert' && calleeNode . property . type === 'Identifier' &&
47- TRUTHY_ASSERTIONS . has ( calleeNode . property . name ) ;
48- }
49-
50- function isFalsyAssertion ( calleeNode ) {
51- return calleeNode . type === 'MemberExpression' && calleeNode . object . type === 'Identifier' &&
52- calleeNode . object . name === 'assert' && calleeNode . property . type === 'Identifier' &&
53- FALSY_ASSERTIONS . has ( calleeNode . property . name ) ;
54- }
55-
56- function reportError ( node , calleeText , firstArgNode , messageId ) {
57+ function reportError (
58+ node : TSESTree . CallExpression ,
59+ calleeText : string ,
60+ firstArgNode : TSESTree . CallExpressionArgument ,
61+ messageId : MessageIds ,
62+ ) {
5763 context . report ( {
5864 node,
5965 messageId,
@@ -69,26 +75,29 @@ export default createRule({
6975
7076 return {
7177 CallExpression ( node ) {
72- if ( node . arguments . length >= 1 ) {
73- const [ argumentNode ] = node . arguments ;
74- if ( argumentNode . type === 'UnaryExpression' && argumentNode . operator === '!' ) {
75- if ( isTruthyAssertion ( node . callee ) ) {
76- reportError ( node , 'assert.isNotOk' , argumentNode . argument , 'useAssertIsNotOkInsteadOfNegation' ) ;
77- return ;
78- }
79- if ( isFalsyAssertion ( node . callee ) ) {
80- reportError ( node , 'assert.isOk' , argumentNode . argument , 'useAssertIsOkInsteadOfNegation' ) ;
81- return ;
82- }
83- }
78+ if ( node . arguments . length < 1 ) {
79+ return ;
80+ }
8481
85- if ( isAssertOk ( node . callee ) ) {
86- reportError ( node , 'assert.isOk' , argumentNode , 'useAssertIsOk' ) ;
87- } else if ( isAssertNotOk ( node . callee ) ) {
88- reportError ( node , 'assert.isNotOk' , argumentNode , 'useAssertIsNotOk' ) ;
82+ const [ argumentNode ] = node . arguments ;
83+ if ( argumentNode . type === 'UnaryExpression' && argumentNode . operator === '!' ) {
84+ if ( isTruthyAssertion ( node . callee ) ) {
85+ reportError ( node , 'assert.isNotOk' , argumentNode . argument , 'useAssertIsNotOkInsteadOfNegation' ) ;
86+ return ;
87+ }
88+ if ( isFalsyAssertion ( node . callee ) ) {
89+ reportError ( node , 'assert.isOk' , argumentNode . argument , 'useAssertIsOkInsteadOfNegation' ) ;
90+ return ;
8991 }
9092 }
93+
94+ if ( isAssertOk ( node . callee ) ) {
95+ reportError ( node , 'assert.isOk' , argumentNode , 'useAssertIsOk' ) ;
96+ } else if ( isAssertNotOk ( node . callee ) ) {
97+ reportError ( node , 'assert.isNotOk' , argumentNode , 'useAssertIsNotOk' ) ;
98+ }
9199 }
100+
92101 } ;
93102 } ,
94103} ) ;
0 commit comments