Skip to content

Commit 4cddc34

Browse files
committed
#3 Fix using replacement
1 parent 8adf583 commit 4cddc34

8 files changed

Lines changed: 63 additions & 4 deletions

File tree

System.IO.Abstractions.Analyzers.Tests/Analyzers/FileServiceInterfaceInjectionAnalyzerTests.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ public class FileServiceInterfaceInjectionAnalyzerTests : CSharpDiagnosticAnalyz
1111
{
1212
[Theory]
1313
[InlineData("Valid.txt")]
14+
[InlineData("Valid2.txt")]
1415
public void Analyzer_is_not_triggered(string filename)
1516
{
1617
var source = ReadFile(filename);

System.IO.Abstractions.Analyzers.Tests/CodeFixes/FileServiceInterfaceInjectionCodeFixTests.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ public class FileServiceInterfaceInjectionCodeFixTests :
1313
[Theory]
1414
[InlineData("BeforeFix.txt", "AfterFix.txt")]
1515
[InlineData("BeforeFixWithoutConstructor.txt", "AfterFix.txt")]
16+
[InlineData("BeforeFixExistsAbstractionsUsing.txt", "AfterFix.txt")]
1617
public void CodeFix(string sourceBefore, string sourceAfter)
1718
{
1819
var sourceBeforeFix = ReadFile(sourceBefore);
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
using System.IO;
2+
using System.IO.Abstractions;
3+
4+
namespace SomeNameSpace
5+
{
6+
public class Valid
7+
{
8+
private readonly IFileSystem _fileSystem;
9+
10+
public Valid(IFileSystem fileSystem)
11+
{
12+
_fileSystem = fileSystem;
13+
}
14+
}
15+
}

System.IO.Abstractions.Analyzers.Tests/TestData/CodeFix/FileServiceInterfaceInjectionCodeFix/AfterFix.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System.IO;
12
using System.IO.Abstractions;
23

34
namespace SomeNameSpace
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
using System.IO;
2+
using System.IO.Abstractions;
3+
4+
namespace SomeNameSpace
5+
{
6+
public class WithOutFileSystem
7+
{
8+
public void SomeMethod()
9+
{
10+
const string filePath = "C:\\temp.txt";
11+
12+
if (File.Exists(filePath))
13+
{
14+
File.Delete(filePath);
15+
}
16+
}
17+
}
18+
}

System.IO.Abstractions.Analyzers/Analyzers/FileServiceInterfaceInjectionAnalyzer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ protected override void AnalyzeCompilation(CompilationStartAnalysisContext compi
6666

6767
var fileSystem = classDeclarationSyntax.Members.OfType<FieldDeclarationSyntax>()
6868
.FirstOrDefault(x =>
69-
x.NormalizeWhitespace().ToFullString() == fileSystemContext.FileSystemType.Name);
69+
x.Declaration.Type.NormalizeWhitespace().ToFullString() == fileSystemContext.FileSystemType.Name);
7070

7171
if (fileSystem == null)
7272
{

System.IO.Abstractions.Analyzers/CodeActions/FileServiceInterfaceInjectionCodeAction.cs

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,27 @@ protected override async Task<Document> GetChangedDocumentAsync(CancellationToke
4848

4949
var compilationUnitSyntax = RoslynClassFileSystem.GetCompilationUnit(_class);
5050

51-
if (compilationUnitSyntax.Usings.Any())
51+
if (!compilationUnitSyntax.Usings.Any())
5252
{
53-
editor.ReplaceNode(RoslynClassFileSystem.GetSystemIoUsing(compilationUnitSyntax),
53+
return editor.GetChangedDocument();
54+
}
55+
56+
var fileSystem = RoslynClassFileSystem.GetUsing(compilationUnitSyntax, Constants.FileSystemNameSpace);
57+
58+
if (fileSystem != default(UsingDirectiveSyntax))
59+
{
60+
return editor.GetChangedDocument();
61+
}
62+
63+
var systemIo = RoslynClassFileSystem.GetSystemIoUsing(compilationUnitSyntax);
64+
65+
if (systemIo == default(UsingDirectiveSyntax))
66+
{
67+
editor.InsertBefore(compilationUnitSyntax.Usings.FirstOrDefault(),
68+
RoslynClassFileSystem.GetFileSystemUsing());
69+
} else
70+
{
71+
editor.InsertAfter(compilationUnitSyntax.Usings.FirstOrDefault(),
5472
RoslynClassFileSystem.GetFileSystemUsing());
5573
}
5674

System.IO.Abstractions.Analyzers/RoslynToken/RoslynClassFileSystem.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,14 @@ public static UsingDirectiveSyntax GetFileSystemUsing()
4242
}
4343

4444
public static UsingDirectiveSyntax GetSystemIoUsing(CompilationUnitSyntax unit)
45+
{
46+
return GetUsing(unit, typeof(Path).Namespace);
47+
}
48+
49+
public static UsingDirectiveSyntax GetUsing(CompilationUnitSyntax unit, string usingName)
4550
{
4651
return unit.Usings.FirstOrDefault(x =>
47-
x.Name.NormalizeWhitespace().ToFullString().Equals(typeof(Path).Namespace));
52+
x.Name.NormalizeWhitespace().ToFullString().Equals(usingName));
4853
}
4954

5055
public static FieldDeclarationSyntax CreateFileSystemFieldDeclaration()

0 commit comments

Comments
 (0)