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

Update 'use auto property' to support using the field keyword #74629

Merged

Conversation

CyrusNajmabadi
Copy link
Member

No description provided.

// Can't remove the field if it has attributes on it.
var attributes = getterField.GetAttributes();
if (attributes.Length > 0 && [email protected])
return 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.

this is changed. we used to always prevent converting if there was an attribute. now we support conversion in C# (since that can have attributes on a property that apply to a field).

// A setter is optional though.
var setMethod = property.SetMethod;
if (setMethod != null)
if (property.SetMethod != null)
Copy link
Member Author

Choose a reason for hiding this comment

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

similar to above. if there is a set-method, we now do a deeper analysis to see which fields it uses.

// so, it's likely that that's the field to use.
getterFields = getterFields.Where(
static (field, property) => field.Name.EndsWith(property.Name, StringComparison.OrdinalIgnoreCase),
property);
Copy link
Member Author

Choose a reason for hiding this comment

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

special heuristic for the very common case where a field/property have virtually the same name (just differing in casing, or having a _ prefix, etc.).


private sealed record AnalysisResult(
Copy link
Member Author

Choose a reason for hiding this comment

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

moved to its own file.

TExpression,
TIdentifierName>
{
/// <param name="Property">The property we will make into an auto-property.</param>
Copy link
Member Author

Choose a reason for hiding this comment

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

just a move, and added docs.

@@ -2,8 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

#nullable disable

using System;
Copy link
Member Author

Choose a reason for hiding this comment

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

second meat of the change.

var needsSetter = NeedsSetter(compilation, propertyDeclaration, isWrittenOutsideOfConstructor);
var fieldInitializer = fieldDeclarator.Initializer?.Value;

if (!isTrivialGetAccessor && !isTrivialSetAccessor && !needsSetter && fieldInitializer == null)
Copy link
Member Author

Choose a reason for hiding this comment

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

next, we update the property to use 'accessor list' form if needed.

Copy link
Member Author

Choose a reason for hiding this comment

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

a lot of the below code changes because it used to be able to just unilaterally assume it would convert directly to get; or get;set;. But now we have to actually properly manipulate the individual accessors depending on if we get a simple accessor, or keep a complex one that uses field within it.

@CyrusNajmabadi
Copy link
Member Author

@genlu @Cosifne @akhera99 ptal. thanks.

Copy link
Member

@akhera99 akhera99 left a comment

Choose a reason for hiding this comment

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

Looks good to me

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-IDE untriaged Issues and PRs which have not yet been triaged by a lead VSCode
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants