Skip to content

Commit

Permalink
Merge pull request #7 from momotaro98/bug/fix-for-gofile-that-doesnt-…
Browse files Browse the repository at this point in the history
…have-import-decl

Fix bugs for go files that doesnt have import decl
  • Loading branch information
momotaro98 authored Feb 10, 2021
2 parents c0d2248 + 3b54e49 commit c0b44b2
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions strictgoimports.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,29 @@ func (e *Err) Error() string {
return fmt.Sprintf("Message: %s. Position: %s", e.Message, e.FileSet.Position(e.Pos).String())
}

func checkTheresImportDecl(f *ast.File) bool {
for _, d := range f.Decls {
switch v := d.(type) {
case *ast.GenDecl:
if v.Tok == token.IMPORT {
return true
}
}
}
return false
}

func Run(filePath, localPaths string) (fileSet *token.FileSet, pos []token.Pos, correctImport string, fixed []byte) {
fileSet = token.NewFileSet()
f, err := parser.ParseFile(fileSet, filePath, nil, parser.ImportsOnly)
if err != nil {
return nil, nil, "", nil
}

if !checkTheresImportDecl(f) {
return fileSet, nil, "", nil
}

realLines := buildImportLines(filePath, f, fileSet)
idealLines, idealSrc := buildIdeal(filePath, localPaths, fileSet)

Expand Down

0 comments on commit c0b44b2

Please sign in to comment.