Exemplo n.º 1
0
        private void AddLostConstructor(ClassDeclaration classNode)
        {
            Constructor             constructor = classNode.GetConstructor();
            List <ClassDeclaration> baseClasses = classNode.Project.GetInheritClasses(classNode);

            if (constructor == null)
            {
                if (baseClasses.Count > 0)
                {
                    var baseClass = baseClasses.Find(c =>
                    {
                        var ctor = c.GetConstructor();
                        return(ctor != null && ctor.Parameters.Count > 0);
                    });

                    if (baseClass != null)
                    {
                        Constructor baseCtor = baseClass.GetConstructor();
                        Constructor newCtor  = (Constructor)NodeHelper.CreateNode((JObject)baseCtor.TsNode.DeepClone());

                        CallExpression baseNode = (CallExpression)NodeHelper.CreateNode(NodeKind.CallExpression);
                        baseNode.SetExpression(NodeHelper.CreateNode(NodeKind.SuperKeyword));
                        foreach (Parameter parameter in baseCtor.Parameters)
                        {
                            baseNode.AddArgument(NodeHelper.CreateNode(NodeKind.Identifier, parameter.Name.Text));
                        }

                        newCtor.SetBase(baseNode);
                        newCtor.Body.ClearStatements();
                        ModifierNormalizer.NormalizeModify(newCtor);
                        classNode.InsertMember(0, newCtor);
                    }
                }
                else if (this.GetInitProperties(classNode).Count > 0)
                {
                    Constructor newCtor = (Constructor)NodeHelper.CreateNode(BuildConstructorString());
                    ModifierNormalizer.NormalizeModify(newCtor);
                    classNode.InsertMember(0, newCtor);
                }
            }
        }