@@ -192,6 +192,7 @@ function maybeHideConstructor(
192192function prunePrivateImports <
193193 T extends ts . InterfaceDeclaration | ts . ClassDeclaration
194194> (
195+ factory : ts . NodeFactory ,
195196 program : ts . Program ,
196197 host : ts . CompilerHost ,
197198 sourceFile : ts . SourceFile ,
@@ -233,13 +234,13 @@ function prunePrivateImports<
233234
234235 if ( exportedTypes . length > 0 ) {
235236 prunedHeritageClauses . push (
236- ts . updateHeritageClause ( heritageClause , exportedTypes )
237+ factory . updateHeritageClause ( heritageClause , exportedTypes )
237238 ) ;
238239 }
239240 }
240241
241242 if ( ts . isClassDeclaration ( node ) ) {
242- return ts . updateClassDeclaration (
243+ return factory . updateClassDeclaration (
243244 node ,
244245 node . decorators ,
245246 node . modifiers ,
@@ -252,7 +253,7 @@ function prunePrivateImports<
252253 ]
253254 ) as T ;
254255 } else if ( ts . isInterfaceDeclaration ( node ) ) {
255- return ts . updateInterfaceDeclaration (
256+ return factory . updateInterfaceDeclaration (
256257 node ,
257258 node . decorators ,
258259 node . modifiers ,
@@ -339,11 +340,23 @@ function extractJSDocComment(
339340 }
340341 } ) ;
341342
342- if ( comments . length > 0 ) {
343+ if ( comments . length > 0 && symbol . declarations ) {
343344 const jsDocTags = ts . getJSDocTags ( symbol . declarations [ overloadCount ] ) ;
344345 const maybeNewline = jsDocTags ?. length > 0 ? '\n' : '' ;
346+ const joinedComments = comments
347+ . map ( comment => {
348+ if ( comment . kind === 'linkText' ) {
349+ return comment . text . trim ( ) ;
350+ }
351+ return comment . text ;
352+ } )
353+ . join ( '' ) ;
354+ const formattedComments = joinedComments
355+ . replace ( '*' , '\n' )
356+ . replace ( ' \n' , '\n' )
357+ . replace ( '\n ' , '\n' ) ;
345358 return ts . factory . createJSDocComment (
346- comments [ 0 ] . text + maybeNewline ,
359+ formattedComments + maybeNewline ,
347360 jsDocTags
348361 ) ;
349362 }
@@ -399,19 +412,21 @@ function extractExportedSymbol(
399412 // See if there is an exported symbol that extends this private symbol.
400413 // In this case, we can safely use the public symbol instead.
401414 for ( const symbol of allExportedSymbols ) {
402- for ( const declaration of symbol . declarations ) {
403- if (
404- ts . isClassDeclaration ( declaration ) ||
405- ts . isInterfaceDeclaration ( declaration )
406- ) {
407- for ( const heritageClause of declaration . heritageClauses || [ ] ) {
408- for ( const type of heritageClause . types || [ ] ) {
409- if ( ts . isIdentifier ( type . expression ) ) {
410- const subclassName = type . expression . escapedText ;
411- if ( subclassName === localSymbolName ) {
412- // TODO: We may need to change this to return a Union type if
413- // more than one public type corresponds to the private type.
414- return symbol ;
415+ if ( symbol . declarations ) {
416+ for ( const declaration of symbol . declarations ) {
417+ if (
418+ ts . isClassDeclaration ( declaration ) ||
419+ ts . isInterfaceDeclaration ( declaration )
420+ ) {
421+ for ( const heritageClause of declaration . heritageClauses || [ ] ) {
422+ for ( const type of heritageClause . types || [ ] ) {
423+ if ( ts . isIdentifier ( type . expression ) ) {
424+ const subclassName = type . expression . escapedText ;
425+ if ( subclassName === localSymbolName ) {
426+ // TODO: We may need to change this to return a Union type if
427+ // more than one public type corresponds to the private type.
428+ return symbol ;
429+ }
415430 }
416431 }
417432 }
@@ -425,8 +440,8 @@ function extractExportedSymbol(
425440 // symbol. Note that this is not always safe as we might replace the local
426441 // symbol with a less restrictive type.
427442 const localSymbol = typeChecker . getSymbolAtLocation ( typeName ) ;
428- if ( localSymbol ) {
429- for ( const declaration of localSymbol ! . declarations ) {
443+ if ( localSymbol ?. declarations ) {
444+ for ( const declaration of localSymbol . declarations ) {
430445 if (
431446 ts . isClassDeclaration ( declaration ) ||
432447 ts . isInterfaceDeclaration ( declaration )
@@ -453,6 +468,7 @@ function dropPrivateApiTransformer(
453468 context : ts . TransformationContext
454469) : ts . Transformer < ts . SourceFile > {
455470 const typeChecker = program . getTypeChecker ( ) ;
471+ const { factory } = context ;
456472
457473 return ( sourceFile : ts . SourceFile ) => {
458474 function visit ( node : ts . Node ) : ts . Node {
@@ -469,7 +485,7 @@ function dropPrivateApiTransformer(
469485 if (
470486 ! node . modifiers ?. find ( m => m . kind === ts . SyntaxKind . ExportKeyword )
471487 ) {
472- return ts . createToken ( ts . SyntaxKind . WhitespaceTrivia ) ;
488+ return factory . createNotEmittedStatement ( node ) ;
473489 }
474490 }
475491
@@ -482,7 +498,7 @@ function dropPrivateApiTransformer(
482498 ) {
483499 // Remove any imports that reference internal APIs, while retaining
484500 // their public members.
485- return prunePrivateImports ( program , host , sourceFile , node ) ;
501+ return prunePrivateImports ( factory , program , host , sourceFile , node ) ;
486502 } else if (
487503 ts . isPropertyDeclaration ( node ) ||
488504 ts . isMethodDeclaration ( node ) ||
@@ -491,7 +507,7 @@ function dropPrivateApiTransformer(
491507 // Remove any class and interface members that are prefixed with
492508 // underscores.
493509 if ( hasPrivatePrefix ( node . name as ts . Identifier ) ) {
494- return ts . createToken ( ts . SyntaxKind . WhitespaceTrivia ) ;
510+ return factory . createNotEmittedStatement ( node ) ;
495511 }
496512 } else if ( ts . isTypeReferenceNode ( node ) ) {
497513 // For public types that refer internal types, find a public type that
@@ -502,9 +518,9 @@ function dropPrivateApiTransformer(
502518 node . typeName
503519 ) ;
504520 return publicName
505- ? ts . updateTypeReferenceNode (
521+ ? factory . updateTypeReferenceNode (
506522 node ,
507- ts . createIdentifier ( publicName . name ) ,
523+ factory . createIdentifier ( publicName . name ) ,
508524 node . typeArguments
509525 )
510526 : node ;
0 commit comments