-
Notifications
You must be signed in to change notification settings - Fork 39
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Bugfix/new lines rule dot qualified expression and safe access expression #1337
Merged
Merged
Changes from 25 commits
Commits
Show all changes
32 commits
Select commit
Hold shift + click to select a range
a8c8ec0
### Whats added:
edfac82
### Whats added:
e24e06b
### Whats added:
678a95b
### Whats added:
56bef60
### Whats added:
e70e1e8
### Whats added:
89ac89e
### Whats added:
7fb1d93
### Whats added:
8f3ed88
### Whats added:
80d5e21
### Whats added:
51c7c47
### Whats added:
9e03d0a
### Whats added:
57201a6
### Whats added:
f8a817a
Merge branch 'master' into bugfix/NewLinesRuleDotQualifiedExpression
Arrgentum dfcf81a
### Whats added:
0e20d17
Merge branch 'master' into bugfix/NewLinesRuleDotQualifiedExpression
5507399
### Whats added:
b4898a8
### Whats added:
d8fb515
Correct warning message
163fb83
Corrected diktats code according to this rule
9192489
Corrected diktats code according to this rule
686f2d6
Corrected diktats code according to this rule
120d2e4
Corrected diktats code according to this rule
279d697
Corrected diktats code according to this rule
55c6e2c
Corrected the code according to the review
05c5a44
Corrected the code using check
cfbdc7d
commented logics code
593a6ed
Merge branch 'master' into bugfix/NewLinesRuleDotQualifiedExpression
Arrgentum c2c49c5
corrected code with diktat:fix
e418532
Merge remote-tracking branch 'origin/bugfix/NewLinesRuleDotQualifiedE…
0a15780
corrected code with diktat:fix
07487f1
Merge branch 'master' into bugfix/NewLinesRuleDotQualifiedExpression
Arrgentum File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,21 +8,7 @@ import org.cqfn.diktat.ruleset.constants.Warnings.COMPLEX_EXPRESSION | |
import org.cqfn.diktat.ruleset.constants.Warnings.REDUNDANT_SEMICOLON | ||
import org.cqfn.diktat.ruleset.constants.Warnings.WRONG_NEWLINES | ||
import org.cqfn.diktat.ruleset.rules.DiktatRule | ||
import org.cqfn.diktat.ruleset.utils.appendNewlineMergingWhiteSpace | ||
import org.cqfn.diktat.ruleset.utils.emptyBlockList | ||
import org.cqfn.diktat.ruleset.utils.extractLineOfText | ||
import org.cqfn.diktat.ruleset.utils.findAllDescendantsWithSpecificType | ||
import org.cqfn.diktat.ruleset.utils.findParentNodeWithSpecificType | ||
import org.cqfn.diktat.ruleset.utils.getFilePath | ||
import org.cqfn.diktat.ruleset.utils.getIdentifierName | ||
import org.cqfn.diktat.ruleset.utils.getRootNode | ||
import org.cqfn.diktat.ruleset.utils.hasParent | ||
import org.cqfn.diktat.ruleset.utils.isBeginByNewline | ||
import org.cqfn.diktat.ruleset.utils.isEol | ||
import org.cqfn.diktat.ruleset.utils.isFollowedByNewline | ||
import org.cqfn.diktat.ruleset.utils.isGradleScript | ||
import org.cqfn.diktat.ruleset.utils.isSingleLineIfElse | ||
import org.cqfn.diktat.ruleset.utils.leaveOnlyOneNewLine | ||
import org.cqfn.diktat.ruleset.utils.* | ||
|
||
import com.pinterest.ktlint.core.ast.ElementType.ANDAND | ||
import com.pinterest.ktlint.core.ast.ElementType.ARROW | ||
|
@@ -124,6 +110,7 @@ class NewlinesRule(configRules: List<RulesConfig>) : DiktatRule( | |
private val configuration by lazy { | ||
NewlinesRuleConfiguration(configRules.getRuleConfig(WRONG_NEWLINES)?.configuration ?: emptyMap()) | ||
} | ||
|
||
override fun logic(node: ASTNode) { | ||
when (node.elementType) { | ||
SEMICOLON -> handleSemicolon(node) | ||
|
@@ -135,11 +122,69 @@ class NewlinesRule(configRules: List<RulesConfig>) : DiktatRule( | |
BLOCK -> handleLambdaBody(node) | ||
RETURN -> handleReturnStatement(node) | ||
SUPER_TYPE_LIST, VALUE_PARAMETER_LIST, VALUE_ARGUMENT_LIST -> handleList(node) | ||
DOT_QUALIFIED_EXPRESSION, SAFE_ACCESS_EXPRESSION, POSTFIX_EXPRESSION -> handDotQualifiedAndSafeAccessExpression(node) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
else -> { | ||
} | ||
} | ||
} | ||
|
||
@Suppress("GENERIC_VARIABLE_WRONG_DECLARATION", "MagicNumber") | ||
private fun handDotQualifiedAndSafeAccessExpression(node: ASTNode) { | ||
val listParentTypesNoFix = listOf<IElementType>(PACKAGE_DIRECTIVE, IMPORT_DIRECTIVE, VALUE_PARAMETER_LIST, | ||
VALUE_ARGUMENT_LIST, DOT_QUALIFIED_EXPRESSION, SAFE_ACCESS_EXPRESSION, POSTFIX_EXPRESSION) | ||
if (isNotFindParentNodeWithSpecificManyType(node, listParentTypesNoFix)) { | ||
val listDot = node.findAllNodesWithCondition( | ||
withSelf = true, | ||
excludeChildrenCondition = { !isDotQuaOrSafeAccessOrPostfixExpression(it) } | ||
) { | ||
isDotQuaOrSafeAccessOrPostfixExpression(it) && it.elementType != POSTFIX_EXPRESSION | ||
}.reversed() | ||
if (listDot.size > 3) { | ||
val without = listDot.filterNot { | ||
val whiteSpaceBeforeDotOrSafeAccess = it.findChildByType(DOT)?.treePrev ?: it.findChildByType(SAFE_ACCESS)?.treePrev | ||
whiteSpaceBeforeDotOrSafeAccess?.elementType == WHITE_SPACE && whiteSpaceBeforeDotOrSafeAccess.text.lines().size > 1 | ||
} | ||
if (without.size > 1 || (without.size == 1 && without[0] != listDot[0])) { | ||
WRONG_NEWLINES.warnAndFix(configRules, emitWarn, isFixMode, "should be split before second and other dot/safe access", node.startOffset, node) { | ||
fixDotQualifiedExpression(listDot) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Return false, if you find parent with types in list else return true | ||
*/ | ||
private fun isNotFindParentNodeWithSpecificManyType(node: ASTNode, list: List<IElementType>): Boolean { | ||
list.forEach { elem -> | ||
node.findParentNodeWithSpecificType(elem)?.let { | ||
return false | ||
} | ||
} | ||
return true | ||
} | ||
|
||
/** | ||
* Fix Dot Qualified Expression and Safe Access Expression - | ||
* 1) Append new White Space node before second and subsequent node Dot or Safe Access | ||
* in Dot Qualified Expression? Safe Access Expression and Postfix Expression | ||
* 2) If before first Dot or Safe Access node stay White Space node with \n - remove this node | ||
*/ | ||
private fun fixDotQualifiedExpression(list: List<ASTNode>) { | ||
list.forEachIndexed { index, astNode -> | ||
val dotNode = astNode.getFirstChildWithType(DOT) ?: astNode.getFirstChildWithType(SAFE_ACCESS) | ||
val nodeBeforeDot = dotNode?.treePrev | ||
if (index > 0) { | ||
astNode.appendNewlineMergingWhiteSpace(nodeBeforeDot, dotNode) | ||
} | ||
} | ||
} | ||
|
||
private fun isDotQuaOrSafeAccessOrPostfixExpression(node: ASTNode) = | ||
node.elementType == DOT_QUALIFIED_EXPRESSION || node.elementType == SAFE_ACCESS_EXPRESSION || node.elementType == POSTFIX_EXPRESSION | ||
|
||
|
||
|
||
/** | ||
* Check that EOL semicolon is used only in enums | ||
*/ | ||
|
@@ -294,9 +339,13 @@ class NewlinesRule(configRules: List<RulesConfig>) : DiktatRule( | |
} | ||
} | ||
|
||
@Suppress("TOO_LONG_FUNCTION") | ||
private fun handleLambdaBody(node: ASTNode) { | ||
if (node.treeParent.elementType == FUNCTION_LITERAL) { | ||
val isSingleLineLambda = node.treeParent.text.lines().size == 1 | ||
val isSingleLineLambda = node.treeParent | ||
.text | ||
.lines() | ||
.size == 1 | ||
val arrowNode = node.siblings(false).find { it.elementType == ARROW } | ||
if (!isSingleLineLambda && arrowNode != null) { | ||
// lambda with explicit arguments | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Now, when we have an editor config - we should prohibit wildcard imports
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please enable it in diktat-analysis
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it's in the excludes, sorry, no need to change