Skip to content

Commit 81d187f

Browse files
committed
Code Fixers should use correct InvocationExpression
1 parent ee13833 commit 81d187f

File tree

4 files changed

+15
-11
lines changed

4 files changed

+15
-11
lines changed

System.IO.Abstractions.Analyzers.Tests/TestData/CodeFix/DirectoryCodeFix/AfterFix.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ namespace SomeNameSpace
1515
{
1616
const string filePath = "C:\\temp";
1717

18-
_fileSystem.Directory.Exists(filePath);
18+
new ConfigurationBuilder().SetBasePath(_fileSystem.Directory.GetCurrentDirectory());
1919
}
2020
}
2121
}

System.IO.Abstractions.Analyzers.Tests/TestData/CodeFix/DirectoryCodeFix/BeforeFix.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ namespace SomeNameSpace
1515
{
1616
const string filePath = "C:\\temp";
1717

18-
Directory.Exists(filePath);
18+
new ConfigurationBuilder().SetBasePath(Directory.GetCurrentDirectory());
1919
}
2020
}
2121
}

System.IO.Abstractions.Analyzers/Analyzers/BaseFileSystemNodeAnalyzer.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,10 @@ protected override void AnalyzeCompilation(CompilationStartAnalysisContext compi
4646

4747
private bool IsTypesEquals(TypeSyntax type) => type.NormalizeWhitespace().ToFullString() == GetFileSystemType().Name;
4848

49-
private bool IsStaticInvocationStartWith(InvocationExpressionSyntax invocation) => invocation.Expression.NormalizeWhitespace()
50-
.ToFullString()
51-
.StartsWith(GetFileSystemType().Name + ".", StringComparison.CurrentCultureIgnoreCase);
49+
private bool IsStaticInvocationStartWith(InvocationExpressionSyntax invocation) =>
50+
invocation.IsKind(SyntaxKind.InvocationExpression)
51+
&& invocation.Expression.NormalizeWhitespace()
52+
.ToFullString()
53+
.StartsWith(GetFileSystemType().Name + ".", StringComparison.CurrentCultureIgnoreCase);
5254
}
5355
}

System.IO.Abstractions.Analyzers/CodeFixes/BaseInvokeCodeFix.cs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,16 @@ public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context)
2222
{
2323
var root = await context.Document.GetSyntaxRootAsync(context.CancellationToken).ConfigureAwait(false);
2424

25-
if (root.FindNode(context.Span).Ancestors().OfType<ClassDeclarationSyntax>().Any(RoslynClassFileSystem.HasFileSystemField))
26-
{
27-
var invocation = root.FindNode(context.Span).FirstAncestorOrSelf<InvocationExpressionSyntax>();
25+
var classDeclaration = root.FindNode(context.Span).FirstAncestorOrSelf<ClassDeclarationSyntax>();
2826

29-
var @class = root.FindNode(context.Span)
30-
.FirstAncestorOrSelf<ClassDeclarationSyntax>();
27+
if (RoslynClassFileSystem.HasFileSystemField(classDeclaration))
28+
{
29+
var invocation = root.FindNode(context.Span)
30+
.DescendantNodesAndSelf()
31+
.OfType<InvocationExpressionSyntax>()
32+
.FirstOrDefault();
3133

32-
var field = RoslynClassFileSystem.GetFileSystemFieldFromClass(@class);
34+
var field = RoslynClassFileSystem.GetFileSystemFieldFromClass(classDeclaration);
3335

3436
context.RegisterCodeFix(new FileSystemInvokeCodeAction(Title,
3537
context.Document,

0 commit comments

Comments
 (0)