Skip to content

Commit

Permalink
Fix NoSuchElementException in trailing-comma rule in property accessor
Browse files Browse the repository at this point in the history
Closes #1280
  • Loading branch information
Paul Dingemans authored and Tapchicoma committed Dec 7, 2021
1 parent 3b27bf4 commit c9fbab9
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ This project adheres to [Semantic Versioning](https://semver.org/).

### Fixed
- Fix false positive in rule spacing-between-declarations-with-annotations ([#1281](https://github.com/pinterest/ktlint/issues/1281))
- Fix NoSuchElementException for property accessor (`trailing-comma`) ([#1280](https://github.com/pinterest/ktlint/issues/1280))

### Changed
- Update Kotlin version to `1.6.0` release
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,10 +129,12 @@ public class TrailingCommaRule :
autoCorrect: Boolean
) {
if (node.treeParent.elementType != ElementType.FUNCTION_LITERAL) {
val inspectNode = node
node
.children()
.last { it.elementType == ElementType.RPAR }
node.reportAndCorrectTrailingCommaNodeBefore(inspectNode, emit, autoCorrect)
.lastOrNull { it.elementType == ElementType.RPAR }
?.let {
node.reportAndCorrectTrailingCommaNodeBefore(it, emit, autoCorrect)
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -908,6 +908,26 @@ class TrailingCommaRuleTest {
.isEqualTo(autoCorrectedCode)
}

@Test
fun `Trailing comma is not added for property setter`() {
val code =
"""
class Test {
var foo = Bar()
set(value) {
}
}
""".trimIndent()

val editorConfigFilePath = writeEditorConfigFile(
ALLOW_TRAILING_COMMA_ON_DECLARATION_SITE, ALLOW_TRAILING_COMMA_ON_CALL_SITE
).absolutePath

assertThat(TrailingCommaRule().lint(editorConfigFilePath, code)).isEmpty()
assertThat(TrailingCommaRule().format(editorConfigFilePath, code))
.isEqualTo(code)
}

private fun writeEditorConfigFile(vararg editorConfigProperties: Pair<PropertyType<Boolean>, String>) = editorConfigTestRule
.writeToEditorConfig(
mapOf(*editorConfigProperties)
Expand Down

0 comments on commit c9fbab9

Please sign in to comment.