Skip to content

Commit

Permalink
clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
annelo-msft committed Aug 11, 2023
1 parent e22b678 commit eda6cd8
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ internal sealed partial class MutableJsonDocument
("MutableJsonElement.cs", @"
namespace Azure.Core.Json
{
internal sealed partial class MutableJsonElement
internal partial struct MutableJsonElement
{
}
}
Expand All @@ -51,26 +51,6 @@ public class Model
internal MutableJsonDocument {|AZC0020:Document|} => {|AZC0020:_document|};
internal event EventHandler<MutableJsonDocument> {|AZC0020:_docEvent|};
internal MutableJsonDocument {|AZC0020:GetDocument|}(MutableJsonDocument {|AZC0020:value|})
{
MutableJsonDocument mdoc = new MutableJsonDocument();
return mdoc;
}
}
}";
await Verifier.VerifyAnalyzerAsync(code, _sharedSourceFiles);
}

[Fact]
public async Task AZC0020ProducedForMutableJsonDocumentUsage2()
{
string code = @"
using Azure.Core.Json;
namespace LibraryNamespace
{
public class Model
{
internal MutableJsonDocument {|AZC0020:GetDocument|}(MutableJsonDocument {|AZC0020:value|})
{
{|AZC0020:MutableJsonDocument mdoc = new MutableJsonDocument();|}
Expand All @@ -91,7 +71,14 @@ namespace LibraryNamespace
{
public class Model
{
MutableJsonElement {|AZC0020:_element|};
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);
Expand All @@ -114,7 +101,7 @@ public class Model
}

[Fact]
public async Task AZC0020NotProducedForTypeWithBannedNameButAllowedNamespace()
public async Task AZC0020NotProducedForTypeWithBannedNameInAllowedNamespace()
{
string code = @"
namespace LibraryNamespace
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Diagnostics;
using System.Linq;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Diagnostics;

Expand Down Expand Up @@ -34,6 +35,13 @@ public sealed class BannedTypesAnalyzer : SymbolAnalyzerBase
SymbolKind.Property,
};

public override void Initialize(AnalysisContext context)
{
base.Initialize(context);

context.RegisterSyntaxNodeAction(c => AnalyzeNode(c), SyntaxKind.LocalDeclarationStatement);
}

public override void Analyze(ISymbolAnalysisContext context)
{
Debug.WriteLine($"{context.Symbol}");
Expand Down Expand Up @@ -86,18 +94,16 @@ public override void AnalyzeNode(SyntaxNodeAnalysisContext context)
return;
}

LocalDeclarationStatementSyntax declaration = context.Node as LocalDeclarationStatementSyntax;

TypeInfo info = context.SemanticModel.GetTypeInfo(declaration.Declaration.Type);

ISymbol symbol = info.Type;
ITypeSymbol type = info.Type;

if (type is INamedTypeSymbol namedTypeSymbol)
if (context.Node is LocalDeclarationStatementSyntax declaration)
{
if (IsBannedType(namedTypeSymbol))
ITypeSymbol type = context.SemanticModel.GetTypeInfo(declaration.Declaration.Type).Type;

if (type is INamedTypeSymbol namedTypeSymbol)
{
context.ReportDiagnostic(Diagnostic.Create(Descriptors.AZC0020, context.Node.GetLocation(), BannedTypesMessageArgs));
if (IsBannedType(namedTypeSymbol))
{
context.ReportDiagnostic(Diagnostic.Create(Descriptors.AZC0020, context.Node.GetLocation(), BannedTypesMessageArgs));
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// Licensed under the MIT License.

using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.Diagnostics;

namespace Azure.ClientSdk.Analyzers
Expand All @@ -19,13 +18,12 @@ public virtual void AnalyzeNode(SyntaxNodeAnalysisContext context) { }

protected INamedTypeSymbol ClientOptionsType { get; private set; }

public sealed override void Initialize(AnalysisContext context)
public override void Initialize(AnalysisContext context)
{
context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.Analyze | GeneratedCodeAnalysisFlags.ReportDiagnostics);
//context.EnableConcurrentExecution();
context.EnableConcurrentExecution();
context.RegisterCompilationStartAction(CompilationStart);
context.RegisterSymbolAction(c => Analyze(new RoslynSymbolAnalysisContext(c)), SymbolKinds);
context.RegisterSyntaxNodeAction(c => AnalyzeNode(c), SyntaxKind.LocalDeclarationStatement);
}

#pragma warning disable RS1012 // Start action has no registered actions.
Expand Down

0 comments on commit eda6cd8

Please sign in to comment.