Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/feature/kotlin-1.4' into feature…
Browse files Browse the repository at this point in the history
…/kotlinx-serialization

# Conflicts:
#	diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/identifiers/LocalVariablesRule.kt
#	diktat-test-framework/pom.xml
  • Loading branch information
petertrr committed Oct 13, 2020
2 parents b832d09 + f24fea1 commit 91c1b5b
Show file tree
Hide file tree
Showing 21 changed files with 222 additions and 114 deletions.
5 changes: 2 additions & 3 deletions .github/workflows/diktat.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,5 @@ jobs:
key: ${{ runner.os }}-maven-diktat-${{ hashFiles('**/pom.xml') }}
restore-keys: |
${{ runner.os }}-maven-diktat-
- name: Run diktat itself via ktlint
# fixme: change to diktat:check when diktat plugin is released
run: mvn -B antrun:run@diktat
- name: Run diktat via maven plugin
run: mvn -B diktat:check@diktat
5 changes: 2 additions & 3 deletions .github/workflows/functional_tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ on:
branches: [ master ]

env:
DIKTAT_VERSION: 0.1.1
DIKTAT_VERSION: 0.1.2
KTLINT_VERSION: 0.39.0

jobs:
Expand Down Expand Up @@ -43,8 +43,7 @@ jobs:
- name: Run diKTat from maven
continue-on-error: true
# fixme: change to diktat:check when diktat plugin is released
run: mvn -B antrun:run@functional-test &> out
run: mvn -B diktat:check@functional-test &> out

