Repository: mvdan/gofumpt
Pull request #160 (2021-11-01 18:24:05)

format: always simplify input files
(see commit message)


Patch:
diff --git a/format/format.go b/format/format.go
index e225286..012b602 100644
--- a/format/format.go
+++ b/format/format.go
@@ -70,6 +70,8 @@ var rxCodeGenerated = regexp.MustCompile(`^// Code generated .* DO NOT EDIT\.$`)
 // changes might include manipulating adding or removing newlines in fset,
 // modifying the position of nodes, or modifying literal values.
 func File(fset *token.FileSet, file *ast.File, opts Options) {
+	simplify(file)
+
 	for _, cg := range file.Comments {
 		if cg.Pos() > file.Package {
 			break
diff --git a/rewrite.go b/format/rewrite.go
similarity index 99%
rename from rewrite.go
rename to format/rewrite.go
index deae0b6..9e5d457 100644
--- a/rewrite.go
+++ b/format/rewrite.go
@@ -2,7 +2,7 @@
 // Use of this source code is governed by a BSD-style
 // license that can be found in the LICENSE file.
 
-package main
+package format
 
 import (
 	"go/ast"
diff --git a/simplify.go b/format/simplify.go
similarity index 99%
rename from simplify.go
rename to format/simplify.go
index 1a0e817..f2fd4d6 100644
--- a/simplify.go
+++ b/format/simplify.go
@@ -2,7 +2,7 @@
 // Use of this source code is governed by a BSD-style
 // license that can be found in the LICENSE file.
 
-package main
+package format
 
 import (
 	"go/ast"
diff --git a/go.mod b/go.mod
index 666e2f5..0e9695e 100644
--- a/go.mod
+++ b/go.mod
@@ -3,8 +3,9 @@ module mvdan.cc/gofumpt
 go 1.16
 
 require (
+	github.com/frankban/quicktest v1.14.0
 	github.com/google/go-cmp v0.5.6
-	github.com/rogpeppe/go-internal v1.8.1-0.20210923151022-86f73c517451
+	github.com/rogpeppe/go-internal v1.8.1-0.20211023094830-115ce09fd6b4
 	golang.org/x/mod v0.5.1
 	golang.org/x/sys v0.0.0-20210809222454-d867a43fc93e
 	golang.org/x/tools v0.1.8-0.20211001212643-0ebff1a9577c
diff --git a/go.sum b/go.sum
index 25254d4..d373eaa 100644
--- a/go.sum
+++ b/go.sum
@@ -1,14 +1,22 @@
+github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
+github.com/frankban/quicktest v1.14.0 h1:+cqqvzZV87b4adx/5ayVOaYZ2CrvM4ejQvUdBzPPUss=
+github.com/frankban/quicktest v1.14.0/go.mod h1:NeW+ay9A/U67EYXNFA1nPE8e/tnQv/09mUdL/ijj8og=
 github.com/google/go-cmp v0.5.6 h1:BKbKCqvP6I+rmFHt06ZmyQtvB8xAkWdhFyr0ZUNZcxQ=
 github.com/google/go-cmp v0.5.6/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
-github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
 github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
+github.com/kr/pretty v0.3.0 h1:WgNl7dwNpEZ6jJ9k1snq4pZsg7DOEN8hP9Xw0Tsjwk0=
+github.com/kr/pretty v0.3.0/go.mod h1:640gp4NfQd8pI5XOwp5fnNeVWj67G7CFk/SaSQn7NBk=
 github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
-github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
 github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
+github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
+github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
 github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e h1:aoZm08cpOy4WuID//EZDgcC4zIxODThtZNPirFr42+A=
 github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA=
+github.com/rogpeppe/go-internal v1.6.1/go.mod h1:xXDCJY+GAPziupqXw64V24skbSoqbTEfhy4qGm1nDQc=
 github.com/rogpeppe/go-internal v1.8.1-0.20210923151022-86f73c517451 h1:d1PiN4RxzIFXCJTvRkvSkKqwtRAl5ZV4lATKtQI0B7I=
 github.com/rogpeppe/go-internal v1.8.1-0.20210923151022-86f73c517451/go.mod h1:JeRgkft04UBgHMgCIwADu4Pn6Mtm5d4nPKWu0nJ5d+o=
+github.com/rogpeppe/go-internal v1.8.1-0.20211023094830-115ce09fd6b4 h1:Ha8xCaq6ln1a+R91Km45Oq6lPXj2Mla6CRJYcuV2h1w=
+github.com/rogpeppe/go-internal v1.8.1-0.20211023094830-115ce09fd6b4/go.mod h1:JeRgkft04UBgHMgCIwADu4Pn6Mtm5d4nPKWu0nJ5d+o=
 github.com/yuin/goldmark v1.4.0/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k=
 golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
 golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
diff --git a/gofmt.go b/gofmt.go
index 98be0d4..eedb60a 100644
--- a/gofmt.go
+++ b/gofmt.go
@@ -117,8 +117,6 @@ func processFile(filename string, in io.Reader, out io.Writer, stdin bool) error
 
 	ast.SortImports(fileSet, file)
 
-	simplify(file)
-
 	// Apply gofumpt's changes before we print the code in gofumpt's format.
 
 	if *langVersion == "" {


Test patch:
diff --git a/format/format_test.go b/format/format_test.go
new file mode 100644
index 0000000..32dc197
--- /dev/null
+++ b/format/format_test.go
@@ -0,0 +1,38 @@
+// Copyright (c) 2021, Daniel Martí <mvdan@mvdan.cc>
+// See LICENSE for licensing information
+
+package format_test
+
+import (
+	"testing"
+
+	qt "github.com/frankban/quicktest"
+
+	"mvdan.cc/gofumpt/format"
+)
+
+func TestSourceIncludesSimplify(t *testing.T) {
+	t.Parallel()
+
+	in := []byte(`
+package p
+
+var ()
+
+func f() {
+	for _ = range v {
+	}
+}
+`[1:])
+	want := []byte(`
+package p
+
+func f() {
+	for range v {
+	}
+}
+`[1:])
+	got, err := format.Source(in, format.Options{})
+	qt.Assert(t, err, qt.IsNil)
+	qt.Assert(t, string(got), qt.Equals, string(want))
+}
diff --git a/main_test.go b/main_test.go
index 98f9744..e5d5bd2 100644
--- a/main_test.go
+++ b/main_test.go
@@ -9,6 +9,8 @@ import (
 	"path/filepath"
 	"testing"
 
+	qt "github.com/frankban/quicktest"
+
 	"github.com/rogpeppe/go-internal/gotooltest"
 	"github.com/rogpeppe/go-internal/testscript"
 )
@@ -30,8 +32,7 @@ func TestScripts(t *testing.T) {
 		Dir:           filepath.Join("testdata", "scripts"),
 		UpdateScripts: *update,
 	}
-	if err := gotooltest.Setup(&p); err != nil {
-		t.Fatal(err)
-	}
+	err := gotooltest.Setup(&p)
+	qt.Assert(t, err, qt.IsNil)
 	testscript.Run(t, p)
 }