catenacyber / perfsprint

Golang linter to use strconv
MIT License
19 stars 2 forks source link

`--fix` should fix imports #14

Closed Jorropo closed 8 months ago

Jorropo commented 8 months ago

Would be nice if it would conditionally remove fmt and add errors, strconv, ... as well as reorder the import std block based on if fmt isn't needed anymore, and if errors, strconv, ... weren't present before. Current alternative is:

perfsprint --fix ./...
find -type f -name "*.go" | parallel goimports -w {}
go fmt ./...
catenacyber commented 8 months ago

I totally agree.

Would you know the best way to code it ?

Jorropo commented 8 months ago

A naive implementation would parse import AST nodes and add them in a list. While traversing count usage of fmt and others, track deletions and additions. And regenerate the import AST node at the end (while using counts to add and remove entries as needed).

Counting the usages is gonna be the trickest part. Since you can do nasty things like shadowing package selectors.

Alternatively you can vendor golang.org/x/tools/internal/imports and run it after your current code.

catenacyber commented 8 months ago

Tried to vendor golang.org/x/tools/imports but it does not work because we need to apply the fmt fixes before running goimports (aka imports.Process ) and singleChecker (or multiChecker) calls os.Exit right after applying fixes...

Jorropo commented 8 months ago

Thx for fixing this :heart: