diff --git a/.version b/.version index 24cb2fb1f..225ac933b 100644 --- a/.version +++ b/.version @@ -1 +1 @@ -0.2.766 \ No newline at end of file +0.2.768 \ No newline at end of file diff --git a/cmd/templ/imports/process.go b/cmd/templ/imports/process.go index 12e88cf43..d3ccbe770 100644 --- a/cmd/templ/imports/process.go +++ b/cmd/templ/imports/process.go @@ -137,16 +137,16 @@ func Process(t parser.TemplateFile) (parser.TemplateFile, error) { return t, nil } -func getImportDetails(imp *ast.ImportSpec) (name, path string, err error) { +func getImportDetails(imp *ast.ImportSpec) (name, importPath string, err error) { if imp.Name != nil { name = imp.Name.Name } if imp.Path != nil { - path, err = strconv.Unquote(imp.Path.Value) + importPath, err = strconv.Unquote(imp.Path.Value) if err != nil { err = fmt.Errorf("failed to unquote package path %s: %w", imp.Path.Value, err) return } } - return name, path, nil + return name, importPath, nil } diff --git a/cmd/templ/imports/process_test.go b/cmd/templ/imports/process_test.go index 87468b6bb..7246527fc 100644 --- a/cmd/templ/imports/process_test.go +++ b/cmd/templ/imports/process_test.go @@ -2,10 +2,13 @@ package imports import ( "bytes" + "os" + "path" "path/filepath" "strings" "testing" + "github.com/a-h/templ/cmd/templ/testproject" "github.com/a-h/templ/parser/v2" "github.com/google/go-cmp/cmp" "golang.org/x/tools/txtar" @@ -60,3 +63,92 @@ func clean(b []byte) string { b = bytes.TrimSuffix(b, []byte("\n")) return string(b) } + +func TestImport(t *testing.T) { + if testing.Short() { + t.Skip("skipping test in short mode.") + return + } + + tests := []struct { + name string + src string + assertions func(t *testing.T, updated string) + }{ + { + name: "un-named imports are removed", + src: `package main + +import "fmt" +import "github.com/a-h/templ/cmd/templ/testproject/css-classes" + +templ Page(count int) { + { fmt.Sprintf("%d", count) } + { cssclasses.Header } +} +`, + assertions: func(t *testing.T, updated string) { + if count := strings.Count(updated, "github.com/a-h/templ/cmd/templ/testproject/css-classes"); count != 0 { + t.Errorf("expected un-named import to be removed, but got %d instance of it", count) + } + }, + }, + { + name: "named imports are retained", + src: `package main + +import "fmt" +import cssclasses "github.com/a-h/templ/cmd/templ/testproject/css-classes" + +templ Page(count int) { + { fmt.Sprintf("%d", count) } + { cssclasses.Header } +} +`, + assertions: func(t *testing.T, updated string) { + if count := strings.Count(updated, "cssclasses \"github.com/a-h/templ/cmd/templ/testproject/css-classes\""); count != 1 { + t.Errorf("expected named import to be retained, got %d instances of it", count) + } + if count := strings.Count(updated, "github.com/a-h/templ/cmd/templ/testproject/css-classes"); count != 1 { + t.Errorf("expected one import, got %d", count) + } + }, + }, + } + + for _, test := range tests { + // Create test project. + dir, err := testproject.Create("github.com/a-h/templ/cmd/templ/testproject") + if err != nil { + t.Fatalf("failed to create test project: %v", err) + } + defer os.RemoveAll(dir) + + // Load the templates.templ file. + filePath := path.Join(dir, "templates.templ") + err = os.WriteFile(filePath, []byte(test.src), 0660) + if err != nil { + t.Fatalf("failed to write file: %v", err) + } + + // Parse the new file. + template, err := parser.Parse(filePath) + if err != nil { + t.Fatalf("failed to parse %v", err) + } + template.Filepath = filePath + tf, err := Process(template) + if err != nil { + t.Fatalf("failed to process file: %v", err) + } + + // Write it back out after processing. + buf := new(strings.Builder) + if err := tf.Write(buf); err != nil { + t.Fatalf("failed to write template file: %v", err) + } + + // Assert. + test.assertions(t, buf.String()) + } +} diff --git a/cmd/templ/testproject/testdata/css-classes/classes.go b/cmd/templ/testproject/testdata/css-classes/classes.go new file mode 100644 index 000000000..3cedc02fb --- /dev/null +++ b/cmd/templ/testproject/testdata/css-classes/classes.go @@ -0,0 +1,3 @@ +package cssclasses + +const Header = "header" diff --git a/cmd/templ/testproject/testproject.go b/cmd/templ/testproject/testproject.go index b1a553d83..f7d9f1748 100644 --- a/cmd/templ/testproject/testproject.go +++ b/cmd/templ/testproject/testproject.go @@ -22,6 +22,12 @@ func Create(moduleRoot string) (dir string, err error) { return dir, fmt.Errorf("failed to read embedded dir: %w", err) } for _, file := range files { + if file.IsDir() { + if err = os.MkdirAll(filepath.Join(dir, file.Name()), 0777); err != nil { + return dir, fmt.Errorf("failed to create dir: %w", err) + } + continue + } src := filepath.Join("testdata", file.Name()) data, err := testdata.ReadFile(src) if err != nil { @@ -38,6 +44,22 @@ func Create(moduleRoot string) (dir string, err error) { return dir, fmt.Errorf("failed to copy file: %w", err) } } + files, err = testdata.ReadDir("testdata/css-classes") + if err != nil { + return dir, fmt.Errorf("failed to read embedded dir: %w", err) + } + for _, file := range files { + src := filepath.Join("testdata", "css-classes", file.Name()) + data, err := testdata.ReadFile(src) + if err != nil { + return dir, fmt.Errorf("failed to read file: %w", err) + } + target := filepath.Join(dir, "css-classes", file.Name()) + err = os.WriteFile(target, data, 0660) + if err != nil { + return dir, fmt.Errorf("failed to copy file: %w", err) + } + } return dir, nil } diff --git a/go.mod b/go.mod index 8708add70..aac48daa3 100644 --- a/go.mod +++ b/go.mod @@ -19,9 +19,9 @@ require ( go.lsp.dev/jsonrpc2 v0.10.0 go.lsp.dev/uri v0.3.0 go.uber.org/zap v1.27.0 - golang.org/x/mod v0.17.0 - golang.org/x/sync v0.3.0 - golang.org/x/tools v0.13.0 + golang.org/x/mod v0.20.0 + golang.org/x/sync v0.8.0 + golang.org/x/tools v0.24.0 ) require ( @@ -33,8 +33,8 @@ require ( github.com/stretchr/testify v1.8.4 // indirect go.lsp.dev/pkg v0.0.0-20210717090340-384b27a52fb2 // indirect go.uber.org/multierr v1.11.0 // indirect - golang.org/x/net v0.24.0 // indirect - golang.org/x/sys v0.21.0 // indirect + golang.org/x/net v0.28.0 // indirect + golang.org/x/sys v0.23.0 // indirect ) // replace github.com/a-h/parse => /Users/adrian/github.com/a-h/parse diff --git a/go.sum b/go.sum index cfc3b4216..900708233 100644 --- a/go.sum +++ b/go.sum @@ -58,19 +58,19 @@ go.uber.org/zap v1.27.0/go.mod h1:GB2qFLM7cTU87MWRP2mPIjqfIDnGu+VIO4V/SdhGo2E= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4= -golang.org/x/mod v0.17.0 h1:zY54UmvipHiNd+pm+m0x9KhZ9hl1/7QNMyxXbc6ICqA= -golang.org/x/mod v0.17.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c= +golang.org/x/mod v0.20.0 h1:utOm6MM3R3dnawAiJgn0y+xvuYRsm1RKM/4giyfDgV0= +golang.org/x/mod v0.20.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c= golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= golang.org/x/net v0.0.0-20210916014120-12bc252f5db8/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c= golang.org/x/net v0.7.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs= -golang.org/x/net v0.24.0 h1:1PcaxkF854Fu3+lvBIx5SYn9wRlBzzcnHZSiaFFAb0w= -golang.org/x/net v0.24.0/go.mod h1:2Q7sJY5mzlzWjKtYUEXSlBWCdyaioyXzRB2RtU8KVE8= +golang.org/x/net v0.28.0 h1:a9JDOJc5GMUJ0+UDqmLT86WiEy7iWyIhz8gz8E4e5hE= +golang.org/x/net v0.28.0/go.mod h1:yqtgsTWOOnlGLG9GFRrK3++bGOUEkNBoHZc8MEDWPNg= golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= -golang.org/x/sync v0.3.0 h1:ftCYgMx6zT/asHUrPw8BLLscYtGznsLAnjq5RH9P66E= -golang.org/x/sync v0.3.0/go.mod h1:FU7BRWz2tNW+3quACPkgCx/L+uEAv1htQ0V83Z9Rj+Y= +golang.org/x/sync v0.8.0 h1:3NFvSEYkUoMifnESzZl15y791HH1qU2xm6eCJU5ZPXQ= +golang.org/x/sync v0.8.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= @@ -80,8 +80,8 @@ golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBc golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.21.0 h1:rF+pYz3DAGSQAxAu1CbC7catZg4ebC4UIeIhKxBZvws= -golang.org/x/sys v0.21.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +golang.org/x/sys v0.23.0 h1:YfKFowiIMvtgl1UERQoTPPToxltDeZfbj4H7dVUCwmM= +golang.org/x/sys v0.23.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k= @@ -93,8 +93,8 @@ golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8= golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc= -golang.org/x/tools v0.13.0 h1:Iey4qkscZuv0VvIt8E0neZjtPVQFSc870HQ448QgEmQ= -golang.org/x/tools v0.13.0/go.mod h1:HvlwmtVNQAhOuCjW7xxvovg8wbNq7LwfXh/k7wXUl58= +golang.org/x/tools v0.24.0 h1:J1shsA93PJUEVaUSaay7UXAyE8aimq3GW0pjlolpa24= +golang.org/x/tools v0.24.0/go.mod h1:YhNqVBIfWHdzvTLs0d8LCuMhkKUgSUKldakyV7W/WDQ= golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= diff --git a/gomod2nix.toml b/gomod2nix.toml index d572a5f19..e19fc06f2 100644 --- a/gomod2nix.toml +++ b/gomod2nix.toml @@ -74,17 +74,17 @@ schema = 3 version = "v1.27.0" hash = "sha256-8655KDrulc4Das3VRduO9MjCn8ZYD5WkULjCvruaYsU=" [mod."golang.org/x/mod"] - version = "v0.17.0" - hash = "sha256-CLaPeF6uTFuRDv4oHwOQE6MCMvrzkUjWN3NuyywZjKU=" + version = "v0.20.0" + hash = "sha256-nXYnY2kpbVkaZ/7Mf7FmxwGDX7N4cID3gKjGghmVRp4=" [mod."golang.org/x/net"] - version = "v0.24.0" - hash = "sha256-w1c21ljta5wNIyel9CSIn/crPzwOCRofNKhqmfs4aEQ=" + version = "v0.28.0" + hash = "sha256-WdH/mgsX/CB+CiYtXEwJAXHN8FgtW2YhFcWwrrHNBLo=" [mod."golang.org/x/sync"] - version = "v0.3.0" - hash = "sha256-bCJKLvwExhYacH2ZrWlZ38lr1d6oNenNt2m1QqDCs0o=" + version = "v0.8.0" + hash = "sha256-usvF0z7gq1vsX58p4orX+8WHlv52pdXgaueXlwj2Wss=" [mod."golang.org/x/sys"] - version = "v0.21.0" - hash = "sha256-gapzPWuEqY36V6W2YhIDYR49sEvjJRd7bSuf9K1f4JY=" + version = "v0.23.0" + hash = "sha256-tC6QVLu72bADgINz26FUGdmYqKgsU45bHPg7sa0ZV7w=" [mod."golang.org/x/tools"] - version = "v0.13.0" - hash = "sha256-OCgLOwia8fNHxfdogXVApf0/qK6jE2ukegOx7lkOzfo=" + version = "v0.24.0" + hash = "sha256-2LBEW//aW8qrHc26F6Ma7CsYJRaCALfi0xQl2KgWems="