Skip to content

Commit

Permalink
Remove unnecessary code: was When generating constructor, trim known …
Browse files Browse the repository at this point in the history
…prefixes from argument name to get parameter name (#34804)

* Fixes #33673

* Respond to feedback

* Remove unneeded code and add tests

* Fix access modifier
  • Loading branch information
chborl authored Apr 9, 2019
1 parent adbfc3e commit 564e387
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3676,5 +3676,43 @@ class D
{
}", options: options.MergeStyles(options.FieldNamesAreCamelCaseWithUnderscore, options.ParameterNamesAreCamelCaseWithPUnderscorePrefix, LanguageNames.CSharp));
}

[WorkItem(33673, "https://github.com/dotnet/roslyn/issues/33673")]
[Theory, Trait(Traits.Feature, Traits.Features.CodeActionsGenerateConstructor)]
[InlineData("_s", "s")]
[InlineData("_S", "s")]
[InlineData("m_s", "s")]
[InlineData("m_S", "s")]
[InlineData("s_s", "s")]
[InlineData("t_s", "s")]
public async Task GenerateConstructor_ArgumentHasCommonPrefix(string argumentName, string fieldName)
{
await TestInRegularAndScriptAsync(
$@"
class Program
{{
static void Main(string[] args)
{{
string {argumentName} = "";
new Prog[||]ram({argumentName});
}}
}}",
$@"
class Program
{{
private string {fieldName};
public Program(string {fieldName})
{{
this.{fieldName} = {fieldName};
}}
static void Main(string[] args)
{{
string {argumentName} = "";
new Program({argumentName});
}}
}}");
}
}
}
4 changes: 1 addition & 3 deletions src/Workspaces/Core/Portable/Utilities/ParameterName.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,7 @@ public ParameterName(string nameBasedOnArgument, bool isFixed, NamingRule parame
{
// Otherwise, massage it a bit to be a more suitable match for
// how people actually writing parameters.
var trimmed = nameBasedOnArgument.TrimStart('_');
BestNameForParameter = trimmed.Length > 0 ? trimmed.ToCamelCase() : nameBasedOnArgument;
BestNameForParameter = parameterNamingRule.NamingStyle.MakeCompliant(BestNameForParameter).First();
BestNameForParameter = parameterNamingRule.NamingStyle.MakeCompliant(nameBasedOnArgument).First();
}
}

Expand Down

0 comments on commit 564e387

Please sign in to comment.