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 8, 2022
1 parent 6e40a7f commit 3da35ac
Show file tree
Hide file tree
Showing 10 changed files with 31 additions and 16 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 @@ -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 @@ -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 {
val editorConfigOverrides = mutableListOf<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 @@ -115,7 +115,9 @@ class KtLintSupportedVersionsTest : AbstractPluginTest() {
"0.44.0",
// "0.45.0" does not work due to logger instantiation problem (https://github.com/pinterest/ktlint/issues/1421)
"0.45.1",
"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"
).also {
// "0.37.0" is failing on Windows machines that is fixed in the next version
if (!OS.WINDOWS.isCurrentOs) it.add("0.37.0")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -588,7 +588,7 @@ class KtlintPluginTest : AbstractPluginTest() {
)

build(":dependencies", "--configuration", KTLINT_RULESET_CONFIGURATION_NAME) {
assertThat(output).contains("com.pinterest.ktlint:ktlint-core:0.34.2 -> 0.45.2")
assertThat(output).contains("com.pinterest.ktlint:ktlint-core:0.34.2 -> 0.46.1")
}
}
}
Expand All @@ -610,7 +610,7 @@ class KtlintPluginTest : AbstractPluginTest() {
)

build(":dependencies", "--configuration", KTLINT_REPORTER_CONFIGURATION_NAME) {
assertThat(output).contains("com.pinterest.ktlint:ktlint-core:0.34.2 -> 0.45.2")
assertThat(output).contains("com.pinterest.ktlint:ktlint-core:0.34.2 -> 0.46.1")
}
}
}
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 3da35ac

Please sign in to comment.