-
Notifications
You must be signed in to change notification settings - Fork 470
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Seal internal private types analyzer
- Loading branch information
1 parent
5b0b341
commit f6061f1
Showing
20 changed files
with
791 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/Runtime/SealInternalTypes.Fixer.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the MIT license. See License.txt in the project root for license information. | ||
|
||
using System.Collections.Immutable; | ||
using System.Composition; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.CodeActions; | ||
using Microsoft.CodeAnalysis.CodeFixes; | ||
using Microsoft.CodeAnalysis.Editing; | ||
|
||
namespace Microsoft.NetCore.Analyzers.Runtime | ||
{ | ||
[ExportCodeFixProvider(LanguageNames.CSharp, LanguageNames.VisualBasic), Shared] | ||
public sealed class SealInternalTypesFixer : CodeFixProvider | ||
{ | ||
public override Task RegisterCodeFixesAsync(CodeFixContext context) | ||
{ | ||
var codeAction = CodeAction.Create( | ||
MicrosoftNetCoreAnalyzersResources.SealInternalTypesCodeFixTitle, | ||
SealClassDeclarationsAsync, | ||
nameof(MicrosoftNetCoreAnalyzersResources.SealInternalTypesCodeFixTitle)); | ||
context.RegisterCodeFix(codeAction, context.Diagnostics); | ||
return Task.CompletedTask; | ||
|
||
// Local functions | ||
|
||
async Task<Solution> SealClassDeclarationsAsync(CancellationToken token) | ||
{ | ||
var solutionEditor = new SolutionEditor(context.Document.Project.Solution); | ||
await SealDeclarationAt(solutionEditor, context.Diagnostics[0].Location, token).ConfigureAwait(false); | ||
|
||
foreach (var location in context.Diagnostics[0].AdditionalLocations) | ||
await SealDeclarationAt(solutionEditor, location, token).ConfigureAwait(false); | ||
|
||
return solutionEditor.GetChangedSolution(); | ||
} | ||
|
||
static async Task SealDeclarationAt(SolutionEditor solutionEditor, Location location, CancellationToken token) | ||
{ | ||
var solution = solutionEditor.OriginalSolution; | ||
var document = solution.GetDocument(location.SourceTree); | ||
|
||
if (document is null) | ||
return; | ||
|
||
var documentEditor = await solutionEditor.GetDocumentEditorAsync(document.Id, token).ConfigureAwait(false); | ||
var root = await document.GetSyntaxRootAsync(token).ConfigureAwait(false); | ||
var declaration = root.FindNode(location.SourceSpan); | ||
var newModifiers = documentEditor.Generator.GetModifiers(declaration).WithIsSealed(true); | ||
var newDeclaration = documentEditor.Generator.WithModifiers(declaration, newModifiers); | ||
documentEditor.ReplaceNode(declaration, newDeclaration); | ||
} | ||
} | ||
|
||
public override ImmutableArray<string> FixableDiagnosticIds { get; } = ImmutableArray.Create(SealInternalTypes.RuleId); | ||
|
||
public override FixAllProvider GetFixAllProvider() => WellKnownFixAllProviders.BatchFixer; | ||
} | ||
} |
69 changes: 69 additions & 0 deletions
69
src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/Runtime/SealInternalTypes.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the MIT license. See License.txt in the project root for license information. | ||
|
||
using System.Collections.Immutable; | ||
using Analyzer.Utilities; | ||
using Analyzer.Utilities.Extensions; | ||
using Analyzer.Utilities.PooledObjects; | ||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.Diagnostics; | ||
using Resx = Microsoft.NetCore.Analyzers.MicrosoftNetCoreAnalyzersResources; | ||
|
||
namespace Microsoft.NetCore.Analyzers.Runtime | ||
{ | ||
[DiagnosticAnalyzer(LanguageNames.CSharp, LanguageNames.VisualBasic)] | ||
public sealed class SealInternalTypes : DiagnosticAnalyzer | ||
{ | ||
internal const string RuleId = "CA1852"; | ||
internal static readonly DiagnosticDescriptor Rule = DiagnosticDescriptorHelper.Create( | ||
RuleId, | ||
Resx.CreateLocalizableResourceString(nameof(Resx.SealInternalTypesTitle)), | ||
Resx.CreateLocalizableResourceString(nameof(Resx.SealInternalTypesMessage)), | ||
DiagnosticCategory.Performance, | ||
RuleLevel.IdeHidden_BulkConfigurable, | ||
Resx.CreateLocalizableResourceString(nameof(Resx.SealInternalTypesDescription)), | ||
isPortedFxCopRule: false, | ||
isDataflowRule: false, | ||
isReportedAtCompilationEnd: true); | ||
|
||
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics { get; } = ImmutableArray.Create(Rule); | ||
|
||
public override void Initialize(AnalysisContext context) | ||
{ | ||
context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None); | ||
context.EnableConcurrentExecution(); | ||
context.RegisterCompilationStartAction(OnCompilationStart); | ||
} | ||
|
||
private static void OnCompilationStart(CompilationStartAnalysisContext context) | ||
{ | ||
var candidateTypes = PooledConcurrentSet<INamedTypeSymbol>.GetInstance(SymbolEqualityComparer.Default); | ||
var baseTypes = PooledConcurrentSet<INamedTypeSymbol>.GetInstance(SymbolEqualityComparer.Default); | ||
|
||
context.RegisterSymbolAction(context => | ||
{ | ||
var type = (INamedTypeSymbol)context.Symbol; | ||
|
||
if (type.TypeKind is TypeKind.Class && !type.IsAbstract && !type.IsStatic && !type.IsSealed && !type.IsExternallyVisible()) | ||
candidateTypes.Add(type); | ||
|
||
for (var baseType = type.BaseType; baseType is not null; baseType = baseType.BaseType) | ||
baseTypes.Add(baseType.OriginalDefinition); | ||
|
||
}, SymbolKind.NamedType); | ||
|
||
context.RegisterCompilationEndAction(context => | ||
{ | ||
foreach (var type in candidateTypes) | ||
{ | ||
if (!baseTypes.Contains(type.OriginalDefinition)) | ||
{ | ||
context.ReportDiagnostic(type.CreateDiagnostic(Rule, type.Name)); | ||
} | ||
} | ||
|
||
candidateTypes.Dispose(); | ||
baseTypes.Dispose(); | ||
}); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.