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

Update Ktlint to v0.47.1 #1506

Merged
merged 32 commits into from
Oct 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
e4026b5
Update Ktlint to v0.47.1
renovate[bot] Oct 22, 2022
5478c7f
Migrate to the new (ktlint 0.47+) Baseline API
0x6675636b796f75676974687562 Oct 25, 2022
fb9bd6b
Configure Surefire and Failsafe plugins for better reporting
0x6675636b796f75676974687562 Oct 25, 2022
c2b0008
Suppress deprecation warnings (`RuleSet` and `RuleSetProvider` are no…
0x6675636b796f75676974687562 Oct 25, 2022
8756724
Temporarily suppress the now deprecated `ASTNode.visit()` call to mak…
0x6675636b796f75676974687562 Oct 25, 2022
ea57ecc
Use the same "me.fabriciorby:maven-surefire-junit5-tree-reporter" ver…
0x6675636b796f75676974687562 Oct 25, 2022
768af6b
Make Diktat happy
0x6675636b796f75676974687562 Oct 25, 2022
f92561e
Suppress more deprecation warnings
0x6675636b796f75676974687562 Oct 25, 2022
311c34e
Suppress more deprecation warnings
0x6675636b796f75676974687562 Oct 25, 2022
5a8a39d
Remove `DIKTAT_RULE_SET_ID` from expected lint errors (ignored in the…
0x6675636b796f75676974687562 Oct 25, 2022
05c543f
Suppress more deprecation warnings
0x6675636b796f75676974687562 Oct 25, 2022
3c73182
Bump versions in documentation and string literals
0x6675636b796f75676974687562 Oct 26, 2022
01ed8c9
Hide the stderr of Maven, if _JAVA_OPTIONS is set
0x6675636b796f75676974687562 Oct 26, 2022
f3a70b7
Correct the KDoc reference
0x6675636b796f75676974687562 Oct 26, 2022
069c1af
Drop `FeatureInAlphaState` annotations (no longer necessary)
0x6675636b796f75676974687562 Oct 26, 2022
7d4db80
Make sure rule ids are always fully-qualified (previous behaviour bro…
0x6675636b796f75676974687562 Oct 26, 2022
3efb83c
Revert "Remove `DIKTAT_RULE_SET_ID` from expected lint errors (ignore…
0x6675636b796f75676974687562 Oct 26, 2022
1135a4b
Print out stack traces when running the snapshot version of the Maven…
0x6675636b796f75676974687562 Oct 26, 2022
f5ca590
Ignore SARIF reports
0x6675636b796f75676974687562 Oct 27, 2022
a8ce2d3
Clean up and fix `OrderedRuleSet`
0x6675636b796f75676974687562 Oct 27, 2022
1598cb9
Merge branch 'master' into renovate/ktlint
0x6675636b796f75676974687562 Oct 27, 2022
f7e3e3b
Test file management
0x6675636b796f75676974687562 Oct 27, 2022
8a952b7
Fix the assertion
0x6675636b796f75676974687562 Oct 27, 2022
cfad5cb
Ignore the corrected errors when in "fix" mode
0x6675636b796f75676974687562 Oct 27, 2022
ad2a991
Allow lint errors to appear in any order
0x6675636b796f75676974687562 Oct 27, 2022
d3c05ed
Fix a smoke test
0x6675636b796f75676974687562 Oct 28, 2022
41a456d
Fix `OrderedRuleSetTest`
0x6675636b796f75676974687562 Oct 28, 2022
b6ed77e
Ignore temporary files produced by smoke tests
0x6675636b796f75676974687562 Oct 28, 2022
e03767a
Merge branch 'master' into renovate/ktlint
0x6675636b796f75676974687562 Oct 28, 2022
3838e02
Enable extra diagnostics when running diktat from save-cli
0x6675636b796f75676974687562 Oct 28, 2022
62825ef
Enable extra save-cli verbosity when running it on Windows, too
0x6675636b796f75676974687562 Oct 28, 2022
a5db5d6
When running save-cli, override the temporary directory on Windows
0x6675636b796f75676974687562 Oct 28, 2022
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
4 changes: 2 additions & 2 deletions .github/workflows/diktat_snapshot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ jobs:
# Base version is stored in env variable for later usage.
# Note: maven-help-plugin requires non-interactive mode (do not add -B flag).
run: |
snapshot_version=$(printf 'VERSION=${project.version}\n0\n' | mvn help:evaluate | grep '^VERSION' | cut -d= -f2)
snapshot_version=$(printf 'VERSION=${project.version}\n0\n' | mvn help:evaluate 2>/dev/null | grep '^VERSION' | cut -d= -f2)
base_version=${snapshot_version%-SNAPSHOT}
echo "BASE_VERSION=$base_version" >> $GITHUB_ENV
mvn -B versions:set -DnewVersion=$base_version-pre
Expand All @@ -46,7 +46,7 @@ jobs:
mvn -B versions:set-property -Dproperty=diktat-check.version -DnewVersion=${{ env.BASE_VERSION }}-pre
- name: Run diktat snapshot via maven plugin
run: |
mvn -B -Dstyle.color=always diktat:check@diktat -Ddiktat.debug=true -Ddiktat.githubActions=true
mvn -B -X -Dstyle.color=always diktat:check@diktat -Ddiktat.debug=true -Ddiktat.githubActions=true
- name: Upload SARIF to Github using the upload-sarif action
uses: github/codeql-action/upload-sarif@v2
if: ${{ always() }}
Expand Down
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,5 @@ out/

# Vim swap files
*.swp

*.sarif
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ Main features of diktat are the following:
```shell
# another option is "brew install ktlint"

curl -sSLO https://github.com/pinterest/ktlint/releases/download/0.46.1/ktlint && chmod a+x ktlint
curl -sSLO https://github.com/pinterest/ktlint/releases/download/0.47.1/ktlint && chmod a+x ktlint
```

1. Load diKTat manually: [here](https://github.com/saveourtool/diKTat/releases/download/v1.2.3/diktat-1.2.3.jar)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,20 @@ fun String.kotlinVersion(): KotlinVersion {
}
}

/**
* Makes sure this _rule id_ is qualified with a _rule set id_.
*
* @param ruleSetId the _rule set id_; defaults to [DIKTAT_RULE_SET_ID].
* @return the fully-qualified _rule id_ in the form of `ruleSetId:ruleId`.
* @see DIKTAT_RULE_SET_ID
* @since 1.2.4
*/
fun String.qualifiedWithRuleSetId(ruleSetId: String = DIKTAT_RULE_SET_ID): String =
when {
this.contains(':') -> this
else -> "$ruleSetId:$this"
}

/**
* Get [RulesConfig] representing common configuration part that can be used in any rule
*/
Expand Down
4 changes: 2 additions & 2 deletions diktat-gradle-plugin/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ repositories {

// default value is needed for correct gradle loading in IDEA; actual value from maven is used during build
// To debug gradle plugin, please set `diktatVersion` manually to the current maven project version.
val ktlintVersion = project.properties.getOrDefault("ktlintVersion", "0.46.1") as String
val diktatVersion = project.version.takeIf { it.toString() != Project.DEFAULT_VERSION } ?: "1.2.1"
val ktlintVersion = project.properties.getOrDefault("ktlintVersion", "0.47.1") as String
val diktatVersion = project.version.takeIf { it.toString() != Project.DEFAULT_VERSION } ?: "1.2.3"
val junitVersion = project.properties.getOrDefault("junitVersion", "5.8.1") as String
val jacocoVersion = project.properties.getOrDefault("jacocoVersion", "0.8.7") as String
dependencies {
Expand Down
54 changes: 54 additions & 0 deletions diktat-maven-plugin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -142,10 +142,37 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<dependencies>
<dependency>
<groupId>me.fabriciorby</groupId>
<artifactId>maven-surefire-junit5-tree-reporter</artifactId>
<version>${junit-tree-reporter.version}</version>
</dependency>
</dependencies>
<configuration>
<excludes>
<exclude>*IntegrationTest*</exclude>
</excludes>
<statelessTestsetReporter implementation="org.apache.maven.plugin.surefire.extensions.junit5.JUnit5Xml30StatelessReporter">
<!-- Prefer @DisplayName & friends, if any. -->
<usePhrasedFileName>true</usePhrasedFileName>
<usePhrasedTestCaseClassName>true</usePhrasedTestCaseClassName>
<usePhrasedTestCaseMethodName>true</usePhrasedTestCaseMethodName>
<usePhrasedTestSuiteClassName>true</usePhrasedTestSuiteClassName>
</statelessTestsetReporter>
<!-- For the console output, use the tree reporter from
https://github.com/fabriciorby/maven-surefire-junit5-tree-reporter. -->
<reportFormat>plain</reportFormat>
<consoleOutputReporter>
<disable>true</disable>
</consoleOutputReporter>
<statelessTestsetInfoReporter implementation="org.apache.maven.plugin.surefire.extensions.junit5.JUnit5StatelessTestsetInfoTreeReporter">
<!-- Prefer @DisplayName & friends, if any. -->
<usePhrasedFileName>true</usePhrasedFileName>
<usePhrasedClassNameInRunning>true</usePhrasedClassNameInRunning>
<!-- Avoid null's in *.txt summary -->
<usePhrasedClassNameInTestCaseSummary>false</usePhrasedClassNameInTestCaseSummary>
</statelessTestsetInfoReporter>
</configuration>
</plugin>
<plugin>
Expand All @@ -165,6 +192,13 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<dependencies>
<dependency>
<groupId>me.fabriciorby</groupId>
<artifactId>maven-surefire-junit5-tree-reporter</artifactId>
<version>${junit-tree-reporter.version}</version>
</dependency>
</dependencies>
<configuration>
<systemProperties>
<maven.version>${maven.version}</maven.version>
Expand All @@ -175,6 +209,26 @@
</includes>
<testSourceDirectory>${project.build.testSourceDirectory}</testSourceDirectory>
<testClassesDirectory>${project.build.testOutputDirectory}</testClassesDirectory>
<statelessTestsetReporter implementation="org.apache.maven.plugin.surefire.extensions.junit5.JUnit5Xml30StatelessReporter">
<!-- Prefer @DisplayName & friends, if any. -->
<usePhrasedFileName>true</usePhrasedFileName>
<usePhrasedTestCaseClassName>true</usePhrasedTestCaseClassName>
<usePhrasedTestCaseMethodName>true</usePhrasedTestCaseMethodName>
<usePhrasedTestSuiteClassName>true</usePhrasedTestSuiteClassName>
</statelessTestsetReporter>
<!-- For the console output, use the tree reporter from
https://github.com/fabriciorby/maven-surefire-junit5-tree-reporter. -->
<reportFormat>plain</reportFormat>
<consoleOutputReporter>
<disable>true</disable>
</consoleOutputReporter>
<statelessTestsetInfoReporter implementation="org.apache.maven.plugin.surefire.extensions.junit5.JUnit5StatelessTestsetInfoTreeReporter">
<!-- Prefer @DisplayName & friends, if any. -->
<usePhrasedFileName>true</usePhrasedFileName>
<usePhrasedClassNameInRunning>true</usePhrasedClassNameInRunning>
<!-- Avoid null's in *.txt summary -->
<usePhrasedClassNameInTestCaseSummary>false</usePhrasedClassNameInTestCaseSummary>
</statelessTestsetInfoReporter>
</configuration>
<executions>
<execution>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,18 @@ 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.internal.CurrentBaseline
import com.pinterest.ktlint.core.internal.containsLintError
import com.pinterest.ktlint.core.internal.loadBaseline
import com.pinterest.ktlint.core.api.Baseline
import com.pinterest.ktlint.core.api.Baseline.Status.VALID
import com.pinterest.ktlint.core.api.containsLintError
import com.pinterest.ktlint.core.api.loadBaseline
import com.pinterest.ktlint.reporter.baseline.BaselineReporter
import com.pinterest.ktlint.reporter.html.HtmlReporter
import com.pinterest.ktlint.reporter.json.JsonReporter
import com.pinterest.ktlint.reporter.plain.PlainReporter
import com.pinterest.ktlint.reporter.sarif.SarifReporter
import org.apache.maven.execution.MavenSession
import org.apache.maven.plugin.AbstractMojo
import org.apache.maven.plugin.Mojo
import org.apache.maven.plugin.MojoExecutionException
import org.apache.maven.plugin.MojoFailureException
import org.apache.maven.plugins.annotations.Parameter
Expand Down Expand Up @@ -119,15 +121,15 @@ abstract class DiktatBaseMojo : AbstractMojo() {
listOf(DiktatRuleSetProvider(configFile).get())
}
val baselineResults = baseline?.let { loadBaseline(it.absolutePath) }
?: CurrentBaseline(emptyMap(), false)
?: Baseline(status = VALID)
reporterImpl = resolveReporter(baselineResults)
reporterImpl.beforeAll()
val lintErrors: MutableList<LintError> = mutableListOf()

inputs
.map(::File)
.forEach {
checkDirectory(it, lintErrors, baselineResults.baselineRules ?: emptyMap(), ruleSets)
checkDirectory(it, lintErrors, baselineResults.lintErrorsPerFile, ruleSets)
}

reporterImpl.afterAll()
Expand All @@ -136,7 +138,7 @@ abstract class DiktatBaseMojo : AbstractMojo() {
}
}

private fun resolveReporter(baselineResults: CurrentBaseline): Reporter {
private fun resolveReporter(baselineResults: Baseline): Reporter {
val output = if (this.output.isBlank()) {
if (this.githubActions) {
// need to set user.home specially for ktlint, so it will be able to put a relative path URI in SARIF
Expand Down Expand Up @@ -164,7 +166,7 @@ abstract class DiktatBaseMojo : AbstractMojo() {
}
}

return if (baselineResults.baselineGenerationNeeded) {
return if (baselineResults.status != VALID) {
val baselineReporter = BaselineReporter(PrintStream(FileOutputStream(baseline, true)))
return Reporter.from(actualReporter, baselineReporter)
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@

package org.cqfn.diktat.plugin.maven

import org.cqfn.diktat.ruleset.rules.DiktatRuleSetProvider
import org.cqfn.diktat.ruleset.utils.ignoreCorrectedErrors

import com.pinterest.ktlint.core.KtLint
import com.pinterest.ktlint.core.api.FeatureInAlphaState
import org.apache.maven.plugins.annotations.Mojo

import java.io.File
Expand All @@ -19,7 +21,6 @@ class DiktatCheckMojo : DiktatBaseMojo() {
/**
* @param params instance of [KtLint.ExperimentalParams] used in analysis
*/
@OptIn(FeatureInAlphaState::class)
override fun runAction(params: KtLint.ExperimentalParams) {
KtLint.lint(params)
}
Expand All @@ -35,11 +36,10 @@ class DiktatFixMojo : DiktatBaseMojo() {
/**
* @param params instance of [KtLint.Params] used in analysis
*/
@OptIn(FeatureInAlphaState::class)
override fun runAction(params: KtLint.ExperimentalParams) {
val fileName = params.fileName
val fileContent = File(fileName).readText(charset("UTF-8"))
val formattedText = KtLint.format(params)
val formattedText = KtLint.format(params.ignoreCorrectedErrors())
if (fileContent != formattedText) {
log.info("Original and formatted content differ, writing to $fileName...")
File(fileName).writeText(formattedText, charset("UTF-8"))
Expand Down
2 changes: 1 addition & 1 deletion diktat-rules/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@
<dependency>
<groupId>me.fabriciorby</groupId>
<artifactId>maven-surefire-junit5-tree-reporter</artifactId>
<version>1.1.0</version>
<version>${junit-tree-reporter.version}</version>
</dependency>
</dependencies>
<configuration>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package org.cqfn.diktat.ruleset.rules

import org.cqfn.diktat.common.config.rules.RulesConfig
import org.cqfn.diktat.common.config.rules.isRuleEnabled
import org.cqfn.diktat.common.config.rules.qualifiedWithRuleSetId
import org.cqfn.diktat.common.utils.loggerWithKtlintConfig
import org.cqfn.diktat.ruleset.constants.EmitType
import org.cqfn.diktat.ruleset.utils.getFilePath
Expand All @@ -25,7 +26,7 @@ abstract class DiktatRule(
val configRules: List<RulesConfig>,
private val inspections: List<DiktatConfigRule>,
visitorModifiers: Set<VisitorModifier> = emptySet(),
) : Rule(id, visitorModifiers) {
) : Rule(id.qualifiedWithRuleSetId(), visitorModifiers) {
/**
* Default value is false
*/
Expand All @@ -36,7 +37,10 @@ abstract class DiktatRule(
*/
lateinit var emitWarn: EmitType

@Suppress("TooGenericExceptionThrown")
@Suppress(
"TooGenericExceptionThrown",
"OVERRIDE_DEPRECATION",
)
override fun visit(
node: ASTNode,
autoCorrect: Boolean,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
@file:Suppress(
"Deprecation"
)

package org.cqfn.diktat.ruleset.rules

import org.cqfn.diktat.common.config.rules.DIKTAT_ANALYSIS_CONF
Expand Down Expand Up @@ -108,6 +112,7 @@ class DiktatRuleSetProvider(private var diktatConfigFile: String = DIKTAT_ANALYS
@Suppress(
"LongMethod",
"TOO_LONG_FUNCTION",
"OVERRIDE_DEPRECATION",
)
override fun get(): RuleSet {
log.debug("Will run $DIKTAT_RULE_SET_ID with $diktatConfigFile" +
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
@file:Suppress(
"Deprecation"
)

package org.cqfn.diktat.ruleset.rules

import org.cqfn.diktat.common.config.rules.qualifiedWithRuleSetId
import org.cqfn.diktat.ruleset.constants.EmitType
import com.pinterest.ktlint.core.Rule
import com.pinterest.ktlint.core.RuleSet
import com.pinterest.ktlint.core.api.EditorConfigProperties
import org.jetbrains.kotlin.com.intellij.lang.ASTNode

/**
Expand All @@ -16,7 +22,7 @@ class OrderedRuleSet(id: String, vararg rules: Rule) : RuleSet(id, rules = adjus
companion object {
private fun adjustRules(ruleSetId: String, vararg rules: Rule): Array<out Rule> {
if (rules.isEmpty()) {
return rules
return emptyArray()
}
return rules.mapIndexed { index, rule ->
if (index == 0) {
Expand All @@ -35,11 +41,13 @@ class OrderedRuleSet(id: String, vararg rules: Rule) : RuleSet(id, rules = adjus
): Set<Rule.VisitorModifier> {
val visitorModifiers: Set<Rule.VisitorModifier> = rule.visitorModifiers
checkVisitorModifiers(rule)
require(rule.id != prevRule.id) {
"PrevRule has same ID as rule: ${rule.id}"
val ruleId = rule.id.qualifiedWithRuleSetId(ruleSetId)
val previousRuleId = prevRule.id.qualifiedWithRuleSetId(ruleSetId)
require(ruleId != previousRuleId) {
"PrevRule has same ID as rule: $ruleId"
}
return visitorModifiers + Rule.VisitorModifier.RunAfterRule(
ruleId = ruleSetId + ":" + prevRule.id,
ruleId = previousRuleId,
loadOnlyWhenOtherRuleIsLoaded = false,
runOnlyWhenOtherRuleIsEnabled = false
)
Expand All @@ -59,7 +67,11 @@ class OrderedRuleSet(id: String, vararg rules: Rule) : RuleSet(id, rules = adjus
/**
* @return RuleSet with ordered rules
*/
fun RuleSet.ordered(): OrderedRuleSet = OrderedRuleSet(id = id, rules = rules)
fun RuleSet.ordered(): RuleSet =
when (this) {
is OrderedRuleSet -> this
else -> OrderedRuleSet(id = id, rules = rules)
}

/**
* @property rule wraps this rule to keep order
Expand All @@ -68,17 +80,37 @@ class OrderedRuleSet(id: String, vararg rules: Rule) : RuleSet(id, rules = adjus
ruleSetId: String,
val rule: Rule,
prevRule: Rule
) : Rule(rule.id, adjustVisitorModifiers(ruleSetId, rule, prevRule)) {
/**
* Delegating a call of this method
*/
) : Rule(rule.id.qualifiedWithRuleSetId(ruleSetId), adjustVisitorModifiers(ruleSetId, rule, prevRule)) {
override fun beforeFirstNode(editorConfigProperties: EditorConfigProperties) =
rule.beforeFirstNode(editorConfigProperties)

override fun beforeVisitChildNodes(
node: ASTNode,
autoCorrect: Boolean,
emit: EmitType,
) =
rule.beforeVisitChildNodes(node, autoCorrect, emit)

@Deprecated(
"Marked for deletion in ktlint 0.48.0",
replaceWith = ReplaceWith("beforeVisitChildNodes(node, autoCorrect, emit)"),
)
override fun visit(
node: ASTNode,
autoCorrect: Boolean,
emit: EmitType
) {
emit: EmitType,
) =
rule.visit(node, autoCorrect, emit)
}

override fun afterVisitChildNodes(
node: ASTNode,
autoCorrect: Boolean,
emit: EmitType,
) =
rule.afterVisitChildNodes(node, autoCorrect, emit)

override fun afterLastNode() =
rule.afterLastNode()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@ class IndentationRule(configRules: List<RulesConfig>) : DiktatRule(
*/
private fun checkIndentation(node: ASTNode) =
with(IndentContext(configuration)) {
@Suppress("Deprecation")
node.visit { astNode ->
checkAndReset(astNode)
val indentationIncrement = astNode.getIndentationIncrement()
Expand Down
Loading