Skip to content

Commit 7371902

Browse files
committed
Combine base Analyzer
1 parent e36b44c commit 7371902

16 files changed

Lines changed: 86 additions & 52 deletions

File tree

System.IO.Abstractions.Analyzers.Tests/Analyzers/DirectoryInfoAnalyzerTests.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ public void Analyzer_is_not_triggered(string filename)
1919

2020
[Theory]
2121
[InlineData("WithOutFileSystem.txt", 15, 28)]
22+
[InlineData("StaticInvocation.txt", 15, 40)]
2223
public void Analyzer_is_triggered(string filename, int diagnosticLine, int diagnosticColumn)
2324
{
2425
var source = ReadFile(filename);

System.IO.Abstractions.Analyzers.Tests/Analyzers/DriveInfoAnalyzerTests.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ public void Analyzer_is_not_triggered(string filename)
1919

2020
[Theory]
2121
[InlineData("WithOutFileSystem.txt", 15, 21)]
22+
[InlineData("StaticInvocation.txt", 15, 25)]
2223
public void Analyzer_is_triggered(string filename, int diagnosticLine, int diagnosticColumn)
2324
{
2425
var source = ReadFile(filename);

System.IO.Abstractions.Analyzers.Tests/Analyzers/FileSystemWatcherAnalyzerTests.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ public void Analyzer_is_not_triggered(string filename)
1919

2020
[Theory]
2121
[InlineData("WithOutFileSystem.txt", 15, 42)]
22+
[InlineData("StaticInvocation.txt", 15, 4)]
2223
public void Analyzer_is_triggered(string filename, int diagnosticLine, int diagnosticColumn)
2324
{
2425
var source = ReadFile(filename);
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
using System.IO;
2+
3+
namespace SomeNameSpace
4+
{
5+
public class WithOutFileSystem
6+
{
7+
public WithOutFileSystem()
8+
{
9+
}
10+
11+
public void SomeMethod()
12+
{
13+
const string path = "C:\\temp.txt";
14+
15+
IEnumerable<FileSystemInfo> infos = DirectoryInfo.InternalEnumerateInfos(path, "*.txt",SearchTarget.Files, EnumerationOptions.Compatible);
16+
}
17+
}
18+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
using System.IO;
2+
3+
namespace SomeNameSpace
4+
{
5+
public class WithOutFileSystem
6+
{
7+
public WithOutFileSystem()
8+
{
9+
}
10+
11+
public void SomeMethod()
12+
{
13+
const string path = "C:\\temp.txt";
14+
15+
DriveInfo[] drives = DriveInfo.GetDrives();
16+
}
17+
}
18+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
using System.IO;
2+
3+
namespace SomeNameSpace
4+
{
5+
public class WithOutFileSystem
6+
{
7+
public WithOutFileSystem()
8+
{
9+
}
10+
11+
public void SomeMethod()
12+
{
13+
const string path = "C:\\temp.txt";
14+
15+
FileSystemWatcher.ReadMaxUserLimit(path);
16+
}
17+
}
18+
}

System.IO.Abstractions.Analyzers/Analyzers/BaseFileSystemNodeObjectCreationAnalyzer.cs renamed to System.IO.Abstractions.Analyzers/Analyzers/BaseFileSystemNodeAnalyzer.cs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
namespace System.IO.Abstractions.Analyzers.Analyzers
77
{
8-
public abstract class BaseFileSystemNodeObjectCreationAnalyzer : BaseFileSystemAnalyzer
8+
public abstract class BaseFileSystemNodeAnalyzer : BaseFileSystemAnalyzer
99
{
1010
protected override void AnalyzeCompilation(CompilationStartAnalysisContext compilationStartContext,
1111
FileSystemContext fileSystemContext)
@@ -15,6 +15,17 @@ protected override void AnalyzeCompilation(CompilationStartAnalysisContext compi
1515
return;
1616
}
1717

18+
compilationStartContext.RegisterSyntaxNodeAction(syntaxContext =>
19+
{
20+
var invocation = (InvocationExpressionSyntax) syntaxContext.Node;
21+
22+
if (invocation.NormalizeWhitespace().ToFullString().StartsWith(GetFileSystemType().Name))
23+
{
24+
Analyze(syntaxContext, invocation);
25+
}
26+
},
27+
SyntaxKind.InvocationExpression);
28+
1829
compilationStartContext.RegisterSyntaxNodeAction(syntaxContext =>
1930
{
2031
var invocation = (ObjectCreationExpressionSyntax) syntaxContext.Node;
@@ -27,7 +38,7 @@ protected override void AnalyzeCompilation(CompilationStartAnalysisContext compi
2738
SyntaxKind.ObjectCreationExpression);
2839
}
2940

30-
protected abstract void Analyze(SyntaxNodeAnalysisContext context, ObjectCreationExpressionSyntax syntax);
41+
protected abstract void Analyze(SyntaxNodeAnalysisContext context, ExpressionSyntax invocation);
3142

3243
protected abstract Type GetFileSystemType();
3344
}

System.IO.Abstractions.Analyzers/Analyzers/BaseFileSystemNodeStaticCallAnalyzer.cs

Lines changed: 0 additions & 34 deletions
This file was deleted.

System.IO.Abstractions.Analyzers/Analyzers/FileSystemTypeAnalyzers/DirectoryAnalyzer.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
namespace System.IO.Abstractions.Analyzers.Analyzers.FileSystemTypeAnalyzers
88
{
99
[DiagnosticAnalyzer(LanguageNames.CSharp)]
10-
public class DirectoryAnalyzer : BaseFileSystemNodeStaticCallAnalyzer
10+
public class DirectoryAnalyzer : BaseFileSystemNodeAnalyzer
1111
{
1212
/// <summary>
1313
/// Diagnostic Identifier
@@ -43,7 +43,7 @@ public class DirectoryAnalyzer : BaseFileSystemNodeStaticCallAnalyzer
4343

4444
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics => ImmutableArray.Create(Rule);
4545

46-
protected override void Analyze(SyntaxNodeAnalysisContext context, InvocationExpressionSyntax invocation)
46+
protected override void Analyze(SyntaxNodeAnalysisContext context, ExpressionSyntax invocation)
4747
{
4848
context.ReportDiagnostic(Diagnostic.Create(Rule, invocation.GetLocation()));
4949
}

System.IO.Abstractions.Analyzers/Analyzers/FileSystemTypeAnalyzers/DirectoryInfoAnalyzer.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
namespace System.IO.Abstractions.Analyzers.Analyzers.FileSystemTypeAnalyzers
88
{
99
[DiagnosticAnalyzer(LanguageNames.CSharp)]
10-
public class DirectoryInfoAnalyzer: BaseFileSystemNodeObjectCreationAnalyzer
10+
public class DirectoryInfoAnalyzer: BaseFileSystemNodeAnalyzer
1111
{
1212
/// <summary>
1313
/// Diagnostic Identifier
@@ -43,7 +43,7 @@ public class DirectoryInfoAnalyzer: BaseFileSystemNodeObjectCreationAnalyzer
4343

4444
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics => ImmutableArray.Create(Rule);
4545

46-
protected override void Analyze(SyntaxNodeAnalysisContext context, ObjectCreationExpressionSyntax syntax)
46+
protected override void Analyze(SyntaxNodeAnalysisContext context, ExpressionSyntax syntax)
4747
{
4848
context.ReportDiagnostic(Diagnostic.Create(Rule, syntax.GetLocation()));
4949
}

0 commit comments

Comments
 (0)