diff --git a/README.md b/README.md index 414a37e2..ee9f6a8e 100644 --- a/README.md +++ b/README.md @@ -15,7 +15,7 @@ Available on the Gradle Plugins Portal: https://plugins.gradle.org/plugin/org.jm ```kotlin plugins { - id("org.jmailen.kotlinter") version "1.25.2" + id("org.jmailen.kotlinter") version "1.26.0" } ``` @@ -26,7 +26,7 @@ plugins { ```groovy plugins { - id "org.jmailen.kotlinter" version "1.25.2" + id "org.jmailen.kotlinter" version "1.26.0" } ``` @@ -46,7 +46,7 @@ buildscript { } } dependencies { - classpath("org.jmailen.gradle:kotlinter-gradle:1.25.2") + classpath("org.jmailen.gradle:kotlinter-gradle:1.26.0") } } ``` @@ -71,7 +71,7 @@ buildscript { } } dependencies { - classpath "org.jmailen.gradle:kotlinter-gradle:1.25.2" + classpath "org.jmailen.gradle:kotlinter-gradle:1.26.0" } } ``` @@ -185,6 +185,7 @@ kotlinter { continuationIndentSize = 4 reporters = arrayOf("checkstyle", "plain") experimentalRules = false + allowWildcardImports = true fileBatchSize = 30 } ``` @@ -201,6 +202,7 @@ kotlinter { continuationIndentSize = 4 reporters = ['checkstyle', 'plain'] experimentalRules = false + allowWildcardImports = true fileBatchSize = 30 } ``` @@ -217,6 +219,8 @@ Reporters behave as described at: https://github.com/pinterest/ktlint The `experimentalRules` property enables rules which are part of ktlint's experimental rule set. +The `allowWildcardImports` property can be set to `false` if you wish to disallow use of wildcard imports. + The `fileBatchSize` property configures the number of files that are processed in one Gradle Worker API call. ### Customizing Tasks diff --git a/build.gradle b/build.gradle index 8d4b805e..fc01bc76 100644 --- a/build.gradle +++ b/build.gradle @@ -3,7 +3,7 @@ plugins { id 'com.gradle.plugin-publish' version '0.10.0' id 'java-gradle-plugin' id 'maven-publish' - id 'org.jmailen.kotlinter' version '1.25.1' + id 'org.jmailen.kotlinter' version '1.25.2' id 'idea' } @@ -14,7 +14,7 @@ repositories { } dependencies { - implementation 'com.pinterest:ktlint:0.32.0' + implementation 'com.pinterest:ktlint:0.33.0' implementation 'me.cassiano:ktlint-html-reporter:0.2.1' compileOnly 'org.jetbrains.kotlin:kotlin-gradle-plugin' @@ -30,7 +30,7 @@ tasks.withType(PluginUnderTestMetadata).configureEach { pluginClasspath.from(configurations.compileOnly) } -version = '1.25.2' +version = '1.26.0' group = 'org.jmailen.gradle' def pluginId = 'org.jmailen.kotlinter' diff --git a/src/main/kotlin/org/jmailen/gradle/kotlinter/KotlinterExtension.kt b/src/main/kotlin/org/jmailen/gradle/kotlinter/KotlinterExtension.kt index 660c2b59..8a5ba1f5 100644 --- a/src/main/kotlin/org/jmailen/gradle/kotlinter/KotlinterExtension.kt +++ b/src/main/kotlin/org/jmailen/gradle/kotlinter/KotlinterExtension.kt @@ -9,6 +9,7 @@ open class KotlinterExtension { const val DEFAULT_CONTINUATION_INDENT_SIZE = 4 val DEFAULT_REPORTER = ReporterType.checkstyle.name const val DEFAULT_EXPERIMENTAL_RULES = false + const val DEFAULT_ALLOW_WILDCARD_IMPORTS = true const val DEFAULT_FILE_BATCH_SIZE = 30 } @@ -25,6 +26,8 @@ open class KotlinterExtension { var experimentalRules = DEFAULT_EXPERIMENTAL_RULES + var allowWildcardImports = DEFAULT_ALLOW_WILDCARD_IMPORTS + /** The file list is split into batches and processed together on a Worker API call */ var fileBatchSize = DEFAULT_FILE_BATCH_SIZE diff --git a/src/main/kotlin/org/jmailen/gradle/kotlinter/KotlinterPlugin.kt b/src/main/kotlin/org/jmailen/gradle/kotlinter/KotlinterPlugin.kt index 5541ee93..ddc1fecc 100644 --- a/src/main/kotlin/org/jmailen/gradle/kotlinter/KotlinterPlugin.kt +++ b/src/main/kotlin/org/jmailen/gradle/kotlinter/KotlinterPlugin.kt @@ -47,12 +47,14 @@ class KotlinterPlugin : Plugin { reporter to project.reportFile("${lintTask.sourceSetId}-lint.${reporterFileExtension(reporter)}") } lintTask.experimentalRules = kotlinterExtension.experimentalRules + lintTask.allowWildcardImports = kotlinterExtension.allowWildcardImports lintTask.fileBatchSize = kotlinterExtension.fileBatchSize } taskCreator.formatTasks.forEach { formatTask -> formatTask.indentSize = kotlinterExtension.indentSize formatTask.continuationIndentSize = kotlinterExtension.continuationIndentSize formatTask.experimentalRules = kotlinterExtension.experimentalRules + formatTask.allowWildcardImports = kotlinterExtension.allowWildcardImports formatTask.fileBatchSize = kotlinterExtension.fileBatchSize } } diff --git a/src/main/kotlin/org/jmailen/gradle/kotlinter/support/ruleSets.kt b/src/main/kotlin/org/jmailen/gradle/kotlinter/support/ruleSets.kt index 08ff0c41..eb227418 100644 --- a/src/main/kotlin/org/jmailen/gradle/kotlinter/support/ruleSets.kt +++ b/src/main/kotlin/org/jmailen/gradle/kotlinter/support/ruleSets.kt @@ -3,15 +3,18 @@ package org.jmailen.gradle.kotlinter.support import com.pinterest.ktlint.core.RuleSet import com.pinterest.ktlint.core.RuleSetProvider import com.pinterest.ktlint.ruleset.experimental.ExperimentalRuleSetProvider +import org.jmailen.rulesets.NoWildcardImportsRuleSetProvider import java.util.ServiceLoader import kotlin.comparisons.compareBy fun resolveRuleSets( providers: Iterable, - includeExperimentalRules: Boolean = false + includeExperimentalRules: Boolean = false, + allowWildcardImports: Boolean = true ): List { return providers .filter { includeExperimentalRules || it !is ExperimentalRuleSetProvider } + .filter { !allowWildcardImports || it !is NoWildcardImportsRuleSetProvider } .map { it.get() } .sortedWith(compareBy { when (it.id) { @@ -22,4 +25,6 @@ fun resolveRuleSets( } // statically resolve providers from plugin classpath -val defaultRuleSetProviders = ServiceLoader.load(RuleSetProvider::class.java).map { it } +val defaultRuleSetProviders = + ServiceLoader.load(RuleSetProvider::class.java).map { it } + + NoWildcardImportsRuleSetProvider() diff --git a/src/main/kotlin/org/jmailen/gradle/kotlinter/tasks/FormatTask.kt b/src/main/kotlin/org/jmailen/gradle/kotlinter/tasks/FormatTask.kt index 7db54ede..4f8cec93 100644 --- a/src/main/kotlin/org/jmailen/gradle/kotlinter/tasks/FormatTask.kt +++ b/src/main/kotlin/org/jmailen/gradle/kotlinter/tasks/FormatTask.kt @@ -31,6 +31,9 @@ open class FormatTask @Inject constructor( @Input var experimentalRules = KotlinterExtension.DEFAULT_EXPERIMENTAL_RULES + @Input + var allowWildcardImports = KotlinterExtension.DEFAULT_ALLOW_WILDCARD_IMPORTS + @Input var fileBatchSize = KotlinterExtension.DEFAULT_FILE_BATCH_SIZE @@ -53,6 +56,7 @@ open class FormatTask @Inject constructor( projectDirectory = project.projectDir, executionContextRepositoryId = executionContextRepositoryId, experimentalRules = experimentalRules, + allowWildcardImports = allowWildcardImports, indentSize = indentSize, continuationIndentSize = continuationIndentSize ) diff --git a/src/main/kotlin/org/jmailen/gradle/kotlinter/tasks/LintTask.kt b/src/main/kotlin/org/jmailen/gradle/kotlinter/tasks/LintTask.kt index a0553b4e..5426d01c 100644 --- a/src/main/kotlin/org/jmailen/gradle/kotlinter/tasks/LintTask.kt +++ b/src/main/kotlin/org/jmailen/gradle/kotlinter/tasks/LintTask.kt @@ -48,6 +48,9 @@ open class LintTask @Inject constructor( @Input var experimentalRules = KotlinterExtension.DEFAULT_EXPERIMENTAL_RULES + @Input + var allowWildcardImports = KotlinterExtension.DEFAULT_ALLOW_WILDCARD_IMPORTS + @Input var fileBatchSize = KotlinterExtension.DEFAULT_FILE_BATCH_SIZE @@ -75,6 +78,7 @@ open class LintTask @Inject constructor( executionContextRepositoryId = executionContextRepositoryId, name = name, experimentalRules = experimentalRules, + allowWildcardImports = allowWildcardImports, indentSize = indentSize, continuationIndentSize = continuationIndentSize ) diff --git a/src/main/kotlin/org/jmailen/gradle/kotlinter/tasks/format/FormatWorkerParameters.kt b/src/main/kotlin/org/jmailen/gradle/kotlinter/tasks/format/FormatWorkerParameters.kt index c858c887..ed111e59 100644 --- a/src/main/kotlin/org/jmailen/gradle/kotlinter/tasks/format/FormatWorkerParameters.kt +++ b/src/main/kotlin/org/jmailen/gradle/kotlinter/tasks/format/FormatWorkerParameters.kt @@ -12,6 +12,7 @@ data class FormatWorkerParameters( val projectDirectory: File, val executionContextRepositoryId: UUID, val experimentalRules: Boolean, + val allowWildcardImports: Boolean, val indentSize: Int, val continuationIndentSize: Int -) : Serializable \ No newline at end of file +) : Serializable diff --git a/src/main/kotlin/org/jmailen/gradle/kotlinter/tasks/format/FormatWorkerRunnable.kt b/src/main/kotlin/org/jmailen/gradle/kotlinter/tasks/format/FormatWorkerRunnable.kt index 153f022d..06893ced 100644 --- a/src/main/kotlin/org/jmailen/gradle/kotlinter/tasks/format/FormatWorkerRunnable.kt +++ b/src/main/kotlin/org/jmailen/gradle/kotlinter/tasks/format/FormatWorkerRunnable.kt @@ -22,6 +22,7 @@ class FormatWorkerRunnable @Inject constructor( private val files: List = parameters.files private val projectDirectory: File = parameters.projectDirectory private val experimentalRules: Boolean = parameters.experimentalRules + private val allowWildcardImports: Boolean = parameters.allowWildcardImports private val indentSize: Int = parameters.indentSize private val continuationIndentSize: Int = parameters.continuationIndentSize @@ -41,7 +42,7 @@ class FormatWorkerRunnable @Inject constructor( null } }?.let { formatFunc -> - val ruleSets = resolveRuleSets(executionContext.ruleSetProviders, experimentalRules) + val ruleSets = resolveRuleSets(executionContext.ruleSetProviders, experimentalRules, allowWildcardImports) val formattedText = formatFunc.invoke(file, ruleSets) { line, col, detail, corrected -> val errorStr = "$relativePath:$line:$col: $detail" val msg = when (corrected) { diff --git a/src/main/kotlin/org/jmailen/gradle/kotlinter/tasks/lint/LintWorkerParameters.kt b/src/main/kotlin/org/jmailen/gradle/kotlinter/tasks/lint/LintWorkerParameters.kt index 10f3358e..1d55056f 100644 --- a/src/main/kotlin/org/jmailen/gradle/kotlinter/tasks/lint/LintWorkerParameters.kt +++ b/src/main/kotlin/org/jmailen/gradle/kotlinter/tasks/lint/LintWorkerParameters.kt @@ -13,6 +13,7 @@ data class LintWorkerParameters( val name: String, val executionContextRepositoryId: UUID, val experimentalRules: Boolean, + val allowWildcardImports: Boolean, val indentSize: Int, val continuationIndentSize: Int -) : Serializable \ No newline at end of file +) : Serializable diff --git a/src/main/kotlin/org/jmailen/gradle/kotlinter/tasks/lint/LintWorkerRunnable.kt b/src/main/kotlin/org/jmailen/gradle/kotlinter/tasks/lint/LintWorkerRunnable.kt index bcb2b4e9..0fb6803b 100644 --- a/src/main/kotlin/org/jmailen/gradle/kotlinter/tasks/lint/LintWorkerRunnable.kt +++ b/src/main/kotlin/org/jmailen/gradle/kotlinter/tasks/lint/LintWorkerRunnable.kt @@ -25,6 +25,7 @@ class LintWorkerRunnable @Inject constructor( private val projectDirectory: File = parameters.projectDirectory private val name: String = parameters.name private val experimentalRules: Boolean = parameters.experimentalRules + private val allowWildcardImports: Boolean = parameters.allowWildcardImports private val indentSize: Int = parameters.indentSize private val continuationIndentSize: Int = parameters.continuationIndentSize @@ -44,7 +45,7 @@ class LintWorkerRunnable @Inject constructor( } } - val ruleSets = resolveRuleSets(executionContext.ruleSetProviders, experimentalRules) + val ruleSets = resolveRuleSets(executionContext.ruleSetProviders, experimentalRules, allowWildcardImports) lintFunc?.invoke(file, ruleSets) { error -> reporters.onEach { it.onLintError(relativePath, error, false) } diff --git a/src/main/kotlin/org/jmailen/rulesets/NoWildcardImportsRuleSetProvider.kt b/src/main/kotlin/org/jmailen/rulesets/NoWildcardImportsRuleSetProvider.kt new file mode 100644 index 00000000..52224c67 --- /dev/null +++ b/src/main/kotlin/org/jmailen/rulesets/NoWildcardImportsRuleSetProvider.kt @@ -0,0 +1,13 @@ +package org.jmailen.rulesets + +import com.pinterest.ktlint.core.RuleSet +import com.pinterest.ktlint.core.RuleSetProvider +import com.pinterest.ktlint.ruleset.standard.NoWildcardImportsRule + +class NoWildcardImportsRuleSetProvider : RuleSetProvider { + + override fun get() = RuleSet( + "no-wildcard-imports", + NoWildcardImportsRule() + ) +} diff --git a/src/test/kotlin/org/jmailen/gradle/kotlinter/support/RuleSetsTest.kt b/src/test/kotlin/org/jmailen/gradle/kotlinter/support/RuleSetsTest.kt index a08cab05..02f8deb2 100644 --- a/src/test/kotlin/org/jmailen/gradle/kotlinter/support/RuleSetsTest.kt +++ b/src/test/kotlin/org/jmailen/gradle/kotlinter/support/RuleSetsTest.kt @@ -25,6 +25,13 @@ class RuleSetsTest { assertEquals(listOf("standard", "experimental"), result.map { it.id }) } + @Test + fun `resolveRuleSets loads from classpath providers optionally disallowing wildcard imports`() { + val result = resolveRuleSets(defaultRuleSetProviders, allowWildcardImports = false) + + assertEquals(listOf("standard", "no-wildcard-imports"), result.map { it.id }) + } + @Test fun `resolveRuleSets puts standard rules first`() { val standard = TestRuleSetProvider(RuleSet("standard", TestRule("one")))