import type { TSESTree , TSESLint } from '@typescript-eslint/experimental-utils' ; import { isIfStatement } from '../utils/nodes' ; import { areEquivalent } from '../utils/equivalence' ; import { collectIfBranches , collectSwitchBranches } from '../utils/conditions' ; import docsUrl from '../utils/docs-url' ; const rule = { meta : { messages : { removeOrEditConditionalStructure : "Remove this conditional structure or edit its code blocks so that they're not all the same." , returnsTheSameValue : 'This conditional operation returns the same value whether the condition is "true" or "false".' , } , schema : [ ] , type : 'problem' , docs : { description : 'All branches in a conditional structure should not have exactly the same implementation' , recommended : 'error' , url : docsUrl ( __filename ) , } , } , create ( context ) { return { IfStatement ( node ) { const ifStmt = node as TSESTree . IfStatement ; if ( ! isIfStatement ( node . parent ) ) { const { branches , endsWithElse } = collectIfBranches ( ifStmt ) ; if ( endsWithElse && allDuplicated ( branches ) ) { context . report ( { messageId : 'removeOrEditConditionalStructure' , node : ifStmt } ) ; } } } , SwitchStatement ( node ) { const switchStmt = node as TSESTree . SwitchStatement ; const { branches , endsWithDefault } = collectSwitchBranches ( switchStmt ) ; if ( endsWithDefault && allDuplicated ( branches ) ) { context . report ( { messageId : 'removeOrEditConditionalStructure' , node : switchStmt } ) ; } } , ConditionalExpression ( node ) { const conditional = node as TSESTree . ConditionalExpression ; const branches = [ conditional . consequent , conditional . alternate ] ; if ( allDuplicated ( branches ) ) { context . report ( { messageId : 'returnsTheSameValue' , node : conditional } ) ; } } , } ; function allDuplicated ( branches ) { return ( branches . length > 1 && branches . slice ( 1 ) . every ( ( branch , index ) => { return areEquivalent ( branch , branches [ index ] , context . getSourceCode ( ) ) ; } ) ) ; } } , } ; export = rule ;