Skip to content

Commit

Permalink
Respect PreferParameterNullChecking user option
Browse files Browse the repository at this point in the history
  • Loading branch information
Youssef1313 committed Feb 15, 2022
1 parent d4440b7 commit b7be789
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,30 +29,50 @@ public async Task TestEmptyFile()
await VerifyCS.VerifyRefactoringAsync(code, code);
}

[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsInitializeParameter)]
public async Task TestSimpleReferenceType()
[Theory, Trait(Traits.Feature, Traits.Features.CodeActionsInitializeParameter)]
[InlineData("", true)]
[InlineData("csharp_style_prefer_parameter_null_checking = true", true)]
[InlineData("csharp_style_prefer_parameter_null_checking = false", false)]
public async Task TestSimpleReferenceType(string editorConfig, bool useParameterNullChecking)
{
await new VerifyCS.Test
{
LanguageVersion = LanguageVersionExtensions.CSharpNext,
TestCode = @"
var fixedCode = useParameterNullChecking ? @"
using System;
class C
{
public C([||]string s)
public C(string s!!)
{
}
}",
FixedCode = @"
}" : @"
using System;
class C
{
public C(string s!!)
public C(string s)
{
if (s is null)
{
throw new ArgumentNullException(nameof(s));
}
}
}";
await new VerifyCS.Test
{
LanguageVersion = LanguageVersionExtensions.CSharpNext,
TestCode = @"
using System;
class C
{
public C([||]string s)
{
}
}"
}",
FixedCode = fixedCode,
EditorConfig = $@"
[*]
{editorConfig}
",
}.RunAsync();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System;
using System.Composition;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.CodeRefactorings;
using Microsoft.CodeAnalysis.CSharp.CodeStyle;
using Microsoft.CodeAnalysis.CSharp.Extensions;
Expand Down Expand Up @@ -102,14 +103,18 @@ protected override StatementSyntax CreateParameterCheckIfStatement(DocumentOptio
@else: null);
}

protected override Document? TryAddNullCheckToParameterDeclaration(Document document, ParameterSyntax parameterSyntax, CancellationToken cancellationToken)
protected override async Task<Document?> TryAddNullCheckToParameterDeclarationAsync(Document document, ParameterSyntax parameterSyntax, CancellationToken cancellationToken)
{
var tree = parameterSyntax.SyntaxTree;
if (!tree.Options.LanguageVersion().IsCSharp11OrAbove())
return null;

var options = await document.GetOptionsAsync(cancellationToken).ConfigureAwait(false);
if (!options.GetOption(CSharpCodeStyleOptions.PreferParameterNullChecking).Value)
return null;

// We expect the syntax tree to already be in memory since we already have a node from the tree
var syntaxRoot = tree.GetRoot(cancellationToken);
var syntaxRoot = await tree.GetRootAsync(cancellationToken).ConfigureAwait(false);
syntaxRoot = syntaxRoot.ReplaceNode(
parameterSyntax,
parameterSyntax.WithExclamationExclamationToken(Token(SyntaxKind.ExclamationExclamationToken)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ internal abstract partial class AbstractAddParameterCheckCodeRefactoringProvider
protected abstract bool PrefersThrowExpression(DocumentOptionSet options);
protected abstract string EscapeResourceString(string input);
protected abstract TStatementSyntax CreateParameterCheckIfStatement(DocumentOptionSet options, TExpressionSyntax condition, TStatementSyntax ifTrueStatement);
protected abstract Document? TryAddNullCheckToParameterDeclaration(Document document, TParameterSyntax parameterSyntax, CancellationToken cancellationToken);
protected abstract Task<Document?> TryAddNullCheckToParameterDeclarationAsync(Document document, TParameterSyntax parameterSyntax, CancellationToken cancellationToken);

protected override async Task<ImmutableArray<CodeAction>> GetRefactoringsForAllParametersAsync(
Document document,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.InitializeParameter
elseBlock:=Nothing)
End Function

Protected Overrides Function TryAddNullCheckToParameterDeclaration(document As Document, parameterSyntax As ParameterSyntax, cancellationToken As CancellationToken) As Document
Protected Overrides Function TryAddNullCheckToParameterDeclarationAsync(document As Document, parameterSyntax As ParameterSyntax, cancellationToken As CancellationToken) As Task(Of Document)
Return Nothing
End Function
End Class
Expand Down

0 comments on commit b7be789

Please sign in to comment.