From 223ac403a0d697a05e41aac6ccf5b024c3effdc4 Mon Sep 17 00:00:00 2001 From: Phodal Huang Date: Mon, 8 Jan 2024 09:37:59 +0800 Subject: [PATCH] refactor: extract APIs for rules --- .../cc/unitmesh/quality/QualityAnalyser.kt | 15 +++++++- .../quality/comment/CommentAnalyser.kt | 7 ++-- .../quality/comment/CommentRuleVisitor.kt | 36 +++++++++++++++++++ 3 files changed, 55 insertions(+), 3 deletions(-) create mode 100644 code-quality/src/main/kotlin/cc/unitmesh/quality/comment/CommentRuleVisitor.kt diff --git a/code-quality/src/main/kotlin/cc/unitmesh/quality/QualityAnalyser.kt b/code-quality/src/main/kotlin/cc/unitmesh/quality/QualityAnalyser.kt index 4d669c1..168cdb0 100644 --- a/code-quality/src/main/kotlin/cc/unitmesh/quality/QualityAnalyser.kt +++ b/code-quality/src/main/kotlin/cc/unitmesh/quality/QualityAnalyser.kt @@ -5,13 +5,26 @@ import cc.unitmesh.quality.extension.JavaServiceAnalyser import cc.unitmesh.quality.extension.JavaRepositoryAnalyser import cc.unitmesh.quality.extension.JavaControllerAnalyser import cc.unitmesh.quality.testbadsmell.TestBadsmellAnalyser +import chapi.domain.core.CodeContainer import chapi.domain.core.CodeDataStruct import org.archguard.rule.core.Issue interface QualityAnalyser { + + /** + * For [CodeQualityType.DocComment], we need to analysis the whole container which contains comments. + */ + fun analysis(container: CodeContainer): List { + return listOf() + } + /** - * Normal analysis, like [CodeQualityType.BadSmell], [CodeQualityType.TestBadSmell], etc. + * Normal analysis, we only need to analysis the given nodes. + * + * - [CodeQualityType.BadSmell], [CodeQualityType.TestBadSmell] + * - [CodeQualityType.JavaController], [CodeQualityType.JavaRepository], [CodeQualityType.JavaService] + * */ fun analysis(nodes: List): List diff --git a/code-quality/src/main/kotlin/cc/unitmesh/quality/comment/CommentAnalyser.kt b/code-quality/src/main/kotlin/cc/unitmesh/quality/comment/CommentAnalyser.kt index f56a0d3..2a98f93 100644 --- a/code-quality/src/main/kotlin/cc/unitmesh/quality/comment/CommentAnalyser.kt +++ b/code-quality/src/main/kotlin/cc/unitmesh/quality/comment/CommentAnalyser.kt @@ -1,11 +1,14 @@ package cc.unitmesh.quality.comment import cc.unitmesh.quality.QualityAnalyser +import chapi.domain.core.CodeContainer import chapi.domain.core.CodeDataStruct import org.archguard.rule.core.Issue -class CommentAnalyser(comments: List, thresholds: Map) : QualityAnalyser { - override fun analysis(nodes: List): List { +class CommentAnalyser(thresholds: Map) : QualityAnalyser { + override fun analysis(nodes: List): List = listOf() + + override fun analysis(container: CodeContainer): List { return listOf() } } diff --git a/code-quality/src/main/kotlin/cc/unitmesh/quality/comment/CommentRuleVisitor.kt b/code-quality/src/main/kotlin/cc/unitmesh/quality/comment/CommentRuleVisitor.kt new file mode 100644 index 0000000..0587606 --- /dev/null +++ b/code-quality/src/main/kotlin/cc/unitmesh/quality/comment/CommentRuleVisitor.kt @@ -0,0 +1,36 @@ +package cc.unitmesh.quality.comment + +import cc.unitmesh.quality.comment.rule.CommentRule +import chapi.domain.core.CodeContainer +import org.archguard.rule.core.Issue +import org.archguard.rule.core.RuleContext +import org.archguard.rule.core.RuleSet +import org.archguard.rule.core.RuleVisitor + +class CommentRuleVisitor(val comments: List, container: CodeContainer) : RuleVisitor(comments) { + override fun visitor(ruleSets: Iterable): List { + val results: MutableList = mutableListOf() + val context = RuleContext() + + ruleSets.forEach { ruleSet -> + ruleSet.rules.forEach { rule -> + val apiRule = rule as CommentRule +// resources.map { +// apiRule.visitResource(it, context, fun(rule: Rule, position: IssuePosition) { +// results += Issue( +// position, +// ruleId = rule.key, +// name = rule.name, +// detail = rule.description, +// ruleType = RuleType.HTTP_API_SMELL, +// fullName = "${it.packageName}:${it.className}:${it.methodName}", +// source = "${it.sourceHttpMethod} ${it.sourceUrl}" +// ) +// }) +// } + } + } + + return results + } +}