Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Issue #41822 Add IsNullOrEmpty or IsNullOrWhiteSpace check for method parameters throws an empty "message" #42101

Merged
14 commits merged into from
Apr 21, 2020
Merged
Original file line number Diff line number Diff line change
Expand Up @@ -321,29 +321,29 @@ public C([||]string a, string b, string c)
{
}
}",
@"
@$"
using System;

class C
{
{{
public C(string a, string b, string c)
{
{{
if (string.IsNullOrEmpty(a))
{
throw new ArgumentException(""message"", nameof(a));
}
{{
throw new ArgumentException($""{string.Format(FeaturesResources._0_cannot_be_null_or_empty, "{nameof(a)}")}"", nameof(a));
}}

if (string.IsNullOrEmpty(b))
{
throw new ArgumentException(""message"", nameof(b));
}
{{
throw new ArgumentException($""{string.Format(FeaturesResources._0_cannot_be_null_or_empty, "{nameof(b)}")}"", nameof(b));
}}

if (string.IsNullOrEmpty(c))
{
throw new ArgumentException(""message"", nameof(c));
}
}
}", index: 3);
{{
throw new ArgumentException($""{string.Format(FeaturesResources._0_cannot_be_null_or_empty, "{nameof(c)}")}"", nameof(c));
}}
}}
}}", index: 3);
}

[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsInitializeParameter)]
Expand Down Expand Up @@ -377,24 +377,24 @@ public C(string a, [||]bool b, string c)
{
}
}",
@"
@$"
using System;

class C
{
{{
public C(string a, bool b, string c)
{
{{
if (string.IsNullOrEmpty(a))
{
throw new ArgumentException(""message"", nameof(a));
}
{{
throw new ArgumentException($""{string.Format(FeaturesResources._0_cannot_be_null_or_empty, "{nameof(a)}")}"", nameof(a));
}}

if (string.IsNullOrEmpty(c))
{
throw new ArgumentException(""message"", nameof(c));
}
}
}", index: 0);
{{
throw new ArgumentException($""{string.Format(FeaturesResources._0_cannot_be_null_or_empty, "{nameof(c)}")}"", nameof(c));
}}
}}
}}", index: 0);
}

[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsInitializeParameter)]
Expand All @@ -411,26 +411,27 @@ public C([||]string a, bool b, string c)
{
}
}",
@"
@$"
using System;

class C
{
{{
public C(string a, bool b, string c)
{
{{
if (string.IsNullOrEmpty(a))
{
throw new ArgumentException(""message"", nameof(a));
}
{{
throw new ArgumentException($""{string.Format(FeaturesResources._0_cannot_be_null_or_empty, "{nameof(a)}")}"", nameof(a));
}}

if (string.IsNullOrEmpty(c))
{
throw new ArgumentException(""message"", nameof(c));
}
}
}", index: 3);
{{
throw new ArgumentException($""{string.Format(FeaturesResources._0_cannot_be_null_or_empty, "{nameof(c)}")}"", nameof(c));
}}
}}
}}", index: 3);

}

[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsInitializeParameter)]
public async Task TestMultiNullableStringsAndObjects()
{
Expand All @@ -445,29 +446,29 @@ public C([||]string a, object b, string c)
{
}
}",
@"
@$"
using System;

class C
{
{{
public C(string a, object b, string c)
{
{{
if (string.IsNullOrEmpty(a))
{
throw new ArgumentException(""message"", nameof(a));
}
{{
throw new ArgumentException($""{string.Format(FeaturesResources._0_cannot_be_null_or_empty, "{nameof(a)}")}"", nameof(a));
}}

if (b is null)
{
{{
throw new ArgumentNullException(nameof(b));
}
}}

if (string.IsNullOrEmpty(c))
{
throw new ArgumentException(""message"", nameof(c));
}
}
}", index: 3);
{{
throw new ArgumentException($""{string.Format(FeaturesResources._0_cannot_be_null_or_empty, "{nameof(c)}")}"", nameof(c));
}}
}}
}}", index: 3);
}

[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsInitializeParameter)]
Expand Down Expand Up @@ -1391,19 +1392,19 @@ public C([||]string s)
{
}
}",
@"
@$"
using System;

class C
{
{{
public C(string s)
{
{{
if (string.IsNullOrEmpty(s))
{
throw new ArgumentException(""message"", nameof(s));
}
}
}", index: 1);
{{
throw new ArgumentException($""{string.Format(FeaturesResources._0_cannot_be_null_or_empty, "{nameof(s)}")}"", nameof(s));
}}
}}
}}", index: 1);
}

[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsInitializeParameter)]
Expand All @@ -1419,19 +1420,19 @@ public C([||]string s)
{
}
}",
@"
@$"
using System;

class C
{
{{
public C(string s)
{
{{
if (string.IsNullOrWhiteSpace(s))
{
throw new ArgumentException(""message"", nameof(s));
}
}
}", index: 2);
{{
throw new ArgumentException($""{string.Format(FeaturesResources._0_cannot_be_null_or_whitespace, "{nameof(s)}")}"", nameof(s));
}}
}}
}}", index: 2);
}

[WorkItem(19173, "https://github.com/dotnet/roslyn/issues/19173")]
Expand Down Expand Up @@ -1466,19 +1467,19 @@ static void Main([||]String bar)
{
}
}",
@"
@$"
using System;

