Skip to content

Commit 3b43f88

Browse files
committed
#6 using existing field IFileSystem for FileServiceInterfaceInjectionCodeAction
1 parent 8c5861d commit 3b43f88

5 files changed

Lines changed: 43 additions & 15 deletions

File tree

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@ namespace SomeNameSpace
55
{
66
public class WithOutFileSystem
77
{
8-
private readonly IFileSystem _fileSystem;
8+
private readonly IFileSystem _fSystem;
99

1010
public WithOutFileSystem(IFileSystem fSystem)
1111
{
12-
_fileSystem = fSystem;
12+
_fSystem = fSystem;
1313
}
1414

1515
public void SomeMethod()

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ namespace SomeNameSpace
44
{
55
public class WithOutFileSystem
66
{
7+
private readonly IFileSystem _fSystem;
8+
79
public WithOutFileSystem(IFileSystem fSystem)
810
{
911
}

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

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System.Collections.Immutable;
2+
using System.IO.Abstractions.Analyzers.RoslynToken;
23
using System.Linq;
34
using JetBrains.Annotations;
45
using Microsoft.CodeAnalysis;
@@ -64,11 +65,18 @@ protected override void AnalyzeCompilation(CompilationStartAnalysisContext compi
6465
return;
6566
}
6667

67-
var fileSystem = classDeclarationSyntax.Members.OfType<FieldDeclarationSyntax>()
68-
.FirstOrDefault(x =>
69-
x.Declaration.Type.NormalizeWhitespace().ToFullString() == fileSystemContext.FileSystemType.Name);
70-
71-
if (fileSystem == null)
68+
if (RoslynClassFileSystem.HasFileSystemField(classDeclarationSyntax)
69+
&& RoslynClassFileSystem.HasConstructor(classDeclarationSyntax))
70+
{
71+
var ctor = RoslynClassFileSystem.GetConstructor(classDeclarationSyntax);
72+
var field = RoslynClassFileSystem.GetFileSystemFieldFromClass(classDeclarationSyntax);
73+
74+
if (!RoslynClassFileSystem.ConstructorHasAssignmentExpression(ctor, field.Declaration.Variables.ToFullString()))
75+
{
76+
syntaxContext.ReportDiagnostic(Diagnostic.Create(Rule,
77+
classDeclarationSyntax.Identifier.GetLocation()));
78+
}
79+
} else
7280
{
7381
syntaxContext.ReportDiagnostic(Diagnostic.Create(Rule,
7482
classDeclarationSyntax.Identifier.GetLocation()));

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

Lines changed: 14 additions & 4 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(string parameter)
77+
private static ExpressionStatementSyntax CreateAssignmentExpression(string field, string parameter)
7878
{
7979
return SF.ExpressionStatement(SF.AssignmentExpression(SyntaxKind.SimpleAssignmentExpression,
80-
SF.IdentifierName(Constants.FieldFileSystemName),
80+
SF.IdentifierName(field),
8181
SF.IdentifierName(parameter)));
8282
}
8383

@@ -101,8 +101,18 @@ private static void ConstructorAddParameter(ClassDeclarationSyntax classDeclarat
101101
{
102102
var parameterSyntax = RoslynClassFileSystem.GetFileSystemParameterFromConstructor(newConstructor);
103103

104-
newConstructor =
105-
newConstructor.AddBodyStatements(CreateAssignmentExpression(parameterSyntax.Identifier.Text));
104+
if (RoslynClassFileSystem.HasFileSystemField(classDeclaration))
105+
{
106+
var fileSystem = RoslynClassFileSystem.GetFileSystemFieldFromClass(classDeclaration);
107+
108+
newConstructor = newConstructor.AddBodyStatements(CreateAssignmentExpression(fileSystem.Declaration.Variables.ToFullString(),
109+
parameterSyntax.Identifier.Text));
110+
} else
111+
{
112+
newConstructor =
113+
newConstructor.AddBodyStatements(CreateAssignmentExpression(Constants.FieldFileSystemName,
114+
parameterSyntax.Identifier.Text));
115+
}
106116
}
107117

108118
if (RoslynClassFileSystem.HasConstructor(classDeclaration))

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

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,13 +66,20 @@ public static bool ConstructorHasFileSystemParameter(BaseMethodDeclarationSyntax
6666
.Any(x => x.Type.NormalizeWhitespace().ToFullString() == GetFileSystemType().NormalizeWhitespace().ToFullString());
6767
}
6868

69-
public static ParameterSyntax GetFileSystemParameterFromConstructor(BaseMethodDeclarationSyntax constructor)
69+
public static ParameterSyntax GetFileSystemParameterFromConstructor(ConstructorDeclarationSyntax constructor)
7070
{
7171
return constructor.ParameterList.Parameters
7272
.FirstOrDefault(x =>
7373
x.Type.NormalizeWhitespace().ToFullString() == GetFileSystemType().NormalizeWhitespace().ToFullString());
7474
}
7575

76+
public static FieldDeclarationSyntax GetFileSystemFieldFromClass(ClassDeclarationSyntax @class)
77+
{
78+
return @class.Members.OfType<FieldDeclarationSyntax>()
79+
.FirstOrDefault(x =>
80+
x.Declaration.Type.NormalizeWhitespace().ToFullString() == GetFileSystemType().ToFullString());
81+
}
82+
7683
public static ParameterSyntax CreateFileSystemParameterDeclaration()
7784
{
7885
return SF.Parameter(SF.Identifier(Constants.ParameterFileSystemName))
@@ -97,16 +104,17 @@ public static CompilationUnitSyntax GetCompilationUnit(SyntaxNode node)
97104
}
98105
}
99106

100-
public static bool ConstructorHasAssignmentExpression(BaseMethodDeclarationSyntax constructor)
107+
public static bool ConstructorHasAssignmentExpression(BaseMethodDeclarationSyntax constructor,
108+
string field = Constants.FieldFileSystemName)
101109
{
102110
if (constructor.Body == null)
103111
{
104112
return false;
105113
}
106114

107115
return constructor.Body.Statements.OfType<ExpressionStatementSyntax>()
108-
.Any(x => x.IsKind(SyntaxKind.SimpleAssignmentExpression)
109-
&& x.Expression.Contains(SF.IdentifierName(Constants.FieldFileSystemName)));
116+
.Any(x => x.Expression.IsKind(SyntaxKind.SimpleAssignmentExpression)
117+
&& x.Expression.ChildNodes().OfType<IdentifierNameSyntax>().Any(a => a.Identifier.Text == field));
110118
}
111119
}
112120
}

0 commit comments

Comments
 (0)