Skip to content

Commit

Permalink
Merge branch 'master' into bugfix/false-positive-local-variable-early…
Browse files Browse the repository at this point in the history
…-declaration(#488)
  • Loading branch information
aktsay6 authored Nov 23, 2020
2 parents ab52690 + 0dd9f90 commit 4d01cc0
Show file tree
Hide file tree
Showing 16 changed files with 85 additions and 14 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 @@ -107,10 +107,10 @@ class DiktatRuleSetProvider(private val diktatConfigFile: String = "diktat-analy
::ExtensionFunctionsSameNameRule,
// formatting: moving blocks, adding line breaks, indentations etc.
::ConsecutiveSpacesRule,
::WhiteSpaceRule, // this rule should be after other rules that can cause wrong spacing
::HeaderCommentRule,
::FileStructureRule, // this rule should be right before indentation because it should operate on already valid code
::NewlinesRule, // newlines need to be inserted right before fixing indentation
::WhiteSpaceRule, // this rule should be after other rules that can cause wrong spacing
::IndentationRule // indentation rule should be the last because it fixes formatting after all the changes done by previous rules
)
.map {
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 @@ -297,7 +297,7 @@ class NewlinesRule(private val configRules: List<RulesConfig>) : Rule("newlines"
val colon = funNode.findChildByType(COLON)!!
val expression = node.findChildByType(RETURN_KEYWORD)!!.nextCodeSibling()!!
funNode.apply {
removeRange(colon, null)
removeRange(if (colon.treePrev.elementType == WHITE_SPACE) colon.treePrev else colon, null)
addChild(PsiWhiteSpaceImpl(" "), null)
addChild(LeafPsiElement(EQ, "="), null)
addChild(PsiWhiteSpaceImpl(" "), null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package org.cqfn.diktat.ruleset.utils

import com.pinterest.ktlint.core.ast.ElementType.LBRACE
import com.pinterest.ktlint.core.ast.ElementType.RBRACE
import com.pinterest.ktlint.core.ast.ElementType.SEMICOLON
import com.pinterest.ktlint.core.ast.ElementType.WHITE_SPACE

/**
Expand All @@ -15,7 +16,7 @@ internal const val SET_PREFIX = "set"
/**
* List of element types present in empty code block `{ }`
*/
val emptyBlockList = listOf(LBRACE, WHITE_SPACE, RBRACE)
val emptyBlockList = listOf(LBRACE, WHITE_SPACE, SEMICOLON, RBRACE)

internal const val EMPTY_BLOCK_TEXT = "{}"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,10 @@ class NewlinesRuleFixTest : FixTestBase("test/paragraph3/newlines", ::NewlinesRu
fun `should insert newlines in a long parameter or supertype list`() {
fixAndCompare("ParameterListExpected.kt", "ParameterListTest.kt")
}

@Test
@Tag(WarningNames.WRONG_NEWLINES)
fun `should fix one line function with and without semicolon`() {
fixAndCompare("OneLineFunctionExpected.kt", "OneLineFunctionTest.kt")
}
}
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()
}
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,5 @@ fun foo(list: List<Bar>?) {
?.qux()
?:foobar
}

fun bar(x :Int,y:Int) = x+ y
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,6 @@ fun foo(list: List<Bar>?) {
?:
foobar
}

fun bar(x :Int,y:Int) :Int {
return x+ y }
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package test.paragraph3.newlines

class Example {
fun doubleA() = 2 * a
fun doubleA() = 2 * a
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package test.paragraph3.newlines

class Example {
fun doubleA(): Int {
return 2 * a;
}
fun doubleA(): Int {
return 2 * a
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,8 @@ private fun foo(node: ASTNode) {
// this is a generated else block
}
}
val qwe = a && b
val qwe = a &&
b
}

Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,8 @@ private fun foo (node: ASTNode) {
when (node.elementType) {
CLASS, FUN, PRIMARY_CONSTRUCTOR, SECONDARY_CONSTRUCTOR -> checkAnnotation(node)
}
val qwe = a
&& b
val qwe = a &&
b
}

0 comments on commit 4d01cc0

Please sign in to comment.