@@ -618,27 +618,27 @@ namespace ts.textChanges {
618
618
} ) ;
619
619
}
620
620
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 ) ;
623
623
}
624
624
625
625
public insertNodeAtObjectStart ( sourceFile : SourceFile , obj : ObjectLiteralExpression , newElement : ObjectLiteralElementLike ) : void {
626
626
this . insertNodeAtStartWorker ( sourceFile , obj , newElement ) ;
627
627
}
628
628
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 ) ) ;
632
632
}
633
633
634
634
/**
635
635
* Tries to guess the indentation from the existing members of a class/interface/object. All members must be on
636
636
* new lines and must share the same indentation.
637
637
*/
638
- private guessIndentationFromExistingMembers ( sourceFile : SourceFile , cls : ClassLikeDeclaration | InterfaceDeclaration | ObjectLiteralExpression ) {
638
+ private guessIndentationFromExistingMembers ( sourceFile : SourceFile , node : ClassLikeDeclaration | InterfaceDeclaration | ObjectLiteralExpression | TypeLiteralNode ) {
639
639
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 ) ) {
642
642
if ( rangeStartPositionsAreOnSameLine ( lastRange , member , sourceFile ) ) {
643
643
// each indented member must be on a new line
644
644
return undefined ;
@@ -657,13 +657,13 @@ namespace ts.textChanges {
657
657
return indentation ;
658
658
}
659
659
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 )
663
663
+ ( this . formatContext . options . indentSize ?? 4 ) ;
664
664
}
665
665
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 {
667
667
// Rules:
668
668
// - Always insert leading newline.
669
669
// - For object literals:
@@ -674,11 +674,11 @@ namespace ts.textChanges {
674
674
// - Only insert a trailing newline if body is single-line and there are no other insertions for the node.
675
675
// NOTE: This is handled in `finishClassesWithNodesInsertedAtStart`.
676
676
677
- const members = getMembersOrProperties ( cls ) ;
677
+ const members = getMembersOrProperties ( node ) ;
678
678
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 ;
682
682
return {
683
683
indentation,
684
684
prefix : ( insertLeadingComma ? "," : "" ) + this . newLineCharacter ,
@@ -998,8 +998,8 @@ namespace ts.textChanges {
998
998
const close = findChildOfKind ( cls , SyntaxKind . CloseBraceToken , sourceFile ) ;
999
999
return [ open ?. end , close ?. end ] ;
1000
1000
}
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 ;
1003
1003
}
1004
1004
1005
1005
export type ValidateNonFormattedText = ( node : Node , text : string ) => void ;
0 commit comments