Skip to content

Commit 60cccf0

Browse files
committed
Make type explicit rule should add using when it needs to qualify type.
1 parent 787e322 commit 60cccf0

File tree

2 files changed

+68
-2
lines changed

2 files changed

+68
-2
lines changed

src/Microsoft.DotNet.CodeFormatter.Analyzers.Tests/ProvideExplicitVariableTypeAnalyzerTests.cs

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ void M()
3232
int[][] z = new[] { new[] { 1, 2, 3 }, new[] { 4, 5, 6 } };
3333
}
3434
}";
35-
const string expected = input;
35+
const string expected = input;
3636
Verify(input, expected, runFormatter: false);
3737
}
3838

@@ -511,6 +511,64 @@ void M()
511511
List<int>.Enumerator x = (new List<int>()).GetEnumerator();
512512
var locationBuilder = ImmutableArray.CreateBuilder<C2>();
513513
}
514+
}";
515+
Verify(input, expected, runFormatter: false);
516+
}
517+
518+
[Fact]
519+
public void TestAddingUsing()
520+
{
521+
const string input = @"
522+
class C1
523+
{
524+
System.Collections.Generic.List<int> f() { return null; }
525+
526+
void M()
527+
{
528+
var x = f();
529+
}
530+
}";
531+
const string expected =
532+
@"using System.Collections.Generic;
533+
534+
class C1
535+
{
536+
System.Collections.Generic.List<int> f() { return null; }
537+
538+
void M()
539+
{
540+
List<int> x = f();
541+
}
542+
}";
543+
Verify(input, expected, runFormatter: false);
544+
}
545+
546+
[Fact]
547+
public void TestNotAddingDuplicateUsing()
548+
{
549+
const string input = @"
550+
using System.Collections.Generic;
551+
552+
class C1
553+
{
554+
System.Collections.Generic.List<int> f() { return null; }
555+
556+
void M()
557+
{
558+
var x = f();
559+
}
560+
}";
561+
const string expected = @"
562+
using System.Collections.Generic;
563+
564+
class C1
565+
{
566+
System.Collections.Generic.List<int> f() { return null; }
567+
568+
void M()
569+
{
570+
List<int> x = f();
571+
}
514572
}";
515573
Verify(input, expected, runFormatter: false);
516574
}

src/Microsoft.DotNet.CodeFormatter.Analyzers/ProvideExplicitVariableTypeFixer.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,15 @@ private async Task<Document> ReplaceVarWithExplicitType(Document document, Synta
8484
.WithAdditionalAnnotations(Simplifier.Annotation)
8585
.WithTriviaFrom(varNode);
8686
documentEditor.ReplaceNode(varNode, explicitTypeNode);
87-
return documentEditor.GetChangedDocument();
87+
var newDocument = documentEditor.GetChangedDocument();
88+
89+
// We don't want the explicit type to be fully qualified. So add an using for this node and the simplifier will
90+
// take care of removing it if it isn't necessary.
91+
// The second parmaeter to AddImportsAsync is an annotation that is used to locate the span
92+
// for which an using should be added. Since we added the simplify annotation on explicitTypeNode,
93+
// we can just use that annotation to locate the node again in newDocument.
94+
newDocument = await ImportAdder.AddImportsAsync(newDocument, Simplifier.Annotation).ConfigureAwait(false);
95+
return newDocument;
8896
}
8997

9098
private static RuleType GetRuleType(Diagnostic diagnostic)

0 commit comments

Comments
 (0)