Closed Jorropo closed 8 months ago
I totally agree.
Would you know the best way to code it ?
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.
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...
Thx for fixing this :heart:
Would be nice if it would conditionally remove
fmt
and adderrors
,strconv
, ... as well as reorder the import std block based on iffmt
isn't needed anymore, and iferrors
,strconv
, ... weren't present before. Current alternative is: