Skip to content

Commit 4bbc4fb

Browse files
committed
chore: add constants
1 parent 445eac7 commit 4bbc4fb

File tree

2 files changed

+26
-11
lines changed

2 files changed

+26
-11
lines changed

.golangci.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,9 @@ linters-settings:
6363
settings:
6464
hugeParam:
6565
sizeThreshold: 100
66+
nolintlint:
67+
require-specific: true
68+
require-explanation: true
6669

6770
issues:
6871
exclude-use-default: false

exptostd.go

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,18 @@ import (
1919
"golang.org/x/tools/go/ast/inspector"
2020
)
2121

22+
const (
23+
pkgExpMaps = "golang.org/x/exp/maps"
24+
pkgExpSlices = "golang.org/x/exp/slices"
25+
pkgExpConstraints = "golang.org/x/exp/constraints"
26+
)
27+
28+
const (
29+
pkgMaps = "maps"
30+
pkgSlices = "slices"
31+
pkgComp = "cmp"
32+
)
33+
2234
const (
2335
go123 = 123
2436
go121 = 121
@@ -153,16 +165,16 @@ func (a *analyzer) run(pass *analysis.Pass) (any, error) {
153165
}
154166

155167
switch ident.Name {
156-
case "maps":
157-
diagnostic, usage := a.detectPackageUsage(pass, a.mapsPkgReplacements, selExpr, ident, node, "golang.org/x/exp/maps")
168+
case pkgMaps:
169+
diagnostic, usage := a.detectPackageUsage(pass, a.mapsPkgReplacements, selExpr, ident, node, pkgExpMaps)
158170
if usage {
159171
pass.Report(diagnostic)
160172
}
161173

162174
shouldKeepExpMaps = shouldKeepExpMaps || !usage
163175

164-
case "slices":
165-
diagnostic, usage := a.detectPackageUsage(pass, a.slicesPkgReplacements, selExpr, ident, node, "golang.org/x/exp/slices")
176+
case pkgSlices:
177+
diagnostic, usage := a.detectPackageUsage(pass, a.slicesPkgReplacements, selExpr, ident, node, pkgExpSlices)
166178
if usage {
167179
resultExpSlices.Diagnostics = append(resultExpSlices.Diagnostics, diagnostic)
168180
}
@@ -203,23 +215,23 @@ func (a *analyzer) run(pass *analysis.Pass) (any, error) {
203215
})
204216

205217
// maps
206-
a.suggestReplaceImport(pass, imports, shouldKeepExpMaps, "golang.org/x/exp/maps", "maps")
218+
a.suggestReplaceImport(pass, imports, shouldKeepExpMaps, pkgExpMaps, pkgMaps)
207219

208220
// slices
209221
if resultExpSlices.shouldKeepImport {
210222
for _, diagnostic := range resultExpSlices.Diagnostics {
211223
pass.Report(diagnostic)
212224
}
213225
} else {
214-
a.suggestReplaceImport(pass, imports, resultExpSlices.shouldKeepImport, "golang.org/x/exp/slices", "slices")
226+
a.suggestReplaceImport(pass, imports, resultExpSlices.shouldKeepImport, pkgExpSlices, pkgSlices)
215227
}
216228

217229
// constraints
218230
for _, diagnostic := range resultExpConstraints.Diagnostics {
219231
pass.Report(diagnostic)
220232
}
221233

222-
a.suggestReplaceImport(pass, imports, resultExpConstraints.shouldKeepImport, "golang.org/x/exp/constraints", "cmp")
234+
a.suggestReplaceImport(pass, imports, resultExpConstraints.shouldKeepImport, pkgExpConstraints, pkgComp)
223235

224236
return nil, nil
225237
}
@@ -290,7 +302,7 @@ func (a *analyzer) detectConstraintsUsage(pass *analysis.Pass, expr ast.Expr, re
290302
return
291303
}
292304

293-
if pkg.Imported().Path() != "golang.org/x/exp/constraints" {
305+
if pkg.Imported().Path() != pkgExpConstraints {
294306
return
295307
}
296308

@@ -307,7 +319,7 @@ func (a *analyzer) detectConstraintsUsage(pass *analysis.Pass, expr ast.Expr, re
307319

308320
diagnostic := analysis.Diagnostic{
309321
Pos: selExpr.Pos(),
310-
Message: fmt.Sprintf("golang.org/x/exp/constraints.%s can be replaced by %s", selExpr.Sel.Name, rp.Text),
322+
Message: fmt.Sprintf("%s.%s can be replaced by %s", pkgExpConstraints, selExpr.Sel.Name, rp.Text),
311323
}
312324

313325
if rp.Suggested != nil {
@@ -370,7 +382,7 @@ func suggestedFixForClear(callExpr *ast.CallExpr) (analysis.SuggestedFix, error)
370382
func suggestedFixForKeysOrValues(callExpr *ast.CallExpr) (analysis.SuggestedFix, error) {
371383
s := &ast.CallExpr{
372384
Fun: &ast.SelectorExpr{
373-
X: &ast.Ident{Name: "slices"},
385+
X: &ast.Ident{Name: pkgSlices},
374386
Sel: &ast.Ident{Name: "Collect"},
375387
},
376388
Args: []ast.Expr{callExpr},
@@ -394,7 +406,7 @@ func suggestedFixForKeysOrValues(callExpr *ast.CallExpr) (analysis.SuggestedFix,
394406

395407
func suggestedFixForConstraintsOrder(selExpr *ast.SelectorExpr) (analysis.SuggestedFix, error) {
396408
s := &ast.SelectorExpr{
397-
X: &ast.Ident{Name: "cmp"},
409+
X: &ast.Ident{Name: pkgComp},
398410
Sel: &ast.Ident{Name: "Ordered"},
399411
}
400412

0 commit comments

Comments
 (0)