Skip to content

Commit

Permalink
Fix EqualsNull code fix to keep EndOfLineTrivia. (#99)
Browse files Browse the repository at this point in the history
  • Loading branch information
maroontress-tomohisa authored Mar 15, 2019
1 parent 6258cef commit 58dcb6a
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 12 deletions.
10 changes: 10 additions & 0 deletions StyleChecker/StyleChecker.Test/Refactoring/EqualsNull/Code.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,15 @@ public void NullableValueType(int? value)
return;
}
}

public void EndOfLineTrivia(object value)
{
if (value != null
//@ ^!=
&& value.ToString() == "")
{
return;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,14 @@ public void NullableValueType(int? value)
return;
}
}

public void EndOfLineTrivia(object value)
{
if (!(value is null)
&& value.ToString() == "")
{
return;
}
}
}
}
10 changes: 10 additions & 0 deletions StyleChecker/StyleChecker.Test/Refactoring/IsNull/Code.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,15 @@ public void KeepTrivia(string value)
return;
}
}

public void EndOfLineTrivia(object value)
{
if (!(value is null)
//@ ^!=
&& value.ToString() == "")
{
return;
}
}
}
}
9 changes: 9 additions & 0 deletions StyleChecker/StyleChecker.Test/Refactoring/IsNull/CodeFix.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,14 @@ public void KeepTrivia(string value)
return;
}
}

public void EndOfLineTrivia(object value)
{
if (value != null
&& value.ToString() == "")
{
return;
}
}
}
}
38 changes: 26 additions & 12 deletions StyleChecker/StyleChecker/Refactoring/EqualsNull/CodeFixer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,23 +84,37 @@ private static async Task<Solution> 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);
Expand Down

0 comments on commit 58dcb6a

Please sign in to comment.