Skip to content

Commit

Permalink
[Proto] Require space between 'service' and service name in regex mat…
Browse files Browse the repository at this point in the history
…ching (#1845)

Prior to this commit, the regex for determining whether a file had
services did not require a space between the word "service" and the
service name. This could lead to false alarms, like "message
serviceABC". In this commit, we update the regex to only match if there
is at least one space between service and the service name.
  • Loading branch information
ckilian867 authored Jul 26, 2024
1 parent 906a322 commit 5385064
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
2 changes: 1 addition & 1 deletion language/proto/fileinfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ func buildProtoRegexp() *regexp.Regexp {
importStmt := `\bimport\s*(?:public|weak)?\s*(?P<import>` + strLit + `)\s*;`
packageStmt := `\bpackage\s*(?P<package>` + fullIdent + `)\s*;`
optionStmt := `\boption\s*(?P<optkey>` + fullIdent + `)\s*=\s*(?P<optval>` + strLit + `)\s*;`
serviceStmt := `(?P<service>service\s*` + ident + `\s*{)`
serviceStmt := `(?P<service>service\s+` + ident + `\s*{)`
comment := `//[^\n]*`
protoReSrc := strings.Join([]string{importStmt, packageStmt, optionStmt, serviceStmt, comment}, "|")
return regexp.MustCompile(protoReSrc)
Expand Down
29 changes: 27 additions & 2 deletions language/proto/fileinfo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,35 @@ import "second.proto";`,
want: FileInfo{
HasServices: true,
},
}, {
},
{
desc: "service multiple spaces",
name: "service.proto",
proto: `service ChatService {}`,
want: FileInfo{
HasServices: true,
},
},
{
desc: "service no space for bracket after service name",
name: "service.proto",
proto: `service ChatService{}`,
want: FileInfo{
HasServices: true,
},
},
{
desc: "service no space before service name not matched",
name: "service.proto",
proto: `serviceChatService {}`,
want: FileInfo{
HasServices: false,
},
},
{
desc: "service as name",
name: "service.proto",
proto: `message ServiceAccount { string service = 1; }`,
proto: `message serviceAccount { string service = 1; }`,
want: FileInfo{
HasServices: false,
},
Expand Down

0 comments on commit 5385064

Please sign in to comment.