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

Copyright as code #740

Merged
merged 14 commits into from
Feb 2, 2021
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import org.cqfn.diktat.ruleset.constants.Warnings.COMMENTED_OUT_CODE
import org.cqfn.diktat.ruleset.utils.findAllNodesWithSpecificType

import com.pinterest.ktlint.core.Rule
import com.pinterest.ktlint.core.ast.ElementType.BINARY_EXPRESSION
import com.pinterest.ktlint.core.ast.ElementType.BLOCK
import com.pinterest.ktlint.core.ast.ElementType.BLOCK_COMMENT
import com.pinterest.ktlint.core.ast.ElementType.EOL_COMMENT
import com.pinterest.ktlint.core.ast.ElementType.FILE
Expand Down Expand Up @@ -52,6 +54,7 @@ class CommentsRule(private val configRules: List<RulesConfig>) : Rule("comments"
* with '// ' with whitespace, while automatic commenting in, e.g., IDEA creates slashes in the beginning of the line
*
*/
@Suppress("UnsafeCallOnNullableType", "TOO_LONG_FUNCTION")
private fun checkCommentedCode(node: ASTNode) {
val eolCommentsOffsetToText = getOffsetsToTextBlocksFromEolComments(node)
val blockCommentsOffsetToText = node
Expand All @@ -70,8 +73,19 @@ class CommentsRule(private val configRules: List<RulesConfig>) : Rule("comments"
offset to ktPsiFactory.createImportDirective(ImportPath.fromString(text.substringAfter("$importKeyword "))).node
text.contains(packageKeyword) ->
offset to ktPsiFactory.createPackageDirective(FqName(text.substringAfter("$packageKeyword "))).node
else ->
offset to ktPsiFactory.createBlockCodeFragment(text, null).node
else -> {
var newNode = ktPsiFactory.createBlockCodeFragment(text, null).node
val isPossibleError = newNode.findChildByType(BLOCK)!!.firstChildNode.elementType == BINARY_EXPRESSION
petertrr marked this conversation as resolved.
Show resolved Hide resolved
// A check is performed for the presence of a BINARY_EXPRESSION as first child,
// because the one-line copyright is perceived by the parser as a BINARY EXPRESSION
if (isPossibleError) {
val nodeInsideClass = ktPsiFactory.createBlockCodeFragment("class A {$text}", null).node
petertrr marked this conversation as resolved.
Show resolved Hide resolved
if (nodeInsideClass.findAllNodesWithSpecificType(TokenType.ERROR_ELEMENT).isNotEmpty()) {
newNode = nodeInsideClass
}
}
offset to newNode
}
}
}
.filter { (_, parsedNode) ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -293,4 +293,24 @@ class CommentedCodeTest : LintTestBase(::CommentsRule) {
| */
""".trimMargin())
}

@Test
@Tag(WarningNames.COMMENTED_OUT_CODE)
fun `should not trigger on Copyright and another comment`() {
lintMethod(
"""
/*
Copyright (c) Your Company Name Here. 2010-2021
*/

package org.cqfn.diktat

/*
x = 2 + 4 + 1
petertrr marked this conversation as resolved.
Show resolved Hide resolved
*/
// x = 2+4

// if true make this
""".trimMargin())
}
}