private static void AnalyzeBinaryExpression(SyntaxNodeAnalysisContext context) { var binaryExpression = (BinaryExpressionSyntax)context.Node; ExpressionSyntax left = binaryExpression.Left; if (left.IsMissing) { return; } ExpressionSyntax right = binaryExpression.Right; if (right.IsMissing) { return; } if (!SyntaxTriviaAnalysis.IsTokenPrecededWithNewLineAndNotFollowedWithNewLine(left, binaryExpression.OperatorToken, right)) { return; } if (CSharpUtility.IsStringConcatenation(binaryExpression, context.SemanticModel, context.CancellationToken)) { return; } context.ReportDiagnostic( DiagnosticDescriptors.AddNewLineAfterBinaryOperatorInsteadOfBeforeIt, Location.Create(binaryExpression.SyntaxTree, binaryExpression.OperatorToken.Span.WithLength(0))); }
private static void AnalyzeConditionalExpression(SyntaxNodeAnalysisContext context) { var conditionalExpression = (ConditionalExpressionSyntax)context.Node; ExpressionSyntax condition = conditionalExpression.Condition; if (condition.IsMissing) { return; } ExpressionSyntax whenTrue = conditionalExpression.WhenTrue; if (whenTrue.IsMissing) { return; } if (SyntaxTriviaAnalysis.IsTokenPrecededWithNewLineAndNotFollowedWithNewLine(condition, conditionalExpression.QuestionToken, whenTrue)) { ReportDiagnostic(context, conditionalExpression.QuestionToken); } else { ExpressionSyntax whenFalse = conditionalExpression.WhenFalse; if (!whenFalse.IsMissing && SyntaxTriviaAnalysis.IsTokenPrecededWithNewLineAndNotFollowedWithNewLine(whenTrue, conditionalExpression.ColonToken, whenFalse)) { ReportDiagnostic(context, conditionalExpression.ColonToken); } } }
private static void AnalyzeConditionalAccess(SyntaxNodeAnalysisContext context) { var conditionalAccess = (ConditionalAccessExpressionSyntax)context.Node; ExpressionSyntax left = conditionalAccess.Expression; if (left.IsMissing) { return; } ExpressionSyntax right = conditionalAccess.WhenNotNull; if (right.IsMissing) { return; } NewLinePosition newLinePosition = context.GetNullConditionalOperatorNewLinePosition(); if (newLinePosition == NewLinePosition.None) { return; } SyntaxToken operatorToken = conditionalAccess.OperatorToken; if (SyntaxTriviaAnalysis.IsTokenFollowedWithNewLineAndNotPrecededWithNewLine(left, operatorToken, right)) { if (newLinePosition == NewLinePosition.Before) { DiagnosticHelpers.ReportDiagnostic( context, DiagnosticRules.PlaceNewLineAfterOrBeforeNullConditionalOperator, operatorToken, "before"); } } else if (SyntaxTriviaAnalysis.IsTokenPrecededWithNewLineAndNotFollowedWithNewLine(left, operatorToken, right) && newLinePosition == NewLinePosition.After) { DiagnosticHelpers.ReportDiagnostic( context, DiagnosticRules.PlaceNewLineAfterOrBeforeNullConditionalOperator, Location.Create(conditionalAccess.SyntaxTree, operatorToken.Span), DiagnosticProperties.NewLinePosition_After, "after"); } }
private static void AnalyzeBinaryExpression(SyntaxNodeAnalysisContext context) { var binaryExpression = (BinaryExpressionSyntax)context.Node; ExpressionSyntax left = binaryExpression.Left; if (left.IsMissing) { return; } ExpressionSyntax right = binaryExpression.Right; if (right.IsMissing) { return; } NewLinePosition newLinePosition = context.GetBinaryExpressionNewLinePosition(); if (newLinePosition == NewLinePosition.None) { return; } if (SyntaxTriviaAnalysis.IsTokenFollowedWithNewLineAndNotPrecededWithNewLine(left, binaryExpression.OperatorToken, right)) { if (newLinePosition == NewLinePosition.Before) { DiagnosticHelpers.ReportDiagnostic( context, DiagnosticRules.PlaceNewLineAfterOrBeforeBinaryOperator, Location.Create(binaryExpression.SyntaxTree, binaryExpression.OperatorToken.Span.WithLength(0)), "before"); } } else if (SyntaxTriviaAnalysis.IsTokenPrecededWithNewLineAndNotFollowedWithNewLine(left, binaryExpression.OperatorToken, right) && newLinePosition == NewLinePosition.After) { DiagnosticHelpers.ReportDiagnostic( context, DiagnosticRules.PlaceNewLineAfterOrBeforeBinaryOperator, Location.Create(binaryExpression.SyntaxTree, binaryExpression.OperatorToken.Span.WithLength(0)), properties: DiagnosticProperties.AnalyzerOption_Invert, "after"); } }
private static void AnalyzeBinaryExpression(SyntaxNodeAnalysisContext context) { var binaryExpression = (BinaryExpressionSyntax)context.Node; ExpressionSyntax left = binaryExpression.Left; if (left.IsMissing) { return; } ExpressionSyntax right = binaryExpression.Right; if (right.IsMissing) { return; } if (SyntaxTriviaAnalysis.IsTokenFollowedWithNewLineAndNotPrecededWithNewLine(left, binaryExpression.OperatorToken, right)) { if (context.IsAnalyzerSuppressed(AnalyzerOptions.AddNewLineAfterBinaryOperatorInsteadOfBeforeIt)) { ReportDiagnostic(DiagnosticDescriptors.AddNewLineBeforeBinaryOperatorInsteadOfAfterItOrViceVersa, ImmutableDictionary <string, string> .Empty); } } else if (SyntaxTriviaAnalysis.IsTokenPrecededWithNewLineAndNotFollowedWithNewLine(left, binaryExpression.OperatorToken, right) && !context.IsAnalyzerSuppressed(AnalyzerOptions.AddNewLineAfterBinaryOperatorInsteadOfBeforeIt)) { ReportDiagnostic(DiagnosticDescriptors.ReportOnly.AddNewLineAfterBinaryOperatorInsteadOfBeforeIt, DiagnosticProperties.AnalyzerOption_Invert); } void ReportDiagnostic(DiagnosticDescriptor descriptor, ImmutableDictionary <string, string> properties) { if (CSharpUtility.IsStringConcatenation(binaryExpression, context.SemanticModel, context.CancellationToken)) { return; } DiagnosticHelpers.ReportDiagnostic( context, descriptor, Location.Create(binaryExpression.SyntaxTree, binaryExpression.OperatorToken.Span.WithLength(0)), properties: properties); } }
private static void AnalyzeConditionalExpression(SyntaxNodeAnalysisContext context) { var conditionalExpression = (ConditionalExpressionSyntax)context.Node; ExpressionSyntax condition = conditionalExpression.Condition; if (condition.IsMissing) { return; } ExpressionSyntax whenTrue = conditionalExpression.WhenTrue; if (whenTrue.IsMissing) { return; } if (SyntaxTriviaAnalysis.IsTokenFollowedWithNewLineAndNotPrecededWithNewLine(condition, conditionalExpression.QuestionToken, whenTrue)) { if (!AnalyzerOptions.AddNewLineAfterConditionalOperatorInsteadOfBeforeIt.IsEnabled(context)) { ReportDiagnostic(DiagnosticRules.AddNewLineBeforeConditionalOperatorInsteadOfAfterItOrViceVersa, conditionalExpression.QuestionToken, ImmutableDictionary <string, string> .Empty); return; } } else if (SyntaxTriviaAnalysis.IsTokenPrecededWithNewLineAndNotFollowedWithNewLine(condition, conditionalExpression.QuestionToken, whenTrue)) { if (AnalyzerOptions.AddNewLineAfterConditionalOperatorInsteadOfBeforeIt.IsEnabled(context)) { ReportDiagnostic(DiagnosticRules.ReportOnly.AddNewLineAfterConditionalOperatorInsteadOfBeforeIt, conditionalExpression.QuestionToken, DiagnosticProperties.AnalyzerOption_Invert); return; } } ExpressionSyntax whenFalse = conditionalExpression.WhenFalse; if (!whenFalse.IsMissing) { if (SyntaxTriviaAnalysis.IsTokenFollowedWithNewLineAndNotPrecededWithNewLine(whenTrue, conditionalExpression.ColonToken, whenFalse)) { if (!AnalyzerOptions.AddNewLineAfterConditionalOperatorInsteadOfBeforeIt.IsEnabled(context)) { ReportDiagnostic(DiagnosticRules.AddNewLineBeforeConditionalOperatorInsteadOfAfterItOrViceVersa, conditionalExpression.ColonToken, ImmutableDictionary <string, string> .Empty); } } else if (SyntaxTriviaAnalysis.IsTokenPrecededWithNewLineAndNotFollowedWithNewLine(whenTrue, conditionalExpression.ColonToken, whenFalse)) { if (AnalyzerOptions.AddNewLineAfterConditionalOperatorInsteadOfBeforeIt.IsEnabled(context)) { ReportDiagnostic(DiagnosticRules.ReportOnly.AddNewLineAfterConditionalOperatorInsteadOfBeforeIt, conditionalExpression.ColonToken, DiagnosticProperties.AnalyzerOption_Invert); } } } void ReportDiagnostic(DiagnosticDescriptor descriptor, SyntaxToken token, ImmutableDictionary <string, string> properties) { DiagnosticHelpers.ReportDiagnostic( context, descriptor, Location.Create(token.SyntaxTree, token.Span.WithLength(0)), properties: properties); } }
private static void AnalyzeConditionalExpression(SyntaxNodeAnalysisContext context) { var conditionalExpression = (ConditionalExpressionSyntax)context.Node; ExpressionSyntax condition = conditionalExpression.Condition; if (condition.IsMissing) { return; } ExpressionSyntax whenTrue = conditionalExpression.WhenTrue; if (whenTrue.IsMissing) { return; } NewLinePosition newLinePosition = context.GetConditionalExpressionNewLinePosition(); if (newLinePosition == NewLinePosition.None) { return; } if (SyntaxTriviaAnalysis.IsTokenFollowedWithNewLineAndNotPrecededWithNewLine(condition, conditionalExpression.QuestionToken, whenTrue)) { if (newLinePosition == NewLinePosition.Before) { ReportDiagnostic(conditionalExpression.QuestionToken, ImmutableDictionary <string, string> .Empty, "before"); return; } } else if (SyntaxTriviaAnalysis.IsTokenPrecededWithNewLineAndNotFollowedWithNewLine(condition, conditionalExpression.QuestionToken, whenTrue)) { if (newLinePosition == NewLinePosition.After) { ReportDiagnostic(conditionalExpression.QuestionToken, DiagnosticProperties.AnalyzerOption_Invert, "after"); return; } } ExpressionSyntax whenFalse = conditionalExpression.WhenFalse; if (!whenFalse.IsMissing) { if (SyntaxTriviaAnalysis.IsTokenFollowedWithNewLineAndNotPrecededWithNewLine(whenTrue, conditionalExpression.ColonToken, whenFalse)) { if (newLinePosition == NewLinePosition.Before) { ReportDiagnostic(conditionalExpression.ColonToken, ImmutableDictionary <string, string> .Empty, "before"); } } else if (SyntaxTriviaAnalysis.IsTokenPrecededWithNewLineAndNotFollowedWithNewLine(whenTrue, conditionalExpression.ColonToken, whenFalse)) { if (newLinePosition == NewLinePosition.After) { ReportDiagnostic(conditionalExpression.ColonToken, DiagnosticProperties.AnalyzerOption_Invert, "after"); } } } void ReportDiagnostic( SyntaxToken token, ImmutableDictionary <string, string> properties, string messageArg) { DiagnosticHelpers.ReportDiagnostic( context, DiagnosticRules.PlaceNewLineAfterOrBeforeConditionalOperator, Location.Create(token.SyntaxTree, token.Span.WithLength(0)), properties: properties, messageArg); } }