Repository: googleapis/api-linter
Pull request #159 (2019-09-17 16:14:23)

Checked if rule name alias is disabled in comments.


Patch:
diff --git a/lint/lint.go b/lint/lint.go
index 725b019..bde96da 100644
--- a/lint/lint.go
+++ b/lint/lint.go
@@ -79,7 +79,7 @@ func (l *Linter) lintFileDescriptor(fd *desc.FileDescriptor) (Response, error) {
 		if !config.Disabled {
 			if problems, err := l.runAndRecoverFromPanics(rule, fd); err == nil {
 				for _, p := range problems {
-					if ruleIsEnabled(rule, p.Descriptor) {
+					if ruleIsEnabled(rule, p.Descriptor, aliasMap) {
 						p.RuleID = rule.GetName()
 						p.Category = config.Category
 						resp.Problems = append(resp.Problems, p)
diff --git a/lint/rule.go b/lint/rule.go
index ca9e47e..5ad6b9c 100644
--- a/lint/rule.go
+++ b/lint/rule.go
@@ -239,21 +239,31 @@ func (r *EnumRule) Lint(fd *desc.FileDescriptor) []Problem {
 
 // ruleIsEnabled returns true if the rule is enabled (not disabled by the comments
 // for the given descriptor or its file), false otherwise.
-func ruleIsEnabled(rule protoRule, d desc.Descriptor) bool {
-	directive := fmt.Sprintf("api-linter: %s=disabled", rule.GetName())
+func ruleIsEnabled(rule protoRule, d desc.Descriptor, aliasMap map[string]string) bool {
+	// Some rules have a legacy name. We add it to the check list.
+	ruleName := string(rule.GetName())
+	names := []string{ruleName}
+	if alias := aliasMap[ruleName]; alias != "" {
+		names = append(names, alias)
+	}
 
-	// If the comments above the descriptor disable the rule,
-	// return false.
-	if sourceInfo := d.GetSourceInfo(); sourceInfo != nil {
-		if strings.Contains(sourceInfo.GetLeadingComments(), directive) {
-			return false
-		}
+	directives := []string{}
+	for _, name := range names {
+		directives = append(directives, fmt.Sprintf("api-linter: %s=disabled", name))
 	}
 
-	// The rule may also be disabled at the file level.
-	// If it is, return false.
-	if strings.Contains(fileHeader(d.GetFile()), directive) {
-		return false
+	for _, directive := range directives {
+		// The rule may be disabled in the comments above the descriptor.
+		if sourceInfo := d.GetSourceInfo(); sourceInfo != nil {
+			if strings.Contains(sourceInfo.GetLeadingComments(), directive) {
+				return false
+			}
+		}
+
+		// The rule may also be disabled at the file level.
+		if strings.Contains(fileHeader(d.GetFile()), directive) {
+			return false
+		}
 	}
 
 	// The rule is enabled.
diff --git a/lint/rule_aliases.go b/lint/rule_aliases.go
new file mode 100644
index 0000000..6f8fabd
--- /dev/null
+++ b/lint/rule_aliases.go
@@ -0,0 +1,6 @@
+package lint
+
+// aliasMap stores legacy names for some rules.
+var aliasMap = map[string]string{
+	"core::0140::lower-snake": "naming-format",
+}


Test patch:
diff --git a/lint/rule_test.go b/lint/rule_test.go
index e459619..38c2adb 100644
--- a/lint/rule_test.go
+++ b/lint/rule_test.go
@@ -291,6 +291,10 @@ func TestRuleIsEnabled(t *testing.T) {
 		},
 	}
 
+	aliases := map[string]string{
+		"test": "alias",
+	}
+
 	// Create appropriate test permutations.
 	tests := []struct {
 		testName       string
@@ -301,6 +305,8 @@ func TestRuleIsEnabled(t *testing.T) {
 		{"Enabled", "", "", true},
 		{"FileDisabled", "api-linter: test=disabled", "", false},
 		{"MessageDisabled", "", "api-linter: test=disabled", false},
+		{"NameNotMatch", "", "api-linter: other=disabled", true},
+		{"AliasDisabled", "", "api-linter: alias=disabled", false},
 	}
 
 	// Run the specific tests individually.
@@ -316,7 +322,7 @@ func TestRuleIsEnabled(t *testing.T) {
 			if err != nil {
 				t.Fatalf("Error building test message")
 			}
-			if got, want := ruleIsEnabled(rule, f.GetMessageTypes()[0]), test.enabled; got != want {
+			if got, want := ruleIsEnabled(rule, f.GetMessageTypes()[0], aliases), test.enabled; got != want {
 				t.Errorf("Expected the test rule to return %v from ruleIsEnabled, got %v", want, got)
 			}
 		})
@@ -343,10 +349,10 @@ func TestRuleIsEnabledFirstMessage(t *testing.T) {
 	if err != nil {
 		t.Fatalf("Error building test file: %q", err)
 	}
-	if got, want := ruleIsEnabled(rule, f.GetMessageTypes()[0]), false; got != want {
+	if got, want := ruleIsEnabled(rule, f.GetMessageTypes()[0], nil), false; got != want {
 		t.Errorf("Expected the first message to return %v from ruleIsEnabled, got %v", want, got)
 	}
-	if got, want := ruleIsEnabled(rule, f.GetMessageTypes()[1]), true; got != want {
+	if got, want := ruleIsEnabled(rule, f.GetMessageTypes()[1], nil), true; got != want {
 		t.Errorf("Expected the second message to return %v from ruleIsEnabled, got %v", want, got)
 	}
 }