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

Report violation when parameter list is preceded by a comment #2541

Merged
merged 2 commits into from
Feb 9, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import com.pinterest.ktlint.rule.engine.core.api.indent
import com.pinterest.ktlint.rule.engine.core.api.nextLeaf
import com.pinterest.ktlint.rule.engine.core.api.noNewLineInClosedRange
import com.pinterest.ktlint.rule.engine.core.api.prevLeaf
import com.pinterest.ktlint.rule.engine.core.api.upsertWhitespaceBeforeMe
import com.pinterest.ktlint.rule.engine.core.api.upsertWhitespaceAfterMe
import com.pinterest.ktlint.ruleset.standard.StandardRule
import org.jetbrains.kotlin.com.intellij.lang.ASTNode

Expand Down Expand Up @@ -97,7 +97,7 @@ public class KdocWrappingRule :
) {
emit(startOffset, "A KDoc comment may not be followed by any other element on that same line", true)
if (autoCorrect) {
this.upsertWhitespaceBeforeMe(kdocCommentNode.indent())
kdocCommentNode.upsertWhitespaceAfterMe(kdocCommentNode.indent())
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ import com.pinterest.ktlint.rule.engine.core.api.editorconfig.INDENT_STYLE_PROPE
import com.pinterest.ktlint.rule.engine.core.api.editorconfig.MAX_LINE_LENGTH_PROPERTY
import com.pinterest.ktlint.rule.engine.core.api.firstChildLeafOrSelf
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.isWhiteSpace
import com.pinterest.ktlint.rule.engine.core.api.isWhiteSpaceWithNewline
import com.pinterest.ktlint.rule.engine.core.api.leavesIncludingSelf
import com.pinterest.ktlint.rule.engine.core.api.nextLeaf
Expand Down Expand Up @@ -74,16 +76,12 @@ public class ParameterListWrappingRule :
emit: (offset: Int, errorMessage: String, canBeAutoCorrected: Boolean) -> Unit,
) {
when (node.elementType) {
NULLABLE_TYPE -> wrapNullableType(node, emit, autoCorrect)
VALUE_PARAMETER_LIST -> {
if (node.needToWrapParameterList()) {
wrapParameterList(node, emit, autoCorrect)
}
}
NULLABLE_TYPE -> visitNullableType(node, emit, autoCorrect)
VALUE_PARAMETER_LIST -> visitParameterList(node, emit, autoCorrect)
}
}

private fun wrapNullableType(
private fun visitNullableType(
node: ASTNode,
emit: (offset: Int, errorMessage: String, canBeAutoCorrected: Boolean) -> Unit,
autoCorrect: Boolean,
Expand Down Expand Up @@ -183,16 +181,27 @@ public class ParameterListWrappingRule :
.orEmpty()
.any { it.elementType == ElementType.ANNOTATION_ENTRY }

private fun wrapParameterList(
private fun visitParameterList(
node: ASTNode,
emit: (offset: Int, errorMessage: String, canBeAutoCorrected: Boolean) -> Unit,
autoCorrect: Boolean,
) {
node
.children()
.forEach { child -> wrapParameterInList(child, emit, autoCorrect) }
if (isPrecededByComment(node)) {
emit(node.startOffset, "Parameter list should not be preceded by a comment", false)
} else if (node.needToWrapParameterList()) {
node
.children()
.forEach { child -> wrapParameterInList(child, emit, autoCorrect) }
}
}

private fun isPrecededByComment(node: ASTNode) =
node
.prevLeaf { !it.isWhiteSpace() }
?.prevLeaf()
?.isPartOfComment()
?: false

private fun intendedIndent(child: ASTNode): String =
when {
// IDEA quirk:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,4 +111,31 @@ class KdocWrappingRuleTest {
.hasLintViolation(2, 29, "A KDoc comment may not be followed by any other element on that same line")
.isFormattedAs(formattedCode)
}

@Test
fun `Issue 2535 - Given a class with a KDoc on the primary constructor instead of on the class name`() {
val code =
// Code sample below contains a code smell. The KDoc is at the wrong position.
"""
class ClassA
/**
* some comment
*/(paramA: String)
""".trimIndent()
val formattedCode =
// Code sample below contains a code smell. The KDoc is at the wrong position. The formatted code is not intended to show
// that this code is well formatted according to good coding practices. It merely is used to validate that the newline is
// inserted at the correct position in the AST so that no exception will be thrown in the ParameterListWrappingRule.
"""
class ClassA
/**
* some comment
*/
(paramA: String)
""".trimIndent()
kdocWrappingRuleAssertThat(code)
.addAdditionalRuleProvider { ParameterListWrappingRule() }
.hasLintViolation(4, 8, "A KDoc comment may not be followed by any other element on that same line")
.isFormattedAs(formattedCode)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -702,4 +702,32 @@ class ParameterListWrappingRuleTest {
LintViolation(3, 61, "Exceeded max line length (60)", false),
).hasNoLintViolationsExceptInAdditionalRules()
}

@Test
fun `Issue 2535 - Given a class with a KDoc on the primary constructor instead of on the class name`() {
val code =
// Code sample below contains a code smell. The KDoc is at the wrong position.
"""
class ClassA
/**
* some comment
*/(paramA: String)
""".trimIndent()
val formattedCode =
// Code sample below contains a code smell. The KDoc is at the wrong position. The formatted code is not intended to show
// that this code is well formatted according to good coding practices. It merely is used to validate that the newline is
// inserted at the correct position in the AST so that no exception will be thrown in the ParameterListWrappingRule.
"""
class ClassA
/**
* some comment
*/
(paramA: String)
""".trimIndent()
parameterListWrappingRuleAssertThat(code)
.addAdditionalRuleProvider { KdocWrappingRule() }
.hasLintViolations(
LintViolation(4, 8, "Parameter list should not be preceded by a comment", false),
).isFormattedAs(formattedCode)
}
}