import { TokenTypes } from './types' ; const INLINE_MAX_LENGTH = 50 ; /**
 * Bookkeeper for inline blocks.
 *
 * Inline blocks are parenthesized expressions that are shorter than INLINE_MAX_LENGTH.
 * These blocks are formatted on a single line, unlike longer parenthesized
 * expressions where open-parenthesis causes newline and increase of indentation.
 */ export default class InlineBlock { private level = 0 ; param { Object [ ] } tokens param { Number } index beginIfPossible ( tokens , index ) { if ( this . level === 0 && this . isInlineBlock ( tokens , index ) ) { this . level = 1 ; } else if ( this . level > 0 ) { this . level ++ ; } else { this . level = 0 ; } } /**
     * Finishes current inline block.
     * There might be several nested ones.
     */ end ( ) { this . level -- ; } return { Boolean } isActive ( ) { return this . level > 0 ; } isInlineBlock ( tokens , index ) { let length = 0 ; let level = 0 ; for ( let i = index ; i < tokens . length ; i ++ ) { const token = tokens [ i ] ; length += token . value . length ; if ( length > INLINE_MAX_LENGTH ) { return false ; } if ( token . type === TokenTypes . OPEN_PAREN ) { level ++ ; } else if ( token . type === TokenTypes . CLOSE_PAREN ) { level -- ; if ( level === 0 ) { return true ; } } if ( this . isForbiddenToken ( token ) ) { return false ; } } return false ; } isForbiddenToken ( { type , value } ) { return ( type === TokenTypes . RESERVED_TOP_LEVEL || type === TokenTypes . RESERVED_NEWLINE || type === TokenTypes . LINE_COMMENT || type === TokenTypes . BLOCK_COMMENT || value === ';' ) ; } }