1
0
mirror of https://github.com/mgechev/revive.git synced 2025-10-30 23:37:49 +02:00
Do not require additional newline for disabling multiple rules
This commit is contained in:
SalvadorC
2019-01-24 18:14:43 +01:00
committed by Minko Gechev
parent e2acdb0e10
commit 202adf0786
3 changed files with 51 additions and 21 deletions

View File

@@ -0,0 +1,24 @@
package fixtures
func foo1() {
//revive:disable-next-line:var-naming
var invalid_name = 0
var invalid_name2 = 1 //revive:disable-line:var-naming
}
func foo2() {
// revive:disable-next-line:var-naming
//revive:disable
var invalid_name = 0
var invalid_name2 = 1
}
func foo3() {
//revive:enable
// revive:disable-next-line:var-naming
var invalid_name = 0
// not a valid annotation revive:disable-next-line:var-naming
var invalid_name2 = 1 // MATCH /don't use underscores in Go names; var invalid_name2 should be invalidName2/
/* revive:disable-next-line:var-naming */
var invalid_name3 = 0 // MATCH /don't use underscores in Go names; var invalid_name3 should be invalidName3/
}

View File

@@ -127,7 +127,7 @@ type enableDisableConfig struct {
}
func (f *File) disabledIntervals(rules []Rule) disabledIntervalsMap {
re := regexp.MustCompile(`^\s*revive:(enable|disable)(?:-(line|next-line))?(:)?([^\s]*)?(\s|$)`)
re := regexp.MustCompile(`^//[\s]*revive:(enable|disable)(?:-(line|next-line))?(?::([^\s]+))?[\s]*$`)
enabledDisabledRulesMap := make(map[string][]enableDisableConfig)
@@ -194,36 +194,38 @@ func (f *File) disabledIntervals(rules []Rule) disabledIntervalsMap {
}
handleComment := func(filename string, c *ast.CommentGroup, line int) {
text := c.Text()
parts := re.FindStringSubmatch(text)
if len(parts) == 0 {
return
}
comments := c.List
for _, c := range comments {
match := re.FindStringSubmatch(c.Text)
if len(match) == 0 {
return
}
ruleNames := []string{}
if len(parts) > 4 {
tempNames := strings.Split(parts[4], ",")
for _, name := range tempNames {
name = strings.Trim(name, "\n")
if len(name) > 0 {
ruleNames = append(ruleNames, name)
ruleNames := []string{}
if len(match) > 2 {
tempNames := strings.Split(match[3], ",")
for _, name := range tempNames {
name = strings.Trim(name, "\n")
if len(name) > 0 {
ruleNames = append(ruleNames, name)
}
}
}
}
// TODO: optimize
if len(ruleNames) == 0 {
for _, rule := range rules {
ruleNames = append(ruleNames, rule.Name())
// TODO: optimize
if len(ruleNames) == 0 {
for _, rule := range rules {
ruleNames = append(ruleNames, rule.Name())
}
}
}
handleRules(filename, parts[2], parts[1] == "enable", line, ruleNames)
handleRules(filename, match[2], match[1] == "enable", line, ruleNames)
}
}
comments := f.AST.Comments
for _, c := range comments {
handleComment(f.Name, c, f.ToPosition(c.Pos()).Line)
handleComment(f.Name, c, f.ToPosition(c.End()).Line)
}
return getEnabledDisabledIntervals()

View File

@@ -10,3 +10,7 @@ import (
func TestDisabledAnnotations(t *testing.T) {
testRule(t, "disable-annotations", &rule.ExportedRule{}, &lint.RuleConfig{})
}
func TestModifiedAnnotations(t *testing.T) {
testRule(t, "disable-annotations2", &rule.VarNamingRule{}, &lint.RuleConfig{})
}