public static Task <Document> RefactorAsync( Document document, IfStatementSyntax ifStatement, CancellationToken cancellationToken) { var statement = (ExpressionStatementSyntax)ifStatement.GetSingleStatementOrDefault(); MemberInvocationStatement memberInvocation = MemberInvocationStatement.Create(statement); int insertIndex = memberInvocation.Expression.Span.End - statement.FullSpan.Start; StatementSyntax newStatement = ParseStatement(statement.ToFullString().Insert(insertIndex, "?")); IEnumerable <SyntaxTrivia> leading = ifStatement.DescendantTrivia(TextSpan.FromBounds(ifStatement.SpanStart, statement.SpanStart)); newStatement = (leading.All(f => f.IsWhitespaceOrEndOfLineTrivia())) ? newStatement.WithLeadingTrivia(ifStatement.GetLeadingTrivia()) : newStatement.WithLeadingTrivia(ifStatement.GetLeadingTrivia().Concat(leading)); IEnumerable <SyntaxTrivia> trailing = ifStatement.DescendantTrivia(TextSpan.FromBounds(statement.Span.End, ifStatement.Span.End)); newStatement = (leading.All(f => f.IsWhitespaceOrEndOfLineTrivia())) ? newStatement.WithTrailingTrivia(ifStatement.GetTrailingTrivia()) : newStatement.WithTrailingTrivia(trailing.Concat(ifStatement.GetTrailingTrivia())); return(document.ReplaceNodeAsync(ifStatement, newStatement, cancellationToken)); }
public static void AnalyzeIfStatement(SyntaxNodeAnalysisContext context) { var ifStatement = (IfStatementSyntax)context.Node; if (ifStatement.IsSimpleIf() && !ifStatement.ContainsDiagnostics) { SyntaxList <StatementSyntax> statements; if (ifStatement.TryGetContainingList(out statements) && !IsPartOfLazyInitialization(ifStatement, statements)) { EqualsToNullExpression equalsToNull; if (EqualsToNullExpression.TryCreate(ifStatement.Condition, out equalsToNull)) { SimpleAssignmentStatement assignment; if (SimpleAssignmentStatement.TryCreate(ifStatement.GetSingleStatementOrDefault(), out assignment) && SyntaxComparer.AreEquivalent(assignment.Left, equalsToNull.Left) && assignment.Right.IsSingleLine() && !ifStatement.SpanContainsDirectives()) { int index = statements.IndexOf(ifStatement); if (index > 0) { StatementSyntax previousStatement = statements[index - 1]; if (!previousStatement.ContainsDiagnostics && CanRefactor(previousStatement, ifStatement, equalsToNull.Left, ifStatement.Parent)) { context.ReportDiagnostic(DiagnosticDescriptors.UseCoalesceExpression, previousStatement); } } if (index < statements.Count - 1) { StatementSyntax nextStatement = statements[index + 1]; if (!nextStatement.ContainsDiagnostics) { MemberInvocationStatement memberInvocation; if (MemberInvocationStatement.TryCreate(nextStatement, out memberInvocation) && SyntaxComparer.AreEquivalent(equalsToNull.Left, memberInvocation.Expression) && !ifStatement.Parent.ContainsDirectives(TextSpan.FromBounds(ifStatement.SpanStart, nextStatement.Span.End))) { context.ReportDiagnostic(DiagnosticDescriptors.InlineLazyInitialization, ifStatement); } } } } } } } }
public static void AnalyzeIfStatement(SyntaxNodeAnalysisContext context, INamedTypeSymbol expressionType) { var ifStatement = (IfStatementSyntax)context.Node; if (ifStatement.IsSimpleIf() && !ifStatement.ContainsDiagnostics) { NotEqualsToNullExpression notEqualsToNull; if (NotEqualsToNullExpression.TryCreate(ifStatement.Condition, out notEqualsToNull)) { MemberInvocationStatement memberInvocation; if (MemberInvocationStatement.TryCreate(ifStatement.GetSingleStatementOrDefault(), out memberInvocation) && notEqualsToNull.Left.IsEquivalentTo(memberInvocation.Expression, topLevel: false) && !ifStatement.IsInExpressionTree(expressionType, context.SemanticModel, context.CancellationToken) && !ifStatement.SpanContainsDirectives()) { context.ReportDiagnostic(DiagnosticDescriptors.UseConditionalAccess, ifStatement); } } } }
public static Task <Document> InlineLazyInitializationAsync( Document document, IfStatementSyntax ifStatement, CancellationToken cancellationToken) { StatementContainer container = StatementContainer.Create(ifStatement); SyntaxList <StatementSyntax> statements = container.Statements; int index = statements.IndexOf(ifStatement); StatementSyntax expressionStatement = (ExpressionStatementSyntax)statements[index + 1]; MemberInvocationStatement invocation = MemberInvocationStatement.Create((ExpressionStatementSyntax)expressionStatement); ExpressionSyntax expression = invocation.Expression; SimpleAssignmentStatement assignment = SimpleAssignmentStatement.Create((ExpressionStatementSyntax)ifStatement.GetSingleStatementOrDefault()); BinaryExpressionSyntax coalesceExpression = CSharpFactory.CoalesceExpression(expression.WithoutTrivia(), ParenthesizedExpression(assignment.AssignmentExpression)); ParenthesizedExpressionSyntax newExpression = ParenthesizedExpression(coalesceExpression) .WithTriviaFrom(expression); StatementSyntax newExpressionStatement = expressionStatement.ReplaceNode(expression, newExpression); IEnumerable <SyntaxTrivia> trivia = container.Node.DescendantTrivia(TextSpan.FromBounds(ifStatement.FullSpan.Start, expressionStatement.FullSpan.Start)); if (trivia.Any(f => !f.IsWhitespaceOrEndOfLineTrivia())) { newExpressionStatement = newExpressionStatement.PrependToLeadingTrivia(trivia); } SyntaxList <StatementSyntax> newStatements = statements .Replace(expressionStatement, newExpressionStatement) .RemoveAt(index); return(document.ReplaceNodeAsync(container.Node, container.NodeWithStatements(newStatements), cancellationToken)); }