Skip to content

Commit

Permalink
Update scala reserved words, handle already defined method names like…
Browse files Browse the repository at this point in the history
… `close` (#1670)

* Update scala reserved words

* Add "Method" to end of method name if it is a already defined method
  • Loading branch information
benthecarman authored Aug 26, 2022
1 parent f9b768b commit 6e38aed
Showing 1 changed file with 36 additions and 5 deletions.
41 changes: 36 additions & 5 deletions codegen/src/main/scala/akka/grpc/gen/scaladsl/Method.scala
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ case class Method(
}

val nameSafe: String =
if (ReservedWords.contains(name)) s"""`$name`"""
if (ReservedScalaWords.contains(name)) s"""`$name`"""
else if (ReservedMethodNames.contains(name)) s"$name$ReservedMethodNameSuffix"
else name

}
Expand Down Expand Up @@ -80,8 +81,8 @@ object Method {
messageType.scalaType.fullName
}

// https://github.com/scalapb/ScalaPB/blob/master/compiler-plugin/src/main/scala/scalapb/compiler/DescriptorImplicits.scala#L1038
private val ReservedWords = Set(
// https://github.com/scalapb/ScalaPB/blob/38845c0cf21173a2242a5d14ed48a7c33b981bae/compiler-plugin/src/main/scala/scalapb/compiler/DescriptorImplicits.scala#L1115
private val ReservedScalaWords = Set(
"abstract",
"case",
"catch",
Expand All @@ -90,21 +91,28 @@ object Method {
"do",
"else",
"enum",
"export",
"extends",
"false",
"final",
"finally",
"for",
"forSome",
"given",
"if",
"implicit",
"import",
"infix",
"inline",
"lazy",
"macro",
"match",
"ne",
"new",
"null",
"object",
"opaque",
"open",
"override",
"package",
"private",
Expand All @@ -116,13 +124,36 @@ object Method {
"this",
"throw",
"trait",
"transparent",
"try",
"true",
"type",
"val",
"var",
"while",
"with",
"yield",
"ne")
"yield")

private val ReservedMethodNameSuffix = "Method"

private val ReservedMethodNames =
Set(
"close",
"closed",
"clone",
"clone",
"hashCode",
"toString",
"isInstanceOf",
"asInstanceOf",
"equals",
"eq",
"notify",
"notifyAll",
"wait",
"finalize",
"synchronized",
"ensuring",
"wait",
"formatted")
}

0 comments on commit 6e38aed

Please sign in to comment.