import type { TSESLint , TSESTree } from '@typescript-eslint/experimental-utils' ; import { isThrowStatement } from '../utils/nodes' ; import { areEquivalent } from '../utils/equivalence' ; import docsUrl from '../utils/docs-url' ; const rule = { meta : { messages : { uselessCatch : 'Add logic to this catch clause or eliminate it and rethrow the exception automatically.' , } , schema : [ ] , type : 'suggestion' , docs : { description : '"catch" clauses should do more than rethrow' , recommended : 'error' , url : docsUrl ( __filename ) , } , } , create ( context ) { return { CatchClause : ( node ) => visitCatchClause ( node as TSESTree . CatchClause , context ) , } ; } , } ; function visitCatchClause ( catchClause , context ) { const statements = catchClause . body . body ; if ( catchClause . param && statements . length === 1 && onlyRethrows ( statements [ 0 ] , catchClause . param , context . getSourceCode ( ) ) ) { const catchKeyword = context . getSourceCode ( ) . getFirstToken ( catchClause ) ! ; context . report ( { messageId : 'uselessCatch' , loc : catchKeyword . loc , } ) ; } } function onlyRethrows ( statement , catchParam , sourceCode ) { return ( isThrowStatement ( statement ) && catchParam !== null && statement . argument !== null && areEquivalent ( catchParam , statement . argument , sourceCode ) ) ; } export = rule ;