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

Remove temporary strings in source generators formatting #281

Merged
merged 2 commits into from
May 30, 2022

Conversation

Sergio0694
Copy link
Member

See dotnet/roslyn-analyzers#6017 and Sergio0694/ComputeSharp#297.

cc. @Neme12

PR Checklist

  • Created a feature/dev branch in your fork (vs. submitting directly from a commit on main)
  • Based off latest main branch of toolkit
  • PR doesn't include merge commits (always rebase on top of our main, if needed)
  • Tested code with current supported SDKs
  • New component
    • Pull Request has been submitted to the documentation repository instructions. Link:
    • Added description of major feature to project description for NuGet package (4000 total character limit, so don't push entire description over that)
  • Tests for the changes have been added (for bug fixes / features) (if applicable)
  • Header has been added to all new source files (run build/UpdateHeaders.bat)
  • Contains NO breaking changes
  • Every new API (including internal ones) has full XML docs
  • Code follows all style conventions

@Sergio0694 Sergio0694 added next preview ✈️ This changes will be available in the upcoming preview optimization ☄ Performance or memory usage improvements mvvm-toolkit 🧰 Issues/PRs for the MVVM Toolkit labels May 28, 2022

// Adjust the visibility of the constructors based on whether the target type is abstract.
// If that is not the case, the constructors have to be declared as public and not protected.
if (!info.IsAbstract)
{
replaced = replaced.Replace("protected", "public");
modifiedConstructor = modifiedConstructor.WithModifiers(TokenList(Token(SyntaxKind.PublicKeyword)));
Copy link

Choose a reason for hiding this comment

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

What if the constructor has other modifiers as well, like unsafe? This would remove all of them. Or is this constructor not from user code but generated by the generator and it's guaranteed that it won't have other modifiers?

Copy link

Choose a reason for hiding this comment

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

If preserving other modifiers is needed, try something like this:

modifiedConstructor = modifiedConstructor.WithModifiers(
    modifiedConstructor.Modifiers.Replace(
        modifiedConstructor.Modifiers.First(token => token.Kind() is SyntaxKind.ProtectedKeyword),
        SyntaxFactory.Token(SyntaxKind.PublicKeyword)));

Copy link
Member Author

Choose a reason for hiding this comment

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

The syntax tree is from the ObservableRecipient.cs embedded resource file, so yeah we can assume that protected is the only modifier that could possibly be there. These trees are not provided by code parsed from consumers 🙂

}

builder.Add((ConstructorDeclarationSyntax)ParseMemberDeclaration(replaced)!);
Copy link

Choose a reason for hiding this comment

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

Ugh it was being reparsed just to replace a token... I'm glad this is fixed 😄

Copy link
Member Author

Choose a reason for hiding this comment

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

Yup, looking back at this it was indeed quite bad ahah

@Sergio0694 Sergio0694 merged commit 7efbb55 into main May 30, 2022
@delete-merged-branch delete-merged-branch bot deleted the dev/remove-generator-temp-strings branch May 30, 2022 16:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
mvvm-toolkit 🧰 Issues/PRs for the MVVM Toolkit next preview ✈️ This changes will be available in the upcoming preview optimization ☄ Performance or memory usage improvements
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants