Skip to content

Commit

Permalink
feat: ignore empty imports
Browse files Browse the repository at this point in the history
  • Loading branch information
haunt98 committed Jan 17, 2023
1 parent 5209660 commit b17a39b
Showing 1 changed file with 11 additions and 11 deletions.
22 changes: 11 additions & 11 deletions internal/imports/formatter.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ var (
ErrNotGoFile = errors.New("not go file")
ErrGoGeneratedFile = errors.New("go generated file")
ErrAlreadyFormatted = errors.New("already formatted")
ErrEmptyImport = errors.New("empty import")
ErrGoModNotExist = errors.New("go mod not exist")
ErrGoModEmptyModule = errors.New("go mod empty module")
)
Expand Down Expand Up @@ -201,16 +202,11 @@ func (ft *Formatter) formatFile(path string) error {
return nil
}

// Copy from goimports, gofumpt, goimports-reviser
// First parse ast
//
// # Then group imports
//
// # Then format imports
//
// # Then update ast decls
//
// Then print ast
// Copy from goimports, gofumpt, goimports-reviser.
// First parse ast.
// Then group imports.
// Then format imports.
// Then print.
func (ft *Formatter) formatImports(
path string,
pathBytes []byte,
Expand Down Expand Up @@ -238,6 +234,9 @@ func (ft *Formatter) formatImports(
if err != nil {
return nil, fmt.Errorf("decorator: failed to parse file [%s]: %w", path, err)
}
if len(dstFile.Imports) == 0 {
return nil, ErrEmptyImport
}
ft.logDSTImportSpecs("formatImports: dstImportSpecs", dstFile.Imports)

groupedDSTImportSpecs, err := ft.groupDSTImportSpecs(
Expand Down Expand Up @@ -406,5 +405,6 @@ func (ft *Formatter) moduleName(path string) (string, error) {
func (ft *Formatter) isIgnoreError(err error) bool {
return errors.Is(err, ErrNotGoFile) ||
errors.Is(err, ErrGoGeneratedFile) ||
errors.Is(err, ErrAlreadyFormatted)
errors.Is(err, ErrAlreadyFormatted) ||
errors.Is(err, ErrEmptyImport)
}

0 comments on commit b17a39b

Please sign in to comment.