Skip to content

Commit

Permalink
funcs/methods: fix sorted subgroups; vars: fix next vars
Browse files Browse the repository at this point in the history
  • Loading branch information
MarioCarrion committed Feb 18, 2019
1 parent ed6e921 commit f350b40
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 32 deletions.
12 changes: 6 additions & 6 deletions file_sections.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,15 +157,15 @@ func (constsFileSection) Funcs() (FileSectionTransition, error) {
}

func (constsFileSection) Imports() (FileSectionTransition, error) {
return nil, errors.New("`imports` is invalid, next one must be `vars`, `functions` or `methods`")
return nil, errors.New("`imports` is invalid, next one must be `var`, `functions` or `methods`")
}

func (constsFileSection) Methods() (FileSectionTransition, error) {
return methodsFileSection{}, nil
}

func (constsFileSection) Types() (FileSectionTransition, error) {
return nil, errors.New("`types` is invalid, next one must be `vars`, `functions` or `methods`")
return nil, errors.New("`types` is invalid, next one must be `var`, `functions` or `methods`")
}

func (constsFileSection) Vars() (FileSectionTransition, error) {
Expand Down Expand Up @@ -195,7 +195,7 @@ func (funcsFileSection) Types() (FileSectionTransition, error) {
}

func (funcsFileSection) Vars() (FileSectionTransition, error) {
return nil, errors.New("`vars` is invalid, next one must be `functions` or `methods`")
return nil, errors.New("`var` is invalid, next one must be `functions` or `methods`")
}

//-
Expand Down Expand Up @@ -247,7 +247,7 @@ func (methodsFileSection) Types() (FileSectionTransition, error) {
}

func (methodsFileSection) Vars() (FileSectionTransition, error) {
return nil, errors.New("`vars` is invalid, next one must be `methods`")
return nil, errors.New("`var` is invalid, next one must be `methods`")
}

//-
Expand All @@ -261,7 +261,7 @@ func (typesFileSection) Funcs() (FileSectionTransition, error) {
}

func (typesFileSection) Imports() (FileSectionTransition, error) {
return nil, errors.New("`imports` is invalid, next one must be `const`, `vars`, `functions` or `methods`")
return nil, errors.New("`imports` is invalid, next one must be `const`, `var`, `functions` or `methods`")
}

func (typesFileSection) Methods() (FileSectionTransition, error) {
Expand Down Expand Up @@ -299,5 +299,5 @@ func (varsFileSection) Types() (FileSectionTransition, error) {
}

func (varsFileSection) Vars() (FileSectionTransition, error) {
return varsFileSection{}, nil
return nil, errors.New("`var` is invalid, next one must be `functions` or `methods`")
}
2 changes: 1 addition & 1 deletion file_sections_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ func TestVarsFileSectionTransition(t *testing.T) {
{
"Vars",
i.Vars,
false,
true,
},
}

Expand Down
7 changes: 3 additions & 4 deletions funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ type (
sortedNamesValidator

comments *BreakComments
lastLine int
}
)

Expand All @@ -33,16 +32,16 @@ func (f *FuncsValidator) Validate(v *ast.FuncDecl, fset *token.FileSet) error {
return err
}

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

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

f.lastLine = fset.PositionFor(v.End(), false).Line
f.comments.MoveTo(f.lastLine)
f.comments.MoveTo(fset.PositionFor(v.End(), false).Line)

return nil
}
12 changes: 6 additions & 6 deletions imports.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,6 @@ func NewImportsSection(path, localPathPrefix string) ImportsSection {
return ImportsSectionExternal
}

// NewImportsValidator returns a new instalce of ImporstValidator with the
// local path prefix set.
func NewImportsValidator(localPath string) ImportsValidator {
return ImportsValidator{localPath: localPath}
}

// NewImportsSectionMachine returns a new ImportsSectionMachine with the
// initial state as `start`.
func NewImportsSectionMachine(start ImportsSection) (*ImportsSectionMachine, error) {
Expand All @@ -94,6 +88,12 @@ func NewImportsTransition(s ImportsSection) (ImportsTransition, error) {
return nil, errors.New("invalid imports value")
}

// NewImportsValidator returns a new instalce of ImporstValidator with the
// local path prefix set.
func NewImportsValidator(localPath string) ImportsValidator {
return ImportsValidator{localPath: localPath}
}

//-

// Current returns the current state.
Expand Down
6 changes: 2 additions & 4 deletions methods.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ type (
sortedTypes sortedNamesValidator
sortedMethods sortedNamesValidator
types map[string]struct{}
lastLine int
lastType string
}

Expand Down Expand Up @@ -68,7 +67,7 @@ func (m *MethodsValidator) Validate(v *ast.FuncDecl, fset *token.FileSet) error
if honorComments {
next := m.comments.Next()

if m.lastLine != 0 && next > m.lastLine {
if next != -1 && fset.PositionFor(i.Pos(), false).Line > next {
v.last = ""
}
}
Expand All @@ -93,8 +92,7 @@ func (m *MethodsValidator) Validate(v *ast.FuncDecl, fset *token.FileSet) error
return err
}

m.lastLine = fset.PositionFor(v.End(), false).Line
m.comments.MoveTo(m.lastLine)
m.comments.MoveTo(fset.PositionFor(v.End(), false).Line)

return nil
}
24 changes: 13 additions & 11 deletions sorted_names_validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,26 +14,28 @@ type (
}
)

func (v *sortedNamesValidator) validateExported(errPrefix string, name *ast.Ident) error {
if v.exported == nil || (*v.exported && !name.IsExported()) {
e := name.IsExported()
v.exported = &e
func (v *sortedNamesValidator) validateName(errPrefix string, name *ast.Ident) error {
if err := v.validateExported(errPrefix, name); err != nil {
return err
}

if *v.exported != name.IsExported() {
return errors.Wrap(errors.Errorf("%s `%s` is not grouped correctly", v.identType, name.Name), errPrefix)
if err := v.validateSortedName(errPrefix, name); err != nil {
return err
}

return nil
}

func (v *sortedNamesValidator) validateName(errPrefix string, name *ast.Ident) error {
if err := v.validateExported(errPrefix, name); err != nil {
return err
//-

func (v *sortedNamesValidator) validateExported(errPrefix string, name *ast.Ident) error {
if v.exported == nil || (*v.exported && !name.IsExported()) {
e := name.IsExported()
v.exported = &e
}

if err := v.validateSortedName(errPrefix, name); err != nil {
return err
if *v.exported != name.IsExported() {
return errors.Wrap(errors.Errorf("%s `%s` is not grouped correctly", v.identType, name.Name), errPrefix)
}

return nil
Expand Down

0 comments on commit f350b40

Please sign in to comment.