Skip to content

Commit

Permalink
type group: add grouping support
Browse files Browse the repository at this point in the history
This allows doing things like:

```go
type (
  X int64
  Y int64

  //-

  A int64
  B int64

  //-

  x int64
  y int64

  //-

  a int64
  b int64
```
  • Loading branch information
MarioCarrion committed Apr 4, 2019
1 parent bd1607b commit 45d3f28
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 5 deletions.
2 changes: 1 addition & 1 deletion methods_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func TestMethodsValidator_Validate(t *testing.T) {
ts.Fatalf("expected no error, got %s", err1)
}
if section == nit.FileSectionTypes {
tvalidator = &nit.TypesValidator{}
tvalidator = nit.NewTypesValidator(comments)
if err1 := tvalidator.Validate(g, fset); err1 != nil {
ts.Fatalf("expected no error, got %s", err1)
}
Expand Down
2 changes: 1 addition & 1 deletion nitpicking.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ func (v *Nitpicker) validateToken(d ast.Decl) error {
if v.tvalidator != nil {
return errors.New("only one `type` section block is allowed per file")
}
v.tvalidator = &TypesValidator{}
v.tvalidator = NewTypesValidator(v.comments)
if err := v.tvalidator.Validate(genDecl, v.fset); err != nil {
return err
}
Expand Down
18 changes: 18 additions & 0 deletions testdata/types_group3.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package testdata

type (
Types_Group3_X int64
Types_Group3_Y int64

//- one

Types_Group3_A int64
Types_Group3_B int64

//- two

//- three

types_group3_1 int64
types_group3_2 int64
)
16 changes: 16 additions & 0 deletions testdata/types_group4.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package testdata

type (
types_group4_1 int64
types_group4_2 int64

//-

Types_Group4_A int64
Types_Group4_B int64

//-

Types_Group4_X int64
Types_Group4_Y int64
)
21 changes: 19 additions & 2 deletions types.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,16 @@ type (
// the `type` sections.
TypesValidator struct {
sortedNamesValidator
types []string
comments *BreakComments
types []string
}
)

// NewTypesValidator returns a correctly initialized TypesValidator.
func NewTypesValidator(c *BreakComments) *TypesValidator {
return &TypesValidator{comments: c}
}

// Types returns the names of all found types.
func (tv *TypesValidator) Types() []string {
dst := make([]string, len(tv.types))
Expand Down Expand Up @@ -43,10 +49,21 @@ func (tv *TypesValidator) Validate(v *ast.GenDecl, fset *token.FileSet) error {
return errors.Wrap(errors.Errorf("invalid token %+v", t), errPrefix)
}

if err := tv.validateName(errPrefix, s.Name); err != nil {
if err := tv.validateExported(errPrefix, s.Name); err != nil {
return err
}

next := tv.comments.Next()
if next != -1 && fset.PositionFor(s.Pos(), false).Line > next {
tv.last = ""
}

if err := tv.validateSortedName(errPrefix, s.Name); err != nil {
return err
}

tv.comments.MoveTo(fset.PositionFor(s.End(), false).Line)

tv.types = append(tv.types, s.Name.Name)
}

Expand Down
14 changes: 13 additions & 1 deletion types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ func TestTypesValidator_Validate(t *testing.T) {
"types_valid.go",
false,
},
{
"OK: group",
"types_group3.go",
false,
},
{
"Error: parenthesized declaration",
"types_paren.go",
Expand All @@ -40,17 +45,24 @@ func TestTypesValidator_Validate(t *testing.T) {
"types_sorted.go",
true,
},
{
"Error: group 4",
"types_group4.go",
true,
},
}

for _, tt := range tests {
t.Run(tt.name, func(ts *testing.T) {
f, fset := newParserFile(ts, tt.filename)

comments := nit.NewBreakComments(fset, f.Comments)
validator := nit.NewTypesValidator(comments)

for _, s := range f.Decls {
switch g := s.(type) {
case *ast.GenDecl:
if g.Tok == token.TYPE {
validator := nit.TypesValidator{}
if err := validator.Validate(g, fset); tt.expectedError != (err != nil) {
ts.Fatalf("expected error %t, got %s", tt.expectedError, err)
}
Expand Down

0 comments on commit 45d3f28

Please sign in to comment.