@@ -24,7 +24,6 @@ import (
24
24
25
25
"github.com/microsoft/typescript-go/internal/ast"
26
26
"github.com/microsoft/typescript-go/internal/core"
27
- "github.com/microsoft/typescript-go/internal/jsnum"
28
27
"github.com/microsoft/typescript-go/internal/scanner"
29
28
"github.com/microsoft/typescript-go/internal/sourcemap"
30
29
"github.com/microsoft/typescript-go/internal/stringutil"
@@ -630,26 +629,30 @@ func (p *Printer) writeCommentRange(comment ast.CommentRange) {
630
629
}
631
630
632
631
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 {
635
638
indentSize := len (getIndentString (1 ))
636
- firstLine := scanner .ComputeLineOfPosition (lineMap , comment .Pos ())
639
+ firstLine := scanner .ComputeLineOfPosition (lineMap , loc .Pos ())
637
640
lineCount := len (lineMap )
638
641
firstCommentLineIndent := - 1
639
- pos := comment .Pos ()
642
+ pos := loc .Pos ()
640
643
currentLine := firstLine
641
- for ; pos < comment .End (); currentLine ++ {
644
+ for ; pos < loc .End (); currentLine ++ {
642
645
var nextLineStart int
643
646
if currentLine + 1 == lineCount {
644
647
nextLineStart = len (text ) + 1
645
648
} else {
646
649
nextLineStart = int (lineMap [currentLine + 1 ])
647
650
}
648
651
649
- if pos != comment .Pos () {
652
+ if pos != loc .Pos () {
650
653
// If we are not emitting first line, we need to write the spaces to adjust the alignment
651
654
if firstCommentLineIndent == - 1 {
652
- firstCommentLineIndent = calculateIndent (text , int (lineMap [firstLine ]), comment .Pos ())
655
+ firstCommentLineIndent = calculateIndent (text , int (lineMap [firstLine ]), loc .Pos ())
653
656
}
654
657
655
658
// These are number of spaces writer is going to write at current indent
@@ -689,11 +692,11 @@ func (p *Printer) writeCommentRange(comment ast.CommentRange) {
689
692
}
690
693
691
694
// Write the comment line text
692
- end := min (comment .End (), nextLineStart - 1 )
695
+ end := min (loc .End (), nextLineStart - 1 )
693
696
currentLineText := strings .TrimSpace (text [pos :end ])
694
697
if len (currentLineText ) > 0 {
695
698
p .writeComment (currentLineText )
696
- if end != comment .End () {
699
+ if end != loc .End () {
697
700
p .writeLine ()
698
701
}
699
702
} else {
@@ -705,19 +708,14 @@ func (p *Printer) writeCommentRange(comment ast.CommentRange) {
705
708
}
706
709
} else {
707
710
// Single line comment of style //....
708
- p .writeComment (text [comment .Pos ():comment .End ()])
711
+ p .writeComment (text [loc .Pos ():loc .End ()])
709
712
}
710
713
}
711
714
712
715
//
713
716
// Custom emit behavior stubs (i.e., from `EmitNode`, `EmitFlags`, etc.)
714
717
//
715
718
716
- func (p * Printer ) getConstantValue (node * ast.Node ) any {
717
- // !!! Const-enum inlining (low priority)
718
- return nil
719
- }
720
-
721
719
func (p * Printer ) shouldEmitComments (node * ast.Node ) bool {
722
720
return ! p .commentsDisabled &&
723
721
p .currentSourceFile != nil &&
@@ -2428,12 +2426,6 @@ func (p *Printer) mayNeedDotDotForPropertyAccess(expression *ast.Expression) boo
2428
2426
! strings .Contains (text , scanner .TokenToString (ast .KindDotToken )) &&
2429
2427
! strings .Contains (text , "E" ) &&
2430
2428
! 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
2437
2429
}
2438
2430
return false
2439
2431
}
@@ -5017,7 +5009,7 @@ func (p *Printer) emitCommentsBeforeNode(node *ast.Node) *commentState {
5017
5009
5018
5010
// Emit leading comments
5019
5011
p .emitLeadingCommentsOfNode (node , emitFlags , commentRange )
5020
- p .emitLeadingSyntheticCommentsOfNode (node )
5012
+ p .emitLeadingSyntheticCommentsOfNode (node , emitFlags )
5021
5013
if emitFlags & EFNoNestedComments != 0 {
5022
5014
p .commentsDisabled = true
5023
5015
}
@@ -5043,7 +5035,7 @@ func (p *Printer) emitCommentsAfterNode(node *ast.Node, state *commentState) {
5043
5035
p .commentsDisabled = false
5044
5036
}
5045
5037
5046
- p .emitTrailingSyntheticCommentsOfNode (node )
5038
+ p .emitTrailingSyntheticCommentsOfNode (node , emitFlags )
5047
5039
p .emitTrailingCommentsOfNode (node , emitFlags , commentRange , containerPos , containerEnd , declarationListContainerEnd )
5048
5040
5049
5041
// !!! Preserve comments from type annotation:
@@ -5182,12 +5174,62 @@ func (p *Printer) emitTrailingCommentsOfNode(node *ast.Node, emitFlags EmitFlags
5182
5174
}
5183
5175
}
5184
5176
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
+ }
5187
5185
}
5188
5186
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 )))
5191
5233
}
5192
5234
5193
5235
func (p * Printer ) emitLeadingComments (pos int , elided bool ) bool {
0 commit comments