Skip to content

Commit

Permalink
go/printer: do not set impliedSemi to false in print, when writing ne…
Browse files Browse the repository at this point in the history
…wlines

Fixes golang#70978

Change-Id: I0cc54bd11bb4170235bb5a6d5eb6d8438beada58
  • Loading branch information
mateusz834 committed Dec 24, 2024
1 parent 500675a commit a1b463e
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 1 deletion.
1 change: 0 additions & 1 deletion src/go/printer/printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -1015,7 +1015,6 @@ func (p *printer) print(args ...any) {
ch = '\f' // use formfeed since we dropped one before
}
p.writeByte(ch, n)
impliedSemi = false
}
}

Expand Down
75 changes: 75 additions & 0 deletions src/go/printer/printer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"io"
"os"
"path/filepath"
"strings"
"testing"
"time"
)
Expand Down Expand Up @@ -863,3 +864,77 @@ func TestEmptyDecl(t *testing.T) { // issue 63566
}
}
}

func TestIssue70978(t *testing.T) {
cases := []struct {
src string
want string
newName string
}{
{
newName: "someotherfmtpackage",
src: `package main
func main() {
// comment
fmt.Println() // Comment
}
`,
want: `package main
func main() {
// comment
someotherfmtpackage.Println() // Comment
}
`,
},
{
newName: "someotherfmtpkg",
src: `package main
func main() {
// comment
fmt.Println() // Comment
}
`,
want: `package main
func main() {
// comment
someotherfmtpkg.Println() // Comment
}
`,
},
}

for _, tt := range cases {
fset := token.NewFileSet()
f, err := parser.ParseFile(fset, "test.go", tt.src, parser.SkipObjectResolution|parser.ParseComments)
if err != nil {
t.Fatal(err)
}

ast.Inspect(f, func(n ast.Node) bool {
switch n := n.(type) {
case *ast.SelectorExpr:
switch x := n.X.(type) {
case *ast.Ident:
x.Name = tt.newName
}
}
return true
})

var b strings.Builder
config := Config{Mode: UseSpaces | TabIndent, Tabwidth: 8}
config.Fprint(&b, fset, f)
got := b.String()
if got != tt.want {
t.Errorf("unexpected Fprint output:\n%s\nwant:\n%s", got, tt.want)
}
}
}

0 comments on commit a1b463e

Please sign in to comment.