Skip to content

Commit

Permalink
Add -no-extra-aliases flag (#6)
Browse files Browse the repository at this point in the history
Add new flag -no-extra-aliases which if set will trigger an error
if an alias is not defined within the alias config.
  • Loading branch information
mattysweeps authored Sep 22, 2021
1 parent 841f0c0 commit 27e0a5d
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 9 deletions.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,18 @@ importas -no-unaliased \
./...
~~~~

### `-no-extra-aliases` option

By default, importas allows aliases which are not specified by `-alias` flags.
With `-no-extra-aliases` option, importas does not allow any unspecified aliases.

~~~~
importas -no-extra-aliases \
-alias knative.dev/serving/pkg/apis/autoscaling/v1alpha1:autoscalingv1alpha1 \
-alias knative.dev/serving/pkg/apis/serving/v1:servingv1 \
./...
~~~~

### Use regular expression

You can specify the package path by regular expression, and alias by regular expression replacement syntax like following snippet.
Expand Down
29 changes: 27 additions & 2 deletions analyzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,17 +81,42 @@ func visitImportSpecNode(config *Config, node *ast.ImportSpec, pass *analysis.Pa
TextEdits: findEdits(node, pass.TypesInfo.Uses, path, alias, required),
}},
})
} else if !exists && config.DisallowExtraAliases {
pass.Report(analysis.Diagnostic{
Pos: node.Pos(),
End: node.End(),
Message: fmt.Sprintf("import %q has alias %q which is not part of config", path, alias),
SuggestedFixes: []analysis.SuggestedFix{{
Message: "remove alias",
TextEdits: findEdits(node, pass.TypesInfo.Uses, path, alias, ""),
}},
})
}
}

func findEdits(node ast.Node, uses map[*ast.Ident]types.Object, importPath, original, required string) []analysis.TextEdit {
// Edit the actual import line.
importLine := strconv.Quote(importPath)
if required != "" {
importLine = required + " " + importLine
}
result := []analysis.TextEdit{{
Pos: node.Pos(),
End: node.End(),
NewText: []byte(required + " " + strconv.Quote(importPath)),
NewText: []byte(importLine),
}}

packageReplacement := required
if required == "" {
packageParts := strings.Split(importPath, "/")
if len(packageParts) != 0 {
packageReplacement = packageParts[len(packageParts)-1]
} else {
// fall back to original
packageReplacement = original
}
}

// Edit all the uses of the alias in the code.
for use, pkg := range uses {
pkgName, ok := pkg.(*types.PkgName)
Expand All @@ -108,7 +133,7 @@ func findEdits(node ast.Node, uses map[*ast.Ident]types.Object, importPath, orig
result = append(result, analysis.TextEdit{
Pos: use.Pos(),
End: use.End(),
NewText: []byte(required),
NewText: []byte(packageReplacement),
})
}

Expand Down
19 changes: 15 additions & 4 deletions analyzer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,11 @@ func TestAnalyzer(t *testing.T) {
testdata := analysistest.TestData()

testCases := []struct {
desc string
pkg string
aliases stringMap
disallowUnaliased bool
desc string
pkg string
aliases stringMap
disallowUnaliased bool
disallowExtraAliases bool
}{
{
desc: "Invalid imports",
Expand Down Expand Up @@ -64,6 +65,11 @@ func TestAnalyzer(t *testing.T) {
},
disallowUnaliased: true,
},
{
desc: "disallow extra alias mode",
pkg: "f",
disallowExtraAliases: true,
},
}

for _, test := range testCases {
Expand Down Expand Up @@ -111,6 +117,11 @@ func TestAnalyzer(t *testing.T) {
t.Fatal(err)
}

noExtraAlisesFlg := a.Flags.Lookup("no-extra-aliases")
if err := noExtraAlisesFlg.Value.Set(strconv.FormatBool(test.disallowExtraAliases)); err != nil {
t.Fatal(err)
}

analysistest.RunWithSuggestedFixes(t, testdata, a, test.pkg)
})
}
Expand Down
7 changes: 4 additions & 3 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ import (
)

type Config struct {
RequiredAlias map[string]string
Rules []*Rule
DisallowUnaliased bool
RequiredAlias map[string]string
Rules []*Rule
DisallowUnaliased bool
DisallowExtraAliases bool
}

func (c *Config) CompileRegexp() error {
Expand Down
1 change: 1 addition & 0 deletions flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ func flags(config *Config) flag.FlagSet {
fs := flag.FlagSet{}
fs.Var(stringMap(config.RequiredAlias), "alias", "required import alias in form path:alias")
fs.BoolVar(&config.DisallowUnaliased, "no-unaliased", false, "do not allow unaliased imports of aliased packages")
fs.BoolVar(&config.DisallowExtraAliases, "no-extra-aliases", false, "do not allow non-required aliases")
return fs
}

Expand Down
7 changes: 7 additions & 0 deletions testdata/src/f/f.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package main

import no_alias_in_config "fmt" // want `import "fmt" has alias "no_alias_in_config" which is not part of config`

func main() {
no_alias_in_config.Println("test")
}
7 changes: 7 additions & 0 deletions testdata/src/f/f.go.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package main

import "fmt" // want `import "fmt" has alias "no_alias_in_config" which is not part of config`

func main() {
fmt.Println("test")
}

0 comments on commit 27e0a5d

Please sign in to comment.