From c95915395134d9f6a3a5c6bb297786d39caadcaa Mon Sep 17 00:00:00 2001 From: Phodal Huang Date: Mon, 8 Jan 2024 14:47:17 +0800 Subject: [PATCH] docs: add doc architecture --- README.md | 6 +- .../quality/comment/rule/CommentRule.kt | 4 +- .../comment/rule/MissingParameterDescRule.kt | 24 ++++- .../rule/MissingParameterDescRuleTest.kt | 98 +++++++++++++++++++ docs/home.md | 6 +- docs/logo.svg | 60 ++++++++++++ 6 files changed, 191 insertions(+), 7 deletions(-) create mode 100644 code-quality/src/test/kotlin/cc/unitmesh/quality/comment/rule/MissingParameterDescRuleTest.kt create mode 100644 docs/logo.svg diff --git a/README.md b/README.md index 135d129..7972852 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,7 @@ -

UnitGen - Code Fine-tuning Data Framework

- +

UnitGen

+

+ Chapi Logo +

CI/CD diff --git a/code-quality/src/main/kotlin/cc/unitmesh/quality/comment/rule/CommentRule.kt b/code-quality/src/main/kotlin/cc/unitmesh/quality/comment/rule/CommentRule.kt index 6a0fa39..9d67a83 100644 --- a/code-quality/src/main/kotlin/cc/unitmesh/quality/comment/rule/CommentRule.kt +++ b/code-quality/src/main/kotlin/cc/unitmesh/quality/comment/rule/CommentRule.kt @@ -7,11 +7,11 @@ import org.archguard.rule.core.Rule import org.archguard.rule.core.RuleContext open class CommentRule : Rule() { - fun visitRoot(node: CodeDataStruct, comment: String, context: RuleContext, callback: IssueEmit) { + open fun visitRoot(node: CodeDataStruct, comment: String, context: RuleContext, callback: IssueEmit) { } - fun visitFunction(node: CodeFunction, comment: String, context: RuleContext, callback: IssueEmit) { + open fun visitFunction(node: CodeFunction, comment: String, context: RuleContext, callback: IssueEmit) { } } diff --git a/code-quality/src/main/kotlin/cc/unitmesh/quality/comment/rule/MissingParameterDescRule.kt b/code-quality/src/main/kotlin/cc/unitmesh/quality/comment/rule/MissingParameterDescRule.kt index 5abcf3f..d60782d 100644 --- a/code-quality/src/main/kotlin/cc/unitmesh/quality/comment/rule/MissingParameterDescRule.kt +++ b/code-quality/src/main/kotlin/cc/unitmesh/quality/comment/rule/MissingParameterDescRule.kt @@ -1,5 +1,10 @@ package cc.unitmesh.quality.comment.rule +import chapi.domain.core.CodeFunction +import org.archguard.rule.core.IssueEmit +import org.archguard.rule.core.IssuePosition +import org.archguard.rule.core.RuleContext + /** * Parse the documentation of the code and check whether the documentation is complete. * @@ -17,6 +22,23 @@ package cc.unitmesh.quality.comment.rule * * We can use this rule to check whether the documentation is complete. */ -class MissingParameterDescRule: CommentRule() { +class MissingParameterDescRule : CommentRule() { + private val pattern = Regex("""@param\s+(\w+)\s+([^@]+)""") + + override fun visitFunction(node: CodeFunction, comment: String, context: RuleContext, callback: IssueEmit) { + val matches = pattern.findAll(comment) + + val nodeSize = node.Parameters.size + + if (matches.count() != nodeSize) { + callback(this, IssuePosition()) + } + + val matchNames = matches.map { it.groupValues[1] }.toSet() + val nodeNames = node.Parameters.map { it.TypeType }.toSet() + if (matchNames != nodeNames) { + callback(this, IssuePosition()) + } + } } \ No newline at end of file diff --git a/code-quality/src/test/kotlin/cc/unitmesh/quality/comment/rule/MissingParameterDescRuleTest.kt b/code-quality/src/test/kotlin/cc/unitmesh/quality/comment/rule/MissingParameterDescRuleTest.kt new file mode 100644 index 0000000..088df47 --- /dev/null +++ b/code-quality/src/test/kotlin/cc/unitmesh/quality/comment/rule/MissingParameterDescRuleTest.kt @@ -0,0 +1,98 @@ +package cc.unitmesh.quality.comment.rule; + +import chapi.domain.core.CodeFunction +import chapi.domain.core.CodeProperty +import org.archguard.rule.core.IssueEmit +import org.archguard.rule.core.IssuePosition +import org.archguard.rule.core.Rule +import org.archguard.rule.core.RuleContext +import org.junit.jupiter.api.Test; + +import kotlin.test.assertFalse +import kotlin.test.assertTrue + +class MissingParameterDescRuleTest { + + @Test + fun shouldNotEmitIssueWhenDocumentationIsComplete() { + // Given + val rule = MissingParameterDescRule() + val function = CodeFunction() + function.Parameters = listOf(CodeProperty(TypeType = "int", TypeValue = "a"), CodeProperty(TypeType = "int", TypeValue = "b")) + val comment = """ + /** + * Sum a and b, and return the result. + * @param a the first number + * @param b the second number + * @return the result of a + b + */ + """.trimIndent() + val context = RuleContext() + val callback = IssueEmitCallback() + + // When + rule.visitFunction(function, comment, context, callback) + + // Then + assertFalse(callback.hasIssue()) + } + + @Test + fun shouldEmitIssueWhenParameterDescriptionIsMissing() { + // Given + val rule = MissingParameterDescRule() + val function = CodeFunction() + function.Parameters = listOf(CodeProperty(TypeType = "int", TypeValue = "a"), CodeProperty(TypeType = "int", TypeValue = "b")) + val comment = """ + /** + * Sum a and b, and return the result. + * @param a the first number + * @return the result of a + b + */ + """.trimIndent() + val context = RuleContext() + val callback = IssueEmitCallback() + + // When + rule.visitFunction(function, comment, context, callback) + + // Then + assertTrue(callback.hasIssue()) + } + + @Test + fun shouldEmitIssueWhenParameterNamesDoNotMatch() { + // Given + val rule = MissingParameterDescRule() + val function = CodeFunction() + function.Parameters = listOf(CodeProperty(TypeType = "int", TypeValue = "a"), CodeProperty(TypeType = "int", TypeValue = "b")) + val comment = """ + /** + * Sum a and b, and return the result. + * @param x the first number + * @param y the second number + * @return the result of a + b + */ + """.trimIndent() + val context = RuleContext() + val callback = IssueEmitCallback() + + // When + rule.visitFunction(function, comment, context, callback) + + // Then + assertTrue(callback.hasIssue()) + } +} + +class IssueEmitCallback : IssueEmit { + private var hasIssue = false + + fun hasIssue(): Boolean { + return hasIssue + } + + override fun invoke(rule: Rule, position: IssuePosition) { + hasIssue = true + } +} \ No newline at end of file diff --git a/docs/home.md b/docs/home.md index c038f73..9fbcf51 100644 --- a/docs/home.md +++ b/docs/home.md @@ -6,8 +6,10 @@ nav_order: 1 permalink: / --- -

UnitGen - Code Fine-tuning Data Framework

- +

UnitGen

+

+ Chapi Logo +

CI/CD diff --git a/docs/logo.svg b/docs/logo.svg new file mode 100644 index 0000000..b71108b --- /dev/null +++ b/docs/logo.svg @@ -0,0 +1,60 @@ + + + + unit-gen + Created with Sketch. + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file