Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion TUnit.Analyzers/ClassParametersAnalyzer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down
18 changes: 10 additions & 8 deletions TUnit.Analyzers/Extensions/AttributeExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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;
}

Expand All @@ -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;
}
Expand All @@ -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;
}
Expand Down Expand Up @@ -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)
{
Expand All @@ -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;
Expand All @@ -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<HookLevel>(lastDot < 0 ? span : span[(lastDot + 1)..]);
}
}
Loading