Skip to content

Commit

Permalink
Merge pull request #1220 from square/egorand/220220/inline-val
Browse files Browse the repository at this point in the history
Inline val property if its getter is inline
  • Loading branch information
Egorand committed Feb 27, 2022
2 parents 8fe75ef + 3d717a6 commit 9570aba
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public class PropertySpec private constructor(
inlineAnnotations: Boolean = inline
) {
val isInlineProperty = getter?.modifiers?.contains(KModifier.INLINE) ?: false &&
setter?.modifiers?.contains(KModifier.INLINE) ?: false
(!mutable || setter?.modifiers?.contains(KModifier.INLINE) ?: false)
val propertyModifiers = if (isInlineProperty) modifiers + KModifier.INLINE else modifiers
if (emitKdoc) {
codeWriter.emitKdoc(kdoc.ensureEndsWithNewLine())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ class PropertySpecTest {
}.hasMessageThat().isEqualTo("parameterless setter cannot have code")
}

@Test fun inlineSingleAccessor() {
@Test fun inlineSingleAccessorVal() {
val prop = PropertySpec.builder("foo", String::class)
.getter(
FunSpec.getterBuilder()
Expand All @@ -81,7 +81,26 @@ class PropertySpecTest {

assertThat(prop.toString()).isEqualTo(
"""
|val foo: kotlin.String
|inline val foo: kotlin.String
| get() = "foo"
|""".trimMargin()
)
}

@Test fun inlineSingleAccessorVar() {
val prop = PropertySpec.builder("foo", String::class)
.mutable()
.getter(
FunSpec.getterBuilder()
.addModifiers(KModifier.INLINE)
.addStatement("return %S", "foo")
.build()
)
.build()

assertThat(prop.toString()).isEqualTo(
"""
|var foo: kotlin.String
| inline get() = "foo"
|""".trimMargin()
)
Expand Down Expand Up @@ -255,8 +274,8 @@ class PropertySpecTest {
.build()
assertThat(prop.toString()).isEqualTo(
"""
|private val <T : kotlin.Any> kotlin.reflect.KClass<T>.someFunction: T
| inline get() = stuff as T
|private inline val <T : kotlin.Any> kotlin.reflect.KClass<T>.someFunction: T
| get() = stuff as T
|""".trimMargin()
)
}
Expand Down Expand Up @@ -296,8 +315,8 @@ class PropertySpecTest {
.build()
assertThat(prop.toString()).isEqualTo(
"""
|private val <reified T> kotlin.reflect.KClass<T>.someFunction: T
| inline get() = stuff as T
|private inline val <reified T> kotlin.reflect.KClass<T>.someFunction: T
| get() = stuff as T
|""".trimMargin()
)
}
Expand Down

0 comments on commit 9570aba

Please sign in to comment.