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

Remove spaces before primary constructor #1211

Merged
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ This project adheres to [Semantic Versioning](http://semver.org/).
- Fix false positive with lambda argument and call chain (`indent`) ([#1202](https://github.com/pinterest/ktlint/issues/1202))
- Fix trailing spaces not formatted inside block comments (`no-trailing-spaces`) ([#1197](https://github.com/pinterest/ktlint/issues/1197))
- Do not check for `.idea` folder presence when using `applyToIDEA` globally ([#1186](https://github.com/pinterest/ktlint/issues/1186))
- Remove spaces before primary constructor (`paren-spacing`) ([#1207](https://github.com/pinterest/ktlint/issues/1207))

### Changed
- Support absolute paths for globs ([#1131](https://github.com/pinterest/ktlint/issues/1131))
Expand Down
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()
)
}
}