Do not change project module name to Upper_Snake_Case (#8697)

close #8649

Do not mangle the project module name. Only strip the unsupported characters and make it a valid Enso module identifier.
This commit is contained in:
Dmitry Bushev 2024-01-08 16:26:35 +00:00 committed by GitHub
parent 4b19da4835
commit 5e0a7337d0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 13 additions and 46 deletions

View File

@ -19,7 +19,8 @@ object NameValidation {
else name
val startingWithUppercase = starting.capitalize
val onlyAlphanumeric = startingWithUppercase.filter(isAllowedNameCharacter)
toUpperSnakeCase(onlyAlphanumeric)
onlyAlphanumeric
}
/** Validate the project name.
@ -39,8 +40,6 @@ object NameValidation {
ListSet(invalidCharacters: _*)
)
)
} else if (name != toUpperSnakeCase(name)) {
Left(InvalidNameError.ShouldBeUpperSnakeCase(toUpperSnakeCase(name)))
} else {
Right(name)
}
@ -54,33 +53,4 @@ object NameValidation {
char.isLetterOrDigit || char == '_'
}
/** Takes a name containing letters, digits, and `_` characters and makes it
* a proper `Upper_Snake_Case` name.
*
* @param string the input string
* @return the transformed string
*/
private def toUpperSnakeCase(string: String): String = {
val beginMarker = '#'
val chars = string.toList
val charPairs = (beginMarker :: chars).zip(chars)
charPairs
.map { case (previous, current) =>
if (previous == beginMarker) {
current.toString
} else if (previous.isLower && current.isUpper) {
s"_$current"
} else if (previous.isLetter && current.isDigit) {
s"_$current"
} else if (previous == '_' && current == '_') {
""
} else if (previous.isDigit && current.isLetter) {
s"_${current.toUpper}"
} else {
current.toString
}
}
.mkString("")
}
}

View File

@ -55,10 +55,10 @@ class ConfigSpec
}
"only require the name and use defaults for everything else" in {
val parsed = Config.fromYaml("name: FooBar").get
parsed.name shouldEqual "FooBar"
val parsed = Config.fromYaml("name: fooBar").get
parsed.name shouldEqual "fooBar"
parsed.normalizedName shouldEqual None
parsed.moduleName shouldEqual "Foo_Bar"
parsed.moduleName shouldEqual "FooBar"
parsed.edition shouldBe empty
}

View File

@ -12,13 +12,14 @@ class NameSanitizationSpec extends AnyWordSpec with Matchers {
"sanitize the name of the project" in {
normalizeName("My_Project") shouldEqual "My_Project"
normalizeName("My___Project") shouldEqual "My_Project"
normalizeName("myProject") shouldEqual "My_Project"
normalizeName("myPro??^ject123") shouldEqual "My_Project_123"
normalizeName("???%$6543lib") shouldEqual "Project_6543_Lib"
normalizeName("MyProject™") shouldEqual "My_Project"
normalizeName("My___Project") shouldEqual "My___Project"
normalizeName("myProject") shouldEqual "MyProject"
normalizeName("myPro??^ject123") shouldEqual "MyProject123"
normalizeName("???%$6543lib") shouldEqual "Project_6543lib"
normalizeName("MyProject™") shouldEqual "MyProject"
normalizeName("$$$$") shouldEqual "Project"
normalizeName("$$42$$") shouldEqual "Project_42"
normalizeName("AoC_1") shouldEqual "AoC_1"
normalizeName("\uD83D\uDE80") shouldEqual "Project"
normalizeName("\uD83D\uDE80_\uD83D\uDE0A") shouldEqual "Project"
normalizeName("\uD83D\uDE80_\uD83D\uDE0A__") shouldEqual "Project"
@ -29,12 +30,8 @@ class NameSanitizationSpec extends AnyWordSpec with Matchers {
validateName("My") shouldEqual Right("My")
validateName("My_Project") shouldEqual Right("My_Project")
validateName("") shouldEqual Left(InvalidNameError.Empty)
validateName("My___Project") shouldEqual Left(
InvalidNameError.ShouldBeUpperSnakeCase("My_Project")
)
validateName("FooBar") shouldEqual Left(
InvalidNameError.ShouldBeUpperSnakeCase("Foo_Bar")
)
validateName("My___Project") shouldEqual Right("My___Project")
validateName("FooBar") shouldEqual Right("FooBar")
validateName("myProject") shouldEqual Left(
InvalidNameError.ShouldStartWithCapitalLetter
)