-
Notifications
You must be signed in to change notification settings - Fork 7
feat: MemberAccessibility #39
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 all commits
Commits
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
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
5 changes: 5 additions & 0 deletions
5
samples/analyzers/SourceKit.Analyzers.MemberAccessibility.Samples/.editorconfig
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,5 @@ | ||
| [*.cs] | ||
|
|
||
| dotnet_diagnostic.SK1100.severity = warning | ||
| dotnet_diagnostic.SK1101.severity = warning | ||
| dotnet_diagnostic.SK1102.severity = warning |
8 changes: 8 additions & 0 deletions
8
...les/analyzers/SourceKit.Analyzers.MemberAccessibility.Samples/MultipleFieldsCase.Fixed.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,8 @@ | ||
| namespace SourceKit.Analyzers.MemberAccessibility.Samples; | ||
|
|
||
| public class MultipleFieldsCaseFixed | ||
| { | ||
| private object _first; | ||
| private object _second; | ||
| private object _third; | ||
| } |
6 changes: 6 additions & 0 deletions
6
samples/analyzers/SourceKit.Analyzers.MemberAccessibility.Samples/MultipleFieldsCase.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,6 @@ | ||
| namespace SourceKit.Analyzers.MemberAccessibility.Samples; | ||
|
|
||
| public class MultipleFieldsCase | ||
| { | ||
| private object _first, _second, _third; | ||
| } |
6 changes: 6 additions & 0 deletions
6
...es/analyzers/SourceKit.Analyzers.MemberAccessibility.Samples/PrivatePropertyCase.Fixed.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,6 @@ | ||
| namespace SourceKit.Analyzers.MemberAccessibility.Samples; | ||
|
|
||
| public class PrivatePropertyCaseFixed | ||
| { | ||
| public object PrivateProperty { get; } | ||
| } |
6 changes: 6 additions & 0 deletions
6
samples/analyzers/SourceKit.Analyzers.MemberAccessibility.Samples/PrivatePropertyCase.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,6 @@ | ||
| namespace SourceKit.Analyzers.MemberAccessibility.Samples; | ||
|
|
||
| public class PrivatePropertyCase | ||
| { | ||
| private object PrivateProperty { get; } | ||
| } |
6 changes: 6 additions & 0 deletions
6
...es/analyzers/SourceKit.Analyzers.MemberAccessibility.Samples/PublicFieldTestCase.Fixed.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,6 @@ | ||
| namespace SourceKit.Analyzers.MemberAccessibility.Samples; | ||
|
|
||
| public class PublicFieldTestCaseFixed | ||
| { | ||
| private object _publicField; | ||
| } |
6 changes: 6 additions & 0 deletions
6
samples/analyzers/SourceKit.Analyzers.MemberAccessibility.Samples/PublicFieldTestCase.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,6 @@ | ||
| namespace SourceKit.Analyzers.MemberAccessibility.Samples; | ||
|
|
||
| public class PublicFieldTestCase | ||
| { | ||
| public object _publicField; | ||
| } |
23 changes: 23 additions & 0 deletions
23
...lyzers.MemberAccessibility.Samples/SourceKit.Analyzers.MemberAccessibility.Samples.csproj
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,23 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk"> | ||
|
|
||
| <PropertyGroup> | ||
| <ImplicitUsings>disable</ImplicitUsings> | ||
| </PropertyGroup> | ||
|
|
||
| <ItemGroup> | ||
| <PackageReference Include="SourceKit.Analyzers.MemberAccessibility"/> | ||
| </ItemGroup> | ||
|
|
||
| <ItemGroup> | ||
| <Compile Update="MultipleFieldsCase.Fixed.cs"> | ||
| <DependentUpon>MultipleFieldsCase.cs</DependentUpon> | ||
| </Compile> | ||
| <Compile Update="PrivatePropertyCase.Fixed.cs"> | ||
| <DependentUpon>PrivatePropertyCase.cs</DependentUpon> | ||
| </Compile> | ||
| <Compile Update="PublicFieldTestCase.Fixed.cs"> | ||
| <DependentUpon>PublicFieldTestCase.cs</DependentUpon> | ||
| </Compile> | ||
| </ItemGroup> | ||
|
|
||
| </Project> |
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
48 changes: 48 additions & 0 deletions
48
...ceKit.Analyzers.MemberAccessibility/Analyzers/FieldCannotHaveMultipleVariablesAnalyzer.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,48 @@ | ||
| using System.Collections.Immutable; | ||
| using Microsoft.CodeAnalysis; | ||
| using Microsoft.CodeAnalysis.CSharp; | ||
| using Microsoft.CodeAnalysis.CSharp.Syntax; | ||
| using Microsoft.CodeAnalysis.Diagnostics; | ||
|
|
||
| namespace SourceKit.Analyzers.MemberAccessibility.Analyzers; | ||
|
|
||
| [DiagnosticAnalyzer(LanguageNames.CSharp, LanguageNames.VisualBasic)] | ||
| public class FieldCannotHaveMultipleVariablesAnalyzer : DiagnosticAnalyzer | ||
| { | ||
| public const string DiagnosticId = "SK1102"; | ||
| public const string Title = nameof(FieldCannotHaveMultipleVariablesAnalyzer); | ||
|
|
||
| public const string Format = """Each field must have separate declaration"""; | ||
|
|
||
| public static readonly DiagnosticDescriptor Descriptor = new DiagnosticDescriptor(DiagnosticId, | ||
| Title, | ||
| Format, | ||
| "Design", | ||
| DiagnosticSeverity.Error, | ||
| true); | ||
|
|
||
| public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics { get; } = | ||
| ImmutableArray.Create(Descriptor); | ||
|
|
||
| public override void Initialize(AnalysisContext context) | ||
| { | ||
| context.EnableConcurrentExecution(); | ||
| context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None); | ||
| context.RegisterSyntaxNodeAction(AnalyzeField, SyntaxKind.FieldDeclaration); | ||
| } | ||
|
|
||
| private void AnalyzeField(SyntaxNodeAnalysisContext context) | ||
| { | ||
| var fieldSyntax = (FieldDeclarationSyntax)context.Node; | ||
|
|
||
| if (fieldSyntax.Declaration.Variables.Count <= 1) | ||
| { | ||
| return; | ||
| } | ||
|
|
||
| Location location = fieldSyntax.GetLocation(); | ||
| var diagnostic = Diagnostic.Create(Descriptor, location); | ||
|
|
||
| context.ReportDiagnostic(diagnostic); | ||
| } | ||
| } | ||
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
23 changes: 0 additions & 23 deletions
23
...rs/SourceKit.Analyzers.MemberAccessibility/CodeFixes/FieldAccessibilityCodeFixProvider.cs
This file was deleted.
Oops, something went wrong.
62 changes: 62 additions & 0 deletions
62
...s/SourceKit.Analyzers.MemberAccessibility/CodeFixes/FieldCannotBePublicCodeFixProvider.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,62 @@ | ||
| using System.Collections.Immutable; | ||
| using Microsoft.CodeAnalysis; | ||
| using Microsoft.CodeAnalysis.CodeActions; | ||
| using Microsoft.CodeAnalysis.CodeFixes; | ||
| using Microsoft.CodeAnalysis.CSharp; | ||
| using Microsoft.CodeAnalysis.CSharp.Syntax; | ||
| using SourceKit.Analyzers.MemberAccessibility.Analyzers; | ||
| using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory; | ||
|
|
||
| namespace SourceKit.Analyzers.MemberAccessibility.CodeFixes; | ||
|
|
||
| [ExportCodeFixProvider(LanguageNames.CSharp, Name = nameof(FieldCannotBePublicCodeFixProvider))] | ||
| public class FieldCannotBePublicCodeFixProvider : CodeFixProvider | ||
| { | ||
| public const string Title = "Make field private"; | ||
|
|
||
| public override ImmutableArray<string> FixableDiagnosticIds { get; } = | ||
| ImmutableArray.Create(FieldCannotBePublicAnalyzer.DiagnosticId); | ||
|
|
||
| public override FixAllProvider GetFixAllProvider() | ||
| => WellKnownFixAllProviders.BatchFixer; | ||
|
|
||
| public override async Task RegisterCodeFixesAsync(CodeFixContext context) | ||
| { | ||
| context.CancellationToken.ThrowIfCancellationRequested(); | ||
|
|
||
| IEnumerable<Task> derivativesMustBePartialDiagnostics = context.Diagnostics | ||
| .Select(x => ProvideDerivativesMustBePartial(context, x)); | ||
|
|
||
| await Task.WhenAll(derivativesMustBePartialDiagnostics); | ||
| } | ||
|
|
||
| private static async Task ProvideDerivativesMustBePartial(CodeFixContext context, Diagnostic diagnostic) | ||
| { | ||
| var root = await context.Document.GetSyntaxRootAsync(context.CancellationToken); | ||
|
|
||
| if (root?.FindNode(diagnostic.Location.SourceSpan) is not { Parent.Parent: FieldDeclarationSyntax fieldSyntax }) | ||
| return; | ||
|
|
||
| var action = CodeAction.Create( | ||
| title: Title, | ||
| priority: CodeActionPriority.High, | ||
| equivalenceKey: nameof(FieldCannotBePublicCodeFixProvider), | ||
| createChangedDocument: _ => | ||
| { | ||
| SyntaxToken privateModifier = fieldSyntax.Modifiers.First(x => x.IsKind(SyntaxKind.PublicKeyword)); | ||
|
|
||
| SyntaxTokenList fixedModifiers = fieldSyntax.Modifiers | ||
| .Replace(privateModifier, Token(SyntaxKind.PrivateKeyword)); | ||
|
|
||
| FieldDeclarationSyntax fixedSyntax = fieldSyntax.WithModifiers(fixedModifiers); | ||
|
|
||
| SyntaxNode newRoot = root.ReplaceNode(fieldSyntax, fixedSyntax); | ||
|
|
||
| Document document = context.Document.WithSyntaxRoot(newRoot); | ||
|
|
||
| return Task.FromResult(document); | ||
| }); | ||
|
|
||
| context.RegisterCodeFix(action, diagnostic); | ||
| } | ||
| } |
Oops, something went wrong.
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.