-
Couldn't load subscription status.
- Fork 1.4k
Improved nullability annotations in MVVM Toolkit #4135
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
Merged
michael-hawker
merged 3 commits into
CommunityToolkit:main
from
Sergio0694:improvement/mvvmtoolkit-annotations
Aug 2, 2021
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
56 changes: 56 additions & 0 deletions
56
Microsoft.Toolkit.Mvvm.SourceGenerators/Attributes/NullabilityAttributesGenerator.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
| // See the LICENSE file in the project root for more information. | ||
|
|
||
| using System.IO; | ||
| using System.Linq; | ||
| using System.Reflection; | ||
| using System.Text; | ||
| using Microsoft.CodeAnalysis; | ||
| using Microsoft.CodeAnalysis.Text; | ||
|
|
||
| namespace Microsoft.Toolkit.Mvvm.SourceGenerators | ||
| { | ||
| /// <summary> | ||
| /// A source generator for necessary nullability attributes. | ||
| /// </summary> | ||
| [Generator] | ||
| public sealed class NullabilityAttributesGenerator : ISourceGenerator | ||
| { | ||
| /// <inheritdoc/> | ||
| public void Initialize(GeneratorInitializationContext context) | ||
| { | ||
| } | ||
|
|
||
| /// <inheritdoc/> | ||
| public void Execute(GeneratorExecutionContext context) | ||
| { | ||
| AddSourceCodeIfTypeIsNotPresent(context, "System.Diagnostics.CodeAnalysis.NotNullAttribute"); | ||
| AddSourceCodeIfTypeIsNotPresent(context, "System.Diagnostics.CodeAnalysis.NotNullIfNotNullAttribute"); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Adds the source for a given attribute type if it's not present already in the compilation. | ||
| /// </summary> | ||
| private void AddSourceCodeIfTypeIsNotPresent(GeneratorExecutionContext context, string typeFullName) | ||
| { | ||
| if (context.Compilation.GetTypeByMetadataName(typeFullName) is not null) | ||
| { | ||
| return; | ||
| } | ||
|
|
||
| string | ||
| typeName = typeFullName.Split('.').Last(), | ||
| filename = $"Microsoft.Toolkit.Mvvm.SourceGenerators.EmbeddedResources.{typeName}.cs"; | ||
|
|
||
| Stream stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(filename); | ||
| StreamReader reader = new(stream); | ||
|
|
||
| string | ||
| originalSource = reader.ReadToEnd(), | ||
| outputSource = originalSource.Replace("NETSTANDARD2_0", "true"); | ||
|
|
||
| context.AddSource($"{typeFullName}.cs", SourceText.From(outputSource, Encoding.UTF8)); | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
| // See the LICENSE file in the project root for more information. | ||
|
|
||
| #if NETSTANDARD2_0 | ||
|
|
||
| namespace System.Diagnostics.CodeAnalysis | ||
| { | ||
| /// <summary> | ||
| /// Specifies that an output will not be null even if the corresponding type allows it. | ||
| /// Specifies that an input argument was not null when the call returns. | ||
| /// </summary> | ||
| /// <remarks>Internal copy from the BCL attribute.</remarks> | ||
| [AttributeUsage(AttributeTargets.Field | AttributeTargets.Parameter | AttributeTargets.Property | AttributeTargets.ReturnValue, Inherited = false)] | ||
| internal sealed class NotNullAttribute : Attribute | ||
| { | ||
| } | ||
| } | ||
|
|
||
| #endif |
27 changes: 27 additions & 0 deletions
27
Microsoft.Toolkit.Mvvm/Attributes/NotNullIfNotNullAttribute.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
| // See the LICENSE file in the project root for more information. | ||
|
|
||
| #if NETSTANDARD2_0 | ||
|
|
||
| namespace System.Diagnostics.CodeAnalysis | ||
| { | ||
| /// <summary> | ||
| /// Specifies that the output will be non-null if the named parameter is non-null. | ||
| /// </summary> | ||
| /// <remarks>Internal copy from the BCL attribute.</remarks> | ||
| [AttributeUsage(AttributeTargets.Parameter | AttributeTargets.Property | AttributeTargets.ReturnValue, AllowMultiple = true, Inherited = false)] | ||
| internal sealed class NotNullIfNotNullAttribute : Attribute | ||
| { | ||
| /// <summary> | ||
| /// Initializes a new instance of the <see cref="NotNullIfNotNullAttribute"/> class. | ||
| /// </summary> | ||
| /// <param name="parameterName"> The associated parameter name. The output will be non-null if the argument to the parameter specified is non-null.</param> | ||
| public NotNullIfNotNullAttribute(string parameterName) => ParameterName = parameterName; | ||
|
|
||
| /// <summary>Gets the associated parameter name.</summary> | ||
| public string ParameterName { get; } | ||
| } | ||
| } | ||
|
|
||
| #endif |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.