Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add imports instead of fully qualifying #38

Merged
merged 3 commits into from
Jun 28, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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