Skip to content

Commit

Permalink
Do not remove parenthesis after explicit class constructor without ar…
Browse files Browse the repository at this point in the history
…guments (#2226)
  • Loading branch information
paul-dingemans authored Sep 4, 2023
1 parent ff3f6f5 commit 0bab9b8
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ 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 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 @@ -6,6 +6,7 @@ import com.pinterest.ktlint.rule.engine.core.api.ElementType.CLASS
import com.pinterest.ktlint.rule.engine.core.api.ElementType.CLASS_BODY
import com.pinterest.ktlint.rule.engine.core.api.ElementType.COLON
import com.pinterest.ktlint.rule.engine.core.api.ElementType.COMMA
import com.pinterest.ktlint.rule.engine.core.api.ElementType.CONSTRUCTOR_KEYWORD
import com.pinterest.ktlint.rule.engine.core.api.ElementType.MODIFIER_LIST
import com.pinterest.ktlint.rule.engine.core.api.ElementType.PRIMARY_CONSTRUCTOR
import com.pinterest.ktlint.rule.engine.core.api.ElementType.RPAR
Expand Down Expand Up @@ -39,6 +40,7 @@ import com.pinterest.ktlint.rule.engine.core.api.isWhiteSpaceWithNewline
import com.pinterest.ktlint.rule.engine.core.api.nextCodeLeaf
import com.pinterest.ktlint.rule.engine.core.api.nextCodeSibling
import com.pinterest.ktlint.rule.engine.core.api.nextLeaf
import com.pinterest.ktlint.rule.engine.core.api.prevCodeSibling
import com.pinterest.ktlint.rule.engine.core.api.prevLeaf
import com.pinterest.ktlint.rule.engine.core.api.prevSibling
import com.pinterest.ktlint.rule.engine.core.api.upsertWhitespaceAfterMe
Expand Down Expand Up @@ -251,7 +253,11 @@ public class ClassSignatureRule :
node
.getPrimaryConstructorParameterListOrNull()
?.takeUnless { it.containsComment() }
?.let { parameterList ->
?.takeUnless {
// Allow:
// class Foo constructor() { ... }
it.prevCodeSibling()?.elementType == CONSTRUCTOR_KEYWORD
}?.let { parameterList ->
if (!dryRun) {
emit(parameterList.startOffset, "No parenthesis expected", true)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1684,6 +1684,17 @@ class ClassSignatureRuleTest {
).isFormattedAs(formattedCode)
}

@Test
fun `Given a class with an explicit constructor not having arguments`() {
val code =
"""
class Foo constructor() {
fun bar()
}
""".trimIndent()
classSignatureWrappingRuleAssertThat(code).hasNoLintViolations()
}

private companion object {
const val UNEXPECTED_SPACES = " "
const val NO_SPACE = ""
Expand Down

0 comments on commit 0bab9b8

Please # to comment.