From 02e8ee6893d42fbed1f021f3acb3d2d2f96a04f3 Mon Sep 17 00:00:00 2001 From: Brad Fitzpatrick Date: Tue, 5 Apr 2016 16:10:15 +0000 Subject: [PATCH] imports: add Options.FormatOnly Fixes golang/go#14500 Change-Id: Ied9d772e5f606ce6716193faa2c1a285f0ab00b9 Reviewed-on: https://go-review.googlesource.com/21532 Reviewed-by: Robert Griesemer --- imports/fix_test.go | 31 +++++++++++++++++++++++++++++-- imports/imports.go | 10 +++++++--- 2 files changed, 36 insertions(+), 5 deletions(-) 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)