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>
|
|||
|
|
/// Interface [接口] 命名规范
|
|||
|
|
/// 使用 Pascal Case。
|
|||
|
|
/// 使用名词或者名词性词组命名接口。
|
|||
|
|
/// 不要轻易使用缩写。
|
|||
|
|
/// 在interface 名称前加上字母I来表示type是interface。(如:ICinfig)
|
|||
|
|
/// 不使用下划线。
|
|||
|
|
/// 文件名应和类名相同。
|
|||
|
|
/// </summary>
|
|||
|
|
public class CheckInterface : ICheckable
|
|||
|
|
{
|
|||
|
|
public List<Detail> Check(string filepath, CompilationUnitSyntax root)
|
|||
|
|
{
|
|||
|
|
var results = new List<Detail>();
|
|||
|
|
|
|||
|
|
var interfaceDecs = root.DescendantNodes().OfType<InterfaceDeclarationSyntax>().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(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 = "接口名禁用以下划线开始",
|
|||
|
|
});
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
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;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|