Repository: googleapis/api-linter
Pull request #230 (2019-10-11 23:39:16)

[AIP-192] Check for presence of comments on everything.


Patch:
diff --git a/rules/aip0192/aip0192.go b/rules/aip0192/aip0192.go
index 7a6a90a..a415d26 100644
--- a/rules/aip0192/aip0192.go
+++ b/rules/aip0192/aip0192.go
@@ -24,6 +24,7 @@ import (
 // AddRules adds all of the AIP-192 rules to the provided registry.
 func AddRules(r lint.RuleRegistry) {
 	r.Register(
+		hasComments,
 		onlyLeadingComments,
 	)
 }
diff --git a/rules/aip0192/has_comments.go b/rules/aip0192/has_comments.go
new file mode 100644
index 0000000..e789ce8
--- /dev/null
+++ b/rules/aip0192/has_comments.go
@@ -0,0 +1,39 @@
+// Copyright 2019 Google LLC
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//     https://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package aip0192
+
+import (
+	"fmt"
+
+	"github.com/googleapis/api-linter/lint"
+	"github.com/jhump/protoreflect/desc"
+)
+
+// hasComments complains if there is no comment above something.
+var hasComments = &lint.DescriptorRule{
+	Name: lint.NewRuleName("core", "0192", "comments", "present"),
+	URI:  "https://aip.dev/192",
+	LintDescriptor: func(d desc.Descriptor) (problems []lint.Problem) {
+		comment := separateInternalComments(d.GetSourceInfo().GetLeadingComments())
+		if len(comment.External) == 0 {
+			problems = append(problems, lint.Problem{
+				Message:    fmt.Sprintf("Missing comment over %q.", d.GetName()),
+				Descriptor: d,
+				Location:   lint.DescriptorNameLocation(d),
+			})
+		}
+		return
+	},
+}


Test patch:
diff --git a/rules/aip0192/has_comments_test.go b/rules/aip0192/has_comments_test.go
new file mode 100644
index 0000000..44bed2b
--- /dev/null
+++ b/rules/aip0192/has_comments_test.go
@@ -0,0 +1,36 @@
+// Copyright 2019 Google LLC
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//     https://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package aip0192
+
+import (
+	"testing"
+
+	"github.com/googleapis/api-linter/rules/internal/testutils"
+)
+
+func TestHasComments(t *testing.T) {
+	file := testutils.ParseProto3String(t, `
+		// This is a book.
+		message Book {
+			// The resource name.
+			string name = 1;
+			string title = 2;
+		}
+	`)
+	wantProblems := testutils.Problems{{Descriptor: file.GetMessageTypes()[0].GetFields()[1]}}
+	if diff := wantProblems.Diff(hasComments.Lint(file)); diff != "" {
+		t.Errorf(diff)
+	}
+}