mirror of
				https://github.com/Tyrrrz/CliFx.git
				synced 2025-10-25 15:19:17 +00:00 
			
		
		
		
	
		
			
				
	
	
		
			66 lines
		
	
	
		
			2.0 KiB
		
	
	
	
		
			C#
		
	
	
	
	
	
			
		
		
	
	
			66 lines
		
	
	
		
			2.0 KiB
		
	
	
	
		
			C#
		
	
	
	
	
	
| using System.Linq;
 | |
| using CliFx.Analyzers.ObjectModel;
 | |
| using CliFx.Analyzers.Utils.Extensions;
 | |
| using Microsoft.CodeAnalysis;
 | |
| using Microsoft.CodeAnalysis.CSharp.Syntax;
 | |
| using Microsoft.CodeAnalysis.Diagnostics;
 | |
| 
 | |
| namespace CliFx.Analyzers;
 | |
| 
 | |
| [DiagnosticAnalyzer(LanguageNames.CSharp)]
 | |
| public class ParameterMustBeLastIfNonScalarAnalyzer : AnalyzerBase
 | |
| {
 | |
|     public ParameterMustBeLastIfNonScalarAnalyzer()
 | |
|         : base(
 | |
|             "Parameters of non-scalar types must be the last in order",
 | |
|             "This parameter has a non-scalar type so it must be the last in order (its order must be highest within the command). "
 | |
|                 + "Property bound to another non-scalar parameter: `{0}`."
 | |
|         ) { }
 | |
| 
 | |
|     private void Analyze(
 | |
|         SyntaxNodeAnalysisContext context,
 | |
|         PropertyDeclarationSyntax propertyDeclaration,
 | |
|         IPropertySymbol property
 | |
|     )
 | |
|     {
 | |
|         if (property.ContainingType is null)
 | |
|             return;
 | |
| 
 | |
|         var parameter = CommandParameterSymbol.TryResolve(property);
 | |
|         if (parameter is null)
 | |
|             return;
 | |
| 
 | |
|         if (parameter.IsScalar())
 | |
|             return;
 | |
| 
 | |
|         var otherProperties = property.ContainingType
 | |
|             .GetMembers()
 | |
|             .OfType<IPropertySymbol>()
 | |
|             .Where(m => !m.Equals(property))
 | |
|             .ToArray();
 | |
| 
 | |
|         foreach (var otherProperty in otherProperties)
 | |
|         {
 | |
|             var otherParameter = CommandParameterSymbol.TryResolve(otherProperty);
 | |
|             if (otherParameter is null)
 | |
|                 continue;
 | |
| 
 | |
|             if (otherParameter.Order > parameter.Order)
 | |
|             {
 | |
|                 context.ReportDiagnostic(
 | |
|                     CreateDiagnostic(
 | |
|                         propertyDeclaration.Identifier.GetLocation(),
 | |
|                         otherProperty.Name
 | |
|                     )
 | |
|                 );
 | |
|             }
 | |
|         }
 | |
|     }
 | |
| 
 | |
|     public override void Initialize(AnalysisContext context)
 | |
|     {
 | |
|         base.Initialize(context);
 | |
|         context.HandlePropertyDeclaration(Analyze);
 | |
|     }
 | |
| }
 |