Skip to content
This repository has been archived by the owner. It is now read-only.

Commit

Permalink
fix #125 - RECS0015 code fix generate invalid code (Missing parenthesis)
Browse files Browse the repository at this point in the history
  • Loading branch information
siegfriedpammer committed Oct 21, 2015
1 parent a6b806b commit 03fb944
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 3 deletions.
Expand Up @@ -34,9 +34,13 @@ public async override Task RegisterCodeFixesAsync(CodeFixContext context)
var node = root.FindNode(context.Span).Parent.Parent as InvocationExpressionSyntax;
if (node == null)
return;
var newRoot = root.ReplaceNode((SyntaxNode)node, node.WithArgumentList(node.ArgumentList.WithArguments(node.ArgumentList.Arguments.RemoveAt(0)))
.WithExpression(((MemberAccessExpressionSyntax)node.Expression).WithExpression(node.ArgumentList.Arguments.First().Expression))
.WithLeadingTrivia(node.GetLeadingTrivia()));
var invocationTarget = ((MemberAccessExpressionSyntax)node.Expression)
.WithExpression(CSharpUtil.AddParensIfRequired(node.ArgumentList.Arguments.First().Expression));
var newNode = node
.WithArgumentList(node.ArgumentList.WithArguments(node.ArgumentList.Arguments.RemoveAt(0)))
.WithExpression(invocationTarget)
.WithLeadingTrivia(node.GetLeadingTrivia());
var newRoot = root.ReplaceNode(node, newNode);
context.RegisterCodeFix(CodeActionFactory.Create(node.Span, diagnostic.Severity, "Convert to extension method call", document.WithSyntaxRoot(newRoot)), diagnostic);
}
}
Expand Down
Expand Up @@ -237,6 +237,38 @@ void F()
}");
}

[Test]
public void AddParenthesesIfNecessary()
{
Analyze<InvokeAsExtensionMethodAnalyzer>(@"using System;
static class Foo
{
public static decimal Abs(this decimal value) => Math.Abs(value);
}
class Program
{
static void Main()
{
Foo.$Abs$(-1.0m); // Apply code fix here
}
}", @"using System;
static class Foo
{
public static decimal Abs(this decimal value) => Math.Abs(value);
}
class Program
{
static void Main()
{
(-1.0m).Abs(); // Apply code fix here
}
}");
}

[Test]
public void IgnoresDelegateExtensionMethodOnMethod()
{
Expand Down

0 comments on commit 03fb944

Please sign in to comment.