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

Add [GeneratedCode] and [ExcludeFromCodeCoverage] to Name Generator output members #15616

Merged
merged 1 commit into from
May 5, 2024
Merged

Add [GeneratedCode] and [ExcludeFromCodeCoverage] to Name Generator output members #15616

merged 1 commit into from
May 5, 2024

Conversation

stevemonaco
Copy link
Contributor

What does the pull request do?

Adds attributes to output of both Name Generators (InitializeComponentCodeGenerator and OnlyPropertiesCodeGenerator).

  • Generated fields now have the [GeneratedCode] attribute.
  • Generated InitializeComponent method now has [GeneratedCode] and [ExcludeFromCodeCoverage]

What is the updated/expected behavior with this PR?

Code generated by InitializeComponentCodeGenerator now looks like:

namespace Sandbox
{
    partial class MainWindow
    {
        [global::System.CodeDom.Compiler.GeneratedCode("Avalonia.Generators.NameGenerator.InitializeComponentCodeGenerator", "11.2.999.0")]
        internal global::Avalonia.Controls.Grid grid;
        [global::System.CodeDom.Compiler.GeneratedCode("Avalonia.Generators.NameGenerator.InitializeComponentCodeGenerator", "11.2.999.0")]
        internal global::Avalonia.Controls.TextBlock text;

        /// <summary>
        /// Wires up the controls and optionally loads XAML markup and attaches dev tools (if Avalonia.Diagnostics package is referenced).
        /// </summary>
        /// <param name="loadXaml">Should the XAML be loaded into the component.</param>
        /// <param name="attachDevTools">Should the dev tools be attached.</param>

        [global::System.CodeDom.Compiler.GeneratedCode("Avalonia.Generators.NameGenerator.InitializeComponentCodeGenerator", "11.2.999.0")]
        [global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
        public void InitializeComponent(bool loadXaml = true, bool attachDevTools = true)
        {
            if (loadXaml)
            {
                AvaloniaXamlLoader.Load(this);
            }

#if DEBUG
            if (attachDevTools)
            {
                this.AttachDevTools();
            }
#endif

            var __thisNameScope__ = this.FindNameScope();
            grid = __thisNameScope__?.Find<global::Avalonia.Controls.Grid>("grid");
            text = __thisNameScope__?.Find<global::Avalonia.Controls.TextBlock>("text");
        }
    }
}

How was the solution implemented (if it's not obvious)?

I mostly tried to match what Mvvm Toolkit is doing for its output, though this generator is still on ISourceGenerator instead of incremental. Code strings were built similar to the existing parts of the generators instead of Roslyn AST.

The unit tests and unit test data required updating. The version parameter of [GeneratedCode] will be ever-changing, so a $GeneratorVersion tag was used in test code files. Mvvm Toolkit uses <ASSEMBLY_VERSION> with a similar replacement strategy at test-time.

Checklist

  • Updated unit tests

Breaking changes

None

Fixed issues

Fixes #15611

@cla-avalonia
Copy link
Collaborator

cla-avalonia commented May 5, 2024

  • All contributors have signed the CLA.

@stevemonaco
Copy link
Contributor Author

@cla-avalonia agree

@avaloniaui-bot
Copy link

You can test this PR using the following package version. 11.2.999-cibuild0048102-alpha. (feed url: https://nuget-feed-all.avaloniaui.net/v3/index.json) [PRBUILDID]

@AvaloniaUI AvaloniaUI deleted a comment from dotnet-policy-service bot May 5, 2024
@kekekeks kekekeks added this pull request to the merge queue May 5, 2024
Merged via the queue into AvaloniaUI:master with commit 0ced67f May 5, 2024
10 of 11 checks passed
Kir-Antipov added a commit to Kir-Antipov/HotAvalonia that referenced this pull request May 5, 2024
@maxkatz6 maxkatz6 added the backport-candidate-11.1.x Consider this PR for backporting to 11.1 branch label May 6, 2024
maxkatz6 pushed a commit that referenced this pull request May 8, 2024
@maxkatz6 maxkatz6 added backported-11.1.x and removed backport-candidate-11.1.x Consider this PR for backporting to 11.1 branch labels May 8, 2024
@stevemonaco stevemonaco deleted the namegen-attributes branch May 13, 2024 00:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Source generators should annotate generated types/members with [GeneratedCode]
5 participants