Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bugfix. CommentedCode when text inside block comment may be interpreted as KDoc #664

Merged
merged 8 commits into from
Dec 24, 2020
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ class CommentsRule(private val configRules: List<RulesConfig>) : Rule("comments"
val eolCommentsOffsetToText = getOffsetsToTextBlocksFromEolComments(node)
val blockCommentsOffsetToText = node
.findAllNodesWithSpecificType(BLOCK_COMMENT)
.map { it.startOffset to it.text.removeSurrounding("/*", "*/") }
.map { it.startOffset to it.text.trim().removeSurrounding("/*", "*/") }

(eolCommentsOffsetToText + blockCommentsOffsetToText)
.flatMap { (offset, text) ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ class CommentedCodeTest : LintTestBase(::CommentsRule) {

@Test
@Tag(WarningNames.COMMENTED_OUT_CODE)
fun `Should warn if commented out function is detected (single line comments)`() {
fun `Should warn if commented out function is detected single line comments`() {
lintMethod(
"""
|import org.junit.Test
Expand Down Expand Up @@ -256,4 +256,42 @@ class CommentedCodeTest : LintTestBase(::CommentsRule) {
""".trimMargin(),
LintError(1, 1, ruleId, "${COMMENTED_OUT_CODE.warnText()} public fun someFunc(name: String): Boolean =", false))
}


@Test
@Tag(WarningNames.COMMENTED_OUT_CODE)
fun `should not trigger on multiline comments #1`() {
lintMethod(
"""
|/*
|
| Copyright 2018-2020 John Doe.
|
| Licensed under the Apache License, Version 2.0 (the "License");
| you may not use this file except in compliance with the License.
| You may obtain a copy of the License at
|
| http://www.apache.org/licenses/LICENSE-2.0
|
| Unless required by applicable law or agreed to in writing, software
| distributed under the License is distributed on an "AS IS" BASIS,
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
| See the License for the specific language governing permissions and
| limitations under the License.
|
|*/
""".trimMargin())
}

@Test
@Tag(WarningNames.COMMENTED_OUT_CODE)
fun `should not trigger on multiline comments #2`() {
lintMethod(
"""
| /*
| * some text here
| maybe even with another line
| */
""".trimMargin())
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import org.junit.jupiter.api.Test
import java.io.File

import kotlinx.serialization.encodeToString
import org.junit.jupiter.api.Assertions

/**
* Test for [DiktatRuleSetProvider] in autocorrect mode as a whole. All rules are applied to a file.
Expand All @@ -44,14 +45,18 @@ class DiktatSmokeTest : FixTestBase("test/smoke/src/main/kotlin",
* Disable some of the rules.
*/
@Suppress("UnsafeCallOnNullableType")
private fun overrideRulesConfig(rulesToDisable: List<Warnings>) {
private fun overrideRulesConfig(rulesToDisable: List<Warnings>, rulesToOverride: Map<String, Map<String, String>> = emptyMap()) {
val rulesConfig = RulesConfigReader(javaClass.classLoader).readResource(configFilePath)!!
.toMutableList()
.also { rulesConfig ->
rulesToDisable.forEach { warning ->
rulesConfig.removeIf { it.name == warning.name }
rulesConfig.add(RulesConfig(warning.name, enabled = false, configuration = emptyMap()))
}
rulesToOverride.forEach { (name, configuration) ->
rulesConfig.removeIf { it.name == name }
rulesConfig.add(RulesConfig(name, enabled = true, configuration = configuration))
}
}
createTempFile()
.also {
Expand Down Expand Up @@ -88,6 +93,30 @@ class DiktatSmokeTest : FixTestBase("test/smoke/src/main/kotlin",
fixAndCompare("DefaultPackageExpected.kt", "DefaultPackageTest.kt")
}

@Test
@Tag("DiktatRuleSetProvider")
fun `smoke test #5`() {
overrideRulesConfig(emptyList(),
mapOf(
Warnings.HEADER_MISSING_OR_WRONG_COPYRIGHT.name to mapOf(
"isCopyrightMandatory" to "true",
"copyrightText" to """|Copyright 2018-2020 John Doe.
| Licensed under the Apache License, Version 2.0 (the "License");
| you may not use this file except in compliance with the License.
| You may obtain a copy of the License at
|
| http://www.apache.org/licenses/LICENSE-2.0
""".trimMargin()
)
)
)
fixAndCompare("Example5Expected.kt", "Example5Test.kt")

Assertions.assertFalse(
unfixedLintErrors.contains(LintError(line=1, col=1, ruleId="diktat-ruleset:comments", detail="${Warnings.COMMENTED_OUT_CODE.warnText()} /*"))
)
}

@Test
@Tag("DiktatRuleSetProvider")
fun `smoke test #4`() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/*
Copyright 2018-2020 John Doe.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0
*/

package org.cqfn.diktat

class Some {

}

Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package org.cqfn.diktat

class Some {

}