Skip to content
This repository has been archived by the owner on Dec 12, 2020. It is now read-only.

Add example for a generator respecting nullability #190

Closed
wants to merge 4 commits into from

Conversation

manne
Copy link
Contributor

@manne manne commented Jan 28, 2020

This PR adds a generator respecting the nullability context of the class.

For the class NullableEnabled where #nullable enable it generates a method string? DoSomething(string?). In the generated code nullable is enabled and restored properly.

For the class NullableDisabled where #nullable disable it generates a method string DoSomething(string).

can solve issue #183 . I did not read the whole conversation properly 🤨

@amis92
Copy link
Collaborator

amis92 commented Jan 28, 2020

So, this example is fine and a good addition. It doesn't fix #183, however.

That would require passing around both LangVersion and Nullable msbuild properties, starting in .targets, through dotnet-codegen up to the creation of Compilation.

@manne
Copy link
Contributor Author

manne commented Jan 28, 2020

The following code/file is generated

// ------------------------------------------------------------------------------
// <auto-generated>
//     This code was generated by a tool.
//
//     Changes to this file may cause incorrect behavior and will be lost if
//     the code is regenerated.
// </auto-generated>
// ------------------------------------------------------------------------------

namespace CodeGeneration.Roslyn.Tests.OtherFolder
{
    using System;
    using System.Diagnostics;
    using CodeGeneration.Roslyn.Tests.Generators;
    using Xunit;

    public partial class NullableEnabled
    {
#nullable enable
        public string? DoSomething(string? value)
        {
            return value;
        }
#nullable restore

#nullable enable
        public string? DoSomethingStrict(string value)
        {
            return value;
        }
#nullable restore
    }
}

namespace CodeGeneration.Roslyn.Tests.OtherFolder
{
    using System;
    using System.Diagnostics;
    using CodeGeneration.Roslyn.Tests.Generators;
    using Xunit;

    public partial class NullableDisabled
    {
        public string DoSomething(string value)
        {
            return value;
        }

        public string DoSomethingStrict(string value)
        {
            return value;
        }
    }
}

@manne manne marked this pull request as ready for review January 28, 2020 21:52
@amis92
Copy link
Collaborator

amis92 commented Jan 28, 2020

I think that since we need to check that the warning is generated (if we don't specify #pragma warning disable), we should write tests more like DocumentTransformTests are, with the use of CompilationTestsBase.

So, inputs and outputs are just strings that contain source code, and we could validate that the resulting compilation has no errors/warnings by checking diagnostics.

It may be probably done in another PR, though.

@AArnott
Copy link
Owner

AArnott commented Dec 11, 2020

This project is getting archived in favor of Source Generators. See #229

@AArnott AArnott closed this Dec 11, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants