Skip to content

Commit 59aaaf3

Browse files
committed
fixed tests
1 parent 810a194 commit 59aaaf3

File tree

9 files changed

+165
-144
lines changed

9 files changed

+165
-144
lines changed

aspects/Directory.Build.targets

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,16 @@
1111
<Import Project="$(MSBuildThisFileDirectory)../src/AspectInjector/build/AspectInjector.targets" />
1212

1313
<ItemGroup>
14-
<ProjectReference Include="$(MSBuildThisFileDirectory)..\src\AspectInjector\AspectInjector.csproj" PrivateAssets="None"/>
14+
<ProjectReference Include="$(MSBuildThisFileDirectory)..\src\AspectInjector\AspectInjector.csproj" PrivateAssets="None" ReferenceOutputAssembly="false"/>
1515
<ProjectReference Include="$(MSBuildThisFileDirectory)..\src\AspectInjector.Broker\AspectInjector.Broker.csproj" PrivateAssets="All"/>
16-
</ItemGroup>
16+
<!-- <ProjectReference Include="$(MSBuildThisFileDirectory)..\..\src\AspectInjector.Analyzer\AspectInjector.Analyzer.csproj">
17+
<ReferenceOutputAssembly>false</ReferenceOutputAssembly>
18+
<OutputItemType>Analyzer</OutputItemType>
19+
</ProjectReference> -->
20+
</ItemGroup>
1721

