Skip to content

Commit

Permalink
Fix regex fixer to maintain string literal syntax (#78172)
Browse files Browse the repository at this point in the history
* Fix regex fixer to maintain string literal syntax

* Simplify

* Still transform interpolated strings
  • Loading branch information
stephentoub committed Nov 11, 2022
1 parent 7db5a57 commit e1dcc9c
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 1 deletion.
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

0 comments on commit e1dcc9c

Please sign in to comment.