Skip to content

Commit

Permalink
Fix shortening of inline interface definitions inside function declar…
Browse files Browse the repository at this point in the history
…ations (#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
  • Loading branch information
yolken-segment authored Dec 17, 2021
1 parent 5338e9f commit 314e7db
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 5 deletions.
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,9 @@ golines [paths to format]
```

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

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

## Options
Expand Down Expand Up @@ -93,7 +93,8 @@ with the `--shorten-comments` flag.

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

#### Generated files

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

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

Expand Down
10 changes: 10 additions & 0 deletions _fixtures/inline_interface.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package fixtures

// Based on example in https://github.com/segmentio/golines/issues/40
func CreateEphemeralRecordEvent(ctx interface {
string
int
string
}, id string, name string, kaid string, districtID string, status string, anotherArg string, aThirdArg string) (*string, error) {
return nil, nil
}
19 changes: 19 additions & 0 deletions _fixtures/inline_interface__exp.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package fixtures

// Based on example in https://github.com/segmentio/golines/issues/40
func CreateEphemeralRecordEvent(
ctx interface {
string
int
string
},
id string,
name string,
kaid string,
districtID string,
status string,
anotherArg string,
aThirdArg string,
) (*string, error) {
return nil, nil
}
16 changes: 16 additions & 0 deletions annotation.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,14 @@ func HasAnnotation(node dst.Node) bool {
IsAnnotation(startDecorations[len(startDecorations)-1])
}

// HasTailAnnotation determines whether the given AST node has a line length annotation at its
// end. This is needed to catch long function declarations with inline interface definitions.
func HasTailAnnotation(node dst.Node) bool {
endDecorations := node.Decorations().End.All()
return len(endDecorations) > 0 &&
IsAnnotation(endDecorations[len(endDecorations)-1])
}

// 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,
// call expressions, and selector expressions only.
Expand All @@ -51,6 +59,8 @@ func HasAnnotationRecursive(node dst.Node) bool {
}
}
}
case *dst.Field:
return HasTailAnnotation(n) || HasAnnotationRecursive(n.Type)
case *dst.SelectorExpr:
return HasAnnotation(n.Sel) || HasAnnotation(n.X)
case *dst.CallExpr:
Expand All @@ -63,6 +73,12 @@ func HasAnnotationRecursive(node dst.Node) bool {
return true
}
}
case *dst.InterfaceType:
for _, field := range n.Methods.List {
if HasAnnotationRecursive(field) {
return true
}
}
}

return false
Expand Down
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
)

const (
versionStr = "0.6.0"
versionStr = "0.7.0"
)

var (
Expand Down

0 comments on commit 314e7db

Please sign in to comment.