Skip to content
This repository has been archived by the owner on Mar 27, 2024. It is now read-only.

Commit

Permalink
made imports fixer supports name imports
Browse files Browse the repository at this point in the history
  • Loading branch information
sio4 committed Sep 20, 2022
1 parent 638212a commit 33dc637
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 3 deletions.
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,13 @@ require (
github.com/gobuffalo/grift v1.5.1
github.com/gobuffalo/logger v1.0.6
github.com/gobuffalo/meta v0.3.2
github.com/gobuffalo/packd v1.0.1
github.com/gobuffalo/plush/v4 v4.1.14
github.com/gobuffalo/pop/v6 v6.0.6
github.com/gobuffalo/refresh v1.13.1
github.com/google/go-cmp v0.5.8
github.com/psanford/memfs v0.0.0-20210214183328-a001468d78ef
github.com/sirupsen/logrus v1.8.1
github.com/sirupsen/logrus v1.9.0
github.com/spf13/cobra v1.5.0
github.com/spf13/pflag v1.0.5
github.com/spf13/viper v1.12.0
Expand Down
4 changes: 3 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -514,8 +514,9 @@ github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPx
github.com/sirupsen/logrus v1.4.1/go.mod h1:ni0Sbl8bgC9z8RoU9G6nDWqqs/fq4eDPysMBDgk/93Q=
github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE=
github.com/sirupsen/logrus v1.6.0/go.mod h1:7uNnSEd1DgxDLC74fIahvMZmmYsHGZGEOFrfsX/uA88=
github.com/sirupsen/logrus v1.8.1 h1:dJKuHgqk1NNQlqoA6BTlM1Wf9DOH3NBjQyu0h9+AZZE=
github.com/sirupsen/logrus v1.8.1/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0=
github.com/sirupsen/logrus v1.9.0 h1:trlNQbNUG3OdDrDil03MCb1H2o9nJ1x4/5LYw7byDE0=
github.com/sirupsen/logrus v1.9.0/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
github.com/sourcegraph/annotate v0.0.0-20160123013949-f4cad6c6324d h1:yKm7XZV6j9Ev6lojP2XaIshpT4ymkqhMeSghO5Ps00E=
github.com/sourcegraph/annotate v0.0.0-20160123013949-f4cad6c6324d/go.mod h1:UdhH50NIW0fCiwBSr0co2m7BnFLdv4fQTgdqdJTHFeE=
github.com/sourcegraph/syntaxhighlight v0.0.0-20170531221838-bd320f5d308e h1:qpG93cPwA5f7s/ZPBJnGOYQNK/vKsaDaseuKT5Asee8=
Expand Down Expand Up @@ -806,6 +807,7 @@ golang.org/x/sys v0.0.0-20220328115105-d36c6a25d886/go.mod h1:oPkhp1MJrh7nUepCBc
golang.org/x/sys v0.0.0-20220412211240-33da011f77ad/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220502124256-b6088ccd6cba/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f h1:v4INt8xihDGvnrfjMDVXGxw9wrfxYyCjk0KbXjhR55s=
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201117132131-f5c789dd3221/go.mod h1:Nr5EML6q2oocZ2LXRh80K7BxOlk5/8JxuGnuhpl+muw=
Expand Down
2 changes: 1 addition & 1 deletion internal/genny/fix/imports.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ func rewriteFile(file genny.File) error {

astutil.DeleteNamedImport(fset, f, name, key)
if value != "" {
astutil.AddImport(fset, f, value)
astutil.AddNamedImport(fset, f, names[key], value)
}

changed = true
Expand Down
62 changes: 62 additions & 0 deletions internal/genny/fix/imports_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ import (
"os"
"path/filepath"
"strconv"
"strings"
"testing"

"github.com/gobuffalo/genny/v2/gentest"
"github.com/gobuffalo/meta"
"github.com/gobuffalo/packd"
"github.com/stretchr/testify/require"
)

Expand Down Expand Up @@ -61,3 +63,63 @@ func Test_Imports(t *testing.T) {
})
}
}

func TestRewriteFile_Simple(t *testing.T) {
r := require.New(t)
r.Nil(nil)

data := `package main
import "github.com/markbates/grift"
`
file, err := packd.NewFile("test.go", strings.NewReader(data))
r.NoError(err)
r.NotNil(file)

err = rewriteFile(file)
r.NoError(err)
r.Equal(`package main
import "github.com/gobuffalo/grift"
`, file.String())
}

func TestRewriteFile_Named(t *testing.T) {
r := require.New(t)
r.Nil(nil)

data := `package main
import gr "github.com/markbates/grift"
`
file, err := packd.NewFile("test.go", strings.NewReader(data))
r.NoError(err)
r.NotNil(file)

err = rewriteFile(file)
r.NoError(err)
r.Equal(`package main
import gr "github.com/gobuffalo/grift"
`, file.String())
}

func TestRewriteFile_DotAlso(t *testing.T) {
r := require.New(t)
r.Nil(nil)

data := `package main
import . "github.com/markbates/grift"
`
file, err := packd.NewFile("test.go", strings.NewReader(data))
r.NoError(err)
r.NotNil(file)

err = rewriteFile(file)
r.NoError(err)
r.Equal(`package main
import . "github.com/gobuffalo/grift"
`, file.String())
}

0 comments on commit 33dc637

Please sign in to comment.