Exemplo n.º 1
0
 internal static void Violate(this SourceAnalyzer sourceAnalyzer, ICodePart value, params object[] args)
 {
     var ruleName = sourceAnalyzer.GetType().Name;
     var rule = sourceAnalyzer.GetRule(ruleName);
     if (rule != null)
     {
         var element = value.FindParentElement();
         sourceAnalyzer.AddViolation(element, value.Location, ruleName, args);
         DebugWrite(element.Violations.Last());
     }
 }
        private static bool IsPartOfInterface(Method method)
        {
            ICodePart current = method.Parent;

            while (!(current is ClassBase))
            {
                current = current.Parent;
            }

            return(current is Interface);
        }
Exemplo n.º 3
0
        private IEnumerable <Class> GetClasses(ICodePart parent)
        {
            foreach (var @class in parent.Children.OfType <Class>())
            {
                yield return(@class);

                foreach (var subClass in GetClasses(@class))
                {
                    yield return(subClass);
                }
            }
        }
Exemplo n.º 4
0
        /// <summary>
        /// Gets the element that contains this code unit, if there is one.
        /// </summary>
        /// <param name="part">
        /// The code part.
        /// </param>
        /// <returns>
        /// Returns the element or null if there is no parent expression.
        /// </returns>
        public static CsElement FindParentElement(this ICodePart part)
        {
            Param.RequireNotNull(part, "part");

            ICodePart parentCodePart = part.Parent;

            while (parentCodePart != null)
            {
                if (parentCodePart.CodePartType == CodePartType.Element)
                {
                    return((CsElement)parentCodePart);
                }

                parentCodePart = parentCodePart.Parent;
            }

            return(null);
        }
Exemplo n.º 5
0
        /// <summary>
        /// Returns true if we're inside one of the container types..
        /// </summary>
        /// <param name="expresion">
        /// The expression to start at.
        /// </param>
        /// <param name="codeUnits">
        /// The containers to check.
        /// </param>
        /// <returns>
        /// True if found.
        /// </returns>
        public static bool IsExpressionInsideContainer(Expression expresion, params Type[] codeUnits)
        {
            ICodePart parent = expresion.Parent;

            while (parent != null)
            {
                foreach (Type codeUnit in codeUnits)
                {
                    if (parent.GetType() == codeUnit)
                    {
                        return(true);
                    }
                }

                parent = parent.Parent;
            }

            return(false);
        }
Exemplo n.º 6
0
        public void Register(ICodePart child, ICodePart parent = null)
        {
            if (parent != null)
            {
                Register(new ChildLink(parent, child));
            }
            switch (child)
            {
            case Solution solution:
                Solutions.TryAdd(solution.FullName, solution);
                solution.Projects.ForEach(p => Register(p, solution));
                break;

            case Project project:
                Projects.TryAdd(project.FullName, project);
                project.SourceFiles.Values.ForEach(c => Register(c, project));
                break;

            case SourceFile sourceFile:
                SourceFiles.TryAdd(sourceFile.FullName, sourceFile);
                sourceFile.Classes.Values.ForEach(c => Register(c, parent));
                sourceFile.Classes.Values.ForEach(c => Register(c, sourceFile));
                break;

            case Class cls:
                Classes.TryAdd(cls.FullName, cls);
                cls.Children.ForEach(c => Register(c, cls));
                cls.Implements.ForEach(interfaceName => Interfaces.TryAdd(interfaceName, cls));
                break;

            case Method method:
                Methods.TryAdd(method.FullName, method);
                break;

            case Property property:
                //TODO
                break;

            default:
                throw new NotSupportedException("Unknown code part: " + child.GetType());
            }
        }
Exemplo n.º 7
0
        /// <summary>
        /// Finds the ClassBase object above this element representing a Class or Struct.
        /// </summary>
        /// <param name="element">
        /// The element to start at.
        /// </param>
        /// <returns>
        /// The ClassBase for the element.
        /// </returns>
        public static ClassBase GetClassBase(CsElement element)
        {
            bool foundParentClass = false;

            ICodePart parent = element.Parent;

            while (!foundParentClass && parent != null)
            {
                if (parent is ClassBase)
                {
                    foundParentClass = true;
                }
                else
                {
                    parent = parent.Parent;
                }
            }

            return(parent as ClassBase);
        }
Exemplo n.º 8
0
        /// <summary>
        /// Determines whether the node is part of a method parameter.
        /// </summary>
        /// <param name="node">
        /// The node to check to see if its part of a method parameter.
        /// </param>
        /// <returns>
        /// Returns true if the node is part of a method parameter, false otherwise.
        /// </returns>
        private static bool IsMethodParameterDeclaration(Node <CsToken> node)
        {
            Param.Ignore(node);

            if (node != null && node.Value != null)
            {
                ICodePart parent = node.Value.Parent;

                while (parent != null)
                {
                    if (parent.CodePartType == CodePartType.Parameter)
                    {
                        return(true);
                    }

                    parent = parent.Parent;
                }
            }

            return(false);
        }
Exemplo n.º 9
0
        /// <summary>
        /// Gets the statement that contains this code unit, if there is one.
        /// </summary>
        /// <param name="part">
        /// The code part.
        /// </param>
        /// <returns>
        /// Returns the statement or null if there is no parent expression.
        /// </returns>
        public static Statement FindParentStatement(this ICodePart part)
        {
            Param.RequireNotNull(part, "part");

            ICodePart parentCodeUnit = part.Parent;

            while (parentCodeUnit != null)
            {
                if (parentCodeUnit.CodePartType == CodePartType.Statement)
                {
                    return((Statement)parentCodeUnit);
                }

                // If we hit an element, then there is no parent statement.
                if (parentCodeUnit.CodePartType == CodePartType.Element)
                {
                    return(null);
                }

                parentCodeUnit = parentCodeUnit.Parent;
            }

            return(null);
        }
Exemplo n.º 10
0
 /// <summary>
 /// Initializes a new instance of the <see cref="CodeUnitCollection{T}"/> class.
 /// Initializes a new instance of the CodeUnitCollection class.
 /// </summary>
 /// <param name="parent">
 /// The parent of all items in the collection.
 /// </param>
 public CodeUnitCollection(ICodePart parent)
 {
     Param.Ignore(parent);
     this.parent = parent;
 }
Exemplo n.º 11
0
 /// <summary>
 /// Sets the parent of this code unit.
 /// </summary>
 /// <param name="parentCodeUnit">
 /// The parent.
 /// </param>
 void IWriteableCodeUnit.SetParent(ICodePart parentCodeUnit)
 {
     Param.Ignore(parentCodeUnit);
     this.parent = parentCodeUnit;
 }
Exemplo n.º 12
0
 public ChildLink(ICodePart parent, ICodePart child) : base(parent, child)
 {
     Parent = parent;
     Child  = child;
 }
Exemplo n.º 13
0
 /// <summary>
 /// Sets the parent of this code unit.
 /// </summary>
 /// <param name="parentCodeUnit">
 /// The parent.
 /// </param>
 void IWriteableCodeUnit.SetParent(ICodePart parentCodeUnit)
 {
     Param.Ignore(parentCodeUnit);
     this.parent = parentCodeUnit;
 }