Skip to content

Commit

Permalink
Merge pull request #7190 from RenderMichael/main
Browse files Browse the repository at this point in the history
Add IsFileLocal lightup, use it in `CA1708`
  • Loading branch information
sharwell authored Feb 17, 2024
2 parents 90721b0 + 92830ba commit ab13ac7
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the MIT license. See License.txt in the project root for license information.
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the MIT license. See License.txt in the project root for license information.

using System;
using System.Collections.Generic;
Expand Down Expand Up @@ -54,7 +54,8 @@ private static void AnalyzeCompilation(CompilationAnalysisContext context)

IEnumerable<INamedTypeSymbol> globalTypes = context.Compilation.GlobalNamespace.GetTypeMembers().Where(item =>
Equals(item.ContainingAssembly, context.Compilation.Assembly) &&
MatchesConfiguredVisibility(item, context.Options, context.Compilation));
MatchesConfiguredVisibility(item, context.Options, context.Compilation) &&
!item.IsFileLocal());

CheckTypeNames(globalTypes, context);
CheckNamespaceMembers(globalNamespaces, context);
Expand Down Expand Up @@ -97,7 +98,8 @@ private static void CheckNamespaceMembers(IEnumerable<INamespaceSymbol> namespac
// Get all the potentially externally visible types in the namespace
IEnumerable<INamedTypeSymbol> typeMembers = @namespace.GetTypeMembers().Where(item =>
Equals(item.ContainingAssembly, context.Compilation.Assembly) &&
MatchesConfiguredVisibility(item, context.Options, context.Compilation));
MatchesConfiguredVisibility(item, context.Options, context.Compilation) &&
!item.IsFileLocal());

if (typeMembers.Any())
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the MIT license. See License.txt in the project root for license information.
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the MIT license. See License.txt in the project root for license information.

using System;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.Testing;
using Test.Utilities;
using Xunit;
using VerifyCS = Test.Utilities.CSharpCodeFixVerifier<
Microsoft.CodeQuality.Analyzers.ApiDesignGuidelines.IdentifiersShouldDifferByMoreThanCaseAnalyzer,
Expand Down Expand Up @@ -142,6 +143,56 @@ public partial class F
}.RunAsync();
}

[Fact, WorkItem(6514, "https://github.com/dotnet/roslyn-analyzers/issues/6514")]
public async Task FileScopedTypesInNamespaceAsync()
{
string fileWithClass = """
namespace N;
file class C
{
}
""";

await new VerifyCS.Test
{
TestState =
{
Sources =
{
fileWithClass,
fileWithClass
}
},
LanguageVersion = CodeAnalysis.CSharp.LanguageVersion.CSharp11,
ReferenceAssemblies = ReferenceAssemblies.Net.Net70
}.RunAsync();
}

[Fact, WorkItem(6514, "https://github.com/dotnet/roslyn-analyzers/issues/6514")]
public async Task FileScopedTypesGlobalAsync()
{
string fileWithClass = """
file class C
{
}
""";

await new VerifyCS.Test
{
TestState =
{
Sources =
{
fileWithClass,
fileWithClass
}
},
LanguageVersion = CodeAnalysis.CSharp.LanguageVersion.CSharp11,
ReferenceAssemblies = ReferenceAssemblies.Net.Net70
}.RunAsync();
}

#endregion

#region Type Level
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,18 @@
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using Analyzer.Utilities.Lightup;
using Microsoft.CodeAnalysis;

namespace Analyzer.Utilities.Extensions
{
internal static class INamedTypeSymbolExtensions
{

private static readonly Func<INamedTypeSymbol, bool> s_isFileLocal = LightupHelpers.CreateSymbolPropertyAccessor<INamedTypeSymbol, bool>(typeof(INamedTypeSymbol), nameof(IsFileLocal), fallbackResult: false);

public static bool IsFileLocal(this INamedTypeSymbol symbol) => s_isFileLocal(symbol);

public static IEnumerable<INamedTypeSymbol> GetBaseTypesAndThis(this INamedTypeSymbol type)
{
INamedTypeSymbol current = type;
Expand Down

0 comments on commit ab13ac7

Please sign in to comment.