-
Notifications
You must be signed in to change notification settings - Fork 464
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Instantiate ArgumentExceptions Correctly analyzer updates and fixer i…
…mplementation (#3500) * ArgumentException analyzer updates and fixer implementation
- Loading branch information
Showing
20 changed files
with
752 additions
and
136 deletions.
There are no files selected for viewing
17 changes: 0 additions & 17 deletions
17
...Microsoft.NetCore.Analyzers/Runtime/CSharpInstantiateArgumentExceptionsCorrectly.Fixer.cs
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
109 changes: 101 additions & 8 deletions
109
.../Core/Microsoft.NetCore.Analyzers/Runtime/InstantiateArgumentExceptionsCorrectly.Fixer.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,122 @@ | ||
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using System.Collections.Immutable; | ||
using System.Composition; | ||
using System.Linq; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using System.Diagnostics; | ||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.CodeActions; | ||
using Microsoft.CodeAnalysis.CodeFixes; | ||
using Microsoft.CodeAnalysis.Editing; | ||
using Microsoft.CodeAnalysis.Operations; | ||
|
||
namespace Microsoft.NetCore.Analyzers.Runtime | ||
{ | ||
/// <summary> | ||
/// CA2208: Instantiate argument exceptions correctly | ||
/// </summary> | ||
public abstract class InstantiateArgumentExceptionsCorrectlyFixer : CodeFixProvider | ||
[ExportCodeFixProvider(LanguageNames.CSharp, LanguageNames.VisualBasic), Shared] | ||
public sealed class InstantiateArgumentExceptionsCorrectlyFixer : CodeFixProvider | ||
{ | ||
public sealed override ImmutableArray<string> FixableDiagnosticIds => ImmutableArray<string>.Empty; | ||
public sealed override ImmutableArray<string> FixableDiagnosticIds => ImmutableArray.Create(InstantiateArgumentExceptionsCorrectlyAnalyzer.RuleId); | ||
|
||
public sealed override FixAllProvider GetFixAllProvider() | ||
public sealed override FixAllProvider GetFixAllProvider() => WellKnownFixAllProviders.BatchFixer; | ||
|
||
public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context) | ||
{ | ||
// See https://github.com/dotnet/roslyn/blob/master/docs/analyzers/FixAllProvider.md for more information on Fix All Providers | ||
return WellKnownFixAllProviders.BatchFixer; | ||
var diagnostic = context.Diagnostics.First(); | ||
string paramPositionString = diagnostic.Properties.GetValueOrDefault(InstantiateArgumentExceptionsCorrectlyAnalyzer.MessagePosition); | ||
if (paramPositionString != null) | ||
{ | ||
SyntaxNode root = await context.Document.GetSyntaxRootAsync(context.CancellationToken).ConfigureAwait(false); | ||
SyntaxNode node = root.FindNode(context.Span, getInnermostNodeForTie: true); | ||
if (node != null) | ||
{ | ||
await PopulateCodeFixAsync(context, diagnostic, paramPositionString, node).ConfigureAwait(false); | ||
} | ||
} | ||
} | ||
|
||
public sealed override Task RegisterCodeFixesAsync(CodeFixContext context) | ||
private static async Task PopulateCodeFixAsync(CodeFixContext context, Diagnostic diagnostic, string paramPositionString, SyntaxNode node) | ||
{ | ||
// Fixer not yet implemented. | ||
return Task.CompletedTask; | ||
SemanticModel model = await context.Document.GetSemanticModelAsync(context.CancellationToken).ConfigureAwait(false); | ||
var operation = model.GetOperation(node, context.CancellationToken); | ||
if (operation is IObjectCreationOperation creation) | ||
{ | ||
if (int.TryParse(paramPositionString, out int paramPosition)) | ||
{ | ||
CodeAction? codeAction = null; | ||
if (creation.Arguments.Length == 1) | ||
{ | ||
// Add null message | ||
codeAction = CodeAction.Create( | ||
title: MicrosoftNetCoreAnalyzersResources.InstantiateArgumentExceptionsCorrectlyChangeToTwoArgumentCodeFixTitle, | ||
createChangedDocument: c => AddNullMessageToArgumentListAsync(context.Document, creation, c), | ||
equivalenceKey: MicrosoftNetCoreAnalyzersResources.InstantiateArgumentExceptionsCorrectlyChangeToTwoArgumentCodeFixTitle); | ||
} | ||
else | ||
{ | ||
// Swap message and paramete name | ||
codeAction = CodeAction.Create( | ||
title: MicrosoftNetCoreAnalyzersResources.InstantiateArgumentExceptionsCorrectlyFlipArgumentOrderCodeFixTitle, | ||
createChangedDocument: c => SwapArgumentsOrderAsync(context.Document, creation, paramPosition, creation.Arguments.Length, c), | ||
equivalenceKey: MicrosoftNetCoreAnalyzersResources.InstantiateArgumentExceptionsCorrectlyFlipArgumentOrderCodeFixTitle); | ||
} | ||
context.RegisterCodeFix(codeAction, diagnostic); | ||
} | ||
} | ||
} | ||
|
||
private static async Task<Document> SwapArgumentsOrderAsync(Document document, IObjectCreationOperation creation, int paramPosition, int argumentCount, CancellationToken token) | ||
{ | ||
DocumentEditor editor = await DocumentEditor.CreateAsync(document, token).ConfigureAwait(false); | ||
SyntaxNode parameter = AddNameOfIfLiteral(creation.Arguments[paramPosition].Value, editor.Generator); | ||
SyntaxNode newCreation; | ||
if (argumentCount == 2) | ||
{ | ||
if (paramPosition == 0) | ||
{ | ||
newCreation = editor.Generator.ObjectCreationExpression(creation.Type, creation.Arguments[1].Syntax, parameter); | ||
} | ||
else | ||
{ | ||
newCreation = editor.Generator.ObjectCreationExpression(creation.Type, parameter, creation.Arguments[0].Syntax); | ||
} | ||
} | ||
else | ||
{ | ||
Debug.Assert(argumentCount == 3); | ||
if (paramPosition == 0) | ||
{ | ||
newCreation = editor.Generator.ObjectCreationExpression(creation.Type, creation.Arguments[1].Syntax, parameter, creation.Arguments[2].Syntax); | ||
} | ||
else | ||
{ | ||
newCreation = editor.Generator.ObjectCreationExpression(creation.Type, parameter, creation.Arguments[1].Syntax, creation.Arguments[0].Syntax); | ||
} | ||
} | ||
editor.ReplaceNode(creation.Syntax, newCreation); | ||
return editor.GetChangedDocument(); | ||
} | ||
|
||
private static async Task<Document> AddNullMessageToArgumentListAsync(Document document, IObjectCreationOperation creation, CancellationToken token) | ||
{ | ||
DocumentEditor editor = await DocumentEditor.CreateAsync(document, token).ConfigureAwait(false); | ||
SyntaxNode argument = AddNameOfIfLiteral(creation.Arguments[0].Value, editor.Generator); | ||
SyntaxNode newCreation = editor.Generator.ObjectCreationExpression(creation.Type, editor.Generator.Argument(editor.Generator.NullLiteralExpression()), argument); | ||
editor.ReplaceNode(creation.Syntax, newCreation); | ||
return editor.GetChangedDocument(); | ||
} | ||
|
||
private static SyntaxNode AddNameOfIfLiteral(IOperation expression, SyntaxGenerator generator) | ||
{ | ||
if (expression is ILiteralOperation literal) | ||
{ | ||
return generator.NameOfExpression(generator.IdentifierName(literal.ConstantValue.Value.ToString())); | ||
} | ||
return expression.Syntax; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.