Skip to content

Remove trailing commas during conversion to class#45778

Merged
CyrusNajmabadi merged 4 commits intodotnet:masterfrom
mahalex:fix-refactor-anonymous-types
Jul 10, 2020
Merged

Remove trailing commas during conversion to class#45778
CyrusNajmabadi merged 4 commits intodotnet:masterfrom
mahalex:fix-refactor-anonymous-types

Conversation

@mahalex
Copy link
Contributor

@mahalex mahalex commented Jul 8, 2020

Fixes #45747

@mahalex mahalex requested a review from a team as a code owner July 8, 2020 10:17
? list
.Replace(
list[^2],
list[^2].AsNode().WithAppendedTrailingTrivia(list[^1].GetTrailingTrivia()))
Copy link
Contributor

Choose a reason for hiding this comment

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

❔ Shouldn't this be leading and trailing trivia?

Suggested change
list[^2].AsNode().WithAppendedTrailingTrivia(list[^1].GetTrailingTrivia()))
list[^2].AsNode().WithAppendedTrailingTrivia(list[^1].GetLeadingTrivia()).WithAppendedTrailingTrivia(list[^1].GetTrailingTrivia()))

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You are right! Fixed.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

By the way, currently some trivia of deleted parts in the initializer list is lost. For example,

            var t1 = new
            {
                // Hi
                a = 1,
                b = 2
            };

is transformed into

            var t1 = new NewClass(
1,
2
            );

Is this considered a bug?

Copy link
Contributor

Choose a reason for hiding this comment

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

yup, i would def consider that a bug :)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Preserving leading trivia from the initializers would also produce more reasonable indentation.

Copy link
Contributor

Choose a reason for hiding this comment

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

yes. i woudl consider preservatin of leading trivia appropriate, as well as the removal of the final trailing trivia of the last element.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Created issue #45792

@sharwell sharwell added Area-IDE Community The pull request was submitted by a contributor who is not a Microsoft employee. labels Jul 8, 2020
Comment on lines +50 to +58
return list.Count >= 2 && list[^1].IsToken
? list
.Replace(
list[^2],
list[^2].AsNode()
.WithAppendedTrailingTrivia(list[^1].GetLeadingTrivia())
.WithAppendedTrailingTrivia(list[^1].GetTrailingTrivia()))
.RemoveAt(list.Count - 1)
: list;
Copy link
Contributor

Choose a reason for hiding this comment

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

this has gotten to bee too much of a mouthful. Can you just have this be:

Suggested change
return list.Count >= 2 && list[^1].IsToken
? list
.Replace(
list[^2],
list[^2].AsNode()
.WithAppendedTrailingTrivia(list[^1].GetLeadingTrivia())
.WithAppendedTrailingTrivia(list[^1].GetTrailingTrivia()))
.RemoveAt(list.Count - 1)
: list;
if (list.Count % 2 == 1)
return list;
// now just return the list without the comma.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

list.Count % 2 == 1 would break the empty list case, so we really need the list.Count >= 2 condition. But I agree that it's better to get rid of the ternary conditional.

Copy link
Contributor

Choose a reason for hiding this comment

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

sounds good.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed.

Comment on lines +1455 to +1456
2
);
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
2
);
2
);

this doesn't seem desirable. If there is only whitespace trivia, i think it would be appropriate to just not transfer that along.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Do you want to change the behavior in the no-trailing-comma case as well? Right now

            var t1 = new
            {
                a = 1,
                b = 2
            };

is transformed into

            var t1 = new NewClass(
1,
2
            );

Also, other refactorings (like "Convert to tuple") have similar behavior, and I feel there should be consistency.

Copy link
Contributor

Choose a reason for hiding this comment

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

i think there is a distinct difference int he patterns used when using { style versus (an initialization list) vs ( style (an argument list). I would prefer we matched the end style we expect people to most commonly use. That said, i'm ok with this being out of scope for this PR as we'd likely need to update a few things at the same time for consistency :)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Agreed, I'll create an issue for that.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Created issue #45792

Copy link
Contributor

@CyrusNajmabadi CyrusNajmabadi left a comment

Choose a reason for hiding this comment

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

Great thanks! LMK when this passes and i'll merge in for you :)

: list;
if (list.Count == 0 || list.Count % 2 == 1)
{
return list;
Copy link
Contributor

Choose a reason for hiding this comment

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

💡

Suggested change
return list;
// The list is either empty, or does not end with a trailing comma
return list;

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed.

@mahalex
Copy link
Contributor Author

mahalex commented Jul 9, 2020

@CyrusNajmabadi looks like the build agents are feeling unwell.

@CyrusNajmabadi
Copy link
Contributor

Thankjs for the PR!

@CyrusNajmabadi CyrusNajmabadi merged commit ecd7f38 into dotnet:master Jul 10, 2020
@ghost ghost added this to the Next milestone Jul 10, 2020
@JoeRobich JoeRobich modified the milestones: Next, 16.8.P1 Jul 20, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Area-IDE Community The pull request was submitted by a contributor who is not a Microsoft employee.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

"Convert to class" refactoring for anonymous types generates a syntax error from trailing comma

4 participants