Repository: googleapis/api-linter
Pull request #778 (2021-03-05 22:54:57)

feat: AIP-140 – Prohibit goofy underscores.


Patch:
diff --git a/docs/rules/0140/underscores.md b/docs/rules/0140/underscores.md
new file mode 100644
index 0000000..7079a28
--- /dev/null
+++ b/docs/rules/0140/underscores.md
@@ -0,0 +1,63 @@
+---
+rule:
+  aip: 140
+  name: [core, '0140', underscores]
+  summary: Field names must not have goofy underscores.
+permalink: /140/underscores
+redirect_from:
+  - /0140/underscores
+---
+
+# Field names: Underscores
+
+This rule enforces that field names do not use leading, trailing, or adjacent
+underscores, as mandated in [AIP-140][].
+
+## Details
+
+This rule checks every field in the proto and complains if it sees leading or
+trailing underscores, or two or more underscores with nothing in between them.
+
+## Examples
+
+**Incorrect** code for this rule:
+
+```proto
+// Incorrect.
+message Book {
+  string name = 1;
+  string _title = 2;  // Should be `title`.
+}
+```
+
+**Correct** code for this rule:
+
+```proto
+// Correct.
+message Book {
+  string name = 1;
+  string title = 2;
+}
+```
+
+## Disabling
+
+If you need to violate this rule, use a leading comment above the method.
+Remember to also include an [aip.dev/not-precedent][] comment explaining why.
+
+**Warning:** Violating this rule is likely to run into tooling failures.
+
+```proto
+// (-- api-linter: core::0140::underscores=disabled
+//     aip.dev/not-precedent: We need to do this because reasons. --)
+message Book {
+  string name = 1;
+  string _title = 2;
+}
+```
+
+If you need to violate this rule for an entire file, place the comment at the
+top of the file.
+
+[aip-140]: https://aip.dev/140
+[aip.dev/not-precedent]: https://aip.dev/not-precedent
diff --git a/rules/aip0140/aip0140.go b/rules/aip0140/aip0140.go
index 0b15088..e990339 100644
--- a/rules/aip0140/aip0140.go
+++ b/rules/aip0140/aip0140.go
@@ -34,6 +34,7 @@ func AddRules(r lint.RuleRegistry) error {
 		noPrepositions,
 		numbers,
 		reservedWords,
+		underscores,
 		uri,
 	)
 }
diff --git a/rules/aip0140/underscores.go b/rules/aip0140/underscores.go
new file mode 100644
index 0000000..7dbc565
--- /dev/null
+++ b/rules/aip0140/underscores.go
@@ -0,0 +1,42 @@
+// Copyright 2021 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 aip0140
+
+import (
+	"regexp"
+	"strings"
+
+	"github.com/googleapis/api-linter/lint"
+	"github.com/googleapis/api-linter/locations"
+	"github.com/jhump/protoreflect/desc"
+)
+
+var underscores = &lint.FieldRule{
+	Name: lint.NewRuleName(140, "underscores"),
+	LintField: func(f *desc.FieldDescriptor) []lint.Problem {
+		n := f.GetName()
+		if strings.HasPrefix(n, "_") || strings.HasSuffix(n, "_") || strings.Contains(n, "__") {
+			return []lint.Problem{{
+				Message:    "Field names must not begin or end with underscore, or have adjacent underscores.",
+				Descriptor: f,
+				Location:   locations.DescriptorName(f),
+				Suggestion: adjacent.ReplaceAllString(strings.TrimRight(strings.TrimLeft(n, "_"), "_"), "_"),
+			}}
+		}
+		return nil
+	},
+}
+
+var adjacent = regexp.MustCompile("_{2,}")


Test patch:
diff --git a/rules/aip0140/underscores_test.go b/rules/aip0140/underscores_test.go
new file mode 100644
index 0000000..2437b09
--- /dev/null
+++ b/rules/aip0140/underscores_test.go
@@ -0,0 +1,50 @@
+// Copyright 2021 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 aip0140
+
+import (
+	"testing"
+
+	"github.com/googleapis/api-linter/rules/internal/testutils"
+)
+
+func TestUnderscores(t *testing.T) {
+	for _, test := range []struct {
+		name     string
+		Field    string
+		problems testutils.Problems
+	}{
+		{"Valid", "foo_bar", nil},
+		{"InvalidLeading", "_foo_bar", testutils.Problems{{Suggestion: "foo_bar"}}},
+		{"InvalidTrailing", "foo_bar_", testutils.Problems{{Suggestion: "foo_bar"}}},
+		{"InvalidDoubleLeading", "__foo_bar", testutils.Problems{{Suggestion: "foo_bar"}}},
+		{"InvalidDoubleTrailing", "foo_bar__", testutils.Problems{{Suggestion: "foo_bar"}}},
+		{"InvalidAdjacent", "foo__bar", testutils.Problems{{Suggestion: "foo_bar"}}},
+		{"InvalidDoubleAdjacent", "foo___bar", testutils.Problems{{Suggestion: "foo_bar"}}},
+		{"InvalidMultiAdjacent", "foo__bar__baz", testutils.Problems{{Suggestion: "foo_bar_baz"}}},
+	} {
+		t.Run(test.name, func(t *testing.T) {
+			f := testutils.ParseProto3Tmpl(t, `
+				message Foo {
+					string {{.Field}} = 1;
+				}
+			`, test)
+			field := f.GetMessageTypes()[0].GetFields()[0]
+			if diff := test.problems.SetDescriptor(field).Diff(underscores.Lint(f)); diff != "" {
+				t.Errorf(diff)
+			}
+		})
+	}
+}