Skip to content

Commit

Permalink
### What's done: WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
nulls committed Jun 21, 2022
1 parent 3ed2677 commit 2a16f9d
Show file tree
Hide file tree
Showing 16 changed files with 48 additions and 48 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 Expand Up @@ -31,6 +31,7 @@ import java.nio.file.Files
import java.nio.file.Paths
import javax.inject.Inject


/**
* A base diktat task for gradle <6.8, which wraps [JavaExec].
*
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
Expand Up @@ -7,7 +7,6 @@ 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.loadBaseline
import com.pinterest.ktlint.reporter.baseline.BaselineReporter
Expand Down Expand Up @@ -94,7 +93,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 @@ -228,7 +226,6 @@ abstract class DiktatBaseMojo : AbstractMojo() {
}
}

@OptIn(FeatureInAlphaState::class)
private fun checkFile(file: File,
lintErrors: MutableList<LintError>,
baselineErrors: List<LintError>,
Expand All @@ -237,10 +234,9 @@ abstract class DiktatBaseMojo : AbstractMojo() {
val text = file.readText()
val params =
KtLint.ExperimentalParams(
fileName = file.relativeTo(mavenProject.basedir).path,
fileName = file.path,
text = text,
ruleSets = ruleSets,
userData = mapOf("file_path" to file.path),
script = file.extension.equals("kts", ignoreCase = true),
cb = { lintError, isCorrected ->
if (baselineErrors.none {
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.EmitType
Expand Down Expand Up @@ -90,13 +93,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 @@ -723,6 +723,19 @@ fun ASTNode.extractLineOfText(): String {
return text.joinToString(separator = "").trim()
}

/**
* @param pred a predicate
* @return filtered sequence
*/
private fun <T> Sequence<T>.takeWhileInclusive(pred: (T) -> Boolean): Sequence<T> {
var shouldContinue = true
return takeWhile {
val result = shouldContinue
shouldContinue = pred(it)
result
}
}

/**
* Checks node has `@Test` annotation
*/
Expand Down Expand Up @@ -783,6 +796,7 @@ fun ASTNode.findAllNodesWithConditionOnLine(
* @return name of the file [this] node belongs to
*/
fun ASTNode.getFilePath(): String = getRootNode().also {
// FIXME: file_path is removed
require(it.elementType == FILE) { "Root node type is not FILE, but file_path 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

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ open class LintTestBase(private val ruleSupplier: (rulesConfigList: List<RulesCo
ruleSets = listOf(DiktatRuleSetProvider4Test(ruleSupplier,
rulesConfigList ?: this.rulesConfigList).get()),
cb = { lintError, _ -> lintErrors += lintError },
userData = mapOf("file_path" to actualFileName)
)
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import com.pinterest.ktlint.core.LintError
import com.pinterest.ktlint.core.Rule
import com.pinterest.ktlint.core.RuleSet
import com.pinterest.ktlint.core.RuleSetProvider
import com.pinterest.ktlint.core.VisitorProvider
import com.pinterest.ktlint.core.api.FeatureInAlphaState
import org.assertj.core.api.Assertions
import org.assertj.core.api.SoftAssertions
Expand Down Expand Up @@ -81,7 +80,6 @@ internal fun List<LintError>.assertEquals(vararg expectedLintErrors: LintError)
* @param cb callback to be called on unhandled [LintError]s
* @return formatted code
*/
@OptIn(FeatureInAlphaState::class)
@Suppress("LAMBDA_IS_NOT_LAST_PARAMETER")
internal fun format(ruleSetProviderRef: (rulesConfigList: List<RulesConfig>?) -> RuleSetProvider,
@Language("kotlin") text: String,
Expand All @@ -97,13 +95,7 @@ internal fun format(ruleSetProviderRef: (rulesConfigList: List<RulesConfig>?) ->
fileName = fileName.removeSuffix("_copy"),
script = fileName.removeSuffix("_copy").endsWith("kts"),
cb = cb,
userData = mapOf("file_path" to fileName.removeSuffix("_copy")),
),
VisitorProvider(
ruleSets = ruleSets,
debug = true,
// setting this to `true` breaks smoke test
isUnitTestContext = false,
)
)
}
Expand Down
7 changes: 7 additions & 0 deletions examples/gradle-kotlin-dsl/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
plugins {
id("org.cqfn.diktat.diktat-gradle-plugin") version "1.1.0"
kotlin("jvm") version "1.7.0"
}

repositories {
mavenCentral()
}

tasks.withType<KotlinCompile> {
kotlinOptions {
freeCompilerArgs += listOf("-Xuse-k2")
}
}

diktat {
inputs { include("src/**/*.kt") }
}
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
<diktat-check.version>1.1.0</diktat-check.version>
<kotlinpoet.version>1.12.0</kotlinpoet.version>
<detekt.version>1.20.0</detekt.version>
<dokka.version>1.6.21</dokka.version>
<dokka.version>1.7.0</dokka.version>
<jacoco.version>0.8.8</jacoco.version>
<maven-plugin-tools.version>3.6.4</maven-plugin-tools.version>
<jbool.version>1.23</jbool.version>
Expand Down

0 comments on commit 2a16f9d

Please sign in to comment.