private bool IsSingleCharViolation(SyntaxNode declaration) { var identifier = declaration.ChildTokens().First(t => t.RawKind == IdentifierToken); if (IsInLoop(declaration)) return false; if (IsInLamda(declaration)) return false; return identifier.Value.ToString().Length == 1; }
private static Location GetDiagnosticLocation(SyntaxNode node) { if (node.HasLeadingTrivia) { return node.GetLeadingTrivia()[0].GetLocation(); } var firstToken = node.ChildTokens().FirstOrDefault(); if (firstToken != default(SyntaxToken)) { return node.ChildTokens().First().GetLocation(); } return Location.None; }
internal static string GetIdentifierValue(this SyntaxNode node) { return(node.ChildTokens().FirstOrDefault(x => x.Kind() == SyntaxKind.IdentifierToken).ValueText); }
/// <summary> /// Determines the content's block position depending on the node type. /// </summary> /// <param name="node">The node to extract the content position from.</param> /// <returns>The determined content position or 0 if not found.</returns> private int DetermineContentPosition(SyntaxNode node) { if(node == null) { throw new ArgumentNullException(nameof(node)); } // Select the content node element depending on the node type TextSpan? contentTextSpan = null; switch (node.Kind()) { // Accessor list content case SyntaxKind.PropertyDeclaration: case SyntaxKind.IndexerDeclaration: case SyntaxKind.EventDeclaration: AccessorListSyntax accessorList = node.DescendantNodes().OfType<AccessorListSyntax>().FirstOrDefault(); if (null != accessorList) { contentTextSpan = accessorList.FullSpan; } break; // Contains children case SyntaxKind.NamespaceDeclaration: case SyntaxKind.InterfaceDeclaration: case SyntaxKind.ClassDeclaration: SyntaxToken token = node.ChildTokens().FirstOrDefault(x => x.Kind() == SyntaxKind.OpenBraceToken); if (null != token) { contentTextSpan = token.FullSpan; } break; // Block content case SyntaxKind.ConstructorDeclaration: case SyntaxKind.DestructorDeclaration: case SyntaxKind.MethodDeclaration: case SyntaxKind.GetAccessorDeclaration: case SyntaxKind.SetAccessorDeclaration: case SyntaxKind.AddAccessorDeclaration: case SyntaxKind.RemoveAccessorDeclaration: BlockSyntax block = node.DescendantNodes().OfType<BlockSyntax>().FirstOrDefault(); if (null != block) { contentTextSpan = block.FullSpan; } break; // Not processed by projbook csharp extractor default: break; } // Compute a line break insensitive position based on the fetched content text span if any is found if (null != contentTextSpan) { int relativeTextSpanStart = contentTextSpan.Value.Start - node.FullSpan.Start; return node .ToFullString() .Substring(0, relativeTextSpanStart) .Replace("\r\n", "") .Replace("\n", "").Length; } // Otherwise return 0 as default value return 0; }
// TODO: Exception e is allowed private bool IsNumberSeriesViolation(SyntaxNode declaration) { const string numberSeriesRegex = "^[a-zA-Z][0-9]{1,3}$"; var identifier = declaration.ChildTokens().First(t => t.RawKind == IdentifierToken); return Regex.IsMatch(identifier.Value.ToString(), numberSeriesRegex); }