Created
March 3, 2020 17:25
-
-
Save sailro/0131a8b334b2f63c6d5caf21ee2b027e to your computer and use it in GitHub Desktop.
Playing with Analyzers and Roslyn
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
using Microsoft.CodeAnalysis; | |
using System; | |
using System.Collections.Generic; | |
using System.Collections.Immutable; | |
using System.Diagnostics; | |
using System.Reflection; | |
using Microsoft.CodeAnalysis.CSharp; | |
using Microsoft.CodeAnalysis.Diagnostics; | |
using Microsoft.CodeAnalysis.Text; | |
namespace RoslynProto | |
{ | |
internal class Program | |
{ | |
private static void Main() | |
{ | |
const string TestProjectName = "TestProject"; | |
const string TestFileName = "Test.cs"; | |
var projectId = ProjectId.CreateNewId(debugName: TestProjectName); | |
var solution = new AdhocWorkspace() | |
.CurrentSolution | |
.AddProject(projectId, TestProjectName, TestProjectName, LanguageNames.CSharp); | |
var documentId = DocumentId.CreateNewId(projectId, debugName: TestFileName); | |
solution = solution.AddDocument(documentId, TestFileName, SourceText.From(@" | |
class TestScript | |
{ | |
void UnUsedMethod() | |
{ | |
} | |
void Demo(int unusedField) | |
{ | |
Demo(0); | |
} | |
} | |
")); | |
var project = solution | |
.GetProject(projectId) | |
.AddAnalyzerConfigDocument(".editorconfig", SourceText.From(@" | |
[*] | |
dotnet_code_quality_unused_parameters = all:suggestion | |
dotnet_diagnostic.IDE0060.severity = suggestion | |
"), filePath: @"z:\\.editorconfig") | |
.Project; | |
var compilation = project | |
.GetCompilationAsync().Result | |
.WithOptions(new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary)) | |
.AddReferences( | |
MetadataReference.CreateFromFile(typeof(object).GetTypeInfo().Assembly.Location), | |
MetadataReference.CreateFromFile(typeof(Console).GetTypeInfo().Assembly.Location) | |
); | |
var compilationWithAnalyzers = compilation | |
.WithAnalyzers(GetAnalyzers()); | |
var diagnostics = compilationWithAnalyzers | |
.GetAllDiagnosticsAsync() | |
.Result; | |
foreach (var diagnostic in diagnostics) | |
{ | |
Console.WriteLine(">>>Diagnostic details:"); | |
Console.WriteLine(diagnostic.ToString()); | |
Console.WriteLine(); | |
} | |
} | |
private static IEnumerable<DiagnosticAnalyzer> LoadAnalyzers(string assembly) | |
{ | |
var reference = new AnalyzerFileReference(assembly, new AnalyzerAssemblyLoader()); | |
reference.AnalyzerLoadFailed += (s, e) => { Debug.Fail(e.Message); }; | |
return reference.GetAnalyzers(LanguageNames.CSharp); | |
} | |
private static ImmutableArray<DiagnosticAnalyzer> GetAnalyzers() | |
{ | |
var analyzers = new List<DiagnosticAnalyzer>(); | |
analyzers.AddRange(LoadAnalyzers("Microsoft.CodeAnalysis.Features.dll")); | |
analyzers.AddRange(LoadAnalyzers("Microsoft.CodeAnalysis.Csharp.Features.dll")); | |
return ImmutableArray | |
.Create<DiagnosticAnalyzer>() | |
.AddRange(analyzers); | |
} | |
internal class AnalyzerAssemblyLoader : IAnalyzerAssemblyLoader | |
{ | |
public Assembly LoadFromPath(string fullPath) | |
{ | |
return Assembly.LoadFrom(fullPath); | |
} | |
public void AddDependencyLocation(string fullPath) | |
{ | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment