Skip to content

Commit

Permalink
merged condition and other logics, fixed recursive logic, renamed fun…
Browse files Browse the repository at this point in the history
…ction, delete IElementType

 ### Whats added:
 * corrected logic fix and warn String Template in LineLength rule
 * added logic fix and warn long Dot Qualified Expression and Safe Access Expression in LineLength rule
 * added logic fix and warn Value Arguments List in LineLength rule
 * added and corrected fix and warn tests in LineLength rule
 * fix code with diktat:fix@diktat
 * added comments to classes and functions
 * corrected code with running detect
 * corrected commented code

 ### Issue (#1243)
  • Loading branch information
Arrgentum committed Jun 1, 2022
1 parent 7d88336 commit 7c1675c
Showing 1 changed file with 6 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ class LineLength(configRules: List<RulesConfig>) : DiktatRule(
when (parent.elementType) {
BINARY_EXPRESSION, PARENTHESIZED -> {
val parentIsValArgListOrFunLitOrWhenEntry = listOf<IElementType>(VALUE_ARGUMENT_LIST, FUNCTION_LITERAL, WHEN_CONDITION_WITH_EXPRESSION)
findParentNodeWithSpecificTypeMany(parent, parentIsValArgListOrFunLitOrWhenEntry)?.let {
findParentNodeMatching(parent, parentIsValArgListOrFunLitOrWhenEntry)?.let {
parent = it
} ?: run {
val splitOffset = searchRightSplitAfterOperationReference(parent, configuration)?.second
Expand All @@ -161,7 +161,6 @@ class LineLength(configRules: List<RulesConfig>) : DiktatRule(
}
}
FUN, PROPERTY -> return checkFunAndProperty(parent)
CONDITION -> return checkCondition(parent, configuration)
VALUE_ARGUMENT_LIST -> parent.findParentNodeWithSpecificType(BINARY_EXPRESSION)?.let {
parent = it
} ?: return checkArgumentsList(parent, configuration)
Expand All @@ -171,8 +170,8 @@ class LineLength(configRules: List<RulesConfig>) : DiktatRule(
FUNCTION_LITERAL -> return Lambda(parent)
STRING_TEMPLATE, DOT_QUALIFIED_EXPRESSION, SAFE_ACCESS_EXPRESSION -> {
stringOrDot = parent
val parentIsBinExpOrValArgListOrWhenEntry = listOf<IElementType>(BINARY_EXPRESSION, VALUE_ARGUMENT_LIST, WHEN_CONDITION_WITH_EXPRESSION)
findParentNodeWithSpecificTypeMany(parent, parentIsBinExpOrValArgListOrWhenEntry)?.let {
val parentIsBinExpOrValArgListOrWhenEntry = listOf(BINARY_EXPRESSION, VALUE_ARGUMENT_LIST, WHEN_CONDITION_WITH_EXPRESSION)
findParentNodeMatching(parent, parentIsBinExpOrValArgListOrWhenEntry)?.let {
parent = it
} ?: run {
val returnElem = checkStringTemplateAndDotQualifiedExpression(parent, configuration)
Expand All @@ -188,7 +187,7 @@ class LineLength(configRules: List<RulesConfig>) : DiktatRule(
return None()
}

private fun findParentNodeWithSpecificTypeMany(node: ASTNode, listType: List<IElementType>): ASTNode? {
private fun findParentNodeMatching(node: ASTNode, listType: List<IElementType>): ASTNode? {
listType.forEach { type ->
node.findParentNodeWithSpecificType(type)?.let {
return it
Expand Down Expand Up @@ -225,7 +224,7 @@ class LineLength(configRules: List<RulesConfig>) : DiktatRule(
configuration: LineLengthConfiguration
): LongLineFixableCases {
val isPropertyOrFun = listOf<IElementType>(PROPERTY, FUN)
val funOrPropertyNode = findParentNodeWithSpecificTypeMany(node, isPropertyOrFun)
val funOrPropertyNode = findParentNodeMatching(node, isPropertyOrFun)
funOrPropertyNode?.let {
if (it.hasChildOfType(EQ)) {
val positionByOffset = positionByOffset(it.getFirstChildWithType(EQ)?.startOffset ?: 0).second
Expand Down Expand Up @@ -334,16 +333,6 @@ class LineLength(configRules: List<RulesConfig>) : DiktatRule(
return Comment(wrongNode, isNewLine, indexLastSpace + stringBeforeCommentContent.length)
}

private fun checkCondition(wrongNode: ASTNode, configuration: LineLengthConfiguration): LongLineFixableCases {
val leftOffset = positionByOffset(wrongNode.firstChildNode.startOffset).second
val binList: MutableList<ASTNode> = mutableListOf()
searchBinaryExpression(wrongNode, binList)
if (binList.size == 1) {
return BinaryExpression(wrongNode)
}
return LongBinaryExpression(wrongNode, configuration, leftOffset, binList)
}

// fixme json method
private fun isKdocValid(node: ASTNode) = try {
if (node.elementType == KDOC_TEXT) {
Expand Down Expand Up @@ -553,10 +542,6 @@ class LineLength(configRules: List<RulesConfig>) : DiktatRule(
private fun searchBinaryExpression(node: ASTNode, binList: MutableList<ASTNode>) {
if (node.hasChildOfType(BINARY_EXPRESSION) || node.hasChildOfType(PARENTHESIZED) || node.hasChildOfType(POSTFIX_EXPRESSION)) {
node.getChildren(null)
.filter {
it.elementType == BINARY_EXPRESSION || it.elementType == PARENTHESIZED ||
it.elementType == POSTFIX_EXPRESSION
}
.forEach {
searchBinaryExpression(it, binList)
}
Expand All @@ -567,9 +552,6 @@ class LineLength(configRules: List<RulesConfig>) : DiktatRule(
}
}

private fun isTypeDotQuaOrSafeAccessOrPostfixExpression(node: ASTNode): Boolean =
node.elementType == DOT_QUALIFIED_EXPRESSION || node.elementType == SAFE_ACCESS_EXPRESSION || node.elementType == POSTFIX_EXPRESSION

/**
* This method uses recursion to store dot qualified expression node in the order in which they are located
* Also dotList contains nodes with PREFIX_EXPRESSION element type ( !isFoo(), !isValid))
Expand All @@ -578,11 +560,8 @@ class LineLength(configRules: List<RulesConfig>) : DiktatRule(
*@param dotList mutable list of ASTNode to store nodes
*/
private fun searchDotOrSafeAccess(node: ASTNode, dotList: MutableList<ASTNode>) {
if (isTypeDotQuaOrSafeAccessOrPostfixExpression(node)) {
if (node.elementType == DOT_QUALIFIED_EXPRESSION || node.elementType == SAFE_ACCESS_EXPRESSION || node.elementType == POSTFIX_EXPRESSION) {
node.getChildren(null)
.filter {
isTypeDotQuaOrSafeAccessOrPostfixExpression(it)
}
.forEach {
searchDotOrSafeAccess(it, dotList)
}
Expand Down

0 comments on commit 7c1675c

Please sign in to comment.