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 ParameterListWrappingRule to correctly identify misplaced left parens #201

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 @@ -47,12 +47,21 @@ class ParameterListWrappingRule : Rule("parameter-list-wrapping") {
if (putParametersOnSeparateLines || maxLineLengthExceeded) {
// aiming for
// ... LPAR
// <LPAR line indent + indentSize> VALUE_PARAMETER...
// <LPAR line indent> RPAR
// <line indent + indentSize> VALUE_PARAMETER...
// <line indent> RPAR
val indent = "\n" + node.psi.lineIndent()
val paramIndent = indent + " ".repeat(indentSize) // single indent as recommended by Jetbrains/Google
nextChild@ for (child in node.children()) {
when (child.elementType) {
KtTokens.LPAR -> {
val prevLeaf = child.psi.prevLeaf()!!
if (prevLeaf.elementType == KtTokens.WHITE_SPACE && prevLeaf.textContains('\n')) {
emit(child.startOffset, errorMessage(child), true)
if (autoCorrect) {
prevLeaf.delete()
}
}
}
KtStubElementTypes.VALUE_PARAMETER,
KtTokens.RPAR -> {
var paramInnerIndentAdjustment = 0
Expand Down Expand Up @@ -128,6 +137,7 @@ class ParameterListWrappingRule : Rule("parameter-list-wrapping") {

private fun errorMessage(node: ASTNode) =
when (node.elementType) {
KtTokens.LPAR -> """Unnecessary newline before "(""""
KtStubElementTypes.VALUE_PARAMETER ->
"Parameter should be on a separate line (unless all parameters can fit a single line)"
KtTokens.RPAR -> """Missing newline before ")""""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -391,4 +391,92 @@ class ParameterListWrappingRuleTest {
LintError(6, 4, "parameter-list-wrapping", "Unexpected indentation (expected 4, actual 3)")
))
}

@Test
fun testLintClassDanglingLeftParen() {
assertThat(
ParameterListWrappingRule().lint(
"""
class ClassA
(
paramA: String,
paramB: String,
paramC: String
)
""".trimIndent()
)
).isEqualTo(
listOf(
LintError(2, 1, "parameter-list-wrapping", """Unnecessary newline before "("""")
)
)
}

@Test
fun testLintFunctionDanglingLeftParen() {
assertThat(
ParameterListWrappingRule().lint(
"""
fun doSomething
(
paramA: String,
paramB: String,
paramC: String
)
""".trimIndent()
)
).isEqualTo(
listOf(
LintError(2, 1, "parameter-list-wrapping", """Unnecessary newline before "("""")
)
)
}

@Test
fun testFormatClassDanglingLeftParen() {
assertThat(
ParameterListWrappingRule().format(
"""
class ClassA constructor
(
paramA: String,
paramB: String,
paramC: String
)
""".trimIndent()
)
).isEqualTo(
"""
class ClassA constructor(
paramA: String,
paramB: String,
paramC: String
)
""".trimIndent()
)
}

@Test
fun testFormatFunctionDanglingLeftParen() {
assertThat(
ParameterListWrappingRule().format(
"""
fun doSomething
(
paramA: String,
paramB: String,
paramC: String
)
""".trimIndent()
)
).isEqualTo(
"""
fun doSomething(
paramA: String,
paramB: String,
paramC: String
)
""".trimIndent()
)
}
}