Skip to content

Commit

Permalink
fixed tests
Browse files Browse the repository at this point in the history
  • Loading branch information
pamidur committed Sep 25, 2024
1 parent 810a194 commit 59aaaf3
Show file tree
Hide file tree
Showing 9 changed files with 165 additions and 144 deletions.
12 changes: 10 additions & 2 deletions aspects/Directory.Build.targets
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,16 @@
<Import Project="$(MSBuildThisFileDirectory)../src/AspectInjector/build/AspectInjector.targets" />

<ItemGroup>
<ProjectReference Include="$(MSBuildThisFileDirectory)..\src\AspectInjector\AspectInjector.csproj" PrivateAssets="None"/>
<ProjectReference Include="$(MSBuildThisFileDirectory)..\src\AspectInjector\AspectInjector.csproj" PrivateAssets="None" ReferenceOutputAssembly="false"/>
<ProjectReference Include="$(MSBuildThisFileDirectory)..\src\AspectInjector.Broker\AspectInjector.Broker.csproj" PrivateAssets="All"/>
</ItemGroup>
<!-- <ProjectReference Include="$(MSBuildThisFileDirectory)..\..\src\AspectInjector.Analyzer\AspectInjector.Analyzer.csproj">
<ReferenceOutputAssembly>false</ReferenceOutputAssembly>
<OutputItemType>Analyzer</OutputItemType>
</ProjectReference> -->
</ItemGroup>


<!-- <ItemGroup>
<Analyzer Include="$(MSBuildThisFileDirectory)..\..\src\AspectInjector.Analyzer\bin\Debug\netstandard2.0\AspectInjector.Analyzer.dll" />
</ItemGroup> -->
</Project>
86 changes: 44 additions & 42 deletions src/AspectInjector.Analyzer/Analyzers/AdviceAttributeAnalyzer.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
using AspectInjector.Broker;
using AspectInjector.Rules;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.Diagnostics;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.Diagnostics;
using System;
using System.Collections.Immutable;
using System.Linq;

