Skip to content

Commit 1a7847b

Browse files
committed
Extract common code
1 parent 991f4e0 commit 1a7847b

4 files changed

Lines changed: 128 additions & 202 deletions

File tree

Lines changed: 12 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System.Linq;
1+
using System.IO.Abstractions.Analyzers.RoslynToken;
22
using System.Threading;
33
using System.Threading.Tasks;
44
using Microsoft.CodeAnalysis;
@@ -34,146 +34,59 @@ protected override async Task<Document> GetChangedDocumentAsync(CancellationToke
3434
{
3535
var editor = await DocumentEditor.CreateAsync(_document, cancellationToken).ConfigureAwait(false);
3636

37-
if (!HasFileSystemProperty(_class))
37+
if (!RoslynClassFyleSystem.HasFileSystemProperty(_class))
3838
{
3939
editor.InsertMembers(_class,
4040
0,
4141
new SyntaxNode[]
4242
{
43-
CreateFileSystemPropertyDeclaration()
43+
RoslynClassFyleSystem.CreateFileSystemPropertyDeclaration()
4444
});
4545
}
4646

4747
ConstructorAddParameter(_class, editor);
4848

49-
var compilationUnitSyntax = GetCompilationUnit(_class);
49+
var compilationUnitSyntax = RoslynClassFyleSystem.GetCompilationUnit(_class);
5050

5151
if (compilationUnitSyntax.Usings.Any())
5252
{
53-
editor.ReplaceNode(GetSystemIoUsing(compilationUnitSyntax), GetFileSystemUsing());
53+
editor.ReplaceNode(RoslynClassFyleSystem.GetSystemIoUsing(compilationUnitSyntax),
54+
RoslynClassFyleSystem.GetFileSystemUsing());
5455
}
5556

5657
return editor.GetChangedDocument();
5758
}
5859

59-
private static UsingDirectiveSyntax GetFileSystemUsing()
60-
{
61-
return SF.UsingDirective(SF.ParseName(Constants.FileSystemNameSpace));
62-
}
63-
64-
private static UsingDirectiveSyntax GetSystemIoUsing(CompilationUnitSyntax unit)
65-
{
66-
return unit.Usings.FirstOrDefault(x =>
67-
x.Name.NormalizeWhitespace().ToFullString().Equals(typeof(Path).Namespace));
68-
}
69-
70-
private static FieldDeclarationSyntax CreateFileSystemPropertyDeclaration()
71-
{
72-
return SF.FieldDeclaration(SF.VariableDeclaration(GetFileSystemType())
73-
.WithVariables(SF.SingletonSeparatedList(SF.VariableDeclarator(SF.Identifier(Constants.FieldFileSystemName)))))
74-
.WithModifiers(SF.TokenList(SF.Token(SyntaxKind.PrivateKeyword),
75-
SF.Token(SyntaxKind.ReadOnlyKeyword)));
76-
}
77-
78-
private static ParameterSyntax CreateFileSystemParameterDeclaration()
79-
{
80-
return SF.Parameter(SF.Identifier(Constants.ParameterFileSystemName))
81-
.WithType(GetFileSystemType())
82-
.WithAdditionalAnnotations(Formatter.Annotation, Simplifier.SpecialTypeAnnotation)
83-
.NormalizeWhitespace();
84-
}
85-
86-
private static TypeSyntax GetFileSystemType()
87-
{
88-
return SF.ParseTypeName(Constants.FileSystemInterfaceName);
89-
}
90-
9160
private static ExpressionStatementSyntax CreateAssignmentExpression()
9261
{
9362
return SyntaxFactory.ExpressionStatement(SyntaxFactory.AssignmentExpression(SyntaxKind.SimpleAssignmentExpression,
94-
SyntaxFactory.IdentifierName("_fileSystem"),
63+
SyntaxFactory.IdentifierName(Constants.FieldFileSystemName),
9564
SyntaxFactory.ObjectCreationExpression(SyntaxFactory.IdentifierName(Constants.FileSystemClassName))
9665
.WithArgumentList(SyntaxFactory.ArgumentList())));
9766
}
9867

99-
private static CompilationUnitSyntax GetCompilationUnit(SyntaxNode node)
100-
{
101-
switch (node)
102-
{
103-
case null:
104-
105-
return null;
106-
case CompilationUnitSyntax compilationUnitSyntax:
107-
108-
return compilationUnitSyntax;
109-
default:
110-
111-
return GetCompilationUnit(node.Parent);
112-
}
113-
}
114-
115-
private static bool HasFileSystemProperty(TypeDeclarationSyntax classDeclaration)
116-
{
117-
return classDeclaration.Members.OfType<PropertyDeclarationSyntax>()
118-
.Any(x => x.Identifier.Text == Constants.FieldFileSystemName && x.Type == GetFileSystemType());
119-
}
120-
121-
private static ConstructorDeclarationSyntax GetConstructor(SyntaxNode classDeclaration)
122-
{
123-
return classDeclaration.ChildNodes().OfType<ConstructorDeclarationSyntax>().FirstOrDefault();
124-
}
125-
126-
private static bool ConstructorHasFileSystemParameter(BaseMethodDeclarationSyntax constructor)
127-
{
128-
return constructor.ParameterList.Parameters
129-
.Any(x => x.Identifier.Text == Constants.ParameterFileSystemName && x.Type == GetFileSystemType());
130-
}
131-
132-
private static bool ConstructorHasAssignmentExpression(BaseMethodDeclarationSyntax constructor)
133-
{
134-
if (constructor.Body == null)
135-
{
136-
return false;
137-
}
138-
139-
return constructor.Body.Statements.OfType<ExpressionStatementSyntax>()
140-
.Any(x => x.IsKind(SyntaxKind.SimpleAssignmentExpression)
141-
&& x.Expression.Contains(SF.IdentifierName(Constants.FieldFileSystemName))
142-
&& x.Expression.Contains(SF.IdentifierName(Constants.ParameterFileSystemName)));
143-
}
144-
145-
private static bool HasConstructor(SyntaxNode classDeclaration)
146-
{
147-
return classDeclaration.ChildNodes().OfType<ConstructorDeclarationSyntax>().Any();
148-
}
149-
15068
private static void ConstructorAddParameter(ClassDeclarationSyntax classDeclaration, SyntaxEditor editor)
15169
{
152-
var constructor = HasConstructor(classDeclaration)
153-
? GetConstructor(classDeclaration)
70+
var constructor = RoslynClassFyleSystem.HasConstructor(classDeclaration)
71+
? RoslynClassFyleSystem.GetConstructor(classDeclaration)
15472
: SF.ConstructorDeclaration(classDeclaration.Identifier)
15573
.WithModifiers(SyntaxTokenList.Create(SyntaxFactory.Token(SyntaxKind.PublicKeyword)));
15674

15775
var newConstructor = constructor.WithAdditionalAnnotations(Formatter.Annotation, Simplifier.Annotation)
15876
.NormalizeWhitespace();
15977

160-
if (!ConstructorHasAssignmentExpression(newConstructor))
78+
if (!RoslynClassFyleSystem.ConstructorHasAssignmentExpression(newConstructor))
16179
{
16280
newConstructor = newConstructor.AddBodyStatements(CreateAssignmentExpression());
16381
}
16482

165-
if (HasConstructor(classDeclaration))
83+
if (RoslynClassFyleSystem.HasConstructor(classDeclaration))
16684
{
16785
editor.ReplaceNode(constructor, newConstructor);
16886
} else
16987
{
170-
editor.InsertBefore(GetMethod(classDeclaration), newConstructor);
88+
editor.InsertBefore(RoslynClassFyleSystem.GetMethod(classDeclaration), newConstructor);
17189
}
17290
}
173-
174-
private static MethodDeclarationSyntax GetMethod(ClassDeclarationSyntax classDeclaration)
175-
{
176-
return classDeclaration.ChildNodes().OfType<MethodDeclarationSyntax>().FirstOrDefault();
177-
}
17891
}
17992
}
Lines changed: 13 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System.Linq;
1+
using System.IO.Abstractions.Analyzers.RoslynToken;
22
using System.Threading;
33
using System.Threading.Tasks;
44
using Microsoft.CodeAnalysis;
@@ -34,151 +34,64 @@ protected override async Task<Document> GetChangedDocumentAsync(CancellationToke
3434
{
3535
var editor = await DocumentEditor.CreateAsync(_document, cancellationToken).ConfigureAwait(false);
3636

37-
if (!HasFileSystemProperty(_class))
37+
if (!RoslynClassFyleSystem.HasFileSystemProperty(_class))
3838
{
3939
editor.InsertMembers(_class,
4040
0,
4141
new SyntaxNode[]
4242
{
43-
CreateFileSystemPropertyDeclaration()
43+
RoslynClassFyleSystem.CreateFileSystemPropertyDeclaration()
4444
});
4545
}
4646

4747
ConstructorAddParameter(_class, editor);
4848

49-
var compilationUnitSyntax = GetCompilationUnit(_class);
49+
var compilationUnitSyntax = RoslynClassFyleSystem.GetCompilationUnit(_class);
5050

5151
if (compilationUnitSyntax.Usings.Any())
5252
{
53-
editor.ReplaceNode(GetSystemIoUsing(compilationUnitSyntax), GetFileSystemUsing());
53+
editor.ReplaceNode(RoslynClassFyleSystem.GetSystemIoUsing(compilationUnitSyntax),
54+
RoslynClassFyleSystem.GetFileSystemUsing());
5455
}
5556

5657
return editor.GetChangedDocument();
5758
}
5859

59-
private static UsingDirectiveSyntax GetFileSystemUsing()
60-
{
61-
return SF.UsingDirective(SF.ParseName(Constants.FileSystemNameSpace));
62-
}
63-
64-
private static UsingDirectiveSyntax GetSystemIoUsing(CompilationUnitSyntax unit)
65-
{
66-
return unit.Usings.FirstOrDefault(x =>
67-
x.Name.NormalizeWhitespace().ToFullString().Equals(typeof(Path).Namespace));
68-
}
69-
70-
private static FieldDeclarationSyntax CreateFileSystemPropertyDeclaration()
71-
{
72-
return SF.FieldDeclaration(SF.VariableDeclaration(GetFileSystemType())
73-
.WithVariables(SF.SingletonSeparatedList(SF.VariableDeclarator(SF.Identifier(Constants.FieldFileSystemName)))))
74-
.WithModifiers(SF.TokenList(SF.Token(SyntaxKind.PrivateKeyword),
75-
SF.Token(SyntaxKind.ReadOnlyKeyword)));
76-
}
77-
78-
private static ParameterSyntax CreateFileSystemParameterDeclaration()
79-
{
80-
return SF.Parameter(SF.Identifier(Constants.ParameterFileSystemName))
81-
.WithType(GetFileSystemType())
82-
.WithAdditionalAnnotations(Formatter.Annotation, Simplifier.SpecialTypeAnnotation)
83-
.NormalizeWhitespace();
84-
}
85-
86-
private static TypeSyntax GetFileSystemType()
87-
{
88-
return SF.ParseTypeName(Constants.FileSystemInterfaceName);
89-
}
90-
9160
private static ExpressionStatementSyntax CreateAssignmentExpression()
9261
{
9362
return SF.ExpressionStatement(SF.AssignmentExpression(SyntaxKind.SimpleAssignmentExpression,
9463
SF.IdentifierName(Constants.FieldFileSystemName),
9564
SF.IdentifierName(Constants.ParameterFileSystemName)));
9665
}
9766

98-
private static CompilationUnitSyntax GetCompilationUnit(SyntaxNode node)
99-
{
100-
switch (node)
101-
{
102-
case null:
103-
104-
return null;
105-
case CompilationUnitSyntax compilationUnitSyntax:
106-
107-
return compilationUnitSyntax;
108-
default:
109-
110-
return GetCompilationUnit(node.Parent);
111-
}
112-
}
113-
114-
private static bool HasFileSystemProperty(TypeDeclarationSyntax classDeclaration)
115-
{
116-
return classDeclaration.Members.OfType<PropertyDeclarationSyntax>()
117-
.Any(x => x.Identifier.Text == Constants.FieldFileSystemName && x.Type == GetFileSystemType());
118-
}
119-
120-
private static ConstructorDeclarationSyntax GetConstructor(SyntaxNode classDeclaration)
121-
{
122-
return classDeclaration.ChildNodes().OfType<ConstructorDeclarationSyntax>().FirstOrDefault();
123-
}
124-
125-
private static bool ConstructorHasFileSystemParameter(BaseMethodDeclarationSyntax constructor)
126-
{
127-
return constructor.ParameterList.Parameters
128-
.Any(x => x.Identifier.Text == Constants.ParameterFileSystemName && x.Type == GetFileSystemType());
129-
}
130-
131-
private static bool ConstructorHasAssignmentExpression(BaseMethodDeclarationSyntax constructor)
132-
{
133-
if (constructor.Body == null)
134-
{
135-
return false;
136-
}
137-
138-
return constructor.Body.Statements.OfType<ExpressionStatementSyntax>()
139-
.Any(x => x.IsKind(SyntaxKind.SimpleAssignmentExpression)
140-
&& x.Expression.Contains(SF.IdentifierName(Constants.FieldFileSystemName))
141-
&& x.Expression.Contains(SF.IdentifierName(Constants.ParameterFileSystemName)));
142-
}
143-
144-
private static bool HasConstructor(SyntaxNode classDeclaration)
145-
{
146-
return classDeclaration.ChildNodes().OfType<ConstructorDeclarationSyntax>().Any();
147-
}
148-
14967
private static void ConstructorAddParameter(ClassDeclarationSyntax classDeclaration, SyntaxEditor editor)
15068
{
151-
var constructor = HasConstructor(classDeclaration)
152-
? GetConstructor(classDeclaration)
69+
var constructor = RoslynClassFyleSystem.HasConstructor(classDeclaration)
70+
? RoslynClassFyleSystem.GetConstructor(classDeclaration)
15371
: SF.ConstructorDeclaration(classDeclaration.Identifier)
15472
.WithModifiers(SyntaxTokenList.Create(SyntaxFactory.Token(SyntaxKind.PublicKeyword)));
15573

15674
var newConstructor = constructor.WithAdditionalAnnotations(Formatter.Annotation, Simplifier.Annotation)
15775
.NormalizeWhitespace();
15876

159-
if (!ConstructorHasAssignmentExpression(newConstructor))
77+
if (!RoslynClassFyleSystem.ConstructorHasAssignmentExpression(newConstructor))
16078
{
16179
newConstructor = newConstructor.AddBodyStatements(CreateAssignmentExpression());
16280
}
16381

164-
if (!ConstructorHasFileSystemParameter(newConstructor))
82+
if (!RoslynClassFyleSystem.ConstructorHasFileSystemParameter(newConstructor))
16583
{
166-
var parameter = CreateFileSystemParameterDeclaration();
84+
var parameter = RoslynClassFyleSystem.CreateFileSystemParameterDeclaration();
16785
newConstructor = newConstructor.AddParameterListParameters(parameter);
16886
}
16987

170-
if (HasConstructor(classDeclaration))
88+
if (RoslynClassFyleSystem.HasConstructor(classDeclaration))
17189
{
17290
editor.ReplaceNode(constructor, newConstructor);
17391
} else
17492
{
175-
editor.InsertBefore(GetMethod(classDeclaration), newConstructor);
93+
editor.InsertBefore(RoslynClassFyleSystem.GetMethod(classDeclaration), newConstructor);
17694
}
17795
}
178-
179-
private static MethodDeclarationSyntax GetMethod(ClassDeclarationSyntax classDeclaration)
180-
{
181-
return classDeclaration.ChildNodes().OfType<MethodDeclarationSyntax>().FirstOrDefault();
182-
}
18396
}
18497
}

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@ namespace System.IO.Abstractions.Analyzers.CodeActions
1010
{
1111
public class FileSystemInvokeCodeAction : CodeAction
1212
{
13-
private const string FieldFileSystemName = "_fileSystem";
14-
1513
private readonly Document _document;
1614

1715
private readonly InvocationExpressionSyntax _invocation;
@@ -31,7 +29,8 @@ protected override async Task<Document> GetChangedDocumentAsync(CancellationToke
3129
{
3230
var editor = await DocumentEditor.CreateAsync(_document, cancellationToken).ConfigureAwait(false);
3331

34-
editor.ReplaceNode(_invocation, SF.ParseExpression($"{FieldFileSystemName}.{_invocation.NormalizeWhitespace().ToFullString()}"));
32+
editor.ReplaceNode(_invocation,
33+
SF.ParseExpression($"{Constants.FieldFileSystemName}.{_invocation.NormalizeWhitespace().ToFullString()}"));
3534

3635
return editor.GetChangedDocument();
3736
}

0 commit comments

Comments
 (0)