Exemple #1
0
        /// <summary>
        /// Determines whether the token list contains the given strings, skipping whitespace tokens and
        /// comments.
        /// </summary>
        /// <param name="comparisonType">
        /// The string comparison type to use.
        /// </param>
        /// <param name="values">
        /// The collection of strings to match against.
        /// </param>
        /// <returns>
        /// Returns true if the tokens match the collection of strings.
        /// </returns>
        public bool MatchTokens(StringComparison comparisonType, params string[] values)
        {
            Param.RequireNotNull(values, "values");
            Param.Ignore(comparisonType);

            return(CsTokenList.MatchTokens(comparisonType, this.First, values));
        }
Exemple #2
0
        /// <summary>
        /// Determines whether the given token list contains the given strings, skipping whitespace tokens and
        /// comments.
        /// </summary>
        /// <param name="start">
        /// Begins matching the given strings with this token.
        /// </param>
        /// <param name="values">
        /// The collection of strings to match against.
        /// </param>
        /// <returns>
        /// Returns true if the tokens match the collection of strings.
        /// </returns>
        public static bool MatchTokens(Node <CsToken> start, params string[] values)
        {
            Param.RequireNotNull(start, "start");
            Param.RequireNotNull(values, "values");

            return(CsTokenList.MatchTokens(StringComparison.Ordinal, start, values));
        }
Exemple #3
0
        /// <summary>
        /// Checks a type to determine whether it should use one of the built-in types.
        /// </summary>
        /// <param name="type">
        /// The type to check.
        /// </param>
        /// <param name="document">
        /// The parent document.
        /// </param>
        private void CheckBuiltInType(Node <CsToken> type, CsDocument document)
        {
            Param.AssertNotNull(type, "type");
            Param.AssertNotNull(document, "document");

            Debug.Assert(type.Value is TypeToken, "The type must be a TypeToken");
            TypeToken typeToken = (TypeToken)type.Value;

            if (type.Value.CsTokenClass != CsTokenClass.GenericType)
            {
                for (int i = 0; i < this.builtInTypes.Length; ++i)
                {
                    string[] builtInType = this.builtInTypes[i];

                    if (CsTokenList.MatchTokens(typeToken.ChildTokens.First, builtInType[0]) ||
                        CsTokenList.MatchTokens(typeToken.ChildTokens.First, "System", ".", builtInType[0]))
                    {
                        // If the previous token is an equals sign, then this is a using alias directive. For example:
                        // using SomeAlias = System.String;
                        // If the previous token is the 'static' keyword, then this is a using static directive. For example:
                        // using static System.String;
                        bool shouldBuiltInTypeAliasBeUsed = true;
                        for (Node <CsToken> previous = type.Previous; previous != null; previous = previous.Previous)
                        {
                            if (previous.Value.CsTokenType != CsTokenType.EndOfLine && previous.Value.CsTokenType != CsTokenType.MultiLineComment &&
                                previous.Value.CsTokenType != CsTokenType.SingleLineComment && previous.Value.CsTokenType != CsTokenType.WhiteSpace)
                            {
                                if (previous.Value.Text == "=" || previous.Value.Text == "static")
                                {
                                    shouldBuiltInTypeAliasBeUsed = false;
                                }

                                break;
                            }
                        }

                        if (shouldBuiltInTypeAliasBeUsed)
                        {
                            this.AddViolation(
                                typeToken.FindParentElement(), typeToken.LineNumber, Rules.UseBuiltInTypeAlias, builtInType[2], builtInType[0], builtInType[1]);
                        }

                        break;
                    }
                }
            }

            for (Node <CsToken> childToken = typeToken.ChildTokens.First; childToken != null; childToken = childToken.Next)
            {
                if (childToken.Value.CsTokenClass == CsTokenClass.Type || childToken.Value.CsTokenClass == CsTokenClass.GenericType)
                {
                    this.CheckBuiltInType(childToken, document);
                }
            }
        }
Exemple #4
0
        /// <summary>
        /// Returns true if the node Contains any sort of <see cref="Nullable"/>.
        /// </summary>
        /// <param name="token">
        /// The token to check.
        /// </param>
        /// <returns>
        /// True if <see cref="Nullable"/> otherwise False.
        /// </returns>
        public static bool TokenContainNullable(Node <CsToken> token)
        {
            if (CsTokenList.MatchTokens(StringComparison.Ordinal, token, new[] { "System", ".", "Nullable" }) ||
                CsTokenList.MatchTokens(StringComparison.Ordinal, token, new[] { "global", "::", "System", ".", "Nullable" }) ||
                token.Value.Text.Equals("Nullable", StringComparison.Ordinal))
            {
                return(true);
            }

            return(false);
        }
        /// <summary>
        /// Checks a type to determine whether it should use one of the built-in types.
        /// </summary>
        /// <param name="type">
        /// The type to check.
        /// </param>
        private void CheckBuiltInTypeForMemberAccessExpressions(Node <CsToken> type)
        {
            Param.AssertNotNull(type, "type");

            for (int i = 0; i < this.builtInTypes.Length; ++i)
            {
                string[] builtInType = this.builtInTypes[i];

                if (CsTokenList.MatchTokens(type, builtInType[0]) || CsTokenList.MatchTokens(type, "System", ".", builtInType[0]))
                {
                    this.AddViolation(type.Value.FindParentElement(), type.Value.LineNumber, Rules.UseBuiltInTypeAlias, builtInType[2], builtInType[0], builtInType[1]);
                    break;
                }
            }
        }
Exemple #6
0
        /// <summary>
        /// Determines whether the token list contains the given strings, skipping whitespace tokens and
        /// comments.
        /// </summary>
        /// <param name="values">
        /// The collection of strings to match against.
        /// </param>
        /// <returns>
        /// Returns true if the tokens match the collection of strings.
        /// </returns>
        public bool MatchTokens(params string[] values)
        {
            Param.RequireNotNull(values, "values");

            return(CsTokenList.MatchTokens(this.First, values));
        }