-
Notifications
You must be signed in to change notification settings - Fork 227
Add an analyzer to prevent use of some internal shared source types #6642
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
17ccf43
Initial implementation of AZC0020
annelo-msft c44a581
Updates
annelo-msft 45252a5
Update
annelo-msft e322220
refactor
annelo-msft 010a6b4
whitespace
annelo-msft 7749725
updates
annelo-msft 36599b0
Merge remote-tracking branch 'upstream/main' into analyzer-internal-mjd
annelo-msft 28792b4
Allow use of shared source types in Azure.Core
annelo-msft b16ca41
pr fb
annelo-msft a5b7810
pr fb
annelo-msft 3cb7a68
Merge remote-tracking branch 'upstream/main' into analyzer-internal-mjd
annelo-msft ad55b23
add back change to file missed in merge
annelo-msft c6d2f22
Update tests
annelo-msft e22b678
pr fb; + WIP for local variables
annelo-msft eda6cd8
clean up
annelo-msft 2a7c508
missed cleanup
annelo-msft bc5bc51
missed file
annelo-msft ce00057
Address warnings
annelo-msft 1453cbe
Updates
annelo-msft 1b7eb5a
refactor
annelo-msft File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
120 changes: 120 additions & 0 deletions
120
src/dotnet/Azure.ClientSdk.Analyzers/Azure.ClientSdk.Analyzers.Tests/AZC0020Tests.cs
This file contains hidden or 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,120 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT License. | ||
|
|
||
| using System.Collections.Generic; | ||
| using System.Threading.Tasks; | ||
| using Xunit; | ||
| using Verifier = Azure.ClientSdk.Analyzers.Tests.AzureAnalyzerVerifier<Azure.ClientSdk.Analyzers.BannedTypesAnalyzer>; | ||
|
|
||
| namespace Azure.ClientSdk.Analyzers.Tests | ||
| { | ||
| public class AZC0020Tests | ||
| { | ||
| private List<(string fileName, string source)> _sharedSourceFiles; | ||
|
|
||
| public AZC0020Tests() | ||
| { | ||
| _sharedSourceFiles = new List<(string fileName, string source)>() { | ||
|
|
||
| ("MutableJsonDocument.cs", @" | ||
| namespace Azure.Core.Json | ||
| { | ||
| internal sealed partial class MutableJsonDocument | ||
| { | ||
| } | ||
| } | ||
| "), | ||
|
|
||
| ("MutableJsonElement.cs", @" | ||
| namespace Azure.Core.Json | ||
| { | ||
| internal partial struct MutableJsonElement | ||
| { | ||
| } | ||
| } | ||
| ") | ||
| }; | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task AZC0020ProducedForMutableJsonDocumentUsage() | ||
| { | ||
| string code = @" | ||
| using System; | ||
| using Azure.Core.Json; | ||
|
|
||
| namespace LibraryNamespace | ||
| { | ||
| public class Model | ||
| { | ||
| private MutableJsonDocument {|AZC0020:_document|}; | ||
| internal MutableJsonDocument {|AZC0020:Document|} => {|AZC0020:_document|}; | ||
| internal event EventHandler<MutableJsonDocument> {|AZC0020:_docEvent|}; | ||
|
|
||
| internal MutableJsonDocument {|AZC0020:GetDocument|}(MutableJsonDocument {|AZC0020:value|}) | ||
| { | ||
| {|AZC0020:MutableJsonDocument mdoc = new MutableJsonDocument();|} | ||
| return mdoc; | ||
| } | ||
| } | ||
| }"; | ||
| await Verifier.VerifyAnalyzerAsync(code, _sharedSourceFiles); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task AZC0020ProducedForMutableJsonElementUsage() | ||
| { | ||
| string code = @" | ||
| using Azure.Core.Json; | ||
|
|
||
| namespace LibraryNamespace | ||
| { | ||
| public class Model | ||
| { | ||
| private MutableJsonElement {|AZC0020:_element|}; | ||
| internal MutableJsonElement {|AZC0020:Element|} => {|AZC0020:_element|}; | ||
|
|
||
| internal MutableJsonElement {|AZC0020:GetDocument|}(MutableJsonElement {|AZC0020:value|}) | ||
| { | ||
| {|AZC0020:MutableJsonElement element = new MutableJsonElement();|} | ||
| return element; | ||
| } | ||
| } | ||
| }"; | ||
| await Verifier.VerifyAnalyzerAsync(code, _sharedSourceFiles); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task AZC0020NotProducedForAllowedTypeUsage() | ||
| { | ||
| string code = @" | ||
| using System.Text.Json; | ||
|
|
||
| namespace LibraryNamespace | ||
| { | ||
| public class Model | ||
| { | ||
| JsonElement _element; | ||
| } | ||
| }"; | ||
| await Verifier.VerifyAnalyzerAsync(code, _sharedSourceFiles); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task AZC0020NotProducedForTypeWithBannedNameInAllowedNamespace() | ||
| { | ||
| string code = @" | ||
| namespace LibraryNamespace | ||
| { | ||
| public class MutableJsonDocument | ||
| { | ||
| } | ||
| public class Model | ||
| { | ||
| MutableJsonDocument _document; | ||
| } | ||
| }"; | ||
| await Verifier.VerifyAnalyzerAsync(code, _sharedSourceFiles); | ||
| } | ||
| } | ||
| } |
This file contains hidden or 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
127 changes: 127 additions & 0 deletions
127
src/dotnet/Azure.ClientSdk.Analyzers/Azure.ClientSdk.Analyzers/BannedTypesAnalyzer.cs
This file contains hidden or 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,127 @@ | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Collections.Immutable; | ||
| using System.Linq; | ||
| using Microsoft.CodeAnalysis; | ||
| using Microsoft.CodeAnalysis.CSharp; | ||
| using Microsoft.CodeAnalysis.CSharp.Syntax; | ||
| using Microsoft.CodeAnalysis.Diagnostics; | ||
|
|
||
| namespace Azure.ClientSdk.Analyzers | ||
| { | ||
| [DiagnosticAnalyzer(LanguageNames.CSharp)] | ||
| public sealed class BannedTypesAnalyzer : DiagnosticAnalyzer | ||
| { | ||
| private static HashSet<string> BannedTypes = new HashSet<string>() | ||
| { | ||
| "Azure.Core.Json.MutableJsonDocument", | ||
| "Azure.Core.Json.MutableJsonElement", | ||
| "Azure.Core.Json.MutableJsonChange", | ||
| "Azure.Core.Json.MutableJsonChangeKind", | ||
| }; | ||
|
|
||
| private static readonly string BannedTypesMessageArgs = string.Join(", ", BannedTypes); | ||
|
|
||
| public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics { get; } = ImmutableArray.Create(Descriptors.AZC0020); | ||
|
|
||
| public SymbolKind[] SymbolKinds { get; } = new[] | ||
| { | ||
| SymbolKind.Event, | ||
| SymbolKind.Field, | ||
| SymbolKind.Method, | ||
| SymbolKind.NamedType, | ||
| SymbolKind.Parameter, | ||
| SymbolKind.Property, | ||
| }; | ||
|
|
||
| public override void Initialize(AnalysisContext context) | ||
| { | ||
| context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.Analyze | GeneratedCodeAnalysisFlags.ReportDiagnostics); | ||
| context.EnableConcurrentExecution(); | ||
| context.RegisterSymbolAction(c => Analyze(c), SymbolKinds); | ||
| context.RegisterSyntaxNodeAction(c => AnalyzeNode(c), SyntaxKind.LocalDeclarationStatement); | ||
| } | ||
|
|
||
| public void Analyze(SymbolAnalysisContext context) | ||
| { | ||
| if (IsAzureCore(context.Symbol.ContainingAssembly)) | ||
| { | ||
| return; | ||
| } | ||
|
|
||
| switch (context.Symbol) | ||
| { | ||
| case IParameterSymbol parameterSymbol: | ||
| CheckType(parameterSymbol.Type, parameterSymbol, context.ReportDiagnostic); | ||
| break; | ||
| case IMethodSymbol methodSymbol: | ||
| CheckType(methodSymbol.ReturnType, methodSymbol, context.ReportDiagnostic); | ||
| break; | ||
| case IEventSymbol eventSymbol: | ||
| CheckType(eventSymbol.Type, eventSymbol, context.ReportDiagnostic); | ||
| break; | ||
| case IPropertySymbol propertySymbol: | ||
| CheckType(propertySymbol.Type, propertySymbol, context.ReportDiagnostic); | ||
| break; | ||
| case IFieldSymbol fieldSymbol: | ||
| CheckType(fieldSymbol.Type, fieldSymbol, context.ReportDiagnostic); | ||
| break; | ||
| case INamedTypeSymbol namedTypeSymbol: | ||
| CheckType(namedTypeSymbol.BaseType, namedTypeSymbol, context.ReportDiagnostic); | ||
| foreach (var iface in namedTypeSymbol.Interfaces) | ||
| { | ||
| CheckType(iface, namedTypeSymbol, context.ReportDiagnostic); | ||
| } | ||
| break; | ||
| } | ||
| } | ||
|
|
||
| public void AnalyzeNode(SyntaxNodeAnalysisContext context) | ||
| { | ||
| if (IsAzureCore(context.ContainingSymbol.ContainingAssembly)) | ||
| { | ||
| return; | ||
| } | ||
|
|
||
| if (context.Node is LocalDeclarationStatementSyntax declaration) | ||
| { | ||
| ITypeSymbol type = context.SemanticModel.GetTypeInfo(declaration.Declaration.Type).Type; | ||
|
|
||
| CheckType(type, type, context.ReportDiagnostic, context.Node.GetLocation()); | ||
| } | ||
| } | ||
|
|
||
| private static Diagnostic CheckType(ITypeSymbol type, ISymbol symbol, Action<Diagnostic> reportDiagnostic, Location location = default) | ||
| { | ||
| if (type is INamedTypeSymbol namedTypeSymbol) | ||
| { | ||
| if (IsBannedType(namedTypeSymbol)) | ||
| { | ||
| reportDiagnostic(Diagnostic.Create(Descriptors.AZC0020, location ?? symbol.Locations.First(), BannedTypesMessageArgs)); | ||
| } | ||
|
|
||
| if (namedTypeSymbol.IsGenericType) | ||
| { | ||
| foreach (var typeArgument in namedTypeSymbol.TypeArguments) | ||
| { | ||
| CheckType(typeArgument, symbol, reportDiagnostic); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return null; | ||
| } | ||
|
|
||
| private static bool IsAzureCore(IAssemblySymbol assembly) | ||
| { | ||
| return | ||
| assembly.Name.Equals("Azure.Core") || | ||
| assembly.Name.Equals("Azure.Core.Experimental"); | ||
| } | ||
|
|
||
| private static bool IsBannedType(INamedTypeSymbol namedTypeSymbol) | ||
| { | ||
| return BannedTypes.Contains($"{namedTypeSymbol.ContainingNamespace}.{namedTypeSymbol.Name}"); | ||
| } | ||
| } | ||
| } |
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.