Skip to content
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

Add IsFileLocal lightup, use it in CA1708 #7190

Merged
merged 10 commits into from
Feb 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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