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

Support virtual path in DiktatProcessor #1874

Merged
merged 1 commit into from
Dec 18, 2023
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 @@ -20,13 +20,13 @@ interface DiktatProcessor {
* Run `diktat fix` on provided [code] using [callback] for detected errors and returned formatted code.
*
* @param code
* @param isScript
* @param virtualPath a path which should be taken into account in processing of [code]
* @param callback
* @return result of `diktat fix`
*/
fun fix(
code: String,
isScript: Boolean,
virtualPath: Path?,
callback: DiktatCallback,
): String

Expand All @@ -42,12 +42,12 @@ interface DiktatProcessor {
* Run `diktat check` on provided [code] using [callback] for detected errors.
*
* @param code
* @param isScript
* @param virtualPath a path which should be taken into account in processing of [code]
* @param callback
*/
fun check(
code: String,
isScript: Boolean,
virtualPath: Path?,
callback: DiktatCallback,
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import org.ec4j.core.model.Property
import org.ec4j.core.model.PropertyType
import java.nio.file.Path
import kotlin.io.path.name

private typealias LintCallback = (LintError) -> Unit

Expand All @@ -30,18 +31,18 @@
): String = ktLintRuleEngine.formatSilentlyThenLint(file.toKtLint(), callback.toKtLintForLint())
override fun fix(
code: String,
isScript: Boolean,
virtualPath: Path?,
callback: DiktatCallback
): String = ktLintRuleEngine.formatSilentlyThenLint(code.toKtLint(isScript), callback.toKtLintForLint())
): String = ktLintRuleEngine.formatSilentlyThenLint(code.toKtLint(virtualPath), callback.toKtLintForLint())

Check warning on line 36 in diktat-ktlint-engine/src/main/kotlin/com/saveourtool/diktat/ktlint/DiktatProcessorFactoryImpl.kt

View check run for this annotation

Codecov / codecov/patch

diktat-ktlint-engine/src/main/kotlin/com/saveourtool/diktat/ktlint/DiktatProcessorFactoryImpl.kt#L36

Added line #L36 was not covered by tests
override fun check(
file: Path,
callback: DiktatCallback,
) = ktLintRuleEngine.lint(file.toKtLint(), callback.toKtLintForLint())
override fun check(
code: String,
isScript: Boolean,
virtualPath: Path?,
callback: DiktatCallback
) = ktLintRuleEngine.lint(code.toKtLint(isScript), callback.toKtLintForLint())
) = ktLintRuleEngine.lint(code.toKtLint(virtualPath), callback.toKtLintForLint())

Check warning on line 45 in diktat-ktlint-engine/src/main/kotlin/com/saveourtool/diktat/ktlint/DiktatProcessorFactoryImpl.kt

View check run for this annotation

Codecov / codecov/patch

diktat-ktlint-engine/src/main/kotlin/com/saveourtool/diktat/ktlint/DiktatProcessorFactoryImpl.kt#L45

Added line #L45 was not covered by tests
}
}

Expand All @@ -66,7 +67,13 @@

private fun Path.toKtLint(): Code = Code.fromFile(this.toFile())

private fun String.toKtLint(isScript: Boolean): Code = Code.fromSnippet(this, isScript)
private fun String.toKtLint(virtualPath: Path?): Code = Code(
content = this,

Check warning on line 71 in diktat-ktlint-engine/src/main/kotlin/com/saveourtool/diktat/ktlint/DiktatProcessorFactoryImpl.kt

View check run for this annotation

Codecov / codecov/patch

diktat-ktlint-engine/src/main/kotlin/com/saveourtool/diktat/ktlint/DiktatProcessorFactoryImpl.kt#L70-L71

Added lines #L70 - L71 were not covered by tests
fileName = virtualPath?.name,
filePath = virtualPath,

Check warning on line 73 in diktat-ktlint-engine/src/main/kotlin/com/saveourtool/diktat/ktlint/DiktatProcessorFactoryImpl.kt

View check run for this annotation

Codecov / codecov/patch

diktat-ktlint-engine/src/main/kotlin/com/saveourtool/diktat/ktlint/DiktatProcessorFactoryImpl.kt#L73

Added line #L73 was not covered by tests
script = virtualPath?.name?.endsWith(".kts", ignoreCase = true) ?: false,
isStdIn = virtualPath == null,
)

Check warning on line 76 in diktat-ktlint-engine/src/main/kotlin/com/saveourtool/diktat/ktlint/DiktatProcessorFactoryImpl.kt

View check run for this annotation

Codecov / codecov/patch

diktat-ktlint-engine/src/main/kotlin/com/saveourtool/diktat/ktlint/DiktatProcessorFactoryImpl.kt#L76

Added line #L76 was not covered by tests

