82 lines
3.0 KiB
C#
82 lines
3.0 KiB
C#
using System.Collections.Generic;
|
||
using System.IO;
|
||
using System.Linq;
|
||
using Microsoft.CodeAnalysis.CSharp.Syntax;
|
||
|
||
namespace wvdet.CodeChecker
|
||
{
|
||
/// <summary>
|
||
/// Struct [结构体] 命名规范
|
||
/// 使用 Pascal Case。
|
||
/// 使用名词或者名词性词组命名接口。
|
||
/// 不要轻易使用缩写。
|
||
/// 在struct 名称前加上字母S来表示type是在struct。(如:SData)
|
||
/// 不使用下划线。
|
||
/// 文件名应和类名相同。
|
||
/// </summary>
|
||
public class CheckStruct : ICheckable
|
||
{
|
||
public List<Detail> Check(string filepath, CompilationUnitSyntax root)
|
||
{
|
||
var results = new List<Detail>();
|
||
|
||
var interfaceDecs = root.DescendantNodes().OfType<StructDeclarationSyntax>().ToList();
|
||
foreach (var dec in interfaceDecs)
|
||
{
|
||
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("S" + identifier),
|
||
guideline = "结构体类型名必须使用PascalCase",
|
||
});
|
||
}
|
||
|
||
else if (identifier[0] != 'S')
|
||
{
|
||
results.Add(new Detail
|
||
{
|
||
level = Level.Warning,
|
||
line = lineNumber,
|
||
codeSnippet = identifier.Trim(),
|
||
suggestion = CheckerUtils.ConvertPascalCase("S" + identifier),
|
||
guideline = "在struct名称前加上字母S来表示type是struct。(如:SData)",
|
||
});
|
||
}
|
||
|
||
else if (identifier[0] == '_')
|
||
{
|
||
results.Add(new Detail
|
||
{
|
||
level = Level.Error,
|
||
line = lineNumber,
|
||
codeSnippet = identifier.Trim(),
|
||
suggestion = CheckerUtils.ConvertPascalCase("S" + identifier.Substring(1)),
|
||
guideline = "结构体名禁用以下划线开始",
|
||
});
|
||
}
|
||
}
|
||
|
||
var filename = Path.GetFileNameWithoutExtension(filepath);
|
||
if (interfaceDecs.Count > 0 && interfaceDecs.All(x => x.Identifier.ValueText != filename))
|
||
{
|
||
results.Add(new Detail
|
||
{
|
||
level = Level.Warning,
|
||
guideline = "文件名应和类名相同",
|
||
});
|
||
}
|
||
|
||
return results;
|
||
}
|
||
}
|
||
} |