Skip to content

Commit f10744f

Browse files
authored
fix(aip-0134): check word boundaries in synonyms rule (#1564)
* fix(aip-0134): check word boundaries in synonyms rule The synonyms rule was incorrectly flagging method names like "Settle", "PatchyClouds", or "PutterAround" because it only checked if the name started with the synonym prefix ("Set", "Patch", "Put"). This change adds word boundary detection by checking if the character after the prefix is uppercase (indicating a new word in PascalCase) or if we've reached the end of the string. Now "SetBook" correctly fails while "Settle" correctly passes. * use unicode.IsUpper * store lengths in vars * remove redundant length check
1 parent 9aadf98 commit f10744f

File tree

2 files changed

+18
-9
lines changed

2 files changed

+18
-9
lines changed

rules/aip0134/synonyms.go

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ package aip0134
1717
import (
1818
"fmt"
1919
"strings"
20+
"unicode"
2021

2122
"github.com/googleapis/api-linter/v2/lint"
2223
"github.com/googleapis/api-linter/v2/locations"
@@ -33,15 +34,20 @@ var synonyms = &lint.MethodRule{
3334
name := string(m.Name())
3435
for _, syn := range []string{"Patch", "Put", "Set"} {
3536
if strings.HasPrefix(name, syn) {
36-
return []lint.Problem{{
37-
Message: fmt.Sprintf(
38-
`%q can be a synonym for "Update". Should this be a Update method?`,
39-
syn,
40-
),
41-
Descriptor: m,
42-
Location: locations.DescriptorName(m),
43-
Suggestion: strings.Replace(name, syn, "Update", 1),
44-
}}
37+
synLen := len(syn)
38+
nameLen := len(name)
39+
// Check for word boundary: either exact match or next char is uppercase
40+
if nameLen == synLen || unicode.IsUpper(rune(name[synLen])) {
41+
return []lint.Problem{{
42+
Message: fmt.Sprintf(
43+
`%q can be a synonym for "Update". Should this be a Update method?`,
44+
syn,
45+
),
46+
Descriptor: m,
47+
Location: locations.DescriptorName(m),
48+
Suggestion: strings.Replace(name, syn, "Update", 1),
49+
}}
50+
}
4551
}
4652
}
4753
return nil

rules/aip0134/synonyms_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ func TestSynonyms(t *testing.T) {
3030
{"PutBook", testutils.Problems{{Suggestion: "UpdateBook"}}},
3131
{"SetBook", testutils.Problems{{Suggestion: "UpdateBook"}}},
3232
{"SetIamPolicy", nil},
33+
{"Settle", testutils.Problems{}},
34+
{"PatchyClouds", testutils.Problems{}},
35+
{"PutterAround", testutils.Problems{}},
3336
}
3437
for _, test := range tests {
3538
t.Run(test.MethodName, func(t *testing.T) {

0 commit comments

Comments
 (0)