-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
…-type Gh 38 substitute for internal type - code fix provider
- Loading branch information
Showing
54 changed files
with
2,507 additions
and
125 deletions.
There are no files selected for viewing
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
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
43 changes: 43 additions & 0 deletions
43
...ubstitute.Analyzers.CSharp/CodeFixProviders/SubstituteForInternalMemberCodeFixProvider.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 |
---|---|---|
@@ -0,0 +1,43 @@ | ||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.CodeFixes; | ||
using Microsoft.CodeAnalysis.CSharp; | ||
using Microsoft.CodeAnalysis.CSharp.Syntax; | ||
using NSubstitute.Analyzers.CSharp.DiagnosticAnalyzers; | ||
using NSubstitute.Analyzers.Shared.CodeFixProviders; | ||
using NSubstitute.Analyzers.Shared.DiagnosticAnalyzers; | ||
using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory; | ||
|
||
namespace NSubstitute.Analyzers.CSharp.CodeFixProviders | ||
{ | ||
[ExportCodeFixProvider(LanguageNames.CSharp)] | ||
internal class SubstituteForInternalMemberCodeFixProvider : AbstractSubstituteForInternalMemberCodeFixProvider<InvocationExpressionSyntax, ExpressionSyntax, CompilationUnitSyntax> | ||
{ | ||
protected override AbstractSubstituteProxyAnalysis<InvocationExpressionSyntax, ExpressionSyntax> GetSubstituteProxyAnalysis() | ||
{ | ||
return new SubstituteProxyAnalysis(); | ||
} | ||
|
||
protected override CompilationUnitSyntax AppendInternalsVisibleToAttribute(CompilationUnitSyntax compilationUnitSyntax) | ||
{ | ||
return compilationUnitSyntax.AddAttributeLists( | ||
AttributeList( | ||
AttributeTargetSpecifier( | ||
Token(SyntaxKind.AssemblyKeyword)), | ||
SingletonSeparatedList( | ||
Attribute( | ||
QualifiedName( | ||
QualifiedName( | ||
QualifiedName( | ||
IdentifierName("System"), | ||
IdentifierName("Runtime")), | ||
IdentifierName("CompilerServices")), | ||
IdentifierName("InternalsVisibleTo")), | ||
AttributeArgumentList( | ||
SingletonSeparatedList( | ||
AttributeArgument( | ||
LiteralExpression( | ||
SyntaxKind.StringLiteralExpression, | ||
Literal("DynamicProxyGenAssembly2"))))))))); | ||
} | ||
} | ||
} |
82 changes: 82 additions & 0 deletions
82
...e.Analyzers.Shared/CodeFixProviders/AbstractSubstituteForInternalMemberCodeFixProvider.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 |
---|---|---|
@@ -0,0 +1,82 @@ | ||
using System.Collections.Immutable; | ||
using System.Linq; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.CodeActions; | ||
using Microsoft.CodeAnalysis.CodeFixes; | ||
using NSubstitute.Analyzers.Shared.DiagnosticAnalyzers; | ||
|
||
namespace NSubstitute.Analyzers.Shared.CodeFixProviders | ||
{ | ||
internal abstract class AbstractSubstituteForInternalMemberCodeFixProvider<TInvocationExpressionSyntax, TExpressionSyntax, TCompilationUnitSyntax> : AbstractSuppressDiagnosticsCodeFixProvider | ||
where TInvocationExpressionSyntax : SyntaxNode | ||
where TExpressionSyntax : SyntaxNode | ||
where TCompilationUnitSyntax : SyntaxNode | ||
{ | ||
public override ImmutableArray<string> FixableDiagnosticIds { get; } = ImmutableArray.Create(DiagnosticIdentifiers.SubstituteForInternalMember); | ||
|
||
public override async Task RegisterCodeFixesAsync(CodeFixContext context) | ||
{ | ||
var diagnostic = context.Diagnostics.FirstOrDefault(diag => diag.Descriptor.Id == DiagnosticIdentifiers.SubstituteForInternalMember); | ||
if (diagnostic == null) | ||
{ | ||
return; | ||
} | ||
|
||
var root = await context.Document.GetSyntaxRootAsync(context.CancellationToken).ConfigureAwait(false); | ||
|
||
var findNode = root.FindNode(diagnostic.Location.SourceSpan, getInnermostNodeForTie: true); | ||
if (!(findNode is TInvocationExpressionSyntax invocationExpression)) | ||
{ | ||
return; | ||
} | ||
|
||
var syntaxReference = await GetDeclaringSyntaxReference(context, invocationExpression); | ||
|
||
if (syntaxReference == null) | ||
{ | ||
return; | ||
} | ||
|
||
var syntaxNode = await syntaxReference.GetSyntaxAsync(); | ||
var document = context.Document.Project.Solution.GetDocument(syntaxNode.SyntaxTree); | ||
var compilationUnitSyntax = FindCompilationUnitSyntax(syntaxNode); | ||
|
||
if (compilationUnitSyntax == null) | ||
{ | ||
return; | ||
} | ||
|
||
var codeAction = CodeAction.Create("Add InternalsVisibleTo attribute", token => CreateChangedDocument(token, compilationUnitSyntax, document), nameof(AbstractSubstituteForInternalMemberCodeFixProvider<TInvocationExpressionSyntax, TExpressionSyntax, TCompilationUnitSyntax>)); | ||
context.RegisterCodeFix(codeAction, diagnostic); | ||
} | ||
|
||
protected abstract AbstractSubstituteProxyAnalysis<TInvocationExpressionSyntax, TExpressionSyntax> GetSubstituteProxyAnalysis(); | ||
|
||
protected abstract TCompilationUnitSyntax AppendInternalsVisibleToAttribute(TCompilationUnitSyntax compilationUnitSyntax); | ||
|
||
private async Task<Document> CreateChangedDocument(CancellationToken cancellationToken, TCompilationUnitSyntax compilationUnitSyntax, Document document) | ||
{ | ||
var updatedCompilationUnitSyntax = AppendInternalsVisibleToAttribute(compilationUnitSyntax); | ||
var root = await document.GetSyntaxRootAsync(cancellationToken); | ||
var replaceNode = root.ReplaceNode(compilationUnitSyntax, updatedCompilationUnitSyntax); | ||
return document.WithSyntaxRoot(replaceNode); | ||
} | ||
|
||
private async Task<SyntaxReference> GetDeclaringSyntaxReference(CodeFixContext context, TInvocationExpressionSyntax invocationExpression) | ||
{ | ||
var semanticModel = await context.Document.GetSemanticModelAsync(context.CancellationToken); | ||
var methodSymbol = semanticModel.GetSymbolInfo(invocationExpression).Symbol as IMethodSymbol; | ||
var proxyAnalysis = GetSubstituteProxyAnalysis(); | ||
var actualProxyTypeSymbol = proxyAnalysis.GetActualProxyTypeSymbol(semanticModel, invocationExpression, methodSymbol); | ||
var syntaxReference = actualProxyTypeSymbol.DeclaringSyntaxReferences.FirstOrDefault(); | ||
return syntaxReference; | ||
} | ||
|
||
private TCompilationUnitSyntax FindCompilationUnitSyntax(SyntaxNode syntaxNode) | ||
{ | ||
return syntaxNode.Parent.Ancestors().OfType<TCompilationUnitSyntax>().LastOrDefault(); | ||
} | ||
} | ||
} |
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
39 changes: 39 additions & 0 deletions
39
...tute.Analyzers.VisualBasic/CodeFixProviders/SubstituteForInternalMemberCodeFixProvider.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 |
---|---|---|
@@ -0,0 +1,39 @@ | ||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.CodeFixes; | ||
using Microsoft.CodeAnalysis.VisualBasic; | ||
using Microsoft.CodeAnalysis.VisualBasic.Syntax; | ||
using NSubstitute.Analyzers.Shared.CodeFixProviders; | ||
using NSubstitute.Analyzers.Shared.DiagnosticAnalyzers; | ||
using NSubstitute.Analyzers.VisualBasic.DiagnosticAnalyzers; | ||
using static Microsoft.CodeAnalysis.VisualBasic.SyntaxFactory; | ||
|
||
namespace NSubstitute.Analyzers.VisualBasic.CodeFixProviders | ||
{ | ||
[ExportCodeFixProvider(LanguageNames.VisualBasic)] | ||
internal class SubstituteForInternalMemberCodeFixProvider : AbstractSubstituteForInternalMemberCodeFixProvider<InvocationExpressionSyntax, ExpressionSyntax, CompilationUnitSyntax> | ||
{ | ||
protected override AbstractSubstituteProxyAnalysis<InvocationExpressionSyntax, ExpressionSyntax> GetSubstituteProxyAnalysis() | ||
{ | ||
return new SubstituteProxyAnalysis(); | ||
} | ||
|
||
protected override CompilationUnitSyntax AppendInternalsVisibleToAttribute(CompilationUnitSyntax compilationUnitSyntax) | ||
{ | ||
return compilationUnitSyntax.AddAttributes( | ||
AttributesStatement(SingletonList( | ||
AttributeList(SingletonSeparatedList(Attribute( | ||
AttributeTarget(Token(SyntaxKind.AssemblyKeyword)), | ||
QualifiedName( | ||
QualifiedName( | ||
QualifiedName( | ||
IdentifierName("System"), | ||
IdentifierName("Runtime")), | ||
IdentifierName("CompilerServices")), | ||
IdentifierName("InternalsVisibleTo")), | ||
ArgumentList(SingletonSeparatedList<ArgumentSyntax>(SimpleArgument( | ||
LiteralExpression( | ||
SyntaxKind.StringLiteralExpression, | ||
Literal("DynamicProxyGenAssembly2"))))))))))); | ||
} | ||
} | ||
} |
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.