Skip to content

Commit

Permalink
Merge pull request #958 from JoeRobich/improve-codefix-applier
Browse files Browse the repository at this point in the history
Implement GetDocumentDiagnosticsAsync in CodeFix DiagnosticProvider
  • Loading branch information
JoeRobich committed Feb 8, 2021
2 parents 68f3fc4 + b3e5183 commit bb5505c
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 2 deletions.
5 changes: 3 additions & 2 deletions src/Analyzers/SolutionCodeFixApplier.cs
Expand Up @@ -109,9 +109,10 @@ public override Task<IEnumerable<Diagnostic>> GetAllDiagnosticsAsync(Project pro
return GetProjectDiagnosticsAsync(project, cancellationToken);
}

public override Task<IEnumerable<Diagnostic>> GetDocumentDiagnosticsAsync(Document document, CancellationToken cancellationToken)
public override async Task<IEnumerable<Diagnostic>> GetDocumentDiagnosticsAsync(Document document, CancellationToken cancellationToken)
{
throw new NotImplementedException();
var projectDiagnostics = await GetProjectDiagnosticsAsync(document.Project, cancellationToken);
return projectDiagnostics.Where(diagnostic => diagnostic.Location.SourceTree?.FilePath == document.FilePath).ToImmutableArray();
}

public override Task<IEnumerable<Diagnostic>> GetProjectDiagnosticsAsync(Project project, CancellationToken cancellationToken)
Expand Down
46 changes: 46 additions & 0 deletions tests/Analyzers/ThirdPartyAnalyzerFormatterTests.cs
Expand Up @@ -118,5 +118,51 @@ void M()

await AssertCodeChangedAsync(testCode, expectedCode, editorConfig, fixCategory: FixCategory.Analyzers, analyzerReferences: analyzerReferences);
}

[Fact]
public async Task TestIDisposableAnalyzer_Loads()
{
var analyzerReferences = GetAnalyzerReferences("IDisposable");

var testCode = @"
using System.IO;
class C
{
void M()
{
var stream = File.OpenRead(string.Empty);
var b = stream.ReadByte();
stream.Dispose();
}
}
";

var expectedCode = @"
using System.IO;
class C
{
void M()
{
using (var stream = File.OpenRead(string.Empty))
{
var b = stream.ReadByte();
}
}
}
";

var editorConfig = new Dictionary<string, string>()
{
// Turn off all diagnostics analyzers
["dotnet_analyzer_diagnostic.severity"] = "none",

// Prefer using. IDISP017
["dotnet_diagnostic.IDISP017.severity"] = "error",
};

await AssertCodeChangedAsync(testCode, expectedCode, editorConfig, fixCategory: FixCategory.Analyzers, analyzerReferences: analyzerReferences);
}
}
}
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<packageSources>
<clear />
<add key="nuget.org" value="https://api.nuget.org/v3/index.json" />
</packageSources>
<disabledPackageSources />
</configuration>
Expand Up @@ -9,6 +9,7 @@
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
<PackageReference Include="IDisposableAnalyzers" Version="3.4.8" PrivateAssets="all" />
</ItemGroup>

</Project>

0 comments on commit bb5505c

Please sign in to comment.