New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added reflection based discovery of analyzers and fixes #698
Merged
JoeRobich
merged 11 commits into
dotnet:feature/analyzers
from
JoeRobich:add-reflection-discovery
Jun 9, 2020
Merged
Changes from 9 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
49d01eb
Added reflection based discovery of analyzers and fixes
JoeRobich 4351b5a
Bump version
JoeRobich 93a6396
Await refactoring
JoeRobich 7b8edc8
Moved parallelism to the AnalyzerRunner
JoeRobich 128de98
Filter analyzers by severity before adding to compilation
JoeRobich 577fbd1
Validate Roslyn CodeStyle.
JoeRobich f14c03f
Run DiagnosticAnalyzers from references
JoeRobich f063ce8
Comment out StyleCop reference
JoeRobich b7c20e4
Fix style error
JoeRobich e2b7721
Addressed PR feedback
JoeRobich 507038a
Fix formatting issues
JoeRobich File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,42 +1,44 @@ | ||
{ | ||
"version": "2.0.0", | ||
"tasks": [ | ||
{ | ||
"label": "build", | ||
"command": "dotnet", | ||
"type": "process", | ||
"args": [ | ||
"build", | ||
"${workspaceFolder}/src/dotnet-format.csproj", | ||
"/property:GenerateFullPaths=true", | ||
"/consoleloggerparameters:NoSummary" | ||
], | ||
"problemMatcher": "$msCompile" | ||
}, | ||
{ | ||
"label": "publish", | ||
"command": "dotnet", | ||
"type": "process", | ||
"args": [ | ||
"publish", | ||
"${workspaceFolder}/src/dotnet-format.csproj", | ||
"/property:GenerateFullPaths=true", | ||
"/consoleloggerparameters:NoSummary" | ||
], | ||
"problemMatcher": "$msCompile" | ||
}, | ||
{ | ||
"label": "watch", | ||
"command": "dotnet", | ||
"type": "process", | ||
"args": [ | ||
"watch", | ||
"run", | ||
"${workspaceFolder}/src/dotnet-format.csproj", | ||
"/property:GenerateFullPaths=true", | ||
"/consoleloggerparameters:NoSummary" | ||
], | ||
"problemMatcher": "$msCompile" | ||
} | ||
] | ||
"version": "2.0.0", | ||
"tasks": [ | ||
{ | ||
"label": "build", | ||
"command": "dotnet", | ||
"type": "process", | ||
"args": [ | ||
"build", | ||
"${workspaceFolder}/src/dotnet-format.csproj", | ||
"/property:GenerateFullPaths=true", | ||
"/consoleloggerparameters:NoSummary" | ||
], | ||
"problemMatcher": "$msCompile" | ||
}, | ||
{ | ||
"label": "publish", | ||
"command": "dotnet", | ||
"type": "process", | ||
"args": [ | ||
"publish", | ||
"${workspaceFolder}/src/dotnet-format.csproj", | ||
"-c", | ||
"Debug", | ||
"/property:GenerateFullPaths=true", | ||
"/consoleloggerparameters:NoSummary" | ||
], | ||
"problemMatcher": "$msCompile" | ||
}, | ||
{ | ||
"label": "watch", | ||
"command": "dotnet", | ||
"type": "process", | ||
"args": [ | ||
"watch", | ||
"run", | ||
"${workspaceFolder}/src/dotnet-format.csproj", | ||
"/property:GenerateFullPaths=true", | ||
"/consoleloggerparameters:NoSummary" | ||
], | ||
"problemMatcher": "$msCompile" | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the MIT license. See License.txt in the project root for license information. | ||
|
||
using System.Collections.Generic; | ||
using System.Collections.Immutable; | ||
using System.Linq; | ||
using System.Reflection; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Microsoft.CodeAnalysis.CodeFixes; | ||
using Microsoft.CodeAnalysis.Diagnostics; | ||
using Microsoft.Extensions.Logging; | ||
|
||
namespace Microsoft.CodeAnalysis.Tools.Analyzers | ||
{ | ||
internal static class AnalyzerFinderHelpers | ||
{ | ||
public static ImmutableArray<(DiagnosticAnalyzer Analyzer, CodeFixProvider? Fixer)> LoadAnalyzersAndFixers( | ||
IEnumerable<Assembly> assemblies, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we add unit tests for this class? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Will add in a follow up |
||
ILogger logger) | ||
{ | ||
var types = assemblies | ||
.SelectMany(assembly => assembly.GetTypes() | ||
.Where(type => !type.GetTypeInfo().IsInterface && | ||
!type.GetTypeInfo().IsAbstract && | ||
!type.GetTypeInfo().ContainsGenericParameters)); | ||
|
||
var codeFixProviders = types | ||
.Where(t => typeof(CodeFixProvider).IsAssignableFrom(t)) | ||
.Select(type => type.TryCreateInstance<CodeFixProvider>(out var instance) ? instance : null) | ||
.OfType<CodeFixProvider>() | ||
.ToImmutableArray(); | ||
|
||
var diagnosticAnalyzers = types | ||
.Where(t => typeof(DiagnosticAnalyzer).IsAssignableFrom(t)) | ||
.Select(type => type.TryCreateInstance<DiagnosticAnalyzer>(out var instance) ? instance : null) | ||
.OfType<DiagnosticAnalyzer>() | ||
.ToImmutableArray(); | ||
|
||
var builder = ImmutableArray.CreateBuilder<(DiagnosticAnalyzer Analyzer, CodeFixProvider? Fixer)>(); | ||
foreach (var diagnosticAnalyzer in diagnosticAnalyzers) | ||
{ | ||
var diagnosticIds = diagnosticAnalyzer.SupportedDiagnostics.Select(diagnostic => diagnostic.Id).ToImmutableHashSet(); | ||
var codeFixProvider = codeFixProviders.FirstOrDefault(codeFixProvider => codeFixProvider.FixableDiagnosticIds.Any(id => diagnosticIds.Contains(id))); | ||
|
||
if (codeFixProvider is null) | ||
{ | ||
continue; | ||
} | ||
|
||
builder.Add((diagnosticAnalyzer, codeFixProvider)); | ||
} | ||
|
||
return builder.ToImmutableArray(); | ||
} | ||
|
||
public static async Task<ImmutableDictionary<Project, ImmutableArray<DiagnosticAnalyzer>>> FilterBySeverityAsync( | ||
IEnumerable<Project> projects, | ||
ImmutableArray<DiagnosticAnalyzer> allAnalyzers, | ||
ImmutableHashSet<string> formattablePaths, | ||
DiagnosticSeverity minimumSeverity, | ||
CancellationToken cancellationToken) | ||
{ | ||
var projectAnalyzers = ImmutableDictionary.CreateBuilder<Project, ImmutableArray<DiagnosticAnalyzer>>(); | ||
foreach (var project in projects) | ||
{ | ||
var analyzers = ImmutableArray.CreateBuilder<DiagnosticAnalyzer>(); | ||
|
||
foreach (var analyzer in allAnalyzers) | ||
{ | ||
var severity = await analyzer.GetSeverityAsync(project, formattablePaths, cancellationToken).ConfigureAwait(false); | ||
if (severity >= minimumSeverity) | ||
{ | ||
analyzers.Add(analyzer); | ||
} | ||
} | ||
|
||
projectAnalyzers.Add(project, analyzers.ToImmutableArray()); | ||
} | ||
|
||
return projectAnalyzers.ToImmutableDictionary(); | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In order to run codestyle analyzers, we need to publish before we run because not all dependencies are copied into the build folder.