Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upgrade to ktlint 0.33.0 and provide configuration for wildcard imports #103

Merged
merged 1 commit into from
May 29, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
```

Expand All @@ -26,7 +26,7 @@ plugins {

```groovy
plugins {
id "org.jmailen.kotlinter" version "1.25.2"
id "org.jmailen.kotlinter" version "1.26.0"
}
```

Expand All @@ -46,7 +46,7 @@ buildscript {
}
}
dependencies {
classpath("org.jmailen.gradle:kotlinter-gradle:1.25.2")
classpath("org.jmailen.gradle:kotlinter-gradle:1.26.0")
}
}
```
Expand All @@ -71,7 +71,7 @@ buildscript {
}
}
dependencies {
classpath "org.jmailen.gradle:kotlinter-gradle:1.25.2"
classpath "org.jmailen.gradle:kotlinter-gradle:1.26.0"
}
}
```
Expand Down Expand Up @@ -185,6 +185,7 @@ kotlinter {
continuationIndentSize = 4
reporters = arrayOf("checkstyle", "plain")
experimentalRules = false
allowWildcardImports = true
fileBatchSize = 30
}
```
Expand All @@ -201,6 +202,7 @@ kotlinter {
continuationIndentSize = 4
reporters = ['checkstyle', 'plain']
experimentalRules = false
allowWildcardImports = true
fileBatchSize = 30
}
```
Expand All @@ -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
Expand Down
6 changes: 3 additions & 3 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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'
}

Expand All @@ -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'
Expand All @@ -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'

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,14 @@ class KotlinterPlugin : Plugin<Project> {
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
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<RuleSetProvider>,
includeExperimentalRules: Boolean = false
includeExperimentalRules: Boolean = false,
allowWildcardImports: Boolean = true
): List<RuleSet> {
return providers
.filter { includeExperimentalRules || it !is ExperimentalRuleSetProvider }
.filter { !allowWildcardImports || it !is NoWildcardImportsRuleSetProvider }
.map { it.get() }
.sortedWith(compareBy {
when (it.id) {
Expand All @@ -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()
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -53,6 +56,7 @@ open class FormatTask @Inject constructor(
projectDirectory = project.projectDir,
executionContextRepositoryId = executionContextRepositoryId,
experimentalRules = experimentalRules,
allowWildcardImports = allowWildcardImports,
indentSize = indentSize,
continuationIndentSize = continuationIndentSize
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -75,6 +78,7 @@ open class LintTask @Inject constructor(
executionContextRepositoryId = executionContextRepositoryId,
name = name,
experimentalRules = experimentalRules,
allowWildcardImports = allowWildcardImports,
indentSize = indentSize,
continuationIndentSize = continuationIndentSize
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
) : Serializable
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class FormatWorkerRunnable @Inject constructor(
private val files: List<File> = 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

Expand All @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
) : Serializable
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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) }

Expand Down
Original file line number Diff line number Diff line change
@@ -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()
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -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")))
Expand Down