Files
plugin-library/Assets/01.CodeChecker/Editor/Rules/CheckInterface.cs
2025-03-04 16:02:44 +08:00

82 lines
3.0 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
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;
}
}
}