private fun DiktatCallback.toKtLintForLint(): LintCallback = { error ->
this(error.wrap(), false)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,10 +158,10 @@
* @param ruleSetSupplier
* @param file
* @param cb callback to be called on unhandled [LintError]s
* @return formatted code
* @return [Unit]
*/
@Suppress("LAMBDA_IS_NOT_LAST_PARAMETER")
fun lint(
fun check(

Check warning on line 164 in diktat-ktlint-engine/src/main/kotlin/com/saveourtool/diktat/ktlint/KtLintUtils.kt

View check run for this annotation

Codecov / codecov/patch

diktat-ktlint-engine/src/main/kotlin/com/saveourtool/diktat/ktlint/KtLintUtils.kt#L164

Added line #L164 was not covered by tests
ruleSetSupplier: () -> DiktatRuleSet,
file: Path,
cb: DiktatCallback = DiktatCallback.empty
Expand All @@ -175,16 +175,16 @@
* @param ruleSetSupplier
* @param text
* @param cb callback to be called on unhandled [LintError]s
* @return formatted code
* @return [Unit]
*/
@Suppress("LAMBDA_IS_NOT_LAST_PARAMETER")
fun lint(
fun check(

Check warning on line 181 in diktat-ktlint-engine/src/main/kotlin/com/saveourtool/diktat/ktlint/KtLintUtils.kt

View check run for this annotation

Codecov / codecov/patch

diktat-ktlint-engine/src/main/kotlin/com/saveourtool/diktat/ktlint/KtLintUtils.kt#L181

Added line #L181 was not covered by tests
ruleSetSupplier: () -> DiktatRuleSet,
@Language("kotlin") text: String,
cb: DiktatCallback = DiktatCallback.empty
) = DiktatProcessorFactoryImpl().invoke(ruleSetSupplier())
.check(
code = text,
isScript = false,
virtualPath = null,

Check warning on line 188 in diktat-ktlint-engine/src/main/kotlin/com/saveourtool/diktat/ktlint/KtLintUtils.kt

View check run for this annotation

Codecov / codecov/patch

diktat-ktlint-engine/src/main/kotlin/com/saveourtool/diktat/ktlint/KtLintUtils.kt#L188

Added line #L188 was not covered by tests
callback = cb.ignoreCorrectedErrors(),
)
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ package com.saveourtool.diktat.ruleset.utils
import com.saveourtool.diktat.api.DiktatErrorEmitter
import com.saveourtool.diktat.api.DiktatRule
import com.saveourtool.diktat.api.DiktatRuleSet
import com.saveourtool.diktat.ktlint.lint
import com.saveourtool.diktat.ktlint.check
import com.saveourtool.diktat.util.applyToCode

import org.jetbrains.kotlin.KtNodeTypes
Expand Down Expand Up @@ -809,7 +809,7 @@ private class PrettyPrintingVisitor(private val elementType: IElementType,
maxLevel: Int = -1,
expected: String
) {
lint(
check(
ruleSetSupplier = { DiktatRuleSet(listOf(PrettyPrintingVisitor(elementType, level, maxLevel, expected))) },
text = code,
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package com.saveourtool.diktat.util

import com.saveourtool.diktat.api.DiktatCallback
import com.saveourtool.diktat.common.config.rules.RulesConfig
import com.saveourtool.diktat.ktlint.lint
import com.saveourtool.diktat.ktlint.check
import com.saveourtool.diktat.ruleset.rules.DiktatRule
import com.saveourtool.diktat.util.DiktatRuleSetFactoryImplTest.Companion.diktatRuleSetForTest
import com.saveourtool.diktat.api.DiktatError
Expand Down Expand Up @@ -127,7 +127,7 @@ open class LintTestBase(private val ruleSupplier: (rulesConfigList: List<RulesCo
): List<DiktatError> {
val lintErrors: MutableList<DiktatError> = mutableListOf()

lint(
check(
ruleSetSupplier = { rulesConfigList.toDiktatRuleSet() },
file = file,
cb = lintErrors.collector(),
Expand All @@ -151,7 +151,7 @@ open class LintTestBase(private val ruleSupplier: (rulesConfigList: List<RulesCo
): List<DiktatError> {
val lintErrors: MutableList<DiktatError> = mutableListOf()

lint(
check(
ruleSetSupplier = { rulesConfigList.toDiktatRuleSet() },
text = code,
cb = lintErrors.collector(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ package com.saveourtool.diktat.util
import com.saveourtool.diktat.api.DiktatErrorEmitter
import com.saveourtool.diktat.api.DiktatRule
import com.saveourtool.diktat.api.DiktatRuleSet
import com.saveourtool.diktat.ktlint.lint
import com.saveourtool.diktat.ktlint.check

import org.assertj.core.api.Assertions.assertThat
import org.assertj.core.api.Assertions.fail
Expand Down Expand Up @@ -111,7 +111,7 @@ internal fun applyToCode(@Language("kotlin") code: String,
applyToNode: (node: ASTNode, counter: AtomicInteger) -> Unit
) {
val counter = AtomicInteger(0)
lint(
check(
ruleSetSupplier = {
DiktatRuleSet(listOf(object : DiktatRule {
override val id: String
Expand Down
Loading