import type { TSESTree , TSESLint } from '@typescript-eslint/experimental-utils' ; import docsUrl from '../utils/docs-url' ; const DEFAULT_MAX_SWITCH_CASES = 30 ; let maxSwitchCases = DEFAULT_MAX_SWITCH_CASES ; type Options = [ number ] ; const rule = { meta : { messages : { reduceNumberOfNonEmptySwitchCases : 'Reduce the number of non-empty switch cases from {{numSwitchCases}} to at most {{maxSwitchCases}}.' , } , type : 'suggestion' , docs : { description : '"switch" statements should not have too many "case" clauses' , recommended : 'error' , url : docsUrl ( __filename ) , } , schema : [ { type : 'integer' , minimum : 0 , } , ] , } , create ( context ) { if ( context . options . length > 0 ) { maxSwitchCases = context . options [ 0 ] ; } return { SwitchStatement : ( node ) => visitSwitchStatement ( node as TSESTree . SwitchStatement , context ) , } ; } , } ; function visitSwitchStatement ( switchStatement , context ) { const nonEmptyCases = switchStatement . cases . filter ( switchCase => switchCase . consequent . length > 0 && ! isDefaultCase ( switchCase ) ) ; if ( nonEmptyCases . length > maxSwitchCases ) { const switchKeyword = context . getSourceCode ( ) . getFirstToken ( switchStatement ) ! ; context . report ( { messageId : 'reduceNumberOfNonEmptySwitchCases' , loc : switchKeyword . loc , data : { numSwitchCases : nonEmptyCases . length . toString ( ) , maxSwitchCases : maxSwitchCases . toString ( ) , } , } ) ; } } function isDefaultCase ( switchCase ) { return switchCase . test === null ; } export = rule ;