22+
23+
<!-- <ItemGroup>
24+
<Analyzer Include="$(MSBuildThisFileDirectory)..\..\src\AspectInjector.Analyzer\bin\Debug\netstandard2.0\AspectInjector.Analyzer.dll" />
25+
</ItemGroup> -->
1826
</Project>
Lines changed: 44 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
using AspectInjector.Broker;
22
using AspectInjector.Rules;
3-
using Microsoft.CodeAnalysis;
4-
using Microsoft.CodeAnalysis.CSharp;
5-
using Microsoft.CodeAnalysis.Diagnostics;
3+
using Microsoft.CodeAnalysis;
4+
using Microsoft.CodeAnalysis.CSharp;
5+
using Microsoft.CodeAnalysis.Diagnostics;
66
using System;
7-
using System.Collections.Immutable;
8-
using System.Linq;
9-
10-
namespace AspectInjector.Analyzer.Analyzers
11-
{
12-
[DiagnosticAnalyzer(LanguageNames.CSharp)]
13-
public class AdviceAttributeAnalyzer : DiagnosticAnalyzer
14-
{
7+
using System.Collections.Immutable;
8+
using System.Linq;
9+
10+
namespace AspectInjector.Analyzer.Analyzers
11+
{
12+
[DiagnosticAnalyzer(LanguageNames.CSharp)]
13+
public class AdviceAttributeAnalyzer : DiagnosticAnalyzer
14+
{
1515
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics
1616
=> ImmutableArray.Create(
1717
EffectRules.EffectMustBePartOfAspect.AsDescriptor()
@@ -20,43 +20,45 @@ public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics
2020
, GeneralRules.UnknownCompilationOption.AsDescriptor()
2121
);
2222

23-
public override void Initialize(AnalysisContext context)
24-
{
25-
context.RegisterSyntaxNodeAction(AnalyzeAttribute, SyntaxKind.Attribute);
26-
}
27-
28-
private static void AnalyzeAttribute(SyntaxNodeAnalysisContext context)
29-
{
30-
var attr = context.ContainingSymbol.GetAttributes().FirstOrDefault(a => a.ApplicationSyntaxReference.Span == context.Node.Span);
31-
23+
public override void Initialize(AnalysisContext context)
24+
{
25+
context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.Analyze | GeneratedCodeAnalysisFlags.ReportDiagnostics);
26+
context.EnableConcurrentExecution();
27+
context.RegisterSyntaxNodeAction(AnalyzeAttribute, SyntaxKind.Attribute);
28+
}
29+
30+
private static void AnalyzeAttribute(SyntaxNodeAnalysisContext context)
31+
{
32+
var attr = context.ContainingSymbol.GetAttributes().FirstOrDefault(a => a.ApplicationSyntaxReference.Span == context.Node.Span);
33+
3234
if (attr == null || attr.AttributeClass.ToDisplayString() != WellKnown.AdviceType)
33-
return;
34-
35+
return;
36+
3537
if (!(context.ContainingSymbol is IMethodSymbol method))
36-
return;
37-
38-
var location = context.Node.GetLocation();
39-
40-
if (!method.ContainingSymbol.GetAttributes().Any(a => a.AttributeClass.ToDisplayString() == WellKnown.AspectType))
38+
return;
39+
40+
var location = context.Node.GetLocation();
41+
42+
if (!method.ContainingSymbol.GetAttributes().Any(a => a.AttributeClass.ToDisplayString() == WellKnown.AspectType))
4143
context.ReportDiagnostic(Diagnostic.Create(EffectRules.EffectMustBePartOfAspect.AsDescriptor(), location, method.ContainingSymbol.Name));
4244

43-
if (method.IsStatic)
45+
if (method.IsStatic)
4446
context.ReportDiagnostic(Diagnostic.Create(EffectRules.AdviceMustHaveValidSingnature.AsDescriptor(), location, method.Name, EffectRules.Literals.IsStatic));
4547

46-
if (method.IsGenericMethod)
48+
if (method.IsGenericMethod)
4749
context.ReportDiagnostic(Diagnostic.Create(EffectRules.AdviceMustHaveValidSingnature.AsDescriptor(), location, method.Name, EffectRules.Literals.IsGeneric));
4850

49-
if (method.DeclaredAccessibility != Accessibility.Public)
50-
context.ReportDiagnostic(Diagnostic.Create(EffectRules.AdviceMustHaveValidSingnature.AsDescriptor(), location, method.Name, EffectRules.Literals.IsNotPublic));
51-
52-
foreach (var param in method.Parameters)
51+
if (method.DeclaredAccessibility != Accessibility.Public)
52+
context.ReportDiagnostic(Diagnostic.Create(EffectRules.AdviceMustHaveValidSingnature.AsDescriptor(), location, method.Name, EffectRules.Literals.IsNotPublic));
53+
54+
foreach (var param in method.Parameters)
5355
if (!param.GetAttributes().Any(a => a.AttributeClass.ToDisplayString() == WellKnown.AdviceArgumentType))
54-
context.ReportDiagnostic(Diagnostic.Create(EffectRules.AdviceArgumentMustBeBound.AsDescriptor(), param.Locations.First(), param.Name));
55-
56-
if (attr.AttributeConstructor == null)
57-
return;
58-
59-
var kindArg = attr.ConstructorArguments[0].Value;
56+
context.ReportDiagnostic(Diagnostic.Create(EffectRules.AdviceArgumentMustBeBound.AsDescriptor(), param.Locations.First(), param.Name));
57+
58+
if (attr.AttributeConstructor == null)
59+
return;
60+
61+
var kindArg = attr.ConstructorArguments[0].Value;
6062
if (kindArg != null)
6163
{
6264
var kind = (Kind)Enum.ToObject(typeof(Kind), kindArg);
@@ -83,6 +85,6 @@ private static void AnalyzeAttribute(SyntaxNodeAnalysisContext context)
8385
if (t > Target.Any)
8486
context.ReportDiagnostic(Diagnostic.Create(GeneralRules.UnknownCompilationOption.AsDescriptor(), location, GeneralRules.Literals.UnknownAdviceTarget(t.ToString())));
8587
}
86-
}
87-
}
88-
}
88+
}
89+
}
90+
}

src/AspectInjector.Analyzer/Analyzers/ArgumentAttributeAnalyzer.cs

