- 
                Notifications
    
You must be signed in to change notification settings  - Fork 4
 
Add OperationWalker support and fix implicit object creation expressions #52
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
          
     Open
      
      
            Copilot
  wants to merge
  6
  commits into
  main
  
    
      
        
          
  
    
      Choose a base branch
      
     
    
      
        
      
      
        
          
          
        
        
          
            
              
              
              
  
           
        
        
          
            
              
              
           
        
       
     
  
        
          
            
          
            
          
        
       
    
      
from
copilot/fix-6
  
      
      
   
  
    
  
  
  
 
  
      
    base: main
Could not load branches
            
              
  
    Branch not found: {{ refName }}
  
            
                
      Loading
              
            Could not load tags
            
            
              Nothing to show
            
              
  
            
                
      Loading
              
            Are you sure you want to change the base?
            Some commits from the old base branch may be removed from the timeline,
            and old review comments may become outdated.
          
          
  
     Open
                    Changes from all commits
      Commits
    
    
            Show all changes
          
          
            6 commits
          
        
        Select commit
          Hold shift + click to select a range
      
      1eca41c
              
                Initial plan
              
              
                Copilot 110c8b9
              
                Add support for implicit object creation expressions (new())
              
              
                Copilot 512567a
              
                Update InvocationsAnalyzer to use OperationWalker - work in progress
              
              
                Copilot 247e5cc
              
                Complete OperationWalker implementation with backward compatibility
              
              
                Copilot e794a17
              
                Add comprehensive test coverage for OperationBasedInvocationsAnalyzer…
              
              
                Copilot cc092e0
              
                Final test coverage improvements - all tests passing (296/296)
              
              
                Copilot File filter
Filter by extension
Conversations
          Failed to load comments.   
        
        
          
      Loading
        
  Jump to
        
          Jump to file
        
      
      
          Failed to load files.   
        
        
          
      Loading
        
  Diff view
Diff view
There are no files selected for viewing
  
    
      This file contains hidden or 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
    
  
  
    
              
        
          
          
            135 changes: 135 additions & 0 deletions
          
          135 
        
  src/DendroDocs.Tool/Analyzers/OperationBasedInvocationsAnalyzer.cs
  
  
      
      
   
        
      
      
    
  
    
      This file contains hidden or 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 | 
|---|---|---|
| @@ -0,0 +1,135 @@ | ||
| using Microsoft.CodeAnalysis.Operations; | ||
| 
     | 
||
| namespace DendroDocs.Tool; | ||
| 
     | 
