Skip to content

Commit

Permalink
添加分析器 实体类禁止同时标记为Component和Child (#524)
Browse files Browse the repository at this point in the history
  • Loading branch information
susices authored Nov 30, 2023
1 parent ccd405d commit 7311a19
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 2 deletions.
19 changes: 18 additions & 1 deletion Share/Analyzer/Analyzer/EntityMemberDeclarationAnalyzer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace ET.Analyzer
public class EntityMemberDeclarationAnalyzer: DiagnosticAnalyzer
{
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics =>ImmutableArray.Create(EntityDelegateDeclarationAnalyzerRule.Rule,
EntityFieldDeclarationInEntityAnalyzerRule.Rule, LSEntityFloatMemberAnalyzer.Rule);
EntityFieldDeclarationInEntityAnalyzerRule.Rule, LSEntityFloatMemberAnalyzer.Rule, EntityComponentChildAnalyzerRule.Rule);

public override void Initialize(AnalysisContext context)
{
Expand Down Expand Up @@ -53,11 +53,13 @@ private void Analyzer(SemanticModelAnalysisContext context, INamedTypeSymbol nam
{
AnalyzeDelegateMember(context, namedTypeSymbol);
AnalyzeEntityMember(context, namedTypeSymbol);
AnalyzeComponentChildAttr(context, namedTypeSymbol);
}else if (baseType == Definition.LSEntityType)
{
AnalyzeDelegateMember(context, namedTypeSymbol);
AnalyzeEntityMember(context, namedTypeSymbol);
AnalyzeFloatMemberInLSEntity(context,namedTypeSymbol);
AnalyzeComponentChildAttr(context, namedTypeSymbol);
}
}

Expand Down Expand Up @@ -209,6 +211,21 @@ private bool GenericTypeHasFloatTypeArgs(INamedTypeSymbol namedTypeSymbol)

return false;
}

/// <summary>
/// 实体类是否同时标记为component child
/// </summary>
private void AnalyzeComponentChildAttr(SemanticModelAnalysisContext context, INamedTypeSymbol namedTypeSymbol)
{
bool hasComponentOf = namedTypeSymbol.HasAttribute(Definition.ComponentOfAttribute);
bool hasChildOf = namedTypeSymbol.HasAttribute(Definition.ChildOfAttribute);
if (hasComponentOf && hasChildOf)
{
var syntax = namedTypeSymbol.DeclaringSyntaxReferences.First().GetSyntax() as ClassDeclarationSyntax;
Diagnostic diagnostic = Diagnostic.Create(EntityComponentChildAnalyzerRule.Rule, syntax?.Identifier.GetLocation(),namedTypeSymbol.Name);
context.ReportDiagnostic(diagnostic);
}
}
}
}

2 changes: 2 additions & 0 deletions Share/Analyzer/Config/DiagnosticIds.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,5 +56,7 @@ public static class DiagnosticIds


public const string EntityHashCodeAnalyzerRuleId = "ET0027";

public const string EntityComponentChildAnalyzerRuleId = "ET0028";
}
}
18 changes: 18 additions & 0 deletions Share/Analyzer/Config/DiagnosticRules.cs
Original file line number Diff line number Diff line change
Expand Up @@ -395,4 +395,22 @@ public static class EntityHashCodeAnalyzerRule
true,
Description);
}

public static class EntityComponentChildAnalyzerRule
{
private const string Title = "实体类禁止同时标记为Component和Child";

private const string MessageFormat = "实体类:{0} 禁止同时标记为Component和Child";

private const string Description = "实体类禁止同时标记为Component和Child.";

public static readonly DiagnosticDescriptor Rule =
new DiagnosticDescriptor(DiagnosticIds.EntityComponentChildAnalyzerRuleId,
Title,
MessageFormat,
DiagnosticCategories.All,
DiagnosticSeverity.Error,
true,
Description);
}
}
1 change: 0 additions & 1 deletion Unity/Assets/Scripts/Codes/Model/Share/LockStep/LSWorld.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ public static TSRandom GetRandom(this LSEntity entity)

[EnableMethod]
[ChildOf]
[ComponentOf]
[MemoryPackable]
public partial class LSWorld: Entity, IAwake, IScene
{
Expand Down

0 comments on commit 7311a19

Please sign in to comment.