Supported other patterns for mapNotNull
This commit is contained in:
+7
@@ -163,6 +163,13 @@ class FilterIsInstanceTransformation(
|
||||
}
|
||||
|
||||
class FilterNotNullTransformation(override val loop: KtForExpression) : SequenceTransformation {
|
||||
override fun mergeWithPrevious(previousTransformation: SequenceTransformation): SequenceTransformation? {
|
||||
return when (previousTransformation) {
|
||||
is MapTransformation -> MapNotNullTransformation(loop, previousTransformation.inputVariable, previousTransformation.mapping)
|
||||
is MapIndexedTransformation -> MapIndexedNotNullTransformation(loop, previousTransformation.inputVariable, previousTransformation.indexVariable, previousTransformation.mapping)
|
||||
else -> null
|
||||
}
|
||||
}
|
||||
|
||||
override val affectsIndex: Boolean
|
||||
get() = true
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
// WITH_RUNTIME
|
||||
// INTENTION_TEXT: "Replace with 'mapIndexedNotNull{}.mapTo(){}'"
|
||||
fun foo(list: List<String?>, target: MutableList<String>) {
|
||||
<caret>for ((index, s) in list.withIndex()) {
|
||||
val length = s?.substring(index)?.length
|
||||
if (length == null) continue
|
||||
target.add(length.toString())
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
// WITH_RUNTIME
|
||||
// INTENTION_TEXT: "Replace with 'mapIndexedNotNull{}.mapTo(){}'"
|
||||
fun foo(list: List<String?>, target: MutableList<String>) {
|
||||
<caret>list
|
||||
.mapIndexedNotNull { index, s -> s?.substring(index)?.length }
|
||||
.mapTo(target) { it.toString() }
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
// WITH_RUNTIME
|
||||
// INTENTION_TEXT: "Replace with 'mapNotNull{}.mapTo(){}'"
|
||||
fun foo(list: List<String?>, target: MutableList<String>) {
|
||||
<caret>for (s in list) {
|
||||
val length = s?.length
|
||||
if (length == null) continue
|
||||
target.add(length.toString())
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
// WITH_RUNTIME
|
||||
// INTENTION_TEXT: "Replace with 'mapNotNull{}.mapTo(){}'"
|
||||
fun foo(list: List<String?>, target: MutableList<String>) {
|
||||
<caret>list
|
||||
.mapNotNull { it?.length }
|
||||
.mapTo(target) { it.toString() }
|
||||
}
|
||||
Reference in New Issue
Block a user