||
| /// <summary> | ||
| /// OperationWalker-based analyzer for method invocations, providing better support for VB.NET, | ||
| /// constant values, and implicit object creation expressions. | ||
| /// | ||
| /// Benefits over CSharpSyntaxWalker: | ||
| /// - Language-agnostic: Works with both C# and VB.NET | ||
| /// - Better type information: Access to resolved types and constant values | ||
| /// - Unified object creation: Handles both explicit and implicit object creation seamlessly | ||
| /// - Enhanced semantic analysis: Works at the operation level rather than syntax level | ||
| /// | ||
| /// Example usage for VB.NET support: | ||
| /// Instead of needing separate VB-specific syntax walkers, this analyzer can process | ||
| /// VB.NET code through IOperation, making it language-neutral. | ||
| /// </summary> | ||
| internal class OperationBasedInvocationsAnalyzer(SemanticModel semanticModel, List<Statement> statements) : OperationWalker | ||
| { | ||
| // Keep semantic model available for future enhancements | ||
| private readonly SemanticModel _semanticModel = semanticModel; | ||
| public override void VisitObjectCreation(IObjectCreationOperation operation) | ||
| { | ||
| string containingType = operation.Type?.ToDisplayString() ?? string.Empty; | ||
| string typeName = operation.Type?.Name ?? string.Empty; | ||
| 
     | 
||
| var invocation = new InvocationDescription(containingType, typeName); | ||
| statements.Add(invocation); | ||
| 
     | 
||
| foreach (var argument in operation.Arguments) | ||
| { | ||
| var value = GetConstantValueOrDefault(argument.Value); | ||
| var argumentDescription = new ArgumentDescription(argument.Value.Type?.ToDisplayString() ?? string.Empty, value); | ||
| invocation.Arguments.Add(argumentDescription); | ||
| } | ||
| 
     | 
||
| if (operation.Initializer != null) | ||
| { | ||
| foreach (var initializer in operation.Initializer.Initializers) | ||
| { | ||
| var value = initializer switch | ||
| { | ||
| IAssignmentOperation assignment => assignment.Value.Syntax.ToString(), | ||
| _ => initializer.Syntax.ToString() | ||
| }; | ||
| 
     | 
||
| var argumentDescription = new ArgumentDescription(initializer.Type?.ToDisplayString() ?? string.Empty, value); | ||
| invocation.Arguments.Add(argumentDescription); | ||
| } | ||
| } | ||
| 
     | 
||
| base.VisitObjectCreation(operation); | ||
| } | ||
| 
     | 
||
| public override void VisitInvocation(IInvocationOperation operation) | ||
| { | ||
| // Check for nameof expression | ||
| if (operation.TargetMethod.Name == "nameof" && operation.Arguments.Length == 1) | ||
| { | ||
| // nameof is compiler sugar, and is actually a method we are not interested in | ||
| return; | ||
| } | ||
| 
     | 
||
| var containingType = operation.TargetMethod.ContainingType?.ToDisplayString() ?? string.Empty; | ||
| var methodName = operation.TargetMethod.Name; | ||
| 
     | 
||
| var invocation = new InvocationDescription(containingType, methodName); | ||
| statements.Add(invocation); | ||
| 
     | 
||
| foreach (var argument in operation.Arguments) | ||
| { | ||
| var value = GetConstantValueOrDefault(argument.Value); | ||
| var argumentDescription = new ArgumentDescription(argument.Value.Type?.ToDisplayString() ?? string.Empty, value); | ||
| invocation.Arguments.Add(argumentDescription); | ||
| } | ||
| 
     | 
||
| base.VisitInvocation(operation); | ||
| } | ||
| 
     | 
||
| public override void VisitReturn(IReturnOperation operation) | ||
| { | ||
| var value = operation.ReturnedValue != null ? GetConstantValueOrDefault(operation.ReturnedValue) : string.Empty; | ||
| var returnDescription = new ReturnDescription(value); | ||
| statements.Add(returnDescription); | ||
| 
     | 
||
| base.VisitReturn(operation); | ||
| } | ||
| 
     | 
||
| public override void VisitSimpleAssignment(ISimpleAssignmentOperation operation) | ||
| { | ||
| var target = operation.Target.Syntax.ToString(); | ||
| var value = operation.Value.Syntax.ToString(); | ||
| 
     | 
||
| var assignmentDescription = new AssignmentDescription(target, "=", value); | ||
| statements.Add(assignmentDescription); | ||
| 
     | 
||
| base.VisitSimpleAssignment(operation); | ||
| } | ||
| 
     | 
||
| public override void VisitCompoundAssignment(ICompoundAssignmentOperation operation) | ||
| { | ||
| var target = operation.Target.Syntax.ToString(); | ||
| var value = operation.Value.Syntax.ToString(); | ||
| var operatorToken = operation.OperatorKind switch | ||
| { | ||
| BinaryOperatorKind.Add => "+=", | ||
| BinaryOperatorKind.Subtract => "-=", | ||
| BinaryOperatorKind.Multiply => "*=", | ||
| BinaryOperatorKind.Divide => "/=", | ||
| BinaryOperatorKind.Remainder => "%=", | ||
| BinaryOperatorKind.And => "&=", | ||
| BinaryOperatorKind.Or => "|=", | ||
| BinaryOperatorKind.ExclusiveOr => "^=", | ||
| BinaryOperatorKind.LeftShift => "<<=", | ||
| BinaryOperatorKind.RightShift => ">>=", | ||
| _ => "=" | ||
| }; | ||
| 
     | 
||
| var assignmentDescription = new AssignmentDescription(target, operatorToken, value); | ||
| statements.Add(assignmentDescription); | ||
| 
     | 
||
| base.VisitCompoundAssignment(operation); | ||
| } | ||
| 
     | 
||
| private static string GetConstantValueOrDefault(IOperation operation) | ||
| { | ||
| return operation switch | ||
| { | ||
| ILiteralOperation literal => literal.ConstantValue.Value?.ToString() ?? string.Empty, | ||
| IFieldReferenceOperation field when field.Field.IsConst => field.Field.ConstantValue?.ToString() ?? string.Empty, | ||
| _ => operation.Syntax.ToString() | ||
| }; | ||
| } | ||
| } | 
      
      Oops, something went wrong.
        
    
  
  Add this suggestion to a batch that can be applied as a single commit.
  This suggestion is invalid because no changes were made to the code.
  Suggestions cannot be applied while the pull request is closed.
  Suggestions cannot be applied while viewing a subset of changes.
  Only one suggestion per line can be applied in a batch.
  Add this suggestion to a batch that can be applied as a single commit.
  Applying suggestions on deleted lines is not supported.
  You must change the existing code in this line in order to create a valid suggestion.
  Outdated suggestions cannot be applied.
  This suggestion has been applied or marked resolved.
  Suggestions cannot be applied from pending reviews.
  Suggestions cannot be applied on multi-line comments.
  Suggestions cannot be applied while the pull request is queued to merge.
  Suggestion cannot be applied right now. Please check back later.
  
    
  
    
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[nitpick] To improve performance and readability, consider storing the result of semanticModel.GetTypeDisplayString(argument.Expression) in a local variable rather than calling it inline multiple times.