Skip to content

Commit

Permalink
Fix Introduce Parameter Crash (#72191)
Browse files Browse the repository at this point in the history
* dont show option for trampoline if refs contain object creation

* feedback

* feedback
  • Loading branch information
akhera99 authored Feb 27, 2024
1 parent b701a39 commit 7764eae
Showing 1 changed file with 11 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ private static bool IsValidExpression(SyntaxNode expression, ISyntaxFactsService
using var actionsBuilder = TemporaryArray<CodeAction>.Empty;
using var actionsBuilderAllOccurrences = TemporaryArray<CodeAction>.Empty;
var syntaxFacts = document.GetRequiredLanguageService<ISyntaxFactsService>();
var methodCallSites = await FindCallSitesAsync(document, methodSymbol, cancellationToken).ConfigureAwait(false);

if (!containsClassExpression)
{
Expand All @@ -157,10 +158,14 @@ private static bool IsValidExpression(SyntaxNode expression, ISyntaxFactsService

if (methodSymbol.MethodKind is not MethodKind.Constructor)
{
actionsBuilder.Add(CreateNewCodeAction(
FeaturesResources.into_extracted_method_to_invoke_at_call_sites, allOccurrences: false, IntroduceParameterCodeActionKind.Trampoline));
actionsBuilderAllOccurrences.Add(CreateNewCodeAction(
FeaturesResources.into_extracted_method_to_invoke_at_call_sites, allOccurrences: true, IntroduceParameterCodeActionKind.Trampoline));
var containsObjectCreationReferences = methodCallSites.Values.Flatten().OfType<TObjectCreationExpressionSyntax>().Any();
if (!containsObjectCreationReferences)
{
actionsBuilder.Add(CreateNewCodeAction(
FeaturesResources.into_extracted_method_to_invoke_at_call_sites, allOccurrences: false, IntroduceParameterCodeActionKind.Trampoline));
actionsBuilderAllOccurrences.Add(CreateNewCodeAction(
FeaturesResources.into_extracted_method_to_invoke_at_call_sites, allOccurrences: true, IntroduceParameterCodeActionKind.Trampoline));
}

if (methodSymbol.MethodKind is not MethodKind.LocalFunction)
{
Expand All @@ -178,7 +183,7 @@ CodeAction CreateNewCodeAction(string actionName, bool allOccurrences, Introduce
{
return CodeAction.Create(
actionName,
cancellationToken => IntroduceParameterAsync(document, expression, methodSymbol, containingMethod, allOccurrences, selectedCodeAction, fallbackOptions, cancellationToken),
cancellationToken => IntroduceParameterAsync(document, expression, methodSymbol, containingMethod, methodCallSites, allOccurrences, selectedCodeAction, fallbackOptions, cancellationToken),
actionName);
}
}
Expand Down Expand Up @@ -229,11 +234,9 @@ CodeAction CreateNewCodeAction(string actionName, bool allOccurrences, Introduce
/// Introduces a new parameter and refactors all the call sites based on the selected code action.
/// </summary>
private async Task<Solution> IntroduceParameterAsync(Document originalDocument, TExpressionSyntax expression,
IMethodSymbol methodSymbol, SyntaxNode containingMethod, bool allOccurrences, IntroduceParameterCodeActionKind selectedCodeAction,
IMethodSymbol methodSymbol, SyntaxNode containingMethod, Dictionary<Document, List<TExpressionSyntax>> methodCallSites, bool allOccurrences, IntroduceParameterCodeActionKind selectedCodeAction,
CodeGenerationOptionsProvider fallbackOptions, CancellationToken cancellationToken)
{
var methodCallSites = await FindCallSitesAsync(originalDocument, methodSymbol, cancellationToken).ConfigureAwait(false);

var modifiedSolution = originalDocument.Project.Solution;
var rewriter = new IntroduceParameterDocumentRewriter(this, originalDocument,
expression, methodSymbol, containingMethod, selectedCodeAction, fallbackOptions, allOccurrences);
Expand Down

0 comments on commit 7764eae

Please sign in to comment.