@@ -24,7 +24,6 @@ import (
2424
2525 "github.com/microsoft/typescript-go/internal/ast"
2626 "github.com/microsoft/typescript-go/internal/core"
27- "github.com/microsoft/typescript-go/internal/jsnum"
2827 "github.com/microsoft/typescript-go/internal/scanner"
2928 "github.com/microsoft/typescript-go/internal/sourcemap"
3029 "github.com/microsoft/typescript-go/internal/stringutil"
@@ -630,26 +629,30 @@ func (p *Printer) writeCommentRange(comment ast.CommentRange) {
630629 }
631630
632631 text := p .currentSourceFile .Text ()
633- if comment .Kind == ast .KindMultiLineCommentTrivia {
634- lineMap := p .currentSourceFile .LineMap ()
632+ lineMap := p .currentSourceFile .LineMap ()
633+ p .writeCommentRangeWorker (text , lineMap , comment .Kind , comment .TextRange )
634+ }
635+
636+ func (p * Printer ) writeCommentRangeWorker (text string , lineMap []core.TextPos , kind ast.Kind , loc core.TextRange ) {
637+ if kind == ast .KindMultiLineCommentTrivia {
635638 indentSize := len (getIndentString (1 ))
636- firstLine := scanner .ComputeLineOfPosition (lineMap , comment .Pos ())
639+ firstLine := scanner .ComputeLineOfPosition (lineMap , loc .Pos ())
637640 lineCount := len (lineMap )
638641 firstCommentLineIndent := - 1
639- pos := comment .Pos ()
642+ pos := loc .Pos ()
640643 currentLine := firstLine
641- for ; pos < comment .End (); currentLine ++ {
644+ for ; pos < loc .End (); currentLine ++ {
642645 var nextLineStart int
643646 if currentLine + 1 == lineCount {
644647 nextLineStart = len (text ) + 1
645648 } else {
646649 nextLineStart = int (lineMap [currentLine + 1 ])
647650 }
648651
649- if pos != comment .Pos () {
652+ if pos != loc .Pos () {
650653 // If we are not emitting first line, we need to write the spaces to adjust the alignment
651654 if firstCommentLineIndent == - 1 {
652- firstCommentLineIndent = calculateIndent (text , int (lineMap [firstLine ]), comment .Pos ())
655+ firstCommentLineIndent = calculateIndent (text , int (lineMap [firstLine ]), loc .Pos ())
653656 }
654657
655658 // These are number of spaces writer is going to write at current indent
@@ -689,11 +692,11 @@ func (p *Printer) writeCommentRange(comment ast.CommentRange) {
689692 }
690693
691694 // Write the comment line text
692- end := min (comment .End (), nextLineStart - 1 )
695+ end := min (loc .End (), nextLineStart - 1 )
693696 currentLineText := strings .TrimSpace (text [pos :end ])
694697 if len (currentLineText ) > 0 {
695698 p .writeComment (currentLineText )
696- if end != comment .End () {
699+ if end != loc .End () {
697700 p .writeLine ()
698701 }
699702 } else {
@@ -705,19 +708,14 @@ func (p *Printer) writeCommentRange(comment ast.CommentRange) {
705708 }
706709 } else {
707710 // Single line comment of style //....
708- p .writeComment (text [comment .Pos ():comment .End ()])
711+ p .writeComment (text [loc .Pos ():loc .End ()])
709712 }
710713}
711714
712715//
713716// Custom emit behavior stubs (i.e., from `EmitNode`, `EmitFlags`, etc.)
714717//
715718
716- func (p * Printer ) getConstantValue (node * ast.Node ) any {
717- // !!! Const-enum inlining (low priority)
718- return nil
719- }
720-
721719func (p * Printer ) shouldEmitComments (node * ast.Node ) bool {
722720 return ! p .commentsDisabled &&
723721 p .currentSourceFile != nil &&
@@ -2428,12 +2426,6 @@ func (p *Printer) mayNeedDotDotForPropertyAccess(expression *ast.Expression) boo
24282426 ! strings .Contains (text , scanner .TokenToString (ast .KindDotToken )) &&
24292427 ! strings .Contains (text , "E" ) &&
24302428 ! strings .Contains (text , "e" )
2431- } else if ast .IsAccessExpression (expression ) {
2432- // check if constant enum value is a non-negative integer
2433- if constantValue , ok := p .getConstantValue (expression ).(jsnum.Number ); ok {
2434- return ! constantValue .IsInf () && constantValue >= 0 && constantValue .Floor () == constantValue
2435- }
2436- return false
24372429 }
24382430 return false
24392431}
@@ -5017,7 +5009,7 @@ func (p *Printer) emitCommentsBeforeNode(node *ast.Node) *commentState {
50175009
50185010 // Emit leading comments
50195011 p .emitLeadingCommentsOfNode (node , emitFlags , commentRange )
5020- p .emitLeadingSyntheticCommentsOfNode (node )
5012+ p .emitLeadingSyntheticCommentsOfNode (node , emitFlags )
50215013 if emitFlags & EFNoNestedComments != 0 {
50225014 p .commentsDisabled = true
50235015 }
@@ -5043,7 +5035,7 @@ func (p *Printer) emitCommentsAfterNode(node *ast.Node, state *commentState) {
50435035 p .commentsDisabled = false
50445036 }
50455037
5046- p .emitTrailingSyntheticCommentsOfNode (node )
5038+ p .emitTrailingSyntheticCommentsOfNode (node , emitFlags )
50475039 p .emitTrailingCommentsOfNode (node , emitFlags , commentRange , containerPos , containerEnd , declarationListContainerEnd )
50485040
50495041 // !!! Preserve comments from type annotation:
@@ -5182,12 +5174,62 @@ func (p *Printer) emitTrailingCommentsOfNode(node *ast.Node, emitFlags EmitFlags
51825174 }
51835175}
51845176
5185- func (p * Printer ) emitLeadingSyntheticCommentsOfNode (node * ast.Node ) {
5186- // !!!
5177+ func (p * Printer ) emitLeadingSyntheticCommentsOfNode (node * ast.Node , emitFlags EmitFlags ) {
5178+ if emitFlags & EFNoLeadingComments != 0 {
5179+ return
5180+ }
5181+ synth := p .emitContext .GetSyntheticLeadingComments (node )
5182+ for _ , c := range synth {
5183+ p .emitLeadingSynthesizedComment (c )
5184+ }
51875185}
51885186
5189- func (p * Printer ) emitTrailingSyntheticCommentsOfNode (node * ast.Node ) {
5190- // !!!
5187+ func (p * Printer ) emitLeadingSynthesizedComment (comment SynthesizedComment ) {
5188+ if comment .HasLeadingNewLine || comment .Kind == ast .KindSingleLineCommentTrivia {
5189+ p .writer .WriteLine ()
5190+ }
5191+ p .writeSynthesizedComment (comment )
5192+ if comment .HasTrailingNewLine || comment .Kind == ast .KindSingleLineCommentTrivia {
5193+ p .writer .WriteLine ()
5194+ } else {
5195+ p .writer .WriteSpace (" " )
5196+ }
5197+ }
5198+
5199+ func (p * Printer ) emitTrailingSyntheticCommentsOfNode (node * ast.Node , emitFlags EmitFlags ) {
5200+ if emitFlags & EFNoTrailingComments != 0 {
5201+ return
5202+ }
5203+ synth := p .emitContext .GetSyntheticTrailingComments (node )
5204+ for _ , c := range synth {
5205+ p .emitTrailingSynthesizedComment (c )
5206+ }
5207+ }
5208+
5209+ func (p * Printer ) emitTrailingSynthesizedComment (comment SynthesizedComment ) {
5210+ if ! p .writer .IsAtStartOfLine () {
5211+ p .writer .WriteSpace (" " )
5212+ }
5213+ p .writeSynthesizedComment (comment )
5214+ if comment .HasTrailingNewLine {
5215+ p .writer .WriteLine ()
5216+ }
5217+ }
5218+
5219+ func formatSynthesizedComment (comment SynthesizedComment ) string {
5220+ if comment .Kind == ast .KindMultiLineCommentTrivia {
5221+ return "/*" + comment .Text + "*/"
5222+ }
5223+ return "//" + comment .Text
5224+ }
5225+
5226+ func (p * Printer ) writeSynthesizedComment (comment SynthesizedComment ) {
5227+ text := formatSynthesizedComment (comment )
5228+ var lineMap []core.TextPos
5229+ if comment .Kind == ast .KindMultiLineCommentTrivia {
5230+ lineMap = core .ComputeLineStarts (text )
5231+ }
5232+ p .writeCommentRangeWorker (text , lineMap , comment .Kind , core .NewTextRange (0 , len (text )))
51915233}
51925234
51935235func (p * Printer ) emitLeadingComments (pos int , elided bool ) bool {
0 commit comments