fileoverview author James Henry <https://github.com/JamesHenry>
 *  copyright import ts from 'typescript' ; import { getLocFor , getNodeContainer } from './node-utils' ; import { ESTreeComment , LineAndColumnData } from './temp-types-based-on-js-source' ; param { boolean } block param { string } text param { number } start param { number } end param { LineAndColumnData } startLoc param { LineAndColumnData } endLoc returns { Object } private function convertTypeScriptCommentToEsprimaComment ( block , text , start , end , startLoc , endLoc ) { const comment = { type : block ? 'Block' : 'Line' , value : text } ; if ( typeof start === 'number' ) { comment . range = [ start , end ] ; } if ( typeof startLoc === 'object' ) { comment . loc = { start : startLoc , end : endLoc } ; } return comment ; } param { ts . Scanner } triviaScanner param { ts . SourceFile } ast param { string } code returns { ESTreeComment } private function getCommentFromTriviaScanner ( triviaScanner , ast , code ) { const kind = triviaScanner . getToken ( ) ; const isBlock = kind === ts . SyntaxKind . MultiLineCommentTrivia ; const range = { pos : triviaScanner . getTokenPos ( ) , end : triviaScanner . getTextPos ( ) , kind : triviaScanner . getToken ( ) } ; const comment = code . substring ( range . pos , range . end ) ; const text = isBlock ? comment . replace ( /^\/\*/ , '' ) . replace ( /\*\/$/ , '' ) : comment . replace ( /^\/\// , '' ) ; const loc = getLocFor ( range . pos , range . end , ast ) ; const esprimaComment = convertTypeScriptCommentToEsprimaComment ( isBlock , text , range . pos , range . end , loc . start , loc . end ) ; return esprimaComment ; } param { ts . SourceFile } ast param { string } code returns { ESTreeComment [ ] } private export function convertComments ( ast , code ) { const comments = [ ] ; /**
     * Create a TypeScript Scanner, with skipTrivia set to false so that
     * we can parse the comments
     */ const triviaScanner = ts . createScanner ( ast . languageVersion , false , ast . languageVariant , code ) ; let kind = triviaScanner . scan ( ) ; while ( kind !== ts . SyntaxKind . EndOfFileToken ) { const start = triviaScanner . getTokenPos ( ) ; const end = triviaScanner . getTextPos ( ) ; let container = null ; switch ( kind ) { case ts . SyntaxKind . SingleLineCommentTrivia : case ts . SyntaxKind . MultiLineCommentTrivia : { const comment = getCommentFromTriviaScanner ( triviaScanner , ast , code ) ; comments . push ( comment ) ; break ; } case ts . SyntaxKind . GreaterThanToken : container = getNodeContainer ( ast , start , end ) ; if ( container && container . parent && container . parent . kind === ts . SyntaxKind . JsxOpeningElement && container . parent . parent && container . parent . parent . kind === ts . SyntaxKind . JsxElement ) { kind = triviaScanner . reScanJsxToken ( ) ; continue ; } break ; case ts . SyntaxKind . CloseBraceToken : container = getNodeContainer ( ast , start , end ) ; if ( container . kind === ts . SyntaxKind . TemplateMiddle || container . kind === ts . SyntaxKind . TemplateTail ) { kind = triviaScanner . reScanTemplateToken ( ) ; continue ; } break ; case ts . SyntaxKind . SlashToken : case ts . SyntaxKind . SlashEqualsToken : container = getNodeContainer ( ast , start , end ) ; if ( container . kind === ts . SyntaxKind . RegularExpressionLiteral ) { kind = triviaScanner . reScanSlashToken ( ) ; continue ; } break ; default : break ; } kind = triviaScanner . scan ( ) ; } return comments ; }