Skip to content

Commit 83e61ef

Browse files
Reducing the amount of compiler warnings in gen-enums.kt (#269)
* Reducing the amount of compiler warnings in gen-enums.kt * Updating code generation after merging in changes from master
1 parent 0832e98 commit 83e61ef

File tree

5 files changed

+235
-208
lines changed

5 files changed

+235
-208
lines changed

buildSrc/src/main/kotlin/kotlinx/html/generate/attributes.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,11 @@ fun Appendable.facade(repository: Repository, facade: AttributeFacade) {
6262
fun Appendable.eventProperty(parent: String, attribute: AttributeInfo, shouldUnsafeCast: Boolean) {
6363
val type = "(org.w3c.dom.events.Event) -> Unit"
6464
variable(
65-
receiver = parent, variable = Var(
65+
receiver = parent,
66+
variable = Var(
6667
name = attribute.fieldName + "Function",
6768
type = type,
68-
mutable = true
69+
varType = VarType.MUTABLE,
6970
)
7071
)
7172
emptyLine()

buildSrc/src/main/kotlin/kotlinx/html/generate/codegen.kt

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,16 @@ fun Appendable.const(value: Const<*>) {
4646
append(value.asValue)
4747
}
4848

49+
enum class VarType {
50+
MUTABLE,
51+
IMMUTABLE,
52+
CONST,
53+
}
54+
4955
data class Var(
5056
val name: String,
5157
val type: String,
52-
val mutable: Boolean = false,
58+
val varType: VarType = VarType.IMMUTABLE,
5359
val override: Boolean = false,
5460
val forceOmitValVar: Boolean = false,
5561
val defaultValue: String = "",
@@ -72,7 +78,12 @@ fun Appendable.variable(variable: Var, omitValVar: Boolean = false, receiver: St
7278
if (variable.override) {
7379
append("override ")
7480
}
75-
append(if (variable.mutable) "var " else "val ")
81+
val typeString = when (variable.varType) {
82+
VarType.MUTABLE -> "var "
83+
VarType.IMMUTABLE -> "val "
84+
VarType.CONST -> "const val "
85+
}
86+
append(typeString)
7687
}
7788

7889
if (receiver.isNotEmpty()) {

buildSrc/src/main/kotlin/kotlinx/html/generate/enums.kt

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package kotlinx.html.generate
22

3-
import java.util.*
4-
53
val reservedNames = setOf("class", "val", "var", "object", "true", "false", "as", "is", "for")
64

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

15-
appendLine("@Suppress(\"unused\")")
13+
appendLine("@Suppress(\"unused\", \"ConstPropertyName\")")
1614
clazz(Clazz(name, isObject = true)) {
1715
attribute.enumValues.forEach {
1816
append(" ")
19-
variable(Var(it.fieldName, "String", false, defaultValue = "\"${it.realName}\""))
17+
variable(Var(it.fieldName, "String", varType = VarType.CONST, defaultValue = "\"${it.realName}\""))
2018
emptyLine()
2119
}
2220

2321
emptyLine()
2422
append(" ")
25-
// append("private ")
26-
variable(Var("values", "List<String>", defaultValue = attribute.enumValues.map {"\"${it.fieldName}\""}.joinToString(", ", "listOf(", ")")))
23+
variable(
24+
Var(
25+
name = "values",
26+
type = "List<String>",
27+
defaultValue = attribute
28+
.enumValues
29+
.joinToString(", ", "listOf(", ")") { "\"${it.fieldName}\"" },
30+
)
31+
)
2732
emptyLine()
2833
}
2934

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

3338
fun Appendable.enum(attribute : AttributeInfo) {
3439
val name = attribute.enumTypeName
35-
val realValue = Var("realValue", "String", false, true)
40+
val realValue = Var(name = "realValue", type = "String", varType = VarType.IMMUTABLE, override = true)
3641

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

5459
emptyLine()
5560
append("internal ")
56-
variable(Var(name.decapitalize() + "Values", "Map<String, $name>", false, defaultValue = "$name.values().associateBy { it.realValue }"))
61+
variable(
62+
Var(
63+
name = name.decapitalize() + "Values",
64+
type = "Map<String, $name>",
65+
varType = VarType.IMMUTABLE,
66+
defaultValue = "$name.entries.associateBy { it.realValue }",
67+
),
68+
)
5769
emptyLine()
5870
}

buildSrc/src/main/kotlin/kotlinx/html/generate/tagsgen.kt

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,18 +29,18 @@ fun Appendable.tagClass(repository: Repository, tag: TagInfo, excludeAttributes:
2929
Var(
3030
name = "initialAttributes",
3131
type = "Map<String, String>",
32-
mutable = false,
32+
varType = VarType.IMMUTABLE,
3333
override = false,
3434
forceOmitValVar = true
3535
)
3636
)
37-
add(Var(name = "consumer", type = "TagConsumer<*>", mutable = false, override = true))
37+
add(Var(name = "consumer", type = "TagConsumer<*>", varType = VarType.IMMUTABLE, override = true))
3838
if (customizableNamespace) {
3939
add(
4040
Var(
4141
name = "namespace",
4242
type = "String?",
43-
mutable = false,
43+
varType = VarType.IMMUTABLE,
4444
override = false,
4545
forceOmitValVar = true,
4646
defaultValue = namespace?.quote() ?: "null"
@@ -197,7 +197,10 @@ internal fun Appendable.tagAttributeVar(
197197
repository.attributeDelegateRequests.add(attributeRequest)
198198

199199
indent(indent)
200-
variable(Var(attribute.fieldName, attribute.typeName, true), receiver = receiver ?: "")
200+
variable(
201+
Var(name = attribute.fieldName, type = attribute.typeName, varType = VarType.MUTABLE),
202+
receiver = receiver ?: "",
203+
)
201204
return attributeRequest
202205
}
203206

@@ -524,7 +527,7 @@ private fun tagBuilderFunctionArguments(tag: TagInfo, blockOrContent: Boolean):
524527
Var(
525528
name = "namespace",
526529
type = "String?",
527-
mutable = false,
530+
varType = VarType.IMMUTABLE,
528531
override = false,
529532
forceOmitValVar = true,
530533
defaultValue = defaultNamespace

0 commit comments

Comments
 (0)