import type { TSESTree , TSESLint } from '@typescript-eslint/experimental-utils' ; import { isBooleanLiteral , isIfStatement , isConditionalExpression } from '../utils/nodes' ; import docsUrl from '../utils/docs-url' ; const rule = { meta : { messages : { removeUnnecessaryBoolean : 'Remove the unnecessary boolean literal.' , } , schema : [ ] , type : 'suggestion' , docs : { description : 'Boolean literals should not be redundant' , recommended : 'error' , url : docsUrl ( __filename ) , } , } , create ( context ) { return { BinaryExpression ( node ) { const expression = node as TSESTree . BinaryExpression ; if ( expression . operator === '==' || expression . operator === '!=' ) { checkBooleanLiteral ( expression . left ) ; checkBooleanLiteral ( expression . right ) ; } } , LogicalExpression ( node ) { const expression = node as TSESTree . LogicalExpression ; checkBooleanLiteral ( expression . left ) ; if ( expression . operator === '&&' ) { checkBooleanLiteral ( expression . right ) ; } const { parent } = node ; if ( expression . operator === '||' && ( ( isConditionalExpression ( parent ) && parent . test === expression ) || isIfStatement ( parent ) ) ) { checkBooleanLiteral ( expression . right ) ; } } , UnaryExpression ( node ) { const unaryExpression = node as TSESTree . UnaryExpression ; if ( unaryExpression . operator === '!' ) { checkBooleanLiteral ( unaryExpression . argument ) ; } } , } ; function checkBooleanLiteral ( expression ) { if ( isBooleanLiteral ( expression ) ) { context . report ( { messageId : 'removeUnnecessaryBoolean' , node : expression } ) ; } } } , } ; export = rule ;