Skip to content

Commit 2e4d3d4

Browse files
committed
refactor: remove useless call to fileCache
1 parent b0a1ae1 commit 2e4d3d4

File tree

4 files changed

+28
-45
lines changed

4 files changed

+28
-45
lines changed

pkg/commands/run.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ func (c *runCommand) preRunE(_ *cobra.Command, args []string) error {
217217

218218
pkgLoader := lint.NewPackageLoader(c.log.Child(logutils.DebugKeyLoader), c.cfg, args, c.goenv, guard)
219219

220-
c.contextBuilder = lint.NewContextBuilder(c.cfg, pkgLoader, c.fileCache, pkgCache, guard)
220+
c.contextBuilder = lint.NewContextBuilder(c.cfg, pkgLoader, pkgCache, guard)
221221

222222
if err = initHashSalt(c.buildInfo.Version, c.cfg); err != nil {
223223
return fmt.Errorf("failed to init hash salt: %w", err)

pkg/golinters/misspell/misspell.go

+23-34
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"fmt"
55
"go/ast"
66
"go/token"
7+
"os"
78
"strings"
89
"unicode"
910

@@ -12,50 +13,38 @@ import (
1213

1314
"github.com/golangci/golangci-lint/pkg/config"
1415
"github.com/golangci/golangci-lint/pkg/goanalysis"
15-
"github.com/golangci/golangci-lint/pkg/lint/linter"
16+
"github.com/golangci/golangci-lint/pkg/golinters/internal"
1617
)
1718

1819
const linterName = "misspell"
1920

2021
func New(settings *config.MisspellSettings) *goanalysis.Linter {
21-
analyzer := &analysis.Analyzer{
22-
Name: linterName,
23-
Doc: goanalysis.TheOnlyanalyzerDoc,
24-
Run: goanalysis.DummyRun,
22+
replacer, err := createMisspellReplacer(settings)
23+
if err != nil {
24+
internal.LinterLogger.Fatalf("%s: %v", linterName, err)
2525
}
2626

27-
return goanalysis.NewLinter(
28-
linterName,
29-
"Finds commonly misspelled English words",
30-
[]*analysis.Analyzer{analyzer},
31-
nil,
32-
).WithContextSetter(func(lintCtx *linter.Context) {
33-
replacer, ruleErr := createMisspellReplacer(settings)
34-
35-
analyzer.Run = func(pass *analysis.Pass) (any, error) {
36-
if ruleErr != nil {
37-
return nil, ruleErr
38-
}
39-
40-
err := runMisspell(lintCtx, pass, replacer, settings.Mode)
41-
if err != nil {
42-
return nil, err
27+
a := &analysis.Analyzer{
28+
Name: linterName,
29+
Doc: "Finds commonly misspelled English words",
30+
Run: func(pass *analysis.Pass) (any, error) {
31+
for _, file := range pass.Files {
32+
err := runMisspellOnFile(pass, file, replacer, settings.Mode)
33+
if err != nil {
34+
return nil, err
35+
}
4336
}
4437

4538
return nil, nil
46-
}
47-
}).WithLoadMode(goanalysis.LoadModeSyntax)
48-
}
49-
50-
func runMisspell(lintCtx *linter.Context, pass *analysis.Pass, replacer *misspell.Replacer, mode string) error {
51-
for _, file := range pass.Files {
52-
err := runMisspellOnFile(lintCtx, pass, file, replacer, mode)
53-
if err != nil {
54-
return err
55-
}
39+
},
5640
}
5741

58-
return nil
42+
return goanalysis.NewLinter(
43+
a.URL,
44+
a.Doc,
45+
[]*analysis.Analyzer{a},
46+
nil,
47+
).WithLoadMode(goanalysis.LoadModeSyntax)
5948
}
6049

6150
func createMisspellReplacer(settings *config.MisspellSettings) (*misspell.Replacer, error) {
@@ -90,13 +79,13 @@ func createMisspellReplacer(settings *config.MisspellSettings) (*misspell.Replac
9079
return replacer, nil
9180
}
9281

93-
func runMisspellOnFile(lintCtx *linter.Context, pass *analysis.Pass, file *ast.File, replacer *misspell.Replacer, mode string) error {
82+
func runMisspellOnFile(pass *analysis.Pass, file *ast.File, replacer *misspell.Replacer, mode string) error {
9483
position, isGoFile := goanalysis.GetGoFilePosition(pass, file)
9584
if !isGoFile {
9685
return nil
9786
}
9887

99-
fileContent, err := lintCtx.FileCache.GetFileBytes(position.Filename)
88+
fileContent, err := os.ReadFile(position.Filename)
10089
if err != nil {
10190
return fmt.Errorf("can't get file %s contents: %w", position.Filename, err)
10291
}

pkg/lint/context.go

+2-6
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
"github.com/golangci/golangci-lint/internal/cache"
88
"github.com/golangci/golangci-lint/pkg/config"
99
"github.com/golangci/golangci-lint/pkg/exitcodes"
10-
"github.com/golangci/golangci-lint/pkg/fsutils"
1110
"github.com/golangci/golangci-lint/pkg/goanalysis/load"
1211
"github.com/golangci/golangci-lint/pkg/lint/linter"
1312
"github.com/golangci/golangci-lint/pkg/logutils"
@@ -18,19 +17,17 @@ type ContextBuilder struct {
1817

1918
pkgLoader *PackageLoader
2019

21-
fileCache *fsutils.FileCache
22-
pkgCache *cache.Cache
20+
pkgCache *cache.Cache
2321

2422
loadGuard *load.Guard
2523
}
2624

2725
func NewContextBuilder(cfg *config.Config, pkgLoader *PackageLoader,
28-
fileCache *fsutils.FileCache, pkgCache *cache.Cache, loadGuard *load.Guard,
26+
pkgCache *cache.Cache, loadGuard *load.Guard,
2927
) *ContextBuilder {
3028
return &ContextBuilder{
3129
cfg: cfg,
3230
pkgLoader: pkgLoader,
33-
fileCache: fileCache,
3431
pkgCache: pkgCache,
3532
loadGuard: loadGuard,
3633
}
@@ -55,7 +52,6 @@ func (cl *ContextBuilder) Build(ctx context.Context, log logutils.Log, linters [
5552

5653
Cfg: cl.cfg,
5754
Log: log,
58-
FileCache: cl.fileCache,
5955
PkgCache: cl.pkgCache,
6056
LoadGuard: cl.loadGuard,
6157
}

pkg/lint/linter/context.go

+2-4
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77

88
"github.com/golangci/golangci-lint/internal/cache"
99
"github.com/golangci/golangci-lint/pkg/config"
10-
"github.com/golangci/golangci-lint/pkg/fsutils"
1110
"github.com/golangci/golangci-lint/pkg/goanalysis/load"
1211
"github.com/golangci/golangci-lint/pkg/logutils"
1312
)
@@ -20,9 +19,8 @@ type Context struct {
2019
// version for each of packages
2120
OriginalPackages []*packages.Package
2221

23-
Cfg *config.Config
24-
FileCache *fsutils.FileCache
25-
Log logutils.Log
22+
Cfg *config.Config
23+
Log logutils.Log
2624

2725
PkgCache *cache.Cache
2826
LoadGuard *load.Guard

0 commit comments

Comments
 (0)