@@ -2,6 +2,7 @@ package org.cqfn.diktat.ruleset.rules.chapter2.comments
2
2
3
3
import org.cqfn.diktat.common.config.rules.RulesConfig
4
4
import org.cqfn.diktat.ruleset.constants.EmitType
5
+ import org.cqfn.diktat.ruleset.constants.ListOfPairs
5
6
import org.cqfn.diktat.ruleset.constants.Warnings.COMMENTED_OUT_CODE
6
7
import org.cqfn.diktat.ruleset.utils.findAllNodesWithSpecificType
7
8
@@ -52,26 +53,35 @@ class CommentsRule(private val configRules: List<RulesConfig>) : Rule("comments"
52
53
* with '// ' with whitespace, while automatic commenting in, e.g., IDEA creates slashes in the beginning of the line
53
54
*
54
55
*/
56
+ @Suppress(" UnsafeCallOnNullableType" , " TOO_LONG_FUNCTION" )
55
57
private fun checkCommentedCode (node : ASTNode ) {
56
- val eolCommentsOffsetToText = getOffsetsToTextBlocksFromEolComments(node)
58
+ val errorNodesWithText: ListOfPairs = mutableListOf ()
59
+ val eolCommentsOffsetToText = getOffsetsToTextBlocksFromEolComments(node, errorNodesWithText)
57
60
val blockCommentsOffsetToText = node
58
61
.findAllNodesWithSpecificType(BLOCK_COMMENT )
59
- .map { it.startOffset to it.text.trim().removeSurrounding(" /*" , " */" ) }
60
-
62
+ .map {
63
+ errorNodesWithText.add(it to it.text.trim().removeSurrounding(" /*" , " */" ))
64
+ it.startOffset to it.text.trim().removeSurrounding(" /*" , " */" )
65
+ }
61
66
(eolCommentsOffsetToText + blockCommentsOffsetToText)
62
67
.flatMap { (offset, text) ->
63
68
val (singleLines, blockLines) = text.lines().partition { it.contains(importOrPackage) }
64
69
val block = if (blockLines.isNotEmpty()) listOf (blockLines.joinToString(" \n " )) else emptyList()
65
- (singleLines + block).map { offset to it }
70
+ (singleLines + block).map {
71
+ offset to it
72
+ }
66
73
}
67
- .map { (offset, text) ->
74
+ .mapNotNull { (offset, text) ->
68
75
when {
69
76
text.contains(importKeyword) ->
70
77
offset to ktPsiFactory.createImportDirective(ImportPath .fromString(text.substringAfter(" $importKeyword " ))).node
71
78
text.contains(packageKeyword) ->
72
79
offset to ktPsiFactory.createPackageDirective(FqName (text.substringAfter(" $packageKeyword " ))).node
73
- else ->
80
+ else -> if (text.contains(requirePartOfCode)) {
74
81
offset to ktPsiFactory.createBlockCodeFragment(text, null ).node
82
+ } else {
83
+ null
84
+ }
75
85
}
76
86
}
77
87
.filter { (_, parsedNode) ->
@@ -80,7 +90,8 @@ class CommentsRule(private val configRules: List<RulesConfig>) : Rule("comments"
80
90
.isEmpty()
81
91
}
82
92
.forEach { (offset, parsedNode) ->
83
- COMMENTED_OUT_CODE .warn(configRules, emitWarn, isFixMode, parsedNode.text.substringBefore(" \n " ).trim(), offset, parsedNode)
93
+ COMMENTED_OUT_CODE .warn(configRules, emitWarn, isFixMode, parsedNode.text.substringBefore(" \n " ).trim(), offset,
94
+ errorNodesWithText.find { it.second == parsedNode.text }?.first ? : parsedNode)
84
95
}
85
96
}
86
97
@@ -90,7 +101,7 @@ class CommentsRule(private val configRules: List<RulesConfig>) : Rule("comments"
90
101
* Splitting back into lines, if necessary, will be done outside of this method, for both text from EOL and block.
91
102
* fixme: in this case offset is lost for lines which will be split once more
92
103
*/
93
- private fun getOffsetsToTextBlocksFromEolComments (node : ASTNode ): List <Pair <Int , String >> {
104
+ private fun getOffsetsToTextBlocksFromEolComments (node : ASTNode , errorNodesWithText : ListOfPairs ): List <Pair <Int , String >> {
94
105
val comments = node
95
106
.findAllNodesWithSpecificType(EOL_COMMENT )
96
107
.filter { ! it.text.contains(eolCommentStart) || isCodeAfterCommentStart(it.text) }
@@ -109,6 +120,7 @@ class CommentsRule(private val configRules: List<RulesConfig>) : Rule("comments"
109
120
acc
110
121
}
111
122
.map { list ->
123
+ list.forEach { errorNodesWithText.add(it to it.text.removePrefix(" //" )) }
112
124
list.first().startOffset to list.joinToString(" \n " ) { it.text.removePrefix(" //" ) }
113
125
}
114
126
} else {
@@ -139,6 +151,7 @@ class CommentsRule(private val configRules: List<RulesConfig>) : Rule("comments"
139
151
private val importOrPackageRegex = """ ^(import|package)?\s+([a-zA-Z.])+;*$""" .toRegex()
140
152
private val functionRegex = """ ^(public|private|protected)*\s*(override|abstract|actual|expect)*\s?fun\s+\w+(\(.*\))?(\s*:\s*\w+)?\s*[{=]${' $' } """ .toRegex()
141
153
private val rightBraceRegex = """ ^\s*}$""" .toRegex()
154
+ private val requirePartOfCode = """ val |var |=|(\{((.|\n)*)\})""" .toRegex()
142
155
private val codeFileStartCases = listOf (classRegex, importOrPackageRegex, functionRegex, rightBraceRegex)
143
156
private val eolCommentStart = """ // \S""" .toRegex()
144
157
}
0 commit comments