]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/fix: always format source file before fixing
authorRuss Cox <rsc@golang.org>
Mon, 29 Jun 2020 14:44:56 +0000 (10:44 -0400)
committerRuss Cox <rsc@golang.org>
Mon, 12 Oct 2020 18:31:15 +0000 (18:31 +0000)
This makes the changes to the file easier to explain.
Not all the changes may come from the fixers directly,
if the file is not gofmt-ed already.

Change-Id: I81776da446a34a1239a3130317d2aae1437d61a5
Reviewed-on: https://go-review.googlesource.com/c/go/+/240555
Trust: Russ Cox <rsc@golang.org>
Reviewed-by: Jay Conrod <jayconrod@google.com>
src/cmd/fix/main.go

index e72c66398f845ed957e0891a481722498b9fbb37..d19dde6b4ae23e0e9e4896c218000bf441b49b5e 100644 (file)
@@ -137,6 +137,21 @@ func processFile(filename string, useStdin bool) error {
                return err
        }
 
+       // Make sure file is in canonical format.
+       // This "fmt" pseudo-fix cannot be disabled.
+       newSrc, err := gofmtFile(file)
+       if err != nil {
+               return err
+       }
+       if !bytes.Equal(newSrc, src) {
+               newFile, err := parser.ParseFile(fset, filename, newSrc, parserMode)
+               if err != nil {
+                       return err
+               }
+               file = newFile
+               fmt.Fprintf(&fixlog, " fmt")
+       }
+
        // Apply all fixes to file.
        newFile := file
        fixed := false
@@ -180,7 +195,7 @@ func processFile(filename string, useStdin bool) error {
        // output of the printer run on a standard AST generated by the parser,
        // but the source we generated inside the loop above is the
        // output of the printer run on a mangled AST generated by a fixer.
-       newSrc, err := gofmtFile(newFile)
+       newSrc, err = gofmtFile(newFile)
        if err != nil {
                return err
        }