Skip to content

Commit

Permalink
Do not report false positives type-argument-list-spacing and `type-…
Browse files Browse the repository at this point in the history
…parameter-list-spacing` (#2303)

Closes #2299
  • Loading branch information
paul-dingemans authored Oct 13, 2023
1 parent b39d63c commit 04dde90
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 11 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ This project adheres to [Semantic Versioning](https://semver.org/).
* Ignore function naming in Kotest classes `function-naming` [#2289](https://github.com/pinterest/ktlint/issue/2289)
* Prevent wrapping of nested multiline binary expression before operation reference as it results in a compilation error `multiline-expression-wrapping` [#2286](https://github.com/pinterest/ktlint/issue/2286)
* Force blank line before object declaration if preceded by another declaration `blank-line-before-declaration` [#2284](https://github.com/pinterest/ktlint/issues/2284)
* Fix malformed AST when `&&` or `||` is at start of line `chain-wrapping` [#2297](https://github.com/pinterest/ktlint/issues/2297)
* Fix malformed AST when `&&` or `||` is at start of line `chain-wrapping` [#2297](https://github.com/pinterest/ktlint/issues/2297)
* Do not report false positives `type-argument-list-spacing` and `type-parameter-list-spacing` [#2299](https://github.com/pinterest/ktlint/issues/2299)

### Changed

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import com.pinterest.ktlint.rule.engine.core.api.editorconfig.INDENT_STYLE_PROPE
import com.pinterest.ktlint.rule.engine.core.api.findCompositeParentElementOfType
import com.pinterest.ktlint.rule.engine.core.api.isPartOfCompositeElementOfType
import com.pinterest.ktlint.rule.engine.core.api.isWhiteSpace
import com.pinterest.ktlint.rule.engine.core.api.isWhiteSpaceWithoutNewline
import com.pinterest.ktlint.rule.engine.core.api.nextLeaf
import com.pinterest.ktlint.rule.engine.core.api.nextSibling
import com.pinterest.ktlint.rule.engine.core.api.prevLeaf
Expand Down Expand Up @@ -109,9 +110,13 @@ public class TypeArgumentListSpacingRule :
?.let { nextSibling ->
if (multiline) {
if (nextSibling.text != expectedIndent) {
emit(nextSibling.startOffset, "Expected newline", true)
if (autoCorrect) {
nextSibling.upsertWhitespaceAfterMe(expectedIndent)
if (nextSibling.isWhiteSpaceWithoutNewline()) {
emit(nextSibling.startOffset, "Expected newline", true)
if (autoCorrect) {
nextSibling.upsertWhitespaceAfterMe(expectedIndent)
}
} else {
// Let Indentation rule fix the indentation
}
}
} else {
Expand All @@ -129,9 +134,13 @@ public class TypeArgumentListSpacingRule :
?.let { prevSibling ->
if (multiline) {
if (prevSibling.text != expectedIndent) {
emit(prevSibling.startOffset, "Expected newline", true)
if (autoCorrect) {
prevSibling.upsertWhitespaceBeforeMe(expectedIndent)
if (prevSibling.isWhiteSpaceWithoutNewline()) {
emit(prevSibling.startOffset, "Expected newline", true)
if (autoCorrect) {
prevSibling.upsertWhitespaceBeforeMe(expectedIndent)
}
} else {
// Let Indentation rule fix the indentation
}
}
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import com.pinterest.ktlint.rule.engine.core.api.SinceKtlint.Status.STABLE
import com.pinterest.ktlint.rule.engine.core.api.editorconfig.EditorConfig
import com.pinterest.ktlint.rule.engine.core.api.editorconfig.INDENT_SIZE_PROPERTY
import com.pinterest.ktlint.rule.engine.core.api.editorconfig.INDENT_STYLE_PROPERTY
import com.pinterest.ktlint.rule.engine.core.api.isWhiteSpaceWithoutNewline
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.nextSibling
Expand Down Expand Up @@ -222,7 +223,7 @@ public class TypeParameterListSpacingRule :
}
}

expectedWhitespace.startsWith("\n") -> {
node.isWhiteSpaceWithoutNewline() && expectedWhitespace.startsWith("\n") -> {
emit(
node.startOffset,
"Expected a newline",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,22 @@ class TypeArgumentListSpacingRuleTest {
""".trimIndent()
typeArgumentListSpacingRuleAssertThat(code).hasNoLintViolations()
}

@Test
fun `Issue 2299 - Given a correctly indented multiline type argument list then do not report violations`() {
val code =
"""
class FooBar(
foo: String,
bar: Int,
) : Baz<
String,
Int,
>(
foo,
bar,
)
""".trimIndent()
typeArgumentListSpacingRuleAssertThat(code).hasNoLintViolations()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -283,16 +283,27 @@ class TypeParameterListSpacingRuleTest {
}

@Test
fun `Issue 1867 - Given a multiline type parameter list which is correctly formatted then do not report a violation`() {
fun `Issue 1867 - Given a function with multiline type parameter list which is correctly formatted then do not report a violation`() {
val code =
"""
fun <
Foo,
Bar,
> foobar()
""".trimIndent()
typeParameterListSpacingRuleAssertThat(code)
.hasNoLintViolations()
typeParameterListSpacingRuleAssertThat(code).hasNoLintViolations()
}

@Test
fun `Issue 2299 - Given a class with multiline type parameter list which is correctly formatted then do not report a violation`() {
val code =
"""
class FooBar<
Foo,
Bar,
>
""".trimIndent()
typeParameterListSpacingRuleAssertThat(code).hasNoLintViolations()
}

@Test
Expand Down

0 comments on commit 04dde90

Please sign in to comment.