import { CapturingGroup , Group , Pattern } from "regexpp/ast" ; import { CleanRegexRule , createRuleListener , getDocUrl } from "../rules-util" ; export default { meta : { type : "problem" , docs : { description : "Disallow alternatives without elements." , url : getDocUrl ( "no-empty-alternative" ) , } , } , create ( context ) { return createRuleListener ( ( { visitAST , reportElement } ) => { function checkAlternatives ( node ) { if ( node . alternatives . length >= 2 ) { for ( let i = 0 ; i < node . alternatives . length ; i ++ ) { const alt = node . alternatives [ i ] ; if ( alt . elements . length === 0 ) { context . report ( { message : "No empty alternatives. Use quantifiers instead." , ... reportElement ( node ) , } ) ; return ; } } } } visitAST ( { onGroupEnter : checkAlternatives , onCapturingGroupEnter : checkAlternatives , onPatternEnter : checkAlternatives , } ) ; } ) ; } , } as CleanRegexRule ;