@@ -472,6 +472,8 @@ export function StatementedNode<T extends Constructor<StatementedNodeExtensionTy
472
472
insertStatements ( index : number , writerFunction : WriterFunction ) : Statement [ ] ;
473
473
insertStatements ( index : number , textOrWriterFunction : string | WriterFunction ) : Statement [ ] ;
474
474
insertStatements ( index : number , textOrWriterFunction : string | WriterFunction ) {
475
+ addBodyIfNotExists ( this ) ;
476
+
475
477
return getChildSyntaxList . call ( this ) . insertChildText ( index , textOrWriterFunction ) ;
476
478
477
479
function getChildSyntaxList ( this : Node ) {
@@ -625,7 +627,6 @@ export function StatementedNode<T extends Constructor<StatementedNodeExtensionTy
625
627
}
626
628
627
629
getFunctions ( ) : FunctionDeclaration [ ] {
628
- // todo: remove type assertion
629
630
return ( this . getChildSyntaxListOrThrow ( ) . getChildrenOfKind ( SyntaxKind . FunctionDeclaration ) )
630
631
. filter ( f => f . isAmbient ( ) || f . isImplementation ( ) ) ;
631
632
}
@@ -877,8 +878,12 @@ export function StatementedNode<T extends Constructor<StatementedNodeExtensionTy
877
878
// need to get the inner-most body for namespaces
878
879
return ( this . getInnerBody ( ) . compilerNode as ts . Block ) . statements ;
879
880
}
880
- else if ( TypeGuards . isBodyableNode ( this ) )
881
- return ( this . getBodyOrThrow ( ) . compilerNode as any ) . statements as ts . NodeArray < ts . Statement > ;
881
+ else if ( TypeGuards . isBodyableNode ( this ) ) {
882
+ const body = this . getBody ( ) ;
883
+ if ( body == null )
884
+ return [ ] as any as ts . NodeArray < ts . Statement > ;
885
+ return ( body . compilerNode as any ) . statements as ts . NodeArray < ts . Statement > ;
886
+ }
882
887
else if ( TypeGuards . isBodiedNode ( this ) )
883
888
return ( this . getBody ( ) . compilerNode as any ) . statements as ts . NodeArray < ts . Statement > ;
884
889
else if ( TypeGuards . isBlock ( this ) )
@@ -888,6 +893,8 @@ export function StatementedNode<T extends Constructor<StatementedNodeExtensionTy
888
893
}
889
894
890
895
_insertChildren < TNode extends Node , TStructure > ( opts : InsertChildrenOptions < TStructure > ) {
896
+ addBodyIfNotExists ( this ) ;
897
+
891
898
return insertIntoBracesOrSourceFileWithGetChildren < TNode , TStructure > ( {
892
899
expectedKind : opts . expectedKind ,
893
900
getIndexedChildren : ( ) => this . getStatements ( ) ,
@@ -918,3 +925,8 @@ export function StatementedNode<T extends Constructor<StatementedNodeExtensionTy
918
925
}
919
926
} ;
920
927
}
928
+
929
+ function addBodyIfNotExists ( node : Node ) {
930
+ if ( TypeGuards . isBodyableNode ( node ) && ! node . hasBody ( ) )
931
+ node . addBody ( ) ;
932
+ }
0 commit comments