Skip to content
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

Fixup analyzers branch #709

Merged
merged 2 commits into from Jun 25, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 2 additions & 4 deletions src/Analyzers/AnalyzerFinderHelpers.cs
Expand Up @@ -6,17 +6,15 @@
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,
ILogger logger)
public static ImmutableArray<(DiagnosticAnalyzer Analyzer, CodeFixProvider? Fixer)> LoadAnalyzersAndFixers(IEnumerable<Assembly> assemblies)
{
var types = assemblies
.SelectMany(assembly => assembly.GetTypes()
Expand Down
8 changes: 4 additions & 4 deletions src/Analyzers/AnalyzerFormatter.cs
Expand Up @@ -48,12 +48,12 @@ internal class AnalyzerFormatter : ICodeFormatter
}

var analysisStopwatch = Stopwatch.StartNew();
logger.LogTrace($"Running {_name} analysis.");
logger.LogTrace(Resources.Running_0_analysis, _name);

var formattablePaths = formattableDocuments.Select(id => solution.GetDocument(id)!.FilePath)
.OfType<string>().ToImmutableHashSet();

logger.LogTrace("Determining diagnostics...");
logger.LogTrace(Resources.Determining_diagnostics);

var allAnalyzers = analyzersAndFixers.Select(pair => pair.Analyzer).ToImmutableArray();
var projectAnalyzers = await _finder.FilterBySeverityAsync(solution.Projects, allAnalyzers, formattablePaths, formatOptions, cancellationToken).ConfigureAwait(false);
Expand All @@ -63,14 +63,14 @@ internal class AnalyzerFormatter : ICodeFormatter
var projectDiagnosticsMS = analysisStopwatch.ElapsedMilliseconds;
logger.LogTrace(Resources.Complete_in_0_ms, projectDiagnosticsMS);

logger.LogTrace("Fixing diagnostics...");
logger.LogTrace(Resources.Fixing_diagnostics);

solution = await FixDiagnosticsAsync(solution, analyzersAndFixers, projectDiagnostics, formattablePaths, logger, cancellationToken).ConfigureAwait(false);

var fixDiagnosticsMS = analysisStopwatch.ElapsedMilliseconds - projectDiagnosticsMS;
logger.LogTrace(Resources.Complete_in_0_ms, fixDiagnosticsMS);

logger.LogTrace("Analysis complete in {0}ms.", analysisStopwatch.ElapsedMilliseconds);
logger.LogTrace(Resources.Analysis_complete_in_0ms_, analysisStopwatch.ElapsedMilliseconds);

return solution;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Analyzers/AnalyzerReferenceAnalyzerFinder.cs
Expand Up @@ -30,7 +30,7 @@ internal class AnalyzerReferenceAnalyzerFinder : IAnalyzerFinder
.Distinct()
.Select(path => Assembly.LoadFrom(path));

return AnalyzerFinderHelpers.LoadAnalyzersAndFixers(assemblies, logger);
return AnalyzerFinderHelpers.LoadAnalyzersAndFixers(assemblies);
}

public Task<ImmutableDictionary<Project, ImmutableArray<DiagnosticAnalyzer>>> FilterBySeverityAsync(
Expand Down
2 changes: 1 addition & 1 deletion src/Analyzers/RoslynCodeStyleAnalyzerFinder.cs
Expand Up @@ -36,7 +36,7 @@ internal class RoslynCodeStyleAnalyzerFinder : IAnalyzerFinder
_featuresVisualBasicPath
}.Select(path => Assembly.LoadFrom(path));

return AnalyzerFinderHelpers.LoadAnalyzersAndFixers(assemblies, logger);
return AnalyzerFinderHelpers.LoadAnalyzersAndFixers(assemblies);
}

