Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix conflict between rules due to annotated super type call #2227

Merged
merged 3 commits into from
Sep 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()
}
}