@@ -192,6 +192,7 @@ function maybeHideConstructor(
192
192
function prunePrivateImports <
193
193
T extends ts . InterfaceDeclaration | ts . ClassDeclaration
194
194
> (
195
+ factory : ts . NodeFactory ,
195
196
program : ts . Program ,
196
197
host : ts . CompilerHost ,
197
198
sourceFile : ts . SourceFile ,
@@ -233,13 +234,13 @@ function prunePrivateImports<
233
234
234
235
if ( exportedTypes . length > 0 ) {
235
236
prunedHeritageClauses . push (
236
- ts . updateHeritageClause ( heritageClause , exportedTypes )
237
+ factory . updateHeritageClause ( heritageClause , exportedTypes )
237
238
) ;
238
239
}
239
240
}
240
241
241
242
if ( ts . isClassDeclaration ( node ) ) {
242
- return ts . updateClassDeclaration (
243
+ return factory . updateClassDeclaration (
243
244
node ,
244
245
node . decorators ,
245
246
node . modifiers ,
@@ -252,7 +253,7 @@ function prunePrivateImports<
252
253
]
253
254
) as T ;
254
255
} else if ( ts . isInterfaceDeclaration ( node ) ) {
255
- return ts . updateInterfaceDeclaration (
256
+ return factory . updateInterfaceDeclaration (
256
257
node ,
257
258
node . decorators ,
258
259
node . modifiers ,
@@ -339,11 +340,23 @@ function extractJSDocComment(
339
340
}
340
341
} ) ;
341
342
342
- if ( comments . length > 0 ) {
343
+ if ( comments . length > 0 && symbol . declarations ) {
343
344
const jsDocTags = ts . getJSDocTags ( symbol . declarations [ overloadCount ] ) ;
344
345
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' ) ;
345
358
return ts . factory . createJSDocComment (
346
- comments [ 0 ] . text + maybeNewline ,
359
+ formattedComments + maybeNewline ,
347
360
jsDocTags
348
361
) ;
349
362
}
@@ -399,19 +412,21 @@ function extractExportedSymbol(
399
412
// See if there is an exported symbol that extends this private symbol.
400
413
// In this case, we can safely use the public symbol instead.
401
414
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
+ }
415
430
}
416
431
}
417
432
}
@@ -425,8 +440,8 @@ function extractExportedSymbol(
425
440
// symbol. Note that this is not always safe as we might replace the local
426
441
// symbol with a less restrictive type.
427
442
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 ) {
430
445
if (
431
446
ts . isClassDeclaration ( declaration ) ||
432
447
ts . isInterfaceDeclaration ( declaration )
@@ -453,6 +468,7 @@ function dropPrivateApiTransformer(
453
468
context : ts . TransformationContext
454
469
) : ts . Transformer < ts . SourceFile > {
455
470
const typeChecker = program . getTypeChecker ( ) ;
471
+ const { factory } = context ;
456
472
457
473
return ( sourceFile : ts . SourceFile ) => {
458
474
function visit ( node : ts . Node ) : ts . Node {
@@ -469,7 +485,7 @@ function dropPrivateApiTransformer(
469
485
if (
470
486
! node . modifiers ?. find ( m => m . kind === ts . SyntaxKind . ExportKeyword )
471
487
) {
472
- return ts . createToken ( ts . SyntaxKind . WhitespaceTrivia ) ;
488
+ return factory . createNotEmittedStatement ( node ) ;
473
489
}
474
490
}
475
491
@@ -482,7 +498,7 @@ function dropPrivateApiTransformer(
482
498
) {
483
499
// Remove any imports that reference internal APIs, while retaining
484
500
// their public members.
485
- return prunePrivateImports ( program , host , sourceFile , node ) ;
501
+ return prunePrivateImports ( factory , program , host , sourceFile , node ) ;
486
502
} else if (
487
503
ts . isPropertyDeclaration ( node ) ||
488
504
ts . isMethodDeclaration ( node ) ||
@@ -491,7 +507,7 @@ function dropPrivateApiTransformer(
491
507
// Remove any class and interface members that are prefixed with
492
508
// underscores.
493
509
if ( hasPrivatePrefix ( node . name as ts . Identifier ) ) {
494
- return ts . createToken ( ts . SyntaxKind . WhitespaceTrivia ) ;
510
+ return factory . createNotEmittedStatement ( node ) ;
495
511
}
496
512
} else if ( ts . isTypeReferenceNode ( node ) ) {
497
513
// For public types that refer internal types, find a public type that
@@ -502,9 +518,9 @@ function dropPrivateApiTransformer(
502
518
node . typeName
503
519
) ;
504
520
return publicName
505
- ? ts . updateTypeReferenceNode (
521
+ ? factory . updateTypeReferenceNode (
506
522
node ,
507
- ts . createIdentifier ( publicName . name ) ,
523
+ factory . createIdentifier ( publicName . name ) ,
508
524
node . typeArguments
509
525
)
510
526
: node ;
0 commit comments