Skip to content
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

Wrong newlines in functional style #346

Merged
merged 14 commits into from
Oct 7, 2020
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import com.pinterest.ktlint.core.ast.ElementType.IDENTIFIER
import com.pinterest.ktlint.core.ast.ElementType.IF
import com.pinterest.ktlint.core.ast.ElementType.IMPORT_DIRECTIVE
import com.pinterest.ktlint.core.ast.ElementType.LAMBDA_ARGUMENT
import com.pinterest.ktlint.core.ast.ElementType.LBRACE
import com.pinterest.ktlint.core.ast.ElementType.LPAR
import com.pinterest.ktlint.core.ast.ElementType.MINUS
import com.pinterest.ktlint.core.ast.ElementType.MINUSEQ
Expand All @@ -40,9 +41,11 @@ import com.pinterest.ktlint.core.ast.ElementType.SAFE_ACCESS
import com.pinterest.ktlint.core.ast.ElementType.SAFE_ACCESS_EXPRESSION
import com.pinterest.ktlint.core.ast.ElementType.SECONDARY_CONSTRUCTOR
import com.pinterest.ktlint.core.ast.ElementType.SEMICOLON
import com.pinterest.ktlint.core.ast.ElementType.VALUE_ARGUMENT
import com.pinterest.ktlint.core.ast.ElementType.VALUE_ARGUMENT_LIST
import com.pinterest.ktlint.core.ast.ElementType.VALUE_PARAMETER_LIST
import com.pinterest.ktlint.core.ast.ElementType.WHITE_SPACE
import com.pinterest.ktlint.core.ast.isWhiteSpaceWithNewline
import com.pinterest.ktlint.core.ast.nextCodeSibling
import com.pinterest.ktlint.core.ast.parent
import com.pinterest.ktlint.core.ast.prevCodeSibling
Expand All @@ -52,6 +55,7 @@ import org.cqfn.diktat.ruleset.constants.Warnings.WRONG_NEWLINES
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.findAllNodesWithSpecificType
import org.cqfn.diktat.ruleset.utils.getAllLeafsWithSpecificType
import org.cqfn.diktat.ruleset.utils.isBeginByNewline
import org.cqfn.diktat.ruleset.utils.isEol
Expand Down Expand Up @@ -141,6 +145,7 @@ class NewlinesRule(private val configRules: List<RulesConfig>) : Rule("newlines"
}
}

@Suppress("ComplexMethod")
private fun handleOperatorWithLineBreakBefore(node: ASTNode) {
if (node.isDotFromPackageOrImport()) {
return
Expand Down Expand Up @@ -305,6 +310,20 @@ class NewlinesRule(private val configRules: List<RulesConfig>) : Rule("newlines"
getAllLeafsWithSpecificType(SAFE_ACCESS, it)
}
}
?.filter {
aktsay6 marked this conversation as resolved.
Show resolved Hide resolved
aktsay6 marked this conversation as resolved.
Show resolved Hide resolved
// should skip node's that are val params. For example: b.some(z.a()) -> skip z.a()
if (it.treeParent.treeParent != null && it.treeParent.treeParent.elementType == VALUE_ARGUMENT)
return@filter false
true
aktsay6 marked this conversation as resolved.
Show resolved Hide resolved
}
?.dropWhile { !it.treeParent.textContains('(') && !it.treeParent.textContains('{') }
// fixme: we can't distinguish fully qualified names from chain of calls for now
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// fixme: we can't distinguish fully qualified names from chain of calls for now
// fixme: we can't distinguish fully qualified names (like java.lang) from chain of property accesses (like list.size) for now

Just to be sure we don't forget what this was about

?.filterIndexed { index, it ->
// if first callee is multiline lambda, then we let user decide how to line lambda
if (index == 0 && isMultilineLambda(it.treeParent))
return@filterIndexed false
aktsay6 marked this conversation as resolved.
Show resolved Hide resolved
true
}
?.filter { it.getParentExpressions().count() > 1 }
?.count()
?.let { it > 1 }
Expand All @@ -313,6 +332,10 @@ class NewlinesRule(private val configRules: List<RulesConfig>) : Rule("newlines"
private fun ASTNode.getParentExpressions() =
parents().takeWhile { it.elementType in chainExpressionTypes && it.elementType != LAMBDA_ARGUMENT }

private fun isMultilineLambda(node: ASTNode): Boolean =
node.findAllNodesWithSpecificType(LAMBDA_ARGUMENT).firstOrNull()?.text?.count { it == '\n' } ?: -1 > 0
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Won't findAllNodesWithSpecificType trigger on some nested lambdas or lambdas further in a call chain?



/**
* This method should be called on OPERATION_REFERENCE in the middle of BINARY_EXPRESSION
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -502,4 +502,56 @@ class NewlinesRuleWarnTest : LintTestBase(::NewlinesRule) {
LintError(2, 5, ruleId, singleReturnWarn, true)
)
}

@Test
@Tag(WarningNames.WRONG_NEWLINES)
fun `should not trigger`() {
lintMethod(
"""
|fun foo(): String {
| val isParallelMode: Boolean
| get() = java.lang.Boolean.getBoolean(properties.getProperty("parallel.mode"))
|
| allProperties.filter {
aktsay6 marked this conversation as resolved.
Show resolved Hide resolved
| predicate(it)
| }
| .foo()
| .bar()
|
| allProperties
| .filter {
| predicate(it)
| }
| .foo()
| .bar()
|
| allProperties?.filter {
| predicate(it)
| }
| .foo()
| .bar()
aktsay6 marked this conversation as resolved.
Show resolved Hide resolved
|}
""".trimMargin()
)
}

@Test
@Tag(WarningNames.WRONG_NEWLINES)
fun `should trigger on non-multiline lambdas`() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add example like

list.filter {

}
.map(::foo).filter {
    bar()
}

It should be considered incorrect, when multiline lambda is not the first call.

lintMethod(
"""
|fun foo(): String {
| allProperties.filter { predicate(it) }
| .foo()
| .bar()
|
| allProperties?.filter { predicate(it) }
| .foo()
| .bar()
|}
""".trimMargin(),
LintError(2, 22, ruleId,"${WRONG_NEWLINES.warnText()} should follow functional style at .", true),
LintError(6, 22, ruleId,"${WRONG_NEWLINES.warnText()} should follow functional style at ?.", true)
)
}
}