Skip to content

Commit d2857e3

Browse files
authored
chore: remove useless packages (#34)
1 parent 02482b1 commit d2857e3

File tree

8 files changed

+50
-66
lines changed

8 files changed

+50
-66
lines changed

analyzer/analyzer.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@ import (
77
"golang.org/x/tools/go/analysis/passes/inspect"
88
"golang.org/x/tools/go/ast/inspector"
99

10-
"github.com/manuelarte/funcorder/internal/features"
11-
"github.com/manuelarte/funcorder/internal/fileprocessor"
10+
"github.com/manuelarte/funcorder/internal"
1211
)
1312

1413
const (
@@ -45,20 +44,20 @@ type funcorder struct {
4544
}
4645

4746
func (f *funcorder) run(pass *analysis.Pass) (any, error) {
48-
var enabledCheckers features.Feature
47+
var enabledCheckers internal.Feature
4948
if f.constructorCheck {
50-
enabledCheckers.Enable(features.ConstructorCheck)
49+
enabledCheckers.Enable(internal.ConstructorCheck)
5150
}
5251

5352
if f.structMethodCheck {
54-
enabledCheckers.Enable(features.StructMethodCheck)
53+
enabledCheckers.Enable(internal.StructMethodCheck)
5554
}
5655

5756
if f.alphabeticalCheck {
58-
enabledCheckers.Enable(features.AlphabeticalCheck)
57+
enabledCheckers.Enable(internal.AlphabeticalCheck)
5958
}
6059

61-
fp := fileprocessor.NewFileProcessor(pass.Fset, enabledCheckers)
60+
fp := internal.NewFileProcessor(pass.Fset, enabledCheckers)
6261

6362
insp, found := pass.ResultOf[inspect.Analyzer].(*inspector.Inspector)
6463
if !found {

internal/astutils/utils.go renamed to internal/astutils.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package astutils
1+
package internal
22

33
import (
44
"bytes"

internal/diag/diag.go renamed to internal/diag.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package diag
1+
package internal
22

33
import (
44
"fmt"

internal/features/features.go renamed to internal/features.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package features
1+
package internal
22

33
const (
44
ConstructorCheck Feature = 1 << iota

internal/fileprocessor/file_processor.go renamed to internal/file_processor.go

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,24 @@
1-
package fileprocessor
1+
package internal
22

33
import (
44
"go/ast"
55
"go/token"
66

77
"golang.org/x/tools/go/analysis"
8-
9-
"github.com/manuelarte/funcorder/internal/astutils"
10-
"github.com/manuelarte/funcorder/internal/features"
11-
"github.com/manuelarte/funcorder/internal/models"
12-
"github.com/manuelarte/funcorder/internal/structholder"
138
)
149

1510
// FileProcessor Holder to store all the functions that are potential to be constructors and all the structs.
1611
type FileProcessor struct {
1712
fset *token.FileSet
18-
structs map[string]*structholder.StructHolder
19-
features features.Feature
13+
structs map[string]*StructHolder
14+
features Feature
2015
}
2116

2217
// NewFileProcessor creates a new file processor.
23-
func NewFileProcessor(fset *token.FileSet, checkers features.Feature) *FileProcessor {
18+
func NewFileProcessor(fset *token.FileSet, checkers Feature) *FileProcessor {
2419
return &FileProcessor{
2520
fset: fset,
26-
structs: make(map[string]*structholder.StructHolder),
21+
structs: make(map[string]*StructHolder),
2722
features: checkers,
2823
}
2924
}
@@ -47,16 +42,16 @@ func (fp *FileProcessor) Analyze() ([]analysis.Diagnostic, error) {
4742
}
4843

4944
func (fp *FileProcessor) NewFileNode(_ *ast.File) {
50-
fp.structs = make(map[string]*structholder.StructHolder)
45+
fp.structs = make(map[string]*StructHolder)
5146
}
5247

5348
func (fp *FileProcessor) NewFuncDecl(n *ast.FuncDecl) {
54-
if sc, ok := models.NewStructConstructor(n); ok {
49+
if sc, ok := NewStructConstructor(n); ok {
5550
fp.addConstructor(sc)
5651
return
5752
}
5853

59-
if st, ok := astutils.FuncIsMethod(n); ok {
54+
if st, ok := FuncIsMethod(n); ok {
6055
fp.addMethod(st.Name, n)
6156
}
6257
}
@@ -66,7 +61,7 @@ func (fp *FileProcessor) NewTypeSpec(n *ast.TypeSpec) {
6661
sh.Struct = n
6762
}
6863

69-
func (fp *FileProcessor) addConstructor(sc models.StructConstructor) {
64+
func (fp *FileProcessor) addConstructor(sc StructConstructor) {
7065
sh := fp.getOrCreate(sc.GetStructReturn().Name)
7166
sh.AddConstructor(sc.GetConstructor())
7267
}
@@ -76,12 +71,12 @@ func (fp *FileProcessor) addMethod(st string, n *ast.FuncDecl) {
7671
sh.AddMethod(n)
7772
}
7873

79-
func (fp *FileProcessor) getOrCreate(structName string) *structholder.StructHolder {
74+
func (fp *FileProcessor) getOrCreate(structName string) *StructHolder {
8075
if holder, ok := fp.structs[structName]; ok {
8176
return holder
8277
}
8378

84-
created := &structholder.StructHolder{
79+
created := &StructHolder{
8580
Fset: fp.fset,
8681
Features: fp.features,
8782
}

internal/models/models.go

Lines changed: 0 additions & 6 deletions
This file was deleted.

internal/models/struct_constructor.go renamed to internal/struct_constructor.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1-
package models
1+
package internal
22

33
import (
44
"go/ast"
5-
6-
"github.com/manuelarte/funcorder/internal/astutils"
75
)
86

97
type StructConstructor struct {
@@ -12,13 +10,13 @@ type StructConstructor struct {
1210
}
1311

1412
func NewStructConstructor(funcDec *ast.FuncDecl) (StructConstructor, bool) {
15-
if !astutils.FuncCanBeConstructor(funcDec) {
13+
if !FuncCanBeConstructor(funcDec) {
1614
return StructConstructor{}, false
1715
}
1816

1917
expr := funcDec.Type.Results.List[0].Type
2018

21-
returnType, ok := astutils.GetIdent(expr)
19+
returnType, ok := GetIdent(expr)
2220
if !ok {
2321
return StructConstructor{}, false
2422
}

internal/structholder/structholder.go renamed to internal/structholder.go

Lines changed: 27 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package structholder
1+
package internal
22

33
import (
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
1914
var 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.
2422
type 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)
186184
func (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

Comments
 (0)