Skip to content
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

Refactor GetSemanticModelAsync to GetRequiredSemanticModelAsync #4039

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,7 @@ public static async Task<Solution> FixSignatureAsync(Document document, SyntaxNo
{
cancellationToken.ThrowIfCancellationRequested();

SemanticModel? semanticModel = await document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false);
if (semanticModel is null)
{
return document.Project.Solution;
}
SemanticModel? semanticModel = await document.GetRequiredSemanticModelAsync(cancellationToken).ConfigureAwait(false);

var methodSymbol = (IMethodSymbol?)semanticModel.GetDeclaredSymbol(node, cancellationToken);
if (methodSymbol is null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,7 @@ public override async Task RegisterCodeFixesAsync(CodeFixContext context)
private static async Task<Document> ReplaceConstructorWithTestInitializeAsync(Document document, ConstructorDeclarationSyntax constructorDeclaration, CancellationToken cancellationToken)
{
DocumentEditor editor = await DocumentEditor.CreateAsync(document, cancellationToken).ConfigureAwait(false);
SemanticModel semanticModel = await document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false)
?? throw new InvalidOperationException("SemanticModel cannot be null.");
SemanticModel semanticModel = await document.GetRequiredSemanticModelAsync(cancellationToken).ConfigureAwait(false);

// Find the class containing the constructor
if (constructorDeclaration.Parent is ClassDeclarationSyntax containingClass)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,14 @@ namespace Analyzer.Utilities;

internal static class DocumentExtensions
{
public static async ValueTask<SemanticModel> GetRequiredSemanticModelAsync(this Document document, CancellationToken cancellationToken)
{
if (document.TryGetSemanticModel(out var semanticModel))
return semanticModel;
semanticModel = await document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false);
return semanticModel ?? throw new InvalidOperationException("SyntaxTree is required to accomplish the task but is not supported by document");
}

public static async ValueTask<SyntaxNode> GetRequiredSyntaxRootAsync(this Document document, CancellationToken cancellationToken)
{
if (document.TryGetSyntaxRoot(out SyntaxNode? root))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,7 @@ public static async Task<Document> FixClassDeclarationAsync(Document document, C
cancellationToken.ThrowIfCancellationRequested();

// Get the SemanticModel and Compilation
SemanticModel semanticModel = await document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false)
?? throw new InvalidOperationException("SemanticModel cannot be null.");
SemanticModel semanticModel = await document.GetRequiredSemanticModelAsync(cancellationToken).ConfigureAwait(false);
bool canDiscoverInternals = semanticModel.Compilation.CanDiscoverInternals();

DocumentEditor editor = await DocumentEditor.CreateAsync(document, cancellationToken).ConfigureAwait(false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,7 @@ public static async Task<Document> FixMemberDeclarationAsync(Document document,
cancellationToken.ThrowIfCancellationRequested();

// Get the SemanticModel and Compilation
SemanticModel semanticModel = await document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false)
?? throw new InvalidOperationException("SemanticModel cannot be null.");
SemanticModel semanticModel = await document.GetRequiredSemanticModelAsync(cancellationToken).ConfigureAwait(false);
bool canDiscoverInternals = semanticModel.Compilation.CanDiscoverInternals();

DocumentEditor editor = await DocumentEditor.CreateAsync(document, cancellationToken).ConfigureAwait(false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,28 +91,25 @@ private static async Task<Solution> FixTestMethodAsync(Document document, Method
}

// Ensure the method returns void or Task/ValueTask.
SemanticModel? semanticModel = await document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false);
if (semanticModel is not null)
SemanticModel semanticModel = await document.GetRequiredSemanticModelAsync(cancellationToken).ConfigureAwait(false);
Compilation compilation = semanticModel.Compilation;
var wellKnownTypeProvider = WellKnownTypeProvider.GetOrCreate(semanticModel.Compilation);
INamedTypeSymbol? taskSymbol = wellKnownTypeProvider.GetOrCreateTypeByMetadataName(WellKnownTypeNames.SystemThreadingTasksTask);
INamedTypeSymbol? valueTaskSymbol = wellKnownTypeProvider.GetOrCreateTypeByMetadataName(WellKnownTypeNames.SystemThreadingTasksValueTask);

if (newMethodDeclaration.ReturnType != null &&
!newMethodDeclaration.ReturnType.IsVoid() &&
(taskSymbol == null || !semanticModel.ClassifyConversion(newMethodDeclaration.ReturnType, taskSymbol).IsImplicit) &&
(valueTaskSymbol == null || !semanticModel.ClassifyConversion(newMethodDeclaration.ReturnType, valueTaskSymbol).IsImplicit))
{
Compilation compilation = semanticModel.Compilation;
var wellKnownTypeProvider = WellKnownTypeProvider.GetOrCreate(semanticModel.Compilation);
INamedTypeSymbol? taskSymbol = wellKnownTypeProvider.GetOrCreateTypeByMetadataName(WellKnownTypeNames.SystemThreadingTasksTask);
INamedTypeSymbol? valueTaskSymbol = wellKnownTypeProvider.GetOrCreateTypeByMetadataName(WellKnownTypeNames.SystemThreadingTasksValueTask);

if (newMethodDeclaration.ReturnType != null &&
!newMethodDeclaration.ReturnType.IsVoid() &&
(taskSymbol == null || !semanticModel.ClassifyConversion(newMethodDeclaration.ReturnType, taskSymbol).IsImplicit) &&
(valueTaskSymbol == null || !semanticModel.ClassifyConversion(newMethodDeclaration.ReturnType, valueTaskSymbol).IsImplicit))
// Change return type to void and remove return statements
newMethodDeclaration = newMethodDeclaration.WithReturnType(SyntaxFactory.PredefinedType(SyntaxFactory.Token(SyntaxKind.VoidKeyword)));

if (newMethodDeclaration.Body != null)
{
// Change return type to void and remove return statements
newMethodDeclaration = newMethodDeclaration.WithReturnType(SyntaxFactory.PredefinedType(SyntaxFactory.Token(SyntaxKind.VoidKeyword)));

if (newMethodDeclaration.Body != null)
{
SyntaxList<StatementSyntax> statements = newMethodDeclaration.Body.Statements;
IEnumerable<StatementSyntax> newStatements = statements.Where(s => s is not ReturnStatementSyntax);
newMethodDeclaration = newMethodDeclaration.WithBody(newMethodDeclaration.Body.WithStatements(SyntaxFactory.List(newStatements)));
}
SyntaxList<StatementSyntax> statements = newMethodDeclaration.Body.Statements;
IEnumerable<StatementSyntax> newStatements = statements.Where(s => s is not ReturnStatementSyntax);
newMethodDeclaration = newMethodDeclaration.WithBody(newMethodDeclaration.Body.WithStatements(SyntaxFactory.List(newStatements)));
}
}

Expand Down