mirror of
https://github.com/golang/go
synced 2024-11-19 04:54:41 -07:00
cmd/gofmt: fix error on partial Go code ending with line comment.
Fix issue by always appending newline after user input, before the closing curly bracket. The adjust func is modified to remove this new newline. Add test case (it fails before CL, passes after). Fixes #8411. LGTM=gri R=golang-codereviews, bradfitz, josharian, gri CC=golang-codereviews https://golang.org/cl/124700043
This commit is contained in:
parent
0be59730fd
commit
9368d6ccbf
@ -277,14 +277,14 @@ func parse(fset *token.FileSet, filename string, src []byte, stdin bool) (*ast.F
|
||||
// into a function body. This handles expressions too.
|
||||
// Insert using a ;, not a newline, so that the line numbers
|
||||
// in fsrc match the ones in src.
|
||||
fsrc := append(append([]byte("package p; func _() {"), src...), '}')
|
||||
fsrc := append(append([]byte("package p; func _() {"), src...), '\n', '}')
|
||||
file, err = parser.ParseFile(fset, filename, fsrc, parserMode)
|
||||
if err == nil {
|
||||
adjust := func(orig, src []byte) []byte {
|
||||
// Remove the wrapping.
|
||||
// Gofmt has turned the ; into a \n\n.
|
||||
src = src[len("package p\n\nfunc _() {"):]
|
||||
src = src[:len(src)-len("}\n")]
|
||||
src = src[:len(src)-len("\n}\n")]
|
||||
// Gofmt has also indented the function body one level.
|
||||
// Remove that indent.
|
||||
src = bytes.Replace(src, []byte("\n\t"), []byte("\n"), -1)
|
||||
|
3
src/cmd/gofmt/testdata/stdin5.golden
vendored
Normal file
3
src/cmd/gofmt/testdata/stdin5.golden
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
//gofmt -stdin
|
||||
|
||||
i := 5 // Line comment without newline.
|
3
src/cmd/gofmt/testdata/stdin5.input
vendored
Normal file
3
src/cmd/gofmt/testdata/stdin5.input
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
//gofmt -stdin
|
||||
|
||||
i :=5// Line comment without newline.
|
Loading…
Reference in New Issue
Block a user