@@ -493,7 +493,8 @@ namespace ts.textChanges {
493
493
if ( getMembersOrProperties ( cls ) . length === 0 ) {
494
494
if ( addToSeen ( this . classesWithNodesInsertedAtStart , getNodeId ( cls ) , { node : cls , sourceFile } ) ) {
495
495
// For `class C {\n}`, don't add the trailing "\n"
496
- const shouldSuffix = ( positionsAreOnSameLine as any ) ( ...getClassOrObjectBraceEnds ( cls , sourceFile ) , sourceFile ) ; // TODO: GH#4130 remove 'as any'
496
+ const [ open , close ] = getClassOrObjectBraceEnds ( cls , sourceFile ) ;
497
+ const shouldSuffix = open && close && positionsAreOnSameLine ( open , close , sourceFile ) ;
497
498
return { prefix : this . newLineCharacter , suffix : comma + ( shouldSuffix ? this . newLineCharacter : "" ) } ;
498
499
}
499
500
else {
@@ -726,7 +727,7 @@ namespace ts.textChanges {
726
727
this . classesWithNodesInsertedAtStart . forEach ( ( { node, sourceFile } ) => {
727
728
const [ openBraceEnd , closeBraceEnd ] = getClassOrObjectBraceEnds ( node , sourceFile ) ;
728
729
// For `class C { }` remove the whitespace inside the braces.
729
- if ( positionsAreOnSameLine ( openBraceEnd , closeBraceEnd , sourceFile ) && openBraceEnd !== closeBraceEnd - 1 ) {
730
+ if ( openBraceEnd && closeBraceEnd && positionsAreOnSameLine ( openBraceEnd , closeBraceEnd , sourceFile ) && openBraceEnd !== closeBraceEnd - 1 ) {
730
731
this . deleteRange ( sourceFile , createRange ( openBraceEnd , closeBraceEnd - 1 ) ) ;
731
732
}
732
733
} ) ;
@@ -783,8 +784,10 @@ namespace ts.textChanges {
783
784
return skipTrivia ( sourceFile . text , getAdjustedStartPosition ( sourceFile , node , { leadingTriviaOption : LeadingTriviaOption . IncludeAll } ) , /*stopAfterLineBreak*/ false , /*stopAtComments*/ true ) ;
784
785
}
785
786
786
- function getClassOrObjectBraceEnds ( cls : ClassLikeDeclaration | InterfaceDeclaration | ObjectLiteralExpression , sourceFile : SourceFile ) : [ number , number ] {
787
- return [ findChildOfKind ( cls , SyntaxKind . OpenBraceToken , sourceFile ) ! . end , findChildOfKind ( cls , SyntaxKind . CloseBraceToken , sourceFile ) ! . end ] ;
787
+ function getClassOrObjectBraceEnds ( cls : ClassLikeDeclaration | InterfaceDeclaration | ObjectLiteralExpression , sourceFile : SourceFile ) : [ number | undefined , number | undefined ] {
788
+ const open = findChildOfKind ( cls , SyntaxKind . OpenBraceToken , sourceFile ) ;
789
+ const close = findChildOfKind ( cls , SyntaxKind . CloseBraceToken , sourceFile ) ;
790
+ return [ open ?. end , close ?. end ] ;
788
791
}
789
792
function getMembersOrProperties ( cls : ClassLikeDeclaration | InterfaceDeclaration | ObjectLiteralExpression ) : NodeArray < Node > {
790
793
return isObjectLiteralExpression ( cls ) ? cls . properties : cls . members ;
0 commit comments