import { zip } from './array' ; import { createLogger } from './logger' ; import { unixEOL } from './string-formatter' ; const logger = createLogger ( 'documentation-block' ) ; experimental param strings param args returns export function documentationBlock ( strings , ... args ) { const firstLineTrimmed = removeLeadingEmptyLines ( strings . raw [ 0 ] ) ; const textIndentation = getIndentation ( firstLineTrimmed ) ; const argsWithIndentation = addIndentationToArguments ( args , textIndentation ) ; let content = zip ( [ firstLineTrimmed , ... strings . raw . slice ( 1 ) ] , argsWithIndentation ) . join ( '' ) ; if ( ! content . match ( /\w/ ) ) { return '' ; } content = maskProblematicCharacters ( content ) ; let lines = content . split ( unixEOL ) ; lines = adjustIndentation ( lines , textIndentation ) ; content = lines . join ( `${ unixEOL } * ` ) ; const result = [ '/**' , ` * ${ content }` , ' */' ] . join ( unixEOL ) ; return result ; } function removeLeadingEmptyLines ( firstLine ) { const lines = firstLine . split ( unixEOL ) ; const indexFirstNonEmpty = lines . findIndex ( str => str . match ( /\w/ ) ) || 0 ; return lines . splice ( indexFirstNonEmpty ) . join ( unixEOL ) ; } function addIndentationToArguments ( args , textIndentation ) { const argsWithIndentation = args . map ( arg => arg . replace ( /\n/g , unixEOL + ' ' . repeat ( textIndentation ) ) ) ; return argsWithIndentation ; } function adjustIndentation ( lines , textIndentation ) { return lines . map ( str => str . slice ( textIndentation ) ) ; } function getIndentation ( firstLine ) { const removeStarting = firstLine ?. replace ( /^\n*/g , '' ) ; const countEmptySpaces = removeStarting ?. search ( /\S/ ) ; return countEmptySpaces > 0 ? countEmptySpaces : 0 ; } function maskProblematicCharacters ( str ) { if ( str . includes ( '*/' ) ) { logger . warn ( `The documentation block ${ str }'
      )} contained */ in the text will be masked as \\*\\/.` ) ; } return str . replace ( /\*\//g , '\\*\\/' ) ; }