diff --git a/TUnit.Analyzers/ClassParametersAnalyzer.cs b/TUnit.Analyzers/ClassParametersAnalyzer.cs index e97a623b82..00324f9a32 100644 --- a/TUnit.Analyzers/ClassParametersAnalyzer.cs +++ b/TUnit.Analyzers/ClassParametersAnalyzer.cs @@ -44,7 +44,7 @@ private void AnalyzeSymbol(SymbolAnalysisContext context) .SelectMany(x => x.GetAttributes()) .Concat(namedTypeSymbol.ContainingAssembly.GetAttributes()) .Any(x => x.IsDataSourceAttribute(context.Compilation) || - x.IsClassConstructorAttribute(context.Compilation)); + x.IsClassConstructorAttribute()); if (!hasDataSourceOrClassConstructor) { diff --git a/TUnit.Analyzers/Extensions/AttributeExtensions.cs b/TUnit.Analyzers/Extensions/AttributeExtensions.cs index 09eeebf473..e306c176cf 100644 --- a/TUnit.Analyzers/Extensions/AttributeExtensions.cs +++ b/TUnit.Analyzers/Extensions/AttributeExtensions.cs @@ -34,7 +34,7 @@ public static bool IsStandardHook(this AttributeData attributeData, Compilation { hookType = HookType.Before; type = attributeData.AttributeClass!; - hookLevel = (HookLevel?) Enum.Parse(typeof(HookLevel), attributeData.ConstructorArguments.First().ToCSharpString().Split('.').Last()); + hookLevel = ParseHookLevel(attributeData); return true; } @@ -44,7 +44,7 @@ public static bool IsStandardHook(this AttributeData attributeData, Compilation { hookType = HookType.After; type = attributeData.AttributeClass!; - hookLevel = (HookLevel?) Enum.Parse(typeof(HookLevel), attributeData.ConstructorArguments.First().ToCSharpString().Split('.').Last()); + hookLevel = ParseHookLevel(attributeData); return true; } @@ -63,7 +63,7 @@ public static bool IsEveryHook(this AttributeData attributeData, Compilation com { hookType = HookType.Before; type = attributeData.AttributeClass!; - hookLevel = (HookLevel?) Enum.Parse(typeof(HookLevel), attributeData.ConstructorArguments.First().ToCSharpString().Split('.').Last()); + hookLevel = ParseHookLevel(attributeData); return true; } @@ -74,7 +74,7 @@ public static bool IsEveryHook(this AttributeData attributeData, Compilation com { hookType = HookType.After; type = attributeData.AttributeClass!; - hookLevel = (HookLevel?) Enum.Parse(typeof(HookLevel), attributeData.ConstructorArguments.First().ToCSharpString().Split('.').Last()); + hookLevel = ParseHookLevel(attributeData); return true; } @@ -119,7 +119,7 @@ public static bool IsDataSourceAttribute(this AttributeData? attributeData, Comp return attributeData.AttributeClass.AllInterfaces.Contains(dataAttributeInterface, SymbolEqualityComparer.Default); } - public static bool IsClassConstructorAttribute(this AttributeData? attributeData, Compilation compilation) + public static bool IsClassConstructorAttribute(this AttributeData? attributeData) { if (attributeData?.AttributeClass is null) { @@ -129,7 +129,7 @@ public static bool IsClassConstructorAttribute(this AttributeData? attributeData var baseType = attributeData.AttributeClass; while (baseType != null) { - if (baseType.Name == "BaseClassConstructorAttribute" || + if (baseType.Name == "BaseClassConstructorAttribute" || baseType.Name == "ClassConstructorAttribute") { return true; @@ -140,8 +140,10 @@ public static bool IsClassConstructorAttribute(this AttributeData? attributeData return false; } - public static string GetHookType(this AttributeData attributeData) + private static HookLevel ParseHookLevel(AttributeData attributeData) { - return attributeData.ConstructorArguments[0].ToCSharpString(); + var span = attributeData.ConstructorArguments.First().ToCSharpString().AsSpan(); + var lastDot = span.LastIndexOf('.'); + return Enum.Parse(lastDot < 0 ? span : span[(lastDot + 1)..]); } }