-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Don't offer "Introduce parameter" on member initializer names #81014
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
Conversation
Add check to prevent "Introduce parameter" refactoring from being offered on the left-hand side of member initializers in both anonymous objects (new { a = ... }) and regular object initializers (new Foo { X = ... }).
- Added abstract method IsAnonymousObjectMemberDeclaratorNameIdentifier to check for anonymous object member names
- Implemented the method in both C# and VB providers
- Added tests to verify refactoring is not offered on member initializer names
- Existing IsMemberInitializerNamedAssignmentIdentifier handles regular object initializers
Co-authored-by: CyrusNajmabadi <[email protected]>
Simplified IsAnonymousObjectMemberDeclaratorNameIdentifier method in VB provider using early returns instead of nested if statements for better readability. Co-authored-by: CyrusNajmabadi <[email protected]>
| class Foo { public int X { get; set; } } | ||
| class C | ||
| { | ||
| Foo M() => new Foo |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@copilot use 'Goo' instead of 'Foo' in all tests.
use [Fact, WorkItem("...")] for all the tests you added.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done in 0cc1448. Renamed 'Foo' to 'Goo' in all tests and added WorkItem attributes with the issue URL.
...ualBasic/Portable/IntroduceParameter/VisualBasicIntroduceParameterCodeRefactoringProvider.vb
Outdated
Show resolved
Hide resolved
…icIntroduceParameterCodeRefactoringProvider.vb
…ibutes - Renamed all instances of 'Foo' to 'Goo' in test classes - Added WorkItem attribute with issue URL to all new tests - Added using statement for Roslyn.Test.Utilities Co-authored-by: CyrusNajmabadi <[email protected]>
….com/dotnet/roslyn into copilot/fix-introduce-parameter-code
Summary: Fix "Introduce parameter for" on member initializer left-hand side
This PR fixes the issue where the "Introduce parameter for" refactoring was incorrectly being offered on the left-hand side (member name) of member initializers.
Problem
When the user placed the cursor on the identifier in the name position of a member initializer (e.g.,
ainnew { a = ... }orXinnew Foo { X = ... }), the "Introduce parameter" refactoring was offered. This doesn't make sense because the identifier represents the member name, not a value that could be parameterized.Solution
Added validation in
IsValidExpressionto detect and exclude these cases:For regular object initializers (
new Foo { X = ... }): The existingIsMemberInitializerNamedAssignmentIdentifiermethod already handles this case.For anonymous object initializers (
new { a = ... }): Added a new abstract methodIsAnonymousObjectMemberDeclaratorNameIdentifierwith language-specific implementations:IdentifierNameSyntax→NameEqualsSyntax→AnonymousObjectMemberDeclaratorSyntaxIdentifierNameSyntax→NamedFieldInitializerSyntax→AnonymousObjectCreationExpressionSyntaxChanges Made
IsValidExpressionto check both casesTesting
Checklist:
IsAnonymousObjectMemberDeclaratorNameIdentifierIsValidExpressionto use both checksOriginal prompt
✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.