Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor with AST #70

Merged
merged 4 commits into from
Jul 8, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
fix no imports
Signed-off-by: Loong Dai <loong.dai@intel.com>
  • Loading branch information
daixiang0 committed Jul 8, 2022
commit 9349c88f1609b0e71ec5cac5576b1a413ac25869
4 changes: 4 additions & 0 deletions pkg/gci/gci.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package gci

import (
"bytes"
"errors"
"fmt"
"os"
"sort"
Expand Down Expand Up @@ -123,6 +124,9 @@ func LoadFormatGoFile(file io.FileObj, cfg config.Config) (src, dist []byte, err

imports, headEnd, tailStart, tailEnd, err := parse.ParseFile(src)
if err != nil {
if errors.Is(err, parse.NoImportError{}) {
return src, src, nil
}
return nil, nil, err
}

Expand Down
15 changes: 15 additions & 0 deletions pkg/parse/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@ func ParseFile(src []byte) (ImportList, int, int, int, error) {
return nil, 0, 0, 0, err
}

if len(f.Imports) == 0 {
return nil, 0, 0, 0, NoImportError{}
}

var data ImportList
for _, imp := range f.Imports {
start, end, name := getImports(imp)
Expand Down Expand Up @@ -113,3 +117,14 @@ func IsGeneratedFileByComment(in string) bool {

return false
}

type NoImportError struct{}

func (n NoImportError) Error() string {
return "No imports"
}

func (i NoImportError) Is(err error) bool {
_, ok := err.(NoImportError)
return ok
}