11// Copyright 2024 The Chromium Authors. All rights reserved.
22// Use of this source code is governed by a BSD-style license that can be
33// found in the LICENSE file.
4- 'use strict' ;
54
6- /**
7- * @fileoverview Prefer `assert.instanceOf` to assert that a value is an instance
8- * of a class.
9- */
10-
11- // ------------------------------------------------------------------------------
12- // Rule Definition
13- // ------------------------------------------------------------------------------
5+ import { createRule } from './tsUtils.ts' ;
146
157const FALSY_ASSERTIONS = new Set ( [ 'isFalse' , 'isNotOk' , 'isNotTrue' , 'notOk' ] ) ;
168const TRUTHY_ASSERTIONS = new Set ( [ 'isNotFalse' , 'isOk' , 'isTrue' , 'ok' ] ) ;
179
18- /** @type { import('eslint').Rule.RuleModule } */
19- module . exports = {
10+ export default createRule ( {
11+ name : 'prefer-assert-instance-of' ,
2012 meta : {
2113 type : 'suggestion' ,
2214
@@ -29,32 +21,27 @@ module.exports = {
2921 useAssertNotInstanceOf : 'Use `assert.notInstanceOf` to assert that a value is not an instance of a class' ,
3022 } ,
3123 fixable : 'code' ,
32- schema : [ ] , // no options
24+ schema : [ ] , // no options
3325 } ,
34- create : function ( context ) {
26+ defaultOptions : [ ] ,
27+ create : function ( context ) {
3528 function isTruthyAssertion ( calleeNode ) {
36- if ( calleeNode . type === 'Identifier' &&
37- calleeNode . name === 'assert' ) {
29+ if ( calleeNode . type === 'Identifier' && calleeNode . name === 'assert' ) {
3830 return true ;
3931 }
40- return calleeNode . type === 'MemberExpression' &&
41- calleeNode . object . type === 'Identifier' &&
42- calleeNode . object . name === 'assert' &&
43- calleeNode . property . type === 'Identifier' &&
44- TRUTHY_ASSERTIONS . has ( calleeNode . property . name ) ;
32+ return calleeNode . type === 'MemberExpression' && calleeNode . object . type === 'Identifier' &&
33+ calleeNode . object . name === 'assert' && calleeNode . property . type === 'Identifier' &&
34+ TRUTHY_ASSERTIONS . has ( calleeNode . property . name ) ;
4535 }
4636
4737 function isFalsyAssertion ( calleeNode ) {
48- return calleeNode . type === 'MemberExpression' &&
49- calleeNode . object . type === 'Identifier' &&
50- calleeNode . object . name === 'assert' &&
51- calleeNode . property . type === 'Identifier' &&
52- FALSY_ASSERTIONS . has ( calleeNode . property . name ) ;
38+ return calleeNode . type === 'MemberExpression' && calleeNode . object . type === 'Identifier' &&
39+ calleeNode . object . name === 'assert' && calleeNode . property . type === 'Identifier' &&
40+ FALSY_ASSERTIONS . has ( calleeNode . property . name ) ;
5341 }
5442
5543 function isInstanceofExpression ( argumentNode ) {
56- return argumentNode . type === 'BinaryExpression' &&
57- argumentNode . operator === 'instanceof' ;
44+ return argumentNode . type === 'BinaryExpression' && argumentNode . operator === 'instanceof' ;
5845 }
5946
6047 function reportError ( node , calleeText , messageId ) {
@@ -86,4 +73,4 @@ module.exports = {
8673 }
8774 } ;
8875 } ,
89- } ;
76+ } ) ;
0 commit comments