diff --git a/src/dotnet/Azure.ClientSdk.Analyzers/Azure.ClientSdk.Analyzers/AnalyzerUtils.cs b/src/dotnet/Azure.ClientSdk.Analyzers/Azure.ClientSdk.Analyzers/AnalyzerUtils.cs index 2428a2c098d..528399fe894 100644 --- a/src/dotnet/Azure.ClientSdk.Analyzers/Azure.ClientSdk.Analyzers/AnalyzerUtils.cs +++ b/src/dotnet/Azure.ClientSdk.Analyzers/Azure.ClientSdk.Analyzers/AnalyzerUtils.cs @@ -1,11 +1,9 @@ // Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT License. +using System.Collections.Generic; using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.CSharp.Syntax; -using System; -using System.Collections.Generic; -using System.Linq; namespace Azure.ClientSdk.Analyzers { @@ -15,9 +13,9 @@ internal class AnalyzerUtils internal static bool IsSdkCode(ISymbol symbol) { - var ns = symbol.ContainingNamespace.GetFullNamespaceName(); + var namespaces = symbol.ContainingNamespace.GetAllNamespaces(); - return IsSdkNamespace(ns); + return IsSdkNamespace(namespaces); } internal static bool IsNotSdkCode(SyntaxNode node, SemanticModel model) => !IsSdkCode(node, model); @@ -30,31 +28,13 @@ internal static bool IsSdkCode(SyntaxNode node, SemanticModel model) return IsSdkCode(symbol); } - var ns = GetNamespace(node); - return IsSdkNamespace(ns); + var namespaces = GetNamespace(node); + return IsSdkNamespace(namespaces); } - private static bool IsSdkNamespace(string ns) - { - var namespaces = ns.AsSpan(); - // if the namespace contains only one level, it's not SDK namespace - var indexOfFirstDot = namespaces.IndexOf('.'); - if (indexOfFirstDot == -1) - return false; - - // first namespace must be `Azure` - var firstNamespace = namespaces.Slice(0, indexOfFirstDot); - if (!firstNamespace.Equals("Azure".AsSpan(), StringComparison.Ordinal)) - return false; + private static bool IsSdkNamespace(IReadOnlyList namespaces) => namespaces.Count >= 2 && namespaces[0] == "Azure" && namespaces[1] != "Core"; - // second namespace must not be `Core` - var remainingNamespace = namespaces.Slice(indexOfFirstDot + 1); - var indexOfSecondDot = remainingNamespace.IndexOf('.'); - var seondNamespace = (indexOfSecondDot == -1 ? remainingNamespace : remainingNamespace.Slice(0, indexOfSecondDot)); - return !seondNamespace.Equals("Core".AsSpan(), StringComparison.Ordinal); - } - - private static string GetNamespace(SyntaxNode node) + private static IReadOnlyList GetNamespace(SyntaxNode node) { var namespaces = new List(); @@ -83,8 +63,7 @@ parent is not NamespaceDeclarationSyntax } } - - return string.Join(".", namespaces.Reverse()); + return namespaces; } } } diff --git a/src/dotnet/Azure.ClientSdk.Analyzers/Azure.ClientSdk.Analyzers/INameSpaceSymbolExtensions.cs b/src/dotnet/Azure.ClientSdk.Analyzers/Azure.ClientSdk.Analyzers/INameSpaceSymbolExtensions.cs index c61a1bacd65..b40e621eb27 100644 --- a/src/dotnet/Azure.ClientSdk.Analyzers/Azure.ClientSdk.Analyzers/INameSpaceSymbolExtensions.cs +++ b/src/dotnet/Azure.ClientSdk.Analyzers/Azure.ClientSdk.Analyzers/INameSpaceSymbolExtensions.cs @@ -10,20 +10,19 @@ public static class INamespaceSymbolExtensions { public static string GetFullNamespaceName(this INamespaceSymbol namespaceSymbol) { - return string.Join(".", GetAllNamespaces(namespaceSymbol)); + return string.Join(".", namespaceSymbol.GetAllNamespaces()); } - private static IList GetAllNamespaces(INamespaceSymbol namespaceSymbol) + internal static IReadOnlyList GetAllNamespaces(this INamespaceSymbol namespaceSymbol) { - if (namespaceSymbol is { ContainingNamespace: not null and { IsGlobalNamespace: false } }) + var namespaces = new List(); + while (namespaceSymbol is { IsGlobalNamespace: false }) { - var namespaces = GetAllNamespaces(namespaceSymbol.ContainingNamespace); namespaces.Add(namespaceSymbol.Name); - return namespaces; + namespaceSymbol = namespaceSymbol.ContainingNamespace; } - - return new List { namespaceSymbol.Name }; - + namespaces.Reverse(); + return namespaces; } } }