@@ -618,27 +618,27 @@ namespace ts.textChanges {
618618 } ) ;
619619 }
620620
621- public insertNodeAtClassStart ( sourceFile : SourceFile , cls : ClassLikeDeclaration | InterfaceDeclaration , newElement : ClassElement ) : void {
622- this . insertNodeAtStartWorker ( sourceFile , cls , newElement ) ;
621+ public insertMemberAtStart ( sourceFile : SourceFile , node : ClassLikeDeclaration | InterfaceDeclaration | TypeLiteralNode , newElement : ClassElement | PropertySignature | MethodSignature ) : void {
622+ this . insertNodeAtStartWorker ( sourceFile , node , newElement ) ;
623623 }
624624
625625 public insertNodeAtObjectStart ( sourceFile : SourceFile , obj : ObjectLiteralExpression , newElement : ObjectLiteralElementLike ) : void {
626626 this . insertNodeAtStartWorker ( sourceFile , obj , newElement ) ;
627627 }
628628
629- private insertNodeAtStartWorker ( sourceFile : SourceFile , cls : ClassLikeDeclaration | InterfaceDeclaration | ObjectLiteralExpression , newElement : ClassElement | ObjectLiteralElementLike ) : void {
630- const indentation = this . guessIndentationFromExistingMembers ( sourceFile , cls ) ?? this . computeIndentationForNewMember ( sourceFile , cls ) ;
631- this . insertNodeAt ( sourceFile , getMembersOrProperties ( cls ) . pos , newElement , this . getInsertNodeAtStartInsertOptions ( sourceFile , cls , indentation ) ) ;
629+ private insertNodeAtStartWorker ( sourceFile : SourceFile , node : ClassLikeDeclaration | InterfaceDeclaration | ObjectLiteralExpression | TypeLiteralNode , newElement : ClassElement | ObjectLiteralElementLike | PropertySignature | MethodSignature ) : void {
630+ const indentation = this . guessIndentationFromExistingMembers ( sourceFile , node ) ?? this . computeIndentationForNewMember ( sourceFile , node ) ;
631+ this . insertNodeAt ( sourceFile , getMembersOrProperties ( node ) . pos , newElement , this . getInsertNodeAtStartInsertOptions ( sourceFile , node , indentation ) ) ;
632632 }
633633
634634 /**
635635 * Tries to guess the indentation from the existing members of a class/interface/object. All members must be on
636636 * new lines and must share the same indentation.
637637 */
638- private guessIndentationFromExistingMembers ( sourceFile : SourceFile , cls : ClassLikeDeclaration | InterfaceDeclaration | ObjectLiteralExpression ) {
638+ private guessIndentationFromExistingMembers ( sourceFile : SourceFile , node : ClassLikeDeclaration | InterfaceDeclaration | ObjectLiteralExpression | TypeLiteralNode ) {
639639 let indentation : number | undefined ;
640- let lastRange : TextRange = cls ;
641- for ( const member of getMembersOrProperties ( cls ) ) {
640+ let lastRange : TextRange = node ;
641+ for ( const member of getMembersOrProperties ( node ) ) {
642642 if ( rangeStartPositionsAreOnSameLine ( lastRange , member , sourceFile ) ) {
643643 // each indented member must be on a new line
644644 return undefined ;
@@ -657,13 +657,13 @@ namespace ts.textChanges {
657657 return indentation ;
658658 }
659659
660- private computeIndentationForNewMember ( sourceFile : SourceFile , cls : ClassLikeDeclaration | InterfaceDeclaration | ObjectLiteralExpression ) {
661- const clsStart = cls . getStart ( sourceFile ) ;
662- return formatting . SmartIndenter . findFirstNonWhitespaceColumn ( getLineStartPositionForPosition ( clsStart , sourceFile ) , clsStart , sourceFile , this . formatContext . options )
660+ private computeIndentationForNewMember ( sourceFile : SourceFile , node : ClassLikeDeclaration | InterfaceDeclaration | ObjectLiteralExpression | TypeLiteralNode ) {
661+ const nodeStart = node . getStart ( sourceFile ) ;
662+ return formatting . SmartIndenter . findFirstNonWhitespaceColumn ( getLineStartPositionForPosition ( nodeStart , sourceFile ) , nodeStart , sourceFile , this . formatContext . options )
663663 + ( this . formatContext . options . indentSize ?? 4 ) ;
664664 }
665665
666- private getInsertNodeAtStartInsertOptions ( sourceFile : SourceFile , cls : ClassLikeDeclaration | InterfaceDeclaration | ObjectLiteralExpression , indentation : number ) : InsertNodeOptions {
666+ private getInsertNodeAtStartInsertOptions ( sourceFile : SourceFile , node : ClassLikeDeclaration | InterfaceDeclaration | ObjectLiteralExpression | TypeLiteralNode , indentation : number ) : InsertNodeOptions {
667667 // Rules:
668668 // - Always insert leading newline.
669669 // - For object literals:
@@ -674,11 +674,11 @@ namespace ts.textChanges {
674674 // - Only insert a trailing newline if body is single-line and there are no other insertions for the node.
675675 // NOTE: This is handled in `finishClassesWithNodesInsertedAtStart`.
676676
677- const members = getMembersOrProperties ( cls ) ;
677+ const members = getMembersOrProperties ( node ) ;
678678 const isEmpty = members . length === 0 ;
679- const isFirstInsertion = addToSeen ( this . classesWithNodesInsertedAtStart , getNodeId ( cls ) , { node : cls , sourceFile } ) ;
680- const insertTrailingComma = isObjectLiteralExpression ( cls ) && ( ! isJsonSourceFile ( sourceFile ) || ! isEmpty ) ;
681- const insertLeadingComma = isObjectLiteralExpression ( cls ) && isJsonSourceFile ( sourceFile ) && isEmpty && ! isFirstInsertion ;
679+ const isFirstInsertion = addToSeen ( this . classesWithNodesInsertedAtStart , getNodeId ( node ) , { node, sourceFile } ) ;
680+ const insertTrailingComma = isObjectLiteralExpression ( node ) && ( ! isJsonSourceFile ( sourceFile ) || ! isEmpty ) ;
681+ const insertLeadingComma = isObjectLiteralExpression ( node ) && isJsonSourceFile ( sourceFile ) && isEmpty && ! isFirstInsertion ;
682682 return {
683683 indentation,
684684 prefix : ( insertLeadingComma ? "," : "" ) + this . newLineCharacter ,
@@ -998,8 +998,8 @@ namespace ts.textChanges {
998998 const close = findChildOfKind ( cls , SyntaxKind . CloseBraceToken , sourceFile ) ;
999999 return [ open ?. end , close ?. end ] ;
10001000 }
1001- function getMembersOrProperties ( cls : ClassLikeDeclaration | InterfaceDeclaration | ObjectLiteralExpression ) : NodeArray < Node > {
1002- return isObjectLiteralExpression ( cls ) ? cls . properties : cls . members ;
1001+ function getMembersOrProperties ( node : ClassLikeDeclaration | InterfaceDeclaration | ObjectLiteralExpression | TypeLiteralNode ) : NodeArray < Node > {
1002+ return isObjectLiteralExpression ( node ) ? node . properties : node . members ;
10031003 }
10041004
10051005 export type ValidateNonFormattedText = ( node : Node , text : string ) => void ;
0 commit comments