TrailingComma: fix case of when without expression or property

#KT-34744
#KT-36393 Fixed
This commit is contained in:
Dmitry Gridin
2020-02-06 19:04:44 +07:00
parent 6739d03823
commit cb66625688
5 changed files with 4 additions and 38 deletions
@@ -22,8 +22,10 @@ import org.jetbrains.kotlin.idea.formatter.kotlinCustomSettings
import org.jetbrains.kotlin.psi.KtDestructuringDeclaration
import org.jetbrains.kotlin.psi.KtFunctionLiteral
import org.jetbrains.kotlin.psi.KtWhenEntry
import org.jetbrains.kotlin.psi.KtWhenExpression
import org.jetbrains.kotlin.psi.psiUtil.endOffset
import org.jetbrains.kotlin.psi.psiUtil.startOffset
import org.jetbrains.kotlin.utils.addToStdlib.cast
/*
* ASTBlock.node is nullable, this extension was introduced to minimize changes
@@ -64,7 +66,7 @@ fun KtFunctionLiteral.needTrailingComma(settings: CodeStyleSettings?, checkExist
fun KtWhenEntry.needTrailingComma(settings: CodeStyleSettings?, checkExistingTrailingComma: Boolean = true): Boolean = needTrailingComma(
settings = settings,
trailingComma = { if (checkExistingTrailingComma) trailingComma else null },
additionalCheck = { !isElse },
additionalCheck = { !isElse && parent.cast<KtWhenExpression>().leftParenthesis != null },
globalEndOffset = { arrow?.endOffset },
)
@@ -70,7 +70,7 @@ fun foo(x: Any) {
}
when {
x in coll,
x in coll
-> {
}
@@ -3443,16 +3443,4 @@
<offset>8</offset>
<length>1</length>
</problem>
<problem>
<file>WhenEntry.kt</file>
<line>69</line>
<module>testProject</module>
<package>light_idea_test_case</package>
<entry_point TYPE="file" FQNAME="WhenEntry.kt" />
<problem_class severity="WEAK WARNING" attribute_key="INFO_ATTRIBUTES">Trailing comma recommendations</problem_class>
<description>Missing trailing comma</description>
<highlighted_element>l</highlighted_element>
<offset>16</offset>
<length>1</length>
</problem>
</problems>
@@ -2027,18 +2027,6 @@
<offset>8</offset>
<length>1</length>
</problem>
<problem>
<file>WhenEntry.kt</file>
<line>69</line>
<module>testProject</module>
<package>light_idea_test_case</package>
<entry_point TYPE="file" FQNAME="WhenEntry.kt" />
<problem_class severity="WEAK WARNING" attribute_key="INFO_ATTRIBUTES">Trailing comma recommendations</problem_class>
<description>Missing trailing comma</description>
<highlighted_element>l</highlighted_element>
<offset>16</offset>
<length>1</length>
</problem>
<problem>
<file>LambdaValueParameters.kt</file>
<line>4</line>
@@ -3443,16 +3443,4 @@
<offset>8</offset>
<length>1</length>
</problem>
<problem>
<file>WhenEntry.kt</file>
<line>69</line>
<module>testProject</module>
<package>light_idea_test_case</package>
<entry_point TYPE="file" FQNAME="WhenEntry.kt" />
<problem_class severity="WEAK WARNING" attribute_key="INFO_ATTRIBUTES">Trailing comma recommendations</problem_class>
<description>Missing trailing comma</description>
<highlighted_element>l</highlighted_element>
<offset>16</offset>
<length>1</length>
</problem>
</problems>