Skip to content

Commit fbd70dd

Browse files
committed
For enum values starting with a digit, prefix the generated symbol with an underscore.
1 parent 0259f52 commit fbd70dd

File tree

4 files changed

+17
-2
lines changed

4 files changed

+17
-2
lines changed

Diff for: codegen-core/src/main/kotlin/software/amazon/smithy/rust/codegen/core/rustlang/RustReservedWords.kt

+2-1
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,8 @@ class RustReservedWordSymbolProvider(
7878
return base.toSymbol(shape)
7979
}
8080
val previousName = base.toMemberName(shape)
81-
val escapedName = this.toMemberName(shape)
81+
// Prefix leading digit with an underscore to avoid invalid identifiers; allow extra leading underscores.
82+
val escapedName = this.toMemberName(shape).replace(Regex("^(_*\\d)"), "_$1")
8283
// if the names don't match and it isn't a simple escaping with `r#`, record a rename
8384
renamedSymbol.toBuilder().name(escapedName)
8485
.letIf(escapedName != previousName && !escapedName.contains("r#")) {

Diff for: codegen-core/src/main/kotlin/software/amazon/smithy/rust/codegen/core/smithy/generators/EnumGenerator.kt

+2-1
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,8 @@ class EnumMemberModel(
102102
parentShape: Shape,
103103
definition: EnumDefinition,
104104
): MaybeRenamed? {
105-
val name = definition.name.orNull()?.toPascalCase() ?: return null
105+
// Prefix leading digit with an underscore to avoid invalid identifiers; allow extra leading underscores.
106+
val name = definition.name.orNull()?.toPascalCase()?.replace(Regex("^(_*\\d)"), "_$1") ?: return null
106107
// Create a fake member shape for symbol look up until we refactor to use EnumShape
107108
val fakeMemberShape =
108109
MemberShape.builder().id(parentShape.id.withMember(name)).target("smithy.api#String").build()

Diff for: codegen-core/src/test/kotlin/software/amazon/smithy/rust/codegen/core/rustlang/RustReservedWordsTest.kt

+2
Original file line numberDiff line numberDiff line change
@@ -206,5 +206,7 @@ internal class RustReservedWordSymbolProviderTest {
206206
expectEnumRename("SelfValue", MaybeRenamed("SelfValue_", "SelfValue"))
207207
expectEnumRename("SelfOther", MaybeRenamed("SelfOther", null))
208208
expectEnumRename("SELF", MaybeRenamed("SelfValue", "Self"))
209+
210+
expectEnumRename("_2DBarcode", MaybeRenamed("_2DBarcode", "2DBarcode"))
209211
}
210212
}

Diff for: codegen-core/src/test/kotlin/software/amazon/smithy/rust/codegen/core/smithy/generators/EnumGeneratorTest.kt

+11
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,9 @@ class EnumGeneratorTest {
5050
{ value: "some-value-2",
5151
name: "someName2",
5252
documentation: "More documentation #escape" },
53+
{ value: "2D_BARCODE",
54+
name: "_2D_BARCODE",
55+
documentation: "Example with leading digit." },
5356
{ value: "unknown",
5457
name: "unknown",
5558
documentation: "It has some docs that #need to be escaped" }
@@ -80,6 +83,14 @@ class EnumGeneratorTest {
8083
}
8184
}
8285

86+
@Test
87+
fun `it prefixes enum names with underscore to avoid generating invalid identifiers starting with a digit`() {
88+
model("_2D_BARCODE").also { member ->
89+
member.derivedName() shouldBe "_2DBarcode"
90+
member.name()!!.renamedFrom shouldBe "2DBarcode"
91+
}
92+
}
93+
8394
@Test
8495
fun `it should render documentation`() {
8596
val rendered = RustWriter.forModule("model").also { model("some_name_1").render(it) }.toString()

0 commit comments

Comments
 (0)