private static void AnalyzeInitializerExpression(SyntaxNodeAnalysisContext context) { var initializer = (InitializerExpressionSyntax)context.Node; SeparatedSyntaxList <ExpressionSyntax> expressions = initializer.Expressions; ExpressionSyntax expression = expressions.SingleOrDefault(shouldThrow: false); if (expression == null) { return; } if (initializer.SpanContainsDirectives()) { return; } if (initializer.IsSingleLine(includeExteriorTrivia: false)) { return; } if (!expression.IsSingleLine()) { return; } if (!SyntaxTriviaAnalysis.IsExteriorTriviaEmptyOrWhitespace(expression)) { return; } if (!SyntaxTriviaAnalysis.IsExteriorTriviaEmptyOrWhitespace(initializer.OpenBraceToken)) { return; } if (!initializer.CloseBraceToken.LeadingTrivia.IsEmptyOrWhitespace()) { return; } if (expressions.SeparatorCount == 1 && !SyntaxTriviaAnalysis.IsExteriorTriviaEmptyOrWhitespace(expressions.GetSeparator(0))) { return; } if (!initializer.OpenBraceToken.GetPreviousToken().TrailingTrivia.IsEmptyOrWhitespace()) { return; } context.ReportDiagnostic(DiagnosticDescriptors.RemoveNewlinesFromInitializerWithSingleLineExpression, initializer); }
public static void AnalyzeInitializerExpression(SyntaxNodeAnalysisContext context) { var initializer = (InitializerExpressionSyntax)context.Node; SeparatedSyntaxList <ExpressionSyntax> expressions = initializer.Expressions; ExpressionSyntax expression = expressions.SingleOrDefault(shouldThrow: false); if (expression == null) { return; } if (initializer.SpanContainsDirectives()) { return; } if (!expression.IsSingleLine()) { return; } if (initializer.IsSingleLine()) { return; } if (!initializer .DescendantTrivia(TextSpan.FromBounds(initializer.FullSpan.Start, initializer.Span.End)) .All(f => f.IsWhitespaceOrEndOfLineTrivia())) { return; } context.ReportDiagnostic(DiagnosticDescriptors.FormatInitializerWithSingleExpressionOnSingleLine, initializer); }
private static bool IsSuppressingThisAnalyzer(SeparatedSyntaxList <ExpressionSyntax> errorCodes) { return(errorCodes.SingleOrDefault(shouldThrow: false) is IdentifierNameSyntax identifierName && string.Equals(identifierName.Identifier.ValueText, DiagnosticIdentifiers.MergePreprocessorDirectives, StringComparison.Ordinal)); }
internal static TNode SingleOrDefault <TNode>(this SeparatedSyntaxList <TNode> list, bool shouldthrow) where TNode : SyntaxNode { return((shouldthrow) ? list.SingleOrDefault() : ((list.Count == 1) ? list[0] : default(TNode))); }