Skip to content

Commit

Permalink
Remove spaces before primary constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
Paul Dingemans committed Aug 9, 2021
1 parent 946283e commit ccb2047
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import com.pinterest.ktlint.core.ast.ElementType.FUNCTION_TYPE
import com.pinterest.ktlint.core.ast.ElementType.IDENTIFIER
import com.pinterest.ktlint.core.ast.ElementType.KDOC_START
import com.pinterest.ktlint.core.ast.ElementType.LPAR
import com.pinterest.ktlint.core.ast.ElementType.PRIMARY_CONSTRUCTOR
import com.pinterest.ktlint.core.ast.ElementType.RPAR
import com.pinterest.ktlint.core.ast.ElementType.SUPER_KEYWORD
import com.pinterest.ktlint.core.ast.ElementType.VALUE_ARGUMENT_LIST
Expand Down Expand Up @@ -38,7 +39,8 @@ class SpacingAroundParensRule : Rule("paren-spacing") {
// val foo: @Composable () -> Unit
node.treeParent?.treeParent?.elementType != FUNCTION_TYPE ||
// Super keyword needs special-casing
prevLeaf.prevLeaf()?.elementType == SUPER_KEYWORD
prevLeaf.prevLeaf()?.elementType == SUPER_KEYWORD ||
prevLeaf.prevLeaf()?.treeParent?.elementType == PRIMARY_CONSTRUCTOR
) &&
(
node.treeParent?.elementType == VALUE_PARAMETER_LIST ||
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.pinterest.ktlint.ruleset.standard

import com.pinterest.ktlint.core.LintError
import com.pinterest.ktlint.test.diffFileFormat
import com.pinterest.ktlint.test.diffFileLint
import com.pinterest.ktlint.test.format
Expand Down Expand Up @@ -89,4 +90,39 @@ class SpacingAroundParensRuleTest {
""".trimIndent()
assertThat(SpacingAroundParensRule().format(code)).isEqualTo(code)
}

@Test
fun `lint trailing spaces after constructor`() {
val code =
"""
class SomeClass constructor ()
""".trimIndent()

val actual = SpacingAroundParensRule().lint(code)

assertThat(actual)
.isEqualTo(
listOf(
LintError(1, 28, "paren-spacing", """Unexpected spacing before "(""""),
)
)
}

@Test
fun `format trailing spaces after constructor`() {
val code =
"""
class SomeClass constructor ()
""".trimIndent()

val actual = SpacingAroundParensRule().format(code)

assertThat(actual)
.isEqualTo(
"""
class SomeClass constructor()
""".trimIndent()
)
}
}

0 comments on commit ccb2047

Please sign in to comment.