Skip to content

Commit 9833b87

Browse files
committed
update
1 parent bfcf41b commit 9833b87

File tree

2 files changed

+30
-33
lines changed

2 files changed

+30
-33
lines changed

src/go/printer/printer.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,16 +63,13 @@ type printer struct {
6363
mode pmode // current printer mode
6464
endAlignment bool // if set, terminate alignment immediately
6565
impliedSemi bool // if set, a linebreak implies a semicolon
66+
inDecl bool // if set, printer is inside declaration (after first token)
6667
lastTok token.Token // last token printed (token.ILLEGAL if it's whitespace)
6768
prevOpen token.Token // previous non-brace "open" token (, [, or token.ILLEGAL
6869
wsbuf []whiteSpace // delayed white space
6970
goBuild []int // start index of all //go:build comments in output
7071
plusBuild []int // start index of all // +build comments in output
7172

72-
// inDecl is set to true when inside of an ast.Decl,
73-
// after printing the first token of that declaration.
74-
inDecl bool
75-
7673
// Positions
7774
// The out position differs from the pos position when the result
7875
// formatting differs from the source formatting (in the amount of

src/go/printer/printer_test.go

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -867,11 +867,11 @@ func TestEmptyDecl(t *testing.T) { // issue 63566
867867

868868
func TestDocFormat(t *testing.T) {
869869
cases := []struct {
870-
in string
871-
fmt string
870+
src string
871+
want string
872872
}{
873873
{
874-
in: `package main
874+
src: `package main
875875
876876
func main() {
877877
//
@@ -880,7 +880,7 @@ func main() {
880880
//
881881
}
882882
`,
883-
fmt: `package main
883+
want: `package main
884884
885885
func main() {
886886
//
@@ -891,7 +891,7 @@ func main() {
891891
`,
892892
},
893893
{
894-
in: `package main
894+
src: `package main
895895
896896
func main() {
897897
//go:directive
@@ -903,7 +903,7 @@ func main() {
903903
test()
904904
}
905905
`,
906-
fmt: `package main
906+
want: `package main
907907
908908
func main() {
909909
//go:directive
@@ -917,15 +917,15 @@ func main() {
917917
`,
918918
},
919919
{
920-
in: `package main
920+
src: `package main
921921
922922
func main() {
923923
//go:directive
924924
// test
925925
type a struct{}
926926
}
927927
`,
928-
fmt: `package main
928+
want: `package main
929929
930930
func main() {
931931
//go:directive
@@ -935,14 +935,14 @@ func main() {
935935
`,
936936
},
937937
{
938-
in: `package main
938+
src: `package main
939939
940940
func a() {
941941
//line a:5:1
942942
//
943943
}
944944
`,
945-
fmt: `package main
945+
want: `package main
946946
947947
func a() {
948948
//line a:5:1
@@ -952,15 +952,15 @@ func a() {
952952
},
953953

954954
{
955-
in: `package main
955+
src: `package main
956956
957957
// test comment
958958
//go:directive2
959959
// test comment
960960
func main() {
961961
}
962962
`,
963-
fmt: `package main
963+
want: `package main
964964
965965
// test comment
966966
// test comment
@@ -971,15 +971,15 @@ func main() {
971971
`,
972972
},
973973
{
974-
in: `package main
974+
src: `package main
975975
976976
// test comment
977977
//go:directive2
978978
// test comment
979979
func main() {
980980
}
981981
`,
982-
fmt: `package main
982+
want: `package main
983983
984984
// test comment
985985
// test comment
@@ -990,7 +990,7 @@ func main() {
990990
`,
991991
},
992992
{
993-
in: `package main
993+
src: `package main
994994
995995
/* test
996996
*/ // test comment
@@ -999,7 +999,7 @@ func main() {
999999
func main() {
10001000
}
10011001
`,
1002-
fmt: `package main
1002+
want: `package main
10031003
10041004
/* test
10051005
*/ // test comment
@@ -1011,12 +1011,12 @@ func main() {
10111011
},
10121012

10131013
{
1014-
in: `package main //comment
1014+
src: `package main //comment
10151015
var a int = 4 //comment
10161016
func a() {
10171017
}
10181018
`,
1019-
fmt: `package main //comment
1019+
want: `package main //comment
10201020
var a int = 4 //comment
10211021
func a() {
10221022
}
@@ -1025,31 +1025,31 @@ func a() {
10251025

10261026
// Edge case found by a fuzzer, not a real-world example.
10271027
{
1028-
in: "package A\n\nimport(\"\f\"\n//\n\"\")",
1029-
fmt: "package A\n\nimport (\n\t\"\f\" //\n\t\"\"\n)\n",
1028+
src: "package A\n\nimport(\"\f\"\n//\n\"\")",
1029+
want: "package A\n\nimport (\n\t\"\f\" //\n\t\"\"\n)\n",
10301030
},
10311031
{
1032-
in: "package A\n\nimport(`\f`\n//\n\"\")",
1033-
fmt: "package A\n\nimport (\n\t`\f` //\n\t\"\"\n)\n",
1032+
src: "package A\n\nimport(`\f`\n//\n\"\")",
1033+
want: "package A\n\nimport (\n\t`\f` //\n\t\"\"\n)\n",
10341034
},
10351035
}
10361036

10371037
for _, tt := range cases {
1038-
fs := token.NewFileSet()
1039-
f, err := parser.ParseFile(fs, "test.go", tt.in, parser.ParseComments|parser.SkipObjectResolution)
1038+
fset := token.NewFileSet()
1039+
f, err := parser.ParseFile(fset, "test.go", tt.src, parser.ParseComments|parser.SkipObjectResolution)
10401040
if err != nil {
10411041
t.Fatal(err)
10421042
}
10431043

1044-
var s strings.Builder
1044+
var buf strings.Builder
10451045
cfg := Config{Tabwidth: 8, Mode: UseSpaces | TabIndent}
1046-
if err := cfg.Fprint(&s, fs, f); err != nil {
1046+
if err := cfg.Fprint(&buf, fset, f); err != nil {
10471047
t.Fatal(err)
10481048
}
10491049

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

0 commit comments

Comments
 (0)