Skip to content

Commit d1d0691

Browse files
committed
#6 using existing field IFileSystem for FileServiceConstructorInitialCodeAction
1 parent 3b43f88 commit d1d0691

4 files changed

Lines changed: 62 additions & 3 deletions

File tree

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ public class FileServiceConstructorInitialCodeFixTests :
1212
{
1313
[Theory]
1414
[InlineData("BeforeFix.txt", "AfterFix.txt")]
15+
[InlineData("BeforeFixWithCustomField.txt", "AfterFixWithCustomField.txt")]
1516
public void CodeFix(string sourceBefore, string sourceAfter)
1617
{
1718
var sourceBeforeFix = ReadFile(sourceBefore);
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
using System.IO;
2+
using System.IO.Abstractions;
3+
4+
namespace SomeNameSpace
5+
{
6+
public class WithOutFileSystem
7+
{
8+
private readonly IFileSystem _fSystem;
9+
10+
public WithOutFileSystem()
11+
{
12+
_fSystem = new FileSystem();
13+
}
14+
15+
public void SomeMethod()
16+
{
17+
const string filePath = "C:\\temp.txt";
18+
19+
if (File.Exists(filePath))
20+
{
21+
File.Delete(filePath);
22+
}
23+
}
24+
}
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
using System.IO;
2+
3+
namespace SomeNameSpace
4+
{
5+
public class WithOutFileSystem
6+
{
7+
private readonly IFileSystem _fSystem;
8+
9+
public WithOutFileSystem()
10+
{
11+
}
12+
13+
public void SomeMethod()
14+
{
15+
const string filePath = "C:\\temp.txt";
16+
17+
if (File.Exists(filePath))
18+
{
19+
File.Delete(filePath);
20+
}
21+
}
22+
}
23+
}

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

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,10 +74,10 @@ protected override async Task<Document> GetChangedDocumentAsync(CancellationToke
7474
return editor.GetChangedDocument();
7575
}
7676

77-
private static ExpressionStatementSyntax CreateAssignmentExpression()
77+
private static ExpressionStatementSyntax CreateAssignmentExpression(string field = Constants.FieldFileSystemName)
7878
{
7979
return SyntaxFactory.ExpressionStatement(SyntaxFactory.AssignmentExpression(SyntaxKind.SimpleAssignmentExpression,
80-
SyntaxFactory.IdentifierName(Constants.FieldFileSystemName),
80+
SyntaxFactory.IdentifierName(field),
8181
SyntaxFactory.ObjectCreationExpression(SyntaxFactory.IdentifierName(Constants.FileSystemClassName))
8282
.WithArgumentList(SyntaxFactory.ArgumentList())));
8383
}
@@ -94,7 +94,17 @@ private static void ConstructorAddParameter(ClassDeclarationSyntax classDeclarat
9494

9595
if (!RoslynClassFileSystem.ConstructorHasAssignmentExpression(newConstructor))
9696
{
97-
newConstructor = newConstructor.AddBodyStatements(CreateAssignmentExpression());
97+
if (RoslynClassFileSystem.HasFileSystemField(classDeclaration))
98+
{
99+
var fileSystem = RoslynClassFileSystem.GetFileSystemFieldFromClass(classDeclaration);
100+
101+
newConstructor =
102+
newConstructor.AddBodyStatements(CreateAssignmentExpression(fileSystem.Declaration.Variables.ToFullString()));
103+
} else
104+
{
105+
newConstructor =
106+
newConstructor.AddBodyStatements(CreateAssignmentExpression());
107+
}
98108
}
99109

100110
if (RoslynClassFileSystem.HasConstructor(classDeclaration))

0 commit comments

Comments
 (0)