Skip to content

Commit

Permalink
Merge branch 'master' into feature/boolean_expression_order
Browse files Browse the repository at this point in the history
  • Loading branch information
nulls authored May 25, 2022
2 parents f753cd9 + 7f116ac commit ed49717
Show file tree
Hide file tree
Showing 12 changed files with 78 additions and 40 deletions.
2 changes: 0 additions & 2 deletions diktat-common/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,10 @@
<dependency>
<groupId>org.jetbrains.kotlinx</groupId>
<artifactId>kotlinx-serialization-json-jvm</artifactId>
<version>${kotlinx.serialization.version}</version>
</dependency>
<dependency>
<groupId>com.charleskorn.kaml</groupId>
<artifactId>kaml-jvm</artifactId>
<version>0.43.0</version>
</dependency>
<dependency>
<groupId>commons-cli</groupId>
Expand Down
5 changes: 0 additions & 5 deletions diktat-maven-plugin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -54,27 +54,22 @@
<dependency>
<groupId>com.pinterest.ktlint</groupId>
<artifactId>ktlint-reporter-plain</artifactId>
<version>${ktlint.version}</version>
</dependency>
<dependency>
<groupId>com.pinterest.ktlint</groupId>
<artifactId>ktlint-reporter-sarif</artifactId>
<version>${ktlint.version}</version>
</dependency>
<dependency>
<groupId>com.pinterest.ktlint</groupId>
<artifactId>ktlint-reporter-json</artifactId>
<version>${ktlint.version}</version>
</dependency>
<dependency>
<groupId>com.pinterest.ktlint</groupId>
<artifactId>ktlint-reporter-html</artifactId>
<version>${ktlint.version}</version>
</dependency>
<dependency>
<groupId>com.pinterest.ktlint</groupId>
<artifactId>ktlint-reporter-baseline</artifactId>
<version>${ktlint.version}</version>
</dependency>
<dependency>
<groupId>org.junit.vintage</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ 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 @@ -91,9 +92,10 @@ abstract class DiktatBaseMojo : AbstractMojo() {
private lateinit var mavenSession: MavenSession

/**
* @param params instance of [KtLint.Params] used in analysis
* @param params instance of [KtLint.ExperimentalParams] used in analysis
*/
abstract fun runAction(params: KtLint.Params)
@OptIn(FeatureInAlphaState::class)
abstract fun runAction(params: KtLint.ExperimentalParams)

/**
* Perform code check using diktat ruleset
Expand Down Expand Up @@ -226,14 +228,15 @@ abstract class DiktatBaseMojo : AbstractMojo() {
}
}

@OptIn(FeatureInAlphaState::class)
private fun checkFile(file: File,
lintErrors: MutableList<LintError>,
baselineErrors: List<LintError>,
ruleSets: Iterable<RuleSet>
) {
val text = file.readText()
val params =
KtLint.Params(
KtLint.ExperimentalParams(
fileName = file.relativeTo(mavenProject.basedir).path,
text = text,
ruleSets = ruleSets,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package org.cqfn.diktat.plugin.maven

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 @@ -16,9 +17,10 @@ import java.io.File
@Suppress("unused")
class DiktatCheckMojo : DiktatBaseMojo() {
/**
* @param params instance of [KtLint.Params] used in analysis
* @param params instance of [KtLint.ExperimentalParams] used in analysis
*/
override fun runAction(params: KtLint.Params) {
@OptIn(FeatureInAlphaState::class)
override fun runAction(params: KtLint.ExperimentalParams) {
KtLint.lint(params)
}
}
Expand All @@ -33,7 +35,8 @@ class DiktatFixMojo : DiktatBaseMojo() {
/**
* @param params instance of [KtLint.Params] used in analysis
*/
override fun runAction(params: KtLint.Params) {
@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"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,13 @@ import com.squareup.kotlinpoet.KModifier
import com.squareup.kotlinpoet.PropertySpec
import com.squareup.kotlinpoet.TypeSpec

import java.io.File
import java.nio.file.Files
import java.nio.file.Paths

import kotlin.io.path.ExperimentalPathApi
import kotlin.io.path.createTempFile
import kotlin.io.path.name
import kotlin.io.path.readLines
import kotlin.io.path.writeLines

/**
* The comment that will be added to the generated sources file.
Expand Down Expand Up @@ -56,20 +59,18 @@ private fun generateWarningNames() {
.addComment(autoGenerationComment)
.build()

kotlinFile.writeTo(File("diktat-rules/src/main/kotlin")) // fixme: need to add it to pom
kotlinFile.writeTo(Paths.get("diktat-rules/src/main/kotlin")) // fixme: need to add it to pom
}

@OptIn(ExperimentalPathApi::class)
private fun validateYear() {
val files = File("diktat-rules/src/test/resources/test/paragraph2/header")
files
.listFiles()
?.filterNot { it.name.contains("CopyrightDifferentYearTest.kt") }
?.forEach { file ->
val tempFile = createTempFile().toFile()
tempFile.printWriter().use { writer ->
file.forEachLine { line ->
writer.println(when {
val folder = Paths.get("diktat-rules/src/test/resources/test/paragraph2/header")
Files.list(folder)
.filter { !it.name.contains("CopyrightDifferentYearTest.kt") }
.forEach { file ->
val tempFile = createTempFile()
tempFile.writeLines(file.readLines()
.map { line ->
when {
line.contains(hyphenRegex) -> line.replace(hyphenRegex) {
val years = it.value.split("-")
"${years[0]}-$curYear"
Expand All @@ -79,10 +80,9 @@ private fun validateYear() {
"${copyrightYears[0]}-$curYear"
}
else -> line
})
}
}
file.delete()
tempFile.renameTo(file)
}
})
Files.delete(file)
Files.move(tempFile, file)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import org.cqfn.diktat.util.applyToCode
import com.pinterest.ktlint.core.KtLint
import com.pinterest.ktlint.core.Rule
import com.pinterest.ktlint.core.RuleSet
import com.pinterest.ktlint.core.api.FeatureInAlphaState
import com.pinterest.ktlint.core.ast.ElementType
import com.pinterest.ktlint.core.ast.ElementType.BLOCK
import com.pinterest.ktlint.core.ast.ElementType.CLASS
Expand Down Expand Up @@ -806,6 +807,7 @@ private class PrettyPrintingVisitor(private val elementType: IElementType,
}

companion object {
@OptIn(FeatureInAlphaState::class)
fun assertStringRepr(
elementType: IElementType,
code: String,
Expand All @@ -814,7 +816,7 @@ private class PrettyPrintingVisitor(private val elementType: IElementType,
expected: String
) {
KtLint.lint(
KtLint.Params(
KtLint.ExperimentalParams(
text = code,
ruleSets = listOf(RuleSet("test", PrettyPrintingVisitor(elementType, level, maxLevel, expected))),
cb = { _, _ -> }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import org.cqfn.diktat.common.config.rules.RulesConfig
import com.pinterest.ktlint.core.KtLint
import com.pinterest.ktlint.core.LintError
import com.pinterest.ktlint.core.Rule
import com.pinterest.ktlint.core.api.FeatureInAlphaState

/**
* Base class for testing rules without fixing code.
Expand All @@ -21,6 +22,7 @@ open class LintTestBase(private val ruleSupplier: (rulesConfigList: List<RulesCo
* @param rulesConfigList optional override for `this.rulesConfigList`
* @param fileName optional override for file name
*/
@OptIn(FeatureInAlphaState::class)
fun lintMethod(code: String,
vararg lintErrors: LintError,
rulesConfigList: List<RulesConfig>? = null,
Expand All @@ -29,7 +31,7 @@ open class LintTestBase(private val ruleSupplier: (rulesConfigList: List<RulesCo
val actualFileName = fileName ?: TEST_FILE_NAME
val res: MutableList<LintError> = mutableListOf()
KtLint.lint(
KtLint.Params(
KtLint.ExperimentalParams(
fileName = actualFileName,
script = actualFileName.endsWith("kts"),
text = code,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,8 @@ 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"))
userData = mapOf("file_path" to fileName.removeSuffix("_copy")),
),
ruleSets = ruleSets,
VisitorProvider(
ruleSets = ruleSets,
debug = true,
Expand All @@ -116,14 +115,15 @@ internal fun format(ruleSetProviderRef: (rulesConfigList: List<RulesConfig>?) ->
* @param expectedAsserts Number of expected times of assert invocation
* @param applyToNode Function to be called on each AST node, should increment counter if assert is called
*/
@OptIn(FeatureInAlphaState::class)
@Suppress("TYPE_ALIAS")
internal fun applyToCode(code: String,
expectedAsserts: Int,
applyToNode: (node: ASTNode, counter: AtomicInteger) -> Unit
) {
val counter = AtomicInteger(0)
KtLint.lint(
KtLint.Params(
KtLint.ExperimentalParams(
text = code,
ruleSets = listOf(
RuleSet("test", object : Rule("astnode-utils-test") {
Expand Down
2 changes: 1 addition & 1 deletion info/guide/diktat-coding-convention.md
Original file line number Diff line number Diff line change
Expand Up @@ -1065,7 +1065,7 @@ if (condition)
fun visit(
node: ASTNode,
autoCorrect: Boolean,
params: KtLint.Params,
params: KtLint.ExperimentalParams,
emit: (offset: Int, errorMessage: String, canBeAutoCorrected: Boolean) -> Unit
) {
}
Expand Down
2 changes: 1 addition & 1 deletion info/guide/guide-chapter-3.md
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ if (condition)
fun visit(
node: ASTNode,
autoCorrect: Boolean,
params: KtLint.Params,
params: KtLint.ExperimentalParams,
emit: (offset: Int, errorMessage: String, canBeAutoCorrected: Boolean) -> Unit
) {
}
Expand Down
37 changes: 36 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
<kotlin.version>1.6.21</kotlin.version>
<kotlin.compiler.incremental>true</kotlin.compiler.incremental>
<kotlinx.serialization.version>1.3.2</kotlinx.serialization.version>
<ktlint.version>0.44.0</ktlint.version>
<ktlint.version>0.45.2</ktlint.version>
<junit.version>5.8.2</junit.version>
<guava.version>31.0.1-jre</guava.version>
<slf4j.version>1.7.36</slf4j.version>
Expand Down Expand Up @@ -104,6 +104,11 @@
<artifactId>kotlin-compiler-embeddable</artifactId>
<version>${kotlin.version}</version>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlinx</groupId>
<artifactId>kotlinx-serialization-json-jvm</artifactId>
<version>${kotlinx.serialization.version}</version>
</dependency>
<dependency>
<groupId>com.pinterest.ktlint</groupId>
<artifactId>ktlint-core</artifactId>
Expand All @@ -115,6 +120,36 @@
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.pinterest.ktlint</groupId>
<artifactId>ktlint-reporter-plain</artifactId>
<version>${ktlint.version}</version>
</dependency>
<dependency>
<groupId>com.pinterest.ktlint</groupId>
<artifactId>ktlint-reporter-sarif</artifactId>
<version>${ktlint.version}</version>
</dependency>
<dependency>
<groupId>com.pinterest.ktlint</groupId>
<artifactId>ktlint-reporter-json</artifactId>
<version>${ktlint.version}</version>
</dependency>
<dependency>
<groupId>com.pinterest.ktlint</groupId>
<artifactId>ktlint-reporter-html</artifactId>
<version>${ktlint.version}</version>
</dependency>
<dependency>
<groupId>com.pinterest.ktlint</groupId>
<artifactId>ktlint-reporter-baseline</artifactId>
<version>${ktlint.version}</version>
</dependency>
<dependency>
<groupId>com.charleskorn.kaml</groupId>
<artifactId>kaml-jvm</artifactId>
<version>0.43.0</version>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion wp/sections/appendix.tex
Original file line number Diff line number Diff line change
Expand Up @@ -1979,7 +1979,7 @@ \subsection*{\textbf{3.3 Indentation}}
fun visit(
node: ASTNode,
autoCorrect: Boolean,
params: KtLint.Params,
params: KtLint.ExperimentalParams,
emit: (offset: Int, errorMessage: String, canBeAutoCorrected: Boolean) -> Unit
) {
}
Expand Down

0 comments on commit ed49717

Please sign in to comment.