diff --git a/src/Spectre.Console.Analyzer/Analyzers/FavorInstanceAnsiConsoleOverStaticAnalyzer.cs b/src/Spectre.Console.Analyzer/Analyzers/FavorInstanceAnsiConsoleOverStaticAnalyzer.cs index eb641797e..379103b0b 100644 --- a/src/Spectre.Console.Analyzer/Analyzers/FavorInstanceAnsiConsoleOverStaticAnalyzer.cs +++ b/src/Spectre.Console.Analyzer/Analyzers/FavorInstanceAnsiConsoleOverStaticAnalyzer.cs @@ -16,11 +16,15 @@ public class FavorInstanceAnsiConsoleOverStaticAnalyzer : SpectreAnalyzer /// protected override void AnalyzeCompilation(CompilationStartAnalysisContext compilationStartContext) { + var ansiConsoleType = compilationStartContext.Compilation.GetTypeByMetadataName("Spectre.Console.AnsiConsole"); + if (ansiConsoleType == null) + { + return; + } + compilationStartContext.RegisterOperationAction( context => { - var ansiConsoleType = context.Compilation.GetTypeByMetadataName("Spectre.Console.AnsiConsole"); - // if this operation isn't an invocation against one of the System.Console methods // defined in _methods then we can safely stop analyzing and return; var invocationOperation = (IInvocationOperation)context.Operation; diff --git a/src/Spectre.Console.Analyzer/Analyzers/NoConcurrentLiveRenderablesAnalyzer.cs b/src/Spectre.Console.Analyzer/Analyzers/NoConcurrentLiveRenderablesAnalyzer.cs index b0cd0b7fd..bae736379 100644 --- a/src/Spectre.Console.Analyzer/Analyzers/NoConcurrentLiveRenderablesAnalyzer.cs +++ b/src/Spectre.Console.Analyzer/Analyzers/NoConcurrentLiveRenderablesAnalyzer.cs @@ -17,6 +17,16 @@ public class NoConcurrentLiveRenderablesAnalyzer : SpectreAnalyzer /// protected override void AnalyzeCompilation(CompilationStartAnalysisContext compilationStartContext) { + var liveTypes = Constants.LiveRenderables + .Select(i => compilationStartContext.Compilation.GetTypeByMetadataName(i)) + .Where(i => i != null) + .ToImmutableArray(); + + if (liveTypes.Length == 0) + { + return; + } + compilationStartContext.RegisterOperationAction( context => { @@ -29,10 +39,6 @@ protected override void AnalyzeCompilation(CompilationStartAnalysisContext compi return; } - var liveTypes = Constants.LiveRenderables - .Select(i => context.Compilation.GetTypeByMetadataName(i)) - .ToImmutableArray(); - if (liveTypes.All(i => !SymbolEqualityComparer.Default.Equals(i, methodSymbol.ContainingType))) { return; diff --git a/src/Spectre.Console.Analyzer/Analyzers/NoPromptsDuringLiveRenderablesAnalyzer.cs b/src/Spectre.Console.Analyzer/Analyzers/NoPromptsDuringLiveRenderablesAnalyzer.cs index a4ab3de11..749716ead 100644 --- a/src/Spectre.Console.Analyzer/Analyzers/NoPromptsDuringLiveRenderablesAnalyzer.cs +++ b/src/Spectre.Console.Analyzer/Analyzers/NoPromptsDuringLiveRenderablesAnalyzer.cs @@ -17,6 +17,14 @@ public class NoPromptsDuringLiveRenderablesAnalyzer : SpectreAnalyzer /// protected override void AnalyzeCompilation(CompilationStartAnalysisContext compilationStartContext) { + var ansiConsoleType = compilationStartContext.Compilation.GetTypeByMetadataName("Spectre.Console.AnsiConsole"); + var ansiConsoleExtensionsType = compilationStartContext.Compilation.GetTypeByMetadataName("Spectre.Console.AnsiConsoleExtensions"); + + if (ansiConsoleType is null && ansiConsoleExtensionsType is null) + { + return; + } + compilationStartContext.RegisterOperationAction( context => { @@ -31,9 +39,6 @@ protected override void AnalyzeCompilation(CompilationStartAnalysisContext compi return; } - var ansiConsoleType = context.Compilation.GetTypeByMetadataName("Spectre.Console.AnsiConsole"); - var ansiConsoleExtensionsType = context.Compilation.GetTypeByMetadataName("Spectre.Console.AnsiConsoleExtensions"); - if (!SymbolEqualityComparer.Default.Equals(methodSymbol.ContainingType, ansiConsoleType) && !SymbolEqualityComparer.Default.Equals(methodSymbol.ContainingType, ansiConsoleExtensionsType)) { diff --git a/src/Spectre.Console.Analyzer/Analyzers/UseSpectreInsteadOfSystemConsoleAnalyzer.cs b/src/Spectre.Console.Analyzer/Analyzers/UseSpectreInsteadOfSystemConsoleAnalyzer.cs index 3836a3213..9f0273932 100644 --- a/src/Spectre.Console.Analyzer/Analyzers/UseSpectreInsteadOfSystemConsoleAnalyzer.cs +++ b/src/Spectre.Console.Analyzer/Analyzers/UseSpectreInsteadOfSystemConsoleAnalyzer.cs @@ -18,6 +18,13 @@ public class UseSpectreInsteadOfSystemConsoleAnalyzer : SpectreAnalyzer /// protected override void AnalyzeCompilation(CompilationStartAnalysisContext compilationStartContext) { + var systemConsoleType = compilationStartContext.Compilation.GetTypeByMetadataName("System.Console"); + var spectreConsoleType = compilationStartContext.Compilation.GetTypeByMetadataName("Spectre.Console.AnsiConsole"); + if (systemConsoleType == null || spectreConsoleType == null) + { + return; + } + compilationStartContext.RegisterOperationAction( context => { @@ -31,8 +38,6 @@ protected override void AnalyzeCompilation(CompilationStartAnalysisContext compi return; } - var systemConsoleType = context.Compilation.GetTypeByMetadataName("System.Console"); - if (!SymbolEqualityComparer.Default.Equals(invocationOperation.TargetMethod.ContainingType, systemConsoleType)) { return;