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

Generate better names for expressions used as parameters. #19334

Merged
merged 8 commits into from
May 8, 2017

Conversation

CyrusNajmabadi
Copy link
Member

@CyrusNajmabadi CyrusNajmabadi commented May 7, 2017

Fixes #2423

@CyrusNajmabadi
Copy link
Member Author

Tagging @dotnet/roslyn-ide

When generating names for expressions, we now check if the expression is in an argument location, and we use the name of the corresponding parameter to help inform the name.

Copy link
Member

@sharwell sharwell left a comment

Choose a reason for hiding this comment

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

Is there a reason we could not use #10072 either directly or as the basis for this?

@@ -556,7 +555,7 @@ public class @class
{
}

public static void Add(object t)
public static void Add(object @class)
Copy link
Member

Choose a reason for hiding this comment

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

❕ This change makes it impossible to tell if the test is using the correct value for the renamed identifier. You should rename class @class at the same time so it's clear that the verbatim identifier comes from the parameter name, not the type name.

If argument.IsNamed Then
Return DirectCast(argument, SimpleArgumentSyntax).NameColonEquals.Name.Identifier.ValueText
ElseIf Not argument.IsOmitted Then
Return semanticModel.GenerateNameForExpression(argument.GetExpression())
Return semanticModel.GenerateNameForExpression(
argument.GetExpression(), capitalize:=False, cancellationToken:=cancellationToken)
Copy link
Member

Choose a reason for hiding this comment

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

❔ Is there no option for this in VB?

Copy link
Member Author

Choose a reason for hiding this comment

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

i don't know what you're asking.

Copy link
Member

Choose a reason for hiding this comment

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

➡️ As was explained by Cyrus in response to my other comment, the answer to my vague question is no such option current exists for parameters, so we pass False since that's what users are generally expecting today. 😄

@@ -222,7 +224,8 @@ private int NonParamsParameterCount(IMethodSymbol method)
}
else
{
var name = semanticFacts.GenerateNameForExpression(semanticModel, expression);
var name = semanticFacts.GenerateNameForExpression(
semanticModel, expression, capitalize: false, cancellationToken: cancellationToken);
Copy link
Member

Choose a reason for hiding this comment

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

❔ Should this be following some style option?

Copy link
Member Author

Choose a reason for hiding this comment

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

not sure what you're asking.

Copy link
Member

Choose a reason for hiding this comment

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

Sorry about that, in both cases I was asking why capitalize was hard-coded as false.

Copy link
Member Author

Choose a reason for hiding this comment

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

i see. Technically we likely want to use some sort of naming service that can follow user preferences. However, in practice, it's highly unlikely that users are going to do anything special with parameters, so i think it's fine to keep the current behavior (which is to make a camel-cased name).

@CyrusNajmabadi
Copy link
Member Author

Tagging @MattGertz

Customer scenario

Our code generation features try to generate good names when tehy can. One area this was lacking was when passing an expression as an argument to a method. In such a case we could use the name of the method parameter to help inform the generated name.

Bugs this fixes:

Fixes #2423

Workarounds, if any

None.

Risk

Low.

Performance impact

low.

Is this a regression from a previous update?

No.

Root cause analysis:

This is a generate improvement.

How was the bug found?

From dogfooding.

@CyrusNajmabadi CyrusNajmabadi merged commit e02548c into dotnet:master May 8, 2017
@CyrusNajmabadi CyrusNajmabadi deleted the generateBetterNames branch May 8, 2017 19:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants