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

feat: imports grouping #1037

Merged
merged 1 commit into from
Jan 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
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
36 changes: 29 additions & 7 deletions cmd/templ/imports/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,21 +104,33 @@ func Process(t parser.TemplateFile) (parser.TemplateFile, error) {
if err := eg.Wait(); err != nil {
return t, err
}

// Delete all the existing imports.
// Delete unused imports.
for _, imp := range firstGoNodeInTemplate.Imports {
name, path, err := getImportDetails(imp)
if err != nil {
return t, err
if !containsImport(updatedImports, imp) {
name, path, err := getImportDetails(imp)
if err != nil {
return t, err
}
astutil.DeleteNamedImport(fset, firstGoNodeInTemplate, name, path)
}
astutil.DeleteNamedImport(fset, firstGoNodeInTemplate, name, path)
}
// Add imports, if there are any to add.
for _, imp := range updatedImports {
name, path, err := getImportDetails(imp)
if !containsImport(firstGoNodeInTemplate.Imports, imp) {
name, path, err := getImportDetails(imp)
if err != nil {
return t, err
}
astutil.AddNamedImport(fset, firstGoNodeInTemplate, name, path)
}
}
// Edge case: reinsert the import to use import syntax without parentheses.
if len(firstGoNodeInTemplate.Imports) == 1 {
name, path, err := getImportDetails(firstGoNodeInTemplate.Imports[0])
if err != nil {
return t, err
}
astutil.DeleteNamedImport(fset, firstGoNodeInTemplate, name, path)
astutil.AddNamedImport(fset, firstGoNodeInTemplate, name, path)
}
// Write out the Go code with the imports.
Expand Down Expand Up @@ -150,3 +162,13 @@ func getImportDetails(imp *ast.ImportSpec) (name, importPath string, err error)
}
return name, importPath, nil
}

func containsImport(imports []*ast.ImportSpec, spec *ast.ImportSpec) bool {
for _, imp := range imports {
if imp.Path.Value == spec.Path.Value {
return true
}
}

return false
}
22 changes: 22 additions & 0 deletions cmd/templ/imports/testdata/groups.txtar
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
-- fmt.templ --
package test

import (
"strings"
"fmt"

"strconv"
)

var _, _ = fmt.Print(strings.Contains(strconv.Quote("Hello"), ""))
-- fmt.templ --
package test

import (
"fmt"
"strings"

"strconv"
)

var _, _ = fmt.Print(strings.Contains(strconv.Quote("Hello"), ""))
21 changes: 21 additions & 0 deletions cmd/templ/imports/testdata/groupsmanynewlines.txtar
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
-- fmt.templ --
package test

import (
"fmt"


"strconv"
)

var _, _ = fmt.Print(strconv.Quote("Hello"))
-- fmt.templ --
package test

import (
"fmt"

"strconv"
)

var _, _ = fmt.Print(strconv.Quote("Hello"))
Loading