Skip to content

Commit 2940f6e

Browse files
committed
feat: make comment analyser works
1 parent bd4d47b commit 2940f6e

File tree

3 files changed

+76
-12
lines changed

3 files changed

+76
-12
lines changed

code-quality/src/main/kotlin/cc/unitmesh/quality/comment/CommentRuleVisitor.kt

+14-12
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,20 @@ class CommentRuleVisitor(val comments: List<CodeComment>, val container: CodeCon
1515
ruleSets.forEach { ruleSet ->
1616
ruleSet.rules.forEach { rule ->
1717
val apiRule = rule as CommentRule
18-
val classComment = lineCommentMap[struct.Position.StartLine - 1] ?: return@forEach
19-
apiRule.visitRoot(struct, classComment, context, fun(rule: Rule, position: IssuePosition) {
20-
results += Issue(
21-
position,
22-
ruleId = rule.key,
23-
name = rule.name,
24-
detail = rule.description,
25-
ruleType = RuleType.CODE_SMELL,
26-
fullName = "${struct.Module}:${struct.Package}:${struct.NodeName}",
27-
source = struct.FilePath
28-
)
29-
})
18+
val classComment = lineCommentMap[struct.Position.StartLine - 1]
19+
if (classComment != null) {
20+
apiRule.visitRoot(struct, classComment, context, fun(rule: Rule, position: IssuePosition) {
21+
results += Issue(
22+
position,
23+
ruleId = rule.key,
24+
name = rule.name,
25+
detail = rule.description,
26+
ruleType = RuleType.CODE_SMELL,
27+
fullName = "${struct.Module}:${struct.Package}:${struct.NodeName}",
28+
source = struct.FilePath
29+
)
30+
})
31+
}
3032

3133
struct.Functions.forEach { method ->
3234
val methodComment = lineCommentMap[method.Position.StartLine - 1] ?: return@forEach

code-quality/src/main/kotlin/cc/unitmesh/quality/comment/rule/MissingParameterDescRule.kt

+11
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import chapi.domain.core.CodeFunction
55
import org.archguard.rule.core.IssueEmit
66
import org.archguard.rule.core.IssuePosition
77
import org.archguard.rule.core.RuleContext
8+
import org.archguard.rule.core.Severity
89

910
/**
1011
* Parse the documentation of the code and check whether the documentation is complete.
@@ -24,6 +25,13 @@ import org.archguard.rule.core.RuleContext
2425
* We can use this rule to check whether the documentation is complete.
2526
*/
2627
class MissingParameterDescRule : CommentRule() {
28+
init {
29+
this.id = "missing-parameter-desc"
30+
this.name = "MissingParameterDesc"
31+
this.key = this.javaClass.name
32+
this.severity = Severity.WARN
33+
}
34+
2735
private val pattern = Regex("""@param\s+(\w+)\s+([^@]+)""")
2836

2937
override fun visitFunction(node: CodeFunction, comment: CodeComment, context: RuleContext, callback: IssueEmit) {
@@ -32,13 +40,16 @@ class MissingParameterDescRule : CommentRule() {
3240
val nodeSize = node.Parameters.size
3341

3442
if (matches.count() != nodeSize) {
43+
this.description = "The documentation is un-complete, parameter description is missing"
3544
callback(this, IssuePosition())
45+
return
3646
}
3747

3848
val matchNames = matches.map { it.groupValues[1] }.toSet()
3949
val nodeNames = node.Parameters.map { it.TypeValue }.toSet()
4050

4151
if (matchNames != nodeNames) {
52+
this.description = "The documentation is error, parameter name is not match"
4253
callback(this, IssuePosition())
4354
}
4455
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package cc.unitmesh.quality.comment;
2+
3+
import chapi.ast.javaast.JavaAnalyser
4+
import chapi.domain.core.CodeDataStruct
5+
import org.junit.jupiter.api.Test
6+
import kotlin.test.assertEquals
7+
import kotlin.test.assertFalse
8+
import kotlin.test.assertTrue
9+
10+
class CommentAnalyserTest {
11+
12+
@Test
13+
fun `should return empty list when analysis nodes given empty nodes`() {
14+
// given
15+
val nodes = ArrayList<CodeDataStruct>()
16+
17+
// when
18+
val commentAnalyser = CommentAnalyser(mapOf())
19+
val result = commentAnalyser.analysis(nodes)
20+
21+
// then
22+
assertTrue(result.isEmpty())
23+
}
24+
25+
@Test
26+
fun `should return list one issue when given lost parameter`() {
27+
// given
28+
val code = """
29+
public class Test {
30+
/**
31+
* Sum a and b, and return the result.
32+
* @param x the first number
33+
* @return the result of x + y
34+
*/
35+
public int calculateSum(int x, int y) {
36+
return x + y;
37+
}
38+
}
39+
""".trimIndent()
40+
val container = JavaAnalyser().analysis(code, "Test.java")
41+
container.Content = code
42+
val commentAnalyser = CommentAnalyser(mapOf())
43+
44+
// when
45+
val result = commentAnalyser.analysis(container)
46+
47+
// then
48+
assertEquals(1, result.size)
49+
assertEquals("MissingParameterDesc", result[0].name)
50+
}
51+
}

0 commit comments

Comments
 (0)