Skip to content

Commit

Permalink
EMPTY_BLOCK_STRUCTURE_ERROR: is not raised on empty when block
Browse files Browse the repository at this point in the history
### What's done:
* added check for WHEN element

Closes #1539
  • Loading branch information
Cheshiriks committed Jan 31, 2023
1 parent ddd8c9b commit 847f4e5
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class EmptyBlock(configRules: List<RulesConfig>) : DiktatRule(

private fun searchNode(node: ASTNode, configuration: EmptyBlockStyleConfiguration) {
val newNode = node.findLBrace()?.treeParent ?: return
if (!isAllowedEmptyBlock(newNode) && ((newNode.elementType == ElementType.WHEN && newNode.isWhenBlockEmpty()) || newNode.isBlockEmpty())) {
if (!isAllowedEmptyBlock(newNode) && newNode.isBlockEmpty()) {
checkEmptyBlock(newNode, configuration)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -246,15 +246,12 @@ fun ASTNode.hasParent(type: IElementType) = parent(type) != null
* check text because some nodes have empty BLOCK element inside (lambda)
*/
fun ASTNode?.isBlockEmpty() = this?.let {
this.text.replace("\\s+".toRegex(), "") == EMPTY_BLOCK_TEXT
} ?: true

/**
* check if WHEN element node text is empty (contains only left and right braces)
*/
fun ASTNode?.isWhenBlockEmpty() = this?.let {
val firstIndex = this.text.indexOf("{")
this.text.substring(firstIndex - 1).replace("\\s+".toRegex(), "") == EMPTY_BLOCK_TEXT
if (this.elementType == ElementType.WHEN) {
val firstIndex = this.text.indexOf("{")
this.text.substring(firstIndex - 1).replace("\\s+".toRegex(), "") == EMPTY_BLOCK_TEXT
} else {
this.text.replace("\\s+".toRegex(), "") == EMPTY_BLOCK_TEXT
}
} ?: true

/**
Expand Down

0 comments on commit 847f4e5

Please sign in to comment.