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

Downgrade ktlint #1565

Merged
merged 12 commits into from
Nov 15, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.47.1/ktlint && chmod a+x ktlint
curl -sSLO https://github.com/pinterest/ktlint/releases/download/0.46.1/ktlint && chmod a+x ktlint
```

1. Load diKTat manually: [here](https://github.com/saveourtool/diKTat/releases/download/v1.2.4/diktat-1.2.4.jar)
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 @@ -28,7 +28,7 @@ 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.47.1") as String
val ktlintVersion = project.properties.getOrDefault("ktlintVersion", "0.46.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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,9 @@ 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.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.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
import com.pinterest.ktlint.reporter.json.JsonReporter
Expand Down Expand Up @@ -121,15 +120,15 @@ abstract class DiktatBaseMojo : AbstractMojo() {
listOf(DiktatRuleSetProvider(configFile).get())
}
val baselineResults = baseline?.let { loadBaseline(it.absolutePath) }
?: Baseline(status = VALID)
?: CurrentBaseline(emptyMap(), false)
reporterImpl = resolveReporter(baselineResults)
reporterImpl.beforeAll()
val lintErrors: MutableList<LintError> = mutableListOf()

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

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

private fun resolveReporter(baselineResults: Baseline): Reporter {
private fun resolveReporter(baselineResults: CurrentBaseline): 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 @@ -166,7 +165,7 @@ abstract class DiktatBaseMojo : AbstractMojo() {
}
}

return if (baselineResults.status != VALID) {
return if (baselineResults.baselineGenerationNeeded) {
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 @@ -34,21 +34,21 @@ abstract class DiktatRule(

/**
* The **file-specific** error emitter, initialized in
* [beforeVisitChildNodes] and used in [logic] implementations.
* [visit] and used in [logic] implementations.
*
* Since the file is indirectly a part of the state of a `Rule`, the same
* `Rule` instance should **never be re-used** to check more than a single
* file, or confusing effects (incl. race conditions) will occur.
* See the documentation of the [Rule] class for more details.
*
* @see Rule
* @see beforeVisitChildNodes
* @see visit
* @see logic
*/
lateinit var emitWarn: EmitType

@Suppress("TooGenericExceptionThrown")
final override fun beforeVisitChildNodes(
final override fun visit(
node: ASTNode,
autoCorrect: Boolean,
emit: EmitType
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ 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 Down Expand Up @@ -81,16 +80,6 @@ class OrderedRuleSet(id: String, vararg rules: Rule) : RuleSet(id, rules = adjus
val rule: Rule,
prevRule: Rule
) : 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)"),
Expand All @@ -101,16 +90,6 @@ class OrderedRuleSet(id: String, vararg rules: Rule) : RuleSet(id, rules = adjus
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 @@ -27,7 +27,7 @@ class RunInScript(private val configRules: List<RulesConfig>) : Rule(NAME_ID.qua
private var isFixMode: Boolean = false
private lateinit var emitWarn: EmitType

override fun beforeVisitChildNodes(
override fun visit(
node: ASTNode,
autoCorrect: Boolean,
emit: EmitType
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,12 +134,19 @@ class OrderedRuleSetTest {
* |
* V
* C(File) -> C(Node) -> C(Leaf)
*
* val expectedRuleInvocationOrder = rules.asSequence()
* .map(Rule::id)
* .flatMap { ruleId ->
* generateSequence { ruleId }.take(astNodeCount)
* }
* .toList()
*/
val expectedRuleInvocationOrder = rules.asSequence()
.map(Rule::id)
.flatMap { ruleId ->
generateSequence { ruleId }.take(astNodeCount)
}
val expectedRuleInvocationOrder = generateSequence {
rules.map(Rule::id)
}
.take(astNodeCount)
.flatten()
.toList()

assertThat(actualRuleInvocationOrder)
Expand All @@ -152,7 +159,7 @@ class OrderedRuleSetTest {
visitorModifiers: Set<Rule.VisitorModifier> = emptySet(),
onVisit: (Rule) -> Unit = { }
): Rule = object : Rule(id.qualifiedWithRuleSetId(), visitorModifiers) {
override fun beforeVisitChildNodes(
override fun visit(
node: ASTNode,
autoCorrect: Boolean,
emit: EmitType
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -792,9 +792,9 @@ private class PrettyPrintingVisitor(private val elementType: IElementType,
private val maxLevel: Int,
private val expected: String
) : Rule("print-ast") {
override fun beforeVisitChildNodes(node: ASTNode,
autoCorrect: Boolean,
emit: EmitType
override fun visit(node: ASTNode,
autoCorrect: Boolean,
emit: EmitType
) {
if (node.elementType == elementType) {
Assertions.assertEquals(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,10 @@ internal fun applyToCode(code: String,
text = code,
ruleSets = listOf(
RuleSet("test", object : Rule("astnode-utils-test") {
override fun beforeVisitChildNodes(node: ASTNode,
autoCorrect: Boolean,
emit: EmitType
override fun visit(
node: ASTNode,
autoCorrect: Boolean,
emit: EmitType
) {
applyToNode(node, counter)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ class DiktatSaveSmokeTest : DiktatSmokeTestBase() {
private const val BASE_DIRECTORY = "src/test/resources/test/smoke"
private const val BUILD_DIRECTORY = "target"
private const val FAT_JAR_GLOB = "diktat-*.jar"
private const val KTLINT_VERSION = "0.47.1"
private const val KTLINT_VERSION = "0.46.1"
private const val SAVE_VERSION: String = "0.3.4"
private val baseDirectoryPath = Path(BASE_DIRECTORY).absolute()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -238,8 +238,8 @@ abstract class DiktatSmokeTestBase {
/*
* This 2nd `MISSING_KDOC_ON_FUNCTION` is a duplicate caused by
* https://github.com/saveourtool/diktat/issues/1538.
* LintError(6, 5, "$DIKTAT_RULE_SET_ID:${KdocMethods.NAME_ID}", "${MISSING_KDOC_ON_FUNCTION.warnText()} foo", false),
*/
LintError(6, 5, "$DIKTAT_RULE_SET_ID:${KdocMethods.NAME_ID}", "${MISSING_KDOC_ON_FUNCTION.warnText()} foo", false),
LintError(9, 3, "$DIKTAT_RULE_SET_ID:${EmptyBlock.NAME_ID}", EMPTY_BLOCK_STRUCTURE_ERROR.warnText() +
" empty blocks are forbidden unless it is function with override keyword", false),
LintError(12, 10, "$DIKTAT_RULE_SET_ID:${KdocFormatting.NAME_ID}", "${KDOC_NO_EMPTY_TAGS.warnText()} @return", false),
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
<kotlin.compiler.incremental>true</kotlin.compiler.incremental>
<kotlin.code.style>official</kotlin.code.style>
<kotlinx.serialization.version>1.4.1</kotlinx.serialization.version>
<ktlint.version>0.47.1</ktlint.version>
<ktlint.version>0.46.1</ktlint.version>
<junit.version>5.9.1</junit.version>
<junit.platform.version>1.9.1</junit.platform.version>
<surefire.junit5.tree-reporter.version>1.1.0</surefire.junit5.tree-reporter.version>
Expand Down