Skip to content

Commit

Permalink
Add imports instead of fully qualifying (#38)
Browse files Browse the repository at this point in the history
* Add imports instead of fully qualifying

* Add skipped test

* Skip on non-Windows
  • Loading branch information
Youssef1313 authored Jun 28, 2021
1 parent 7184ac0 commit a7d8c68
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Editing;
using Microsoft.CodeAnalysis.Simplification;

namespace YAnalyzers.CSharp
{
Expand Down Expand Up @@ -90,7 +91,7 @@ private static async Task<Document> UseExplicitTypeAsync(Document document, Synt
TypeInfo typeInfo = model.GetTypeInfo(typeSyntax, cancellationToken);

SyntaxGenerator generator = SyntaxGenerator.GetGenerator(document);
SyntaxNode newNode = generator.TypeExpression(typeInfo.ConvertedType).WithTriviaFrom(typeSyntax);
SyntaxNode newNode = generator.TypeExpression(typeInfo.ConvertedType).WithTriviaFrom(typeSyntax).WithAdditionalAnnotations(Simplifier.AddImportsAnnotation);

return document.WithSyntaxRoot(root.ReplaceNode(typeSyntax, newNode));
}
Expand Down
78 changes: 77 additions & 1 deletion src/UnitTests/UseImplicitOrExplicitTypeAnalyzerTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System.Threading.Tasks;
using System;
using System.Runtime.InteropServices;
using System.Threading.Tasks;
using Microsoft.VisualStudio.TestTools.UnitTesting;

using VerifyCS = YAnalyzers.Test.CSharpCodeFixVerifier<
Expand Down Expand Up @@ -449,6 +451,80 @@ public void Process(string s)
}
}
}
";
await VerifyCS.VerifyCodeFixAsync(code, VerifyCS.Diagnostic(UseImplicitOrExplicitTypeAnalyzer.UseExplicitTypeDiagnosticId).WithLocation(0), fixedCode);
}

[TestMethod]
public async Task TestAddUsing()
{
if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
// Ugly way to conditionally skip a test.
// This should be removed anyway.
// https://github.com/dotnet/roslyn-sdk/issues/876
return;
}

var code = @"
using System.Linq;
class C
{
public void M(string[] s)
{
{|#0:var x = s.ToList()|};
}
}
";
var fixedCode = @"
using System.Collections.Generic;
using System.Linq;
class C
{
public void M(string[] s)
{
List<string> x = s.ToList();
}
}
";
await VerifyCS.VerifyCodeFixAsync(code, VerifyCS.Diagnostic(UseImplicitOrExplicitTypeAnalyzer.UseExplicitTypeDiagnosticId).WithLocation(0), fixedCode);
}

[TestMethod]
[Ignore("https://github.com/dotnet/roslyn/issues/54437 - https://github.com/Youssef1313/YAnalyzers/issues/33")]
public async Task TestDeconstruction()
{
var code = @"
using System.Collections.Generic;
using System.Linq;
class C
{
public void M(string[] s)
{
var x = new Dictionary<int, string>();
foreach ({|#0:var (a, b)|} in x)
{
}
}
}
";
var fixedCode = @"
using System.Collections.Generic;
using System.Linq;
class C
{
public void M(string[] s)
{
var x = new Dictionary<int, string>();
foreach ((int a, string b) in x)
{
}
}
}
";
await VerifyCS.VerifyCodeFixAsync(code, VerifyCS.Diagnostic(UseImplicitOrExplicitTypeAnalyzer.UseExplicitTypeDiagnosticId).WithLocation(0), fixedCode);
}
Expand Down

0 comments on commit a7d8c68

Please sign in to comment.