Lines changed: 38 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
using AspectInjector.Broker;
22
using AspectInjector.Rules;
3-
using Microsoft.CodeAnalysis;
4-
using Microsoft.CodeAnalysis.CSharp;
5-
using Microsoft.CodeAnalysis.Diagnostics;
3+
using Microsoft.CodeAnalysis;
4+
using Microsoft.CodeAnalysis.CSharp;
5+
using Microsoft.CodeAnalysis.Diagnostics;
66
using System;
7-
using System.Collections.Immutable;
8-
using System.Linq;
9-
10-
namespace AspectInjector.Analyzer.Analyzers
11-
{
12-
[DiagnosticAnalyzer(LanguageNames.CSharp)]
13-
public class ArgumentAttributeAnalyzer : DiagnosticAnalyzer
14-
{
7+
using System.Collections.Immutable;
8+
using System.Linq;
9+
10+
namespace AspectInjector.Analyzer.Analyzers
11+
{
12+
[DiagnosticAnalyzer(LanguageNames.CSharp)]
13+
public class ArgumentAttributeAnalyzer : DiagnosticAnalyzer
14+
{
1515
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics
1616
=> ImmutableArray.Create(
1717
EffectRules.ArgumentMustBePartOfAdvice.AsDescriptor()
@@ -20,32 +20,34 @@ public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics
2020
, GeneralRules.UnknownCompilationOption.AsDescriptor()
2121
);
2222

23-
public override void Initialize(AnalysisContext context)
24-
{
25-
context.RegisterSyntaxNodeAction(AnalyzeAttribute, SyntaxKind.Attribute);
26-
}
27-
28-
private static void AnalyzeAttribute(SyntaxNodeAnalysisContext context)
29-
{
30-
if (!(context.SemanticModel.GetDeclaredSymbol(context.Node.Parent.Parent) is IParameterSymbol param))
31-
return;
32-
33-
var attr = param.GetAttributes().FirstOrDefault(a => a.ApplicationSyntaxReference.Span == context.Node.Span);
34-
35-
if (attr == null || attr.AttributeClass.ToDisplayString() != WellKnown.AdviceArgumentType)
23+
public override void Initialize(AnalysisContext context)
24+
{
25+
context.EnableConcurrentExecution();
26+
context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.Analyze | GeneratedCodeAnalysisFlags.ReportDiagnostics);
27+
context.RegisterSyntaxNodeAction(AnalyzeAttribute, SyntaxKind.Attribute);
28+
}
29+
30+
private static void AnalyzeAttribute(SyntaxNodeAnalysisContext context)
31+
{
32+
if (!(context.SemanticModel.GetDeclaredSymbol(context.Node.Parent.Parent) is IParameterSymbol param))
33+
return;
34+
35+
var attr = param.GetAttributes().FirstOrDefault(a => a.ApplicationSyntaxReference.Span == context.Node.Span);
36+
37+
if (attr == null || attr.AttributeClass.ToDisplayString() != WellKnown.AdviceArgumentType)
3638
return;
3739

38-
var location = context.Node.GetLocation();
39-
40-
var adviceattr = param.ContainingSymbol.GetAttributes().FirstOrDefault(a => a.AttributeClass.ToDisplayString() == WellKnown.AdviceType);
41-
42-
if (adviceattr == null)
43-
context.ReportDiagnostic(Diagnostic.Create(EffectRules.ArgumentMustBePartOfAdvice.AsDescriptor(), location, param.ContainingSymbol.Name));
44-
45-
if (attr.AttributeConstructor == null)
40+
var location = context.Node.GetLocation();
41+
42+
var adviceattr = param.ContainingSymbol.GetAttributes().FirstOrDefault(a => a.AttributeClass.ToDisplayString() == WellKnown.AdviceType);
43+
44+
if (adviceattr == null)
45+
context.ReportDiagnostic(Diagnostic.Create(EffectRules.ArgumentMustBePartOfAdvice.AsDescriptor(), location, param.ContainingSymbol.Name));
46+
47+
if (attr.AttributeConstructor == null)
4648
return;
4749

48-
var sourceArg = attr.ConstructorArguments[0].Value;
50+
var sourceArg = attr.ConstructorArguments[0].Value;
4951
if (sourceArg != null)
5052
{
5153
var source = (Source)Enum.ToObject(typeof(Source), sourceArg);
@@ -109,6 +111,6 @@ private static void AnalyzeAttribute(SyntaxNodeAnalysisContext context)
109111
context.ReportDiagnostic(Diagnostic.Create(EffectRules.ArgumentIsAlwaysNull.AsDescriptor(), location, param.Name, kind));
110112
}
111113
}
112-
}
113-
}
114-
}
114+
}
115+
}
116+
}

