Skip to content

Commit

Permalink
cmd/fix: always format source file before fixing
Browse files Browse the repository at this point in the history
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 <[email protected]>
Reviewed-by: Jay Conrod <[email protected]>
  • Loading branch information
rsc committed Oct 12, 2020
1 parent 7b77ff4 commit 09833da
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion src/cmd/fix/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}
Expand Down

0 comments on commit 09833da

Please sign in to comment.