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

Added support to normalize auto properties on a single line. #49495

Merged
merged 17 commits into from
Dec 8, 2020
Merged

Added support to normalize auto properties on a single line. #49495

merged 17 commits into from
Dec 8, 2020

Conversation

raffaeler
Copy link
Contributor

The discussion started here: #49468

The work consisted in:

  • Added support in the SyntaxNormalizer class for the auto-properties (the ones who have null Body property)
  • Added more tests for properties including init accessor and initializers
  • Fixed the two existing tests (property and indexer) to reflect the changes

The following comparisons shows the results of the actual tests, before and after this PR.

Before:
class a
{
  b c
  {
    get;
  }
}

After:
class a
{
  b c { get; }
}

Before:
class a
{
  int X
  {
    get;
    set;
  }

  = 2;
}

After:
class a
{
  int X { get; set; } = 2;
}
Before:
class a
{
  int Y
  {
    get;
    set;
  }

  = 99;
}

After:
class a
{
  int Y { get; set; } = 99;
}
Before:
class a
{
  int Z
  {
    get;
  }
}

After:
class a
{
  int Z { get; }
}

Before:
class a
{
  int T
  {
    get;
    init;
  }

  int R
  {
    get => 1;
  }
}

After:
class a
{
  int T { get; init; }

  int R { get => 1; }
}

Before:
class a
{
  int Q
  {
    get
    {
      return 0;
    }

    init
    {
    }
  }

  int R
  {
    get => 1;
  }
}

After:
class a
{
  int Q
  {
    get
    {
      return 0;
    }

    init
    {
    }
  }

  int R { get => 1; }
}

Before:
class a
{
  int R
  {
    get => 1;
  }
}

After:
class a
{
  int R { get => 1; }
}
Before:
class a
{
  int S => 2;
}

After:
class a
{
  int S => 2;
}
Before:
class a
{
  b this[c d]
  {
    get;
  }
}

After:
class a
{
  b this[c d] { get; }
}


Added more tests including init accessor and initializers
Changed two existing tests to reflect the changes
Co-authored-by: CyrusNajmabadi <cyrus.najmabadi@gmail.com>
@CyrusNajmabadi
Copy link
Member

in general, the appraoch is good. just needs more clarity :)

@raffaeler
Copy link
Contributor Author

@CyrusNajmabadi I see errors in the Linux/OSX builds but have no idea why. Do they depend on my PR?

@CyrusNajmabadi
Copy link
Member

Yes, you're failing some test in Microsoft.CodeAnalysis.CSharp.Syntax.UnitTests. This should also trigger in the other tests to state what test actually failed.

@raffaeler
Copy link
Contributor Author

I see them locally. They are TestSpacingOnNullableDatetimeType and TestSpacingOnNullableIntType. I have to fix the expected result as, of course, the formatting has changed for auto properties.
Is that okay for you?

…use of the auto-properties formatting is now different:

TestSpacingOnNullableDatetimeType, TestSpacingOnNullableIntType
@CyrusNajmabadi
Copy link
Member

Is that okay for you?

Yup. If it's adjusting the behavior for just these new constructs, that would be fine. Thanks! :)

@raffaeler
Copy link
Contributor Author

The check was easy and made sense, so I proceeded. Thank you

@CyrusNajmabadi
Copy link
Member

CyrusNajmabadi commented Nov 19, 2020

Thank you

You're welcome! Thank you for the PR :)

@sharwell sharwell changed the title Added support to format auto properties on a single line. Added support to normalize auto properties on a single line. Nov 19, 2020
@raffaeler
Copy link
Contributor Author

Apparently there is another set of tests suffering from this changes, but this time I run the test locally and they succeeded.

Microsoft.CodeAnalysis.EditorFeatures2.UnitTests
  Tests in group: 7838
   Total Duration: 8 min

Outcomes
   7788 Passed
   50 Skipped

Fail message:

Determinism failed for the following binaries:
	Microsoft.CodeAnalysis.EditorFeatures2.UnitTests.dll

@CyrusNajmabadi do you have suggestions?

@CyrusNajmabadi
Copy link
Member

This is not on you. @jaredpar we have a determinism failure as per:

Determinism failed for the following binaries:
Microsoft.CodeAnalysis.EditorFeatures2.UnitTests.dll

How do you generally handle this?

@raffaeler
Copy link
Contributor Author

I am trying to re-trigger the tests because I saw the failed test is the following one and I can't see how this PR could affect it.

[WpfFact]
public void BclMathCall()
{
    VisualStudio.InteractiveWindow.SubmitText("Math.Sin(1)");
    VisualStudio.InteractiveWindow.WaitForLastReplOutput("0.8414709848078965");
}

@333fred
Copy link
Member

333fred commented Nov 30, 2020

I am trying to re-trigger the tests because I saw the failed test is the following one and I can't see how this PR could affect it.

In the future, just let us know and don't use commits to retrigger. We'll take a look and retrigger if necessary.

