@@ -330,6 +330,8 @@ module ts {
330330 /** Emit Trailing comments of the node */
331331 var emitTrailingComments = compilerOptions . removeComments ? ( node : Node ) => { } : emitTrailingDeclarationComments ;
332332
333+ var emitLeadingCommentsOfPosition = compilerOptions . removeComments ? ( pos : number ) => { } : emitLeadingCommentsOfLocalPosition ;
334+
333335 var detachedCommentsInfo : { nodePos : number ; detachedCommentEndPos : number } [ ] ;
334336 /** Emit detached comments of the node */
335337 var emitDetachedComments = compilerOptions . removeComments ? ( node : TextRange ) => { } : emitDetachedCommentsAtPosition ;
@@ -1390,12 +1392,14 @@ module ts {
13901392 write ( ";" ) ;
13911393 emitTrailingComments ( node . body ) ;
13921394 }
1393- decreaseIndent ( ) ;
13941395 writeLine ( ) ;
13951396 if ( node . body . kind === SyntaxKind . FunctionBlock ) {
1397+ emitLeadingCommentsOfPosition ( ( < Block > node . body ) . statements . end ) ;
1398+ decreaseIndent ( ) ;
13961399 emitToken ( SyntaxKind . CloseBraceToken , ( < Block > node . body ) . statements . end ) ;
13971400 }
13981401 else {
1402+ decreaseIndent ( ) ;
13991403 emitStart ( node . body ) ;
14001404 write ( "}" ) ;
14011405 emitEnd ( node . body ) ;
@@ -1648,8 +1652,11 @@ module ts {
16481652 if ( superCall ) statements = statements . slice ( 1 ) ;
16491653 emitLines ( statements ) ;
16501654 }
1651- decreaseIndent ( ) ;
16521655 writeLine ( ) ;
1656+ if ( ctor ) {
1657+ emitLeadingCommentsOfPosition ( ( < Block > ctor . body ) . statements . end ) ;
1658+ }
1659+ decreaseIndent ( ) ;
16531660 emitToken ( SyntaxKind . CloseBraceToken , ctor ? ( < Block > ctor . body ) . statements . end : node . members . end ) ;
16541661 scopeEmitEnd ( ) ;
16551662 emitEnd ( < Node > ctor || node ) ;
@@ -2077,23 +2084,34 @@ module ts {
20772084 }
20782085 }
20792086
2087+ function hasDetachedComments ( pos : number ) {
2088+ return detachedCommentsInfo !== undefined && detachedCommentsInfo [ detachedCommentsInfo . length - 1 ] . nodePos === pos ;
2089+ }
2090+
2091+ function getLeadingCommentsWithoutDetachedComments ( ) {
2092+ // get the leading comments from detachedPos
2093+ var leadingComments = getLeadingComments ( currentSourceFile . text , detachedCommentsInfo [ detachedCommentsInfo . length - 1 ] . detachedCommentEndPos ) ;
2094+ if ( detachedCommentsInfo . length - 1 ) {
2095+ detachedCommentsInfo . pop ( ) ;
2096+ }
2097+ else {
2098+ detachedCommentsInfo = undefined ;
2099+ }
2100+
2101+ return leadingComments ;
2102+ }
2103+
20802104 function emitLeadingDeclarationComments ( node : Node ) {
20812105 // Emit the leading comments only if the parent's pos doesnt match because parent should take care of emitting these comments
20822106 if ( node . parent . kind === SyntaxKind . SourceFile || node . pos !== node . parent . pos ) {
20832107 var leadingComments : Comment [ ] ;
2084- if ( detachedCommentsInfo === undefined || detachedCommentsInfo [ detachedCommentsInfo . length - 1 ] . nodePos !== node . pos ) {
2085- // get the leading comments from the node
2086- leadingComments = getLeadingCommentsOfNode ( node , currentSourceFile ) ;
2108+ if ( hasDetachedComments ( node . pos ) ) {
2109+ // get comments without detached comments
2110+ leadingComments = getLeadingCommentsWithoutDetachedComments ( ) ;
20872111 }
20882112 else {
2089- // get the leading comments from detachedPos
2090- leadingComments = getLeadingComments ( currentSourceFile . text , detachedCommentsInfo [ detachedCommentsInfo . length - 1 ] . detachedCommentEndPos ) ;
2091- if ( detachedCommentsInfo . length - 1 ) {
2092- detachedCommentsInfo . pop ( ) ;
2093- }
2094- else {
2095- detachedCommentsInfo = undefined ;
2096- }
2113+ // get the leading comments from the node
2114+ leadingComments = getLeadingCommentsOfNode ( node , currentSourceFile ) ;
20972115 }
20982116 emitNewLineBeforeLeadingComments ( node , leadingComments , writer ) ;
20992117 // Leading comments are emitted at /*leading comment1 */space/*leading comment*/space
@@ -2110,6 +2128,21 @@ module ts {
21102128 }
21112129 }
21122130
2131+ function emitLeadingCommentsOfLocalPosition ( pos : number ) {
2132+ var leadingComments : Comment [ ] ;
2133+ if ( hasDetachedComments ( pos ) ) {
2134+ // get comments without detached comments
2135+ leadingComments = getLeadingCommentsWithoutDetachedComments ( ) ;
2136+ }
2137+ else {
2138+ // get the leading comments from the node
2139+ leadingComments = getLeadingComments ( currentSourceFile . text , pos ) ;
2140+ }
2141+ emitNewLineBeforeLeadingComments ( { pos : pos , end : pos } , leadingComments , writer ) ;
2142+ // Leading comments are emitted at /*leading comment1 */space/*leading comment*/space
2143+ emitComments ( leadingComments , /*trailingSeparator*/ true , writer , writeComment ) ;
2144+ }
2145+
21132146 function emitDetachedCommentsAtPosition ( node : TextRange ) {
21142147 var leadingComments = getLeadingComments ( currentSourceFile . text , node . pos ) ;
21152148 if ( leadingComments ) {
0 commit comments