1
0
mirror of https://github.com/golang/go synced 2024-11-25 11:27:56 -07:00
This commit is contained in:
Mateusz Poliwczak 2024-09-04 09:00:59 +02:00
parent bfcf41b517
commit 9833b87536
2 changed files with 30 additions and 33 deletions

View File

@ -63,16 +63,13 @@ type printer struct {
mode pmode // current printer mode mode pmode // current printer mode
endAlignment bool // if set, terminate alignment immediately endAlignment bool // if set, terminate alignment immediately
impliedSemi bool // if set, a linebreak implies a semicolon impliedSemi bool // if set, a linebreak implies a semicolon
inDecl bool // if set, printer is inside declaration (after first token)
lastTok token.Token // last token printed (token.ILLEGAL if it's whitespace) lastTok token.Token // last token printed (token.ILLEGAL if it's whitespace)
prevOpen token.Token // previous non-brace "open" token (, [, or token.ILLEGAL prevOpen token.Token // previous non-brace "open" token (, [, or token.ILLEGAL
wsbuf []whiteSpace // delayed white space wsbuf []whiteSpace // delayed white space
goBuild []int // start index of all //go:build comments in output goBuild []int // start index of all //go:build comments in output
plusBuild []int // start index of all // +build comments in output plusBuild []int // start index of all // +build comments in output
// inDecl is set to true when inside of an ast.Decl,
// after printing the first token of that declaration.
inDecl bool
// Positions // Positions
// The out position differs from the pos position when the result // The out position differs from the pos position when the result
// formatting differs from the source formatting (in the amount of // formatting differs from the source formatting (in the amount of

View File

@ -867,11 +867,11 @@ func TestEmptyDecl(t *testing.T) { // issue 63566
func TestDocFormat(t *testing.T) { func TestDocFormat(t *testing.T) {
cases := []struct { cases := []struct {
in string src string
fmt string want string
}{ }{
{ {
in: `package main src: `package main
func main() { func main() {
// //
@ -880,7 +880,7 @@ func main() {
// //
} }
`, `,
fmt: `package main want: `package main
func main() { func main() {
// //
@ -891,7 +891,7 @@ func main() {
`, `,
}, },
{ {
in: `package main src: `package main
func main() { func main() {
//go:directive //go:directive
@ -903,7 +903,7 @@ func main() {
test() test()
} }
`, `,
fmt: `package main want: `package main
func main() { func main() {
//go:directive //go:directive
@ -917,7 +917,7 @@ func main() {
`, `,
}, },
{ {
in: `package main src: `package main
func main() { func main() {
//go:directive //go:directive
@ -925,7 +925,7 @@ func main() {
type a struct{} type a struct{}
} }
`, `,
fmt: `package main want: `package main
func main() { func main() {
//go:directive //go:directive
@ -935,14 +935,14 @@ func main() {
`, `,
}, },
{ {
in: `package main src: `package main
func a() { func a() {
//line a:5:1 //line a:5:1
// //
} }
`, `,
fmt: `package main want: `package main
func a() { func a() {
//line a:5:1 //line a:5:1
@ -952,7 +952,7 @@ func a() {
}, },
{ {
in: `package main src: `package main
// test comment // test comment
//go:directive2 //go:directive2
@ -960,7 +960,7 @@ func a() {
func main() { func main() {
} }
`, `,
fmt: `package main want: `package main
// test comment // test comment
// test comment // test comment
@ -971,7 +971,7 @@ func main() {
`, `,
}, },
{ {
in: `package main src: `package main
// test comment // test comment
//go:directive2 //go:directive2
@ -979,7 +979,7 @@ func main() {
func main() { func main() {
} }
`, `,
fmt: `package main want: `package main
// test comment // test comment
// test comment // test comment
@ -990,7 +990,7 @@ func main() {
`, `,
}, },
{ {
in: `package main src: `package main
/* test /* test
*/ // test comment */ // test comment
@ -999,7 +999,7 @@ func main() {
func main() { func main() {
} }
`, `,
fmt: `package main want: `package main
/* test /* test
*/ // test comment */ // test comment
@ -1011,12 +1011,12 @@ func main() {
}, },
{ {
in: `package main //comment src: `package main //comment
var a int = 4 //comment var a int = 4 //comment
func a() { func a() {
} }
`, `,
fmt: `package main //comment want: `package main //comment
var a int = 4 //comment var a int = 4 //comment
func a() { func a() {
} }
@ -1025,31 +1025,31 @@ func a() {
// Edge case found by a fuzzer, not a real-world example. // Edge case found by a fuzzer, not a real-world example.
{ {
in: "package A\n\nimport(\"\f\"\n//\n\"\")", src: "package A\n\nimport(\"\f\"\n//\n\"\")",
fmt: "package A\n\nimport (\n\t\"\f\" //\n\t\"\"\n)\n", want: "package A\n\nimport (\n\t\"\f\" //\n\t\"\"\n)\n",
}, },
{ {
in: "package A\n\nimport(`\f`\n//\n\"\")", src: "package A\n\nimport(`\f`\n//\n\"\")",
fmt: "package A\n\nimport (\n\t`\f` //\n\t\"\"\n)\n", want: "package A\n\nimport (\n\t`\f` //\n\t\"\"\n)\n",
}, },
} }
for _, tt := range cases { for _, tt := range cases {
fs := token.NewFileSet() fset := token.NewFileSet()
f, err := parser.ParseFile(fs, "test.go", tt.in, parser.ParseComments|parser.SkipObjectResolution) f, err := parser.ParseFile(fset, "test.go", tt.src, parser.ParseComments|parser.SkipObjectResolution)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
var s strings.Builder var buf strings.Builder
cfg := Config{Tabwidth: 8, Mode: UseSpaces | TabIndent} cfg := Config{Tabwidth: 8, Mode: UseSpaces | TabIndent}
if err := cfg.Fprint(&s, fs, f); err != nil { if err := cfg.Fprint(&buf, fset, f); err != nil {
t.Fatal(err) t.Fatal(err)
} }
out := s.String() got := buf.String()
if out != tt.fmt { if got != tt.want {
t.Errorf("source\n%v\nformatted as:\n%v\nwant formatted as:\n%v", tt.in, out, tt.fmt) t.Errorf("source\n%v\nformatted as:\n%v\nwant formatted as:\n%v", tt.src, got, tt.want)
} }
} }
} }