Skip to content

Commit

Permalink
Update Ktlint to v0.46.1 (#1374)
Browse files Browse the repository at this point in the history
* Support ktlint v0.46.* (#1379)
* enabled K2 compiler where it's require (after v0.46.0)
* supported removed classes and fields

Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
  • Loading branch information
renovate[bot] authored Jun 22, 2022
1 parent 76033d4 commit a34c513
Show file tree
Hide file tree
Showing 96 changed files with 263 additions and 277 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,13 @@ const val DIKTAT_COMMON = "DIKTAT_COMMON"
*/
const val DIKTAT = "diktat"

/**
* this constant will be used everywhere in the code to mark usage of Diktat ruleset
*/
const val DIKTAT_RULE_SET_ID = "diktat-ruleset"
const val DIKTAT_ANALYSIS_CONF = "diktat-analysis.yml"
const val DIKTAT_CONF_PROPERTY = "diktat.config.path"

/**
* This interface represents individual inspection in rule set.
*/
Expand Down
2 changes: 1 addition & 1 deletion diktat-gradle-plugin/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ val jacocoVersion = project.properties.getOrDefault("jacocoVersion", "0.8.7") as
dependencies {
implementation(kotlin("gradle-plugin-api"))

implementation("org.cqfn.diktat:diktat-rules:$diktatVersion") {
implementation("org.cqfn.diktat:diktat-common:$diktatVersion") {
exclude("org.jetbrains.kotlin", "kotlin-compiler-embeddable")
exclude("org.jetbrains.kotlin", "kotlin-stdlib-jdk8")
exclude("org.jetbrains.kotlin", "kotlin-stdlib-jdk7")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package org.cqfn.diktat.plugin.gradle

import org.cqfn.diktat.common.config.rules.DIKTAT_CONF_PROPERTY
import org.cqfn.diktat.plugin.gradle.DiktatGradlePlugin.Companion.DIKTAT_CHECK_TASK
import org.cqfn.diktat.plugin.gradle.DiktatGradlePlugin.Companion.DIKTAT_FIX_TASK
import org.cqfn.diktat.plugin.gradle.DiktatGradlePlugin.Companion.MIN_JVM_REQUIRES_ADD_OPENS
import org.cqfn.diktat.ruleset.rules.DIKTAT_CONF_PROPERTY

import generated.DIKTAT_VERSION
import generated.KTLINT_VERSION
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.cqfn.diktat.plugin.gradle

import org.cqfn.diktat.ruleset.rules.DIKTAT_CONF_PROPERTY
import org.cqfn.diktat.common.config.rules.DIKTAT_CONF_PROPERTY

import org.gradle.api.Project
import org.gradle.testfixtures.ProjectBuilder
import org.junit.jupiter.api.AfterEach
Expand Down Expand Up @@ -207,7 +208,7 @@ class DiktatJavaExecTaskTest {
}

private fun combinePathParts(vararg parts: String) = parts.joinToString(File.separator)

companion object {
private const val DIKTAT_CHECK_TASK = "diktatCheck"
}
Expand Down
5 changes: 5 additions & 0 deletions diktat-maven-plugin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,11 @@
</configuration>
</execution>
</executions>
<configuration>
<args>
<arg>-Xuse-k2</arg>
</args>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
@file:Suppress(
"Deprecation"
)

package org.cqfn.diktat.plugin.maven

import org.cqfn.diktat.ruleset.rules.DiktatRuleSetProvider
Expand All @@ -7,8 +11,8 @@ import com.pinterest.ktlint.core.LintError
import com.pinterest.ktlint.core.Reporter
import com.pinterest.ktlint.core.RuleExecutionException
import com.pinterest.ktlint.core.RuleSet
import com.pinterest.ktlint.core.api.FeatureInAlphaState
import com.pinterest.ktlint.core.internal.CurrentBaseline
import com.pinterest.ktlint.core.internal.containsLintError
import com.pinterest.ktlint.core.internal.loadBaseline
import com.pinterest.ktlint.reporter.baseline.BaselineReporter
import com.pinterest.ktlint.reporter.html.HtmlReporter
Expand Down Expand Up @@ -94,7 +98,6 @@ abstract class DiktatBaseMojo : AbstractMojo() {
/**
* @param params instance of [KtLint.ExperimentalParams] used in analysis
*/
@OptIn(FeatureInAlphaState::class)
abstract fun runAction(params: KtLint.ExperimentalParams)

/**
Expand Down Expand Up @@ -210,7 +213,7 @@ abstract class DiktatBaseMojo : AbstractMojo() {
.forEach { file ->
log.debug("Checking file $file")
try {
reporterImpl.before(file.path)
reporterImpl.before(file.absolutePath)
checkFile(
file,
lintErrors,
Expand All @@ -220,15 +223,14 @@ abstract class DiktatBaseMojo : AbstractMojo() {
),
ruleSets
)
reporterImpl.after(file.path)
reporterImpl.after(file.absolutePath)
} catch (e: RuleExecutionException) {
log.error("Unhandled exception during rule execution: ", e)
throw MojoExecutionException("Unhandled exception during rule execution", e)
}
}
}

@OptIn(FeatureInAlphaState::class)
private fun checkFile(file: File,
lintErrors: MutableList<LintError>,
baselineErrors: List<LintError>,
Expand All @@ -237,18 +239,13 @@ abstract class DiktatBaseMojo : AbstractMojo() {
val text = file.readText()
val params =
KtLint.ExperimentalParams(
fileName = file.relativeTo(mavenProject.basedir).path,
fileName = file.absolutePath,
text = text,
ruleSets = ruleSets,
userData = mapOf("file_path" to file.path),
script = file.extension.equals("kts", ignoreCase = true),
cb = { lintError, isCorrected ->
if (baselineErrors.none {
// ktlint's BaselineReporter stores only these fields
it.line == lintError.line && it.col == lintError.col &&
it.ruleId == lintError.ruleId
}) {
reporterImpl.onLintError(file.path, lintError, isCorrected)
if (!baselineErrors.containsLintError(lintError)) {
reporterImpl.onLintError(file.absolutePath, lintError, isCorrected)
lintErrors.add(lintError)
}
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,11 @@ class DiktatFixMojo : DiktatBaseMojo() {
@OptIn(FeatureInAlphaState::class)
override fun runAction(params: KtLint.ExperimentalParams) {
val fileName = params.fileName
val filePath = params.userData["file_path"] ?: error("File path should be provided")
val fileContent = File(filePath).readText(charset("UTF-8"))
val fileContent = File(fileName).readText(charset("UTF-8"))
val formattedText = KtLint.format(params)
if (fileContent != formattedText) {
log.info("Original and formatted content differ, writing to $fileName...")
File(filePath).writeText(formattedText, charset("UTF-8"))
File(fileName).writeText(formattedText, charset("UTF-8"))
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ class DiktatMavenPluginIntegrationTest {

with(SoftAssertions()) {
try {
assertThat(mavenLog).containsPattern("""Original and formatted content differ, writing to [:\w/\\]+Test\.kt\.\.\.""")
assertThat(mavenLog).containsPattern("""Original and formatted content differ, writing to [:\w/\\-]+Test\.kt\.\.\.""")
assertThat(mavenLog).containsPattern("There are \\d+ lint errors")
assertThat(mavenLog).contains("[MISSING_KDOC_TOP_LEVEL]")
} finally {
Expand Down
1 change: 1 addition & 0 deletions diktat-rules/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@
<configuration>
<args>
<arg>-Xinline-classes</arg>
<arg>-Xuse-k2</arg>
</args>
</configuration>
</plugin>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package org.cqfn.diktat.ruleset.rules

import org.cqfn.diktat.common.config.rules.DIKTAT_ANALYSIS_CONF
import org.cqfn.diktat.common.config.rules.DIKTAT_COMMON
import org.cqfn.diktat.common.config.rules.DIKTAT_CONF_PROPERTY
import org.cqfn.diktat.common.config.rules.DIKTAT_RULE_SET_ID
import org.cqfn.diktat.common.config.rules.RulesConfig
import org.cqfn.diktat.common.config.rules.RulesConfigReader
import org.cqfn.diktat.ruleset.constants.Warnings
Expand Down Expand Up @@ -87,13 +90,6 @@ import org.slf4j.LoggerFactory

import java.io.File

/**
* this constant will be used everywhere in the code to mark usage of Diktat ruleset
*/
const val DIKTAT_RULE_SET_ID = "diktat-ruleset"
const val DIKTAT_ANALYSIS_CONF = "diktat-analysis.yml"
const val DIKTAT_CONF_PROPERTY = "diktat.config.path"

/**
* [RuleSetProvider] that provides diKTat ruleset.
* By default it is expected to have diktat-analysis.yml configuration in the root folder where 'ktlint' is run
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -783,7 +783,7 @@ fun ASTNode.findAllNodesWithConditionOnLine(
* @return name of the file [this] node belongs to
*/
fun ASTNode.getFilePath(): String = getRootNode().also {
require(it.elementType == FILE) { "Root node type is not FILE, but file_path is present in user_data only in FILE nodes" }
require(it.elementType == FILE) { "Root node type is not FILE, but ${KtLint.FILE_PATH_USER_DATA_KEY} is present in user_data only in FILE nodes" }
}.getUserData(KtLint.FILE_PATH_USER_DATA_KEY).let {
requireNotNull(it) { "File path is not present in user data" }
}
Expand Down Expand Up @@ -833,6 +833,15 @@ fun ASTNode.takeByChainOfTypes(vararg types: IElementType): ASTNode? {
return node
}

private fun <T> Sequence<T>.takeWhileInclusive(pred: (T) -> Boolean): Sequence<T> {
var shouldContinue = true
return takeWhile {
val result = shouldContinue
shouldContinue = pred(it)
result
}
}

private fun Collection<KtAnnotationEntry>.containSuppressWithName(name: String) =
this.any {
it.shortName.toString() == (Suppress::class.simpleName) &&
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.cqfn.diktat.ruleset.chapter1

import org.cqfn.diktat.common.config.rules.DIKTAT_RULE_SET_ID
import org.cqfn.diktat.common.config.rules.RulesConfig
import org.cqfn.diktat.ruleset.constants.Warnings.BACKTICKS_PROHIBITED
import org.cqfn.diktat.ruleset.constants.Warnings.CLASS_NAME_INCORRECT
Expand All @@ -14,7 +15,6 @@ import org.cqfn.diktat.ruleset.constants.Warnings.OBJECT_NAME_INCORRECT
import org.cqfn.diktat.ruleset.constants.Warnings.VARIABLE_HAS_PREFIX
import org.cqfn.diktat.ruleset.constants.Warnings.VARIABLE_NAME_INCORRECT
import org.cqfn.diktat.ruleset.constants.Warnings.VARIABLE_NAME_INCORRECT_FORMAT
import org.cqfn.diktat.ruleset.rules.DIKTAT_RULE_SET_ID
import org.cqfn.diktat.ruleset.rules.chapter1.IdentifierNaming
import org.cqfn.diktat.util.LintTestBase

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package org.cqfn.diktat.ruleset.chapter1

import org.cqfn.diktat.common.config.rules.DIKTAT_RULE_SET_ID
import org.cqfn.diktat.ruleset.constants.Warnings.FUNCTION_BOOLEAN_PREFIX
import org.cqfn.diktat.ruleset.constants.Warnings.FUNCTION_NAME_INCORRECT_CASE
import org.cqfn.diktat.ruleset.constants.Warnings.TYPEALIAS_NAME_INCORRECT_CASE
import org.cqfn.diktat.ruleset.rules.DIKTAT_RULE_SET_ID
import org.cqfn.diktat.ruleset.rules.chapter1.IdentifierNaming
import org.cqfn.diktat.util.LintTestBase

Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package org.cqfn.diktat.ruleset.chapter1

import org.cqfn.diktat.common.config.rules.DIKTAT_RULE_SET_ID
import org.cqfn.diktat.common.config.rules.RulesConfig
import org.cqfn.diktat.ruleset.constants.Warnings.INCORRECT_PACKAGE_SEPARATOR
import org.cqfn.diktat.ruleset.constants.Warnings.PACKAGE_NAME_INCORRECT_CASE
import org.cqfn.diktat.ruleset.constants.Warnings.PACKAGE_NAME_INCORRECT_PATH
import org.cqfn.diktat.ruleset.constants.Warnings.PACKAGE_NAME_INCORRECT_PREFIX
import org.cqfn.diktat.ruleset.constants.Warnings.PACKAGE_NAME_INCORRECT_SYMBOLS
import org.cqfn.diktat.ruleset.constants.Warnings.PACKAGE_NAME_MISSING
import org.cqfn.diktat.ruleset.rules.DIKTAT_RULE_SET_ID
import org.cqfn.diktat.ruleset.rules.chapter1.PackageNaming
import org.cqfn.diktat.util.LintTestBase
import org.cqfn.diktat.util.TEST_FILE_NAME
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package org.cqfn.diktat.ruleset.chapter2

import org.cqfn.diktat.common.config.rules.DIKTAT_RULE_SET_ID
import org.cqfn.diktat.ruleset.constants.Warnings
import org.cqfn.diktat.ruleset.constants.Warnings.IF_ELSE_COMMENTS
import org.cqfn.diktat.ruleset.rules.DIKTAT_RULE_SET_ID
import org.cqfn.diktat.ruleset.rules.chapter2.kdoc.CommentsFormatting
import org.cqfn.diktat.util.LintTestBase

Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package org.cqfn.diktat.ruleset.chapter2

import org.cqfn.diktat.common.config.rules.DIKTAT_RULE_SET_ID
import org.cqfn.diktat.common.config.rules.RulesConfig
import org.cqfn.diktat.ruleset.constants.Warnings
import org.cqfn.diktat.ruleset.constants.Warnings.HEADER_MISSING_IN_NON_SINGLE_CLASS_FILE
import org.cqfn.diktat.ruleset.constants.Warnings.HEADER_MISSING_OR_WRONG_COPYRIGHT
import org.cqfn.diktat.ruleset.constants.Warnings.HEADER_NOT_BEFORE_PACKAGE
import org.cqfn.diktat.ruleset.constants.Warnings.HEADER_WRONG_FORMAT
import org.cqfn.diktat.ruleset.rules.DIKTAT_RULE_SET_ID
import org.cqfn.diktat.ruleset.rules.chapter2.comments.HeaderCommentRule
import org.cqfn.diktat.util.LintTestBase

Expand Down Expand Up @@ -478,7 +478,7 @@ class HeaderCommentRuleTest : LintTestBase(::HeaderCommentRule) {
@Tag(WarningNames.HEADER_NOT_BEFORE_PACKAGE)
fun `header KDoc in gradle script`() {
lintMethod(
"""
"""
|version = "0.1.0-SNAPSHOT"
|
""".trimMargin(),
Expand All @@ -491,7 +491,7 @@ class HeaderCommentRuleTest : LintTestBase(::HeaderCommentRule) {
@Tag(WarningNames.HEADER_NOT_BEFORE_PACKAGE)
fun `header KDoc in kts script`() {
lintMethod(
"""
"""
|val version = "0.1.0-SNAPSHOT"
|
""".trimMargin(),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package org.cqfn.diktat.ruleset.chapter2

import org.cqfn.diktat.common.config.rules.DIKTAT_RULE_SET_ID
import org.cqfn.diktat.ruleset.constants.Warnings
import org.cqfn.diktat.ruleset.constants.Warnings.KDOC_EXTRA_PROPERTY
import org.cqfn.diktat.ruleset.constants.Warnings.KDOC_NO_CLASS_BODY_PROPERTIES_IN_HEADER
import org.cqfn.diktat.ruleset.constants.Warnings.KDOC_NO_CONSTRUCTOR_PROPERTY
import org.cqfn.diktat.ruleset.constants.Warnings.KDOC_NO_CONSTRUCTOR_PROPERTY_WITH_COMMENT
import org.cqfn.diktat.ruleset.constants.Warnings.MISSING_KDOC_CLASS_ELEMENTS
import org.cqfn.diktat.ruleset.constants.Warnings.MISSING_KDOC_TOP_LEVEL
import org.cqfn.diktat.ruleset.rules.DIKTAT_RULE_SET_ID
import org.cqfn.diktat.ruleset.rules.chapter2.kdoc.KdocComments
import org.cqfn.diktat.util.LintTestBase

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.cqfn.diktat.ruleset.chapter2

import org.cqfn.diktat.common.config.rules.DIKTAT_RULE_SET_ID
import org.cqfn.diktat.common.config.rules.RulesConfig
import org.cqfn.diktat.ruleset.constants.Warnings
import org.cqfn.diktat.ruleset.constants.Warnings.KDOC_EMPTY_KDOC
Expand All @@ -10,7 +11,6 @@ import org.cqfn.diktat.ruleset.constants.Warnings.KDOC_NO_NEWLINES_BETWEEN_BASIC
import org.cqfn.diktat.ruleset.constants.Warnings.KDOC_NO_NEWLINE_AFTER_SPECIAL_TAGS
import org.cqfn.diktat.ruleset.constants.Warnings.KDOC_WRONG_SPACES_AFTER_TAG
import org.cqfn.diktat.ruleset.constants.Warnings.KDOC_WRONG_TAGS_ORDER
import org.cqfn.diktat.ruleset.rules.DIKTAT_RULE_SET_ID
import org.cqfn.diktat.ruleset.rules.chapter2.kdoc.KdocFormatting
import org.cqfn.diktat.util.LintTestBase

Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package org.cqfn.diktat.ruleset.chapter2

import org.cqfn.diktat.common.config.rules.DIKTAT_COMMON
import org.cqfn.diktat.common.config.rules.DIKTAT_RULE_SET_ID
import org.cqfn.diktat.common.config.rules.RulesConfig
import org.cqfn.diktat.ruleset.constants.Warnings.KDOC_TRIVIAL_KDOC_ON_FUNCTION
import org.cqfn.diktat.ruleset.constants.Warnings.KDOC_WITHOUT_PARAM_TAG
import org.cqfn.diktat.ruleset.constants.Warnings.KDOC_WITHOUT_RETURN_TAG
import org.cqfn.diktat.ruleset.constants.Warnings.KDOC_WITHOUT_THROWS_TAG
import org.cqfn.diktat.ruleset.constants.Warnings.MISSING_KDOC_ON_FUNCTION
import org.cqfn.diktat.ruleset.rules.DIKTAT_RULE_SET_ID
import org.cqfn.diktat.ruleset.rules.chapter2.kdoc.KdocMethods
import org.cqfn.diktat.util.LintTestBase

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package org.cqfn.diktat.ruleset.chapter2

import org.cqfn.diktat.common.config.rules.DIKTAT_RULE_SET_ID
import org.cqfn.diktat.ruleset.constants.Warnings.KDOC_WITHOUT_PARAM_TAG
import org.cqfn.diktat.ruleset.constants.Warnings.MISSING_KDOC_ON_FUNCTION
import org.cqfn.diktat.ruleset.rules.DIKTAT_RULE_SET_ID
import org.cqfn.diktat.ruleset.rules.chapter2.kdoc.KdocMethods
import org.cqfn.diktat.util.LintTestBase

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package org.cqfn.diktat.ruleset.chapter2.comments

import org.cqfn.diktat.common.config.rules.DIKTAT_RULE_SET_ID
import org.cqfn.diktat.ruleset.constants.Warnings.COMMENTED_OUT_CODE
import org.cqfn.diktat.ruleset.rules.DIKTAT_RULE_SET_ID
import org.cqfn.diktat.ruleset.rules.chapter2.comments.CommentsRule
import org.cqfn.diktat.util.LintTestBase

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package org.cqfn.diktat.ruleset.chapter3

import org.cqfn.diktat.common.config.rules.DIKTAT_RULE_SET_ID
import org.cqfn.diktat.ruleset.constants.Warnings
import org.cqfn.diktat.ruleset.rules.DIKTAT_RULE_SET_ID
import org.cqfn.diktat.ruleset.rules.chapter3.AnnotationNewLineRule
import org.cqfn.diktat.util.LintTestBase

Expand Down
Loading

0 comments on commit a34c513

Please sign in to comment.