namespace AspectInjector.Analyzer.Analyzers
{
[DiagnosticAnalyzer(LanguageNames.CSharp)]
public class AdviceAttributeAnalyzer : DiagnosticAnalyzer
{
using System.Collections.Immutable;
using System.Linq;

namespace AspectInjector.Analyzer.Analyzers
{
[DiagnosticAnalyzer(LanguageNames.CSharp)]
public class AdviceAttributeAnalyzer : DiagnosticAnalyzer
{
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics
=> ImmutableArray.Create(
EffectRules.EffectMustBePartOfAspect.AsDescriptor()
Expand All @@ -20,43 +20,45 @@ public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics
, GeneralRules.UnknownCompilationOption.AsDescriptor()
);

public override void Initialize(AnalysisContext context)
{
context.RegisterSyntaxNodeAction(AnalyzeAttribute, SyntaxKind.Attribute);
}

private static void AnalyzeAttribute(SyntaxNodeAnalysisContext context)
{
var attr = context.ContainingSymbol.GetAttributes().FirstOrDefault(a => a.ApplicationSyntaxReference.Span == context.Node.Span);

public override void Initialize(AnalysisContext context)
{
context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.Analyze | GeneratedCodeAnalysisFlags.ReportDiagnostics);
context.EnableConcurrentExecution();
context.RegisterSyntaxNodeAction(AnalyzeAttribute, SyntaxKind.Attribute);
}

private static void AnalyzeAttribute(SyntaxNodeAnalysisContext context)
{
var attr = context.ContainingSymbol.GetAttributes().FirstOrDefault(a => a.ApplicationSyntaxReference.Span == context.Node.Span);

if (attr == null || attr.AttributeClass.ToDisplayString() != WellKnown.AdviceType)
return;

return;

if (!(context.ContainingSymbol is IMethodSymbol method))
return;

var location = context.Node.GetLocation();

if (!method.ContainingSymbol.GetAttributes().Any(a => a.AttributeClass.ToDisplayString() == WellKnown.AspectType))
return;

var location = context.Node.GetLocation();

if (!method.ContainingSymbol.GetAttributes().Any(a => a.AttributeClass.ToDisplayString() == WellKnown.AspectType))
context.ReportDiagnostic(Diagnostic.Create(EffectRules.EffectMustBePartOfAspect.AsDescriptor(), location, method.ContainingSymbol.Name));

if (method.IsStatic)
if (method.IsStatic)
context.ReportDiagnostic(Diagnostic.Create(EffectRules.AdviceMustHaveValidSingnature.AsDescriptor(), location, method.Name, EffectRules.Literals.IsStatic));

if (method.IsGenericMethod)
if (method.IsGenericMethod)
context.ReportDiagnostic(Diagnostic.Create(EffectRules.AdviceMustHaveValidSingnature.AsDescriptor(), location, method.Name, EffectRules.Literals.IsGeneric));

if (method.DeclaredAccessibility != Accessibility.Public)
context.ReportDiagnostic(Diagnostic.Create(EffectRules.AdviceMustHaveValidSingnature.AsDescriptor(), location, method.Name, EffectRules.Literals.IsNotPublic));

foreach (var param in method.Parameters)
if (method.DeclaredAccessibility != Accessibility.Public)
context.ReportDiagnostic(Diagnostic.Create(EffectRules.AdviceMustHaveValidSingnature.AsDescriptor(), location, method.Name, EffectRules.Literals.IsNotPublic));

foreach (var param in method.Parameters)
if (!param.GetAttributes().Any(a => a.AttributeClass.ToDisplayString() == WellKnown.AdviceArgumentType))
context.ReportDiagnostic(Diagnostic.Create(EffectRules.AdviceArgumentMustBeBound.AsDescriptor(), param.Locations.First(), param.Name));

if (attr.AttributeConstructor == null)
return;

var kindArg = attr.ConstructorArguments[0].Value;
context.ReportDiagnostic(Diagnostic.Create(EffectRules.AdviceArgumentMustBeBound.AsDescriptor(), param.Locations.First(), param.Name));

if (attr.AttributeConstructor == null)
return;

var kindArg = attr.ConstructorArguments[0].Value;
if (kindArg != null)
{
var kind = (Kind)Enum.ToObject(typeof(Kind), kindArg);
Expand All @@ -83,6 +85,6 @@ private static void AnalyzeAttribute(SyntaxNodeAnalysisContext context)
if (t > Target.Any)
context.ReportDiagnostic(Diagnostic.Create(GeneralRules.UnknownCompilationOption.AsDescriptor(), location, GeneralRules.Literals.UnknownAdviceTarget(t.ToString())));
}
}
}
}
}
}
}
74 changes: 38 additions & 36 deletions src/AspectInjector.Analyzer/Analyzers/ArgumentAttributeAnalyzer.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
using AspectInjector.Broker;
using AspectInjector.Rules;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.Diagnostics;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.Diagnostics;
using System;
using System.Collections.Immutable;
using System.Linq;

