-
Notifications
You must be signed in to change notification settings - Fork 4k
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 accessibility modifier on file creation #51935
Merged
CyrusNajmabadi
merged 11 commits into
dotnet:main
from
Youssef1313:cleanup-file-creation
Jul 30, 2021
Merged
Changes from 6 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
e4c90bb
Add accessibility modifier on file creation
Youssef1313 a358ab7
Update AbstractAddAccessibilityModifiersCodeFixProvider.cs
Youssef1313 02ed185
Merge remote-tracking branch 'upstream/main' into cleanup-file-creation
CyrusNajmabadi 1f97af2
Move code.
CyrusNajmabadi 6a80471
fix
CyrusNajmabadi 9af41e4
Fix
CyrusNajmabadi 9f796a6
introduce service
CyrusNajmabadi c0a18e2
Merge remote-tracking branch 'upstream/main' into cleanup-file-creation
CyrusNajmabadi bf79730
Merge branch 'main' into cleanup-file-creation
CyrusNajmabadi fda6aea
move down
CyrusNajmabadi f8f5ab4
Use COnfigureAwait(true)
CyrusNajmabadi 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 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
63 changes: 63 additions & 0 deletions
63
src/Analyzers/Core/CodeFixes/AddAccessibilityModifiers/AddAccessibilityModifiersHelpers.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,63 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
// See the LICENSE file in the project root for more information. | ||
|
||
using System.Threading; | ||
using Microsoft.CodeAnalysis.Editing; | ||
using Microsoft.CodeAnalysis.Shared.Extensions; | ||
using Roslyn.Utilities; | ||
|
||
namespace Microsoft.CodeAnalysis.AddAccessibilityModifiers | ||
{ | ||
internal static class AddAccessibilityModifiersHelpers | ||
{ | ||
public static void UpdateDeclaration( | ||
SyntaxEditor editor, ISymbol symbol, SyntaxNode declaration) | ||
{ | ||
Contract.ThrowIfNull(symbol); | ||
|
||
var preferredAccessibility = GetPreferredAccessibility(symbol); | ||
|
||
// Check to see if we need to add or remove | ||
// If there's a modifier, then we need to remove it, otherwise no modifier, add it. | ||
editor.ReplaceNode( | ||
declaration, | ||
(currentDeclaration, _) => UpdateAccessibility(currentDeclaration, preferredAccessibility)); | ||
|
||
return; | ||
|
||
SyntaxNode UpdateAccessibility(SyntaxNode declaration, Accessibility preferredAccessibility) | ||
{ | ||
var generator = editor.Generator; | ||
|
||
// If there was accessibility on the member, then remove it. If there was no accessibility, then add | ||
// the preferred accessibility for this member. | ||
return generator.GetAccessibility(declaration) == Accessibility.NotApplicable | ||
? generator.WithAccessibility(declaration, preferredAccessibility) | ||
: generator.WithAccessibility(declaration, Accessibility.NotApplicable); | ||
} | ||
} | ||
|
||
private static Accessibility GetPreferredAccessibility(ISymbol symbol) | ||
{ | ||
// If we have an overridden member, then if we're adding an accessibility modifier, use the | ||
// accessibility of the member we're overriding as both should be consistent here. | ||
if (symbol.GetOverriddenMember() is { DeclaredAccessibility: var accessibility }) | ||
return accessibility; | ||
|
||
// Default abstract members to be protected, and virtual members to be public. They can't be private as | ||
// that's not legal. And these are reasonable default values for them. | ||
if (symbol is IMethodSymbol or IPropertySymbol or IEventSymbol) | ||
{ | ||
if (symbol.IsAbstract) | ||
return Accessibility.Protected; | ||
|
||
if (symbol.IsVirtual) | ||
return Accessibility.Public; | ||
} | ||
|
||
// Otherwise, default to whatever accessibility no-accessibility means for this member; | ||
return symbol.DeclaredAccessibility; | ||
} | ||
} | ||
} |
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
📝 These can all be
ConfigureAwait(true)
to improve efficiency (the main thread is the caller and available to process continuations without the potential latency of the thread pool)