@raffaeler
Copy link
Contributor Author

In the future, just let us know and don't use commits to retrigger. We'll take a look and retrigger if necessary.

Sorry, I did it because it failed multiple times last week for random reasons (but not becuse of the PR) and I committed the requested changes in the middle of US festivities.
Should it happen again, I won't do it.

@333fred
Copy link
Member

333fred commented Nov 30, 2020

Sorry, I did it because it failed multiple times last week for random reasons (but not becuse of the PR) and I committed the requested changes in the middle of US festivities.
Should it happen again, I won't do it.

No worries, just letting you know for the future.

Comment on lines 282 to 284
=> node is AccessorListSyntax accessorList &&
accessorList.Parent is BasePropertyDeclarationSyntax basePropertyDeclarationSyntax &&
HasInitializer(basePropertyDeclarationSyntax);
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
=> node is AccessorListSyntax accessorList &&
accessorList.Parent is BasePropertyDeclarationSyntax basePropertyDeclarationSyntax &&
HasInitializer(basePropertyDeclarationSyntax);
=> node is AccessorListSyntax &&
node.Parent is PropertyDeclarationSyntax property &&
property.Initializer != null;

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@CyrusNajmabadi Are you sure about this?
This prevents any initializers outside the properties (if they will ever exists)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@AlekseyTs what do you think about this?
I was searching to avoid looking for properties on purpose so that any derived class from BasePropertyDeclarationSyntax having an accessor fit in the inline formatting.

Copy link
Contributor

Choose a reason for hiding this comment

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

what do you think about this?

I am fine with the code and names as it is.


In reply to: 534376783 [](ancestors = 534376783)

Copy link
Member

Choose a reason for hiding this comment

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

This prevents any initializers outside the properties (if they will ever exists)

your existing code already prevents taht as you do a type check for PropertyDeclarationSyntax yourself in HasInitializer.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

your existing code already prevents taht as you do a type check for PropertyDeclarationSyntax yourself in HasInitializer.

Sure, but is well identified in that point. Currently I can't do differently because properties arei the only one allowed.
Could in the future be possible, let's say, for indexers to be initialized, the changes would be limited to a single point and this function would work correctly.
I don't see how the current version could cause problems. Let me know if you want me to change it anyway, so that I can send the commit before night (here). Thanks

Copy link
Member

Choose a reason for hiding this comment

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

the changes would be limited to a single point and this function would work correctly.

that is true with the change i suggested as well. in both cases we will need to update the code. so might as well have hte code be correct and precise for the trees we have today.

Let me know if you want me to change it anyway

Yes please :)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Okay, and what about the method names?

Copy link
Member

Choose a reason for hiding this comment

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

i'll defer to Aleksey's judgement on those :)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@AlekseyTs I committed all the required changes but not the method renaming.
It's up to you now :)

@CyrusNajmabadi
Copy link
Member

tests LGTM. code is almost there. If you would like someone to pick this up to complete this @raffaeler i would be happy to help.

If you'd prefer to finish it yourself, that's 100% ok. I just dont' want you to burn out from all the nitpicking i give :)

THanks!

@raffaeler
Copy link
Contributor Author

tests LGTM. code is almost there. If you would like someone to pick this up to complete this @raffaeler i would be happy to help.

no problems, thanks. At this point I want to see the end :)

If you'd prefer to finish it yourself, that's 100% ok. I just dont' want you to burn out from all the nitpicking i give :)

Since I startyed renaming on my side, I am not committing your suggestions to avoid merging. That's all, but thanks :)

Copy link
Contributor

@AlekseyTs AlekseyTs left a comment

Choose a reason for hiding this comment

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

LGTM (commit 17)

@AlekseyTs
Copy link
Contributor

@dotnet/roslyn-compiler For the second review. Note, when merging this PR we need to squash commits and adjust the commit title to reflect the broader impact of this change.

@AlekseyTs AlekseyTs added the Community The pull request was submitted by a contributor who is not a Microsoft employee. label Dec 4, 2020
@AlekseyTs
Copy link
Contributor

@dotnet/roslyn-compiler For the second review of a small community PR. Note, when merging this PR we need to squash commits and adjust the commit title to reflect the broader impact of this change.

@AlekseyTs AlekseyTs requested a review from a team December 8, 2020 15:31
@AlekseyTs
Copy link
Contributor

@dotnet/roslyn-compiler For the second review of a small community PR. Note, when merging this PR we need to squash commits and adjust the commit title to reflect the broader impact of this change.

@AlekseyTs AlekseyTs merged commit fc5c2af into dotnet:master Dec 8, 2020
@ghost ghost added this to the Next milestone Dec 8, 2020
@AlekseyTs
Copy link
Contributor

@raffaeler Thanks for the contribution.

@raffaeler
Copy link
Contributor Author

Thank you @AlekseyTs and @CyrusNajmabadi :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-Compilers 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.

7 participants