Skip to content

Commit a1b463e

Browse files
committed
go/printer: do not set impliedSemi to false in print, when writing newlines
Fixes golang#70978 Change-Id: I0cc54bd11bb4170235bb5a6d5eb6d8438beada58
1 parent 500675a commit a1b463e

File tree

2 files changed

+75
-1
lines changed

2 files changed

+75
-1
lines changed

src/go/printer/printer.go

-1
Original file line numberDiff line numberDiff line change
@@ -1015,7 +1015,6 @@ func (p *printer) print(args ...any) {
10151015
ch = '\f' // use formfeed since we dropped one before
10161016
}
10171017
p.writeByte(ch, n)
1018-
impliedSemi = false
10191018
}
10201019
}
10211020

src/go/printer/printer_test.go

+75
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
"io"
1717
"os"
1818
"path/filepath"
19+
"strings"
1920
"testing"
2021
"time"
2122
)
@@ -863,3 +864,77 @@ func TestEmptyDecl(t *testing.T) { // issue 63566
863864
}
864865
}
865866
}
867+
868+
func TestIssue70978(t *testing.T) {
869+
cases := []struct {
870+
src string
871+
want string
872+
newName string
873+
}{
874+
{
875+
newName: "someotherfmtpackage",
876+
src: `package main
877+
878+
func main() {
879+
// comment
880+
881+
fmt.Println() // Comment
882+
}
883+
`,
884+
want: `package main
885+
886+
func main() {
887+
// comment
888+
889+
someotherfmtpackage.Println() // Comment
890+
}
891+
`,
892+
},
893+
{
894+
newName: "someotherfmtpkg",
895+
src: `package main
896+
897+
func main() {
898+
// comment
899+
900+
fmt.Println() // Comment
901+
}
902+
`,
903+
want: `package main
904+
905+
func main() {
906+
// comment
907+
908+
someotherfmtpkg.Println() // Comment
909+
}
910+
`,
911+
},
912+
}
913+
914+
for _, tt := range cases {
915+
fset := token.NewFileSet()
916+
f, err := parser.ParseFile(fset, "test.go", tt.src, parser.SkipObjectResolution|parser.ParseComments)
917+
if err != nil {
918+
t.Fatal(err)
919+
}
920+
921+
ast.Inspect(f, func(n ast.Node) bool {
922+
switch n := n.(type) {
923+
case *ast.SelectorExpr:
924+
switch x := n.X.(type) {
925+
case *ast.Ident:
926+
x.Name = tt.newName
927+
}
928+
}
929+
return true
930+
})
931+
932+
var b strings.Builder
933+
config := Config{Mode: UseSpaces | TabIndent, Tabwidth: 8}
934+
config.Fprint(&b, fset, f)
935+
got := b.String()
936+
if got != tt.want {
937+
t.Errorf("unexpected Fprint output:\n%s\nwant:\n%s", got, tt.want)
938+
}
939+
}
940+
}

0 commit comments

Comments
 (0)