public Task<ImmutableDictionary<Project, ImmutableArray<DiagnosticAnalyzer>>> FilterBySeverityAsync(
Expand Down
6 changes: 3 additions & 3 deletions src/Analyzers/SolutionCodeFixApplier.cs
Expand Up @@ -26,14 +26,14 @@ internal class SolutionCodeFixApplier : ICodeFixApplier
var fixAllProvider = codeFix.GetFixAllProvider();
if (fixAllProvider?.GetSupportedFixAllScopes()?.Contains(FixAllScope.Solution) != true)
{
logger.LogWarning($"Unable to fix {diagnosticId}. Code fix {codeFix.GetType().Name} doesn't support Fix All in Solution.");
logger.LogWarning(Resources.Unable_to_fix_0_Code_fix_1_doesnt_support_Fix_All_in_Solution, diagnosticId, codeFix.GetType().Name);
return solution;
}

var project = solution.Projects.FirstOrDefault();
if (project == null)
{
throw new InvalidOperationException($"Solution {solution} has no projects");
throw new InvalidOperationException(string.Format(Resources.Solution_0_has__no_projects, solution));
}

var fixAllContext = new FixAllContext(
Expand All @@ -56,7 +56,7 @@ internal class SolutionCodeFixApplier : ICodeFixApplier
}
catch (Exception ex)
{
logger.LogWarning($"Failed to apply code fix {codeFix?.GetType().Name} for {diagnosticId}: {ex.Message}");
logger.LogWarning(Resources.Failed_to_apply_code_fix_0_for_1_2, codeFix?.GetType().Name, diagnosticId, ex.Message);
return solution;
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/CodeFormatter.cs
Expand Up @@ -29,8 +29,8 @@ internal static class CodeFormatter
new EndOfLineFormatter(),
new CharsetFormatter(),
new ImportsFormatter(),
new AnalyzerFormatter("Code Style", new RoslynCodeStyleAnalyzerFinder(), new AnalyzerRunner(), new SolutionCodeFixApplier()),
new AnalyzerFormatter("Analyzer Reference", new AnalyzerReferenceAnalyzerFinder(), new AnalyzerRunner(), new SolutionCodeFixApplier()),
new AnalyzerFormatter(Resources.Code_Style, new RoslynCodeStyleAnalyzerFinder(), new AnalyzerRunner(), new SolutionCodeFixApplier()),
new AnalyzerFormatter(Resources.Analyzer_Reference, new AnalyzerReferenceAnalyzerFinder(), new AnalyzerRunner(), new SolutionCodeFixApplier()),
}.ToImmutableArray();

public static async Task<WorkspaceFormatResult> FormatWorkspaceAsync(
Expand Down
85 changes: 56 additions & 29 deletions src/Resources.resx
@@ -1,17 +1,17 @@
<?xml version="1.0" encoding="utf-8"?>
<root>
<!--
Microsoft ResX Schema

<!--
Microsoft ResX Schema
Version 2.0

The primary goals of this format is to allow a simple XML format
that is mostly human readable. The generation and parsing of the
various data types are done through the TypeConverter classes
The primary goals of this format is to allow a simple XML format
that is mostly human readable. The generation and parsing of the
various data types are done through the TypeConverter classes
associated with the data types.

Example:

... ado.net/XML headers & schema ...
<resheader name="resmimetype">text/microsoft-resx</resheader>
<resheader name="version">2.0</resheader>
Expand All @@ -26,36 +26,36 @@
<value>[base64 mime encoded string representing a byte array form of the .NET Framework object]</value>
<comment>This is a comment</comment>
</data>

There are any number of "resheader" rows that contain simple
There are any number of "resheader" rows that contain simple
name/value pairs.

Each data row contains a name, and value. The row also contains a
type or mimetype. Type corresponds to a .NET class that support
text/value conversion through the TypeConverter architecture.
Classes that don't support this are serialized and stored with the
Each data row contains a name, and value. The row also contains a
type or mimetype. Type corresponds to a .NET class that support
text/value conversion through the TypeConverter architecture.
Classes that don't support this are serialized and stored with the
mimetype set.

The mimetype is used for serialized objects, and tells the
ResXResourceReader how to depersist the object. This is currently not
The mimetype is used for serialized objects, and tells the
ResXResourceReader how to depersist the object. This is currently not
extensible. For a given mimetype the value must be set accordingly:

Note - application/x-microsoft.net.object.binary.base64 is the format
that the ResXResourceWriter will generate, however the reader can
Note - application/x-microsoft.net.object.binary.base64 is the format
that the ResXResourceWriter will generate, however the reader can
read any of the formats listed below.

mimetype: application/x-microsoft.net.object.binary.base64
value : The object must be serialized with
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
: and then encoded with base64 encoding.

mimetype: application/x-microsoft.net.object.soap.base64
value : The object must be serialized with
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Soap.SoapFormatter
: and then encoded with base64 encoding.

mimetype: application/x-microsoft.net.object.bytearray.base64
value : The object must be serialized into a byte array
value : The object must be serialized into a byte array
: using a System.ComponentModel.TypeConverter
: and then encoded with base64 encoding.
-->
Expand Down Expand Up @@ -243,7 +243,7 @@
<data name="The_workspace_option_is_deprecated_Use_the_project_argument_instead" xml:space="preserve">
<value>The `--workspace` option is deprecated. Use the `&lt;project&gt;` argument instead.</value>
</data>
<data name="The_folder_option_is_deprecated_for_specifying_the_path_Pass_the_folder_option_but_specify_the_path_with_the_project_argument_instead" xml:space="preserve">
<data name="The_folder_option_is_deprecated_for_specifying_the_path_Pass_the_folder_option_but_specify_the_path_with_the_project_argument_instead" xml:space="preserve">
<value>The `--folder` option is deprecated for specifying the path. Pass the `--folder` option but specify the path with the project argument instead.</value>
</data>
<data name="The_files_option_is_deprecated_Use_the_include_option_instead" xml:space="preserve">
Expand All @@ -255,10 +255,37 @@
<data name="Fix_imports_ordering" xml:space="preserve">
<value>Fix imports ordering.</value>
</data>
<data name="Unable_to_organize_imports_for_0_The_document_is_too_complex">
<data name="Unable_to_organize_imports_for_0_The_document_is_too_complex" xml:space="preserve">
<value>Unable to organize imports for '{0}'. The document is too complex.</value>
</data>
<data name="Run_code_style_analyzer_and_apply_fixes" xml:space="preserve">
<value>Run code style analyzers and apply fixes.</value>
</data>
<data name="Analyzer_Reference" xml:space="preserve">
<value>Analyzer Reference</value>
</data>
<data name="Code_Style" xml:space="preserve">
<value>Code Style</value>
</data>
<data name="Analysis_complete_in_0ms." xml:space="preserve">
<value>Analysis complete in {0}ms.</value>
</data>
<data name="Determining_diagnostics" xml:space="preserve">
<value>Determining diagnostics...</value>
</data>
<data name="Failed_to_apply_code_fix_0_for_1_2" xml:space="preserve">
<value>Failed to apply code fix {0} for {1}: {2}</value>
</data>
<data name="Fixing_diagnostics" xml:space="preserve">
<value>Fixing diagnostics...</value>
</data>
<data name="Running_0_analysis" xml:space="preserve">
<value>Running {0} analysis.</value>
</data>
<data name="Solution_0_has_ no_projects" xml:space="preserve">
<value>Solution {0} has no projects</value>
</data>
<data name="Unable_to_fix_0_Code_fix_1_doesnt_support_Fix_All_in_Solution" xml:space="preserve">
<value>Unable to fix {0}. Code fix {1} doesn't support Fix All in Solution.</value>
</data>
</root>
45 changes: 45 additions & 0 deletions src/xlf/Resources.cs.xlf
Expand Up @@ -17,6 +17,16 @@
<target state="new">Accepts a file path, which if provided, will produce a json report in the given directory.</target>
<note />
</trans-unit>
<trans-unit id="Analysis_complete_in_0ms.">
<source>Analysis complete in {0}ms.</source>
<target state="new">Analysis complete in {0}ms.</target>
<note />
</trans-unit>
<trans-unit id="Analyzer_Reference">
<source>Analyzer Reference</source>
<target state="new">Analyzer Reference</target>
<note />
</trans-unit>
<trans-unit id="Both_a_MSBuild_project_file_and_solution_file_found_in_0_Specify_which_to_use_with_the_workspace_option">
<source>Both a MSBuild project file and solution file found in '{0}'. Specify which to use with the --workspace option.</source>
<target state="translated">{0} obsahuje jak soubor projektu MSBuild, tak soubor řešení. Určete, který soubor chcete použít, pomocí parametru --workspace.</target>
Expand All @@ -37,6 +47,11 @@
<target state="new">Cannot specify both project argument and workspace option.</target>
<note />
</trans-unit>
<trans-unit id="Code_Style">
<source>Code Style</source>
<target state="new">Code Style</target>
<note />
</trans-unit>
<trans-unit id="Complete_in_0_ms">
<source>Complete in {0}ms.</source>
<target state="new">Complete in {0}ms.</target>
Expand All @@ -52,11 +67,21 @@
<target state="translated">{0} nejde formátovat. Formátovat jde v současné době jenom projekty v jazycích C# a Visual Basic.</target>
<note />
</trans-unit>
<trans-unit id="Determining_diagnostics">
<source>Determining diagnostics...</source>
<target state="new">Determining diagnostics...</target>
<note />
</trans-unit>
<trans-unit id="Determining_formattable_files">
<source>Determining formattable files.</source>
<target state="new">Determining formattable files.</target>
<note />
</trans-unit>
<trans-unit id="Failed_to_apply_code_fix_0_for_1_2">
<source>Failed to apply code fix {0} for {1}: {2}</source>
<target state="new">Failed to apply code fix {0} for {1}: {2}</target>
<note />
</trans-unit>
<trans-unit id="Failed_to_save_formatting_changes">
<source>Failed to save formatting changes.</source>
<target state="translated">Nepodařilo se uložit změny formátování.</target>
Expand Down Expand Up @@ -87,6 +112,11 @@
<target state="new">Fix whitespace formatting.</target>
<note />
</trans-unit>
<trans-unit id="Fixing_diagnostics">
<source>Fixing diagnostics...</source>
<target state="new">Fixing diagnostics...</target>
<note />
</trans-unit>
<trans-unit id="Format_complete_in_0_ms">
<source>Format complete in {0}ms.</source>
<target state="new">Format complete in {0}ms.</target>
Expand Down Expand Up @@ -142,6 +172,11 @@
<target state="new">Run code style analyzers and apply fixes.</target>
<note />
</trans-unit>
<trans-unit id="Running_0_analysis">
<source>Running {0} analysis.</source>
<target state="new">Running {0} analysis.</target>
<note />
</trans-unit>
<trans-unit id="Running_formatters">
<source>Running formatters.</source>
<target state="new">Running formatters.</target>
Expand All @@ -157,6 +192,11 @@
<target state="translated">Přeskočí se odkazovaný projekt {0}.</target>
<note />
</trans-unit>
<trans-unit id="Solution_0_has_ no_projects">
<source>Solution {0} has no projects</source>
<target state="new">Solution {0} has no projects</target>
<note />
</trans-unit>
<trans-unit id="The_dotnet_CLI_version_is_0">
<source>The dotnet CLI version is '{0}'.</source>
<target state="new">The dotnet CLI version is '{0}'.</target>
Expand Down Expand Up @@ -207,6 +247,11 @@
<target state="new">The `--workspace` option is deprecated. Use the `&lt;project&gt;` argument instead.</target>
<note />
</trans-unit>
<trans-unit id="Unable_to_fix_0_Code_fix_1_doesnt_support_Fix_All_in_Solution">
<source>Unable to fix {0}. Code fix {1} doesn't support Fix All in Solution.</source>
<target state="new">Unable to fix {0}. Code fix {1} doesn't support Fix All in Solution.</target>
<note />
</trans-unit>
<trans-unit id="Unable_to_locate_MSBuild_Ensure_the_NET_SDK_was_installed_with_the_official_installer">
<source>Unable to locate MSBuild. Ensure the .NET SDK was installed with the official installer.</source>
<target state="new">Unable to locate MSBuild. Ensure the .NET SDK was installed with the official installer.</target>
Expand Down