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

Introduce Parameter Should Not Try to Introduce on a Parameter #58959

Merged
merged 5 commits into from
Jan 24, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1951,5 +1951,43 @@ public static void Main(string[] args)

await TestMissingInRegularAndScriptAsync(code);
}

[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsIntroduceParameter)]
public async Task TestIntroduceParameterOnExpressionContainingParameter()
{
var code =
@"
public class C
{
public void M(string s)
{
localFunction();

void localFunction()
{
_ = [|s|].ToString();
}
}
}
";

var expected =
@"
public class C
{
public void M(string s)
{
localFunction(s);

void localFunction(string s)
{
_ = s.ToString();
}
}
}
";

await TestInRegularAndScriptAsync(code, expected, 0);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,6 @@ public sealed override async Task ComputeRefactoringsAsync(CodeRefactoringContex
return;
}

var expressionSymbol = semanticModel.GetSymbolInfo(expression, cancellationToken).Symbol;
if (expressionSymbol is IParameterSymbol)
{
return;
}

var syntaxFacts = document.GetRequiredLanguageService<ISyntaxFactsService>();

// Need to special case for expressions that are contained within a parameter
Expand Down Expand Up @@ -97,6 +91,12 @@ public sealed override async Task ComputeRefactoringsAsync(CodeRefactoringContex
return;
}

var expressionSymbol = semanticModel.GetSymbolInfo(expression, cancellationToken).Symbol;
if (expressionSymbol is IParameterSymbol && !syntaxFacts.IsLocalFunctionStatement(containingMethod))
{
return;
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think the original fix was corrrect. i don't see why we'd want to special case local functions.

note: we should have extra tests here. for example if the user is on a local-func parameter, we shouldn't add that a second time either.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMO the correct fix is none of the two, but:

            if (expressionSymbol is IParameterSymbol parameterSymbol && parameterSymbol.ContainingSymbol.Equals(containingSymbol))
            {
                return;
            }

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i'm ok not adding one parameter to another container. but i don't feel strongly about that.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@CyrusNajmabadi This scenario is important IMO if I want to move from:

public class C
{
    public void M(string s)
    {
        localFunction();

        void localFunction()
        {
            _ = s.ToString();
        }
    }
}

to:

public class C
{
    public void M(string s)
    {
        localFunction(s);

        localFunction("Another string");

        void localFunction(string s)
        {
            _ = s.ToString();
        }
    }
}

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Youssef1313 I agree, I like that example.


var containingSymbol = semanticModel.GetDeclaredSymbol(containingMethod, cancellationToken);
if (containingSymbol is not IMethodSymbol methodSymbol)
{
Expand Down