Skip to content

Commit

Permalink
Add support to disable particular rules via plugin extension.
Browse files Browse the repository at this point in the history
Signed-off-by: Yahor Berdnikau <egorr.berd@gmail.com>
  • Loading branch information
Tapchicoma committed Sep 11, 2019
1 parent e0756dd commit d7b8947
Show file tree
Hide file tree
Showing 8 changed files with 153 additions and 5 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/).

## [Unreleased]
### Added
- ?
- `disabledRules` extension property to disable rules usage by id (#267)
### Changed
- Update Gradle to `5.6` version
- Update Kotlin to `1.3.50` version
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ ktlint {
ignoreFailures = true
enableExperimentalRules = true
additionalEditorconfigFile = file("/some/additional/.editorconfig")
disabledRules = ["final-newline"]
kotlinScriptAdditionalPaths {
include fileTree("scripts/")
}
Expand Down Expand Up @@ -189,6 +190,7 @@ ktlint {
ignoreFailures.set(true)
enableExperimentalRules.set(true)
additionalEditorconfigFile.set(file("/some/additional/.editorconfig"))
disabledRules.set(setOf("final-newline"))
kotlinScriptAdditionalPaths {
include(fileTree("scripts/"))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ abstract class BaseKtlintCheckTask(
internal val coloredOutput: Property<Boolean> = objectFactory.property()
@get:Input
internal val enableExperimentalRules: Property<Boolean> = objectFactory.property()
@get:Input
internal val disabledRules: SetProperty<String> = objectFactory.setProperty()

@get:Internal
internal val enabledReports
Expand Down Expand Up @@ -101,6 +103,7 @@ abstract class BaseKtlintCheckTask(
checkMinimalSupportedKtlintVersion()
checkCWEKtlintVersion()
checkExperimentalRulesSupportedKtlintVersion()
checkDisabledRulesSupportedKtlintVersion()

project.javaexec(generateJavaExecSpec(additionalConfig()))
}
Expand Down Expand Up @@ -147,6 +150,12 @@ abstract class BaseKtlintCheckTask(
enabledReports
.map { it.asArgument() }
.forEach { argsWriter.println(it) }
disabledRules
.get()
.joinToString(separator = ",")
.run {
if (isNotEmpty()) argsWriter.println("--disabled_rules=$this")
}
getSource()
.toRelativeFilesList()
.forEach { argsWriter.println(it) }
Expand Down Expand Up @@ -180,6 +189,13 @@ abstract class BaseKtlintCheckTask(
}
}

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.")
}
}

private fun ReporterType.isAvailable() =
SemVer.parse(ktlintVersion.get()) >= availableSinceVersion

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,15 @@ internal constructor(
*/
val additionalEditorconfigFile: RegularFileProperty = newFileProperty(objectFactory, projectLayout)

/**
* Disable particular rules, by default enabled in ktlint, using rule id.
*
* @since ktlint `0.34.2`
*/
val disabledRules: SetProperty<String> = objectFactory.setProperty {
set(emptySet())
}

private val kscriptExtension = KScriptExtension(kotlinScriptAdditionalPathApplier)

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,7 @@ open class KtlintPlugin : Plugin<Project> {
reporters.set(pluginHolder.extension.reporters)
android.set(pluginHolder.extension.android)
enableExperimentalRules.set(pluginHolder.extension.enableExperimentalRules)
disabledRules.set(pluginHolder.extension.disabledRules)

additionalTaskConfig()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,37 @@ abstract class AbstractPluginTest {
.withArguments(arguments.toList() + "--stacktrace")

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

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

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

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

protected fun File.restoreFailingSources() {
val sourceFile = resolve("src/main/kotlin/fail-source.kt")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package org.jlleitschuh.gradle.ktlint

import org.assertj.core.api.Assertions.assertThat
import org.gradle.testkit.runner.GradleRunner
import org.gradle.testkit.runner.TaskOutcome
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test

/**
* Runs [EditorConfigTests] with the current version of Gradle.
*/
class GradleCurrentCurrentDisabledRulesTest : DisabledRulesTest()

/**
* Runs [EditorConfigTests] with lowest supported Gradle version.
*/
@Suppress("ClassName")
class GradleLowestSupportedDisabledRulesTest : DisabledRulesTest() {
override fun gradleRunnerFor(vararg arguments: String): GradleRunner =
super.gradleRunnerFor(*arguments).withGradleVersion(LOWEST_SUPPORTED_GRADLE_VERSION)
}

abstract class DisabledRulesTest : AbstractPluginTest() {
@BeforeEach
internal fun setUp() {
projectRoot.defaultProjectSetup()
}

@Test
internal fun `Should lint without errors when "final-newline" is disabled`() {
projectRoot.buildFile().appendText(
"""
ktlint.disabledRules = ["final-newline"]
""".trimIndent()
)

projectRoot.createSourceFile(
"src/main/kotlin/clean-source.kt",
"""
val foo = "bar"
""".trimIndent()
)

build(":ktlintCheck").apply {
assertThat(task(":ktlintMainSourceSetCheck")?.outcome).isEqualTo(TaskOutcome.SUCCESS)
}
}

@Test
internal fun `Should lint without errors when "final-newline" and "no-consecutive-blank-lines" are disabled`() {
projectRoot.buildFile().appendText(
"""
ktlint.disabledRules = ["final-newline", "no-consecutive-blank-lines"]
""".trimIndent()
)

projectRoot.createSourceFile(
"src/main/kotlin/clean-source.kt",
"""
fun some() {
print("Woohoo!")
}
val foo = "bar"
""".trimIndent()
)

build(":ktlintCheck").apply {
assertThat(task(":ktlintMainSourceSetCheck")?.outcome).isEqualTo(TaskOutcome.SUCCESS)
}
}

@Test
internal fun `Should fail if ktlint version is lower then 0_34_2 and disabled rules configuration is set`() {
projectRoot.buildFile().appendText(
"""
ktlint.version = "0.33.0"
ktlint.disabledRules = ["final-newline"]
""".trimIndent()
)

projectRoot.withCleanSources()

buildAndFail(":ktlintCheck").apply {
assertThat(task(":ktlintMainSourceSetCheck")?.outcome).isEqualTo(TaskOutcome.FAILED)
assertThat(output).contains("Rules disabling is supported since 0.34.2 ktlint version.")
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package org.jlleitschuh.gradle.ktlint
import org.intellij.lang.annotations.Language
import java.io.File

const val LOWEST_SUPPORTED_GRADLE_VERSION = "4.10"

fun File.buildFile() = resolve("build.gradle")

@Language("Groovy")
Expand Down

0 comments on commit d7b8947

Please sign in to comment.