public static void Analyze(SyntaxNodeAnalysisContext context) { SyntaxNode node = context.Node; if (!node.IsKind(SyntaxKind.IfStatement) || !IfElseChain.IsPartOfChain((IfStatementSyntax)node)) { BlockSyntax block = GetBlockThatCanBeEmbeddedStatement(node); if (block != null) { SyntaxToken openBrace = block.OpenBraceToken; SyntaxToken closeBrace = block.CloseBraceToken; if (!openBrace.IsMissing && !closeBrace.IsMissing && openBrace.GetLeadingAndTrailingTrivia().All(f => f.IsWhitespaceOrEndOfLineTrivia()) && closeBrace.GetLeadingAndTrailingTrivia().All(f => f.IsWhitespaceOrEndOfLineTrivia())) { context.ReportDiagnostic( DiagnosticDescriptors.RemoveBraces, block, GetName(node)); context.ReportBraces(DiagnosticDescriptors.RemoveBracesFadeOut, block); } } } }
private static void AnalyzeIfStatement(SyntaxNodeAnalysisContext context) { SyntaxNode node = context.Node; if (IfElseChain.IsPartOfChain((IfStatementSyntax)node)) { Analyze(context, node); } }
public static void ComputeRefactoring(RefactoringContext context, SelectedStatementCollection selectedStatements) { if (selectedStatements.Count == 2) { StatementSyntax[] statements = selectedStatements.ToArray(); if (statements[0].IsKind(SyntaxKind.IfStatement) && statements[1].IsKind(SyntaxKind.ReturnStatement)) { var ifStatement = (IfStatementSyntax)statements[0]; if (!IfElseChain.IsPartOfChain(ifStatement)) { ExpressionSyntax returnExpression = ReplaceIfWithStatementRefactoring.GetReturnExpression(ifStatement); if (returnExpression != null) { var returnStatement = (ReturnStatementSyntax)statements[1]; ExpressionSyntax returnExpression2 = returnStatement.Expression; if (returnExpression2 != null) { const string title = "Replace if-return with return"; if (returnExpression.IsBooleanLiteralExpression() || returnExpression2.IsBooleanLiteralExpression()) { context.RegisterRefactoring( title, cancellationToken => RefactorAsync(context.Document, selectedStatements.Container, ifStatement, returnStatement, cancellationToken)); } else { context.RegisterRefactoring( title, cancellationToken => { return(RefactorAsync( context.Document, selectedStatements.Container, ifStatement, returnStatement, returnExpression, returnExpression2, cancellationToken)); }); } } } } } } }
public static void Analyze(SyntaxNodeAnalysisContext context, StatementSyntax statement) { if (!statement.IsKind(SyntaxKind.IfStatement) || !IfElseChain.IsPartOfChain((IfStatementSyntax)statement)) { StatementSyntax embeddedStatement = GetEmbeddedStatementThatShouldBeInsideBlock(statement); if (embeddedStatement != null) { context.ReportDiagnostic( DiagnosticDescriptors.AddBraces, embeddedStatement.GetLocation(), GetName(statement)); } } }
public static void Analyze(SyntaxNodeAnalysisContext context, IfStatementSyntax ifStatement) { if (!IfElseChain.IsPartOfChain(ifStatement) && CheckCondition(ifStatement.Condition)) { IfStatementSyntax nestedIf = GetNestedIfStatement(ifStatement); if (nestedIf != null && nestedIf.Else == null && CheckCondition(nestedIf.Condition) && CheckTrivia(ifStatement, nestedIf) && !ifStatement.SpanContainsDirectives()) { ReportDiagnostic(context, ifStatement, nestedIf); } } }
public static async Task ComputeRefactoringAsync(RefactoringContext context, IfStatementSyntax ifStatement) { if (IfElseChain.IsTopmostIf(ifStatement)) { SemanticModel semanticModel = await context.GetSemanticModelAsync().ConfigureAwait(false); if (IfElseChain.GetChain(ifStatement) .Where(f => f.IsKind(SyntaxKind.IfStatement)) .All(f => IsValidIf((IfStatementSyntax)f, semanticModel, context.CancellationToken))) { string title = (IfElseChain.IsPartOfChain(ifStatement)) ? "Replace if-else with switch" : "Replace if with switch"; context.RegisterRefactoring( title, cancellationToken => RefactorAsync(context.Document, ifStatement, cancellationToken)); } } }
public static void AnalyzeIfStatement(SyntaxNodeAnalysisContext context) { var ifStatement = (IfStatementSyntax)context.Node; if (!IfElseChain.IsPartOfChain(ifStatement)) { ExpressionSyntax condition = ifStatement.Condition; if (condition?.IsKind(SyntaxKind.EqualsExpression) == true) { var equalsExpression = (BinaryExpressionSyntax)condition; ExpressionSyntax left = equalsExpression.Left; if (left != null) { ExpressionSyntax right = equalsExpression.Right; if (right?.IsKind(SyntaxKind.NullLiteralExpression) == true) { StatementSyntax blockOrStatement = ifStatement.Statement; StatementSyntax childStatement = GetSingleStatementOrDefault(blockOrStatement); if (childStatement?.IsKind(SyntaxKind.ExpressionStatement) == true) { var expressionStatement = (ExpressionStatementSyntax)childStatement; ExpressionSyntax expression = expressionStatement.Expression; if (expression?.IsKind(SyntaxKind.SimpleAssignmentExpression) == true) { var assignment = (AssignmentExpressionSyntax)expression; if (assignment.Left?.IsEquivalentTo(left, topLevel: false) == true && assignment.Right?.IsMissing == false && !ifStatement.SpanContainsDirectives()) { StatementSyntax statementToReport = ifStatement; SyntaxNode parent = ifStatement.Parent; SyntaxList <StatementSyntax> statements = GetStatements(parent); if (statements.Any()) { int index = statements.IndexOf(ifStatement); if (index > 0) { StatementSyntax previousStatement = statements[index - 1]; if (CanRefactor(previousStatement, ifStatement, left, parent)) { statementToReport = previousStatement; } } } context.ReportDiagnostic(DiagnosticDescriptors.UseCoalesceExpression, statementToReport); } } } } } } } }