Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
mateusz834 committed Sep 4, 2024
1 parent bfcf41b commit 9833b87
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 33 deletions.
5 changes: 1 addition & 4 deletions src/go/printer/printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,16 +63,13 @@ type printer struct {
mode pmode // current printer mode
endAlignment bool // if set, terminate alignment immediately
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)
prevOpen token.Token // previous non-brace "open" token (, [, or token.ILLEGAL
wsbuf []whiteSpace // delayed white space
goBuild []int // start index of all //go: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
// The out position differs from the pos position when the result
// formatting differs from the source formatting (in the amount of
Expand Down
58 changes: 29 additions & 29 deletions src/go/printer/printer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -867,11 +867,11 @@ func TestEmptyDecl(t *testing.T) { // issue 63566

func TestDocFormat(t *testing.T) {
cases := []struct {
in string
fmt string
src string
want string
}{
{
in: `package main
src: `package main
func main() {
//
Expand All @@ -880,7 +880,7 @@ func main() {
//
}
`,
fmt: `package main
want: `package main
func main() {
//
Expand All @@ -891,7 +891,7 @@ func main() {
`,
},
{
in: `package main
src: `package main
func main() {
//go:directive
Expand All @@ -903,7 +903,7 @@ func main() {
test()
}
`,
fmt: `package main
want: `package main
func main() {
//go:directive
Expand All @@ -917,15 +917,15 @@ func main() {
`,
},
{
in: `package main
src: `package main
func main() {
//go:directive
// test
type a struct{}
}
`,
fmt: `package main
want: `package main
func main() {
//go:directive
Expand All @@ -935,14 +935,14 @@ func main() {
`,
},
{
in: `package main
src: `package main
func a() {
//line a:5:1
//
}
`,
fmt: `package main
want: `package main
func a() {
//line a:5:1
Expand All @@ -952,15 +952,15 @@ func a() {
},

{
in: `package main
src: `package main
// test comment
//go:directive2
// test comment
func main() {
}
`,
fmt: `package main
want: `package main
// test comment
// test comment
Expand All @@ -971,15 +971,15 @@ func main() {
`,
},
{
in: `package main
src: `package main
// test comment
//go:directive2
// test comment
func main() {
}
`,
fmt: `package main
want: `package main
// test comment
// test comment
Expand All @@ -990,7 +990,7 @@ func main() {
`,
},
{
in: `package main
src: `package main
/* test
*/ // test comment
Expand All @@ -999,7 +999,7 @@ func main() {
func main() {
}
`,
fmt: `package main
want: `package main
/* test
*/ // test comment
Expand All @@ -1011,12 +1011,12 @@ func main() {
},

{
in: `package main //comment
src: `package main //comment
var a int = 4 //comment
func a() {
}
`,
fmt: `package main //comment
want: `package main //comment
var a int = 4 //comment
func a() {
}
Expand All @@ -1025,31 +1025,31 @@ func a() {

// Edge case found by a fuzzer, not a real-world example.
{
in: "package A\n\nimport(\"\f\"\n//\n\"\")",
fmt: "package A\n\nimport (\n\t\"\f\" //\n\t\"\"\n)\n",
src: "package A\n\nimport(\"\f\"\n//\n\"\")",
want: "package A\n\nimport (\n\t\"\f\" //\n\t\"\"\n)\n",
},
{
in: "package A\n\nimport(`\f`\n//\n\"\")",
fmt: "package A\n\nimport (\n\t`\f` //\n\t\"\"\n)\n",
src: "package A\n\nimport(`\f`\n//\n\"\")",
want: "package A\n\nimport (\n\t`\f` //\n\t\"\"\n)\n",
},
}

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

var s strings.Builder
var buf strings.Builder
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)
}

out := s.String()
if out != tt.fmt {
t.Errorf("source\n%v\nformatted as:\n%v\nwant formatted as:\n%v", tt.in, out, tt.fmt)
got := buf.String()
if got != tt.want {
t.Errorf("source\n%v\nformatted as:\n%v\nwant formatted as:\n%v", tt.src, got, tt.want)
}
}
}

0 comments on commit 9833b87

Please sign in to comment.