Skip to content

Commit

Permalink
Fix conflict between rules due to annotated super type call (#2227)
Browse files Browse the repository at this point in the history
Fix conflict between indentation rule and modifier list spacing rule resulting in cyclic changing of indentation of super type call entry which is annotated
  • Loading branch information
paul-dingemans committed Sep 4, 2023
1 parent 0bab9b8 commit 35ef6e5
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 20 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,8 @@ If an `EditorConfigProperty` is defined in a `Rule` that is only provided via a
* Fix wrapping of multiline postfix expression `multiline-expression-wrapping` [#2183](https://github.com/pinterest/ktlint/issues/2183)
* Remove registration of class "org.jetbrains.kotlin.com.intellij.treeCopyHandler" as extension point for the compiler as this is not supported in the embedded Kotlin compiler version 1.9. Also remove Ktlint CLI command line flag `disable-kotlin-extension-point`, and parameter `enableKotlinCompilerExtensionPoint` from `KtLintRuleEngine` to disable the kotlin extension point [#2061](https://github.com/pinterest/ktlint/issues/2061)
* Do not wrap expression after a spread operator `multiline-expression-wrapping` [#2188](https://github.com/pinterest/ktlint/issues/2188)
* Do not wrap expression after a spread operator `multiline-expression-wrapping` [#2188](https://github.com/pinterest/ktlint/issues/2188)
* Prevent cyclic dependency between `modifier-list-spacing` rule and `indent` rule when the super type call entry is annotated ([#2227](https://github.com/pinterest/ktlint/pull/2227))
* Do not remove parenthesis after explicit constructor keyword when it has no parameters ([#2226](https://github.com/pinterest/ktlint/pull/2226))

### Changed
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import com.pinterest.ktlint.rule.engine.core.api.SinceKtlint
import com.pinterest.ktlint.rule.engine.core.api.SinceKtlint.Status.EXPERIMENTAL
import com.pinterest.ktlint.rule.engine.core.api.SinceKtlint.Status.STABLE
import com.pinterest.ktlint.rule.engine.core.api.children
import com.pinterest.ktlint.rule.engine.core.api.indent
import com.pinterest.ktlint.rule.engine.core.api.isPartOfComment
import com.pinterest.ktlint.rule.engine.core.api.nextLeaf
import com.pinterest.ktlint.rule.engine.core.api.nextSibling
Expand Down Expand Up @@ -59,28 +58,21 @@ public class ModifierListSpacingRule : StandardRule("modifier-list-spacing") {
if (node.isAnnotationElement() ||
(node.elementType == MODIFIER_LIST && node.lastChildNode.isAnnotationElement())
) {
val expectedWhiteSpace =
if (whitespace.textContains('\n')) {
node.indent()
} else {
" "
if (whitespace.text.contains("\n\n")) {
emit(whitespace.startOffset, "Single newline expected after annotation", true)
if (autoCorrect) {
(whitespace as LeafPsiElement).rawReplaceWithText(
"\n".plus(whitespace.text.substringAfterLast("\n")),
)
}
if (whitespace.text != expectedWhiteSpace) {
emit(
whitespace.startOffset,
"Single whitespace or newline expected after annotation",
true,
)
} else if (!whitespace.text.contains('\n') && whitespace.text != " ") {
emit(whitespace.startOffset, "Single whitespace or newline expected after annotation", true)
if (autoCorrect) {
(whitespace as LeafPsiElement).rawReplaceWithText(expectedWhiteSpace)
(whitespace as LeafPsiElement).rawReplaceWithText(" ")
}
}
} else {
emit(
whitespace.startOffset,
"Single whitespace expected after modifier",
true,
)
emit(whitespace.startOffset, "Single whitespace expected after modifier", true)
if (autoCorrect) {
(whitespace as LeafPsiElement).rawReplaceWithText(" ")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,8 @@ class ModifierListSpacingRuleTest {
""".trimIndent()
modifierListSpacingRuleAssertThat(code)
.hasLintViolations(
LintViolation(1, 6, "Single whitespace or newline expected after annotation"),
LintViolation(3, 6, "Single whitespace or newline expected after annotation"),
LintViolation(1, 6, "Single newline expected after annotation"),
LintViolation(3, 6, "Single newline expected after annotation"),
).isFormattedAs(formattedCode)
}

Expand Down Expand Up @@ -173,4 +173,16 @@ class ModifierListSpacingRuleTest {
""".trimIndent()
modifierListSpacingRuleAssertThat(code).hasNoLintViolations()
}

@Test
fun `Given a class with an annotated super type call entry`() {
val code =
"""
class Foo(
bar: Bar,
) : @Unused
FooBar()
""".trimIndent()
modifierListSpacingRuleAssertThat(code).hasNoLintViolations()
}
}

0 comments on commit 35ef6e5

Please sign in to comment.