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. CommentsRule doesn't detect commented out class declaration #630

Merged
merged 10 commits into from
Dec 14, 2020
Original file line number Diff line number Diff line change
Expand Up @@ -126,21 +126,19 @@ class CommentsRule(private val configRules: List<RulesConfig>) : Rule("comments"
*/
private fun isCodeAfterCommentStart(text: String): Boolean {
val textWithoutCommentStartToken = text.removePrefix("//").trim()
return textWithoutCommentStartToken.contains(classRegex) ||
textWithoutCommentStartToken.contains(importOrPackageRegex) ||
textWithoutCommentStartToken.contains(functionRegex) ||
textWithoutCommentStartToken.contains(rightBraceRegex)
return codeFileStartCases.any { textWithoutCommentStartToken.contains(it) }
}

companion object {
private val importKeyword = KtTokens.IMPORT_KEYWORD.value
private val packageKeyword = KtTokens.PACKAGE_KEYWORD.value
private val importOrPackage = """($importKeyword|$packageKeyword) """.toRegex()
private val classRegex =
"""^\s*(public|private|open|internal|protected)*\s*(class|object)\s+(\w+)(\(.*\))*(\s*:\s*\w+(\(.*\))*)?\s*\{*$""".toRegex()
private val importOrPackageRegex = """^(import|package)?\s+([a-zA-Z.])+$""".toRegex()
private val functionRegex = """^(override)*\s?fun\s+\w+(\(.*\))?(\s*:\s*\w+)?\s*\{$""".toRegex()
"""^\s*(public|private|open|internal|protected|data|sealed)*\s*(class|object)\s+(\w+)(\(.*\))*(\s*:\s*\w+(\(.*\))*)?\s*\{*$""".toRegex()
private val importOrPackageRegex = """^(import|package)?\s+([a-zA-Z.])+;*$""".toRegex()
private val functionRegex = """^(override|abstract|actual|expected)*\s?fun\s+\w+(\(.*\))?(\s*:\s*\w+)?\s*[{=]$""".toRegex()
private val rightBraceRegex = """^\s*}$""".toRegex()
private val codeFileStartCases = listOf(classRegex, importOrPackageRegex, functionRegex, rightBraceRegex)
private val eolCommentStart = """// \S""".toRegex()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ class CommentedCodeTest : LintTestBase(::CommentsRule) {

@Test
@Tag(WarningNames.COMMENTED_OUT_CODE)
fun `Should warn if commented out function is detected single line comments with surrounding text`() {
fun `Should warn if commented out function is detected - single line comments with surrounding text`() {
lintMethod(
"""
|import org.junit.Test
Expand Down Expand Up @@ -205,7 +205,7 @@ class CommentedCodeTest : LintTestBase(::CommentsRule) {

@Test
@Tag(WarningNames.COMMENTED_OUT_CODE)
fun `should trigger on function with one space after comment start token`() {
fun `should trigger on function with one space after comment start token - { sign`() {
lintMethod(
"""
|// fun someFunc(name: String): Boolean {
Expand All @@ -214,4 +214,15 @@ class CommentedCodeTest : LintTestBase(::CommentsRule) {
""".trimMargin(),
LintError(1, 1, ruleId, "${COMMENTED_OUT_CODE.warnText()} fun someFunc(name: String): Boolean {", false))
}

@Test
@Tag(WarningNames.COMMENTED_OUT_CODE)
fun `should trigger on function with one space after comment start token - = sign`() {
lintMethod(
"""
|// fun someFunc(name: String): Boolean =
|// name.contains("a")
""".trimMargin(),
LintError(1, 1, ruleId, "${COMMENTED_OUT_CODE.warnText()} fun someFunc(name: String): Boolean =", false))
}
}