Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reducing the amount of compiler warnings in gen-enums.kt #269

Merged
merged 3 commits into from
May 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions buildSrc/src/main/kotlin/kotlinx/html/generate/attributes.kt
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,11 @@ fun Appendable.facade(repository: Repository, facade: AttributeFacade) {
fun Appendable.eventProperty(parent: String, attribute: AttributeInfo, shouldUnsafeCast: Boolean) {
val type = "(org.w3c.dom.events.Event) -> Unit"
variable(
receiver = parent, variable = Var(
receiver = parent,
variable = Var(
name = attribute.fieldName + "Function",
type = type,
mutable = true
varType = VarType.MUTABLE,
)
)
emptyLine()
Expand Down
15 changes: 13 additions & 2 deletions buildSrc/src/main/kotlin/kotlinx/html/generate/codegen.kt
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,16 @@ fun Appendable.const(value: Const<*>) {
append(value.asValue)
}

enum class VarType {
MUTABLE,
IMMUTABLE,
CONST,
}

data class Var(
val name: String,
val type: String,
val mutable: Boolean = false,
val varType: VarType = VarType.IMMUTABLE,
val override: Boolean = false,
val forceOmitValVar: Boolean = false,
val defaultValue: String = "",
Expand All @@ -72,7 +78,12 @@ fun Appendable.variable(variable: Var, omitValVar: Boolean = false, receiver: St
if (variable.override) {
append("override ")
}
append(if (variable.mutable) "var " else "val ")
val typeString = when (variable.varType) {
VarType.MUTABLE -> "var "
VarType.IMMUTABLE -> "val "
VarType.CONST -> "const val "
}
append(typeString)
}

if (receiver.isNotEmpty()) {
Expand Down
30 changes: 21 additions & 9 deletions buildSrc/src/main/kotlin/kotlinx/html/generate/enums.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package kotlinx.html.generate

import java.util.*

val reservedNames = setOf("class", "val", "var", "object", "true", "false", "as", "is", "for")

fun String.replaceIfReserved() = if (this in reservedNames) "html" + this.capitalize() else this
Expand All @@ -12,18 +10,25 @@ fun List<String>.toAttributeValues() : List<AttributeEnumValue> =
fun Appendable.enumObject(attribute : AttributeInfo) {
val name = attribute.enumTypeName

appendLine("@Suppress(\"unused\")")
appendLine("@Suppress(\"unused\", \"ConstPropertyName\")")
clazz(Clazz(name, isObject = true)) {
attribute.enumValues.forEach {
append(" ")
variable(Var(it.fieldName, "String", false, defaultValue = "\"${it.realName}\""))
variable(Var(it.fieldName, "String", varType = VarType.CONST, defaultValue = "\"${it.realName}\""))
emptyLine()
}

emptyLine()
append(" ")
// append("private ")
variable(Var("values", "List<String>", defaultValue = attribute.enumValues.map {"\"${it.fieldName}\""}.joinToString(", ", "listOf(", ")")))
variable(
Var(
name = "values",
type = "List<String>",
defaultValue = attribute
.enumValues
.joinToString(", ", "listOf(", ")") { "\"${it.fieldName}\"" },
)
)
emptyLine()
}

Expand All @@ -32,9 +37,9 @@ fun Appendable.enumObject(attribute : AttributeInfo) {

fun Appendable.enum(attribute : AttributeInfo) {
val name = attribute.enumTypeName
val realValue = Var("realValue", "String", false, true)
val realValue = Var(name = "realValue", type = "String", varType = VarType.IMMUTABLE, override = true)

appendLine("@Suppress(\"unused\")")
appendLine("@Suppress(\"unused\", \"EnumEntryName\")")
append("enum ")
clazz(Clazz(name, variables = listOf(realValue), parents = listOf("AttributeEnum"))) {
attribute.enumValues.forEachIndexed { idx, it ->
Expand All @@ -53,6 +58,13 @@ fun Appendable.enum(attribute : AttributeInfo) {

emptyLine()
append("internal ")
variable(Var(name.decapitalize() + "Values", "Map<String, $name>", false, defaultValue = "$name.values().associateBy { it.realValue }"))
variable(
Var(
name = name.decapitalize() + "Values",
type = "Map<String, $name>",
varType = VarType.IMMUTABLE,
defaultValue = "$name.entries.associateBy { it.realValue }",
),
)
emptyLine()
}
13 changes: 8 additions & 5 deletions buildSrc/src/main/kotlin/kotlinx/html/generate/tagsgen.kt
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,18 @@ fun Appendable.tagClass(repository: Repository, tag: TagInfo, excludeAttributes:
Var(
name = "initialAttributes",
type = "Map<String, String>",
mutable = false,
varType = VarType.IMMUTABLE,
override = false,
forceOmitValVar = true
)
)
add(Var(name = "consumer", type = "TagConsumer<*>", mutable = false, override = true))
add(Var(name = "consumer", type = "TagConsumer<*>", varType = VarType.IMMUTABLE, override = true))
if (customizableNamespace) {
add(
Var(
name = "namespace",
type = "String?",
mutable = false,
varType = VarType.IMMUTABLE,
override = false,
forceOmitValVar = true,
defaultValue = namespace?.quote() ?: "null"
Expand Down Expand Up @@ -197,7 +197,10 @@ internal fun Appendable.tagAttributeVar(
repository.attributeDelegateRequests.add(attributeRequest)

indent(indent)
variable(Var(attribute.fieldName, attribute.typeName, true), receiver = receiver ?: "")
variable(
Var(name = attribute.fieldName, type = attribute.typeName, varType = VarType.MUTABLE),
receiver = receiver ?: "",
)
return attributeRequest
}

Expand Down Expand Up @@ -524,7 +527,7 @@ private fun tagBuilderFunctionArguments(tag: TagInfo, blockOrContent: Boolean):
Var(
name = "namespace",
type = "String?",
mutable = false,
varType = VarType.IMMUTABLE,
override = false,
forceOmitValVar = true,
defaultValue = defaultNamespace
Expand Down
Loading
Loading