import type { TSESTree , TSESLint } from '@typescript-eslint/experimental-utils' ; import docsUrl from '../utils/docs-url' ; const rule = { meta : { messages : { addMissingElseClause : 'Add the missing "else" clause.' , } , schema : [ ] , type : 'suggestion' , docs : { description : '"if ... else if" constructs should end with "else" clauses' , recommended : false , url : docsUrl ( __filename ) , } , } , create ( context ) { return { IfStatement : ( node ) => { const ifstmt = node as TSESTree . IfStatement ; if ( isElseIf ( ifstmt ) && ! ifstmt . alternate ) { const sourceCode = context . getSourceCode ( ) ; const elseKeyword = sourceCode . getTokenBefore ( node , token => token . type === 'Keyword' && token . value === 'else' ) ; const ifKeyword = sourceCode . getFirstToken ( node , token => token . type === 'Keyword' && token . value === 'if' ) ; context . report ( { messageId : 'addMissingElseClause' , loc : { start : elseKeyword ! . loc . start , end : ifKeyword ! . loc . end , } , } ) ; } } , } ; } , } ; function isElseIf ( node ) { const { parent } = node ; return parent ?. type === 'IfStatement' && parent . alternate === node ; } export = rule ;