@@ -4,9 +4,10 @@ import { getLeadingComment, getTrailingComment } from "../helpers/node-helper";
44
55export abstract class ElementNode
66{
7- // #region Properties (5 )
7+ // #region Properties (6 )
88
99 public readonly dependencies : string [ ] = [ ] ;
10+ public readonly indentation : string = "" ;
1011 public readonly leadingComment : string | null ;
1112 public abstract readonly name : string ;
1213 public readonly sourceCode : string ;
@@ -18,20 +19,86 @@ export abstract class ElementNode
1819
1920 constructor ( sourceFile : ts . SourceFile , public readonly node : ts . Node , leadingComment : string | null = null , trailingComment : string | null = null )
2021 {
21- this . sourceCode = ElementNode . getSourceCode ( sourceFile , node . getFullStart ( ) , node . getEnd ( ) ) ;
22+ this . indentation = this . getIndentation ( sourceFile , node ) ;
2223
2324 this . leadingComment = leadingComment ?? getLeadingComment ( node , sourceFile ) ;
2425 this . trailingComment = trailingComment ?? getTrailingComment ( node , sourceFile ) ;
26+
27+ this . sourceCode = node . getText ( sourceFile ) ;
28+ this . sourceCode = this . sourceCode . replace ( leadingComment ?? "" , "" ) ;
29+ this . sourceCode = this . sourceCode . replace ( trailingComment ?? "" , "" ) ;
30+ this . sourceCode = this . indentation + this . sourceCode . trim ( ) ;
2531 }
2632
2733 // #endregion Constructors
2834
29- // #region Private Static Methods (1)
35+ // #region Protected Methods (2)
36+
37+ protected getClosingBraceIndex ( sourceFile : ts . SourceFile , node : ts . ClassDeclaration | ts . InterfaceDeclaration | ts . TypeLiteralNode )
38+ {
39+ const closingBrace = "}" ;
40+ const sourceCode = sourceFile . getText ( ) ;
41+
42+ for ( let i = node . getEnd ( ) ; i > node . getStart ( sourceFile ) ; i -- )
43+ {
44+ if ( sourceCode [ i ] === closingBrace )
45+ {
46+ return i
47+ }
48+ }
49+
50+ throw new Error ( "Closing brace not found" ) ;
51+ }
52+
53+ protected getOpeningBraceIndex ( sourceFile : ts . SourceFile , node : ts . ClassDeclaration | ts . InterfaceDeclaration | ts . TypeLiteralNode )
54+ {
55+ const openingBrace = "{" ;
56+ const sourceCode = sourceFile . getText ( ) ;
57+ let startIndex = node . getStart ( sourceFile ) ;
58+
59+ if ( ts . isClassDeclaration ( node ) )
60+ {
61+ // class could start with a decorator containing curly braces
62+ startIndex = sourceCode . indexOf ( "class " , startIndex ) ;
63+ }
64+
65+ for ( let i = startIndex ; i < node . getEnd ( ) ; i ++ )
66+ {
67+ if ( sourceCode [ i ] === openingBrace )
68+ {
69+ return i
70+ }
71+ }
72+
73+ throw new Error ( "Opening brace not found" ) ;
74+ }
75+
76+ // #endregion Protected Methods
77+
78+ // #region Private Methods (1)
3079
31- private static getSourceCode ( sourceFile : ts . SourceFile , start : number , end : number )
80+ private getIndentation ( sourceFile : ts . SourceFile , node : ts . Node )
3281 {
33- return sourceFile . getFullText ( ) . substring ( start , end ) ;
82+ const space = " " ;
83+ const tab = "\t" ;
84+ const sourceCode = sourceFile . getText ( ) ;
85+ const startIndex = sourceCode . indexOf ( node . getText ( sourceFile ) ) ;
86+ let indentation = "" ;
87+
88+ for ( let i = startIndex - 1 ; i > 0 ; i -- )
89+ {
90+ if ( sourceCode [ i ] === space || sourceCode [ i ] === tab )
91+ {
92+ indentation = sourceCode [ i ] + indentation ;
93+ }
94+ else
95+ {
96+ break ;
97+ }
98+ }
99+
100+ return indentation ;
34101 }
35102
36- // #endregion Private Static Methods
103+ // #endregion Private Methods
37104}
0 commit comments