CSLA.NET 6.0.0
CSLA .NET is a software development framework that helps you build a reusable, maintainable object-oriented business layer for your app.
FindOperationsWithIncorrectReturnTypeResolveCorrectTypeCodeFix.cs
Go to the documentation of this file.
1using System.Collections.Immutable;
2using System.Composition;
3using System.Linq;
4using System.Threading.Tasks;
5using Microsoft.CodeAnalysis;
6using Microsoft.CodeAnalysis.CodeFixes;
7using Microsoft.CodeAnalysis.CSharp.Syntax;
8using Microsoft.CodeAnalysis.CSharp;
9using Microsoft.CodeAnalysis.CodeActions;
11
12namespace Csla.Analyzers
13{
14 [ExportCodeFixProvider(LanguageNames.CSharp)]
15 [Shared]
17 : CodeFixProvider
18 {
19 public override ImmutableArray<string> FixableDiagnosticIds => ImmutableArray.Create(Constants.AnalyzerIdentifiers.FindOperationsWithIncorrectReturnTypes);
20
21 public sealed override FixAllProvider GetFixAllProvider() => WellKnownFixAllProviders.BatchFixer;
22
23 public override async Task RegisterCodeFixesAsync(CodeFixContext context)
24 {
25 var root = await context.Document.GetSyntaxRootAsync(context.CancellationToken).ConfigureAwait(false);
26
27 context.CancellationToken.ThrowIfCancellationRequested();
28
29 var diagnostic = context.Diagnostics.First();
30 var methodNode = root.FindNode(diagnostic.Location.SourceSpan) as MethodDeclarationSyntax;
31
32 context.CancellationToken.ThrowIfCancellationRequested();
33
34 await AddCodeFixAsync(context, root, diagnostic, methodNode);
35 }
36
37 private static async Task AddCodeFixAsync(CodeFixContext context, SyntaxNode root,
38 Diagnostic diagnostic, MethodDeclarationSyntax methodNode)
39 {
40 var model = await context.Document.GetSemanticModelAsync(context.CancellationToken);
41 var methodSymbol = model.GetDeclaredSymbol(methodNode);
42
43 if(methodSymbol.IsAsync)
44 {
45 var newRoot = root.ReplaceNode(methodNode.ReturnType,
46 SyntaxFactory.IdentifierName(typeof(Task).Name));
47
48 if (!root.HasUsing(FindOperationsWithIncorrectReturnTypeResolveCorrectTypeCodeFixConstants.SystemThreadingTasksNamespace))
49 {
50 newRoot = (newRoot as CompilationUnitSyntax).AddUsings(
51 SyntaxFactory.UsingDirective(SyntaxFactory.ParseName(
52 FindOperationsWithIncorrectReturnTypeResolveCorrectTypeCodeFixConstants.SystemThreadingTasksNamespace)));
53 }
54
55 context.RegisterCodeFix(
56 CodeAction.Create(
57 FindOperationsWithIncorrectReturnTypeResolveCorrectTypeCodeFixConstants.ChangeReturnTypeToTaskDescription,
58 _ => Task.FromResult(context.Document.WithSyntaxRoot(newRoot)),
59 FindOperationsWithIncorrectReturnTypeResolveCorrectTypeCodeFixConstants.ChangeReturnTypeToTaskDescription), diagnostic);
60 }
61 else
62 {
63 var newRoot = root.ReplaceNode(methodNode.ReturnType,
64 SyntaxFactory.PredefinedType(SyntaxFactory.Token(SyntaxKind.VoidKeyword)));
65 context.RegisterCodeFix(
66 CodeAction.Create(
67 FindOperationsWithIncorrectReturnTypeResolveCorrectTypeCodeFixConstants.ChangeReturnTypeToVoidDescription,
68 _ => Task.FromResult(context.Document.WithSyntaxRoot(newRoot)),
69 FindOperationsWithIncorrectReturnTypeResolveCorrectTypeCodeFixConstants.ChangeReturnTypeToVoidDescription), diagnostic);
70 }
71 }
72 }
73}