Repository: goplus/gop
Pull request #1024 (2021-12-03 14:39:58)

gop/format.Source: don't break compatibility of go/format.Source


Patch:
diff --git a/cmd/gopfmt/gopfmt.go b/cmd/gopfmt/gopfmt.go
index a74afa08..e29bcb13 100644
--- a/cmd/gopfmt/gopfmt.go
+++ b/cmd/gopfmt/gopfmt.go
@@ -69,7 +69,7 @@ func processFile(filename string, in io.Reader, out io.Writer) error {
 		return err
 	}
 
-	res, err := format.Source(filename, src)
+	res, err := format.Source(src, filename)
 	if err != nil {
 		return err
 	}
diff --git a/cmd/internal/gopfmt/fmt.go b/cmd/internal/gopfmt/fmt.go
index 0be10663..11b1f150 100644
--- a/cmd/internal/gopfmt/fmt.go
+++ b/cmd/internal/gopfmt/fmt.go
@@ -70,9 +70,9 @@ func gopfmt(path string, smart, mvgo bool) (err error) {
 	}
 	var target []byte
 	if smart {
-		target, err = xformat.GopstyleSource(path, src)
+		target, err = xformat.GopstyleSource(src, path)
 	} else {
-		target, err = format.Source(path, src)
+		target, err = format.Source(src, path)
 	}
 	if err != nil {
 		return
diff --git a/format/format.go b/format/format.go
index 63d92793..3ee26f05 100644
--- a/format/format.go
+++ b/format/format.go
@@ -101,9 +101,13 @@ func Node(dst io.Writer, fset *token.FileSet, node interface{}) error {
 // space as src), and the result is indented by the same amount as the first
 // line of src containing code. Imports are not sorted for partial source files.
 //
-func Source(filename string, src []byte) ([]byte, error) {
+func Source(src []byte, filename ...string) ([]byte, error) {
+	var fname string
+	if filename != nil {
+		fname = filename[0]
+	}
 	fset := token.NewFileSet()
-	file, sourceAdj, indentAdj, err := parse(fset, filename, src, true)
+	file, sourceAdj, indentAdj, err := parse(fset, fname, src, true)
 	if err != nil {
 		return nil, err
 	}
diff --git a/x/format/gopstyle.go b/x/format/gopstyle.go
index 7fba5f83..a5307d7a 100644
--- a/x/format/gopstyle.go
+++ b/x/format/gopstyle.go
@@ -11,10 +11,14 @@ import (
 
 // -----------------------------------------------------------------------------
 
-func GopstyleSource(filename string, src []byte) (ret []byte, err error) {
+func GopstyleSource(src []byte, filename ...string) (ret []byte, err error) {
+	var fname string
+	if filename != nil {
+		fname = filename[0]
+	}
 	fset := token.NewFileSet()
 	var f *ast.File
-	if f, err = parser.ParseFile(fset, filename, src, parser.ParseComments); err == nil {
+	if f, err = parser.ParseFile(fset, fname, src, parser.ParseComments); err == nil {
 		Gopstyle(f)
 		var buf bytes.Buffer
 		if err = format.Node(&buf, fset, f); err == nil {


Test patch:
diff --git a/printer/gop_test.go b/printer/gop_test.go
index 329aaa22..25517a3c 100644
--- a/printer/gop_test.go
+++ b/printer/gop_test.go
@@ -108,7 +108,7 @@ func testFrom(t *testing.T, fpath string) {
 		t.Fatal(err)
 	}
 
-	res, err := format.Source(fpath, src)
+	res, err := format.Source(src, fpath)
 	if err != nil {
 		t.Fatal("Source failed:", err)
 	}
diff --git a/x/format/gopstyle_test.go b/x/format/gopstyle_test.go
index 77dfe072..d6116bf5 100644
--- a/x/format/gopstyle_test.go
+++ b/x/format/gopstyle_test.go
@@ -8,7 +8,7 @@ import (
 
 func testFormat(t *testing.T, name string, src, expect string) {
 	t.Run(name, func(t *testing.T) {
-		result, err := GopstyleSource(name, []byte(src))
+		result, err := GopstyleSource([]byte(src), name)
 		if err != nil {
 			t.Fatal("format.Source failed:", err)
 		}