Skip to content

Commit

Permalink
refactor: remove useless call to fileCache
Browse files Browse the repository at this point in the history
  • Loading branch information
ldez committed Jan 12, 2025
1 parent b0a1ae1 commit 7f854b3
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 45 deletions.
2 changes: 1 addition & 1 deletion pkg/commands/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
57 changes: 23 additions & 34 deletions pkg/golinters/misspell/misspell.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"go/ast"
"go/token"
"os"
"strings"
"unicode"

Expand All @@ -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) {
Expand Down Expand Up @@ -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)
}
Expand Down
8 changes: 2 additions & 6 deletions pkg/lint/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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,
}
Expand All @@ -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,
}
Expand Down
6 changes: 2 additions & 4 deletions pkg/lint/linter/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -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
Expand Down

0 comments on commit 7f854b3

Please sign in to comment.