namespace AspectInjector.Analyzer.Analyzers
{
[DiagnosticAnalyzer(LanguageNames.CSharp)]
public class ArgumentAttributeAnalyzer : DiagnosticAnalyzer
{
using System.Collections.Immutable;
using System.Linq;

namespace AspectInjector.Analyzer.Analyzers
{
[DiagnosticAnalyzer(LanguageNames.CSharp)]
public class ArgumentAttributeAnalyzer : DiagnosticAnalyzer
{
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics
=> ImmutableArray.Create(
EffectRules.ArgumentMustBePartOfAdvice.AsDescriptor()
Expand All @@ -20,32 +20,34 @@ public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics
, GeneralRules.UnknownCompilationOption.AsDescriptor()
);

public override void Initialize(AnalysisContext context)
{
context.RegisterSyntaxNodeAction(AnalyzeAttribute, SyntaxKind.Attribute);
}

private static void AnalyzeAttribute(SyntaxNodeAnalysisContext context)
{
if (!(context.SemanticModel.GetDeclaredSymbol(context.Node.Parent.Parent) is IParameterSymbol param))
return;

var attr = param.GetAttributes().FirstOrDefault(a => a.ApplicationSyntaxReference.Span == context.Node.Span);

if (attr == null || attr.AttributeClass.ToDisplayString() != WellKnown.AdviceArgumentType)
public override void Initialize(AnalysisContext context)
{
context.EnableConcurrentExecution();
context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.Analyze | GeneratedCodeAnalysisFlags.ReportDiagnostics);
context.RegisterSyntaxNodeAction(AnalyzeAttribute, SyntaxKind.Attribute);
}

private static void AnalyzeAttribute(SyntaxNodeAnalysisContext context)
{
if (!(context.SemanticModel.GetDeclaredSymbol(context.Node.Parent.Parent) is IParameterSymbol param))
return;

var attr = param.GetAttributes().FirstOrDefault(a => a.ApplicationSyntaxReference.Span == context.Node.Span);

if (attr == null || attr.AttributeClass.ToDisplayString() != WellKnown.AdviceArgumentType)
return;

var location = context.Node.GetLocation();

var adviceattr = param.ContainingSymbol.GetAttributes().FirstOrDefault(a => a.AttributeClass.ToDisplayString() == WellKnown.AdviceType);

if (adviceattr == null)
context.ReportDiagnostic(Diagnostic.Create(EffectRules.ArgumentMustBePartOfAdvice.AsDescriptor(), location, param.ContainingSymbol.Name));

if (attr.AttributeConstructor == null)
var location = context.Node.GetLocation();

var adviceattr = param.ContainingSymbol.GetAttributes().FirstOrDefault(a => a.AttributeClass.ToDisplayString() == WellKnown.AdviceType);

if (adviceattr == null)
context.ReportDiagnostic(Diagnostic.Create(EffectRules.ArgumentMustBePartOfAdvice.AsDescriptor(), location, param.ContainingSymbol.Name));

if (attr.AttributeConstructor == null)
return;

var sourceArg = attr.ConstructorArguments[0].Value;
var sourceArg = attr.ConstructorArguments[0].Value;
if (sourceArg != null)
{
var source = (Source)Enum.ToObject(typeof(Source), sourceArg);
Expand Down Expand Up @@ -109,6 +111,6 @@ private static void AnalyzeAttribute(SyntaxNodeAnalysisContext context)
context.ReportDiagnostic(Diagnostic.Create(EffectRules.ArgumentIsAlwaysNull.AsDescriptor(), location, param.Name, kind));
}
}
}
}
}
}
}
}
44 changes: 23 additions & 21 deletions src/AspectInjector.Analyzer/Analyzers/AspectAttributeAnalyzer.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
using AspectInjector.Broker;
using AspectInjector.Rules;
using AspectInjector.Broker;
using AspectInjector.Rules;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.Diagnostics;
using System;
using System;
using System.Collections.Immutable;
using System.Linq;

