Esempio n. 1
0
        /// <summary>
        /// Checks variables to look for underscores.
        /// </summary>
        /// <param name="element">The parent element.</param>
        /// <param name="variables">The variables to check.</param>
        private void CheckUnderscores(Element element, VariableCollection variables)
        {
            Param.AssertNotNull(element, "element");
            Param.AssertNotNull(variables, "variables");

            foreach (IVariable variable in variables)
            {
                if (variable.VariableName.StartsWith("_", StringComparison.Ordinal) && variable.VariableName != "__arglist")
                {
                    this.AddViolation(element, variable.Location.LineNumber, Rules.FieldNamesMustNotBeginWithUnderscore);
                }
            }
        }
        /// <summary>
        /// Determines whether a matching local variable is contained in the given variable list.
        /// </summary>
        /// <param name="variables">The variable list.</param>
        /// <param name="word">The variable name to check.</param>
        /// <param name="item">The token containing the variable name.</param>
        /// <returns>Returns true if there is a matching local variable.</returns>
        private static bool ContainsVariable(VariableCollection variables, string word, Token item)
        {
            Param.AssertNotNull(variables, "variables");
            Param.AssertValidString(word, "word");
            Param.AssertNotNull(item, "item");

            IVariable variable = variables[word];
            if (variable != null)
            {
                // Make sure the variable appears before the word.
                if (variable.Location.LineNumber < item.LineNumber)
                {
                    return true;
                }
                else if (variable.Location.LineNumber == item.LineNumber)
                {
                    if (variable.Location.StartPoint.IndexOnLine <= item.Location.StartPoint.IndexOnLine)
                    {
                        return true;
                    }
                }
            }

            return false;
        }
Esempio n. 3
0
        /// <summary>
        /// Gets the variables defined by an element with parameters and child statements, such as a method or a constructor.
        /// </summary>
        /// <param name="element">The element.</param>
        /// <param name="parameters">The element's parameters.</param>
        /// <returns>Returns the variables.</returns>
        internal static VariableCollection GatherVariablesForElementWithParametersAndChildStatements(Element element, IList<Parameter> parameters)
        {
            Param.AssertNotNull(element, "element");
            Param.Ignore(parameters);

            var variables = new VariableCollection();

            if (parameters != null && parameters.Count > 0)
            {
                for (int i = 0; i < parameters.Count; ++i)
                {
                    variables.Add(parameters[i]);
                }
            }

            for (VariableDeclarationStatement variableStatement = element.FindFirstChild<VariableDeclarationStatement>();
                variableStatement != null;
                variableStatement = variableStatement.FindNextSibling<VariableDeclarationStatement>())
            {
                variables.AddRange(variableStatement.Variables);
            }

            return variables;
        }