Skip to content

Instantly share code, notes, and snippets.

@manne
Last active January 28, 2020 22:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save manne/7646b01b7e72664654063d90adec890d to your computer and use it in GitHub Desktop.
Save manne/7646b01b7e72664654063d90adec890d to your computer and use it in GitHub Desktop.
Gets the NullableContext of a certain position
<Query Kind="Program">
<NuGetReference>Microsoft.CodeAnalysis</NuGetReference>
<NuGetReference>Microsoft.CodeAnalysis.Compilers</NuGetReference>
<NuGetReference>Microsoft.CodeAnalysis.CSharp</NuGetReference>
<Namespace>Microsoft.CodeAnalysis</Namespace>
<Namespace>Microsoft.CodeAnalysis.CSharp</Namespace>
<Namespace>Microsoft.CodeAnalysis.CSharp.Syntax</Namespace>
</Query>
void Main()
{
string text = @"
public class MyClass
{
#nullable enable
public string? Get() => null;
#nullable disable
public string GetNonNull() => null;
}";
SyntaxTree tree = CSharpSyntaxTree.ParseText(text);
PortableExecutableReference mscorlib = MetadataReference.CreateFromFile(typeof(object).Assembly.Location);
CSharpCompilation compilation = CSharpCompilation.Create("RefitCompilation", syntaxTrees: new[] { tree }, references: new[] { mscorlib });
SemanticModel semanticModel = compilation.GetSemanticModel(tree);
var methods = tree.GetRoot().DescendantNodes().OfType<MethodDeclarationSyntax>();
MethodDeclarationSyntax nonGenericMethodSyntax = methods.First();
semanticModel.GetNullableContext(nonGenericMethodSyntax.SpanStart).Dump("Should be Enabled");
MethodDeclarationSyntax nonGenericMethodNonNull = methods.Skip(1).First();
semanticModel.GetNullableContext(nonGenericMethodNonNull.SpanStart).Dump("Should be Disabled");
}
<Query Kind="Program">
<NuGetReference>Microsoft.CodeAnalysis</NuGetReference>
<NuGetReference>Microsoft.CodeAnalysis.Compilers</NuGetReference>
<NuGetReference>Microsoft.CodeAnalysis.CSharp</NuGetReference>
<Namespace>Microsoft.CodeAnalysis</Namespace>
<Namespace>Microsoft.CodeAnalysis.CSharp</Namespace>
<Namespace>Microsoft.CodeAnalysis.CSharp.Syntax</Namespace>
</Query>
void Main()
{
string text = @"
public class MyClass
{
public string? Get() => null;
#nullable enable
public string? Get() => null;
#nullable disable
public string GetNonNull() => null;
}";
SyntaxTree tree = CSharpSyntaxTree.ParseText(text);
PortableExecutableReference mscorlib = MetadataReference.CreateFromFile(typeof(object).Assembly.Location);
CSharpCompilation compilation = CSharpCompilation.Create("RefitCompilation", syntaxTrees: new[] { tree }, references: new[] { mscorlib });
SemanticModel semanticModel = compilation.GetSemanticModel(tree);
var methods = tree.GetRoot().DescendantNodes().OfType<MethodDeclarationSyntax>();
MethodDeclarationSyntax nonGenericMethodSyntaxCI = methods.First();
semanticModel.GetNullableContext(nonGenericMethodSyntaxCI.SpanStart).Dump("Should be ContextInherited");
MethodDeclarationSyntax nonGenericMethodSyntax = methods.Skip(1).First();
semanticModel.GetNullableContext(nonGenericMethodSyntax.SpanStart).Dump("Should be Enabled");
MethodDeclarationSyntax nonGenericMethodNonNull = methods.Skip(2).First();
semanticModel.GetNullableContext(nonGenericMethodNonNull.SpanStart).Dump("Should be Disabled");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment