Skip to content

Commit

Permalink
improve the performacne of checking sdk namespace
Browse files Browse the repository at this point in the history
  • Loading branch information
Mingzhe Huang (from Dev Box) committed Sep 14, 2023
1 parent 8c78b84 commit 5d28699
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 37 deletions.
Original file line number Diff line number Diff line change
@@ -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
{
Expand All @@ -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);
Expand All @@ -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<string> 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<string> GetNamespace(SyntaxNode node)
{
var namespaces = new List<string>();

Expand Down Expand Up @@ -83,8 +63,7 @@ parent is not NamespaceDeclarationSyntax
}
}


return string.Join(".", namespaces.Reverse<string>());
return namespaces;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<string> GetAllNamespaces(INamespaceSymbol namespaceSymbol)
internal static IReadOnlyList<string> GetAllNamespaces(this INamespaceSymbol namespaceSymbol)
{
if (namespaceSymbol is { ContainingNamespace: not null and { IsGlobalNamespace: false } })
var namespaces = new List<string>();
while (namespaceSymbol is { IsGlobalNamespace: false })
{
var namespaces = GetAllNamespaces(namespaceSymbol.ContainingNamespace);
namespaces.Add(namespaceSymbol.Name);
return namespaces;
namespaceSymbol = namespaceSymbol.ContainingNamespace;
}

return new List<string> { namespaceSymbol.Name };

namespaces.Reverse();
return namespaces;
}
}
}

0 comments on commit 5d28699

Please sign in to comment.