Skip to content

Commit

Permalink
No trigger on individual tag without KDoc (#583)
Browse files Browse the repository at this point in the history
* No trigger on individual tag without KDoc

### What's done:
    Fixed rule and tests, added tests
  • Loading branch information
kentr0w authored Dec 7, 2020
1 parent c0bacd1 commit 312bab6
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -113,22 +113,22 @@ class KdocMethods(private val configRules: List<RulesConfig>) : Rule("kdoc-metho
val returnCheckFailed = hasReturnCheckFailed(node, kdocTags)
val throwsCheckFailed = missingExceptions.isNotEmpty()

if (paramCheckFailed) {
handleParamCheck(node, kdoc, missingParameters, kDocMissingParameters, kdocTags)
}
if (returnCheckFailed) {
handleReturnCheck(node, kdoc, kdocTags)
}
if (throwsCheckFailed) {
handleThrowsCheck(node, kdoc, missingExceptions)
}

// if no tag failed, we have too little information to suggest KDoc - it would just be empty
val anyTagFailed = paramCheckFailed || returnCheckFailed || throwsCheckFailed
// if no tag failed, we have too little information to suggest KDoc - it would just be empty
if (kdoc == null && anyTagFailed) {
addKdocTemplate(node, name, missingParameters, explicitlyThrownExceptions, returnCheckFailed)
} else if (kdoc == null) {
MISSING_KDOC_ON_FUNCTION.warn(configRules, emitWarn, false, name, node.startOffset, node)
} else {
if (paramCheckFailed) {
handleParamCheck(node, kdoc, missingParameters, kDocMissingParameters, kdocTags)
}
if (returnCheckFailed) {
handleReturnCheck(node, kdoc, kdocTags)
}
if (throwsCheckFailed) {
handleThrowsCheck(node, kdoc, missingExceptions)
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,17 +61,11 @@ class KdocMethodsTest : LintTestBase(::KdocMethods) {
lintMethod(validCode, fileName = "src/main/kotlin/org/cqfn/diktat/Example.kt")
// no false positive triggers on annotations
lintMethod(complexAnnotationCode,
LintError(1, 1, ruleId, "${KDOC_WITHOUT_PARAM_TAG.warnText()} doubleInt (a)", true),
LintError(1, 1, ruleId, "${KDOC_WITHOUT_RETURN_TAG.warnText()} doubleInt", true),
LintError(1, 1, ruleId, "${KDOC_WITHOUT_THROWS_TAG.warnText()} doubleInt (IllegalStateException)", true),
LintError(1, 1, ruleId, "${MISSING_KDOC_ON_FUNCTION.warnText()} doubleInt", true),
fileName = "src/main/kotlin/org/cqfn/diktat/Example.kt"
)
// should check all .kt files unless both conditions on location and name are true
lintMethod(funCode,
LintError(1, 1, ruleId, "${KDOC_WITHOUT_PARAM_TAG.warnText()} doubleInt (a)", true),
LintError(1, 1, ruleId, "${KDOC_WITHOUT_RETURN_TAG.warnText()} doubleInt", true),
LintError(1, 1, ruleId, "${KDOC_WITHOUT_THROWS_TAG.warnText()} doubleInt (IllegalStateException)", true),
LintError(1, 1, ruleId, "${MISSING_KDOC_ON_FUNCTION.warnText()} doubleInt", true),
fileName = "src/test/kotlin/org/cqfn/diktat/Example.kt"
)
Expand Down Expand Up @@ -308,9 +302,7 @@ class KdocMethodsTest : LintTestBase(::KdocMethods) {
|}
""".trimMargin(),
LintError(10, 5, ruleId, "${MISSING_KDOC_ON_FUNCTION.warnText()} getY", false),
LintError(12, 5, ruleId, "${KDOC_WITHOUT_PARAM_TAG.warnText()} setY (y)", true),
LintError(12, 5, ruleId, "${MISSING_KDOC_ON_FUNCTION.warnText()} setY", true),
LintError(17, 5, ruleId, "${KDOC_WITHOUT_RETURN_TAG.warnText()} getZ", true),
LintError(17, 5, ruleId, "${MISSING_KDOC_ON_FUNCTION.warnText()} getZ", true)
)
}
Expand Down Expand Up @@ -353,4 +345,19 @@ class KdocMethodsTest : LintTestBase(::KdocMethods) {
""".trimMargin()
)
}

@Test
@Tag(WarningNames.MISSING_KDOC_ON_FUNCTION)
fun `should check if KfDoc is not trivial`() {
lintMethod(
"""
|fun foo(x: Int): TypeX {
| val q = goo()
| throw UnsupportedOperationException()
| return qwe
|}
""".trimMargin(),
LintError(1, 1, ruleId, "${MISSING_KDOC_ON_FUNCTION.warnText()} foo", true)
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,6 @@ class KdocParamPresentWarnTest : LintTestBase(::KdocMethods) {
|* @param a - qwe
|*/
""".trimMargin(),
LintError(1, 1, ruleId, "${KDOC_WITHOUT_PARAM_TAG.warnText()} foo (b, a)", true),
LintError(1, 1, ruleId, "${MISSING_KDOC_ON_FUNCTION.warnText()} foo", true)
)
}
Expand Down

0 comments on commit 312bab6

Please sign in to comment.