Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,10 @@ static RegexOptions GetRegexOptionsFromArgument(ImmutableArray<IArgumentOperatio
string optionsLiteral = Literal(((RegexOptions)(int)argument.Value.ConstantValue.Value).ToString());
return SyntaxFactory.ParseExpression(optionsLiteral);
}
else if (argument.Value is ILiteralOperation literalOperation)
{
return literalOperation.Syntax;
}
else
{
return generator.LiteralExpression(argument.Value.ConstantValue.Value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -792,7 +792,7 @@ public async Task LeadingAndTrailingTriviaIsPreservedByFixer()
static class Class
{
public static string CollapseWhitespace(this string text) =>
[|Regex.Replace(text, @"" \s+"" , "" "")|];
[|Regex.Replace(text, "" \\s+"" , "" "")|];
}";

string expectedFixedCode = @"using System.Text.RegularExpressions;
Expand All @@ -808,6 +808,94 @@ public static string CollapseWhitespace(this string text) =>
await VerifyCS.VerifyCodeFixAsync(test, expectedFixedCode);
}

[Fact]
public async Task VerbatimStringLiteralSyntaxPreservedByFixer()
{
string test = @"using System.Text.RegularExpressions;

static class Class
{
public static string CollapseWhitespace(this string text) =>
[|Regex.Replace(text, @"" \s+"" , @"" "")|];
}";

string expectedFixedCode = @"using System.Text.RegularExpressions;

static partial class Class
{
public static string CollapseWhitespace(this string text) =>
MyRegex().Replace(text, @"" "");
[GeneratedRegex(@"" \s+"")]
private static partial Regex MyRegex();
}";

await VerifyCS.VerifyCodeFixAsync(test, expectedFixedCode);
}

[Fact]
public async Task RawStringLiteralSyntaxPreservedByFixer()
{
string test = @"using System.Text.RegularExpressions;

static class Class
{
public static string CollapseWhitespace(this string text) =>
[|Regex.Replace(text, """"""
\s+
"""""",
"""""""" hello """""" world """""""")|];
}";

string expectedFixedCode = @"using System.Text.RegularExpressions;

static partial class Class
{
public static string CollapseWhitespace(this string text) =>
MyRegex().Replace(text, """""""" hello """""" world """""""");
[GeneratedRegex(""""""
\s+
"""""")]
private static partial Regex MyRegex();
}";

await VerifyCS.VerifyCodeFixAsync(test, expectedFixedCode);
}

[Fact]
public async Task InterpolatedStringLiteralSyntaxPreservedByFixer()
{
string test = @"using System.Text.RegularExpressions;

partial class Program
{
static void Main(string[] args)
{
const string pattern = @""a|b\s\n"";
const string pattern2 = $""{pattern}2"";

Regex regex = [|new Regex(pattern2)|];
}
}";

string expectedFixedCode = @"using System.Text.RegularExpressions;

partial class Program
{
static void Main(string[] args)
{
const string pattern = @""a|b\s\n"";
const string pattern2 = $""{pattern}2"";

Regex regex = MyRegex();
}

[GeneratedRegex(""a|b\\s\\n2"")]
private static partial Regex MyRegex();
}";

await VerifyCS.VerifyCodeFixAsync(test, expectedFixedCode);
}

[Fact]
public async Task TestAsArgument()
{
Expand Down