Skip to content

Commit b885da9

Browse files
committed
Support "Enable nullable reference types" from disable keyword
Contributes to #57476
1 parent 81d83ce commit b885da9

File tree

2 files changed

+96
-7
lines changed

2 files changed

+96
-7
lines changed

src/EditorFeatures/CSharpTest/CodeActions/EnableNullable/EnableNullableTests.cs

Lines changed: 94 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,9 @@ public class EnableNullableTests
5252
return solution;
5353
};
5454

55+
private static readonly Func<Solution, ProjectId, Solution> s_enableNullableInFixedSolutionFromDisableKeyword =
56+
s_enableNullableInFixedSolutionFromRestoreKeyword;
57+
5558
[Theory]
5659
[InlineData("$$#nullable enable")]
5760
[InlineData("#$$nullable enable")]
@@ -712,16 +715,103 @@ class Example4
712715
[InlineData("#nullable $$disable")]
713716
[InlineData("#nullable dis$$able")]
714717
[InlineData("#nullable disable$$")]
715-
public async Task DisabledOnNullableDisable(string directive)
718+
public async Task EnabledOnNullableDisable(string directive)
716719
{
717-
var code = $@"
720+
var code1 = $@"
718721
{directive}
722+
723+
class Example
724+
{{
725+
string value;
726+
}}
727+
728+
#nullable restore
729+
";
730+
var code2 = @"
731+
class Example2
732+
{
733+
string value;
734+
}
735+
";
736+
var code3 = @"
737+
class Example3
738+
{
739+
#nullable enable
740+
string? value;
741+
#nullable restore
742+
}
743+
";
744+
var code4 = @"
745+
#nullable disable
746+
747+
class Example4
748+
{
749+
string value;
750+
}
751+
";
752+
753+
var fixedDirective = directive.Replace("$$", "");
754+
755+
var fixedCode1 = $@"
756+
{fixedDirective}
757+
758+
class Example
759+
{{
760+
string value;
761+
}}
762+
763+
#nullable disable
764+
";
765+
var fixedCode2 = @"
766+
#nullable disable
767+
768+
class Example2
769+
{
770+
string value;
771+
}
772+
";
773+
var fixedCode3 = @"
774+
#nullable disable
775+
776+
class Example3
777+
{
778+
#nullable restore
779+
string? value;
780+
#nullable disable
781+
}
782+
";
783+
var fixedCode4 = @"
784+
#nullable disable
785+
786+
class Example4
787+
{
788+
string value;
789+
}
719790
";
720791

721792
await new VerifyCS.Test
722793
{
723-
TestCode = code,
724-
FixedCode = code,
794+
TestState =
795+
{
796+
Sources =
797+
{
798+
code1,
799+
code2,
800+
code3,
801+
code4,
802+
},
803+
},
804+
FixedState =
805+
{
806+
Sources =
807+
{
808+
fixedCode1,
809+
fixedCode2,
810+
fixedCode3,
811+
fixedCode4,
812+
},
813+
},
814+
SolutionTransforms = { s_enableNullableInFixedSolutionFromDisableKeyword },
725815
}.RunAsync();
726816
}
727817
}

src/Features/CSharp/Portable/CodeRefactorings/EnableNullable/EnableNullableCodeRefactoringProvider.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,8 @@ public override async Task ComputeRefactoringsAsync(CodeRefactoringContext conte
5252
if (token.IsKind(SyntaxKind.EndOfDirectiveToken))
5353
token = root.FindToken(textSpan.Start - 1, findInsideTrivia: true);
5454

55-
if (!token.IsKind(SyntaxKind.EnableKeyword, SyntaxKind.RestoreKeyword, SyntaxKind.NullableKeyword, SyntaxKind.HashToken)
56-
|| !token.Parent.IsKind(SyntaxKind.NullableDirectiveTrivia, out NullableDirectiveTriviaSyntax? nullableDirectiveTrivia)
57-
|| !nullableDirectiveTrivia.SettingToken.IsKind(SyntaxKind.EnableKeyword, SyntaxKind.RestoreKeyword))
55+
if (!token.IsKind(SyntaxKind.EnableKeyword, SyntaxKind.RestoreKeyword, SyntaxKind.DisableKeyword, SyntaxKind.NullableKeyword, SyntaxKind.HashToken)
56+
|| !token.Parent.IsKind(SyntaxKind.NullableDirectiveTrivia, out NullableDirectiveTriviaSyntax? nullableDirectiveTrivia))
5857
{
5958
return;
6059
}

0 commit comments

Comments
 (0)