class Program
{
{{
static void Main(String bar)
{
{{
if (String.IsNullOrEmpty(bar))
{
throw new ArgumentException(""message"", nameof(bar));
}
}
}", index: 1,
{{
throw new ArgumentException($""{string.Format(FeaturesResources._0_cannot_be_null_or_empty, "{nameof(bar)}")}"", nameof(bar));
}}
}}
}}", index: 1,
parameters: new TestParameters(
options: Option(
CodeStyleOptions2.PreferIntrinsicPredefinedTypeKeywordInMemberAccess,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -434,13 +434,13 @@ class C
public sub new([||]s as string)
end sub
end class",
"
$"
Imports System

class C
public sub new(s as string)
If String.IsNullOrEmpty(s) Then
Throw New ArgumentException(""message"", NameOf(s))
Throw New ArgumentException($""{String.Format(FeaturesResources._0_cannot_be_null_or_empty, "{NameOf(s)}")}"", NameOf(s))
End If
end sub
end class", index:=1)
Expand All @@ -456,13 +456,13 @@ class C
public sub new([||]s as string)
end sub
end class",
"
$"
Imports System

class C
public sub new(s as string)
If String.IsNullOrWhiteSpace(s) Then
Throw New ArgumentException(""message"", NameOf(s))
Throw New ArgumentException($""{String.Format(FeaturesResources._0_cannot_be_null_or_whitespace, "{NameOf(s)}")}"", NameOf(s))
End If
end sub
end class", index:=2)
Expand All @@ -478,21 +478,21 @@ class C
public sub new([||]a as string, b as string, c as string)
end sub
end class",
"
$"
Imports System

class C
public sub new(a as string, b as string, c as string)
If String.IsNullOrEmpty(a) Then
Throw New ArgumentException(""message"", NameOf(a))
Throw New ArgumentException($""{String.Format(FeaturesResources._0_cannot_be_null_or_empty, "{NameOf(a)}")}"", NameOf(a))
End If

If String.IsNullOrEmpty(b) Then
Throw New ArgumentException(""message"", NameOf(b))
Throw New ArgumentException($""{String.Format(FeaturesResources._0_cannot_be_null_or_empty, "{NameOf(b)}")}"", NameOf(b))
End If

If String.IsNullOrEmpty(c) Then
Throw New ArgumentException(""message"", NameOf(c))
Throw New ArgumentException($""{String.Format(FeaturesResources._0_cannot_be_null_or_empty, "{NameOf(c)}")}"", NameOf(c))
End If
end sub
end class", index:=3)
Expand All @@ -508,13 +508,13 @@ class C
public sub new([||]a as boolean, b as string, c as object)
end sub
end class",
"
$"
Imports System

class C
public sub new(a as boolean, b as string, c as object)
If String.IsNullOrEmpty(b) Then
Throw New ArgumentException(""message"", NameOf(b))
Throw New ArgumentException($""{String.Format(FeaturesResources._0_cannot_be_null_or_empty, "{NameOf(b)}")}"", NameOf(b))
End If

If c Is Nothing Then
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,12 @@ namespace Microsoft.CodeAnalysis.CSharp.ConvertToInterpolatedString
internal class CSharpConvertConcatenationToInterpolatedStringRefactoringProvider :
AbstractConvertConcatenationToInterpolatedStringRefactoringProvider<ExpressionSyntax>
{
private const string InterpolatedVerbatimText = "$@\"";

[ImportingConstructor]
[SuppressMessage("RoslynDiagnosticsReliability", "RS0033:Importing constructor should be [Obsolete]", Justification = "Used in test code: https://github.com/dotnet/roslyn/issues/42814")]
public CSharpConvertConcatenationToInterpolatedStringRefactoringProvider()
{
}

protected override SyntaxToken CreateInterpolatedStringStartToken(bool isVerbatim)
{
return isVerbatim
? SyntaxFactory.Token(default, SyntaxKind.InterpolatedVerbatimStringStartToken, InterpolatedVerbatimText, InterpolatedVerbatimText, default)
: SyntaxFactory.Token(SyntaxKind.InterpolatedStringStartToken);
}

protected override SyntaxToken CreateInterpolatedStringEndToken()
=> SyntaxFactory.Token(SyntaxKind.InterpolatedStringEndToken);

protected override string GetTextWithoutQuotes(string text, bool isVerbatim, bool isCharacterLiteral)
=> isVerbatim
? text.Substring("@'".Length, text.Length - "@''".Length)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,9 +119,9 @@ protected SyntaxNode CreateInterpolatedString(
{
var syntaxFacts = document.GetLanguageService<ISyntaxFactsService>();
var generator = SyntaxGenerator.GetGenerator(document);
var startToken = CreateInterpolatedStringStartToken(isVerbatimStringLiteral)
var startToken = generator.CreateInterpolatedStringStartToken(isVerbatimStringLiteral)
.WithLeadingTrivia(pieces.First().GetLeadingTrivia());
var endToken = CreateInterpolatedStringEndToken()
var endToken = generator.CreateInterpolatedStringEndToken()
.WithTrailingTrivia(pieces.Last().GetTrailingTrivia());

var content = new List<SyntaxNode>(pieces.Count);
Expand Down Expand Up @@ -175,8 +175,6 @@ private static SyntaxNode ConcatinateTextToTextNode(SyntaxGenerator generator, S
}

protected abstract string GetTextWithoutQuotes(string text, bool isVerbatimStringLiteral, bool isCharacterLiteral);
protected abstract SyntaxToken CreateInterpolatedStringStartToken(bool isVerbatimStringLiteral);
protected abstract SyntaxToken CreateInterpolatedStringEndToken();

private void CollectPiecesDown(
ISyntaxFactsService syntaxFacts,
Expand Down
6 changes: 6 additions & 0 deletions src/Features/Core/Portable/FeaturesResources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -2744,4 +2744,10 @@ Zero-width positive lookbehind assertions are typically used at the beginning of
<data name="Regex_inline_options_short" xml:space="preserve">
<value>inline options</value>
</data>
<data name="_0_cannot_be_null_or_empty" xml:space="preserve">
<value>'{0}' cannot be null or empty</value>
</data>
<data name="_0_cannot_be_null_or_whitespace" xml:space="preserve">
<value>'{0}' cannot be null or whitespace</value>
</data>
</root>
Loading