From 831e5cca5ae85e28a71f53e9e27d4249eeaf5fe0 Mon Sep 17 00:00:00 2001 From: Bela VanderVoort Date: Sun, 12 May 2024 12:11:47 -0500 Subject: [PATCH] better walkers --- Src/SyntaxFinder/ResultWriter.cs | 5 +++ Src/SyntaxFinder/SyntaxFinderWalker.cs | 12 ++---- .../{ => Walkers}/ExampleWalker.cs | 31 +--------------- .../{ => Walkers}/ModifiersWalker.cs | 3 +- .../NestedConditionalExpressionsWalker.cs | 37 +++++++++++++++++++ .../{ => Walkers}/ObjectInitializerWalker.cs | 2 +- .../{ => Walkers}/SpreadWalker.cs | 4 +- 7 files changed, 50 insertions(+), 44 deletions(-) rename Src/SyntaxFinder/{ => Walkers}/ExampleWalker.cs (76%) rename Src/SyntaxFinder/{ => Walkers}/ModifiersWalker.cs (88%) create mode 100644 Src/SyntaxFinder/Walkers/NestedConditionalExpressionsWalker.cs rename Src/SyntaxFinder/{ => Walkers}/ObjectInitializerWalker.cs (97%) rename Src/SyntaxFinder/{ => Walkers}/SpreadWalker.cs (91%) diff --git a/Src/SyntaxFinder/ResultWriter.cs b/Src/SyntaxFinder/ResultWriter.cs index e41475041..84d4322a8 100644 --- a/Src/SyntaxFinder/ResultWriter.cs +++ b/Src/SyntaxFinder/ResultWriter.cs @@ -4,6 +4,11 @@ public static class ResultWriter { public static void WriteMatching(int total, int matching) { + if (total == 0) + { + return; + } + if (matching > total) { Console.WriteLine("Matching was > than Total, so you did something wrong."); diff --git a/Src/SyntaxFinder/SyntaxFinderWalker.cs b/Src/SyntaxFinder/SyntaxFinderWalker.cs index cda5b101c..79bd461a1 100644 --- a/Src/SyntaxFinder/SyntaxFinderWalker.cs +++ b/Src/SyntaxFinder/SyntaxFinderWalker.cs @@ -3,17 +3,11 @@ namespace SyntaxFinder; using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.CSharp; -public abstract class SyntaxFinderWalker : CSharpSyntaxWalker +public abstract class SyntaxFinderWalker(string file) : CSharpSyntaxWalker { private bool wroteFile; private readonly int maxCodeWrites = 250; - private static int codeWrites = 0; - private readonly string file; - - protected SyntaxFinderWalker(string file) - { - this.file = file; - } + private static int codeWrites; protected void WriteCode(SyntaxNode syntaxNode) { @@ -34,7 +28,7 @@ protected void WriteFilePath(bool onlyIfWritingCode = false) if (!this.wroteFile) { - Console.WriteLine(this.file); + Console.WriteLine(file); this.wroteFile = true; } } diff --git a/Src/SyntaxFinder/ExampleWalker.cs b/Src/SyntaxFinder/Walkers/ExampleWalker.cs similarity index 76% rename from Src/SyntaxFinder/ExampleWalker.cs rename to Src/SyntaxFinder/Walkers/ExampleWalker.cs index ca9606eb2..43424381f 100644 --- a/Src/SyntaxFinder/ExampleWalker.cs +++ b/Src/SyntaxFinder/Walkers/ExampleWalker.cs @@ -1,24 +1,15 @@ -namespace SyntaxFinder; +namespace SyntaxFinder.Walkers; using System.Collections.Concurrent; using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.CSharp; using Microsoft.CodeAnalysis.CSharp.Syntax; -public class ExampleWalker : CSharpSyntaxWalker +public class ExampleWalker(string file) : SyntaxFinderWalker(file) { public static readonly ConcurrentDictionary> MembersInType = new(); public static int Total; public static int Matching; - private readonly string file; - private bool wroteFile; - private readonly int maxCodeWrites = 250; - private int codeWrites = 0; - - public ExampleWalker(string file) - { - this.file = file; - } public override void VisitCompilationUnit(CompilationUnitSyntax node) { @@ -70,24 +61,6 @@ node.Parent is TypeDeclarationSyntax typeDeclarationSyntax base.VisitMethodDeclaration(node); } - private void WriteCode(SyntaxNode syntaxNode) - { - if (this.codeWrites < this.maxCodeWrites) - { - Interlocked.Increment(ref this.codeWrites); - Console.WriteLine(syntaxNode.SyntaxTree.GetText().ToString(syntaxNode.Span)); - } - } - - private void WriteFilePath() - { - if (!this.wroteFile) - { - Console.WriteLine(this.file); - this.wroteFile = true; - } - } - private static bool IsMultiline(SyntaxNode syntaxNode) { var lineSpan = syntaxNode.SyntaxTree.GetLineSpan(syntaxNode.Span); diff --git a/Src/SyntaxFinder/ModifiersWalker.cs b/Src/SyntaxFinder/Walkers/ModifiersWalker.cs similarity index 88% rename from Src/SyntaxFinder/ModifiersWalker.cs rename to Src/SyntaxFinder/Walkers/ModifiersWalker.cs index 8c0dfd368..868184db4 100644 --- a/Src/SyntaxFinder/ModifiersWalker.cs +++ b/Src/SyntaxFinder/Walkers/ModifiersWalker.cs @@ -1,6 +1,5 @@ -namespace SyntaxFinder; +namespace SyntaxFinder.Walkers; -using System.Collections.Concurrent; using Microsoft.CodeAnalysis.CSharp.Syntax; public class ModifiersWalker : SyntaxFinderWalker diff --git a/Src/SyntaxFinder/Walkers/NestedConditionalExpressionsWalker.cs b/Src/SyntaxFinder/Walkers/NestedConditionalExpressionsWalker.cs new file mode 100644 index 000000000..13ccd77cc --- /dev/null +++ b/Src/SyntaxFinder/Walkers/NestedConditionalExpressionsWalker.cs @@ -0,0 +1,37 @@ +namespace SyntaxFinder.Walkers; + +using System.Collections.Concurrent; +using Microsoft.CodeAnalysis; +using Microsoft.CodeAnalysis.CSharp; +using Microsoft.CodeAnalysis.CSharp.Syntax; + +public class NestedConditionalExpressionsWalker(string file) : SyntaxFinderWalker(file) +{ + public static readonly ConcurrentDictionary> MembersInType = new(); + public static int Total; + public static int Matching; + + public override void VisitConditionalExpression(ConditionalExpressionSyntax node) + { + if (node.WhenTrue is ConditionalExpressionSyntax) + { + this.WriteFilePath(); + // this.WriteCode(node.Parent!); + Interlocked.Increment(ref Total); + } + } + + public static void WriteResult() + { + foreach (var entry in MembersInType) + { + Console.WriteLine(entry.Key); + foreach (var member in entry.Value.OrderBy(o => o)) + { + Console.WriteLine(" " + member); + } + } + + ResultWriter.WriteMatching(Total, Matching); + } +} diff --git a/Src/SyntaxFinder/ObjectInitializerWalker.cs b/Src/SyntaxFinder/Walkers/ObjectInitializerWalker.cs similarity index 97% rename from Src/SyntaxFinder/ObjectInitializerWalker.cs rename to Src/SyntaxFinder/Walkers/ObjectInitializerWalker.cs index e48216ffe..903540254 100644 --- a/Src/SyntaxFinder/ObjectInitializerWalker.cs +++ b/Src/SyntaxFinder/Walkers/ObjectInitializerWalker.cs @@ -1,4 +1,4 @@ -namespace SyntaxFinder; +namespace SyntaxFinder.Walkers; using Microsoft.CodeAnalysis.CSharp; using Microsoft.CodeAnalysis.CSharp.Syntax; diff --git a/Src/SyntaxFinder/SpreadWalker.cs b/Src/SyntaxFinder/Walkers/SpreadWalker.cs similarity index 91% rename from Src/SyntaxFinder/SpreadWalker.cs rename to Src/SyntaxFinder/Walkers/SpreadWalker.cs index 2758ce459..ba5ae061f 100644 --- a/Src/SyntaxFinder/SpreadWalker.cs +++ b/Src/SyntaxFinder/Walkers/SpreadWalker.cs @@ -1,7 +1,5 @@ -namespace SyntaxFinder; +namespace SyntaxFinder.Walkers; -using System.Collections.Concurrent; -using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.CSharp; using Microsoft.CodeAnalysis.CSharp.Syntax;