1- namespace CommunityToolkit . Maui . Analyzers ;
2-
3- using System . Collections . Immutable ;
1+ using System . Collections . Immutable ;
42using Microsoft . CodeAnalysis ;
53using Microsoft . CodeAnalysis . CSharp ;
64using Microsoft . CodeAnalysis . CSharp . Syntax ;
75using Microsoft . CodeAnalysis . Diagnostics ;
86
7+ namespace CommunityToolkit . Maui . Analyzers ;
8+
99[ DiagnosticAnalyzer ( LanguageNames . CSharp ) ]
1010public class MaximumRatingRangeAnalyzer : DiagnosticAnalyzer
1111{
@@ -38,28 +38,16 @@ public override void Initialize(AnalysisContext context)
3838
3939 static void AnalyzePropertyAssignment ( SyntaxNodeAnalysisContext context )
4040 {
41- AssignmentExpressionSyntax assignmentExpression = ( AssignmentExpressionSyntax ) context . Node ;
42- if ( assignmentExpression . Left is IdentifierNameSyntax leftIdentifier && leftIdentifier . Identifier . Text == "MaximumRating" )
41+ if ( context . Node is AssignmentExpressionSyntax { Left : IdentifierNameSyntax { Identifier . Text : "MaximumRating" } leftIdentifier , Right : LiteralExpressionSyntax { Token . Value : int value } } assignmentExpression )
4342 {
44- SemanticModel semanticModel = context . SemanticModel ;
45- IPropertySymbol ? propertySymbol = semanticModel . GetSymbolInfo ( leftIdentifier ) . Symbol as IPropertySymbol ;
46- if ( propertySymbol ? . ContainingType . Name == "RatingView" && assignmentExpression . Right is LiteralExpressionSyntax literal && literal . IsKind ( SyntaxKind . NumericLiteralExpression ) && literal . Token . Value is not null )
47- {
48- int value = ( int ) literal . Token . Value ;
43+ var semanticModel = context . SemanticModel ;
44+ var propertySymbol = semanticModel . GetSymbolInfo ( leftIdentifier ) . Symbol as IPropertySymbol ;
4945
50- // Validate the value is within the range
51- if ( value is < minValue or > maxValue )
52- {
53- Diagnostic diagnostic = Diagnostic . Create (
54- rule ,
55- assignmentExpression . GetLocation ( ) ,
56- minValue ,
57- maxValue
58- ) ;
59-
60- context . ReportDiagnostic ( diagnostic ) ;
61- }
46+ if ( propertySymbol ? . ContainingType . Name == "RatingView" && ( value < minValue || value > maxValue ) )
47+ {
48+ var diagnostic = Diagnostic . Create ( rule , assignmentExpression . GetLocation ( ) , minValue , maxValue ) ;
49+ context . ReportDiagnostic ( diagnostic ) ;
6250 }
6351 }
6452 }
65- }
53+ }
0 commit comments