80 lines
3.0 KiB
C#
80 lines
3.0 KiB
C#
using System.Collections.Generic;
|
||
using System.Linq;
|
||
using Microsoft.CodeAnalysis.CSharp.Syntax;
|
||
|
||
namespace wvdet.CodeChecker
|
||
{
|
||
/// <summary>
|
||
/// Enumeration Type [枚举] 命名规范
|
||
/// 使用 Pascal Case。
|
||
/// 不要在Enum类型名称后面加上Enum后缀。(自己定义其他后缀)
|
||
/// 对于大多数Enum类型使用单数名称,仅仅在这个Enum类型是位域地时候使用复数形式。
|
||
/// 如果是用于位域的枚举,那么结尾加上FlagsAttribute。
|
||
/// </summary>
|
||
public class CheckEnum : ICheckable
|
||
{
|
||
public List<Detail> Check(string filepath, CompilationUnitSyntax root)
|
||
{
|
||
var results = new List<Detail>();
|
||
|
||
var enumDecls = root.DescendantNodes().OfType<EnumDeclarationSyntax>();
|
||
foreach (var dec in enumDecls)
|
||
{
|
||
var identifier = dec.Identifier.ValueText;
|
||
var lineNumber = dec.GetLocation().GetLineSpan().StartLinePosition.Line;
|
||
|
||
if (CheckerUtils.IsChineseIdentifier(identifier, lineNumber, out var ret))
|
||
results.Add(ret);
|
||
|
||
if (char.IsLower(identifier[0]))
|
||
{
|
||
results.Add(new Detail
|
||
{
|
||
level = Level.Error,
|
||
line = lineNumber,
|
||
codeSnippet = identifier.Trim(),
|
||
suggestion = CheckerUtils.ConvertPascalCase(identifier),
|
||
guideline = "枚举类型名必须使用PascalCase",
|
||
});
|
||
}
|
||
|
||
if (identifier[0] == 'I')
|
||
{
|
||
results.Add(new Detail
|
||
{
|
||
level = Level.Warning,
|
||
line = lineNumber,
|
||
codeSnippet = identifier.Trim(),
|
||
suggestion = CheckerUtils.ConvertPascalCase("I" + identifier),
|
||
guideline = "在interface名称前加上字母I来表示type是interface。(如:ICinfig)",
|
||
});
|
||
}
|
||
|
||
if (identifier[0] == '_')
|
||
{
|
||
results.Add(new Detail
|
||
{
|
||
level = Level.Error,
|
||
line = lineNumber,
|
||
codeSnippet = identifier.Trim(),
|
||
suggestion = CheckerUtils.ConvertPascalCase(identifier.Substring(1)),
|
||
guideline = "枚举类型名禁用以下划线开始",
|
||
});
|
||
}
|
||
|
||
if (identifier.EndsWith("Enum"))
|
||
{
|
||
results.Add(new Detail
|
||
{
|
||
level = Level.Error,
|
||
line = lineNumber,
|
||
codeSnippet = identifier.Trim(),
|
||
guideline = "不要在Enum类型名称后面加上Enum后缀。(自己定义其他后缀)",
|
||
});
|
||
}
|
||
}
|
||
|
||
return results;
|
||
}
|
||
}
|
||
} |