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

Bump ktlint.version from 0.46.1 to 0.47.0 #495

Merged
merged 2 commits into from Aug 20, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions pom.xml
Expand Up @@ -53,8 +53,8 @@
<jacoco-maven-plugin.version>0.8.7</jacoco-maven-plugin.version>
<java.require.version>11</java.require.version>
<jdeps.multiRelease>11</jdeps.multiRelease>
<kotlin.version>1.7.0</kotlin.version>
<ktlint.version>0.46.1</ktlint.version>
<kotlin.version>1.7.10</kotlin.version>
<ktlint.version>0.47.0</ktlint.version>
<license-maven-plugin.version>1.20</license-maven-plugin.version>
<maven.compiler.release>8</maven.compiler.release>
<maven.version>3.5.4</maven.version>
Expand Down
Expand Up @@ -31,7 +31,7 @@ import com.pinterest.ktlint.core.KtLint
import com.pinterest.ktlint.core.LintError
import com.pinterest.ktlint.core.Reporter
import com.pinterest.ktlint.core.ReporterProvider
import com.pinterest.ktlint.core.RuleSet
import com.pinterest.ktlint.core.RuleProvider
import com.pinterest.ktlint.core.api.DefaultEditorConfigProperties
import com.pinterest.ktlint.core.api.EditorConfigOverride
import com.pinterest.ktlint.core.api.EditorConfigOverride.Companion.plus
Expand Down Expand Up @@ -181,15 +181,18 @@ internal abstract class AbstractCheckSupport(

val sourceFiles = ds.includedFiles.map { File(sourceRoot, it) }

val workingDir = File(".").absoluteFile

sourceFiles.forEach { file ->
if (!checkedFiles.add(file.canonicalFile)) {
return@forEach
}

val relativePath = file.toRelativeString(basedir)
reporter.before(relativePath)
val workingRelativePath = file.toRelativeString(workingDir)
val baseRelativePath = file.toRelativeString(basedir)
reporter.before(baseRelativePath)

log.debug("checking: $relativePath")
log.debug("checking: $baseRelativePath")

val editorConfigOverride = EditorConfigOverride.emptyEditorConfigOverride
if (android) {
Expand All @@ -199,23 +202,22 @@ internal abstract class AbstractCheckSupport(
val sourceText = file.readText(charset)

lintFile(
relativePath,
workingRelativePath,
sourceText,
ruleSets,
ruleProviders,
{ error ->
reporter.onLintError(relativePath, error, false)
reporter.onLintError(baseRelativePath, error, false)

val lintError =
"$relativePath:${error.line}:${error.col}: ${error.detail}"
"$baseRelativePath:${error.line}:${error.col}: ${error.detail}"
log.debug("Style error > $lintError")

hasErrors = true
},
editorConfigOverride,
file.editorConfigPath
editorConfigOverride
)

reporter.after(relativePath)
reporter.after(baseRelativePath)
}
}
}
Expand All @@ -226,19 +228,17 @@ internal abstract class AbstractCheckSupport(
private fun lintFile(
fileName: String,
sourceText: String,
ruleSets: List<RuleSet>,
ruleProviders: Set<RuleProvider>,
onError: (error: LintError) -> Unit,
editorConfigOverride: EditorConfigOverride,
editorConfigPath: String?
editorConfigOverride: EditorConfigOverride
) = KtLint.lint(
KtLint.ExperimentalParams(
fileName = fileName,
text = sourceText,
ruleSets = ruleSets,
ruleProviders = ruleProviders,
script = !fileName.endsWith(".kt", ignoreCase = true),
cb = { e, _ -> onError(e) },
editorConfigOverride = editorConfigOverride,
editorConfigPath = editorConfigPath
editorConfigOverride = editorConfigOverride
)
)
}
Expand Up @@ -25,12 +25,10 @@
*/
package com.github.gantsign.maven.plugin.ktlint.internal

import com.pinterest.ktlint.core.RuleSet
import com.pinterest.ktlint.core.RuleSetProvider
import com.pinterest.ktlint.core.RuleProvider
import com.pinterest.ktlint.core.RuleSetProviderV2
import java.io.File
import java.util.Comparator.comparingInt
import java.util.ServiceLoader
import java.util.concurrent.ConcurrentHashMap
import org.apache.maven.plugin.logging.Log

internal abstract class AbstractLintSupport(
Expand All @@ -39,84 +37,23 @@ internal abstract class AbstractLintSupport(
protected val android: Boolean,
private val enableExperimentalRules: Boolean
) {

protected val ruleSets: List<RuleSet> by lazy {

val ruleSetComparator =
comparingInt<RuleSet> {
return@comparingInt when (it.id) {
"standard" -> 0
"experimental" -> 1
else -> 2
}
}.thenComparing(RuleSet::id)

var ruleSets: List<RuleSet> =
ServiceLoader.load(RuleSetProvider::class.java)
.map(RuleSetProvider::get)
.sortedWith(ruleSetComparator)

if (log.isDebugEnabled) {
for (ruleSet in ruleSets) {
log.debug("Discovered ruleset '${ruleSet.id}'")
}
}

if (!enableExperimentalRules) {
ruleSets = ruleSets.filter { it.id != "experimental" }
log.debug("Disabled ruleset 'experimental'")
}

return@lazy ruleSets
}

private val editorConfigPathCache = ConcurrentHashMap<File, String>()

protected val File.editorConfigPath: String?
get() {
val basedir: File = this.parentFile ?: return null

var path = editorConfigPathCache[this]
if (path != null) {
return if (path == none) null else path
}
var editorconfig = File(basedir, ".editorconfig")
if (editorconfig.isFile) {
path = editorconfig.absolutePath
editorConfigPathCache[basedir] = path
return path
}

val childDirs = mutableListOf(basedir)
var dir: File? = basedir.parentFile

while (dir != null) {
path = editorConfigPathCache[this]
if (path != null) {
for (childDir in childDirs) {
editorConfigPathCache[childDir] = path
}
return if (path == none) null else path
protected val ruleProviders: Set<RuleProvider> by lazy {
return@lazy ServiceLoader.load(RuleSetProviderV2::class.java)
.asSequence()
.map { ruleSetProviderV2 -> Pair(ruleSetProviderV2.id, ruleSetProviderV2.getRuleProviders()) }
.distinctBy { (id, _) -> id }
.onEach { (id, _) ->
if (log.isDebugEnabled) {
log.debug("Discovered RuleSetProviderV2 '$id'")
}
editorconfig = File(dir, ".editorconfig")
if (editorconfig.isFile) {
path = editorconfig.absolutePath
editorConfigPathCache[dir] = path
for (childDir in childDirs) {
editorConfigPathCache[childDir] = path
}
return path
}
childDirs += dir
dir = dir.parentFile
}
for (childDir in childDirs) {
editorConfigPathCache[childDir] = none
.filter { (id, _) ->
if (!enableExperimentalRules && id == "experimental") {
log.debug("Disabled RuleSetProviderV2 'experimental'")
false
} else true
}
return null
}

companion object {
const val none = "none.gantsign.com"
.flatMap { (_, ruleProviders) -> ruleProviders }
.toSet()
}
}
Expand Up @@ -27,7 +27,7 @@ package com.github.gantsign.maven.plugin.ktlint.internal

import com.pinterest.ktlint.core.KtLint
import com.pinterest.ktlint.core.LintError
import com.pinterest.ktlint.core.RuleSet
import com.pinterest.ktlint.core.RuleProvider
import com.pinterest.ktlint.core.api.DefaultEditorConfigProperties.codeStyleSetProperty
import com.pinterest.ktlint.core.api.EditorConfigOverride
import com.pinterest.ktlint.core.api.EditorConfigOverride.Companion.plus
Expand Down Expand Up @@ -87,14 +87,17 @@ internal class Format(

val sourceFiles = ds.includedFiles.map { File(sourceRoot, it) }

val workingDir = File(".").absoluteFile

sourceFiles.forEach { file ->
if (!checkedFiles.add(file.canonicalFile)) {
return@forEach
}

val relativePath = file.toRelativeString(basedir)
val workingRelativePath = file.toRelativeString(workingDir)
val baseRelativePath = file.toRelativeString(basedir)

log.debug("checking format: $relativePath")
log.debug("checking format: $baseRelativePath")

val editorConfigOverride = EditorConfigOverride.emptyEditorConfigOverride
if (android) {
Expand All @@ -104,18 +107,17 @@ internal class Format(
val sourceText = file.readText(charset)

val formattedText = formatFile(
relativePath,
workingRelativePath,
sourceText,
ruleSets,
ruleProviders,
{ (line, col, _, detail), corrected ->
val lintError = "$relativePath:$line:$col: $detail"
val lintError = "$baseRelativePath:$line:$col: $detail"
log.debug("Format ${if (corrected) "fixed" else "could not fix"} > $lintError")
},
editorConfigOverride,
file.editorConfigPath
editorConfigOverride
)
if (formattedText !== sourceText) {
log.debug("Format fixed > $relativePath")
log.debug("Format fixed > $baseRelativePath")
file.writeText(formattedText, charset)
formattedFileCount.incrementAndGet()
}
Expand All @@ -128,19 +130,17 @@ internal class Format(
private fun formatFile(
fileName: String,
sourceText: String,
ruleSets: List<RuleSet>,
ruleProviders: Set<RuleProvider>,
onError: (err: LintError, corrected: Boolean) -> Unit,
editorConfigOverride: EditorConfigOverride,
editorConfigPath: String?
editorConfigOverride: EditorConfigOverride
): String = KtLint.format(
KtLint.ExperimentalParams(
fileName = fileName,
text = sourceText,
ruleSets = ruleSets,
ruleProviders = ruleProviders,
script = !fileName.endsWith(".kt", ignoreCase = true),
cb = onError,
editorConfigOverride = editorConfigOverride,
editorConfigPath = editorConfigPath
editorConfigOverride = editorConfigOverride
)
)
}
Expand Up @@ -71,9 +71,9 @@ class CheckMojoTest {
}

verify(atLeast = 1) { log.isDebugEnabled }
verify { log.debug("Discovered ruleset 'standard'") }
verify { log.debug("Discovered ruleset 'experimental'") }
verify { log.debug("Disabled ruleset 'experimental'") }
verify { log.debug("Discovered RuleSetProviderV2 'standard'") }
verify { log.debug("Discovered RuleSetProviderV2 'experimental'") }
verify { log.debug("Disabled RuleSetProviderV2 'experimental'") }
verify { log.debug("Discovered reporter 'maven'") }
verify { log.debug("Discovered reporter 'plain'") }
verify { log.debug("Discovered reporter 'json'") }
Expand Down Expand Up @@ -109,9 +109,9 @@ class CheckMojoTest {
}

verify(atLeast = 1) { log.isDebugEnabled }
verify { log.debug("Discovered ruleset 'standard'") }
verify { log.debug("Discovered ruleset 'experimental'") }
verify { log.debug("Disabled ruleset 'experimental'") }
verify { log.debug("Discovered RuleSetProviderV2 'standard'") }
verify { log.debug("Discovered RuleSetProviderV2 'experimental'") }
verify { log.debug("Disabled RuleSetProviderV2 'experimental'") }
verify { log.debug("Discovered reporter 'maven'") }
verify { log.debug("Discovered reporter 'plain'") }
verify { log.debug("Discovered reporter 'json'") }
Expand Down Expand Up @@ -148,9 +148,9 @@ class CheckMojoTest {
checkMojo.execute()

verify(atLeast = 1) { log.isDebugEnabled }
verify { log.debug("Discovered ruleset 'standard'") }
verify { log.debug("Discovered ruleset 'experimental'") }
verify { log.debug("Disabled ruleset 'experimental'") }
verify { log.debug("Discovered RuleSetProviderV2 'standard'") }
verify { log.debug("Discovered RuleSetProviderV2 'experimental'") }
verify { log.debug("Disabled RuleSetProviderV2 'experimental'") }
verify { log.debug("Discovered reporter 'maven'") }
verify { log.debug("Discovered reporter 'plain'") }
verify { log.debug("Discovered reporter 'json'") }
Expand Down
Expand Up @@ -63,14 +63,17 @@ class FormatMojoTest {
formatMojo.execute()

verify(atLeast = 1) { log.isDebugEnabled }
verify { log.debug("Disabled ruleset 'experimental'") }
verify { log.debug("Disabled RuleSetProviderV2 'experimental'") }
verify { log.debug("checking format: $source") }
verify { log.debug("Format fixed > $source:1:1: Unnecessary semicolon") }
verify {
log.debug(
"Format could not fix > $source:29:14: " +
"Exceeded max line length (80)"
"Format fixed > $source:29:13: Argument should be on a separate line " +
"(unless all arguments can fit a single line)"
)
}
verify { log.debug("Format fixed > $source:30:8: Missing newline before \")\"") }
verify { log.debug("Format could not fix > $source:29:14: Exceeded max line length (80)") }
verify { log.debug("Format fixed > $source") }
verify { log.warn("Source root doesn't exist: $testRoot") }
verify { log.info("1 file(s) formatted.") }
Expand All @@ -94,14 +97,17 @@ class FormatMojoTest {
formatMojo.execute()

verify(atLeast = 1) { log.isDebugEnabled }
verify { log.debug("Disabled ruleset 'experimental'") }
verify { log.debug("Disabled RuleSetProviderV2 'experimental'") }
verify { log.debug("checking format: $scriptSource") }
verify { log.debug("Format fixed > $scriptSource:1:1: Unnecessary semicolon") }
verify {
log.debug(
"Format could not fix > $scriptSource:29:14: " +
"Exceeded max line length (80)"
"Format fixed > $scriptSource:29:13: Argument should be on a separate line " +
"(unless all arguments can fit a single line)"
)
}
verify { log.debug("Format fixed > $scriptSource:30:8: Missing newline before \")\"") }
verify { log.debug("Format could not fix > $scriptSource:29:14: Exceeded max line length (80)") }
verify { log.debug("Format fixed > $scriptSource") }
verify { log.warn("Source root doesn't exist: $testRoot") }
verify { log.info("1 file(s) formatted.") }
Expand Down
Expand Up @@ -62,9 +62,9 @@ class KtlintReportTest {
ktlintReport.execute()

verify(atLeast = 1) { log.isDebugEnabled }
verify { log.debug("Discovered ruleset 'standard'") }
verify { log.debug("Discovered ruleset 'experimental'") }
verify { log.debug("Disabled ruleset 'experimental'") }
verify { log.debug("Discovered RuleSetProviderV2 'standard'") }
verify { log.debug("Discovered RuleSetProviderV2 'experimental'") }
verify { log.debug("Disabled RuleSetProviderV2 'experimental'") }
verify { log.debug("Discovered reporter 'maven'") }
verify { log.debug("Discovered reporter 'plain'") }
verify { log.debug("Discovered reporter 'json'") }
Expand Down