imports: add Options.FormatOnly
Fixes golang/go#14500 Change-Id: Ied9d772e5f606ce6716193faa2c1a285f0ab00b9 Reviewed-on: https://go-review.googlesource.com/21532 Reviewed-by: Robert Griesemer <gri@golang.org>
This commit is contained in:
parent
f2e4f834ec
commit
02e8ee6893
|
@ -20,6 +20,7 @@ var only = flag.String("only", "", "If non-empty, the fix test to run")
|
||||||
|
|
||||||
var tests = []struct {
|
var tests = []struct {
|
||||||
name string
|
name string
|
||||||
|
formatOnly bool
|
||||||
in, out string
|
in, out string
|
||||||
}{
|
}{
|
||||||
// Adding an import to an existing parenthesized import
|
// Adding an import to an existing parenthesized import
|
||||||
|
@ -748,6 +749,31 @@ func main() { fmt.Println() }
|
||||||
import "fmt"
|
import "fmt"
|
||||||
|
|
||||||
func main() { fmt.Println() }
|
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 {
|
for _, tt := range tests {
|
||||||
|
options.FormatOnly = tt.formatOnly
|
||||||
if *only != "" && tt.name != *only {
|
if *only != "" && tt.name != *only {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
|
@ -31,6 +31,8 @@ type Options struct {
|
||||||
Comments bool // Print comments (true if nil *Options provided)
|
Comments bool // Print comments (true if nil *Options provided)
|
||||||
TabIndent bool // Use tabs for indent (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)
|
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.
|
// Process formats and adjusts imports for the provided file.
|
||||||
|
@ -50,10 +52,12 @@ func Process(filename string, src []byte, opt *Options) ([]byte, error) {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if !opt.FormatOnly {
|
||||||
_, err = fixImports(fileSet, file, filename)
|
_, err = fixImports(fileSet, file, filename)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
sortImports(fileSet, file)
|
sortImports(fileSet, file)
|
||||||
imps := astutil.Imports(fileSet, file)
|
imps := astutil.Imports(fileSet, file)
|
||||||
|
|
Loading…
Reference in New Issue