Skip to content

Commit 314e7db

Browse files
Fix shortening of inline interface definitions inside function declarations (#46)
* Fix shortening of inline interfaces in function declarations * Fix spelling * Revert debugging in shortener.go * Bump version * Improve documentation of custom formatters * More README fixes
1 parent 5338e9f commit 314e7db

File tree

5 files changed

+51
-5
lines changed

5 files changed

+51
-5
lines changed

README.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,9 @@ golines [paths to format]
6060
```
6161

6262
The paths can be either directories or individual files. If no paths are
63-
provided, then input is taken from stdin (as with `gofmt`).
63+
provided, then input is taken from `stdin` (as with `gofmt`).
6464

65-
By default, the results are printed to stdout. To overwrite the existing
65+
By default, the results are printed to `stdout`. To overwrite the existing
6666
files in place, use the `-w` flag.
6767

6868
## Options
@@ -93,7 +93,8 @@ with the `--shorten-comments` flag.
9393

9494
By default, the tool will use [`goimports`](https://godoc.org/golang.org/x/tools/cmd/goimports) as
9595
the base formatter (if found), otherwise it will revert to `gofmt`. An explicit formatter can be
96-
set via the `--base-formatter` flag.
96+
set via the `--base-formatter` flag; the command provided here should accept its input via
97+
`stdin` and write its output to `stdout`.
9798

9899
#### Generated files
99100

@@ -203,7 +204,7 @@ For each input source file, `golines` runs through the following process:
203204
the newlines around the node and/or its children
204205
6. Repeat steps 2-5 until no more shortening can be done
205206
7. Run the base formatter (e.g., `gofmt`) over the results, write these to either
206-
stdout or the source file
207+
`stdout` or the source file
207208

208209
See [this blog post](https://yolken.net/blog/cleaner-go-code-golines) for more technical details.
209210

_fixtures/inline_interface.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package fixtures
2+
3+
// Based on example in https://github.com/segmentio/golines/issues/40
4+
func CreateEphemeralRecordEvent(ctx interface {
5+
string
6+
int
7+
string
8+
}, id string, name string, kaid string, districtID string, status string, anotherArg string, aThirdArg string) (*string, error) {
9+
return nil, nil
10+
}

_fixtures/inline_interface__exp.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package fixtures
2+
3+
// Based on example in https://github.com/segmentio/golines/issues/40
4+
func CreateEphemeralRecordEvent(
5+
ctx interface {
6+
string
7+
int
8+
string
9+
},
10+
id string,
11+
name string,
12+
kaid string,
13+
districtID string,
14+
status string,
15+
anotherArg string,
16+
aThirdArg string,
17+
) (*string, error) {
18+
return nil, nil
19+
}

annotation.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,14 @@ func HasAnnotation(node dst.Node) bool {
3434
IsAnnotation(startDecorations[len(startDecorations)-1])
3535
}
3636

37+
// HasTailAnnotation determines whether the given AST node has a line length annotation at its
38+
// end. This is needed to catch long function declarations with inline interface definitions.
39+
func HasTailAnnotation(node dst.Node) bool {
40+
endDecorations := node.Decorations().End.All()
41+
return len(endDecorations) > 0 &&
42+
IsAnnotation(endDecorations[len(endDecorations)-1])
43+
}
44+
3745
// HasAnnotationRecursive determines whether the given node or one of its children has a
3846
// golines annotation on it. It's currently implemented for function declarations, fields,
3947
// call expressions, and selector expressions only.
@@ -51,6 +59,8 @@ func HasAnnotationRecursive(node dst.Node) bool {
5159
}
5260
}
5361
}
62+
case *dst.Field:
63+
return HasTailAnnotation(n) || HasAnnotationRecursive(n.Type)
5464
case *dst.SelectorExpr:
5565
return HasAnnotation(n.Sel) || HasAnnotation(n.X)
5666
case *dst.CallExpr:
@@ -63,6 +73,12 @@ func HasAnnotationRecursive(node dst.Node) bool {
6373
return true
6474
}
6575
}
76+
case *dst.InterfaceType:
77+
for _, field := range n.Methods.List {
78+
if HasAnnotationRecursive(field) {
79+
return true
80+
}
81+
}
6682
}
6783

6884
return false

main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import (
1616
)
1717

1818
const (
19-
versionStr = "0.6.0"
19+
versionStr = "0.7.0"
2020
)
2121

2222
var (

0 commit comments

Comments
 (0)