Skip to content

Commit

Permalink
string sort order in imports
Browse files Browse the repository at this point in the history
  • Loading branch information
mariotoffia committed Oct 2, 2020
1 parent f54dc89 commit eb66bd2
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 additions & 3 deletions goparser/gofile.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"os"
"path/filepath"
"sort"
"strings"
)

Expand Down Expand Up @@ -59,15 +60,25 @@ func (g *GoFile) DeclImports() string {
return fmt.Sprintf(`import "%s"`, g.Imports[0].Path)
}

// Filter out any duplicate
set := make(map[string]bool)

s := "import (\n"
for _, i := range g.Imports {
if !set[i.Path] {
s += fmt.Sprintf("\t\"%s\"\n", i.Path)
set[i.Path] = true
}
}

// Sort imports
keys := make([]string, 0, len(set))
for k := range set {
keys = append(keys, k)
}
sort.Strings(keys)

s := "import (\n"
for _, k := range keys {
s += fmt.Sprintf("\t\"%s\"\n", k)
}

return s + ")"
}

0 comments on commit eb66bd2

Please sign in to comment.