Skip to content

Commit

Permalink
Bugfix. Newline at the beginning of code block is not removed if ther…
Browse files Browse the repository at this point in the history
…e are more than one (#537)

* bugfix/newline-is-not-removed(#519)

### What's done:
  * Fixed bugs
  • Loading branch information
aktsay6 authored Nov 23, 2020
1 parent 090e575 commit 0bf93cf
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 11 deletions.
16 changes: 8 additions & 8 deletions diktat-rules/src/main/kotlin/generated/WarningNames.kt
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,14 @@ public object WarningNames {

public const val GENERIC_NAME: String = "GENERIC_NAME"

public const val BACKTICKS_PROHIBITED: String = "BACKTICKS_PROHIBITED"

public const val FUNCTION_NAME_INCORRECT_CASE: String = "FUNCTION_NAME_INCORRECT_CASE"

public const val FUNCTION_BOOLEAN_PREFIX: String = "FUNCTION_BOOLEAN_PREFIX"

public const val FILE_NAME_INCORRECT: String = "FILE_NAME_INCORRECT"

public const val FILE_NAME_MATCH_CLASS: String = "FILE_NAME_MATCH_CLASS"

public const val EXCEPTION_SUFFIX: String = "EXCEPTION_SUFFIX"

public const val CONFUSING_IDENTIFIER_NAMING: String = "CONFUSING_IDENTIFIER_NAMING"
Expand Down Expand Up @@ -137,8 +137,6 @@ public object WarningNames {

public const val LONG_LINE: String = "LONG_LINE"

public const val BACKTICKS_PROHIBITED: String = "BACKTICKS_PROHIBITED"

public const val REDUNDANT_SEMICOLON: String = "REDUNDANT_SEMICOLON"

public const val WRONG_NEWLINES: String = "WRONG_NEWLINES"
Expand All @@ -165,6 +163,12 @@ public object WarningNames {

public const val LOCAL_VARIABLE_EARLY_DECLARATION: String = "LOCAL_VARIABLE_EARLY_DECLARATION"

public const val STRING_TEMPLATE_CURLY_BRACES: String = "STRING_TEMPLATE_CURLY_BRACES"

public const val STRING_TEMPLATE_QUOTES: String = "STRING_TEMPLATE_QUOTES"

public const val FILE_NAME_MATCH_CLASS: String = "FILE_NAME_MATCH_CLASS"

public const val NULLABLE_PROPERTY_TYPE: String = "NULLABLE_PROPERTY_TYPE"

public const val TYPE_ALIAS: String = "TYPE_ALIAS"
Expand All @@ -176,10 +180,6 @@ public object WarningNames {
public const val GENERIC_VARIABLE_WRONG_DECLARATION: String =
"GENERIC_VARIABLE_WRONG_DECLARATION"

public const val STRING_TEMPLATE_CURLY_BRACES: String = "STRING_TEMPLATE_CURLY_BRACES"

public const val STRING_TEMPLATE_QUOTES: String = "STRING_TEMPLATE_QUOTES"

public const val FLOAT_IN_ACCURATE_CALCULATIONS: String = "FLOAT_IN_ACCURATE_CALCULATIONS"

public const val AVOID_NULL_CHECKS: String = "AVOID_NULL_CHECKS"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,16 @@ package org.cqfn.diktat.ruleset.rules.files
import com.pinterest.ktlint.core.Rule
import com.pinterest.ktlint.core.ast.ElementType.BLOCK
import com.pinterest.ktlint.core.ast.ElementType.CLASS_BODY
import com.pinterest.ktlint.core.ast.ElementType.FILE
import com.pinterest.ktlint.core.ast.ElementType.FUNCTION_LITERAL
import com.pinterest.ktlint.core.ast.ElementType.LAMBDA_EXPRESSION
import com.pinterest.ktlint.core.ast.ElementType.LBRACE
import com.pinterest.ktlint.core.ast.ElementType.RBRACE
import com.pinterest.ktlint.core.ast.ElementType.WHITE_SPACE
import org.cqfn.diktat.common.config.rules.RulesConfig
import org.cqfn.diktat.ruleset.constants.Warnings.TOO_MANY_BLANK_LINES
import org.cqfn.diktat.ruleset.utils.findAllNodesWithSpecificType
import org.cqfn.diktat.ruleset.utils.getFirstChildWithType
import org.cqfn.diktat.ruleset.utils.leaveExactlyNumNewLines
import org.cqfn.diktat.ruleset.utils.leaveOnlyOneNewLine
import org.cqfn.diktat.ruleset.utils.numNewLines
Expand Down Expand Up @@ -39,7 +44,7 @@ class BlankLinesRule(private val configRules: List<RulesConfig>) : Rule("blank-l
}

private fun handleBlankLine(node: ASTNode) {
if (node.treeParent.elementType.let { it == BLOCK || it == CLASS_BODY }) {
if (node.treeParent.elementType.let { it == BLOCK || it == CLASS_BODY || it == FUNCTION_LITERAL }) {
if ((node.treeNext.elementType == RBRACE) xor (node.treePrev.elementType == LBRACE)) {
// if both are present, this is not beginning or end
// if both are null, then this block is empty and is handled in another rule
Expand All @@ -53,7 +58,10 @@ class BlankLinesRule(private val configRules: List<RulesConfig>) : Rule("blank-l

private fun handleTooManyBlankLines(node: ASTNode) {
TOO_MANY_BLANK_LINES.warnAndFix(configRules, emitWarn, isFixMode, "do not use more than two consecutive blank lines", node.startOffset, node) {
node.leaveExactlyNumNewLines(2)
if (node.treeParent.elementType != FILE && node.treeParent.getFirstChildWithType(WHITE_SPACE) == node)
node.leaveExactlyNumNewLines(1)
else
node.leaveExactlyNumNewLines(2)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,10 @@ class Example {
fun foo() {
bar()
}

fun bar() {
println()
println()
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,12 @@ class Example {
bar()

}

fun bar() {


println()
println()
}
}

Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
package test.paragraph3.blank_lines

class Example {

val foo = 0

fun bar() { }

fun foo() {
list.map {
bar()
}
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,12 @@ class Example {


fun bar() { }

fun foo() {
list.map {

bar()
}
}
}

0 comments on commit 0bf93cf

Please sign in to comment.