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

New line in script #886

Merged
merged 5 commits into from
May 13, 2021
Merged
Show file tree
Hide file tree
Changes from all 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 @@ -8,10 +8,7 @@ import org.cqfn.diktat.common.config.rules.RulesConfig
import org.cqfn.diktat.common.config.rules.getRuleConfig
import org.cqfn.diktat.ruleset.constants.Warnings.WRONG_INDENTATION
import org.cqfn.diktat.ruleset.rules.DiktatRule
import org.cqfn.diktat.ruleset.utils.getAllChildrenWithType
import org.cqfn.diktat.ruleset.utils.getAllLeafsWithSpecificType
import org.cqfn.diktat.ruleset.utils.getFilePath
import org.cqfn.diktat.ruleset.utils.indentBy
import org.cqfn.diktat.ruleset.utils.*
import org.cqfn.diktat.ruleset.utils.indentation.ArrowInWhenChecker
import org.cqfn.diktat.ruleset.utils.indentation.AssignmentOperatorChecker
import org.cqfn.diktat.ruleset.utils.indentation.ConditionalsAndLoopsWithoutBracesChecker
Expand All @@ -23,7 +20,6 @@ import org.cqfn.diktat.ruleset.utils.indentation.IndentationConfig
import org.cqfn.diktat.ruleset.utils.indentation.KdocIndentationChecker
import org.cqfn.diktat.ruleset.utils.indentation.SuperTypeListChecker
import org.cqfn.diktat.ruleset.utils.indentation.ValueParameterListChecker
import org.cqfn.diktat.ruleset.utils.leaveOnlyOneNewLine

import com.pinterest.ktlint.core.ast.ElementType.CALL_EXPRESSION
import com.pinterest.ktlint.core.ast.ElementType.DOT_QUALIFIED_EXPRESSION
Expand Down Expand Up @@ -72,6 +68,7 @@ import kotlin.math.abs
* Additionally, a set of CustomIndentationChecker objects checks all WHITE_SPACE node if they are exceptions from general rules.
* @see CustomIndentationChecker
*/
@Suppress("LargeClass")
class IndentationRule(configRules: List<RulesConfig>) : DiktatRule(
"indentation",
configRules,
Expand Down Expand Up @@ -136,7 +133,7 @@ class IndentationRule(configRules: List<RulesConfig>) : DiktatRule(
*/
private fun checkNewlineAtEnd(node: ASTNode) {
if (configuration.newlineAtEnd) {
val lastChild = node.lastChildNode
val lastChild = generateSequence(node) { it.lastChildNode }.last()
val numBlankLinesAfter = lastChild.text.count { it == '\n' }
if (lastChild.elementType != WHITE_SPACE || numBlankLinesAfter != 1) {
val warnText = if (lastChild.elementType != WHITE_SPACE || numBlankLinesAfter == 0) "no newline" else "too many blank lines"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import generated.WarningNames
import org.junit.jupiter.api.Tag
import org.junit.jupiter.api.Test

@Suppress("LargeClass")
class IndentationRuleWarnTest : LintTestBase(::IndentationRule) {
private val ruleId = "$DIKTAT_RULE_SET_ID:indentation"
private val rulesConfigList = listOf(
Expand Down Expand Up @@ -631,5 +632,29 @@ class IndentationRuleWarnTest : LintTestBase(::IndentationRule) {
)
}

@Test
@Tag(WarningNames.WRONG_INDENTATION)
fun `check script`() {
lintMethod(
"""
|val q = 1
|
""".trimMargin(),
fileName = "src/main/kotlin/org/cqfn/diktat/Example.kts"
)
}

@Test
@Tag(WarningNames.WRONG_INDENTATION)
fun `check gradle script`() {
lintMethod(
"""
|projectName = "diKTat"
|
""".trimMargin(),
fileName = "src/main/kotlin/org/cqfn/diktat/build.gradle.kts"
)
}

private fun warnText(expected: Int, actual: Int) = "${WRONG_INDENTATION.warnText()} expected $expected but was $actual"
}