Skip to content

Commit 2c8f9b4

Browse files
committed
Merge branch 'f/3-fix-interface-injection-code-action' into develop
2 parents 793d41b + 4cddc34 commit 2c8f9b4

10 files changed

Lines changed: 68 additions & 9 deletions

File tree

Roslyn.Testing/Roslyn.Testing.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
<ItemGroup>
99
<PackageReference Include="JetBrains.Annotations" Version="2019.1.1" />
10-
<PackageReference Include="Microsoft.CodeAnalysis" Version="2.10.0" />
10+
<PackageReference Include="Microsoft.CodeAnalysis" Version="3.0.0" />
1111
<PackageReference Include="Shouldly" Version="3.0.2" />
1212
</ItemGroup>
1313

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()

System.IO.Abstractions.Analyzers/System.IO.Abstractions.Analyzers.csproj

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFramework>netstandard1.4</TargetFramework>
4+
<TargetFramework>netstandard2.0</TargetFramework>
55
<PackageTargetFallback>portable-net45+win8+wp8+wpa81</PackageTargetFallback>
66
<IncludeBuildOutput>false</IncludeBuildOutput>
77
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
@@ -17,7 +17,7 @@
1717
</PropertyGroup>
1818
<PropertyGroup>
1919
<PackageId>Roslyn.System.IO.Abstractions.Analyzers</PackageId>
20-
<PackageVersion>4.0.0</PackageVersion>
20+
<PackageVersion>4.0.1</PackageVersion>
2121
<Authors>inyutin_maxim</Authors>
2222
<PackageLicenseExpression>MIT</PackageLicenseExpression>
2323
<PackageProjectUrl>https://github.com/System-IO-Abstractions/System.IO.Abstractions.Analyzers/</PackageProjectUrl>
@@ -37,8 +37,8 @@
3737
<ItemGroup>
3838
<PackageReference Include="JetBrains.Annotations" Version="2019.1.1" PrivateAssets="all" />
3939
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="2.9.1" PrivateAssets="all" />
40-
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="2.10.0" PrivateAssets="all" />
41-
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="2.10.0" PrivateAssets="all" />
40+
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="3.0.0" PrivateAssets="all" />
41+
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="3.0.0" PrivateAssets="all" />
4242
<PackageReference Update="NETStandard.Library" PrivateAssets="all" />
4343
<PackageReference Include="Microsoft.CodeQuality.Analyzers" Version="2.9.1" PrivateAssets="all" />
4444
<PackageReference Include="Roslyn.Diagnostics.Analyzers" Version="2.9.1" PrivateAssets="all" />

0 commit comments

Comments
 (0)