Skip to content

Commit

Permalink
Merge pull request #46999 from CyrusNajmabadi/extractMethodFix
Browse files Browse the repository at this point in the history
Fix extract method crash with conditional access expressions.
  • Loading branch information
msftbot[bot] authored Aug 21, 2020
2 parents fa6725e + e36304e commit 8949725
Show file tree
Hide file tree
Showing 47 changed files with 995 additions and 333 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,8 @@ protected bool ExpressionContainsValuePatternOrReferencesInitializedSymbol(Synta
{
foreach (var subExpression in expression.DescendantNodesAndSelf().OfType<TExpressionSyntax>())
{
if (!_syntaxFacts.IsNameOfMemberAccessExpression(subExpression))
if (!_syntaxFacts.IsNameOfSimpleMemberAccessExpression(subExpression) &&
!_syntaxFacts.IsNameOfMemberBindingExpression(subExpression))
{
if (ValuePatternMatches(subExpression))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3969,6 +3969,237 @@ private static Action<Context> NewMethod()
return context => context.ToString();
}
}
}");
}

[Fact, WorkItem(41895, "https://github.com/dotnet/roslyn/issues/41895")]
public async Task TestConditionalAccess1()
{
await TestInRegularAndScript1Async(@"
using System;
using System.Collections.Generic;
class C
{
void Test()
{
List<int> b = null;
b?.Clear();
_ = b?.[|ToString|]();
}
}", @"
using System;
using System.Collections.Generic;
class C
{
void Test()
{
List<int> b = null;
b?.Clear();
_ = {|Rename:NewMethod|}(b);
}
private static string NewMethod(List<int> b)
{
return b?.ToString();
}
}");
}

[Fact, WorkItem(41895, "https://github.com/dotnet/roslyn/issues/41895")]
public async Task TestConditionalAccess2()
{
await TestInRegularAndScript1Async(@"
using System;
using System.Collections.Generic;
class C
{
void Test()
{
List<int> b = null;
b?.Clear();
_ = b?.[|ToString|]().Length;
}
}", @"
using System;
using System.Collections.Generic;
class C
{
void Test()
{
List<int> b = null;
b?.Clear();
_ = {|Rename:NewMethod|}(b);
}
private static int? NewMethod(List<int> b)
{
return b?.ToString().Length;
}
}");
}

[Fact, WorkItem(41895, "https://github.com/dotnet/roslyn/issues/41895")]
public async Task TestConditionalAccess3()
{
await TestInRegularAndScript1Async(@"
using System;
using System.Collections.Generic;
class C
{
void Test()
{
List<int> b = null;
b?.Clear();
_ = b?.Count.[|ToString|]();
}
}", @"
using System;
using System.Collections.Generic;
class C
{
void Test()
{
List<int> b = null;
b?.Clear();
_ = {|Rename:NewMethod|}(b);
}
private static string NewMethod(List<int> b)
{
return b?.Count.ToString();
}
}");
}

[Fact, WorkItem(41895, "https://github.com/dotnet/roslyn/issues/41895")]
public async Task TestConditionalAccess4()
{
await TestInRegularAndScript1Async(@"
using System;
using System.Collections.Generic;
class C
{
void Test()
{
List<int> b = null;
b?.Clear();
_ = b?.[|Count|].ToString();
}
}", @"
using System;
using System.Collections.Generic;
class C
{
void Test()
{
List<int> b = null;
b?.Clear();
_ = {|Rename:NewMethod|}(b);
}
private static string NewMethod(List<int> b)
{
return b?.Count.ToString();
}
}");
}

[Fact, WorkItem(41895, "https://github.com/dotnet/roslyn/issues/41895")]
public async Task TestConditionalAccess5()
{
await TestInRegularAndScript1Async(@"
using System;
using System.Collections.Generic;
class C
{
void Test()
{
List<int> b = null;
b?.Clear();
_ = b?.[|ToString|]()?.ToString();
}
}", @"
using System;
using System.Collections.Generic;
class C
{
void Test()
{
List<int> b = null;
b?.Clear();
_ = {|Rename:NewMethod|}(b);
}
private static string NewMethod(List<int> b)
{
return b?.ToString()?.ToString();
}
}");
}

[Fact, WorkItem(41895, "https://github.com/dotnet/roslyn/issues/41895")]
public async Task TestConditionalAccess6()
{
await TestInRegularAndScript1Async(@"
using System;
using System.Collections.Generic;
class C
{
void Test()
{
List<int> b = null;
b?.Clear();
_ = b?.ToString()?.[|ToString|]();
}
}", @"
using System;
using System.Collections.Generic;
class C
{
void Test()
{
List<int> b = null;
b?.Clear();
_ = {|Rename:NewMethod|}(b);
}
private static string NewMethod(List<int> b)
{
return b?.ToString()?.ToString();
}
}");
}

[Fact, WorkItem(41895, "https://github.com/dotnet/roslyn/issues/41895")]
public async Task TestConditionalAccess7()
{
await TestInRegularAndScript1Async(@"
using System;
using System.Collections.Generic;
class C
{
void Test()
{
List<int> b = null;
b?.Clear();
_ = b?[|[0]|];
}
}", @"
using System;
using System.Collections.Generic;
class C
{
void Test()
{
List<int> b = null;
b?.Clear();
_ = {|Rename:NewMethod|}(b);
}
private static int? NewMethod(List<int> b)
{
return b?[0];
}
}");
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8074,7 +8074,7 @@ public void goo()

[WorkItem(1109319, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/1109319")]
[Fact, Trait(Traits.Feature, Traits.Features.Completion)]
public async Task WithinChainOfConditionalAccesses()
public async Task WithinChainOfConditionalAccesses1()
{
var markup = @"
class Program
Expand All @@ -8093,6 +8093,48 @@ class D { public int e; }";
await VerifyItemExistsAsync(markup, "b");
}

[WorkItem(1109319, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/1109319")]
[Fact, Trait(Traits.Feature, Traits.Features.Completion)]
public async Task WithinChainOfConditionalAccesses2()
{
var markup = @"
class Program
{
static void Main(string[] args)
{
A a;
var x = a?.b?.$$c?.d.e;
}
}
class A { public B b; }
class B { public C c; }
class C { public D d; }
class D { public int e; }";
await VerifyItemExistsAsync(markup, "c");
}

[WorkItem(1109319, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/1109319")]
[Fact, Trait(Traits.Feature, Traits.Features.Completion)]
public async Task WithinChainOfConditionalAccesses3()
{
var markup = @"
class Program
{
static void Main(string[] args)
{
A a;
var x = a?.b?.c?.$$d.e;
}
}
class A { public B b; }
class B { public C c; }
class C { public D d; }
class D { public int e; }";
await VerifyItemExistsAsync(markup, "d");
}

[WorkItem(843466, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/843466")]
[Fact, Trait(Traits.Feature, Traits.Features.Completion)]
public async Task NestedAttributeAccessibleOnSelf()
Expand Down
Loading

0 comments on commit 8949725

Please sign in to comment.