Skip to content

Commit

Permalink
Bugfix: Checking white space before opening brace that starts a line (#…
Browse files Browse the repository at this point in the history
…1354)

### What's done:
* Fixed logic
* Added test

This pull request closes #1313
  • Loading branch information
petertrr authored Jun 30, 2022
1 parent 43e6098 commit cfd9eab
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -225,16 +225,19 @@ class WhiteSpaceRule(configRules: List<RulesConfig>) : DiktatRule(
.parent({ it.elementType == VALUE_ARGUMENT })
.let { it?.prevSibling { prevNode -> prevNode.elementType == COMMA } == null }

// If it is lambda, then we don't force it to be on newline or same line
// Handling this case: `foo({ it.bar() }, 2, 3)`
if (numWhiteSpace != 0 && isFirstArgument) {
WRONG_WHITESPACE.warnAndFix(configRules, emitWarn, isFixMode, "there should be no whitespace before '{' of lambda" +
" inside argument list", node.startOffset, node) {
whitespaceOrPrevNode.treeParent.removeChild(whitespaceOrPrevNode)
}
}
} else if (prevNode.elementType !in keywordsWithSpaceAfter && numWhiteSpace != 1) {
WRONG_WHITESPACE.warnAndFix(configRules, emitWarn, isFixMode, "there should be a whitespace before '{'", node.startOffset, node) {
prevNode.leaveSingleWhiteSpace()
val hasOnlyWhiteSpaceBefore = whitespaceOrPrevNode.elementType == WHITE_SPACE && whitespaceOrPrevNode.textContains('\n')
if (!hasOnlyWhiteSpaceBefore) {
WRONG_WHITESPACE.warnAndFix(configRules, emitWarn, isFixMode, "there should be a whitespace before '{'", node.startOffset, node) {
prevNode.leaveSingleWhiteSpace()
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -670,4 +670,18 @@ class WhiteSpaceRuleWarnTest : LintTestBase(::WhiteSpaceRule) {
""".trimMargin()
)
}

@Test
@Tag(WarningNames.WRONG_WHITESPACE)
fun `should not trigger in braces on the beginning of the line`() {
lintMethod(
"""
|val onClick: () -> Unit = remember {
| {
| /* do stuff */
| }
|}
""".trimMargin()
)
}
}

0 comments on commit cfd9eab

Please sign in to comment.