-
Notifications
You must be signed in to change notification settings - Fork 480
Add MakeTypesInternalAnalyzer #6820
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
Merged
Merged
Changes from 7 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
3c09ae3
Add MakeTypesInternalAnalyzer
CollinAlpert 8607e16
Add FixAll support
CollinAlpert ef03d12
Add header comment.
CollinAlpert 7244561
Merge branch 'master' into issue_78388
CollinAlpert e2ecef9
Add file header
CollinAlpert 59c2ace
Merge branch 'master' into issue_78388
CollinAlpert fa70d87
Address PR feedback
CollinAlpert bc210a0
Merge branch 'master' into issue_78388
CollinAlpert 075c130
Applied code review suggestions.
CollinAlpert bda3e0b
Make all OutputKinds configurable.
CollinAlpert File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
45 changes: 45 additions & 0 deletions
45
...s/CSharp/Microsoft.CodeQuality.Analyzers/Maintainability/CSharpMakeTypesInternal.Fixer.cs
This file contains hidden or 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,45 @@ | ||
| // Copyright (c) Microsoft. All Rights Reserved. Licensed under the MIT license. See License.txt in the project root for license information. | ||
|
|
||
| using System.Composition; | ||
| using System.Linq; | ||
| using Microsoft.CodeAnalysis; | ||
| using Microsoft.CodeAnalysis.CodeFixes; | ||
| using Microsoft.CodeAnalysis.CSharp; | ||
| using Microsoft.CodeAnalysis.CSharp.Syntax; | ||
| using Microsoft.CodeQuality.Analyzers.Maintainability; | ||
|
|
||
| namespace Microsoft.CodeQuality.CSharp.Analyzers.Maintainability | ||
| { | ||
| [ExportCodeFixProvider(LanguageNames.CSharp), Shared] | ||
| public sealed class CSharpMakeTypesInternalFixer : MakeTypesInternalFixer | ||
| { | ||
| protected override SyntaxNode? MakeInternal(SyntaxNode node) | ||
| { | ||
| if (node is TypeDeclarationSyntax type) | ||
| { | ||
| var publicKeyword = type.Modifiers.First(m => m.IsKind(SyntaxKind.PublicKeyword)); | ||
| var modifiers = type.Modifiers.Replace(publicKeyword, SyntaxFactory.Token(SyntaxKind.InternalKeyword)); | ||
|
|
||
| return type.WithModifiers(modifiers); | ||
| } | ||
|
|
||
| if (node is EnumDeclarationSyntax @enum) | ||
| { | ||
| var publicKeyword = @enum.Modifiers.First(m => m.IsKind(SyntaxKind.PublicKeyword)); | ||
| var modifiers = @enum.Modifiers.Replace(publicKeyword, SyntaxFactory.Token(SyntaxKind.InternalKeyword)); | ||
|
|
||
| return @enum.WithModifiers(modifiers); | ||
| } | ||
|
|
||
| if (node is DelegateDeclarationSyntax @delegate) | ||
| { | ||
| var publicKeyword = @delegate.Modifiers.First(m => m.IsKind(SyntaxKind.PublicKeyword)); | ||
| var modifiers = @delegate.Modifiers.Replace(publicKeyword, SyntaxFactory.Token(SyntaxKind.InternalKeyword)); | ||
|
|
||
| return @delegate.WithModifiers(modifiers); | ||
| } | ||
|
|
||
| return null; | ||
buyaa-n marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
| } | ||
49 changes: 49 additions & 0 deletions
49
...alyzers/CSharp/Microsoft.CodeQuality.Analyzers/Maintainability/CSharpMakeTypesInternal.cs
This file contains hidden or 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,49 @@ | ||
| // 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.Extensions; | ||
| using Microsoft.CodeAnalysis; | ||
| using Microsoft.CodeAnalysis.CSharp; | ||
| using Microsoft.CodeAnalysis.CSharp.Syntax; | ||
| using Microsoft.CodeAnalysis.Diagnostics; | ||
| using Microsoft.CodeQuality.Analyzers.Maintainability; | ||
|
|
||
| namespace Microsoft.CodeQuality.CSharp.Analyzers.Maintainability | ||
| { | ||
| [DiagnosticAnalyzer(LanguageNames.CSharp)] | ||
| public sealed class CSharpMakeTypesInternal : MakeTypesInternal<SyntaxKind> | ||
| { | ||
| protected override ImmutableArray<SyntaxKind> TypeKinds { get; } = | ||
| ImmutableArray.Create(SyntaxKind.ClassDeclaration, SyntaxKind.StructDeclaration, SyntaxKind.InterfaceDeclaration, SyntaxKind.RecordDeclaration); | ||
|
|
||
| protected override SyntaxKind EnumKind { get; } = SyntaxKind.EnumDeclaration; | ||
|
|
||
| protected override ImmutableArray<SyntaxKind> DelegateKinds { get; } = ImmutableArray.Create(SyntaxKind.DelegateDeclaration); | ||
|
|
||
| protected override void AnalyzeTypeDeclaration(SyntaxNodeAnalysisContext context) | ||
| { | ||
| var type = (TypeDeclarationSyntax)context.Node; | ||
| ReportIfPublic(context, type.Modifiers, type.Identifier); | ||
| } | ||
|
|
||
| protected override void AnalyzeEnumDeclaration(SyntaxNodeAnalysisContext context) | ||
| { | ||
| var @enum = (EnumDeclarationSyntax)context.Node; | ||
| ReportIfPublic(context, @enum.Modifiers, @enum.Identifier); | ||
| } | ||
|
|
||
| protected override void AnalyzeDelegateDeclaration(SyntaxNodeAnalysisContext context) | ||
| { | ||
| var @delegate = (DelegateDeclarationSyntax)context.Node; | ||
| ReportIfPublic(context, @delegate.Modifiers, @delegate.Identifier); | ||
| } | ||
|
|
||
| private static void ReportIfPublic(SyntaxNodeAnalysisContext context, SyntaxTokenList modifiers, SyntaxToken identifier) | ||
| { | ||
| if (modifiers.Any(SyntaxKind.PublicKeyword)) | ||
| { | ||
| context.ReportDiagnostic(identifier.CreateDiagnostic(Rule)); | ||
| } | ||
| } | ||
| } | ||
| } |
This file contains hidden or 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
43 changes: 43 additions & 0 deletions
43
...Analyzers/Core/Microsoft.CodeQuality.Analyzers/Maintainability/MakeTypesInternal.Fixer.cs
This file contains hidden or 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,43 @@ | ||
| // 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.Threading.Tasks; | ||
| using Analyzer.Utilities; | ||
| using Microsoft.CodeAnalysis; | ||
| using Microsoft.CodeAnalysis.CodeActions; | ||
| using Microsoft.CodeAnalysis.CodeFixes; | ||
|
|
||
| namespace Microsoft.CodeQuality.Analyzers.Maintainability | ||
| { | ||
| public abstract class MakeTypesInternalFixer : CodeFixProvider | ||
| { | ||
| public override async Task RegisterCodeFixesAsync(CodeFixContext context) | ||
| { | ||
| var root = await context.Document.GetRequiredSyntaxRootAsync(context.CancellationToken).ConfigureAwait(false); | ||
| var node = root.FindNode(context.Span); | ||
|
|
||
| var codeAction = CodeAction.Create( | ||
| MicrosoftCodeQualityAnalyzersResources.MakeTypesInternalCodeFixTitle, | ||
| _ => | ||
| { | ||
| var newNode = MakeInternal(node); | ||
| if (newNode is null) | ||
| { | ||
| return Task.FromResult(context.Document); | ||
| } | ||
|
|
||
| var newRoot = root.ReplaceNode(node, newNode.WithTriviaFrom(node)); | ||
|
|
||
| return Task.FromResult(context.Document.WithSyntaxRoot(newRoot)); | ||
| }, | ||
| MicrosoftCodeQualityAnalyzersResources.MakeTypesInternalCodeFixTitle); | ||
| context.RegisterCodeFix(codeAction, context.Diagnostics); | ||
| } | ||
|
|
||
| public override FixAllProvider GetFixAllProvider() => WellKnownFixAllProviders.BatchFixer; | ||
|
|
||
| protected abstract SyntaxNode? MakeInternal(SyntaxNode node); | ||
|
|
||
| public override ImmutableArray<string> FixableDiagnosticIds { get; } = ImmutableArray.Create(MakeTypesInternal<SymbolKind>.RuleId); | ||
| } | ||
| } |
60 changes: 60 additions & 0 deletions
60
src/NetAnalyzers/Core/Microsoft.CodeQuality.Analyzers/Maintainability/MakeTypesInternal.cs
This file contains hidden or 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; | ||
| using System.Collections.Immutable; | ||
| using Analyzer.Utilities; | ||
| using Microsoft.CodeAnalysis; | ||
| using Microsoft.CodeAnalysis.Diagnostics; | ||
|
|
||
| namespace Microsoft.CodeQuality.Analyzers.Maintainability | ||
| { | ||
| using static MicrosoftCodeQualityAnalyzersResources; | ||
|
|
||
| public abstract class MakeTypesInternal<TSyntaxKind> : DiagnosticAnalyzer | ||
| where TSyntaxKind : struct, Enum | ||
| { | ||
| internal const string RuleId = "CA1514"; | ||
|
|
||
| protected static readonly DiagnosticDescriptor Rule = DiagnosticDescriptorHelper.Create( | ||
| RuleId, | ||
| CreateLocalizableResourceString(nameof(MakeTypesInternalTitle)), | ||
| CreateLocalizableResourceString(nameof(MakeTypesInternalMessage)), | ||
| DiagnosticCategory.Maintainability, | ||
| RuleLevel.Disabled, | ||
| description: CreateLocalizableResourceString(nameof(MakeTypesInternalDescription)), | ||
| isPortedFxCopRule: false, | ||
| isDataflowRule: false); | ||
|
|
||
| public override void Initialize(AnalysisContext context) | ||
| { | ||
| context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None); | ||
| context.EnableConcurrentExecution(); | ||
| context.RegisterCompilationStartAction(context => | ||
| { | ||
| var compilation = context.Compilation; | ||
| if (compilation.Options.OutputKind is not (OutputKind.ConsoleApplication or OutputKind.WindowsApplication or OutputKind.WindowsRuntimeApplication)) | ||
| { | ||
| return; | ||
| } | ||
|
|
||
| context.RegisterSyntaxNodeAction(AnalyzeTypeDeclaration, TypeKinds); | ||
| context.RegisterSyntaxNodeAction(AnalyzeEnumDeclaration, EnumKind); | ||
| context.RegisterSyntaxNodeAction(AnalyzeDelegateDeclaration, DelegateKinds); | ||
| }); | ||
| } | ||
|
|
||
| protected abstract ImmutableArray<TSyntaxKind> TypeKinds { get; } | ||
|
|
||
| protected abstract TSyntaxKind EnumKind { get; } | ||
|
|
||
| protected abstract ImmutableArray<TSyntaxKind> DelegateKinds { get; } | ||
|
|
||
| protected abstract void AnalyzeTypeDeclaration(SyntaxNodeAnalysisContext context); | ||
|
|
||
| protected abstract void AnalyzeEnumDeclaration(SyntaxNodeAnalysisContext context); | ||
|
|
||
| protected abstract void AnalyzeDelegateDeclaration(SyntaxNodeAnalysisContext context); | ||
|
|
||
| public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics { get; } = ImmutableArray.Create(Rule); | ||
| } | ||
| } |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.