private static void AnalyzeBraces(SyntaxNodeAnalysisContext context, IfStatementSyntax ifStatement) { if (!ifStatement.IsParentKind(SyntaxKind.ElseClause) && ifStatement.Else != null) { BracesAnalysisResult result = CSharpAnalysis.AnalyzeBraces(ifStatement); if ((result & BracesAnalysisResult.AddBraces) != 0) { context.ReportDiagnostic(DiagnosticDescriptors.AddBracesToIfElse, ifStatement); } if ((result & BracesAnalysisResult.RemoveBraces) != 0) { context.ReportDiagnostic(DiagnosticDescriptors.RemoveBracesFromIfElse, ifStatement); foreach (SyntaxNode node in ifStatement.DescendantNodes()) { if (node.IsKind(SyntaxKind.Block)) { context.ReportBraces(DiagnosticDescriptors.RemoveBracesFromIfElseFadeOut, (BlockSyntax)node); } } } } }
private static void RemoveBracesFromIfElseFadeOut(SyntaxNodeAnalysisContext context, IfStatementSyntax ifStatement) { foreach (SyntaxNode node in ifStatement.DescendantNodes()) { if (node.IsKind(SyntaxKind.Block)) { context.FadeOutBraces( DiagnosticDescriptors.RemoveBracesFromIfElseFadeOut, (BlockSyntax)node); } } }
public override SyntaxNode VisitIfStatement(IfStatementSyntax node) { return(node.ReplaceNodes(node.DescendantNodes().OfType <ElementAccessExpressionSyntax>(), (o, n) => { if (o.Parent.IsKind(SyntaxKind.EqualsExpression)) { return o; } return SyntaxFactory.BinaryExpression(SyntaxKind.EqualsExpression, o, SyntaxFactory.LiteralExpression(SyntaxKind.TrueLiteralExpression)); })); }
private void AnalyzeIf(IfStatementSyntax node) { var startLine = node.SyntaxTree.GetLineSpan(node.Span).StartLinePosition.Line; var endLine = node.SyntaxTree.GetLineSpan(node.Span).EndLinePosition.Line; var Level = node.DescendantNodes().OfType <IfStatementSyntax>().LongCount <IfStatementSyntax>(); if (Level > 1) { ErrorList.Add(new Error() { startLine = startLine, endLine = endLine, ErrorMessage = "More Than Two Indent Level", Name = "IF", Type = "IfStatementSyntax" }); } }