From 58dcb6a0777abc4ab38ae358b79364520b478818 Mon Sep 17 00:00:00 2001 From: "Tomohisa Tanaka (Maroontress)" Date: Fri, 15 Mar 2019 22:54:13 +0900 Subject: [PATCH] Fix EqualsNull code fix to keep EndOfLineTrivia. (#99) --- .../Refactoring/EqualsNull/Code.cs | 10 +++++ .../Refactoring/EqualsNull/CodeFix.cs | 9 +++++ .../Refactoring/IsNull/Code.cs | 10 +++++ .../Refactoring/IsNull/CodeFix.cs | 9 +++++ .../Refactoring/EqualsNull/CodeFixer.cs | 38 +++++++++++++------ 5 files changed, 64 insertions(+), 12 deletions(-) diff --git a/StyleChecker/StyleChecker.Test/Refactoring/EqualsNull/Code.cs b/StyleChecker/StyleChecker.Test/Refactoring/EqualsNull/Code.cs index b5e3f4a3..7c1e5950 100644 --- a/StyleChecker/StyleChecker.Test/Refactoring/EqualsNull/Code.cs +++ b/StyleChecker/StyleChecker.Test/Refactoring/EqualsNull/Code.cs @@ -28,5 +28,15 @@ public void NullableValueType(int? value) return; } } + + public void EndOfLineTrivia(object value) + { + if (value != null + //@ ^!= + && value.ToString() == "") + { + return; + } + } } } diff --git a/StyleChecker/StyleChecker.Test/Refactoring/EqualsNull/CodeFix.cs b/StyleChecker/StyleChecker.Test/Refactoring/EqualsNull/CodeFix.cs index 96ebe4be..d3e12456 100644 --- a/StyleChecker/StyleChecker.Test/Refactoring/EqualsNull/CodeFix.cs +++ b/StyleChecker/StyleChecker.Test/Refactoring/EqualsNull/CodeFix.cs @@ -25,5 +25,14 @@ public void NullableValueType(int? value) return; } } + + public void EndOfLineTrivia(object value) + { + if (!(value is null) + && value.ToString() == "") + { + return; + } + } } } diff --git a/StyleChecker/StyleChecker.Test/Refactoring/IsNull/Code.cs b/StyleChecker/StyleChecker.Test/Refactoring/IsNull/Code.cs index 3972d384..0655b088 100644 --- a/StyleChecker/StyleChecker.Test/Refactoring/IsNull/Code.cs +++ b/StyleChecker/StyleChecker.Test/Refactoring/IsNull/Code.cs @@ -37,5 +37,15 @@ public void KeepTrivia(string value) return; } } + + public void EndOfLineTrivia(object value) + { + if (!(value is null) + //@ ^!= + && value.ToString() == "") + { + return; + } + } } } diff --git a/StyleChecker/StyleChecker.Test/Refactoring/IsNull/CodeFix.cs b/StyleChecker/StyleChecker.Test/Refactoring/IsNull/CodeFix.cs index 63df366f..a9e27894 100644 --- a/StyleChecker/StyleChecker.Test/Refactoring/IsNull/CodeFix.cs +++ b/StyleChecker/StyleChecker.Test/Refactoring/IsNull/CodeFix.cs @@ -33,5 +33,14 @@ public void KeepTrivia(string value) return; } } + + public void EndOfLineTrivia(object value) + { + if (value != null + && value.ToString() == "") + { + return; + } + } } } diff --git a/StyleChecker/StyleChecker/Refactoring/EqualsNull/CodeFixer.cs b/StyleChecker/StyleChecker/Refactoring/EqualsNull/CodeFixer.cs index 5cfa8558..523d71b9 100644 --- a/StyleChecker/StyleChecker/Refactoring/EqualsNull/CodeFixer.cs +++ b/StyleChecker/StyleChecker/Refactoring/EqualsNull/CodeFixer.cs @@ -84,23 +84,37 @@ private static async Task Replace( var root = await document.GetSyntaxRootAsync(cancellationToken) .ConfigureAwait(false); - var newNode = SyntaxFactory.IsPatternExpression( - node.Left, - SyntaxFactory.Token(SyntaxKind.IsKeyword) - .WithTriviaFrom(node.OperatorToken), - SyntaxFactory.ConstantPattern( - SyntaxFactory.LiteralExpression( - SyntaxKind.NullLiteralExpression)) - .WithTriviaFrom(node.Right)) as ExpressionSyntax; + var right = node.Right; + var @null = SyntaxFactory.ConstantPattern( + SyntaxFactory.LiteralExpression( + SyntaxKind.NullLiteralExpression)); + var @is = SyntaxFactory.Token(SyntaxKind.IsKeyword) + .WithTriviaFrom(node.OperatorToken); - if (node.OperatorToken.Kind() - == SyntaxKind.ExclamationEqualsToken) + ExpressionSyntax NewEqualNode() { - newNode = SyntaxFactory.PrefixUnaryExpression( + return SyntaxFactory.IsPatternExpression( + node.Left, + @is, + @null.WithTriviaFrom(right)); + } + + ExpressionSyntax NewNotEqualNode() + { + var n = SyntaxFactory.IsPatternExpression( + node.Left, + @is, + @null.WithLeadingTrivia(right.GetLeadingTrivia())); + return SyntaxFactory.PrefixUnaryExpression( SyntaxKind.LogicalNotExpression, - SyntaxFactory.ParenthesizedExpression(newNode)); + SyntaxFactory.ParenthesizedExpression(n) + .WithTrailingTrivia(right.GetTrailingTrivia())); } + var newNode = (node.OperatorToken.Kind() + == SyntaxKind.ExclamationEqualsToken) + ? NewNotEqualNode() + : NewEqualNode(); newNode = newNode.WithTriviaFrom(node); var newRoot = root.ReplaceNode(node, newNode);