Skip to content

Commit

Permalink
Merge branch 'master' into feature/bool-expressions-simplifier(#809)
Browse files Browse the repository at this point in the history
  • Loading branch information
aktsay6 authored May 11, 2021
2 parents f2633f7 + a2f4056 commit abe20fb
Show file tree
Hide file tree
Showing 72 changed files with 580 additions and 228 deletions.
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,11 @@ Main features of diktat are the following:
# another option is "brew install ktlint"
```

2. Load diKTat manually: [here](https://github.com/cqfn/diKTat/releases/download/v0.5.1/diktat.jar)
2. Load diKTat manually: [here](https://github.com/cqfn/diKTat/releases/download/v0.5.2/diktat.jar)

**OR** use curl:
```bash
$ curl -sSLO https://github.com/cqfn/diKTat/releases/download/v0.5.1/diktat-0.5.1.jar
$ curl -sSLO https://github.com/cqfn/diKTat/releases/download/v0.5.2/diktat-0.5.2.jar
```

3. Finally, run KTlint (with diKTat injected) to check your `*.kt` files in `dir/your/dir`:
Expand Down Expand Up @@ -110,7 +110,7 @@ This plugin is available since version 0.1.5. You can see how the plugin is conf
Add this plugin to your `build.gradle.kts`:
```kotlin
plugins {
id("org.cqfn.diktat.diktat-gradle-plugin") version "0.5.1"
id("org.cqfn.diktat.diktat-gradle-plugin") version "0.5.2"
}
```

Expand All @@ -121,7 +121,7 @@ buildscript {
mavenCentral()
}
dependencies {
classpath("org.cqfn.diktat:diktat-gradle-plugin:0.5.1")
classpath("org.cqfn.diktat:diktat-gradle-plugin:0.5.2")
}
}
Expand Down Expand Up @@ -194,7 +194,7 @@ spotless {
```kotlin
spotless {
kotlin {
diktat("0.5.1").configFile("full/path/to/diktat-analysis.yml")
diktat("0.5.2").configFile("full/path/to/diktat-analysis.yml")
}
}
```
Expand Down Expand Up @@ -225,7 +225,7 @@ Diktat can be run via spotless-maven-plugin since version 2.8.0

```xml
<diktat>
<version>0.5.1</version> <!-- optional -->
<version>0.5.2</version> <!-- optional -->
<configFile>full/path/to/diktat-analysis.yml</configFile> <!-- optional, configuration file path -->
</diktat>
```
Expand Down
2 changes: 1 addition & 1 deletion diktat-common/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<parent>
<groupId>org.cqfn.diktat</groupId>
<artifactId>diktat-parent</artifactId>
<version>0.5.2-SNAPSHOT</version>
<version>0.5.3-SNAPSHOT</version>
</parent>

<dependencies>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ abstract class JsonResourceConfigReader<T> {
protected abstract fun parseResource(fileStream: BufferedReader): T

companion object {
/**
* A [Logger] that can be used
*/
val log: Logger = LoggerFactory.getLogger(JsonResourceConfigReader::class.java)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ import java.util.concurrent.atomic.AtomicInteger
import kotlinx.serialization.Serializable
import kotlinx.serialization.decodeFromString

/**
* Name of common configuration
*/
const val DIKTAT_COMMON = "DIKTAT_COMMON"

/**
Expand Down Expand Up @@ -49,7 +52,6 @@ data class RulesConfig(
* @property config a map of strings with configuration options for a particular rule
*/
open class RuleConfiguration(protected val config: Map<String, String>)
object EmptyConfiguration : RuleConfiguration(emptyMap())

/**
* class returns the list of configurations that we have read from a yml: diktat-analysis.yml
Expand Down Expand Up @@ -87,6 +89,9 @@ open class RulesConfigReader(override val classLoader: ClassLoader) : JsonResour
}

companion object {
/**
* A [Logger] that can be used
*/
val log: Logger = LoggerFactory.getLogger(RulesConfigReader::class.java)
}
}
Expand Down
14 changes: 9 additions & 5 deletions diktat-gradle-plugin/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import org.gradle.nativeplatform.platform.internal.DefaultNativePlatform.getCurr

plugins {
`java-gradle-plugin`
kotlin("jvm") version "1.4.32"
kotlin("jvm") version "1.3.72"
jacoco
id("pl.droidsonroids.jacoco.testkit") version "1.0.7"
id("org.gradle.test-retry") version "1.2.1"
Expand All @@ -26,10 +26,13 @@ val jacocoVersion = project.properties.getOrDefault("jacocoVersion", "0.8.6") as
dependencies {
implementation(kotlin("gradle-plugin-api"))

implementation("com.pinterest.ktlint:ktlint-core:$ktlintVersion") {
exclude("com.pinterest.ktlint", "ktlint-ruleset-standard")
implementation("org.cqfn.diktat:diktat-rules:$diktatVersion") {
exclude("org.jetbrains.kotlin", "kotlin-compiler-embeddable")
exclude("org.jetbrains.kotlin", "kotlin-stdlib-jdk8")
exclude("org.jetbrains.kotlin", "kotlin-stdlib-jdk7")
exclude("org.jetbrains.kotlin", "kotlin-stdlib")
exclude("org.jetbrains.kotlin", "kotlin-stdlib-common")
}
implementation("org.cqfn.diktat:diktat-rules:$diktatVersion")

testImplementation("org.junit.jupiter:junit-jupiter-api:$junitVersion")
testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:$junitVersion")
Expand Down Expand Up @@ -62,7 +65,8 @@ tasks.withType<KotlinCompile> {
apiVersion = "1.3"
jvmTarget = "1.8"
useIR = true
}
allWarningsAsErrors = true
}

dependsOn.add(generateVersionsFile)
}
Expand Down
2 changes: 1 addition & 1 deletion diktat-gradle-plugin/gradle-plugin-marker/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<parent>
<artifactId>diktat-gradle-plugin</artifactId>
<groupId>org.cqfn.diktat</groupId>
<version>0.5.2-SNAPSHOT</version>
<version>0.5.3-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down
2 changes: 1 addition & 1 deletion diktat-gradle-plugin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<artifactId>diktat-parent</artifactId>
<groupId>org.cqfn.diktat</groupId>
<version>0.5.2-SNAPSHOT</version>
<version>0.5.3-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,24 @@ class DiktatGradlePlugin : Plugin<Project> {
}

companion object {
/**
* Task to check diKTat
*/
const val DIKTAT_CHECK_TASK = "diktatCheck"

/**
* DiKTat configuration
*/
const val DIKTAT_CONFIGURATION = "diktat"

/**
* DiKTat extension
*/
const val DIKTAT_EXTENSION = "diktat"

/**
* Task to run diKTat with fix
*/
const val DIKTAT_FIX_TASK = "diktatFix"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,9 @@ open class DiktatJavaExecTaskBase @Inject constructor(
project.logger.debug("Setting JavaExec args to $args")
}

/**
* Function to execute diKTat
*/
@TaskAction
override fun exec() {
if (shouldRun) {
Expand Down
2 changes: 1 addition & 1 deletion diktat-maven-plugin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<artifactId>diktat-parent</artifactId>
<groupId>org.cqfn.diktat</groupId>
<version>0.5.2-SNAPSHOT</version>
<version>0.5.3-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down
7 changes: 6 additions & 1 deletion diktat-rules/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<parent>
<groupId>org.cqfn.diktat</groupId>
<artifactId>diktat-parent</artifactId>
<version>0.5.2-SNAPSHOT</version>
<version>0.5.3-SNAPSHOT</version>
</parent>

<properties>
Expand Down Expand Up @@ -124,6 +124,11 @@
</configuration>
</execution>
</executions>
<configuration>
<args>
<arg>-Xinline-classes</arg>
</args>
</configuration>
</plugin>

<plugin>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ typealias ListOfPairs = MutableList<Pair<ASTNode, String>>
"WRONG_NEWLINES"
)
enum class Warnings(
val canBeAutoCorrected: Boolean,
private val canBeAutoCorrected: Boolean,
val ruleId: String,
private val warn: String) : Rule {
// ======== dummy test warning ======
Expand Down Expand Up @@ -207,7 +207,9 @@ enum class Warnings(
canBeAutoCorrected: Boolean = this.canBeAutoCorrected,
autoFix: () -> Unit) {
warn(configRules, emit, canBeAutoCorrected, freeText, offset, node)
fix(configRules, isFixMode, node, autoFix)
if (canBeAutoCorrected) {
fix(configRules, isFixMode, node, autoFix)
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class DummyWarning(configRules: List<RulesConfig>) : DiktatRule(
Warnings.FILE_NAME_MATCH_CLASS
)
) {
@Suppress("UNUSED")
private lateinit var filePath: String

@Suppress("EmptyFunctionBlock")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ private fun validateYear() {
val files = File("diktat-rules/src/test/resources/test/paragraph2/header")
files
.listFiles()
.filterNot { it.name.contains("CopyrightDifferentYearTest.kt") }
.forEach { file ->
?.filterNot { it.name.contains("CopyrightDifferentYearTest.kt") }
?.forEach { file ->
val tempFile = createTempFile().toFile()
tempFile.printWriter().use { writer ->
file.forEachLine { line ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ private typealias DiktatConfigRule = org.cqfn.diktat.common.config.rules.Rule
@Suppress("TooGenericExceptionCaught")
abstract class DiktatRule(id: String,
val configRules: List<RulesConfig>,
val inspections: List<DiktatConfigRule>) : Rule(id) {
private val inspections: List<DiktatConfigRule>) : Rule(id) {
/**
* Default value is false
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ import com.pinterest.ktlint.core.ast.ElementType.DESTRUCTURING_DECLARATION
import com.pinterest.ktlint.core.ast.ElementType.DESTRUCTURING_DECLARATION_ENTRY
import com.pinterest.ktlint.core.ast.ElementType.FILE
import com.pinterest.ktlint.core.ast.ElementType.FUNCTION_TYPE
import com.pinterest.ktlint.core.ast.ElementType.PROPERTY
import com.pinterest.ktlint.core.ast.ElementType.REFERENCE_EXPRESSION
import com.pinterest.ktlint.core.ast.ElementType.TYPE_PARAMETER
import com.pinterest.ktlint.core.ast.ElementType.TYPE_REFERENCE
Expand Down Expand Up @@ -163,7 +162,7 @@ class IdentifierNaming(configRules: List<RulesConfig>) : DiktatRule(
var namesOfVariables = extractVariableIdentifiers(node)
// Only local private properties will be autofix in order not to break code if there are usages in other files.
// Destructuring declarations are only allowed for local variables/values, so we don't need to calculate `isFix` for every node in `namesOfVariables`
val isFix = isFixMode && if (node.elementType == PROPERTY) (node.psi as KtProperty).run { isLocal || isPrivate() } else true
val isFix = isFixMode && if (node.elementType == ElementType.PROPERTY) (node.psi as KtProperty).run { isLocal || isPrivate() } else true
namesOfVariables
.forEach { variableName ->
// variable should not contain only one letter in it's name. This is a bad example: b512
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ class CommentsRule(configRules: List<RulesConfig>) : DiktatRule(
private val importOrPackageRegex = """^(import|package)?\s+([a-zA-Z.])+;*$""".toRegex()
private val functionRegex = """^(public|private|protected)*\s*(override|abstract|actual|expect)*\s?fun\s+\w+(\(.*\))?(\s*:\s*\w+)?\s*[{=]${'$'}""".toRegex()
private val rightBraceRegex = """^\s*}$""".toRegex()
private val requirePartOfCode = """val |var |=|(\{((.|\n)*)\})""".toRegex()
private val requirePartOfCode = """val |var |=|(\{((.|\n)*)})""".toRegex()
private val codeFileStartCases = listOf(classRegex, importOrPackageRegex, functionRegex, rightBraceRegex)
private val eolCommentStart = """// \S""".toRegex()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,6 @@ class CommentsFormatting(configRules: List<RulesConfig>) : DiktatRule(
IF_ELSE_COMMENTS, WRONG_NEWLINES_AROUND_KDOC)) {
/**
* @param node
* @param autoCorrect
* @param emit
*/
override fun logic(node: ASTNode) {
val configuration = CommentsFormattingConfiguration(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,6 @@ class KdocComments(configRules: List<RulesConfig>) : DiktatRule(
KDOC_NO_CONSTRUCTOR_PROPERTY_WITH_COMMENT, MISSING_KDOC_CLASS_ELEMENTS, MISSING_KDOC_TOP_LEVEL)) {
/**
* @param node
* @param autoCorrect
* @param emit
*/
override fun logic(node: ASTNode) {
val config = configRules.getCommonConfiguration()
Expand All @@ -76,13 +74,13 @@ class KdocComments(configRules: List<RulesConfig>) : DiktatRule(
?.findChildByType(KDOC) ?: return
val propertiesInKdoc = kdocBeforeClass
.kDocTags()
?.filter { it.knownTag == KDocKnownTag.PROPERTY }
.filter { it.knownTag == KDocKnownTag.PROPERTY }
val propertyNames = (node.psi as KtParameterList)
.parameters
.mapNotNull { it.nameIdentifier?.text }
propertiesInKdoc
?.filterNot { it.getSubjectName() == null || it.getSubjectName() in propertyNames }
?.forEach { KDOC_EXTRA_PROPERTY.warn(configRules, emitWarn, isFixMode, it.text, it.node.startOffset, node) }
.filterNot { it.getSubjectName() == null || it.getSubjectName() in propertyNames }
.forEach { KDOC_EXTRA_PROPERTY.warn(configRules, emitWarn, isFixMode, it.text, it.node.startOffset, node) }
}

@Suppress("UnsafeCallOnNullableType", "ComplexMethod")
Expand Down Expand Up @@ -126,7 +124,7 @@ class KdocComments(configRules: List<RulesConfig>) : DiktatRule(
private fun checkBasicKdocBeforeClass(node: ASTNode, kdocBeforeClass: ASTNode) {
val propertyInClassKdoc = kdocBeforeClass
.kDocTags()
?.firstOrNull { it.knownTag == KDocKnownTag.PROPERTY && it.getSubjectName() == node.findChildByType(IDENTIFIER)!!.text }
.firstOrNull { it.knownTag == KDocKnownTag.PROPERTY && it.getSubjectName() == node.findChildByType(IDENTIFIER)!!.text }
if (propertyInClassKdoc == null && node.getFirstChildWithType(MODIFIER_LIST).isAccessibleOutside()) {
KDOC_NO_CONSTRUCTOR_PROPERTY.warnAndFix(configRules, emitWarn, isFixMode,
"add <${node.findChildByType(IDENTIFIER)!!.text}> to KDoc", node.startOffset, node) {
Expand All @@ -142,12 +140,12 @@ class KdocComments(configRules: List<RulesConfig>) : DiktatRule(
prevComment: ASTNode) {
val propertyInClassKdoc = kdocBeforeClass
.kDocTags()
?.firstOrNull { it.knownTag == KDocKnownTag.PROPERTY && it.getSubjectName() == node.findChildByType(IDENTIFIER)!!.text }
.firstOrNull { it.knownTag == KDocKnownTag.PROPERTY && it.getSubjectName() == node.findChildByType(IDENTIFIER)!!.text }
?.node
val propertyInLocalKdoc = if (prevComment.elementType == KDOC) {
prevComment
.kDocTags()
?.firstOrNull { it.knownTag == KDocKnownTag.PROPERTY && it.getSubjectName() == node.findChildByType(IDENTIFIER)!!.text }
.firstOrNull { it.knownTag == KDocKnownTag.PROPERTY && it.getSubjectName() == node.findChildByType(IDENTIFIER)!!.text }
?.node
} else {
null
Expand Down
Loading

0 comments on commit abe20fb

Please sign in to comment.