Skip to content

Commit

Permalink
start 1.0 support
Browse files Browse the repository at this point in the history
  • Loading branch information
wakingrufus committed Sep 6, 2023
1 parent 059252a commit 888644f
Show file tree
Hide file tree
Showing 5 changed files with 133 additions and 6 deletions.
14 changes: 12 additions & 2 deletions plugin/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,9 @@ sourceSets {
val adapter50 by creating {
compileClasspath += adapter.output
}
val adapter1 by creating {
compileClasspath += adapter.output
}
val adapters = listOf(
adapter,
adapter34,
Expand All @@ -97,7 +100,8 @@ sourceSets {
adapter47,
adapter48,
adapter49,
adapter50
adapter50,
adapter1
)
val main by getting {
kotlin {
Expand All @@ -121,7 +125,8 @@ val adapterSources = listOf(
sourceSets.named("adapter47"),
sourceSets.named("adapter48"),
sourceSets.named("adapter49"),
sourceSets.named("adapter50")
sourceSets.named("adapter50"),
sourceSets.named("adapter1")
)
tasks.named<Jar>("shadowJar") {
this.from(adapterSources.map { sourceSet -> sourceSet.map { it.output.classesDirs } })
Expand Down Expand Up @@ -159,6 +164,11 @@ dependencies {
add("adapter50CompileOnly", "com.pinterest.ktlint:ktlint-ruleset-standard:0.50.0")
add("adapter50CompileOnly", "com.pinterest.ktlint:ktlint-reporter-baseline:0.50.0")

add("adapter1CompileOnly", "com.pinterest.ktlint:ktlint-cli-reporter-core:1.0.0")
add("adapter1CompileOnly", "com.pinterest.ktlint:ktlint-rule-engine:1.0.0")
add("adapter1CompileOnly", "com.pinterest.ktlint:ktlint-ruleset-standard:1.0.0")
add("adapter1CompileOnly", "com.pinterest.ktlint:ktlint-cli-reporter-baseline:1.0.0")

compileOnly(libs.kotlin.gradle.plugin)
compileOnly(libs.android.gradle.plugin)
compileOnly(kotlin("stdlib-jdk8"))
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package org.jlleitschuh.gradle.ktlint.worker

import com.pinterest.ktlint.cli.ruleset.core.api.RuleSetProviderV3
import com.pinterest.ktlint.rule.engine.api.Code
import com.pinterest.ktlint.rule.engine.api.EditorConfigOverride
import com.pinterest.ktlint.rule.engine.api.EditorConfigPropertyRegistry
import com.pinterest.ktlint.rule.engine.api.KtLintRuleEngine
import com.pinterest.ktlint.rule.engine.api.LintError
import com.pinterest.ktlint.rule.engine.core.api.RuleProvider
import java.io.File
import java.util.ServiceLoader

class KtLintInvocation1(
private val engine: KtLintRuleEngine
) : KtLintInvocation {
companion object Factory : KtLintInvocationFactory {
fun initialize( editorConfigOverrides: Map<String, String>): KtLintInvocation {
val ruleProviders = loadRuleSetsFromClasspathWithRuleSetProviderV3()
val editorConfigPropertyRegistry = EditorConfigPropertyRegistry(ruleProviders)
val engine = if(editorConfigOverrides.isEmpty()){
KtLintRuleEngine(ruleProviders = ruleProviders)
} else {
KtLintRuleEngine(
ruleProviders = ruleProviders,
editorConfigOverride = EditorConfigOverride.from(*editorConfigOverrides
.mapKeys { editorConfigPropertyRegistry.find(it.key) }
.entries
.map { it.key to it.value }
.toTypedArray())
)
}
return KtLintInvocation1(engine)
}

private fun loadRuleSetsFromClasspathWithRuleSetProviderV3(): Set<RuleProvider> {
return ServiceLoader
.load(RuleSetProviderV3::class.java)
.flatMap { it.getRuleProviders() }
.toSet()
}
}

override fun invokeLint(file: File): LintErrorResult {
val errors = mutableListOf<Pair<SerializableLintError, Boolean>>()
engine.lint(Code.fromFile(file)) { le: LintError ->
errors.add(le.toSerializable() to false)
}
return LintErrorResult(file, errors)
}

override fun invokeFormat(file: File): Pair<String, LintErrorResult> {
val errors = mutableListOf<Pair<SerializableLintError, Boolean>>()
val newCode =
engine.format(Code.fromFile(file)) { le, boolean ->
errors.add(le.toSerializable() to boolean)
}
return newCode to LintErrorResult(file, errors)
}

override fun trimMemory() {
engine.trimMemory()
}
}

internal fun LintError.toSerializable(): SerializableLintError {
return SerializableLintError(line, col, ruleId.value, detail, canBeAutoCorrected)
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import org.jlleitschuh.gradle.ktlint.worker.BaselineLoader46
import org.jlleitschuh.gradle.ktlint.worker.BaselineLoader47
import org.jlleitschuh.gradle.ktlint.worker.BaselineLoader48
import org.jlleitschuh.gradle.ktlint.worker.BaselineLoader49
import org.jlleitschuh.gradle.ktlint.worker.KtLintInvocation1
import org.jlleitschuh.gradle.ktlint.worker.KtLintInvocation45
import org.jlleitschuh.gradle.ktlint.worker.KtLintInvocation46
import org.jlleitschuh.gradle.ktlint.worker.KtLintInvocation47
Expand All @@ -34,8 +35,10 @@ internal fun selectInvocation(version: String): KtLintInvocationFactory {
KtLintInvocation48
} else if (semVer.minor == 49) {
KtLintInvocation49
} else {
} else if(semVer.minor == 50){
KtLintInvocation50
} else {
KtLintInvocation1
}
} else {
KtLintInvocation50
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,10 +103,10 @@ class KtLintSupportedVersionsTest : AbstractPluginTest() {
}
}

@DisplayName("Lint should use editorconfig override")
@DisplayName("Lint should use editorconfig override (standard rule)")
@ParameterizedTest(name = "{0} with KtLint {1}: {displayName}")
@ArgumentsSource(SupportedKtlintVersionsProvider::class)
internal fun `Lint should use editorconfig override`(
internal fun `Lint should use editorconfig override standard rule`(
gradleVersion: GradleVersion,
ktLintVersion: String
) {
Expand All @@ -122,14 +122,51 @@ class KtLintSupportedVersionsTest : AbstractPluginTest() {
""".trimIndent()
)
withFailingSources()
if (SemVer.parse(ktLintVersion) < SemVer(1, 0)) {
buildAndFail(CHECK_PARENT_TASK_NAME) {
assertThat(task(":$mainSourceSetCheckTaskName")?.outcome)
.`as`("standard rules not supported by additionalEditorconfig until 1.0")
.isEqualTo(TaskOutcome.FAILED)
assertThat(output).contains("additionalEditorconfig not supported until ktlint 0.49")
}
} else {
build(CHECK_PARENT_TASK_NAME) {
assertThat(task(":$mainSourceSetCheckTaskName")?.outcome).isEqualTo(TaskOutcome.SUCCESS)
}
}
}
}

@DisplayName("Lint should use editorconfig override")
@ParameterizedTest(name = "{0} with KtLint {1}: {displayName}")
@ArgumentsSource(SupportedKtlintVersionsProvider::class)
internal fun `Lint should use editorconfig override`(
gradleVersion: GradleVersion,
ktLintVersion: String
) {
project(gradleVersion) {
//language=Groovy
buildGradle.appendText(
"""
ktlint.version = "$ktLintVersion"
ktlint.additionalEditorconfig = [
"ij_kotlin_allow_trailing_comma": "true"
]
""".trimIndent()
)
withFailingMaxLineSources()
if (SemVer.parse(ktLintVersion) < SemVer(0, 49)) {
buildAndFail(CHECK_PARENT_TASK_NAME) {
assertThat(task(":$mainSourceSetCheckTaskName")?.outcome).isEqualTo(TaskOutcome.FAILED)
assertThat(output).contains("additionalEditorconfig not supported until ktlint 0.49")
}
} else {
build(CHECK_PARENT_TASK_NAME) {
assertThat(task(":$mainSourceSetCheckTaskName")?.outcome).isEqualTo(TaskOutcome.SUCCESS)
assertThat(task(":$mainSourceSetCheckTaskName")?.outcome)
.`as`("additionalEditorconfig takes effect")
.isEqualTo(TaskOutcome.SUCCESS)
assertThat(output).doesNotContain("additionalEditorconfig not supported until ktlint 0.49")
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,16 @@ class TestProject(
)
}

fun withFailingMaxLineSources() {
createSourceFile(
FAIL_SOURCE_FILE,
buildString {
append("val nameOfVariable = listOf(1, 2, 3,)")
append("\n")
}
)
}

fun withAdditionalEditorConfig() {
createSourceFile(
ADDITIONAL_EDITOR_CONFIG + "/.editorconfig",
Expand Down

0 comments on commit 888644f

Please sign in to comment.