Skip to content

Commit d295aba

Browse files
committed
merging adjacent parameters is now behind -extra
Since this rule was more controversial than others; in many cases, explicitly separate types can be a way to signal that two parameters do not belong together or are not related. However, the feature is still useful. For example, when obtaining a large chunk of generated Go code, or when code is written by new Go developers who weren't aware of this language feature, it can still be useful to automatically apply the rule and use tools like 'git diff' or 'git add -p' to manually vet whether or not the changes make sense. Fixes #53.
1 parent e6f6e0e commit d295aba

File tree

11 files changed

+32
-12
lines changed

11 files changed

+32
-12
lines changed

flag.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@ package main
55

66
import "flag"
77

8-
var langVersion = flag.String("lang", "", "target Go version in the form 1.X (default from go.mod)")
8+
var (
9+
langVersion = flag.String("lang", "", "target Go version in the form 1.X (default from go.mod)")
10+
extraRules = flag.Bool("extra", false, "enable extra rules which should be vetted by a human")
11+
)
912

1013
func init() {
1114
// make -s default to true

format/format.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ type Options struct {
3838
// is equivalent to "1.14.0". When empty, it is equivalent to "v1", to
3939
// not use language features which could break programs.
4040
LangVersion string
41+
42+
ExtraRules bool
4143
}
4244

4345
// Source formats src in gofumpt's format, assuming that src holds a valid Go
@@ -474,6 +476,10 @@ func (f *fumpter) applyPre(c *astutil.Cursor) {
474476
f.stmts(node.Body)
475477

476478
case *ast.FieldList:
479+
// Merging adjacent fields (e.g. parameters) is disabled by default.
480+
if !f.ExtraRules {
481+
break
482+
}
477483
switch c.Parent().(type) {
478484
case *ast.FuncDecl, *ast.FuncType, *ast.InterfaceType:
479485
node.List = f.mergeAdjacentFields(node.List)

gen.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,10 @@ func copyGofmt(pkg *Package) {
252252
// This is the only gofumpt change on gofmt's codebase, besides changing
253253
// the name in the usage text.
254254
` + extraSrcLangVersion + `
255-
gformat.File(fileSet, file, gformat.Options{LangVersion: *langVersion})
255+
gformat.File(fileSet, file, gformat.Options{
256+
LangVersion: *langVersion,
257+
ExtraRules: *extraRules,
258+
})
256259
`
257260
for _, path := range sourceFiles(pkg) {
258261
body := readFile(path)

gofmt.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,10 @@ func processFile(filename string, in io.Reader, out io.Writer, stdin bool) error
126126
*langVersion = string(out)
127127
}
128128
}
129-
gformat.File(fileSet, file, gformat.Options{LangVersion: *langVersion})
129+
gformat.File(fileSet, file, gformat.Options{
130+
LangVersion: *langVersion,
131+
ExtraRules: *extraRules,
132+
})
130133

131134
res, err := format(fileSet, file, sourceAdj, indentAdj, src, printer.Config{Mode: printerMode, Tabwidth: tabWidth})
132135
if err != nil {

gofumports/internal/event/core/fast.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ func Log1(ctx context.Context, message string, t1 label.Label) {
2222

2323
// Log2 takes a message and two labels and delivers a log event to the exporter.
2424
// It is a customized version of Print that is faster and does no allocation.
25-
func Log2(ctx context.Context, message string, t1, t2 label.Label) {
25+
func Log2(ctx context.Context, message string, t1 label.Label, t2 label.Label) {
2626
Export(ctx, MakeEvent([3]label.Label{
2727
keys.Msg.Of(message),
2828
t1,

gofumports/internal/gocommand/invoke.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ type Invocation struct {
135135
Logf func(format string, args ...interface{})
136136
}
137137

138-
func (i *Invocation) runWithFriendlyError(ctx context.Context, stdout, stderr io.Writer) (friendlyError, rawError error) {
138+
func (i *Invocation) runWithFriendlyError(ctx context.Context, stdout, stderr io.Writer) (friendlyError error, rawError error) {
139139
rawError = i.run(ctx, stdout, stderr)
140140
if rawError != nil {
141141
friendlyError = rawError

gofumports/internal/imports/imports.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ func initialize(filename string, src []byte, opt *Options) ([]byte, *Options, er
165165
return src, opt, nil
166166
}
167167

168-
func formatFile(fileSet *token.FileSet, file *ast.File, src []byte, adjust func(orig, src []byte) []byte, opt *Options) ([]byte, error) {
168+
func formatFile(fileSet *token.FileSet, file *ast.File, src []byte, adjust func(orig []byte, src []byte) []byte, opt *Options) ([]byte, error) {
169169
mergeImports(opt.Env, fileSet, file)
170170
sortImports(opt.Env, fileSet, file)
171171
imps := astutil.Imports(fileSet, file)
@@ -343,7 +343,7 @@ func cutSpace(b []byte) (before, middle, after []byte) {
343343
// to every non-blank line in src.
344344
// 3) matchSpace copies the trailing space from orig and uses it in place
345345
// of src's trailing space.
346-
func matchSpace(orig, src []byte) []byte {
346+
func matchSpace(orig []byte, src []byte) []byte {
347347
before, _, after := cutSpace(orig)
348348
i := bytes.LastIndex(before, []byte{'\n'})
349349
before, indent := before[:i+1], before[i+1:]

gofumports/internal/imports/mod.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,7 @@ func (r *ModuleResolver) dirIsNestedModule(dir string, mod *gocommand.ModuleJSON
331331
return modDir != mod.Dir
332332
}
333333

334-
func (r *ModuleResolver) modInfo(dir string) (modDir, modName string) {
334+
func (r *ModuleResolver) modInfo(dir string) (modDir string, modName string) {
335335
readModName := func(modFile string) string {
336336
modBytes, err := ioutil.ReadFile(modFile)
337337
if err != nil {

rewrite.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ func match(m map[string]reflect.Value, pattern, val reflect.Value) bool {
245245
// of wildcards and pos used as the position of tokens from the pattern.
246246
// if m == nil, subst returns a copy of pattern and doesn't change the line
247247
// number information.
248-
func subst(m map[string]reflect.Value, pattern, pos reflect.Value) reflect.Value {
248+
func subst(m map[string]reflect.Value, pattern reflect.Value, pos reflect.Value) reflect.Value {
249249
if !pattern.IsValid() {
250250
return reflect.Value{}
251251
}

testdata/scripts/comment-spaced.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ package p
7070
//export CgoFunc
7171

7272
//extern open
73-
func c_open(name *byte, mode, perm int) int
73+
func c_open(name *byte, mode int, perm int) int
7474

7575
//line 123
7676

testdata/scripts/func-merge-parameters.txt

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
1-
gofumpt -w foo.go
2-
cmp foo.go foo.go.golden
1+
# By default, this rule isn't enabled.
2+
gofumpt foo.go
3+
cmp stdout foo.go
4+
5+
# It's run with -extra.
6+
gofumpt -extra foo.go
7+
cmp stdout foo.go.golden
38

49
gofumpt -d foo.go.golden
510
! stdout .

0 commit comments

Comments
 (0)