diff --git a/imports/fix_test.go b/imports/fix_test.go index 9e5ea8cb..3506dbd1 100644 --- a/imports/fix_test.go +++ b/imports/fix_test.go @@ -19,8 +19,9 @@ import ( var only = flag.String("only", "", "If non-empty, the fix test to run") var tests = []struct { - name string - in, out string + name string + formatOnly bool + in, out string }{ // Adding an import to an existing parenthesized import { @@ -748,6 +749,31 @@ func main() { fmt.Println() } import "fmt" func main() { fmt.Println() } +`, + }, + + // FormatOnly + { + name: "format only", + formatOnly: true, + in: `package main + +import ( +"fmt" +"golang.org/x/foo" +) + +func main() {} +`, + out: `package main + +import ( + "fmt" + + "golang.org/x/foo" +) + +func main() {} `, }, } @@ -782,6 +808,7 @@ func TestFixImports(t *testing.T) { } for _, tt := range tests { + options.FormatOnly = tt.formatOnly if *only != "" && tt.name != *only { continue } diff --git a/imports/imports.go b/imports/imports.go index c0d68601..9260823c 100644 --- a/imports/imports.go +++ b/imports/imports.go @@ -31,6 +31,8 @@ type Options struct { Comments bool // Print comments (true if nil *Options provided) TabIndent bool // Use tabs for indent (true if nil *Options provided) TabWidth int // Tab width (8 if nil *Options provided) + + FormatOnly bool // Disable the insertion and deletion of imports } // Process formats and adjusts imports for the provided file. @@ -50,9 +52,11 @@ func Process(filename string, src []byte, opt *Options) ([]byte, error) { return nil, err } - _, err = fixImports(fileSet, file, filename) - if err != nil { - return nil, err + if !opt.FormatOnly { + _, err = fixImports(fileSet, file, filename) + if err != nil { + return nil, err + } } sortImports(fileSet, file)