1
0
mirror of https://github.com/mgechev/revive.git synced 2025-11-23 22:04: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

@@ -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()