-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Removed cached delegates (the compiler does it since C# 11)
- Loading branch information
Showing
56 changed files
with
210 additions
and
505 deletions.
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
100 changes: 94 additions & 6 deletions
100
...CSharpGuidelinesAnalyzer/CSharpGuidelinesAnalyzer/Extensions/AnalysisContextExtensions.cs
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,40 +1,128 @@ | ||
using System; | ||
using System.Collections.Immutable; | ||
using System.Linq; | ||
using JetBrains.Annotations; | ||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.CSharp; | ||
using Microsoft.CodeAnalysis.Diagnostics; | ||
|
||
namespace CSharpGuidelinesAnalyzer.Extensions; | ||
|
||
/// <summary /> | ||
/// <summary> | ||
/// Replaces the built-in registration methods to not run on broken code. | ||
/// </summary> | ||
internal static class AnalysisContextExtensions | ||
{ | ||
public static void SkipInvalid(this OperationAnalysisContext context, [NotNull] Action<OperationAnalysisContext> action) | ||
public static void SafeRegisterOperationAction([NotNull] this AnalysisContext analysisContext, [NotNull] Action<OperationAnalysisContext> action, | ||
[NotNull] params OperationKind[] operationKinds) | ||
{ | ||
analysisContext.RegisterOperationAction(context => SkipInvalid(context, action), operationKinds); | ||
} | ||
|
||
public static void SafeRegisterOperationAction([NotNull] this AnalysisContext analysisContext, [NotNull] Action<OperationAnalysisContext> action, | ||
ImmutableArray<OperationKind> operationKinds) | ||
{ | ||
analysisContext.RegisterOperationAction(context => SkipInvalid(context, action), operationKinds); | ||
} | ||
|
||
public static void SafeRegisterOperationBlockAction([NotNull] this AnalysisContext analysisContext, [NotNull] Action<OperationBlockAnalysisContext> action) | ||
{ | ||
analysisContext.RegisterOperationBlockAction(context => SkipInvalid(context, action)); | ||
} | ||
|
||
public static void SafeRegisterSymbolAction([NotNull] this AnalysisContext analysisContext, [NotNull] Action<SymbolAnalysisContext> action, | ||
[NotNull] params SymbolKind[] symbolKinds) | ||
{ | ||
analysisContext.RegisterSymbolAction(context => SkipEmptyName(context, action), symbolKinds); | ||
} | ||
|
||
public static void SafeRegisterSymbolAction([NotNull] this AnalysisContext analysisContext, [NotNull] Action<SymbolAnalysisContext> action, | ||
ImmutableArray<SymbolKind> symbolKinds) | ||
{ | ||
analysisContext.RegisterSymbolAction(context => SkipEmptyName(context, action), symbolKinds); | ||
} | ||
|
||
public static void SafeRegisterSyntaxNodeAction([NotNull] this AnalysisContext analysisContext, [NotNull] Action<SymbolAnalysisContext> action, | ||
[NotNull] params SyntaxKind[] syntaxKinds) | ||
{ | ||
analysisContext.RegisterSyntaxNodeAction(context => SkipEmptyName(context, action), syntaxKinds); | ||
} | ||
|
||
public static void SafeRegisterSyntaxNodeAction([NotNull] this AnalysisContext analysisContext, [NotNull] Action<SymbolAnalysisContext> action, | ||
ImmutableArray<SyntaxKind> syntaxKinds) | ||
{ | ||
analysisContext.RegisterSyntaxNodeAction(context => SkipEmptyName(context, action), syntaxKinds); | ||
} | ||
|
||
public static void SafeRegisterOperationAction([NotNull] this CompilationStartAnalysisContext compilationStartAnalysisContext, | ||
[NotNull] Action<OperationAnalysisContext> action, [NotNull] params OperationKind[] operationKinds) | ||
{ | ||
compilationStartAnalysisContext.RegisterOperationAction(context => SkipInvalid(context, action), operationKinds); | ||
} | ||
|
||
public static void SafeRegisterOperationAction([NotNull] this CompilationStartAnalysisContext compilationStartAnalysisContext, | ||
[NotNull] Action<OperationAnalysisContext> action, ImmutableArray<OperationKind> operationKinds) | ||
{ | ||
compilationStartAnalysisContext.RegisterOperationAction(context => SkipInvalid(context, action), operationKinds); | ||
} | ||
|
||
public static void SafeRegisterOperationBlockAction([NotNull] this CompilationStartAnalysisContext compilationStartAnalysisContext, | ||
[NotNull] Action<OperationBlockAnalysisContext> action) | ||
{ | ||
compilationStartAnalysisContext.RegisterOperationBlockAction(context => SkipInvalid(context, action)); | ||
} | ||
|
||
public static void SafeRegisterSymbolAction([NotNull] this CompilationStartAnalysisContext compilationStartAnalysisContext, | ||
[NotNull] Action<SymbolAnalysisContext> action, [NotNull] params SymbolKind[] symbolKinds) | ||
{ | ||
compilationStartAnalysisContext.RegisterSymbolAction(context => SkipEmptyName(context, action), symbolKinds); | ||
} | ||
|
||
public static void SafeRegisterSymbolAction([NotNull] this CompilationStartAnalysisContext compilationStartAnalysisContext, | ||
[NotNull] Action<SymbolAnalysisContext> action, ImmutableArray<SymbolKind> symbolKinds) | ||
{ | ||
compilationStartAnalysisContext.RegisterSymbolAction(context => SkipEmptyName(context, action), symbolKinds); | ||
} | ||
|
||
public static void SafeRegisterSyntaxNodeAction([NotNull] this CompilationStartAnalysisContext compilationStartAnalysisContext, | ||
[NotNull] Action<SymbolAnalysisContext> action, [NotNull] params SyntaxKind[] syntaxKinds) | ||
{ | ||
compilationStartAnalysisContext.RegisterSyntaxNodeAction(context => SkipEmptyName(context, action), syntaxKinds); | ||
} | ||
|
||
public static void SafeRegisterSyntaxNodeAction([NotNull] this CompilationStartAnalysisContext compilationStartAnalysisContext, | ||
[NotNull] Action<SymbolAnalysisContext> action, ImmutableArray<SyntaxKind> syntaxKinds) | ||
{ | ||
compilationStartAnalysisContext.RegisterSyntaxNodeAction(context => SkipEmptyName(context, action), syntaxKinds); | ||
} | ||
|
||
private static void SkipInvalid(OperationAnalysisContext context, [NotNull] Action<OperationAnalysisContext> action) | ||
{ | ||
if (!context.Operation.HasErrors(context.Compilation, context.CancellationToken)) | ||
{ | ||
action(context); | ||
} | ||
} | ||
|
||
public static void SkipInvalid(this OperationBlockAnalysisContext context, [NotNull] Action<OperationBlockAnalysisContext> action) | ||
private static void SkipInvalid(OperationBlockAnalysisContext context, [NotNull] Action<OperationBlockAnalysisContext> action) | ||
{ | ||
if (!context.OperationBlocks.Any(block => block.HasErrors(context.Compilation, context.CancellationToken))) | ||
{ | ||
action(context); | ||
} | ||
} | ||
|
||
public static void SkipEmptyName(this SymbolAnalysisContext context, [NotNull] Action<SymbolAnalysisContext> action) | ||
private static void SkipEmptyName(SymbolAnalysisContext context, [NotNull] Action<SymbolAnalysisContext> action) | ||
{ | ||
if (!string.IsNullOrEmpty(context.Symbol.Name)) | ||
{ | ||
action(context); | ||
} | ||
} | ||
|
||
public static void SkipEmptyName(this SyntaxNodeAnalysisContext context, [NotNull] Action<SymbolAnalysisContext> action) | ||
private static void SkipEmptyName(SyntaxNodeAnalysisContext context, [NotNull] Action<SymbolAnalysisContext> action) | ||
{ | ||
SymbolAnalysisContext symbolContext = context.ToSymbolContext(); | ||
symbolContext.SkipEmptyName(_ => action(symbolContext)); | ||
SkipEmptyName(symbolContext, _ => action(symbolContext)); | ||
} | ||
} |
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
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
Oops, something went wrong.