private bool IsOperator(Token token) { return(TokenTraits.HasTrait(token.Kind, TokenFlags.AssignmentOperator) || TokenTraits.HasTrait(token.Kind, TokenFlags.BinaryPrecedenceAdd) || TokenTraits.HasTrait(token.Kind, TokenFlags.BinaryPrecedenceMultiply) || token.Kind == TokenKind.AndAnd || token.Kind == TokenKind.OrOr); }
private IEnumerable <DiagnosticRecord> FindOperatorViolations(TokenOperations tokenOperations) { foreach (var tokenNode in tokenOperations.GetTokenNodes(IsOperator)) { if (tokenNode.Previous == null || tokenNode.Next == null || tokenNode.Value.Kind == TokenKind.DotDot) { continue; } // exclude unary operator for cases like $foo.bar(-$Var) if (TokenTraits.HasTrait(tokenNode.Value.Kind, TokenFlags.UnaryOperator) && tokenNode.Previous.Value.Kind == TokenKind.LParen && tokenNode.Next.Value.Kind == TokenKind.Variable) { continue; } // exclude assignment operator inside of multi-line hash tables if requested if (IgnoreAssignmentOperatorInsideHashTable && tokenNode.Value.Kind == TokenKind.Equals) { Ast containingAst = tokenOperations.GetAstPosition(tokenNode.Value); if (containingAst is HashtableAst && containingAst.Extent.EndLineNumber != containingAst.Extent.StartLineNumber) { continue; } } var hasWhitespaceBefore = IsPreviousTokenOnSameLineAndApartByWhitespace(tokenNode); var hasWhitespaceAfter = tokenNode.Next.Value.Kind == TokenKind.NewLine || IsPreviousTokenOnSameLineAndApartByWhitespace(tokenNode.Next); if (!hasWhitespaceAfter || !hasWhitespaceBefore) { yield return(new DiagnosticRecord( GetError(ErrorKind.Operator), tokenNode.Value.Extent, GetName(), GetDiagnosticSeverity(), tokenOperations.Ast.Extent.File, null, GetCorrections( tokenNode.Previous.Value, tokenNode.Value, tokenNode.Next.Value, hasWhitespaceBefore, hasWhitespaceAfter))); } } }
private IEnumerable <DiagnosticRecord> FindOperatorViolations(TokenOperations tokenOperations) { foreach (var tokenNode in tokenOperations.GetTokenNodes(IsOperator)) { if (tokenNode.Previous == null || tokenNode.Next == null || tokenNode.Value.Kind == TokenKind.DotDot) { continue; } // exclude unary operator for cases like $foo.bar(-$Var) if (TokenTraits.HasTrait(tokenNode.Value.Kind, TokenFlags.UnaryOperator) && tokenNode.Previous.Value.Kind == TokenKind.LParen && tokenNode.Next.Value.Kind == TokenKind.Variable) { continue; } var hasWhitespaceBefore = IsPreviousTokenOnSameLineAndApartByWhitespace(tokenNode); var hasWhitespaceAfter = tokenNode.Next.Value.Kind == TokenKind.NewLine || IsPreviousTokenOnSameLineAndApartByWhitespace(tokenNode.Next); if (!hasWhitespaceAfter || !hasWhitespaceBefore) { yield return(new DiagnosticRecord( GetError(ErrorKind.Operator), tokenNode.Value.Extent, GetName(), GetDiagnosticSeverity(), tokenOperations.Ast.Extent.File, null, GetCorrections( tokenNode.Previous.Value, tokenNode.Value, tokenNode.Next.Value, hasWhitespaceBefore, hasWhitespaceAfter))); } } }
private IEnumerable <DiagnosticRecord> FindOpenParenViolations(TokenOperations tokenOperations) { foreach (var lparen in tokenOperations.GetTokenNodes(TokenKind.LParen)) { if (lparen.Previous != null && IsPreviousTokenOnSameLine(lparen) && TokenTraits.HasTrait(lparen.Previous.Value.Kind, TokenFlags.Keyword) && IsKeyword(lparen.Previous.Value) && !IsPreviousTokenApartByWhitespace(lparen)) { yield return(new DiagnosticRecord( GetError(ErrorKind.Paren), lparen.Value.Extent, GetName(), GetDiagnosticSeverity(), tokenOperations.Ast.Extent.File, null, GetCorrections(lparen.Previous.Value, lparen.Value, lparen.Next.Value, false, true).ToList())); } } }