src/AspectInjector.Analyzer/Analyzers/AspectAttributeAnalyzer.cs

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
1-
using AspectInjector.Broker;
2-
using AspectInjector.Rules;
1+
using AspectInjector.Broker;
2+
using AspectInjector.Rules;
33
using Microsoft.CodeAnalysis;
44
using Microsoft.CodeAnalysis.CSharp;
55
using Microsoft.CodeAnalysis.Diagnostics;
6-
using System;
6+
using System;
77
using System.Collections.Immutable;
88
using System.Linq;
99

1010
namespace AspectInjector.Analyzer.Analyzers
1111
{
1212
[DiagnosticAnalyzer(LanguageNames.CSharp)]
1313
public class AspectAttributeAnalyzer : DiagnosticAnalyzer
14-
{
14+
{
1515
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics =>
1616
ImmutableArray.Create(
1717
AspectRules.AspectMustHaveValidSignature.AsDescriptor()
@@ -23,6 +23,8 @@ public class AspectAttributeAnalyzer : DiagnosticAnalyzer
2323

2424
public override void Initialize(AnalysisContext context)
2525
{
26+
context.EnableConcurrentExecution();
27+
context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.Analyze | GeneratedCodeAnalysisFlags.ReportDiagnostics);
2628
context.RegisterSyntaxNodeAction(AnalyzeAttribute, SyntaxKind.Attribute);
2729
}
2830

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

3638
var symbol = context.ContainingSymbol as INamedTypeSymbol;
3739
if (symbol == null)
38-
return;
39-
40+
return;
41+
4042
var factory = attr.NamedArguments.FirstOrDefault(n => n.Key == nameof(Aspect.Factory)).Value.Value;
4143
var ctor = symbol.Constructors.FirstOrDefault(m => m.Parameters.IsEmpty);
4244

43-
var location = context.Node.GetLocation();
44-
45-
if (attr.AttributeConstructor != null)
46-
{
47-
var scopeArg = attr.ConstructorArguments[0].Value;
48-
if (scopeArg != null)
49-
{
50-
var scope = (Scope)Enum.ToObject(typeof(Scope), scopeArg);
51-
52-
if (!Enum.IsDefined(typeof(Scope), scope))
53-
context.ReportDiagnostic(Diagnostic.Create(GeneralRules.UnknownCompilationOption.AsDescriptor(), location, GeneralRules.Literals.UnknownAspectScope(scope.ToString())));
54-
}
55-
}
56-
57-
45+
var location = context.Node.GetLocation();
46+
47+
if (attr.AttributeConstructor != null)
48+
{
49+
var scopeArg = attr.ConstructorArguments[0].Value;
50+
if (scopeArg != null)
51+
{
52+
var scope = (Scope)Enum.ToObject(typeof(Scope), scopeArg);
53+
54+
if (!Enum.IsDefined(typeof(Scope), scope))
55+
context.ReportDiagnostic(Diagnostic.Create(GeneralRules.UnknownCompilationOption.AsDescriptor(), location, GeneralRules.Literals.UnknownAspectScope(scope.ToString())));
56+
}
57+
}
58+
59+
5860
if (symbol.IsStatic)
5961
context.ReportDiagnostic(Diagnostic.Create(AspectRules.AspectMustHaveValidSignature.AsDescriptor(), location, symbol.Name, AspectRules.Literals.IsStatic));
6062

src/AspectInjector.Analyzer/Analyzers/InjectionAttributeAnalyzer.cs

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,26 @@
1-
using AspectInjector.Rules;
1+
using AspectInjector.Rules;
22
using Microsoft.CodeAnalysis;
33
using Microsoft.CodeAnalysis.CSharp;
44
using Microsoft.CodeAnalysis.Diagnostics;
5-
using System;
5+
using System;
66
using System.Collections.Immutable;
77
using System.Linq;
8-
8+
99
namespace AspectInjector.Analyzer.Analyzers
1010
{
1111
[DiagnosticAnalyzer(LanguageNames.CSharp)]
1212
public class InjectionAttributeAnalyzer : DiagnosticAnalyzer
1313
{
14-
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics
15-
=> ImmutableArray.Create(
16-
InjectionRules.InjectionMustReferToAspect.AsDescriptor()
17-
, InjectionRules.InjectionMustBeAttribute.AsDescriptor()
18-
);
19-
14+
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics
15+
=> ImmutableArray.Create(
16+
InjectionRules.InjectionMustReferToAspect.AsDescriptor()
17+
, InjectionRules.InjectionMustBeAttribute.AsDescriptor()
18+
);
19+
2020
public override void Initialize(AnalysisContext context)
2121
{
22+
context.EnableConcurrentExecution();
23+
context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.Analyze | GeneratedCodeAnalysisFlags.ReportDiagnostics);
2224
context.RegisterSyntaxNodeAction(AnalyzeAttribute, SyntaxKind.Attribute);
2325
}
2426

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

3436
var compilation = context.SemanticModel.Compilation;
3537

36-
var attributeType = compilation.GetTypeByMetadataName(typeof(Attribute).FullName);
37-
38-
if (context.ContainingSymbol is ITypeSymbol type)
39-
{
38+
var attributeType = compilation.GetTypeByMetadataName(typeof(Attribute).FullName);
39+
40+
if (context.ContainingSymbol is ITypeSymbol type)
41+
{
4042
if (type.TypeKind != TypeKind.Interface && !compilation.ClassifyConversion(type, attributeType).IsImplicit)
41-
context.ReportDiagnostic(Diagnostic.Create(InjectionRules.InjectionMustBeAttribute.AsDescriptor(), location, context.ContainingSymbol.Name));
43+
context.ReportDiagnostic(Diagnostic.Create(InjectionRules.InjectionMustBeAttribute.AsDescriptor(), location, context.ContainingSymbol.Name));
4244
}
4345

4446
if (attr.AttributeConstructor == null)
45-
return;
46-
47-
if (attr.ConstructorArguments[0].Value is INamedTypeSymbol arg)
48-
{
49-
if (arg.TypeKind == TypeKind.Error)
50-
return;
51-
52-
if (!arg.GetAttributes().Any(a => a.AttributeClass.ToDisplayString() == WellKnown.AspectType))
53-
context.ReportDiagnostic(Diagnostic.Create(InjectionRules.InjectionMustReferToAspect.AsDescriptor(), location, arg.Name));
47+
return;
48+
49+
if (attr.ConstructorArguments[0].Value is INamedTypeSymbol arg)
50+
{
51+
if (arg.TypeKind == TypeKind.Error)
52+
return;
53+
54+
if (!arg.GetAttributes().Any(a => a.AttributeClass.ToDisplayString() == WellKnown.AspectType))
55+
context.ReportDiagnostic(Diagnostic.Create(InjectionRules.InjectionMustReferToAspect.AsDescriptor(), location, arg.Name));
5456
}
5557
}
5658
}

0 commit comments

Comments
 (0)