diff --git a/pkg/commands/run.go b/pkg/commands/run.go index d9aa7578cd70..08de8f53c9fc 100644 --- a/pkg/commands/run.go +++ b/pkg/commands/run.go @@ -217,7 +217,7 @@ func (c *runCommand) preRunE(_ *cobra.Command, args []string) error { pkgLoader := lint.NewPackageLoader(c.log.Child(logutils.DebugKeyLoader), c.cfg, args, c.goenv, guard) - c.contextBuilder = lint.NewContextBuilder(c.cfg, pkgLoader, c.fileCache, pkgCache, guard) + c.contextBuilder = lint.NewContextBuilder(c.cfg, pkgLoader, pkgCache, guard) if err = initHashSalt(c.buildInfo.Version, c.cfg); err != nil { return fmt.Errorf("failed to init hash salt: %w", err) diff --git a/pkg/golinters/misspell/misspell.go b/pkg/golinters/misspell/misspell.go index 3ace5fddb9f8..c6eed6b76831 100644 --- a/pkg/golinters/misspell/misspell.go +++ b/pkg/golinters/misspell/misspell.go @@ -4,6 +4,7 @@ import ( "fmt" "go/ast" "go/token" + "os" "strings" "unicode" @@ -12,50 +13,38 @@ import ( "github.com/golangci/golangci-lint/pkg/config" "github.com/golangci/golangci-lint/pkg/goanalysis" - "github.com/golangci/golangci-lint/pkg/lint/linter" + "github.com/golangci/golangci-lint/pkg/golinters/internal" ) const linterName = "misspell" func New(settings *config.MisspellSettings) *goanalysis.Linter { - analyzer := &analysis.Analyzer{ - Name: linterName, - Doc: goanalysis.TheOnlyanalyzerDoc, - Run: goanalysis.DummyRun, + replacer, err := createMisspellReplacer(settings) + if err != nil { + internal.LinterLogger.Fatalf("%s: %v", linterName, err) } - return goanalysis.NewLinter( - linterName, - "Finds commonly misspelled English words", - []*analysis.Analyzer{analyzer}, - nil, - ).WithContextSetter(func(lintCtx *linter.Context) { - replacer, ruleErr := createMisspellReplacer(settings) - - analyzer.Run = func(pass *analysis.Pass) (any, error) { - if ruleErr != nil { - return nil, ruleErr - } - - err := runMisspell(lintCtx, pass, replacer, settings.Mode) - if err != nil { - return nil, err + a := &analysis.Analyzer{ + Name: linterName, + Doc: "Finds commonly misspelled English words", + Run: func(pass *analysis.Pass) (any, error) { + for _, file := range pass.Files { + err := runMisspellOnFile(pass, file, replacer, settings.Mode) + if err != nil { + return nil, err + } } return nil, nil - } - }).WithLoadMode(goanalysis.LoadModeSyntax) -} - -func runMisspell(lintCtx *linter.Context, pass *analysis.Pass, replacer *misspell.Replacer, mode string) error { - for _, file := range pass.Files { - err := runMisspellOnFile(lintCtx, pass, file, replacer, mode) - if err != nil { - return err - } + }, } - return nil + return goanalysis.NewLinter( + a.Name, + a.Doc, + []*analysis.Analyzer{a}, + nil, + ).WithLoadMode(goanalysis.LoadModeSyntax) } func createMisspellReplacer(settings *config.MisspellSettings) (*misspell.Replacer, error) { @@ -90,13 +79,13 @@ func createMisspellReplacer(settings *config.MisspellSettings) (*misspell.Replac return replacer, nil } -func runMisspellOnFile(lintCtx *linter.Context, pass *analysis.Pass, file *ast.File, replacer *misspell.Replacer, mode string) error { +func runMisspellOnFile(pass *analysis.Pass, file *ast.File, replacer *misspell.Replacer, mode string) error { position, isGoFile := goanalysis.GetGoFilePosition(pass, file) if !isGoFile { return nil } - fileContent, err := lintCtx.FileCache.GetFileBytes(position.Filename) + fileContent, err := os.ReadFile(position.Filename) if err != nil { return fmt.Errorf("can't get file %s contents: %w", position.Filename, err) } diff --git a/pkg/lint/context.go b/pkg/lint/context.go index d04a11b81f1a..2ac5a2d2c4fc 100644 --- a/pkg/lint/context.go +++ b/pkg/lint/context.go @@ -7,7 +7,6 @@ import ( "github.com/golangci/golangci-lint/internal/cache" "github.com/golangci/golangci-lint/pkg/config" "github.com/golangci/golangci-lint/pkg/exitcodes" - "github.com/golangci/golangci-lint/pkg/fsutils" "github.com/golangci/golangci-lint/pkg/goanalysis/load" "github.com/golangci/golangci-lint/pkg/lint/linter" "github.com/golangci/golangci-lint/pkg/logutils" @@ -18,19 +17,17 @@ type ContextBuilder struct { pkgLoader *PackageLoader - fileCache *fsutils.FileCache - pkgCache *cache.Cache + pkgCache *cache.Cache loadGuard *load.Guard } func NewContextBuilder(cfg *config.Config, pkgLoader *PackageLoader, - fileCache *fsutils.FileCache, pkgCache *cache.Cache, loadGuard *load.Guard, + pkgCache *cache.Cache, loadGuard *load.Guard, ) *ContextBuilder { return &ContextBuilder{ cfg: cfg, pkgLoader: pkgLoader, - fileCache: fileCache, pkgCache: pkgCache, loadGuard: loadGuard, } @@ -55,7 +52,6 @@ func (cl *ContextBuilder) Build(ctx context.Context, log logutils.Log, linters [ Cfg: cl.cfg, Log: log, - FileCache: cl.fileCache, PkgCache: cl.pkgCache, LoadGuard: cl.loadGuard, } diff --git a/pkg/lint/linter/context.go b/pkg/lint/linter/context.go index 9f29b5c4c884..6986b6231477 100644 --- a/pkg/lint/linter/context.go +++ b/pkg/lint/linter/context.go @@ -7,7 +7,6 @@ import ( "github.com/golangci/golangci-lint/internal/cache" "github.com/golangci/golangci-lint/pkg/config" - "github.com/golangci/golangci-lint/pkg/fsutils" "github.com/golangci/golangci-lint/pkg/goanalysis/load" "github.com/golangci/golangci-lint/pkg/logutils" ) @@ -20,9 +19,8 @@ type Context struct { // version for each of packages OriginalPackages []*packages.Package - Cfg *config.Config - FileCache *fsutils.FileCache - Log logutils.Log + Cfg *config.Config + Log logutils.Log PkgCache *cache.Cache LoadGuard *load.Guard