Example #1
0
        private bool IsKeepResult(Match m)
        {
            string text  = m.Groups[0].Value;
            int    count = CommentRule.GetWordCount(text);

            return(count < CommentRule.LeastWordCount);               // 少于5个汉字,认为是不合格的注释,就保留结果
        }
        private void CheckXmlComment(List <string> commentLines, int commentStartLine)
        {
            if (commentStartLine <= 0 || commentLines.Count == 0)
            {
                return;
            }

            // 全并成XML字符串
            string xml = string.Join("\n", commentLines.ToArray());

            xml = "<xml>" + xml + "</xml>";             // 重新构造一个根节点,要不然就是不合法的XML

            // XML注释中的summary的内容
            string summary = null;

            // 加载XML
            XmlDocument xmldoc = new XmlDocument();

            try {
                xmldoc.LoadXml(xml);

                // 读取summary节点
                XmlNode node = xmldoc.SelectSingleNode("//summary");
                summary = node.InnerText.Trim();
            }
            catch (Exception ex) /* XML注释如果不能加载,就不检查了!  */ {
                string message = ex.Message;                    // 方便调试时查看
                return;
            }

            // 纯粹的空注释
            if (string.IsNullOrEmpty(summary))
            {
                _list.Add(new CodeCheckResult {
                    RuleCode = _rule.RuleCode,
                    Reason   = _rule.RuleCode + "; " + _rule.RuleName,
                    LineText = commentLines[0],
                    // 默认 <summary> 独占一行,所以行号加 1, 如果遇到奇葩写法,行号可能不准确了!
                    LineNo   = commentStartLine + 1,
                    FileName = _filePath
                });

                return;
            }

            // 注释中汉字的数量
            int wordCount = CommentRule.GetWordCount(summary);

            bool isOK = false;

            //if( _filePath.IndexOf(".Model") > 0 )
            if (s_fileRegex.IsMatch(_filePath))
            {
                isOK = wordCount >= 2;                      // 允许实体的属性只包含2个汉字
            }
            else
            {
                isOK = wordCount >= CommentRule.LeastWordCount;                      // 默认要求注释要包含5个汉字
            }
            // summary内容没不符合规范
            if (isOK == false)
            {
                _list.Add(new CodeCheckResult {
                    RuleCode = _rule.RuleCode,
                    Reason   = _rule.RuleCode + "; " + _rule.RuleName,
                    // 取第一行
                    LineText = GetFirstLine(summary),
                    // 默认 <summary> 独占一行,所以行号加 1, 如果遇到奇葩写法,行号可能不准确了!
                    LineNo   = commentStartLine + 1,
                    FileName = _filePath
                });
            }
        }