saveourtool / diktat Goto Github PK
View Code? Open in Web Editor NEWStrict coding standard for Kotlin and a custom set of rules for detecting code smells, code style issues and bugs
Home Page: https://diktat.saveourtool.com
License: MIT License
Strict coding standard for Kotlin and a custom set of rules for detecting code smells, code style issues and bugs
Home Page: https://diktat.saveourtool.com
License: MIT License
If your code has a copyright and it's actual year differs from the current year - it should be fixed (or warning should be raised).
For example if current year is 2020, this code
/**
* Copyright (c) Ivanoff Technologies Co., Ltd. 1992-2017. All rights reserved
*/
should be changed to this one:
/**
* Copyright (c) Ivanoff Technologies Co., Ltd. 1992-2020. All rights reserved
*/
diktat should check it's own code
@Test
@Ignore("this test is failing because params.fileName!! throws NPE")
fun `missing package name (check)`() {
Method is considered from tests if one of the following is true:
@Test
src/test
test
src\main\kotlin\org\cqfn\diktat\ruleset\rules\KdocMethods.kt:70:17
May be you can look if it is already done in ktlint-standard? If yes - simply inherit it with customizations
Only spaces are allowed for indentation and each indentation should equal to 4 spaces (tabs are not allowed).
In case you prefer using tabs - just simply configure auto change of tabs to spaces in your IDE.
Exceptions:
class Class(propertiesFileName: String,
otherName: String) {
or
fun visit(
node: ASTNode,
autoCorrect: Boolean,
params: KtLint.Params,
emit: (offset: Int, errorMessage: String, canBeAutoCorrected: Boolean) -> Unit
) {
val abcdef = "my splitted" +
" string"
Now it's hosted on Artipie. I don't see why can't we host it on Maven Central under org.cqfn
domain.
Underscores should be allowed for ignored parameters
[INFO] [ktlint] C:\Users\pwx5333130\Documents\diKTat\diktat-rules\src\test\kotlin\org\cqfn\diktat\ruleset\utils\TestUtils.kt:23:31: [9] variable name should be in camel case (correct: checkIpConfig, incorrect: CheckIPConfig) should contain only latin (ASCII) letters or numbers and should start from lower letter: _
If there are issues with user's json config, we should report it asap and fail without start of analysis. Possible issues:
There are several tools in the market place:
-- https://github.com/actions/create-release
-- https://github.com/marketplace/actions/maven-release
Rule 3.4 For non-empty blocks and block structures, the opening brace is placed at the end of the line
For non-empty code blocks with braces, they should follow the K&R style (1TBS or OTBS style):
else
, finally
or catch
keywordsGood example:
return () -> {
while (condition()) {
method()
}
}
return MyClass() {
@Override
fun method() {
if (condition()) {
try {
something()
} catch (ProblemException e) {
recover()
}
} else if (otherCondition()) {
somethingElse()
} else {
lastThing()
}
}
}
We have to prohibit fun 'some name'()
usage everywhere in the code, except test files
Currently we write all new warnings with description, list of tests and fixmes into a table manually. What could be automated:
@Test
methods[DEBUG] Internal Error (diktat-ruleset:package-naming)
com.pinterest.ktlint.core.RuleExecutionException: java.util.NoSuchElementException: Key domainName is missing in the map.
at com.pinterest.ktlint.core.KtLint$lint$1.invoke(KtLint.kt:177)
at com.pinterest.ktlint.core.KtLint$lint$1.invoke(KtLint.kt:43)
at com.pinterest.ktlint.core.KtLint$visitor$2$1.invoke(KtLint.kt:246)
at com.pinterest.ktlint.core.KtLint$visitor$2$1.invoke(KtLint.kt:43)
at com.pinterest.ktlint.core.KtLint.visit(KtLint.kt:572)
at com.pinterest.ktlint.core.KtLint.access$visit(KtLint.kt:43)
at com.pinterest.ktlint.core.KtLint$visitor$2.invoke(KtLint.kt:244)
at com.pinterest.ktlint.core.KtLint$visitor$2.invoke(KtLint.kt:43)
at com.pinterest.ktlint.core.KtLint.lint(KtLint.kt:162)
at com.pinterest.ktlint.internal.FileUtilsKt.lintFile(FileUtils.kt:51)
There is awesome app like Depfu, but it cannot be used for maven and Kotlin. Need to find some analog for it.
[INFO] [ktlint] C:\Users\pwx5333130\Documents\diKTat\diktat-rules\src\main\kotlin\org\cqfn\diktat\ruleset\rules\kdoc\KdocFormatting.kt:298:25: [17] functions that return the value of Boolean type should have <is> or <has> prefix: hasEmptyLineAfter
[INFO] [ktlint] C:\Users\pwx5333130\Documents\diKTat\diktat-rules\src\main\kotlin\org\cqfn\diktat\ruleset\utils\FunctonASTNodeUtils.kt:21:13: [17] functions that return the value of Boolean type should have <is> or <has> prefix: hasParametersKDoc
[INFO] [ktlint] C:\Users\pwx5333130\Documents\diKTat\diktat-rules\src\main\kotlin\org\cqfn\diktat\ruleset\utils\KDocUtils.kt:20:23: [17] functions that return the value of Boolean type should have <is> or <has> prefix: hasKnownKDocTag
[INFO] [ktlint] C:\Users\pwx5333130\Documents\diKTat\diktat-rules\src\main\kotlin\org\cqfn\diktat\ruleset\utils\StringUtils.kt:23:12: [17] functions that return the value of Boolean type should have <is> or <has> prefix: isASCIILettersAndDigits
Probably because this is an extension function.
Gradle is really popular in kotlin community, also ktlint has good integration with gradle. We should add instructions on how to add artipie and our dependencies in build.gradle and how to use diktat with ktlint plugin.
@deprecated("Foo")
/**
In addition to rule 2.5:
@return
, @param
tag for getters/setters at all.toString
, equals
and hashCode
NestedBlockDepth - 5/4 - [visit] at C:\Users\a00543375\Desktop\projects\diKTat\diktat-ules\src\main\kotlin\org\cqfn\diktat\ruleset\rules\FileSize.kt:28:18
Sort ENUM values alphabetically, sort function parameters alphabetically, sort argument list alphabetically, e.t.c
Now it is awful:
if (debug) {
}
On
fun ASTNode.findChildrenMatching(elementType: IElementType? = null, predicate: (ASTNode) -> Boolean): List<ASTNode> =
diktat fails with
[INFO] [ktlint] C:\Users\pwx5333130\Documents\diKTat\diktat-rules\src\main\kotlin\org\cqfn\diktat\ruleset\utils\AstNodeUtils.kt:182:81: Internal Error (diktat-ruleset:identifier-naming). Please create a ticket at https://github.com/pinterest/ktlint/issues (if possible, provide the source code that triggered an error) (cannot be auto-corrected)
Offset corresponds to opening parentheses at (ASTNode)
Can be detected as follows: warn if there are two consecutive KDocs - i.e. one is misplaced top-level, which isn't bound to any entity, and another one is KDoc for the next entity. Ensure the former is at the top of the file, right after the copyright block if it is necessary.
It incorrectly checks object companion.
For example, developer could have renamed parameter in code and forgot to update KDoc. Currently KDocMethods rule only checks if all arguments are present in KDoc, not vice versa.
mvn versions:set-property -Dproperty=diktat-check.version
etc.
Property is introduced in #111
Rule 3.3 Braces must be used in conditional statements and loop blocks
In if
, else
, when
, for
, do
, and while
statements, even if the program body is empty or contains only one statement, braces should be used.
Exception: Only The only exception is ternary operator in Kotlin (it is a single line if () <> else <>
)
When the entire expression can be
Bad example:
val value = if (string.isEmpty()) // WRONG!
0
else
1
Valid example:
val value = if (string.isEmpty()) 0 else 1 // Okay
if (condition) {
println("test")
} else {
println(0)
}
./ktlint -R diktat.jar will fail with an exception that it cannot find rules-config.json
[INFO] [ktlint] C:\Users\pwx5333130\Documents\diKTat\diktat-common\src\test\kotlin\org\cqfn\diktat\test\JsonReaderTest.kt:1:9: [5] package name does not match the directory hierarchy for this file, the path to the file is: org.cqfn.diktat.test.org.cqfn.diktat.test
I can install KTlint through Homebrew, but can't do the same for diKTat. Would be great if you can make it possible.
Now it is failing due to a code style on chinese
Currently we have rule 3.2 regarding file header and recommendation 3.2 regarding order of declarations inside class or object. Need to add requirements on declarations inside a file as a whole.
The following snippet can be a starting point for implementation inside FileStructureRule.kt
:
private val recommendedComparator = Comparator<ASTNode> { import1, import2 ->
require(import1.elementType == IMPORT_DIRECTIVE && import2.elementType == IMPORT_DIRECTIVE) { "This comparator is for sorting imports" }
val pathSegments1 = (import1.psi as KtImportDirective).importPath!!.fqName.pathSegments()
val pathSegments2 = (import2.psi as KtImportDirective).importPath!!.fqName.pathSegments()
return@Comparator 0
}
There are actions and tools like "codecov" they have maven plugin - need to integrate it
[ktlint] Caused by: kotlin.KotlinNullPointerException
[ktlint] at org.cqfn.diktat.ruleset.rules.IdentifierNaming.checkObjectNaming(IdentifierNaming.kt:185)
[ktlint] at org.cqfn.diktat.ruleset.rules.IdentifierNaming.visit(IdentifierNaming.kt:69)
[ktlint] at com.pinterest.ktlint.core.KtLint$lint$1.invoke(KtLint.kt:167)
[ktlint] ... 20 more
Same occurs on anonymous objects
object : Rule("astnode-utils-test") {
override fun visit(node: ASTNode,
In addition to rule 2.3:
Notes:
The declaration part of a class or interface should be in the following order:
KDocs added by KDocMethods
rule are not properly indented by IndentationRule
. This is probably because KDoc template is added as LeafPsiElement
, while it should be a CompositeElement
.
Example of CompositeElement
construction can be found in KdocUtils: ASTNode.insertTagBefore
.
Once this is fixed, one should try applying KDocMethods
and IndentationRule
simultaneously. Such case should also be added in #50 .
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.