private static void ProcessWhere( SyntaxNodeAnalysisContext context, InvocationExpressionSyntax invocation, MemberAccessExpressionSyntax memberAccess, string methodName) { if (memberAccess.Expression?.IsKind(SyntaxKind.InvocationExpression) == true) { var invocation2 = (InvocationExpressionSyntax)memberAccess.Expression; if (invocation2.ArgumentList?.Arguments.Count == 1 && invocation2.Expression?.IsKind(SyntaxKind.SimpleMemberAccessExpression) == true) { var memberAccess2 = (MemberAccessExpressionSyntax)invocation2.Expression; if (memberAccess2.Name?.Identifier.ValueText == "Where" && IsEnumerableExtensionMethod(context, invocation, methodName) && (IsEnumerableWhereMethod(context, invocation2) || IsImmutableArrayWhereMethod(context, invocation2))) { TextSpan span = TextSpan.FromBounds(invocation2.Span.End, invocation.Span.End); if (invocation .DescendantTrivia(span) .All(f => f.IsWhitespaceOrEndOfLine())) { context.ReportDiagnostic( DiagnosticDescriptors.SimplifyLinqMethodChain, memberAccess2.Name.GetLocation()); } } } } }
public static Task <Document> RefactorAsync( Document document, InvocationExpressionSyntax invocation, CancellationToken cancellationToken = default(CancellationToken)) { var memberAccess = (MemberAccessExpressionSyntax)invocation.Expression; var invocation2 = (InvocationExpressionSyntax)memberAccess.Expression; var memberAccess2 = (MemberAccessExpressionSyntax)invocation2.Expression; InvocationExpressionSyntax newNode = invocation2.WithExpression( memberAccess2.WithName(memberAccess.Name.WithTriviaFrom(memberAccess2.Name))); IEnumerable <SyntaxTrivia> trivia = invocation.DescendantTrivia(TextSpan.FromBounds(invocation2.Span.End, invocation.Span.End)); if (trivia.Any(f => !f.IsWhitespaceOrEndOfLineTrivia())) { newNode = newNode.WithTrailingTrivia(trivia.Concat(invocation.GetTrailingTrivia())); } else { newNode = newNode.WithTrailingTrivia(invocation.GetTrailingTrivia()); } return(document.ReplaceNodeAsync(invocation, newNode, cancellationToken)); }
public static Task <Document> RefactorAsync( Document document, InvocationExpressionSyntax invocation, CancellationToken cancellationToken) { var memberAccess = (MemberAccessExpressionSyntax)invocation.Expression; ExpressionSyntax expression = memberAccess.Expression; IEnumerable <SyntaxTrivia> trailing = invocation.DescendantTrivia(TextSpan.FromBounds(expression.SpanStart, invocation.Span.End)); if (trailing.All(f => f.IsWhitespaceOrEndOfLineTrivia())) { trailing = invocation.GetTrailingTrivia(); } else { trailing = trailing.Concat(invocation.GetTrailingTrivia()); } ExpressionSyntax newNode = expression .WithTrailingTrivia(trailing) .WithFormatterAnnotation(); return(document.ReplaceNodeAsync(invocation, newNode, cancellationToken)); }
public static void Analyze(SyntaxNodeAnalysisContext context, InvocationExpressionSyntax invocation, MemberAccessExpressionSyntax memberAccess) { SemanticModel semanticModel = context.SemanticModel; CancellationToken cancellationToken = context.CancellationToken; MethodInfo methodInfo; if (semanticModel.TryGetExtensionMethodInfo(invocation, out methodInfo, ExtensionMethodKind.Reduced, cancellationToken) && methodInfo.IsLinqExtensionOfIEnumerableOfTWithoutParameters("Count")) { string propertyName = GetCountOrLengthPropertyName(memberAccess.Expression, semanticModel, cancellationToken); if (propertyName != null) { TextSpan span = TextSpan.FromBounds(memberAccess.Name.Span.Start, invocation.Span.End); if (invocation .DescendantTrivia(span) .All(f => f.IsWhitespaceOrEndOfLineTrivia())) { Diagnostic diagnostic = Diagnostic.Create( DiagnosticDescriptors.UseCountOrLengthPropertyInsteadOfCountMethod, Location.Create(context.Node.SyntaxTree, span), ImmutableDictionary.CreateRange(new KeyValuePair <string, string>[] { new KeyValuePair <string, string>("PropertyName", propertyName) }), propertyName); context.ReportDiagnostic(diagnostic); } } else if (invocation.IsParentKind( SyntaxKind.EqualsExpression, SyntaxKind.GreaterThanExpression, SyntaxKind.GreaterThanOrEqualExpression, SyntaxKind.LessThanExpression, SyntaxKind.LessThanOrEqualExpression)) { var binaryExpression = (BinaryExpressionSyntax)invocation.Parent; if (IsFixableBinaryExpression(binaryExpression)) { TextSpan span = TextSpan.FromBounds(invocation.Span.End, binaryExpression.Span.End); if (binaryExpression .DescendantTrivia(span) .All(f => f.IsWhitespaceOrEndOfLineTrivia())) { context.ReportDiagnostic( DiagnosticDescriptors.UseAnyMethodInsteadOfCountMethod, binaryExpression); } } } } }
public static void Analyze(SyntaxNodeAnalysisContext context, InvocationExpressionSyntax invocation, MemberAccessExpressionSyntax memberAccess) { if (!invocation.IsParentKind(SyntaxKind.SimpleMemberAccessExpression)) { SemanticModel semanticModel = context.SemanticModel; CancellationToken cancellationToken = context.CancellationToken; if (semanticModel .GetExtensionMethodInfo(invocation, ExtensionMethodKind.Reduced, cancellationToken) .MethodInfo .IsLinqExtensionOfIEnumerableOfTWithoutParameters("Any")) { string propertyName = GetCountOrLengthPropertyName(memberAccess.Expression, semanticModel, cancellationToken); if (propertyName != null) { bool success = false; TextSpan span = TextSpan.FromBounds(memberAccess.Name.Span.Start, invocation.Span.End); if (invocation.DescendantTrivia(span).All(f => f.IsWhitespaceOrEndOfLineTrivia())) { if (invocation.IsParentKind(SyntaxKind.LogicalNotExpression)) { var logicalNot = (PrefixUnaryExpressionSyntax)invocation.Parent; if (logicalNot.OperatorToken.TrailingTrivia.All(f => f.IsWhitespaceOrEndOfLineTrivia()) && logicalNot.Operand.GetLeadingTrivia().All(f => f.IsWhitespaceOrEndOfLineTrivia())) { success = true; } } else { success = true; } } if (success) { Diagnostic diagnostic = Diagnostic.Create( DiagnosticDescriptors.UseCountOrLengthPropertyInsteadOfAnyMethod, Location.Create(context.Node.SyntaxTree, span), ImmutableDictionary.CreateRange(new KeyValuePair <string, string>[] { new KeyValuePair <string, string>("PropertyName", propertyName) }), propertyName); context.ReportDiagnostic(diagnostic); } } } } }
public static void Analyze(SyntaxNodeAnalysisContext context, InvocationExpressionSyntax invocation, MemberAccessExpressionSyntax memberAccess) { if (!invocation.IsParentKind(SyntaxKind.SimpleMemberAccessExpression)) { IMethodSymbol methodSymbol = context.SemanticModel.GetMethodSymbol(invocation, context.CancellationToken); if (methodSymbol != null && Symbol.IsEnumerableMethodWithoutParameters(methodSymbol, "Any", context.SemanticModel)) { string propertyName = GetCountOrLengthPropertyName(memberAccess.Expression, context.SemanticModel, context.CancellationToken); if (propertyName != null) { string messageArg = null; TextSpan span = TextSpan.FromBounds(memberAccess.Name.Span.Start, invocation.Span.End); if (invocation.DescendantTrivia(span).All(f => f.IsWhitespaceOrEndOfLineTrivia())) { if (invocation.IsParentKind(SyntaxKind.LogicalNotExpression)) { var logicalNot = (PrefixUnaryExpressionSyntax)invocation.Parent; if (logicalNot.OperatorToken.TrailingTrivia.All(f => f.IsWhitespaceOrEndOfLineTrivia()) && logicalNot.Operand.GetLeadingTrivia().All(f => f.IsWhitespaceOrEndOfLineTrivia())) { messageArg = $"{propertyName} == 0"; } } else { messageArg = $"{propertyName} > 0"; } } if (messageArg != null) { Diagnostic diagnostic = Diagnostic.Create( DiagnosticDescriptors.ReplaceAnyMethodWithCountOrLengthProperty, Location.Create(context.Node.SyntaxTree, span), ImmutableDictionary.CreateRange(new KeyValuePair <string, string>[] { new KeyValuePair <string, string>("PropertyName", propertyName) }), messageArg); context.ReportDiagnostic(diagnostic); } } } } }
public static void Analyze(SyntaxNodeAnalysisContext context, InvocationExpressionSyntax invocation, MemberAccessExpressionSyntax memberAccess) { IMethodSymbol methodSymbol = context.SemanticModel.GetMethodSymbol(invocation, context.CancellationToken); if (methodSymbol != null && Symbol.IsEnumerableMethodWithoutParameters(methodSymbol, "Count", context.SemanticModel)) { string propertyName = GetCountOrLengthPropertyName(memberAccess.Expression, context.SemanticModel, context.CancellationToken); if (propertyName != null) { TextSpan span = TextSpan.FromBounds(memberAccess.Name.Span.Start, invocation.Span.End); if (invocation .DescendantTrivia(span) .All(f => f.IsWhitespaceOrEndOfLineTrivia())) { Diagnostic diagnostic = Diagnostic.Create( DiagnosticDescriptors.ReplaceCountMethodWithCountOrLengthProperty, Location.Create(context.Node.SyntaxTree, span), ImmutableDictionary.CreateRange(new KeyValuePair <string, string>[] { new KeyValuePair <string, string>("PropertyName", propertyName) }), propertyName); context.ReportDiagnostic(diagnostic); } } else if (invocation.Parent?.IsKind( SyntaxKind.EqualsExpression, SyntaxKind.GreaterThanExpression, SyntaxKind.LessThanExpression) == true) { var binaryExpression = (BinaryExpressionSyntax)invocation.Parent; if (IsFixableBinaryExpression(binaryExpression)) { TextSpan span = TextSpan.FromBounds(invocation.Span.End, binaryExpression.Span.End); if (binaryExpression .DescendantTrivia(span) .All(f => f.IsWhitespaceOrEndOfLineTrivia())) { context.ReportDiagnostic( DiagnosticDescriptors.ReplaceCountMethodWithAnyMethod, binaryExpression.GetLocation()); } } } } }
private static void ProcessCount(SyntaxNodeAnalysisContext context, InvocationExpressionSyntax invocation, MemberAccessExpressionSyntax memberAccess) { if (IsEnumerableExtensionMethod(context, invocation, "Count")) { string propertyName = GetCountOrLengthPropertyName(context, memberAccess.Expression, allowImmutableArray: true); if (propertyName != null) { TextSpan span = TextSpan.FromBounds(memberAccess.Name.Span.Start, invocation.Span.End); if (invocation .DescendantTrivia(span) .All(f => f.IsWhitespaceOrEndOfLine())) { Diagnostic diagnostic = Diagnostic.Create( DiagnosticDescriptors.UseCountOrLengthPropertyInsteadOfCountMethod, Location.Create(context.Node.SyntaxTree, span), (propertyName == "Count") ? _propertiesCount : _propertiesLength, propertyName); context.ReportDiagnostic(diagnostic); } } else if (invocation.Parent?.IsAnyKind( SyntaxKind.EqualsExpression, SyntaxKind.GreaterThanExpression, SyntaxKind.LessThanExpression) == true) { var binaryExpression = (BinaryExpressionSyntax)invocation.Parent; if (IsFixableBinaryExpression(binaryExpression)) { TextSpan span = TextSpan.FromBounds(invocation.Span.End, binaryExpression.Span.End); if (binaryExpression .DescendantTrivia(span) .All(f => f.IsWhitespaceOrEndOfLine())) { context.ReportDiagnostic( DiagnosticDescriptors.UseAnyMethodInsteadOfCountMethod, binaryExpression.GetLocation()); } } } } }
private static void ProcessAny(SyntaxNodeAnalysisContext context, InvocationExpressionSyntax invocation, MemberAccessExpressionSyntax memberAccess) { if (invocation.Parent?.IsKind(SyntaxKind.SimpleMemberAccessExpression) == false && IsEnumerableExtensionMethod(context, invocation, "Any")) { string propertyName = GetCountOrLengthPropertyName(context, memberAccess.Expression, allowImmutableArray: false); if (propertyName != null) { string messageArg = null; TextSpan span = TextSpan.FromBounds(memberAccess.Name.Span.Start, invocation.Span.End); if (invocation.DescendantTrivia(span).All(f => f.IsWhitespaceOrEndOfLine())) { if (invocation.Parent?.IsKind(SyntaxKind.LogicalNotExpression) == true) { var logicalNot = (PrefixUnaryExpressionSyntax)invocation.Parent; if (logicalNot.OperatorToken.TrailingTrivia.IsWhitespaceOrEndOfLine() && logicalNot.Operand.GetLeadingTrivia().IsWhitespaceOrEndOfLine()) { messageArg = $"{propertyName} == 0"; } } else { messageArg = $"{propertyName} > 0"; } } if (messageArg != null) { Diagnostic diagnostic = Diagnostic.Create( DiagnosticDescriptors.UseCountOrLengthPropertyInsteadOfAnyMethod, Location.Create(context.Node.SyntaxTree, span), (propertyName == "Count") ? _propertiesCount : _propertiesLength, messageArg); context.ReportDiagnostic(diagnostic); } } } }
public static Task <Document> RefactorAsync( Document document, InvocationExpressionSyntax invocation, CancellationToken cancellationToken) { var memberAccess = (MemberAccessExpressionSyntax)invocation.Expression; var invocation2 = (InvocationExpressionSyntax)memberAccess.Expression; var memberAccess2 = (MemberAccessExpressionSyntax)invocation2.Expression; ExpressionSyntax expression1 = GetCondition(invocation); ExpressionSyntax expression2 = GetCondition(invocation2); InvocationExpressionSyntax newInvocation = invocation2.ReplaceNode( expression2, LogicalAndExpression( expression2.Parenthesize().WithSimplifierAnnotation(), expression1.Parenthesize().WithSimplifierAnnotation())); var newMemberAccess = (MemberAccessExpressionSyntax)newInvocation.Expression; SyntaxTriviaList trailingTrivia = invocation.GetTrailingTrivia(); IEnumerable <SyntaxTrivia> trivia = invocation.DescendantTrivia(TextSpan.FromBounds(invocation2.Span.End, memberAccess.Name.SpanStart)); if (trivia.Any(f => !f.IsWhitespaceOrEndOfLineTrivia())) { trailingTrivia = trailingTrivia.InsertRange(0, trivia); } newInvocation = newInvocation .WithExpression(newMemberAccess) .WithTrailingTrivia(trailingTrivia) .WithFormatterAnnotation(); return(document.ReplaceNodeAsync(invocation, newInvocation, cancellationToken)); }