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

Don't use PSI for line number calculation #950

Merged
merged 5 commits into from
Jul 2, 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 @@ -96,6 +96,11 @@ class LocalVariablesRule(configRules: List<RulesConfig>) : DiktatRule(
checkLineNumbers(property, firstUsageStatementLine, firstUsageLine = firstUsage.node.getLineNumber(), offset = offset)
}

/**
* Check declarations, for which the properties are used on the same line.
* If properties are used for the first time in the same statement, then they can be declared on consecutive lines
* with maybe empty lines in between.
*/
@Suppress("TOO_LONG_FUNCTION")
private fun handleConsecutiveDeclarations(statement: PsiElement, properties: List<KtProperty>) {
val numLinesAfterLastProp =
Expand Down Expand Up @@ -173,6 +178,8 @@ class LocalVariablesRule(configRules: List<RulesConfig>) : DiktatRule(

/**
* Returns the [KtBlockExpression] with which a property should be compared.
* If the usage is in nested block, compared to declaration, then statement from declaration scope, which contains block
* with usage, is returned.
*
* @return either the line on which the property is used if it is first used in the same scope, or the block in the same scope as declaration
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ import com.pinterest.ktlint.core.ast.isLeaf
import com.pinterest.ktlint.core.ast.isPartOfComment
import com.pinterest.ktlint.core.ast.isRoot
import com.pinterest.ktlint.core.ast.isWhiteSpace
import com.pinterest.ktlint.core.ast.lineNumber
import com.pinterest.ktlint.core.ast.parent
import org.jetbrains.kotlin.com.intellij.lang.ASTNode
import org.jetbrains.kotlin.com.intellij.psi.TokenType
Expand All @@ -52,7 +51,6 @@ import org.jetbrains.kotlin.psi.KtAnnotationEntry
import org.jetbrains.kotlin.psi.KtClass
import org.jetbrains.kotlin.psi.KtIfExpression
import org.jetbrains.kotlin.psi.psiUtil.children
import org.jetbrains.kotlin.psi.psiUtil.endOffset
import org.jetbrains.kotlin.psi.psiUtil.parents
import org.jetbrains.kotlin.psi.psiUtil.siblings
import org.slf4j.Logger
Expand Down Expand Up @@ -763,18 +761,12 @@ fun ASTNode.hasEqBinaryExpression(): Boolean =
?: false

/**
* Get line number, where this node's content starts. To avoid `ArrayIndexOutOfBoundsException`s we check whether node's maximum offset is less than
* Document's maximum offset, and calculate line number manually if needed.
* Get line number, where this node's content starts.
*
* @return line number or null if it cannot be calculated
* @return line number
*/
fun ASTNode.getLineNumber(): Int =
psi.containingFile
.viewProvider
.document
?.takeIf { it.getLineEndOffset(it.lineCount - 1) >= psi.endOffset }
?.let { lineNumber() }
?: calculateLineNumber()
calculateLineNumber()

/**
* This function calculates line number instead of using cached values.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,15 @@ class DiktatSmokeTest : FixTestBase("test/smoke/src/main/kotlin",
fixAndCompare("KdocFormattingMultilineTagsExpected.kt", "KdocFormattingMultilineTagsTest.kt")
}

@Test
@Tag("DiktatRuleSetProvider")
fun `regression - FP of local variables rule`() {
fixAndCompare("LocalVariableWithOffsetExpected.kt", "LocalVariableWithOffsetTest.kt")
org.assertj.core.api.Assertions.assertThat(unfixedLintErrors).noneMatch {
it.ruleId == "diktat-ruleset:local-variables"
}
}

companion object {
private const val DEFAULT_CONFIG_PATH = "../diktat-analysis.yml"
private val unfixedLintErrors: MutableList<LintError> = mutableListOf()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package org.cqfn.diktat

override fun boo() {
val listTestResult: MutableList<TestResult> = mutableListOf()
files.chunked(warnPluginConfig.batchSize ?: 1).map { chunk ->
handleTestFile(chunk.map { it.single() }, warnPluginConfig, generalConfig)
}.forEach { listTestResult.addAll(it) }
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package org.cqfn.diktat

override fun boo() {
val listTestResult: MutableList<TestResult> = mutableListOf()
files.chunked(warnPluginConfig.batchSize ?: 1).map { chunk ->
handleTestFile(chunk.map { it.single() }, warnPluginConfig, generalConfig)
}.forEach { listTestResult.addAll(it) }
}