Skip to content

Commit e47f4c5

Browse files
authored
feat: retain import grouping during fmt (#1037)
1 parent e54517e commit e47f4c5

File tree

3 files changed

+72
-7
lines changed

3 files changed

+72
-7
lines changed

cmd/templ/imports/process.go

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -104,21 +104,33 @@ func Process(t parser.TemplateFile) (parser.TemplateFile, error) {
104104
if err := eg.Wait(); err != nil {
105105
return t, err
106106
}
107-
108-
// Delete all the existing imports.
107+
// Delete unused imports.
109108
for _, imp := range firstGoNodeInTemplate.Imports {
110-
name, path, err := getImportDetails(imp)
111-
if err != nil {
112-
return t, err
109+
if !containsImport(updatedImports, imp) {
110+
name, path, err := getImportDetails(imp)
111+
if err != nil {
112+
return t, err
113+
}
114+
astutil.DeleteNamedImport(fset, firstGoNodeInTemplate, name, path)
113115
}
114-
astutil.DeleteNamedImport(fset, firstGoNodeInTemplate, name, path)
115116
}
116117
// Add imports, if there are any to add.
117118
for _, imp := range updatedImports {
118-
name, path, err := getImportDetails(imp)
119+
if !containsImport(firstGoNodeInTemplate.Imports, imp) {
120+
name, path, err := getImportDetails(imp)
121+
if err != nil {
122+
return t, err
123+
}
124+
astutil.AddNamedImport(fset, firstGoNodeInTemplate, name, path)
125+
}
126+
}
127+
// Edge case: reinsert the import to use import syntax without parentheses.
128+
if len(firstGoNodeInTemplate.Imports) == 1 {
129+
name, path, err := getImportDetails(firstGoNodeInTemplate.Imports[0])
119130
if err != nil {
120131
return t, err
121132
}
133+
astutil.DeleteNamedImport(fset, firstGoNodeInTemplate, name, path)
122134
astutil.AddNamedImport(fset, firstGoNodeInTemplate, name, path)
123135
}
124136
// Write out the Go code with the imports.
@@ -150,3 +162,13 @@ func getImportDetails(imp *ast.ImportSpec) (name, importPath string, err error)
150162
}
151163
return name, importPath, nil
152164
}
165+
166+
func containsImport(imports []*ast.ImportSpec, spec *ast.ImportSpec) bool {
167+
for _, imp := range imports {
168+
if imp.Path.Value == spec.Path.Value {
169+
return true
170+
}
171+
}
172+
173+
return false
174+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
-- fmt.templ --
2+
package test
3+
4+
import (
5+
"strings"
6+
"fmt"
7+
8+
"strconv"
9+
)
10+
11+
var _, _ = fmt.Print(strings.Contains(strconv.Quote("Hello"), ""))
12+
-- fmt.templ --
13+
package test
14+
15+
import (
16+
"fmt"
17+
"strings"
18+
19+
"strconv"
20+
)
21+
22+
var _, _ = fmt.Print(strings.Contains(strconv.Quote("Hello"), ""))
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
-- fmt.templ --
2+
package test
3+
4+
import (
5+
"fmt"
6+
7+
8+
"strconv"
9+
)
10+
11+
var _, _ = fmt.Print(strconv.Quote("Hello"))
12+
-- fmt.templ --
13+
package test
14+
15+
import (
16+
"fmt"
17+
18+
"strconv"
19+
)
20+
21+
var _, _ = fmt.Print(strconv.Quote("Hello"))

0 commit comments

Comments
 (0)