@@ -616,27 +616,27 @@ namespace ts.textChanges {
616
616
} ) ;
617
617
}
618
618
619
- public insertNodeAtClassStart ( sourceFile : SourceFile , cls : ClassLikeDeclaration | InterfaceDeclaration , newElement : ClassElement ) : void {
620
- this . insertNodeAtStartWorker ( sourceFile , cls , newElement ) ;
619
+ public insertMemberAtStart ( sourceFile : SourceFile , node : ClassLikeDeclaration | InterfaceDeclaration | TypeLiteralNode , newElement : ClassElement | PropertySignature | MethodSignature ) : void {
620
+ this . insertNodeAtStartWorker ( sourceFile , node , newElement ) ;
621
621
}
622
622
623
623
public insertNodeAtObjectStart ( sourceFile : SourceFile , obj : ObjectLiteralExpression , newElement : ObjectLiteralElementLike ) : void {
624
624
this . insertNodeAtStartWorker ( sourceFile , obj , newElement ) ;
625
625
}
626
626
627
- private insertNodeAtStartWorker ( sourceFile : SourceFile , cls : ClassLikeDeclaration | InterfaceDeclaration | ObjectLiteralExpression , newElement : ClassElement | ObjectLiteralElementLike ) : void {
628
- const indentation = this . guessIndentationFromExistingMembers ( sourceFile , cls ) ?? this . computeIndentationForNewMember ( sourceFile , cls ) ;
629
- this . insertNodeAt ( sourceFile , getMembersOrProperties ( cls ) . pos , newElement , this . getInsertNodeAtStartInsertOptions ( sourceFile , cls , indentation ) ) ;
627
+ private insertNodeAtStartWorker ( sourceFile : SourceFile , node : ClassLikeDeclaration | InterfaceDeclaration | ObjectLiteralExpression | TypeLiteralNode , newElement : ClassElement | ObjectLiteralElementLike | PropertySignature | MethodSignature ) : void {
628
+ const indentation = this . guessIndentationFromExistingMembers ( sourceFile , node ) ?? this . computeIndentationForNewMember ( sourceFile , node ) ;
629
+ this . insertNodeAt ( sourceFile , getMembersOrProperties ( node ) . pos , newElement , this . getInsertNodeAtStartInsertOptions ( sourceFile , node , indentation ) ) ;
630
630
}
631
631
632
632
/**
633
633
* Tries to guess the indentation from the existing members of a class/interface/object. All members must be on
634
634
* new lines and must share the same indentation.
635
635
*/
636
- private guessIndentationFromExistingMembers ( sourceFile : SourceFile , cls : ClassLikeDeclaration | InterfaceDeclaration | ObjectLiteralExpression ) {
636
+ private guessIndentationFromExistingMembers ( sourceFile : SourceFile , node : ClassLikeDeclaration | InterfaceDeclaration | ObjectLiteralExpression | TypeLiteralNode ) {
637
637
let indentation : number | undefined ;
638
- let lastRange : TextRange = cls ;
639
- for ( const member of getMembersOrProperties ( cls ) ) {
638
+ let lastRange : TextRange = node ;
639
+ for ( const member of getMembersOrProperties ( node ) ) {
640
640
if ( rangeStartPositionsAreOnSameLine ( lastRange , member , sourceFile ) ) {
641
641
// each indented member must be on a new line
642
642
return undefined ;
@@ -655,13 +655,13 @@ namespace ts.textChanges {
655
655
return indentation ;
656
656
}
657
657
658
- private computeIndentationForNewMember ( sourceFile : SourceFile , cls : ClassLikeDeclaration | InterfaceDeclaration | ObjectLiteralExpression ) {
659
- const clsStart = cls . getStart ( sourceFile ) ;
658
+ private computeIndentationForNewMember ( sourceFile : SourceFile , node : ClassLikeDeclaration | InterfaceDeclaration | ObjectLiteralExpression | TypeLiteralNode ) {
659
+ const clsStart = node . getStart ( sourceFile ) ;
660
660
return formatting . SmartIndenter . findFirstNonWhitespaceColumn ( getLineStartPositionForPosition ( clsStart , sourceFile ) , clsStart , sourceFile , this . formatContext . options )
661
661
+ ( this . formatContext . options . indentSize ?? 4 ) ;
662
662
}
663
663
664
- private getInsertNodeAtStartInsertOptions ( sourceFile : SourceFile , cls : ClassLikeDeclaration | InterfaceDeclaration | ObjectLiteralExpression , indentation : number ) : InsertNodeOptions {
664
+ private getInsertNodeAtStartInsertOptions ( sourceFile : SourceFile , node : ClassLikeDeclaration | InterfaceDeclaration | ObjectLiteralExpression | TypeLiteralNode , indentation : number ) : InsertNodeOptions {
665
665
// Rules:
666
666
// - Always insert leading newline.
667
667
// - For object literals:
@@ -672,11 +672,11 @@ namespace ts.textChanges {
672
672
// - Only insert a trailing newline if body is single-line and there are no other insertions for the node.
673
673
// NOTE: This is handled in `finishClassesWithNodesInsertedAtStart`.
674
674
675
- const members = getMembersOrProperties ( cls ) ;
675
+ const members = getMembersOrProperties ( node ) ;
676
676
const isEmpty = members . length === 0 ;
677
- const isFirstInsertion = addToSeen ( this . classesWithNodesInsertedAtStart , getNodeId ( cls ) , { node : cls , sourceFile } ) ;
678
- const insertTrailingComma = isObjectLiteralExpression ( cls ) && ( ! isJsonSourceFile ( sourceFile ) || ! isEmpty ) ;
679
- const insertLeadingComma = isObjectLiteralExpression ( cls ) && isJsonSourceFile ( sourceFile ) && isEmpty && ! isFirstInsertion ;
677
+ const isFirstInsertion = addToSeen ( this . classesWithNodesInsertedAtStart , getNodeId ( node ) , { node, sourceFile } ) ;
678
+ const insertTrailingComma = isObjectLiteralExpression ( node ) && ( ! isJsonSourceFile ( sourceFile ) || ! isEmpty ) ;
679
+ const insertLeadingComma = isObjectLiteralExpression ( node ) && isJsonSourceFile ( sourceFile ) && isEmpty && ! isFirstInsertion ;
680
680
return {
681
681
indentation,
682
682
prefix : ( insertLeadingComma ? "," : "" ) + this . newLineCharacter ,
@@ -980,8 +980,8 @@ namespace ts.textChanges {
980
980
const close = findChildOfKind ( cls , SyntaxKind . CloseBraceToken , sourceFile ) ;
981
981
return [ open ?. end , close ?. end ] ;
982
982
}
983
- function getMembersOrProperties ( cls : ClassLikeDeclaration | InterfaceDeclaration | ObjectLiteralExpression ) : NodeArray < Node > {
984
- return isObjectLiteralExpression ( cls ) ? cls . properties : cls . members ;
983
+ function getMembersOrProperties ( node : ClassLikeDeclaration | InterfaceDeclaration | ObjectLiteralExpression | TypeLiteralNode ) : NodeArray < Node > {
984
+ return isObjectLiteralExpression ( node ) ? node . properties : node . members ;
985
985
}
986
986
987
987
export type ValidateNonFormattedText = ( node : Node , text : string ) => void ;
0 commit comments