mirror of
https://github.com/nsnail/spectre.console.git
synced 2025-08-04 04:47:59 +08:00
Adding analyzers for common mistakes with live renderables
This commit is contained in:

committed by
Patrik Svensson

parent
4f293d887d
commit
bdcc01ea68
@ -0,0 +1,78 @@
|
||||
using System.Collections.Immutable;
|
||||
using System.Composition;
|
||||
using System.Linq;
|
||||
using Microsoft.CodeAnalysis;
|
||||
using Microsoft.CodeAnalysis.CSharp;
|
||||
using Microsoft.CodeAnalysis.CSharp.Syntax;
|
||||
using Microsoft.CodeAnalysis.Diagnostics;
|
||||
using Microsoft.CodeAnalysis.Operations;
|
||||
|
||||
namespace Spectre.Console.Analyzer
|
||||
{
|
||||
/// <summary>
|
||||
/// Analyzer to detect calls to live renderables within a live renderable context.
|
||||
/// </summary>
|
||||
[DiagnosticAnalyzer(LanguageNames.CSharp)]
|
||||
[Shared]
|
||||
public class NoConcurrentLiveRenderablesAnalyzer : BaseAnalyzer
|
||||
{
|
||||
private static readonly DiagnosticDescriptor _diagnosticDescriptor =
|
||||
Descriptors.S1020_AvoidConcurrentCallsToMultipleLiveRenderables;
|
||||
|
||||
/// <inheritdoc />
|
||||
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics =>
|
||||
ImmutableArray.Create(_diagnosticDescriptor);
|
||||
|
||||
/// <inheritdoc />
|
||||
protected override void AnalyzeCompilation(CompilationStartAnalysisContext compilationStartContext)
|
||||
{
|
||||
compilationStartContext.RegisterOperationAction(
|
||||
context =>
|
||||
{
|
||||
var invocationOperation = (IInvocationOperation)context.Operation;
|
||||
var methodSymbol = invocationOperation.TargetMethod;
|
||||
|
||||
const string StartMethod = "Start";
|
||||
if (methodSymbol.Name != StartMethod)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var liveTypes = Constants.LiveRenderables
|
||||
.Select(i => context.Compilation.GetTypeByMetadataName(i))
|
||||
.ToImmutableArray();
|
||||
|
||||
if (liveTypes.All(i => !Equals(i, methodSymbol.ContainingType)))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var model = context.Compilation.GetSemanticModel(context.Operation.Syntax.SyntaxTree);
|
||||
var parentInvocations = invocationOperation
|
||||
.Syntax.Ancestors()
|
||||
.OfType<InvocationExpressionSyntax>()
|
||||
.Select(i => model.GetOperation(i))
|
||||
.OfType<IInvocationOperation>()
|
||||
.ToList();
|
||||
|
||||
if (parentInvocations.All(parent =>
|
||||
parent.TargetMethod.Name != StartMethod || !liveTypes.Contains(parent.TargetMethod.ContainingType)))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var displayString = SymbolDisplay.ToDisplayString(
|
||||
methodSymbol,
|
||||
SymbolDisplayFormat.CSharpShortErrorMessageFormat
|
||||
.WithParameterOptions(SymbolDisplayParameterOptions.None)
|
||||
.WithGenericsOptions(SymbolDisplayGenericsOptions.None));
|
||||
|
||||
context.ReportDiagnostic(
|
||||
Diagnostic.Create(
|
||||
_diagnosticDescriptor,
|
||||
invocationOperation.Syntax.GetLocation(),
|
||||
displayString));
|
||||
}, OperationKind.Invocation);
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,84 @@
|
||||
using System.Collections.Immutable;
|
||||
using System.Composition;
|
||||
using System.Linq;
|
||||
using Microsoft.CodeAnalysis;
|
||||
using Microsoft.CodeAnalysis.CSharp;
|
||||
using Microsoft.CodeAnalysis.CSharp.Syntax;
|
||||
using Microsoft.CodeAnalysis.Diagnostics;
|
||||
using Microsoft.CodeAnalysis.Operations;
|
||||
|
||||
namespace Spectre.Console.Analyzer
|
||||
{
|
||||
/// <summary>
|
||||
/// Analyzer to detect calls to live renderables within a live renderable context.
|
||||
/// </summary>
|
||||
[DiagnosticAnalyzer(LanguageNames.CSharp)]
|
||||
[Shared]
|
||||
public class NoPromptsDuringLiveRenderablesAnalyzer : BaseAnalyzer
|
||||
{
|
||||
private static readonly DiagnosticDescriptor _diagnosticDescriptor =
|
||||
Descriptors.S1021_AvoidPromptCallsDuringLiveRenderables;
|
||||
|
||||
/// <inheritdoc />
|
||||
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics =>
|
||||
ImmutableArray.Create(_diagnosticDescriptor);
|
||||
|
||||
/// <inheritdoc />
|
||||
protected override void AnalyzeCompilation(CompilationStartAnalysisContext compilationStartContext)
|
||||
{
|
||||
compilationStartContext.RegisterOperationAction(
|
||||
context =>
|
||||
{
|
||||
// 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;
|
||||
var methodSymbol = invocationOperation.TargetMethod;
|
||||
|
||||
var promptMethods = ImmutableArray.Create("Ask", "Confirm", "Prompt");
|
||||
if (!promptMethods.Contains(methodSymbol.Name))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var ansiConsoleType = context.Compilation.GetTypeByMetadataName("Spectre.Console.AnsiConsole");
|
||||
var ansiConsoleExtensionsType = context.Compilation.GetTypeByMetadataName("Spectre.Console.AnsiConsoleExtensions");
|
||||
|
||||
if (!Equals(methodSymbol.ContainingType, ansiConsoleType) && !Equals(methodSymbol.ContainingType, ansiConsoleExtensionsType))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var model = context.Compilation.GetSemanticModel(context.Operation.Syntax.SyntaxTree);
|
||||
var parentInvocations = invocationOperation
|
||||
.Syntax.Ancestors()
|
||||
.OfType<InvocationExpressionSyntax>()
|
||||
.Select(i => model.GetOperation(i))
|
||||
.OfType<IInvocationOperation>()
|
||||
.ToList();
|
||||
|
||||
var liveTypes = Constants.LiveRenderables
|
||||
.Select(i => context.Compilation.GetTypeByMetadataName(i))
|
||||
.ToImmutableArray();
|
||||
|
||||
if (parentInvocations.All(parent =>
|
||||
parent.TargetMethod.Name != "Start" ||
|
||||
!liveTypes.Contains(parent.TargetMethod.ContainingType)))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var displayString = SymbolDisplay.ToDisplayString(
|
||||
methodSymbol,
|
||||
SymbolDisplayFormat.CSharpShortErrorMessageFormat
|
||||
.WithParameterOptions(SymbolDisplayParameterOptions.None)
|
||||
.WithGenericsOptions(SymbolDisplayGenericsOptions.None));
|
||||
|
||||
context.ReportDiagnostic(
|
||||
Diagnostic.Create(
|
||||
_diagnosticDescriptor,
|
||||
invocationOperation.Syntax.GetLocation(),
|
||||
displayString));
|
||||
}, OperationKind.Invocation);
|
||||
}
|
||||
}
|
||||
}
|
@ -1,5 +1,4 @@
|
||||
using System.Collections.Immutable;
|
||||
using System.Linq;
|
||||
using Microsoft.CodeAnalysis;
|
||||
using Microsoft.CodeAnalysis.CSharp;
|
||||
using Microsoft.CodeAnalysis.Diagnostics;
|
||||
@ -27,37 +26,38 @@ namespace Spectre.Console.Analyzer
|
||||
{
|
||||
compilationStartContext.RegisterOperationAction(
|
||||
context =>
|
||||
{
|
||||
// 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;
|
||||
var systemConsoleType = context.Compilation.GetTypeByMetadataName("System.Console");
|
||||
|
||||
if (!Equals(invocationOperation.TargetMethod.ContainingType, systemConsoleType))
|
||||
{
|
||||
return;
|
||||
}
|
||||
// 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;
|
||||
|
||||
var methodName = System.Array.Find(_methods, i => i.Equals(invocationOperation.TargetMethod.Name));
|
||||
if (methodName == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
var methodName = System.Array.Find(_methods, i => i.Equals(invocationOperation.TargetMethod.Name));
|
||||
if (methodName == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var methodSymbol = invocationOperation.TargetMethod;
|
||||
var systemConsoleType = context.Compilation.GetTypeByMetadataName("System.Console");
|
||||
|
||||
var displayString = SymbolDisplay.ToDisplayString(
|
||||
methodSymbol,
|
||||
SymbolDisplayFormat.CSharpShortErrorMessageFormat
|
||||
.WithParameterOptions(SymbolDisplayParameterOptions.None)
|
||||
.WithGenericsOptions(SymbolDisplayGenericsOptions.None));
|
||||
if (!Equals(invocationOperation.TargetMethod.ContainingType, systemConsoleType))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
context.ReportDiagnostic(
|
||||
Diagnostic.Create(
|
||||
_diagnosticDescriptor,
|
||||
invocationOperation.Syntax.GetLocation(),
|
||||
displayString));
|
||||
}, OperationKind.Invocation);
|
||||
var methodSymbol = invocationOperation.TargetMethod;
|
||||
|
||||
var displayString = SymbolDisplay.ToDisplayString(
|
||||
methodSymbol,
|
||||
SymbolDisplayFormat.CSharpShortErrorMessageFormat
|
||||
.WithParameterOptions(SymbolDisplayParameterOptions.None)
|
||||
.WithGenericsOptions(SymbolDisplayGenericsOptions.None));
|
||||
|
||||
context.ReportDiagnostic(
|
||||
Diagnostic.Create(
|
||||
_diagnosticDescriptor,
|
||||
invocationOperation.Syntax.GetLocation(),
|
||||
displayString));
|
||||
}, OperationKind.Invocation);
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user