namespace AspectInjector.Analyzer.Analyzers
{
[DiagnosticAnalyzer(LanguageNames.CSharp)]
public class AspectAttributeAnalyzer : DiagnosticAnalyzer
{
{
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics =>
ImmutableArray.Create(
AspectRules.AspectMustHaveValidSignature.AsDescriptor()
Expand All @@ -23,6 +23,8 @@ public class AspectAttributeAnalyzer : DiagnosticAnalyzer

public override void Initialize(AnalysisContext context)
{
context.EnableConcurrentExecution();
context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.Analyze | GeneratedCodeAnalysisFlags.ReportDiagnostics);
context.RegisterSyntaxNodeAction(AnalyzeAttribute, SyntaxKind.Attribute);
}

Expand All @@ -35,26 +37,26 @@ private static void AnalyzeAttribute(SyntaxNodeAnalysisContext context)

var symbol = context.ContainingSymbol as INamedTypeSymbol;
if (symbol == null)
return;

return;

var factory = attr.NamedArguments.FirstOrDefault(n => n.Key == nameof(Aspect.Factory)).Value.Value;
var ctor = symbol.Constructors.FirstOrDefault(m => m.Parameters.IsEmpty);

var location = context.Node.GetLocation();

if (attr.AttributeConstructor != null)
{
var scopeArg = attr.ConstructorArguments[0].Value;
if (scopeArg != null)
{
var scope = (Scope)Enum.ToObject(typeof(Scope), scopeArg);

if (!Enum.IsDefined(typeof(Scope), scope))
context.ReportDiagnostic(Diagnostic.Create(GeneralRules.UnknownCompilationOption.AsDescriptor(), location, GeneralRules.Literals.UnknownAspectScope(scope.ToString())));
}
}


var location = context.Node.GetLocation();

if (attr.AttributeConstructor != null)
{
var scopeArg = attr.ConstructorArguments[0].Value;
if (scopeArg != null)
{
var scope = (Scope)Enum.ToObject(typeof(Scope), scopeArg);

if (!Enum.IsDefined(typeof(Scope), scope))
context.ReportDiagnostic(Diagnostic.Create(GeneralRules.UnknownCompilationOption.AsDescriptor(), location, GeneralRules.Literals.UnknownAspectScope(scope.ToString())));
}
}


if (symbol.IsStatic)
context.ReportDiagnostic(Diagnostic.Create(AspectRules.AspectMustHaveValidSignature.AsDescriptor(), location, symbol.Name, AspectRules.Literals.IsStatic));

Expand Down
48 changes: 25 additions & 23 deletions src/AspectInjector.Analyzer/Analyzers/InjectionAttributeAnalyzer.cs
Original file line number Diff line number Diff line change
@@ -1,24 +1,26 @@
using AspectInjector.Rules;
using AspectInjector.Rules;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.Diagnostics;
using System;
using System;
using System.Collections.Immutable;
using System.Linq;


namespace AspectInjector.Analyzer.Analyzers
{
[DiagnosticAnalyzer(LanguageNames.CSharp)]
public class InjectionAttributeAnalyzer : DiagnosticAnalyzer
{
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics
=> ImmutableArray.Create(
InjectionRules.InjectionMustReferToAspect.AsDescriptor()
, InjectionRules.InjectionMustBeAttribute.AsDescriptor()
);

public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics
=> ImmutableArray.Create(
InjectionRules.InjectionMustReferToAspect.AsDescriptor()
, InjectionRules.InjectionMustBeAttribute.AsDescriptor()
);

public override void Initialize(AnalysisContext context)
{
context.EnableConcurrentExecution();
context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.Analyze | GeneratedCodeAnalysisFlags.ReportDiagnostics);
context.RegisterSyntaxNodeAction(AnalyzeAttribute, SyntaxKind.Attribute);
}

Expand All @@ -33,24 +35,24 @@ private static void AnalyzeAttribute(SyntaxNodeAnalysisContext context)

var compilation = context.SemanticModel.Compilation;

var attributeType = compilation.GetTypeByMetadataName(typeof(Attribute).FullName);

if (context.ContainingSymbol is ITypeSymbol type)
{
var attributeType = compilation.GetTypeByMetadataName(typeof(Attribute).FullName);

if (context.ContainingSymbol is ITypeSymbol type)
{
if (type.TypeKind != TypeKind.Interface && !compilation.ClassifyConversion(type, attributeType).IsImplicit)
context.ReportDiagnostic(Diagnostic.Create(InjectionRules.InjectionMustBeAttribute.AsDescriptor(), location, context.ContainingSymbol.Name));
context.ReportDiagnostic(Diagnostic.Create(InjectionRules.InjectionMustBeAttribute.AsDescriptor(), location, context.ContainingSymbol.Name));
}

if (attr.AttributeConstructor == null)
return;

if (attr.ConstructorArguments[0].Value is INamedTypeSymbol arg)
{
if (arg.TypeKind == TypeKind.Error)
return;

if (!arg.GetAttributes().Any(a => a.AttributeClass.ToDisplayString() == WellKnown.AspectType))
context.ReportDiagnostic(Diagnostic.Create(InjectionRules.InjectionMustReferToAspect.AsDescriptor(), location, arg.Name));
return;

if (attr.ConstructorArguments[0].Value is INamedTypeSymbol arg)
{
if (arg.TypeKind == TypeKind.Error)
return;

if (!arg.GetAttributes().Any(a => a.AttributeClass.ToDisplayString() == WellKnown.AspectType))
context.ReportDiagnostic(Diagnostic.Create(InjectionRules.InjectionMustReferToAspect.AsDescriptor(), location, arg.Name));
}
}
}
Expand Down
Loading

0 comments on commit 59aaaf3

Please sign in to comment.