-
Notifications
You must be signed in to change notification settings - Fork 4.1k
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 sync namespace analyzer #49990
Add sync namespace analyzer #49990
Conversation
src/Analyzers/CSharp/Analyzers/NamespaceFileSync/NamespaceFileSyncDiagnosticAnalyzer.cs
Outdated
Show resolved
Hide resolved
src/Analyzers/CSharp/Analyzers/NamespaceFileSync/NamespaceFileSyncDiagnosticAnalyzer.cs
Outdated
Show resolved
Hide resolved
src/Analyzers/CSharp/Analyzers/NamespaceFileSync/NamespaceFileSyncDiagnosticAnalyzer.cs
Outdated
Show resolved
Hide resolved
src/Analyzers/CSharp/Analyzers/NamespaceFileSync/NamespaceFileSyncDiagnosticAnalyzer.cs
Outdated
Show resolved
Hide resolved
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.
Makes sense to me.
src/Analyzers/CSharp/Analyzers/NamespaceFileSync/NamespaceFileSyncDiagnosticAnalyzer.cs
Outdated
Show resolved
Hide resolved
src/Analyzers/CSharp/Analyzers/NamespaceFileSync/NamespaceFileSyncDiagnosticAnalyzer.cs
Outdated
Show resolved
Hide resolved
src/Analyzers/CSharp/Analyzers/NamespaceFileSync/NamespaceFileSyncDiagnosticAnalyzer.cs
Outdated
Show resolved
Hide resolved
src/Workspaces/SharedUtilitiesAndExtensions/Compiler/Core/Utilities/PathMetadataUtilities.cs
Outdated
Show resolved
Hide resolved
src/Analyzers/CSharp/Analyzers/NamespaceFileSync/NamespaceFileSyncDiagnosticAnalyzer.cs
Outdated
Show resolved
Hide resolved
// TODO : can this cause a loop? This condition was used by the service before, | ||
// but now could possibly add an Error severity as an analyzer | ||
var isCurrentNamespaceInvalid = namespaceDeclaration.Name | ||
.GetDiagnostics().Any(d => d.Severity == DiagnosticSeverity.Error); |
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.
Maybe check for DefaultSeverity + id?
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.
this feels like an odd check. i'm not sure what it's really trying to ask
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.
I'm not 100% sure this is needed. Even if the namespace is invalid, is it practical to suggest the correct name?
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.
Ping on this. Intent was to check that the namespace was valid before suggesting to fix. Thoughts on what the right behavior should be?
Co-authored-by: Manish Vasani <[email protected]> Co-authored-by: David Wengier <[email protected]>
...ortable/CodeRefactorings/SyncNamespace/AbstractSyncNamespaceCodeRefactoringProvider.State.cs
Show resolved
Hide resolved
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.
overall looking good. some things i found a bit confusing though and would like clarity on.
where TNamespaceSyntax : SyntaxNode | ||
{ | ||
private static readonly LocalizableResourceString s_localizableTitle = new( | ||
nameof(AnalyzersResources.Namespace_does_not_match_folder_structure), AnalyzersResources.ResourceManager, typeof(AnalyzersResources)); |
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.
indent properly.
nameof(AnalyzersResources.Namespace_does_not_match_folder_structure), AnalyzersResources.ResourceManager, typeof(AnalyzersResources)); | ||
|
||
private static readonly LocalizableResourceString s_localizableInsideMessage = new( | ||
nameof(AnalyzersResources.Namespace_0_does_not_match_folder_structure_expected_1), AnalyzersResources.ResourceManager, typeof(AnalyzersResources)); |
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.
nit: consider inlining these two statics. up to you, but it's waht we do in lots of other places.
|
||
private static readonly SymbolDisplayFormat s_namespaceDisplayFormat = SymbolDisplayFormat | ||
.FullyQualifiedFormat | ||
.WithGlobalNamespaceStyle(SymbolDisplayGlobalNamespaceStyle.Omitted); |
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.
nit: place statics above constructor.
return; | ||
} | ||
|
||
if (context.Node is TNamespaceSyntax namespaceDecl) |
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.
my preference would be a hard-cast here as this shouldn't be possible to fail.
/// on a sequential level instead of batch fixing and merging the changes. This prevents | ||
/// collissions that the batch fixer won't handle correctly but is slower. | ||
/// </summary> | ||
internal abstract partial class AbstractChangeNamespaceToMatchFolderCodeFixProvider |
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.
this name is so much better. t hanks.
public MyCodeAction(string title, Func<CancellationToken, Task<Solution>> createChangedSolution) | ||
: base(title, createChangedSolution) | ||
{ | ||
|
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.
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.
Auto-approval
Sync namespace analyzer will warn and provide code fixes for when namespaces do not match the folder structure. It should follow the same logic as https://docs.microsoft.com/en-us/visualstudio/ide/reference/sync-namespace-and-folder-name?view=vs-2019 but will allow users to configure warning severity and have CI integration.