Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Prev Previous commit
Next Next commit
Use file scoped namespaces and primary cosntructors
  • Loading branch information
CyrusNajmabadi committed Dec 6, 2023
commit bb36656fbf9e0c52b5fcedc51ec7da2e2103b38e
Original file line number Diff line number Diff line change
Expand Up @@ -15,46 +15,41 @@
using Microsoft.CodeAnalysis.Shared.Extensions;
using Roslyn.Utilities;

namespace Microsoft.CodeAnalysis.UseExplicitTupleName
namespace Microsoft.CodeAnalysis.UseExplicitTupleName;

[ExportCodeFixProvider(LanguageNames.CSharp, LanguageNames.VisualBasic, Name = PredefinedCodeFixProviderNames.UseExplicitTupleName), Shared]
[method: ImportingConstructor]
[method: Obsolete(MefConstruction.ImportingConstructorMessage, error: true)]
internal partial class UseExplicitTupleNameCodeFixProvider() : SyntaxEditorBasedCodeFixProvider
{
[ExportCodeFixProvider(LanguageNames.CSharp, LanguageNames.VisualBasic, Name = PredefinedCodeFixProviderNames.UseExplicitTupleName), Shared]
internal partial class UseExplicitTupleNameCodeFixProvider : SyntaxEditorBasedCodeFixProvider
{
[ImportingConstructor]
[Obsolete(MefConstruction.ImportingConstructorMessage, error: true)]
public UseExplicitTupleNameCodeFixProvider()
{
}
public override ImmutableArray<string> FixableDiagnosticIds { get; }
= ImmutableArray.Create(IDEDiagnosticIds.UseExplicitTupleNameDiagnosticId);

public override ImmutableArray<string> FixableDiagnosticIds { get; }
= ImmutableArray.Create(IDEDiagnosticIds.UseExplicitTupleNameDiagnosticId);
public override Task RegisterCodeFixesAsync(CodeFixContext context)
{
RegisterCodeFix(context, AnalyzersResources.Use_explicitly_provided_tuple_name, nameof(AnalyzersResources.Use_explicitly_provided_tuple_name));
return Task.CompletedTask;
}

public override Task RegisterCodeFixesAsync(CodeFixContext context)
{
RegisterCodeFix(context, AnalyzersResources.Use_explicitly_provided_tuple_name, nameof(AnalyzersResources.Use_explicitly_provided_tuple_name));
return Task.CompletedTask;
}
protected override Task FixAllAsync(
Document document, ImmutableArray<Diagnostic> diagnostics,
SyntaxEditor editor, CodeActionOptionsProvider fallbackOptions, CancellationToken cancellationToken)
{
var generator = editor.Generator;

protected override Task FixAllAsync(
Document document, ImmutableArray<Diagnostic> diagnostics,
SyntaxEditor editor, CodeActionOptionsProvider fallbackOptions, CancellationToken cancellationToken)
foreach (var diagnostic in diagnostics)
{
var generator = editor.Generator;
var oldNameNode = diagnostic.Location.FindNode(
getInnermostNodeForTie: true, cancellationToken: cancellationToken);

foreach (var diagnostic in diagnostics)
{
var oldNameNode = diagnostic.Location.FindNode(
getInnermostNodeForTie: true, cancellationToken: cancellationToken);
var preferredName = diagnostic.Properties[nameof(UseExplicitTupleNameDiagnosticAnalyzer.ElementName)];
Contract.ThrowIfNull(preferredName);

var preferredName = diagnostic.Properties[nameof(UseExplicitTupleNameDiagnosticAnalyzer.ElementName)];
Contract.ThrowIfNull(preferredName);
var newNameNode = generator.IdentifierName(preferredName).WithTriviaFrom(oldNameNode);

var newNameNode = generator.IdentifierName(preferredName).WithTriviaFrom(oldNameNode);

editor.ReplaceNode(oldNameNode, newNameNode);
}

return Task.CompletedTask;
editor.ReplaceNode(oldNameNode, newNameNode);
}

return Task.CompletedTask;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,75 +16,70 @@
using Microsoft.CodeAnalysis.Shared.Extensions;
using Microsoft.CodeAnalysis.Shared.Utilities;

namespace Microsoft.CodeAnalysis.UseSystemHashCode
namespace Microsoft.CodeAnalysis.UseSystemHashCode;

[ExportCodeFixProvider(LanguageNames.CSharp, LanguageNames.VisualBasic, Name = PredefinedCodeFixProviderNames.UseSystemHashCode), Shared]
[method: ImportingConstructor]
[method: SuppressMessage("RoslynDiagnosticsReliability", "RS0033:Importing constructor should be [Obsolete]", Justification = "Used in test code: https://github.com/dotnet/roslyn/issues/42814")]
internal class UseSystemHashCodeCodeFixProvider() : SyntaxEditorBasedCodeFixProvider
{
[ExportCodeFixProvider(LanguageNames.CSharp, LanguageNames.VisualBasic, Name = PredefinedCodeFixProviderNames.UseSystemHashCode), Shared]
internal class UseSystemHashCodeCodeFixProvider : SyntaxEditorBasedCodeFixProvider
public override ImmutableArray<string> FixableDiagnosticIds { get; }
= ImmutableArray.Create(IDEDiagnosticIds.UseSystemHashCode);

public override Task RegisterCodeFixesAsync(CodeFixContext context)
{
[ImportingConstructor]
[SuppressMessage("RoslynDiagnosticsReliability", "RS0033:Importing constructor should be [Obsolete]", Justification = "Used in test code: https://github.com/dotnet/roslyn/issues/42814")]
public UseSystemHashCodeCodeFixProvider()
RegisterCodeFix(context, AnalyzersResources.Use_System_HashCode, nameof(AnalyzersResources.Use_System_HashCode));
return Task.CompletedTask;
}

protected override async Task FixAllAsync(
Document document, ImmutableArray<Diagnostic> diagnostics,
SyntaxEditor editor, CodeActionOptionsProvider fallbackOptions, CancellationToken cancellationToken)
{
var generator = SyntaxGenerator.GetGenerator(document);
var generatorInternal = document.GetRequiredLanguageService<SyntaxGeneratorInternal>();
var declarationService = document.GetLanguageService<ISymbolDeclarationService>();
if (declarationService == null)
{
return;
}

public override ImmutableArray<string> FixableDiagnosticIds { get; }
= ImmutableArray.Create(IDEDiagnosticIds.UseSystemHashCode);

public override Task RegisterCodeFixesAsync(CodeFixContext context)
var semanticModel = await document.GetRequiredSemanticModelAsync(cancellationToken).ConfigureAwait(false);
if (!HashCodeAnalyzer.TryGetAnalyzer(semanticModel.Compilation, out var analyzer))
{
RegisterCodeFix(context, AnalyzersResources.Use_System_HashCode, nameof(AnalyzersResources.Use_System_HashCode));
return Task.CompletedTask;
Debug.Fail("Could not get analyzer");
return;
}

protected override async Task FixAllAsync(
Document document, ImmutableArray<Diagnostic> diagnostics,
SyntaxEditor editor, CodeActionOptionsProvider fallbackOptions, CancellationToken cancellationToken)
foreach (var diagnostic in diagnostics)
{
var generator = SyntaxGenerator.GetGenerator(document);
var generatorInternal = document.GetRequiredLanguageService<SyntaxGeneratorInternal>();
var declarationService = document.GetLanguageService<ISymbolDeclarationService>();
if (declarationService == null)
{
return;
}
var operationLocation = diagnostic.AdditionalLocations[0].FindNode(cancellationToken);
var operation = semanticModel.GetOperation(operationLocation, cancellationToken);

var semanticModel = await document.GetRequiredSemanticModelAsync(cancellationToken).ConfigureAwait(false);
if (!HashCodeAnalyzer.TryGetAnalyzer(semanticModel.Compilation, out var analyzer))
{
Debug.Fail("Could not get analyzer");
return;
}
var methodDecl = diagnostic.AdditionalLocations[1].FindNode(cancellationToken);
var method = semanticModel.GetDeclaredSymbol(methodDecl, cancellationToken);
if (method == null)
continue;

foreach (var diagnostic in diagnostics)
{
var operationLocation = diagnostic.AdditionalLocations[0].FindNode(cancellationToken);
var operation = semanticModel.GetOperation(operationLocation, cancellationToken);
var methodBlock = declarationService.GetDeclarations(method)[0].GetSyntax(cancellationToken);

var methodDecl = diagnostic.AdditionalLocations[1].FindNode(cancellationToken);
var method = semanticModel.GetDeclaredSymbol(methodDecl, cancellationToken);
if (method == null)
continue;

var methodBlock = declarationService.GetDeclarations(method)[0].GetSyntax(cancellationToken);

var (accessesBase, members, _) = analyzer.GetHashedMembers(method, operation);
if (accessesBase || !members.IsDefaultOrEmpty)
{
// Produce the new statements for the GetHashCode method and replace the
// existing ones with them.
var (accessesBase, members, _) = analyzer.GetHashedMembers(method, operation);
if (accessesBase || !members.IsDefaultOrEmpty)
{
// Produce the new statements for the GetHashCode method and replace the
// existing ones with them.

// Only if there was a base.GetHashCode() do we pass in the ContainingType
// so that we generate the same.
var containingType = accessesBase ? method!.ContainingType : null;
var components = generator.GetGetHashCodeComponents(
generatorInternal, semanticModel.Compilation, containingType, members, justMemberReference: true);
// Only if there was a base.GetHashCode() do we pass in the ContainingType
// so that we generate the same.
var containingType = accessesBase ? method!.ContainingType : null;
var components = generator.GetGetHashCodeComponents(
generatorInternal, semanticModel.Compilation, containingType, members, justMemberReference: true);

var updatedDecl = generator.WithStatements(
methodBlock,
generator.CreateGetHashCodeStatementsUsingSystemHashCode(
generatorInternal, analyzer.SystemHashCodeType, components));
editor.ReplaceNode(methodBlock, updatedDecl);
}
var updatedDecl = generator.WithStatements(
methodBlock,
generator.CreateGetHashCodeStatementsUsingSystemHashCode(
generatorInternal, analyzer.SystemHashCodeType, components));
editor.ReplaceNode(methodBlock, updatedDecl);
}
}
}
Expand Down