Skip to content

Commit

Permalink
Improve handling of chained methods (#28)
Browse files Browse the repository at this point in the history
* Update comment

* Update comments and recursive checking

* Change default behavior of change-methods

* Update fixtures

* Fix flag name and update README

* Update README

* Update README

* Update README

* Fix README
  • Loading branch information
yolken-segment authored Apr 11, 2021
1 parent 285bfb8 commit fb67b70
Show file tree
Hide file tree
Showing 9 changed files with 157 additions and 49 deletions.
31 changes: 31 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,37 @@ set via the `--base-formatter` flag.
By default, the tool will not format any files that look like they're generated. If you
want to reformat these too, run with the `--no-ignore-generated` flag.

#### Chained method splitting

There are several possible ways to split lines that are part of
[method chains](https://en.wikipedia.org/wiki/Method_chaining). The original
approach taken by `golines` was to split on the args, e.g.:

```go
myObj.Method(
arg1,
arg2,
arg3,
).AnotherMethod(
arg1,
arg2,
).AThirdMethod(
arg1,
arg2,
)
```

Starting in version 0.3.0, the tool now splits on the dots by default, e.g.:

```go
myObj.Method(arg1, arg2, arg3).
AnotherMethod(arg1, arg2).
AThirdMethod(arg1, arg2)
```

The original behavior can be used by running the tool with the `--no-chain-split-dots`
flag.

#### Struct tag reformatting

In addition to shortening long lines, the tool also aligns struct tag keys; see the
Expand Down
15 changes: 14 additions & 1 deletion _fixtures/chained_calls.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,25 @@
package fixtures

import "fmt"

type Chain struct{}

func (c *Chain) ChainCall(a string, b string, c string) *Chain {
func (c *Chain) ChainCall(arg1 string, arg2 string, arg3 string) *Chain {
return c
}

func NewChain() *Chain {
return &Chain{}
}

func ChainedCalls() {
c := Chain{}
c.ChainCall("a long argument", "another long argument", "a third long argument").ChainCall("a long argument2", "another long argument2", "a third long argument2").ChainCall("a long argument3", "another long argument3", "a third long argument3")
NewChain().ChainCall(
"a really really really really really long argument4",
"another really really really really really long argument4",
fmt.Sprintf("%v", "this is a long method")).ChainCall("a really really really really really long argument5", "another really really really really really long argument5", "a third really really really really really long argument5").ChainCall("a", "b", fmt.Sprintf("%v", "this is a long method"))
NewChain().ChainCall("a", "b", "c").ChainCall("d", "e", "f")
NewChain().ChainCall("a", "b", "c").
ChainCall("d", "e", "f").ChainCall("g", "h", "i")
}
34 changes: 20 additions & 14 deletions _fixtures/chained_calls__exp.go
Original file line number Diff line number Diff line change
@@ -1,24 +1,30 @@
package fixtures

import "fmt"

type Chain struct{}

func (c *Chain) ChainCall(a string, b string, c string) *Chain {
func (c *Chain) ChainCall(arg1 string, arg2 string, arg3 string) *Chain {
return c
}

func NewChain() *Chain {
return &Chain{}
}

func ChainedCalls() {
c := Chain{}
c.ChainCall(
"a long argument",
"another long argument",
"a third long argument",
).ChainCall(
"a long argument2",
"another long argument2",
"a third long argument2",
).ChainCall(
"a long argument3",
"another long argument3",
"a third long argument3",
)
c.ChainCall("a long argument", "another long argument", "a third long argument").
ChainCall("a long argument2", "another long argument2", "a third long argument2").
ChainCall("a long argument3", "another long argument3", "a third long argument3")
NewChain().ChainCall(
"a really really really really really long argument4",
"another really really really really really long argument4",
fmt.Sprintf("%v", "this is a long method"),
).
ChainCall("a really really really really really long argument5", "another really really really really really long argument5", "a third really really really really really long argument5").
ChainCall("a", "b", fmt.Sprintf("%v", "this is a long method"))
NewChain().ChainCall("a", "b", "c").ChainCall("d", "e", "f")
NewChain().ChainCall("a", "b", "c").
ChainCall("d", "e", "f").ChainCall("g", "h", "i")
}
10 changes: 7 additions & 3 deletions annotation.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func HasAnnotation(node dst.Node) bool {

// HasAnnotationRecursive determines whether the given node or one of its children has a
// golines annotation on it. It's currently implemented for function declarations, fields,
// and call expressions only.
// call expressions, and selector expressions only.
func HasAnnotationRecursive(node dst.Node) bool {
if HasAnnotation(node) {
return true
Expand All @@ -51,9 +51,13 @@ func HasAnnotationRecursive(node dst.Node) bool {
}
}
}
case *dst.Field:
return HasAnnotation(n)
case *dst.SelectorExpr:
return HasAnnotation(n.Sel) || HasAnnotation(n.X)
case *dst.CallExpr:
if HasAnnotationRecursive(n.Fun) {
return true
}

for _, arg := range n.Args {
if HasAnnotation(arg) {
return true
Expand Down
5 changes: 2 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
module github.com/segmentio/golines

go 1.13
go 1.16

require (
github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751 // indirect
github.com/alecthomas/units v0.0.0-20190924025748-f65c72e2690d // indirect
github.com/dave/dst v0.23.1
github.com/dave/dst v0.26.2
github.com/dave/jennifer v1.2.0
github.com/fatih/structtag v1.2.0
github.com/konsorten/go-windows-terminal-sequences v1.0.2 // indirect
Expand All @@ -19,6 +19,5 @@ require (
github.com/x-cray/logrus-prefixed-formatter v0.5.2
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550 // indirect
golang.org/x/sys v0.0.0-20191024172528-b4ff53e7a1cb // indirect
golang.org/x/tools v0.0.0-20191024220359-3d91e92cde03 // indirect
gopkg.in/alecthomas/kingpin.v2 v2.2.6
)
11 changes: 11 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ github.com/alecthomas/units v0.0.0-20190924025748-f65c72e2690d h1:UQZhZ2O0vMHr2c
github.com/alecthomas/units v0.0.0-20190924025748-f65c72e2690d/go.mod h1:rBZYJk541a8SKzHPHnH3zbiI+7dagKZ0cgpgrD7Fyho=
github.com/dave/dst v0.23.1 h1:2obX6c3RqALrEOp6u01qsqPvwp0t+RpOp9O4Bf9KhXs=
github.com/dave/dst v0.23.1/go.mod h1:LjPcLEauK4jC5hQ1fE/wr05O41zK91Pr4Qs22Ljq7gs=
github.com/dave/dst v0.26.2 h1:lnxLAKI3tx7MgLNVDirFCsDTlTG9nKTk7GcptKcWSwY=
github.com/dave/dst v0.26.2/go.mod h1:UMDJuIRPfyUCC78eFuB+SV/WI8oDeyFDvM/JR6NI3IU=
github.com/dave/gopackages v0.0.0-20170318123100-46e7023ec56e/go.mod h1:i00+b/gKdIDIxuLDFob7ustLAVqhsZRk2qVZrArELGQ=
github.com/dave/jennifer v1.2.0 h1:S15ZkFMRoJ36mGAQgWL1tnr0NQJh9rZ8qatseX/VbBc=
github.com/dave/jennifer v1.2.0/go.mod h1:fIb+770HOpJ2fmN9EPPKOqm1vMGhB+TwXKMZhrIygKg=
Expand Down Expand Up @@ -59,21 +61,25 @@ github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJy
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/x-cray/logrus-prefixed-formatter v0.5.2 h1:00txxvfBM9muc0jiLIEAkAcIMJzfthRT6usrui8uGmg=
github.com/x-cray/logrus-prefixed-formatter v0.5.2/go.mod h1:2duySbKsL6M18s5GU7VPsoEPHyzalCE06qoARUCeBBE=
github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
golang.org/x/arch v0.0.0-20180920145803-b19384d3c130/go.mod h1:cYlCBUl1MsqxdiKgmc4uh7TxZfWSFLOGSRR090WDxt8=
golang.org/x/crypto v0.0.0-20181127143415-eb0de9b17e85 h1:et7+NAX3lLIk5qUCTA9QelBjGE/NkhzYw/mhnr0s7nI=
golang.org/x/crypto v0.0.0-20181127143415-eb0de9b17e85/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550 h1:ObdrDkeb4kJdCP557AjRjq69pTHfNouLtWZG7j9rPN8=
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/net v0.0.0-20180906233101-161cd47e91fd h1:nTDtHvHSdCn1m6ITfMRqtOd/9+7a3s8RBNOZ3eYZzJA=
golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859 h1:R/3boaszxrf1GEUWTVDzSKVwLmSJpwZ1yqXm8j0v2QI=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20200226121028-0de0cce0169b/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f h1:wMNYb4v58l5UBM7MYRLPG6ZhfOqbKu7X5eyFl8ZhKvA=
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20190423024810-112230192c58 h1:8gQV6CLnAEikrhgkHFbMAEhagSSnXWGV915qUMm9mrU=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20180903190138-2b024373dcd9/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
Expand All @@ -90,7 +96,12 @@ golang.org/x/tools v0.0.0-20181127232545-e782529d0ddd h1:lpAYSh4h+rmI2UtC34xD0/D
golang.org/x/tools v0.0.0-20181127232545-e782529d0ddd/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20191024220359-3d91e92cde03 h1:4gtJXHJ9ud0q8MNSDxJsRU/WH+afypbe4Vk4zq+8qow=
golang.org/x/tools v0.0.0-20191024220359-3d91e92cde03/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.0.0-20200509030707-2212a7e161a5 h1:MeC2gMlMdkd67dn17MEby3rGXRxZtWeiRXOnISfTQ74=
golang.org/x/tools v0.0.0-20200509030707-2212a7e161a5/go.mod h1:EkVYQZoAsY45+roYkvgYkIh4xh/qjgUK9TdY2XT94GE=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gopkg.in/alecthomas/kingpin.v2 v2.2.6 h1:jMFz6MfLP0/4fUyZle81rXUoxOBFi19VUFKVDOQfozc=
gopkg.in/alecthomas/kingpin.v2 v2.2.6/go.mod h1:FMv+mEhP44yOT+4EoQTLFTRgOQ1FBLkstjWtayDeSgw=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
Expand Down
7 changes: 6 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,18 @@ import (
)

const (
versionStr = "0.2.0"
versionStr = "0.3.0"
)

var (
// Flags
baseFormatterCmd = kingpin.Flag(
"base-formatter",
"Base formatter to use").Default("").String()
chainSplitDots = kingpin.Flag(
"chain-split-dots",
"Split chained methods on the dots as opposed to the arguments").
Default("true").Bool()
debug = kingpin.Flag(
"debug",
"Show debug output").Short('d').Default("false").Bool()
Expand Down Expand Up @@ -118,6 +122,7 @@ func run() error {
IgnoreGenerated: *ignoreGenerated,
DotFile: *dotFile,
BaseFormatterCmd: *baseFormatterCmd,
ChainSplitDots: *chainSplitDots,
}
shortener := NewShortener(config)

Expand Down
Loading

0 comments on commit fb67b70

Please sign in to comment.