Skip to content

Commit

Permalink
feat: make comment analyser works
Browse files Browse the repository at this point in the history
  • Loading branch information
phodal committed Jan 8, 2024
1 parent bd4d47b commit 2940f6e
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,20 @@ class CommentRuleVisitor(val comments: List<CodeComment>, val container: CodeCon
ruleSets.forEach { ruleSet ->
ruleSet.rules.forEach { rule ->
val apiRule = rule as CommentRule
val classComment = lineCommentMap[struct.Position.StartLine - 1] ?: return@forEach
apiRule.visitRoot(struct, classComment, context, fun(rule: Rule, position: IssuePosition) {
results += Issue(
position,
ruleId = rule.key,
name = rule.name,
detail = rule.description,
ruleType = RuleType.CODE_SMELL,
fullName = "${struct.Module}:${struct.Package}:${struct.NodeName}",
source = struct.FilePath
)
})
val classComment = lineCommentMap[struct.Position.StartLine - 1]
if (classComment != null) {
apiRule.visitRoot(struct, classComment, context, fun(rule: Rule, position: IssuePosition) {
results += Issue(
position,
ruleId = rule.key,
name = rule.name,
detail = rule.description,
ruleType = RuleType.CODE_SMELL,
fullName = "${struct.Module}:${struct.Package}:${struct.NodeName}",
source = struct.FilePath
)
})
}

struct.Functions.forEach { method ->
val methodComment = lineCommentMap[method.Position.StartLine - 1] ?: return@forEach
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import chapi.domain.core.CodeFunction
import org.archguard.rule.core.IssueEmit
import org.archguard.rule.core.IssuePosition
import org.archguard.rule.core.RuleContext
import org.archguard.rule.core.Severity

/**
* Parse the documentation of the code and check whether the documentation is complete.
Expand All @@ -24,6 +25,13 @@ import org.archguard.rule.core.RuleContext
* We can use this rule to check whether the documentation is complete.
*/
class MissingParameterDescRule : CommentRule() {
init {
this.id = "missing-parameter-desc"
this.name = "MissingParameterDesc"
this.key = this.javaClass.name
this.severity = Severity.WARN
}

private val pattern = Regex("""@param\s+(\w+)\s+([^@]+)""")

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

if (matches.count() != nodeSize) {
this.description = "The documentation is un-complete, parameter description is missing"
callback(this, IssuePosition())
return
}

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

if (matchNames != nodeNames) {
this.description = "The documentation is error, parameter name is not match"
callback(this, IssuePosition())
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package cc.unitmesh.quality.comment;

import chapi.ast.javaast.JavaAnalyser
import chapi.domain.core.CodeDataStruct
import org.junit.jupiter.api.Test
import kotlin.test.assertEquals
import kotlin.test.assertFalse
import kotlin.test.assertTrue

class CommentAnalyserTest {

@Test
fun `should return empty list when analysis nodes given empty nodes`() {
// given
val nodes = ArrayList<CodeDataStruct>()

// when
val commentAnalyser = CommentAnalyser(mapOf())
val result = commentAnalyser.analysis(nodes)

// then
assertTrue(result.isEmpty())
}

@Test
fun `should return list one issue when given lost parameter`() {
// given
val code = """
public class Test {
/**
* Sum a and b, and return the result.
* @param x the first number
* @return the result of x + y
*/
public int calculateSum(int x, int y) {
return x + y;
}
}
""".trimIndent()
val container = JavaAnalyser().analysis(code, "Test.java")
container.Content = code
val commentAnalyser = CommentAnalyser(mapOf())

// when
val result = commentAnalyser.analysis(container)

// then
assertEquals(1, result.size)
assertEquals("MissingParameterDesc", result[0].name)
}
}

0 comments on commit 2940f6e

Please sign in to comment.