Skip to content

Commit 6e4e3e8

Browse files
committed
chore: remove golangci-lint mode
1 parent e25313b commit 6e4e3e8

File tree

4 files changed

+26
-89
lines changed

4 files changed

+26
-89
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
*.test
1818

1919
.vscode
20+
.idea/
2021

2122
# Output of the go coverage tool, specifically when used with LiteIDE
2223
*.out

go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module github.com/4meepo/tagalign
22

3-
go 1.19
3+
go 1.21.0
44

55
require (
66
github.com/fatih/structtag v1.2.0

options.go

-7
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,6 @@ package tagalign
22

33
type Option func(*Helper)
44

5-
// WithMode specify the mode of tagalign.
6-
func WithMode(mode Mode) Option {
7-
return func(h *Helper) {
8-
h.mode = mode
9-
}
10-
}
11-
125
// WithSort enable tags sort.
136
// fixedOrder specify the order of tags, the other tags will be sorted by name.
147
// Sory is disabled by default.

tagalign.go

+24-81
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ package tagalign
33
import (
44
"fmt"
55
"go/ast"
6-
"go/token"
7-
"log"
86
"reflect"
97
"sort"
108
"strconv"
@@ -15,13 +13,6 @@ import (
1513
"golang.org/x/tools/go/analysis"
1614
)
1715

18-
type Mode int
19-
20-
const (
21-
StandaloneMode Mode = iota
22-
GolangciLintMode
23-
)
24-
2516
type Style int
2617

2718
const (
@@ -44,11 +35,9 @@ func NewAnalyzer(options ...Option) *analysis.Analyzer {
4435
}
4536
}
4637

47-
func Run(pass *analysis.Pass, options ...Option) []Issue {
48-
var issues []Issue
38+
func Run(pass *analysis.Pass, options ...Option) {
4939
for _, f := range pass.Files {
5040
h := &Helper{
51-
mode: StandaloneMode,
5241
style: DefaultStyle,
5342
align: true,
5443
}
@@ -63,22 +52,19 @@ func Run(pass *analysis.Pass, options ...Option) []Issue {
6352

6453
if !h.align && !h.sort {
6554
// do nothing
66-
return nil
55+
return
6756
}
6857

6958
ast.Inspect(f, func(n ast.Node) bool {
7059
h.find(pass, n)
7160
return true
7261
})
62+
7363
h.Process(pass)
74-
issues = append(issues, h.issues...)
7564
}
76-
return issues
7765
}
7866

7967
type Helper struct {
80-
mode Mode
81-
8268
style Style
8369

8470
align bool // whether enable tags align.
@@ -87,19 +73,6 @@ type Helper struct {
8773

8874
singleFields []*ast.Field
8975
consecutiveFieldsGroups [][]*ast.Field // fields in this group, must be consecutive in struct.
90-
issues []Issue
91-
}
92-
93-
// Issue is used to integrate with golangci-lint's inline auto fix.
94-
type Issue struct {
95-
Pos token.Position
96-
Message string
97-
InlineFix InlineFix
98-
}
99-
type InlineFix struct {
100-
StartCol int // zero-based
101-
Length int
102-
NewString string
10376
}
10477

10578
func (w *Helper) find(pass *analysis.Pass, n ast.Node) {
@@ -159,39 +132,24 @@ func (w *Helper) find(pass *analysis.Pass, n ast.Node) {
159132
split()
160133
}
161134

162-
func (w *Helper) report(pass *analysis.Pass, field *ast.Field, startCol int, msg, replaceStr string) {
163-
if w.mode == GolangciLintMode {
164-
iss := Issue{
165-
Pos: pass.Fset.Position(field.Tag.Pos()),
166-
Message: msg,
167-
InlineFix: InlineFix{
168-
StartCol: startCol,
169-
Length: len(field.Tag.Value),
170-
NewString: replaceStr,
171-
},
172-
}
173-
w.issues = append(w.issues, iss)
174-
}
175-
176-
if w.mode == StandaloneMode {
177-
pass.Report(analysis.Diagnostic{
178-
Pos: field.Tag.Pos(),
179-
End: field.Tag.End(),
180-
Message: msg,
181-
SuggestedFixes: []analysis.SuggestedFix{
182-
{
183-
Message: msg,
184-
TextEdits: []analysis.TextEdit{
185-
{
186-
Pos: field.Tag.Pos(),
187-
End: field.Tag.End(),
188-
NewText: []byte(replaceStr),
189-
},
135+
func (w *Helper) report(pass *analysis.Pass, field *ast.Field, msg, replaceStr string) {
136+
pass.Report(analysis.Diagnostic{
137+
Pos: field.Tag.Pos(),
138+
End: field.Tag.End(),
139+
Message: msg,
140+
SuggestedFixes: []analysis.SuggestedFix{
141+
{
142+
Message: msg,
143+
TextEdits: []analysis.TextEdit{
144+
{
145+
Pos: field.Tag.Pos(),
146+
End: field.Tag.End(),
147+
NewText: []byte(replaceStr),
190148
},
191149
},
192150
},
193-
})
194-
}
151+
},
152+
})
195153
}
196154

197155
func (w *Helper) Process(pass *analysis.Pass) { //nolint:gocognit
@@ -220,15 +178,15 @@ func (w *Helper) Process(pass *analysis.Pass) { //nolint:gocognit
220178
tag, err := strconv.Unquote(field.Tag.Value)
221179
if err != nil {
222180
// if tag value is not a valid string, report it directly
223-
w.report(pass, field, column, errTagValueSyntax, field.Tag.Value)
181+
w.report(pass, field, errTagValueSyntax, field.Tag.Value)
224182
fields = removeField(fields, i)
225183
continue
226184
}
227185

228186
tags, err := structtag.Parse(tag)
229187
if err != nil {
230188
// if tag value is not a valid struct tag, report it directly
231-
w.report(pass, field, column, err.Error(), field.Tag.Value)
189+
w.report(pass, field, err.Error(), field.Tag.Value)
232190
fields = removeField(fields, i)
233191
continue
234192
}
@@ -340,22 +298,21 @@ func (w *Helper) Process(pass *analysis.Pass) { //nolint:gocognit
340298

341299
msg := "tag is not aligned, should be: " + unquoteTag
342300

343-
w.report(pass, field, offsets[i], msg, newTagValue)
301+
w.report(pass, field, msg, newTagValue)
344302
}
345303
}
346304

347305
// process single fields
348306
for _, field := range w.singleFields {
349-
column := pass.Fset.Position(field.Tag.Pos()).Column - 1
350307
tag, err := strconv.Unquote(field.Tag.Value)
351308
if err != nil {
352-
w.report(pass, field, column, errTagValueSyntax, field.Tag.Value)
309+
w.report(pass, field, errTagValueSyntax, field.Tag.Value)
353310
continue
354311
}
355312

356313
tags, err := structtag.Parse(tag)
357314
if err != nil {
358-
w.report(pass, field, column, err.Error(), field.Tag.Value)
315+
w.report(pass, field, err.Error(), field.Tag.Value)
359316
continue
360317
}
361318
originalTags := append([]*structtag.Tag(nil), tags.Tags()...)
@@ -371,17 +328,10 @@ func (w *Helper) Process(pass *analysis.Pass) { //nolint:gocognit
371328

372329
msg := "tag is not aligned , should be: " + tags.String()
373330

374-
w.report(pass, field, column, msg, newTagValue)
331+
w.report(pass, field, msg, newTagValue)
375332
}
376333
}
377334

378-
// Issues returns all issues found by the analyzer.
379-
// It is used to integrate with golangci-lint.
380-
func (w *Helper) Issues() []Issue {
381-
log.Println("tagalign 's Issues() should only be called in golangci-lint mode")
382-
return w.issues
383-
}
384-
385335
// sortBy sorts tags by fixed order.
386336
// If a tag is not in the fixed order, it will be sorted by name.
387337
func sortBy(fixedOrder []string, tags *structtag.Tags) {
@@ -443,13 +393,6 @@ func alignFormat(length int) string {
443393
return "%" + fmt.Sprintf("-%ds", length)
444394
}
445395

446-
func max(a, b int) int {
447-
if a > b {
448-
return a
449-
}
450-
return b
451-
}
452-
453396
func removeField(fields []*ast.Field, index int) []*ast.Field {
454397
if index < 0 || index >= len(fields) {
455398
return fields

0 commit comments

Comments
 (0)