- name: Check maven
run: |
Expand Down
11 changes: 5 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,11 @@ The full list of available supported rules and inspections is [here](info/availa
# another option is "brew install ktlint"
```

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

**OR** use curl:
```bash
$ curl -sSLO https://github.com/cqfn/diKTat/releases/download/v0.1.1/diktat-0.1.1.jar
$ curl -sSLO https://github.com/cqfn/diKTat/releases/download/v0.1.2/diktat-0.1.2.jar
```

3. Finally, run KTlint (with diKTat injected) to check your `*.kt` files in `dir/your/dir`:
Expand Down Expand Up @@ -100,7 +100,7 @@ Add this plugin to your pom.xml:
<dependency>
<groupId>org.cqfn.diktat</groupId>
<artifactId>diktat-rules</artifactId>
<version>0.1.1</version> <!-- replace it with diktat latest version -->
<version>0.1.2</version> <!-- replace it with diktat latest version -->
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
Expand All @@ -125,8 +125,7 @@ To run diktat to check/fix code style - run `$ mvn antrun:run@diktat`.
### Use the new diktat-maven-plugin

You can see how it is configured in our project for self-checks: [pom.xml](pom.xml).
This plugin should be available since version 0.1.2. It requires less configuration but may contain bugs.
If you use it and encounter any problems, feel free to open issues on [github](https://github.com/cqfn/diktat/issues).
This plugin is available since version 0.1.2. If you use it and encounter any problems, feel free to open issues on [github](https://github.com/cqfn/diktat/issues).

Add this plugin to your pom.xml:
<details>
Expand Down Expand Up @@ -179,7 +178,7 @@ dependencies {
}
// diktat ruleset
ktlint("org.cqfn.diktat:diktat-rules:0.1.1")
ktlint("org.cqfn.diktat:diktat-rules:0.1.2")
}
val outputDir = "${project.buildDir}/reports/diktat/"
Expand Down
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ dependencies {
}

// diktat ruleset
ktlint("org.cqfn.diktat:diktat-rules:0.1.1")
ktlint("org.cqfn.diktat:diktat-rules:0.1.2")
}

val outputDir = "${project.buildDir}/reports/diktat/"
Expand Down
6 changes: 5 additions & 1 deletion diktat-analysis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -308,4 +308,8 @@
- name: NESTED_BLOCK
enabled: true
configuration:
maxNestedBlockQuantity: '4'
maxNestedBlockQuantity: '4'
# Checks that function use default values, instead overloading
- name: WRONG_OVERLOADING_FUNCTION_ARGUMENTS
enabled: true
configuration: {}
9 changes: 1 addition & 8 deletions 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.1.2-SNAPSHOT</version>
<version>0.1.3-SNAPSHOT</version>
</parent>

<dependencies>
Expand Down Expand Up @@ -63,13 +63,6 @@
<goals>
<goal>compile</goal>
</goals>
<configuration>
<sourceDirs>
<source>src/main/kotlin</source>
<source>src/main/resources</source>
</sourceDirs>
<jvmTarget>1.8</jvmTarget>
</configuration>
</execution>
<execution>
<id>test-compile</id>
Expand Down
10 changes: 1 addition & 9 deletions 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.1.2-SNAPSHOT</version>
<version>0.1.3-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down Expand Up @@ -63,13 +63,6 @@
<goals>
<goal>compile</goal>
</goals>
<configuration>
<sourceDirs>
<source>src/main/kotlin</source>
<source>src/main/resources</source>
</sourceDirs>
<jvmTarget>1.8</jvmTarget>
</configuration>
</execution>
<execution>
<id>test-compile</id>
Expand All @@ -81,7 +74,6 @@
<sourceDirs>
<source>src/test/kotlin</source>
</sourceDirs>
<jvmTarget>1.8</jvmTarget>
</configuration>
</execution>
</executions>
Expand Down
11 changes: 1 addition & 10 deletions 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.1.2-SNAPSHOT</version>
<version>0.1.3-SNAPSHOT</version>

</parent>
<properties>
Expand Down Expand Up @@ -88,7 +88,6 @@
<sourceDirs>
<source>${project.basedir}/src/main/kotlin/generated</source>
</sourceDirs>
<jvmTarget>1.8</jvmTarget>
</configuration>
</execution>
<execution>
Expand All @@ -97,13 +96,6 @@
<goals>
<goal>compile</goal>
</goals>
<configuration>
<sourceDirs>
<source>src/main/kotlin</source>
<source>src/main/resources</source>
</sourceDirs>
<jvmTarget>1.8</jvmTarget>
</configuration>
</execution>
<execution>
<id>test-compile</id>
Expand All @@ -115,7 +107,6 @@
<sourceDirs>
<source>src/test/kotlin</source>
</sourceDirs>
<jvmTarget>1.8</jvmTarget>
</configuration>
</execution>
</executions>
Expand Down
2 changes: 2 additions & 0 deletions diktat-rules/src/main/kotlin/generated/WarningNames.kt
Original file line number Diff line number Diff line change
Expand Up @@ -179,4 +179,6 @@ object WarningNames {
const val TOO_MANY_PARAMETERS: String = "TOO_MANY_PARAMETERS"

const val NESTED_BLOCK: String = "NESTED_BLOCK"

const val WRONG_OVERLOADING_FUNCTION_ARGUMENTS: String = "WRONG_OVERLOADING_FUNCTION_ARGUMENTS"
}
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ enum class Warnings(private val canBeAutoCorrected: Boolean, private val warn: S
LAMBDA_IS_NOT_LAST_PARAMETER(false, "lambda inside function parameters should be in the end"),
TOO_MANY_PARAMETERS(false, "function has too many parameters"),
NESTED_BLOCK(false, "function has too many nested blocks and should be simplified"),
WRONG_OVERLOADING_FUNCTION_ARGUMENTS(false, "use default argument instead of function overloading"),
;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ class DiktatRuleSetProvider(private val diktatConfigFile: String = "diktat-analy
::AccurateCalculationsRule,
::LineLength,
::TypeAliasRule,
::OverloadingArgumentsFunction,
::FunctionLength,
::LambdaParameterOrder,
::FunctionArgumentsSize,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package org.cqfn.diktat.ruleset.rules

import com.pinterest.ktlint.core.Rule
import com.pinterest.ktlint.core.ast.ElementType.FUN
import com.pinterest.ktlint.core.ast.ElementType.IDENTIFIER
import com.pinterest.ktlint.core.ast.ElementType.TYPE_REFERENCE
import org.cqfn.diktat.common.config.rules.RulesConfig
import org.cqfn.diktat.ruleset.constants.Warnings.WRONG_OVERLOADING_FUNCTION_ARGUMENTS
import org.cqfn.diktat.ruleset.utils.allSiblings
import org.cqfn.diktat.ruleset.utils.findChildAfter
import org.cqfn.diktat.ruleset.utils.findChildBefore
import org.jetbrains.kotlin.com.intellij.lang.ASTNode
import org.jetbrains.kotlin.psi.KtFunction
import org.jetbrains.kotlin.psi.psiUtil.startOffset

class OverloadingArgumentsFunction(private val configRules: List<RulesConfig>) : Rule("overloading-default-values") {

private lateinit var emitWarn: ((offset: Int, errorMessage: String, canBeAutoCorrected: Boolean) -> Unit)
private var isFixMode: Boolean = false

override fun visit(node: ASTNode,
autoCorrect: Boolean,
emit: (offset: Int, errorMessage: String, canBeAutoCorrected: Boolean) -> Unit) {
isFixMode = autoCorrect
emitWarn = emit

if (node.elementType == FUN) {
checkFun(node.psi as KtFunction)
}
}

@Suppress("UnsafeCallOnNullableType")
private fun checkFun(funPsi: KtFunction) {
val allOverloadFunction = funPsi.node.allSiblings(withSelf = false)
.asSequence()
.filter { it.elementType == FUN }
.map { it.psi as KtFunction }
.filter { it.nameIdentifier!!.text == funPsi.nameIdentifier!!.text && it.valueParameters.containsAll(funPsi.valueParameters) }
.filter { funPsi.node.findChildBefore(IDENTIFIER, TYPE_REFERENCE)?.text == it.node.findChildBefore(IDENTIFIER, TYPE_REFERENCE)?.text }
.filter {funPsi.node.findChildAfter(IDENTIFIER, TYPE_REFERENCE)?.text == it.node.findChildAfter(IDENTIFIER, TYPE_REFERENCE)?.text }
.toList()
if (allOverloadFunction.isNotEmpty()) {
WRONG_OVERLOADING_FUNCTION_ARGUMENTS.warn(configRules, emitWarn, isFixMode, funPsi.node.findChildByType(IDENTIFIER)!!.text, funPsi.startOffset, funPsi.node)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,8 @@ class LocalVariablesRule(private val configRules: List<RulesConfig>) : Rule("loc
val declarationScope = property.getDeclarationScope()

val firstUsageStatementLine = getFirstUsageStatementOrBlock(usages, declarationScope).node.lineNumber()!!
checkLineNumbers(property, firstUsageStatementLine, firstUsageLine = usages.minOf { it.node.lineNumber()!! })
val firstUsage = usages.minBy { it.node.lineNumber()!! }!!
checkLineNumbers(property, firstUsageStatementLine, firstUsageLine = firstUsage.node.lineNumber()!!)
}

@Suppress("UnsafeCallOnNullableType")
Expand Down Expand Up @@ -129,7 +130,7 @@ class LocalVariablesRule(private val configRules: List<RulesConfig>) : Rule("loc
*/
@Suppress("UnsafeCallOnNullableType")
private fun getFirstUsageStatementOrBlock(usages: List<KtNameReferenceExpression>, declarationScope: KtBlockExpression?): PsiElement {
val firstUsage = usages.minByOrNull { it.node.lineNumber()!! }!!
val firstUsage = usages.minBy { it.node.lineNumber()!! }!!
val firstUsageScope = firstUsage.getParentOfType<KtBlockExpression>(true)

return if (firstUsageScope == declarationScope) {
Expand All @@ -139,7 +140,7 @@ class LocalVariablesRule(private val configRules: List<RulesConfig>) : Rule("loc
.find { it.parent == declarationScope }!!
} else {
// first usage is in deeper block compared to declaration, need to check how close is declaration to the first line of the block
usages.minByOrNull { it.node.lineNumber()!! }!!
usages.minBy { it.node.lineNumber()!! }!!
.parentsWithSelf
.find { it.parent == declarationScope }!!
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,7 @@ fun ASTNode.isChildAfterAnother(child: ASTNode, afterChild: ASTNode): Boolean =
getChildren(null).indexOf(child) > getChildren(null).indexOf(afterChild)

fun ASTNode.isChildAfterGroup(child: ASTNode, group: List<ASTNode>): Boolean =
getChildren(null).indexOf(child) > (group.map { getChildren(null).indexOf(it) }.maxOrNull() ?: 0)
getChildren(null).indexOf(child) > (group.map { getChildren(null).indexOf(it) }.max() ?: 0)

fun ASTNode.isChildBeforeAnother(child: ASTNode, beforeChild: ASTNode): Boolean = areChildrenBeforeGroup(listOf(child), listOf(beforeChild))

Expand All @@ -437,7 +437,7 @@ fun ASTNode.areChildrenBeforeChild(children: List<ASTNode>, beforeChild: ASTNode
@Suppress("UnsafeCallOnNullableType")
fun ASTNode.areChildrenBeforeGroup(children: List<ASTNode>, group: List<ASTNode>): Boolean {
require(children.isNotEmpty() && group.isNotEmpty()) { "no sense to operate on empty lists" }
return children.map { getChildren(null).indexOf(it) }.maxOrNull()!! < group.map { getChildren(null).indexOf(it) }.minOrNull()!!
return children.map { getChildren(null).indexOf(it) }.max()!! < group.map { getChildren(null).indexOf(it) }.min()!!
}

fun List<ASTNode>.handleIncorrectOrder(getSiblingBlocks: ASTNode.() -> Pair<ASTNode?, ASTNode>,
Expand Down
6 changes: 5 additions & 1 deletion diktat-rules/src/main/resources/diktat-analysis-huawei.yml
Original file line number Diff line number Diff line change
Expand Up @@ -307,4 +307,8 @@
- name: NESTED_BLOCK
enabled: true
configuration:
maxNestedBlockQuantity: '4'
maxNestedBlockQuantity: '4'
# Checks that function use default values, instead overloading
- name: WRONG_OVERLOADING_FUNCTION_ARGUMENTS
enabled: true
configuration: {}
6 changes: 5 additions & 1 deletion diktat-rules/src/main/resources/diktat-analysis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -310,4 +310,8 @@
- name: NESTED_BLOCK
enabled: true
configuration:
maxNestedBlockQuantity: '4'
maxNestedBlockQuantity: '4'
# Checks that function use default values, instead overloading
- name: WRONG_OVERLOADING_FUNCTION_ARGUMENTS
enabled: true
configuration: {}
Loading

0 comments on commit 91c1b5b

Please sign in to comment.