1- package structholder
1+ package internal
22
33import (
44 "cmp"
@@ -8,24 +8,22 @@ import (
88 "strings"
99
1010 "golang.org/x/tools/go/analysis"
11-
12- "github.com/manuelarte/funcorder/internal/astutils"
13- "github.com/manuelarte/funcorder/internal/diag"
14- "github.com/manuelarte/funcorder/internal/features"
15- "github.com/manuelarte/funcorder/internal/models"
1611)
1712
1813//nolint:gochecknoglobals // constant
1914var alphabeticalSortFunc = func (a , b * ast.FuncDecl ) int {
2015 return strings .Compare (a .Name .Name , b .Name .Name )
2116}
2217
18+ type ExportedMethods []* ast.FuncDecl
19+ type UnexportedMethods []* ast.FuncDecl
20+
2321// StructHolder contains all the information around a Go struct.
2422type StructHolder struct {
2523 // The fileset
2624 Fset * token.FileSet
2725 // The features to be analyzed
28- Features features. Feature
26+ Features Feature
2927
3028 // The struct declaration
3129 Struct * ast.TypeSpec
@@ -55,15 +53,15 @@ func (sh *StructHolder) Analyze() ([]analysis.Diagnostic, error) {
5553
5654 var reports []analysis.Diagnostic
5755
58- if sh .Features .IsEnabled (features . ConstructorCheck ) {
56+ if sh .Features .IsEnabled (ConstructorCheck ) {
5957 newReports , err := sh .analyzeConstructor ()
6058 if err != nil {
6159 return nil , err
6260 }
6361 reports = append (reports , newReports ... )
6462 }
6563
66- if sh .Features .IsEnabled (features . StructMethodCheck ) {
64+ if sh .Features .IsEnabled (StructMethodCheck ) {
6765 newReports , err := sh .analyzeStructMethod ()
6866 if err != nil {
6967 return nil , err
@@ -80,17 +78,17 @@ func (sh *StructHolder) analyzeConstructor() ([]analysis.Diagnostic, error) {
8078
8179 for i , constructor := range sh .Constructors {
8280 if constructor .Pos () < sh .Struct .Pos () {
83- reports = append (reports , diag . NewConstructorNotAfterStructType (sh .Struct , constructor ))
81+ reports = append (reports , NewConstructorNotAfterStructType (sh .Struct , constructor ))
8482 }
8583
8684 if len (sh .StructMethods ) > 0 && constructor .Pos () > sh .StructMethods [0 ].Pos () {
87- reports = append (reports , diag . NewConstructorNotBeforeStructMethod (sh .Struct , constructor , sh .StructMethods [0 ]))
85+ reports = append (reports , NewConstructorNotBeforeStructMethod (sh .Struct , constructor , sh .StructMethods [0 ]))
8886 }
8987
90- if sh .Features .IsEnabled (features . AlphabeticalCheck ) &&
88+ if sh .Features .IsEnabled (AlphabeticalCheck ) &&
9189 i < len (sh .Constructors )- 1 && sh .Constructors [i ].Name .Name > sh .Constructors [i + 1 ].Name .Name {
9290 reports = append (reports ,
93- diag . NewAdjacentConstructorsNotSortedAlphabetically (sh .Struct , sh .Constructors [i ], sh .Constructors [i + 1 ]),
91+ NewAdjacentConstructorsNotSortedAlphabetically (sh .Struct , sh .Constructors [i ], sh .Constructors [i + 1 ]),
9492 )
9593 }
9694
@@ -131,12 +129,12 @@ func (sh *StructHolder) analyzeStructMethod() ([]analysis.Diagnostic, error) {
131129 continue
132130 }
133131
134- reports = append (reports , diag . NewUnexportedMethodBeforeExportedForStruct (sh .Struct , m , lastExportedMethod ))
132+ reports = append (reports , NewUnexportedMethodBeforeExportedForStruct (sh .Struct , m , lastExportedMethod ))
135133 }
136134 }
137135
138- if sh .Features .IsEnabled (features . AlphabeticalCheck ) {
139- exported , unexported := astutils . SplitExportedUnexported (sh .StructMethods )
136+ if sh .Features .IsEnabled (AlphabeticalCheck ) {
137+ exported , unexported := SplitExportedUnexported (sh .StructMethods )
140138 reports = slices .Concat (reports ,
141139 sortDiagnostics (sh .Struct , exported ),
142140 sortDiagnostics (sh .Struct , unexported ),
@@ -160,11 +158,11 @@ func (sh *StructHolder) suggestConstructorFix() ([]analysis.SuggestedFix, error)
160158 addingConstructorsTextEdit := make ([]analysis.TextEdit , len (sh .Constructors ))
161159 for i , constructor := range sortedConstructors {
162160 removingConstructorsTextEdit [i ] = analysis.TextEdit {
163- Pos : astutils . GetStartingPos (constructor ),
161+ Pos : GetStartingPos (constructor ),
164162 End : constructor .End (),
165163 NewText : make ([]byte , 0 ),
166164 }
167- constructorBytes , err := astutils . NodeToBytes (sh .Fset , constructor )
165+ constructorBytes , err := NodeToBytes (sh .Fset , constructor )
168166 if err != nil {
169167 return nil , err
170168 }
@@ -186,7 +184,7 @@ func (sh *StructHolder) suggestConstructorFix() ([]analysis.SuggestedFix, error)
186184func (sh * StructHolder ) copyAndSortConstructors () []* ast.FuncDecl {
187185 sortedConstructors := make ([]* ast.FuncDecl , len (sh .Constructors ))
188186 copy (sortedConstructors , sh .Constructors )
189- if sh .Features .IsEnabled (features . AlphabeticalCheck ) {
187+ if sh .Features .IsEnabled (AlphabeticalCheck ) {
190188 slices .SortFunc (sortedConstructors , alphabeticalSortFunc )
191189 }
192190
@@ -199,31 +197,31 @@ func (sh *StructHolder) suggestMethodFix() ([]analysis.SuggestedFix, error) {
199197 addingMethodsTextEdit := make ([]analysis.TextEdit , len (sh .StructMethods ))
200198 for i , method := range sortedExported {
201199 removingMethodsTextEdit [i ] = analysis.TextEdit {
202- Pos : astutils . GetStartingPos (method ),
200+ Pos : GetStartingPos (method ),
203201 End : method .End (),
204202 NewText : make ([]byte , 0 ),
205203 }
206- methodBytes , err := astutils . NodeToBytes (sh .Fset , method )
204+ methodBytes , err := NodeToBytes (sh .Fset , method )
207205 if err != nil {
208206 return nil , err
209207 }
210208 addingMethodsTextEdit [i ] = analysis.TextEdit {
211- Pos : astutils . GetStartingPos (sh .StructMethods [0 ]),
209+ Pos : GetStartingPos (sh .StructMethods [0 ]),
212210 NewText : slices .Concat (methodBytes , []byte ("\n \n " )),
213211 }
214212 }
215213 for i , method := range sortedUnexported {
216214 removingMethodsTextEdit [i + len (sortedExported )] = analysis.TextEdit {
217- Pos : astutils . GetStartingPos (method ),
215+ Pos : GetStartingPos (method ),
218216 End : method .End (),
219217 NewText : make ([]byte , 0 ),
220218 }
221- methodBytes , err := astutils . NodeToBytes (sh .Fset , method )
219+ methodBytes , err := NodeToBytes (sh .Fset , method )
222220 if err != nil {
223221 return nil , err
224222 }
225223 addingMethodsTextEdit [i + len (sortedExported )] = analysis.TextEdit {
226- Pos : astutils . GetStartingPos (sh .StructMethods [0 ]),
224+ Pos : GetStartingPos (sh .StructMethods [0 ]),
227225 NewText : slices .Concat (methodBytes , []byte ("\n \n " )),
228226 }
229227 }
@@ -237,13 +235,13 @@ func (sh *StructHolder) suggestMethodFix() ([]analysis.SuggestedFix, error) {
237235 return suggestedFixes , nil
238236}
239237
240- func (sh * StructHolder ) copyAndSortMethods () (models. ExportedMethods , models. UnexportedMethods ) {
241- exported , unexported := astutils . SplitExportedUnexported (sh .StructMethods )
238+ func (sh * StructHolder ) copyAndSortMethods () (ExportedMethods , UnexportedMethods ) {
239+ exported , unexported := SplitExportedUnexported (sh .StructMethods )
242240 sortedExported := make ([]* ast.FuncDecl , len (exported ))
243241 sortedUnexported := make ([]* ast.FuncDecl , len (unexported ))
244242 copy (sortedExported , exported )
245243 copy (sortedUnexported , unexported )
246- if sh .Features .IsEnabled (features . AlphabeticalCheck ) {
244+ if sh .Features .IsEnabled (AlphabeticalCheck ) {
247245 slices .SortFunc (sortedExported , alphabeticalSortFunc )
248246 slices .SortFunc (sortedUnexported , alphabeticalSortFunc )
249247 }
@@ -261,7 +259,7 @@ func sortDiagnostics(typeSpec *ast.TypeSpec, funcDecls []*ast.FuncDecl) []analys
261259
262260 if funcDecls [i ].Name .Name > funcDecls [i + 1 ].Name .Name {
263261 reports = append (reports ,
264- diag . NewAdjacentStructMethodsNotSortedAlphabetically (typeSpec , funcDecls [i ], funcDecls [i + 1 ]))
262+ NewAdjacentStructMethodsNotSortedAlphabetically (typeSpec , funcDecls [i ], funcDecls [i + 1 ]))
265263 }
266264 }
267265
0 commit comments