-
Notifications
You must be signed in to change notification settings - Fork 285
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add support for import-alias-naming rule (#881)
- Loading branch information
1 parent
0357df7
commit f900b6c
Showing
7 changed files
with
145 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package rule | ||
|
||
import ( | ||
"fmt" | ||
"regexp" | ||
"sync" | ||
|
||
"github.com/mgechev/revive/lint" | ||
) | ||
|
||
// ImportAliasNamingRule lints import alias naming. | ||
type ImportAliasNamingRule struct { | ||
configured bool | ||
namingRuleRegexp *regexp.Regexp | ||
sync.Mutex | ||
} | ||
|
||
const defaultNamingRule = "^[a-z][a-z0-9]{0,}$" | ||
|
||
var defaultNamingRuleRegexp = regexp.MustCompile(defaultNamingRule) | ||
|
||
func (r *ImportAliasNamingRule) configure(arguments lint.Arguments) { | ||
r.Lock() | ||
defer r.Unlock() | ||
if r.configured { | ||
return | ||
} | ||
|
||
if len(arguments) < 1 { | ||
r.namingRuleRegexp = defaultNamingRuleRegexp | ||
return | ||
} | ||
|
||
namingRule, ok := arguments[0].(string) // Alt. non panicking version | ||
if !ok { | ||
panic(fmt.Sprintf("Invalid argument '%v' for 'import-alias-naming' rule. Expecting string, got %T", arguments[0], arguments[0])) | ||
} | ||
|
||
var err error | ||
r.namingRuleRegexp, err = regexp.Compile(namingRule) | ||
if err != nil { | ||
panic(fmt.Sprintf("Invalid argument to the import-alias-naming rule. Expecting %q to be a valid regular expression, got: %v", namingRule, err)) | ||
} | ||
} | ||
|
||
// Apply applies the rule to given file. | ||
func (r *ImportAliasNamingRule) Apply(file *lint.File, arguments lint.Arguments) []lint.Failure { | ||
r.configure(arguments) | ||
|
||
var failures []lint.Failure | ||
|
||
for _, is := range file.AST.Imports { | ||
path := is.Path | ||
if path == nil { | ||
continue | ||
} | ||
|
||
alias := is.Name | ||
if alias == nil || alias.Name == "_" { | ||
continue | ||
} | ||
|
||
if !r.namingRuleRegexp.MatchString(alias.Name) { | ||
failures = append(failures, lint.Failure{ | ||
Confidence: 1, | ||
Failure: fmt.Sprintf("import name (%s) must match the regular expression: %s", alias.Name, r.namingRuleRegexp.String()), | ||
Node: alias, | ||
Category: "imports", | ||
}) | ||
} | ||
} | ||
|
||
return failures | ||
} | ||
|
||
// Name returns the rule name. | ||
func (*ImportAliasNamingRule) Name() string { | ||
return "import-alias-naming" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/mgechev/revive/lint" | ||
"github.com/mgechev/revive/rule" | ||
) | ||
|
||
func TestImportAliasNaming(t *testing.T) { | ||
testRule(t, "import-alias-naming", &rule.ImportAliasNamingRule{}) | ||
testRule(t, "import-alias-naming-custom-config", &rule.ImportAliasNamingRule{}, &lint.RuleConfig{ | ||
Arguments: []any{`^[a-z]+$`}, | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package fixtures | ||
|
||
import ( | ||
_ "strings" | ||
bar_foo "strings" // MATCH /import name (bar_foo) must match the regular expression: ^[a-z]+$/ | ||
fooBAR "strings" // MATCH /import name (fooBAR) must match the regular expression: ^[a-z]+$/ | ||
v1 "strings" // MATCH /import name (v1) must match the regular expression: ^[a-z]+$/ | ||
magical "magic/hat" | ||
) | ||
|
||
func somefunc() { | ||
fooBAR.Clone("") | ||
bar_foo.Clone("") | ||
v1.Clone("") | ||
magical.Clone("") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package fixtures | ||
|
||
import ( | ||
_ "strings" | ||
bar_foo "strings" // MATCH /import name (bar_foo) must match the regular expression: ^[a-z][a-z0-9]{0,}$/ | ||
fooBAR "strings" // MATCH /import name (fooBAR) must match the regular expression: ^[a-z][a-z0-9]{0,}$/ | ||
v1 "strings" | ||
magical "magic/hat" | ||
) | ||
|
||
func somefunc() { | ||
fooBAR.Clone("") | ||
bar_foo.Clone("") | ||
v1.Clone("") | ||
magical.Clone("") | ||
} |