Last active
January 28, 2020 22:06
-
-
Save manne/7646b01b7e72664654063d90adec890d to your computer and use it in GitHub Desktop.
Gets the NullableContext of a certain position
This file contains 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
<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"); | |
} |
This file contains 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
<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