Skip to content

Commit 8c5861d

Browse files
committed
Merge branch 'f/5-check-existing-constructor-parameter' into develop
2 parents d6e4396 + 5d74328 commit 8c5861d

6 files changed

Lines changed: 88 additions & 10 deletions

File tree

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ public class FileServiceInterfaceInjectionCodeFixTests :
1414
[InlineData("BeforeFix.txt", "AfterFix.txt")]
1515
[InlineData("BeforeFixWithoutConstructor.txt", "AfterFix.txt")]
1616
[InlineData("BeforeFixExistsAbstractionsUsing.txt", "AfterFix.txt")]
17+
[InlineData("BeforeFixExistingConstructorParameter.txt", "AfterFix.txt")]
18+
[InlineData("BeforeFixExistingConstructorParameterUniqName.txt", "AfterFixConstructorParameterUniqName.txt")]
1719
public void CodeFix(string sourceBefore, string sourceAfter)
1820
{
1921
var sourceBeforeFix = ReadFile(sourceBefore);
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 _fileSystem;
9+
10+
public WithOutFileSystem(IFileSystem fSystem)
11+
{
12+
_fileSystem = fSystem;
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,21 @@
1+
using System.IO;
2+
3+
namespace SomeNameSpace
4+
{
5+
public class WithOutFileSystem
6+
{
7+
public WithOutFileSystem(IFileSystem fileSystem)
8+
{
9+
}
10+
11+
public void SomeMethod()
12+
{
13+
const string filePath = "C:\\temp.txt";
14+
15+
if (File.Exists(filePath))
16+
{
17+
File.Delete(filePath);
18+
}
19+
}
20+
}
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using System.IO;
2+
3+
namespace SomeNameSpace
4+
{
5+
public class WithOutFileSystem
6+
{
7+
public WithOutFileSystem(IFileSystem fSystem)
8+
{
9+
}
10+
11+
public void SomeMethod()
12+
{
13+
const string filePath = "C:\\temp.txt";
14+
15+
if (File.Exists(filePath))
16+
{
17+
File.Delete(filePath);
18+
}
19+
}
20+
}
21+
}

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

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

77-
private static ExpressionStatementSyntax CreateAssignmentExpression()
77+
private static ExpressionStatementSyntax CreateAssignmentExpression(string parameter)
7878
{
7979
return SF.ExpressionStatement(SF.AssignmentExpression(SyntaxKind.SimpleAssignmentExpression,
8080
SF.IdentifierName(Constants.FieldFileSystemName),
81-
SF.IdentifierName(Constants.ParameterFileSystemName)));
81+
SF.IdentifierName(parameter)));
8282
}
8383

8484
private static void ConstructorAddParameter(ClassDeclarationSyntax classDeclaration, SyntaxEditor editor)
@@ -91,17 +91,20 @@ private static void ConstructorAddParameter(ClassDeclarationSyntax classDeclarat
9191
var newConstructor = constructor.WithAdditionalAnnotations(Formatter.Annotation, Simplifier.Annotation)
9292
.NormalizeWhitespace();
9393

94-
if (!RoslynClassFileSystem.ConstructorHasAssignmentExpression(newConstructor))
95-
{
96-
newConstructor = newConstructor.AddBodyStatements(CreateAssignmentExpression());
97-
}
98-
9994
if (!RoslynClassFileSystem.ConstructorHasFileSystemParameter(newConstructor))
10095
{
10196
var parameter = RoslynClassFileSystem.CreateFileSystemParameterDeclaration();
10297
newConstructor = newConstructor.AddParameterListParameters(parameter);
10398
}
10499

100+
if (!RoslynClassFileSystem.ConstructorHasAssignmentExpression(newConstructor))
101+
{
102+
var parameterSyntax = RoslynClassFileSystem.GetFileSystemParameterFromConstructor(newConstructor);
103+
104+
newConstructor =
105+
newConstructor.AddBodyStatements(CreateAssignmentExpression(parameterSyntax.Identifier.Text));
106+
}
107+
105108
if (RoslynClassFileSystem.HasConstructor(classDeclaration))
106109
{
107110
editor.ReplaceNode(constructor, newConstructor);

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

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,14 @@ public static FieldDeclarationSyntax CreateFileSystemFieldDeclaration()
6363
public static bool ConstructorHasFileSystemParameter(BaseMethodDeclarationSyntax constructor)
6464
{
6565
return constructor.ParameterList.Parameters
66-
.Any(x => x.Identifier.Text == Constants.ParameterFileSystemName && x.Type == GetFileSystemType());
66+
.Any(x => x.Type.NormalizeWhitespace().ToFullString() == GetFileSystemType().NormalizeWhitespace().ToFullString());
67+
}
68+
69+
public static ParameterSyntax GetFileSystemParameterFromConstructor(BaseMethodDeclarationSyntax constructor)
70+
{
71+
return constructor.ParameterList.Parameters
72+
.FirstOrDefault(x =>
73+
x.Type.NormalizeWhitespace().ToFullString() == GetFileSystemType().NormalizeWhitespace().ToFullString());
6774
}
6875

6976
public static ParameterSyntax CreateFileSystemParameterDeclaration()
@@ -99,8 +106,7 @@ public static bool ConstructorHasAssignmentExpression(BaseMethodDeclarationSynta
99106

100107
return constructor.Body.Statements.OfType<ExpressionStatementSyntax>()
101108
.Any(x => x.IsKind(SyntaxKind.SimpleAssignmentExpression)
102-
&& x.Expression.Contains(SF.IdentifierName(Constants.FieldFileSystemName))
103-
&& x.Expression.Contains(SF.IdentifierName(Constants.ParameterFileSystemName)));
109+
&& x.Expression.Contains(SF.IdentifierName(Constants.FieldFileSystemName)));
104110
}
105111
}
106112
}

0 commit comments

Comments
 (0)