Skip to content

Commit

Permalink
Upgrade ktlint supported version up to 0.46.1
Browse files Browse the repository at this point in the history
  • Loading branch information
fthouraud committed Jul 24, 2022
1 parent 1af8fc5 commit 0767c95
Show file tree
Hide file tree
Showing 15 changed files with 43 additions and 43 deletions.
2 changes: 1 addition & 1 deletion plugin/gradle/libs.versions.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[versions]
kotlin = "1.6.21"
ktlint = "0.45.2"
ktlint = "0.46.1"
androidPlugin = "4.1.0"
semver = "1.1.1"
jgit = "5.6.0.201912101111-r"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ open class KtlintApplyToIdeaTask @Inject constructor(
fun generate() {
project.javaexec {
it.classpath = classpath
it.mainClass.set("com.pinterest.ktlint.Main")
it.main = "com.pinterest.ktlint.Main"

// Global flags
if (android.get()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ internal constructor(
/**
* The version of KtLint to use.
*/
val version: Property<String> = objectFactory.property { set("0.45.2") }
val version: Property<String> = objectFactory.property { set("0.46.1") }

/**
* Enable relative paths in reports
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,9 +125,9 @@ internal fun Logger.logKtLintDebugMessage(
}

internal fun checkMinimalSupportedKtLintVersion(ktLintVersion: String) {
if (SemVer.parse(ktLintVersion) < SemVer(0, 34, 0)) {
if (SemVer.parse(ktLintVersion) < SemVer(0, 45, 2)) {
throw GradleException(
"KtLint versions less than 0.34.0 are not supported. " +
"KtLint versions less than 0.45.2 are not supported. " +
"Detected KtLint version: $ktLintVersion."
)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package org.jlleitschuh.gradle.ktlint.tasks

import groovy.lang.Closure
import net.swiftzer.semver.SemVer
import org.gradle.api.DefaultTask
import org.gradle.api.GradleException
import org.gradle.api.file.ConfigurableFileCollection
import org.gradle.api.file.FileCollection
import org.gradle.api.file.FileTreeElement
Expand Down Expand Up @@ -181,8 +179,6 @@ abstract class BaseKtLintCheckTask @Inject constructor(
protected fun runLint(
inputChanges: InputChanges
) {
checkDisabledRulesSupportedKtLintVersion()

val editorConfigUpdated = wasEditorConfigFilesUpdated(inputChanges)
val filesToCheck = if (editorConfigUpdated) {
source.files
Expand All @@ -200,8 +196,6 @@ abstract class BaseKtLintCheckTask @Inject constructor(
inputChanges: InputChanges,
formatSnapshot: File
) {
checkDisabledRulesSupportedKtLintVersion()

val editorConfigUpdated = wasEditorConfigFilesUpdated(inputChanges)
val filesToCheck = if (editorConfigUpdated) {
source.files
Expand Down Expand Up @@ -289,12 +283,4 @@ abstract class BaseKtLintCheckTask @Inject constructor(
}
.map { it.file }
.toSet()

private fun checkDisabledRulesSupportedKtLintVersion() {
if (disabledRules.get().isNotEmpty() &&
SemVer.parse(ktLintVersion.get()) < SemVer(0, 34, 2)
) {
throw GradleException("Rules disabling is supported since 0.34.2 ktlint version.")
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,12 @@ import com.pinterest.ktlint.core.LintError
import com.pinterest.ktlint.core.ParseException
import com.pinterest.ktlint.core.RuleSet
import com.pinterest.ktlint.core.RuleSetProvider
import com.pinterest.ktlint.core.api.DefaultEditorConfigProperties
import com.pinterest.ktlint.core.api.EditorConfigOverride
import com.pinterest.ktlint.core.api.UsesEditorConfigProperties
import net.swiftzer.semver.SemVer
import org.apache.commons.io.input.MessageDigestCalculatingInputStream
import org.ec4j.core.model.PropertyType
import org.gradle.api.GradleException
import org.gradle.api.file.ConfigurableFileCollection
import org.gradle.api.file.RegularFileProperty
Expand All @@ -27,6 +31,16 @@ abstract class KtLintWorkAction : WorkAction<KtLintWorkAction.KtLintWorkParamete

private val logger = Logging.getLogger("ktlint-worker")

private val androidProperty = UsesEditorConfigProperties.EditorConfigProperty(
type = PropertyType.LowerCasingPropertyType(
"android",
"A boolean value indicating that the project is an Android one.",
PropertyType.PropertyValueParser.BOOLEAN_VALUE_PARSER,
setOf(true.toString(), false.toString())
),
defaultValue = false
)

override fun execute() {
val ruleSets = loadRuleSetsAndFilterThem(
parameters.enableExperimental.getOrElse(false),
Expand All @@ -38,7 +52,7 @@ abstract class KtLintWorkAction : WorkAction<KtLintWorkAction.KtLintWorkParamete
.orNull
?.asFile
?.absolutePath
val userData = generateUserData()
val editorConfigOverride = generateEditorConfigOverride()
val debug = parameters.debug.get()
val formatSource = parameters.formatSource.getOrElse(false)

Expand All @@ -49,13 +63,13 @@ abstract class KtLintWorkAction : WorkAction<KtLintWorkAction.KtLintWorkParamete

parameters.filesToLint.files.forEach {
val errors = mutableListOf<Pair<LintError, Boolean>>()
val ktLintParameters = KtLint.Params(
val ktLintParameters = KtLint.ExperimentalParams(
fileName = it.absolutePath,
text = it.readText(),
ruleSets = ruleSets,
userData = userData,
debug = debug,
editorConfigPath = additionalEditorConfig,
editorConfigOverride = editorConfigOverride,
script = !it.name.endsWith(".kt", ignoreCase = true),
cb = { lintError, isCorrected ->
errors.add(lintError to isCorrected)
Expand Down Expand Up @@ -114,16 +128,16 @@ abstract class KtLintWorkAction : WorkAction<KtLintWorkAction.KtLintWorkParamete
}
}

private fun generateUserData(): Map<String, String> {
val userData = mutableMapOf(
"android" to parameters.android.get().toString()
private fun generateEditorConfigOverride(): EditorConfigOverride {
var editorConfigOverrides = listOf<Pair<UsesEditorConfigProperties.EditorConfigProperty<*>, *>>(
androidProperty to parameters.android.get().toString()
)
val disabledRules = parameters.disabledRules.get()
if (disabledRules.isNotEmpty()) {
userData["disabled_rules"] = disabledRules.joinToString(separator = ",")

parameters.disabledRules.get().takeIf { it.isNotEmpty() }?.let {
editorConfigOverrides += DefaultEditorConfigProperties.disabledRulesProperty to it.joinToString(separator = ",")
}

return userData.toMap()
return EditorConfigOverride.from(*editorConfigOverrides.toTypedArray())
}

private fun loadRuleSetsAndFilterThem(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,32 +30,32 @@ abstract class AbstractPluginTest {

protected
fun File.withCleanSources() = createSourceFile(
"src/main/kotlin/clean-source.kt",
"src/main/kotlin/CleanSource.kt",
"""
val foo = "bar"
""".trimIndent()
)

protected fun File.withCleanKotlinScript() = createSourceFile(
"kotlin-script.kts",
"""
println("zzz")
""".trimIndent()
)

protected fun File.withFailingKotlinScript() = createSourceFile(
"kotlin-script-fail.kts",
"""
println("zzz")
println("zzz")
""".trimIndent()
)

protected
fun File.withAlternativeFailingSources(baseDir: String) =
createSourceFile("$baseDir/fail-source.kt", """val foo = "bar"""")
createSourceFile("$baseDir/FailSource.kt", """val foo = "bar"""")

protected
fun File.createSourceFile(sourceFilePath: String, contents: String) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@ class ConfigurationCacheTest : AbstractPluginTest() {
internal fun configurationCacheForCheckTask(gradleVersion: GradleVersion) {
project(gradleVersion) {
createSourceFile(
"src/main/kotlin/clean-source.kt",
"src/main/kotlin/CleanSource.kt",
"""
val foo = "bar"
""".trimIndent()
)

Expand Down Expand Up @@ -64,7 +64,7 @@ class ConfigurationCacheTest : AbstractPluginTest() {
fun configurationCacheForFormatTasks(gradleVersion: GradleVersion) {
project(gradleVersion) {
createSourceFile(
"src/main/kotlin/clean-source.kt",
"src/main/kotlin/CleanSource.kt",
"""
val foo = "bar"
""".trimIndent()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import org.jlleitschuh.gradle.ktlint.testdsl.build
import org.jlleitschuh.gradle.ktlint.testdsl.buildAndFail
import org.jlleitschuh.gradle.ktlint.testdsl.project
import org.junit.jupiter.api.DisplayName
import org.junit.jupiter.api.condition.OS
import org.junit.jupiter.api.extension.ExtensionContext
import org.junit.jupiter.params.ParameterizedTest
import org.junit.jupiter.params.provider.Arguments
Expand Down Expand Up @@ -96,7 +95,9 @@ class KtLintSupportedVersionsTest : AbstractPluginTest() {

class SupportedKtlintVersionsProvider : GradleArgumentsProvider() {
private val supportedKtlintVersions = mutableListOf(
"0.45.2"
"0.45.2",
// "0.46.0" has been compiled with the K2 compiler expecting us to do the same
"0.46.1"
)

override fun provideArguments(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,8 @@ class TestProject(
}

companion object {
const val CLEAN_SOURCES_FILE = "src/main/kotlin/clean-source.kt"
const val FAIL_SOURCE_FILE = "src/main/kotlin/fail-source.kt"
const val CLEAN_SOURCES_FILE = "src/main/kotlin/CleanSource.kt"
const val FAIL_SOURCE_FILE = "src/main/kotlin/FailSource.kt"
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ val secondDiv: dynamic = document.createElement("div").apply {
}

fun main() {

val firstDiv = document.createElement("div").apply { innerHTML = "<h1>Hello!</h1>" }
document.body?.appendChild(firstDiv)

Expand Down

0 comments on commit 0767c95

Please sign in to comment.