Skip to content

Commit

Permalink
Filter serialVersionUID from PropertyNamingRule
Browse files Browse the repository at this point in the history
  • Loading branch information
atulgpt committed May 19, 2023
1 parent e6ca5a7 commit 45c1ab9
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,8 @@ public class PropertyNamingRule :
) {
identifier
.text
.takeUnless { it.matches(SCREAMING_SNAKE_CASE_REGEXP) }
.takeUnless { it == SERIAL_VERSION_UID_PROPERTY_NAME }
?.takeUnless { it.matches(SCREAMING_SNAKE_CASE_REGEXP) }
?.let {
emit(
identifier.startOffset,
Expand Down Expand Up @@ -144,6 +145,7 @@ public class PropertyNamingRule :
private companion object {
val LOWER_CAMEL_CASE_REGEXP = "[a-z][a-zA-Z0-9]*".regExIgnoringDiacriticsAndStrokesOnLetters()
val SCREAMING_SNAKE_CASE_REGEXP = "[A-Z][_A-Z0-9]*".regExIgnoringDiacriticsAndStrokesOnLetters()
const val SERIAL_VERSION_UID_PROPERTY_NAME = "serialVersionUID"
val BACKING_PROPERTY_LOWER_CAMEL_CASE_REGEXP = "_[a-z][a-zA-Z0-9]*".regExIgnoringDiacriticsAndStrokesOnLetters()
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.pinterest.ktlint.ruleset.standard.rules

import com.pinterest.ktlint.test.KtLintAssertThat
import org.junit.jupiter.api.Nested
import org.junit.jupiter.api.Test
import org.junit.jupiter.params.ParameterizedTest
import org.junit.jupiter.params.provider.ValueSource
Expand Down Expand Up @@ -158,4 +159,50 @@ class PropertyNamingRuleTest {
""".trimIndent()
propertyNamingRuleAssertThat(code).hasNoLintViolations()
}

@Nested
inner class `Given property is serialVersionUID` {
@Test
fun `Given property is present in companion object`() {
val code =
"""
class Foo1 {
companion object {
private const val serialVersionUID: Long = 123
}
}
class Foo2 {
companion object {
private val serialVersionUID: Long = 123
}
}
class Foo3 {
companion object {
@JvmStatic private val serialVersionUID: Long = 123
}
}
class Foo4 {
private val serialVersionUID: Long = 123
}
""".trimIndent()
propertyNamingRuleAssertThat(code).hasNoLintViolations()
}

@Test
fun `Given property defined object is private const`() {
val code =
"""
object Foo1 {
private const val serialVersionUID: Long = 123
}
object Foo2 {
private val serialVersionUID: Long = 123
}
object Foo3 {
@JvmStatic private val serialVersionUID: Long = 123
}
""".trimIndent()
propertyNamingRuleAssertThat(code).hasNoLintViolations()
}
}
}

0 comments on commit 45c1ab9

Please sign in to comment.