Exemplo n.º 1
0
            public void Run()
            {
                var cls = item as ITypeDefinition;

                if (cls != null && cls.DirectBaseTypes != null)
                {
                    foreach (var bt in cls.DirectBaseTypes)
                    {
                        var def = bt.GetDefinition();
                        if (def != null && def.Kind != TypeKind.Interface && !def.Region.IsEmpty)
                        {
                            IdeApp.Workbench.OpenDocument(def.Region.FileName, def.Region.BeginLine, def.Region.BeginColumn);
                            return;
                        }
                    }
                }

                var method = item as IMember;

                if (method != null)
                {
                    var baseMethod = InheritanceHelper.GetBaseMember(method);
                    if (baseMethod != null)
                    {
                        IdeApp.Workbench.OpenDocument(baseMethod.Region.FileName, baseMethod.Region.BeginLine, baseMethod.Region.EndLine);
                    }
                    return;
                }
            }
Exemplo n.º 2
0
        private static NameRule[] GetVirtualMemberRules(NameSemantic semantic)
        {
            if (semantic.Entity is IMember member)
            {
                if (member.IsOverride)
                {
                    var baseMember   = InheritanceHelper.GetBaseMember(member);
                    var baseSemantic = NameSemantic.Create(baseMember, semantic.Emitter);
                    //do not remove baseName, it calculates AppliedRule
                    var baseName = baseSemantic.Name;
                    if (baseSemantic.AppliedRule != null)
                    {
                        return(new[] { baseSemantic.AppliedRule });
                    }
                }

                if (member.ImplementedInterfaceMembers.Count > 0)
                {
                    var interfaceMember = member.ImplementedInterfaceMembers.First();
                    return(NameConvertor.GetClassRules(new NameSemantic {
                        Emitter = semantic.Emitter
                    }, interfaceMember.DeclaringTypeDefinition));
                }
            }

            return(null);
        }
Exemplo n.º 3
0
            public void Run()
            {
                var cls = item as ITypeDefinition;

                if (cls != null && cls.DirectBaseTypes != null)
                {
                    foreach (var bt in cls.DirectBaseTypes)
                    {
                        var def = bt.GetDefinition();
                        if (def != null && def.Kind != TypeKind.Interface)
                        {
                            IdeApp.ProjectOperations.JumpToDeclaration(def);
                            return;
                        }
                    }
                }

                var method = item as IMember;

                if (method != null)
                {
                    var baseMethod = InheritanceHelper.GetBaseMember(method);
                    if (baseMethod != null)
                    {
                        IdeApp.ProjectOperations.JumpToDeclaration(baseMethod);
                    }
                    return;
                }
            }
Exemplo n.º 4
0
        public static IAttribute GetInheritedAttribute(IMember member, string attrName)
        {
            foreach (var attr in member.Attributes)
            {
                if (attr.AttributeType.FullName == attrName)
                {
                    return attr;
                }
            }

            if (member.IsOverride)
            {
                member = InheritanceHelper.GetBaseMember(member);

                if (member != null)
                {
                    return Helpers.GetInheritedAttribute(member, attrName);
                }
            }
            else if (member.ImplementedInterfaceMembers != null && member.ImplementedInterfaceMembers.Count > 0)
            {
                foreach (var interfaceMember in member.ImplementedInterfaceMembers)
                {
                    var attr = Helpers.GetInheritedAttribute(interfaceMember, attrName);
                    if (attr != null)
                    {
                        return attr;
                    }
                }
            }

            return null;
        }
 bool IsInactiveConditionalMethod(IParameterizedMember member)
 {
     if (member.EntityType != EntityType.Method || member.ReturnType.Kind != TypeKind.Void)
     {
         return(false);
     }
     while (member.IsOverride)
     {
         member = (IParameterizedMember)InheritanceHelper.GetBaseMember(member);
     }
     return(IsInactiveConditional(member.Attributes));
 }
Exemplo n.º 6
0
        protected virtual int GetIndex(IMember member)
        {
            var originalMember = member;

            while (member != null && member.IsOverride && !this.IsTemplateOverride(member))
            {
                member = InheritanceHelper.GetBaseMember(member);
            }

            if (member == null)
            {
                member = originalMember;
            }

            return(this.Members.IndexOf(member.MemberDefinition));
        }
Exemplo n.º 7
0
            void Check(EntityDeclaration entity)
            {
                if (!entity.HasModifier(Modifiers.Override))
                {
                    return;
                }
                var rr = ctx.Resolve(entity) as MemberResolveResult;

                if (rr == null || rr.IsError)
                {
                    return;
                }
                var method = rr.Member as IParameterizedMember;

                if (method == null)
                {
                    return;
                }
                var baseMethod = InheritanceHelper.GetBaseMember(method) as IParameterizedMember;

                if (baseMethod == null)
                {
                    return;
                }

                for (int i = 0; i < Math.Min(method.Parameters.Count, baseMethod.Parameters.Count); i++)
                {
                    var arg     = method.Parameters[i];
                    var baseArg = baseMethod.Parameters[i];

                    if (arg.Name != baseArg.Name)
                    {
                        int _i         = i;
                        var parameters = entity.GetChildrenByRole(Roles.Parameter);
                        AddIssue(new CodeIssue(
                                     parameters.ElementAt(_i).NameToken,
                                     ctx.TranslateString("Parameter name differs in base method declaration"),
                                     string.Format(ctx.TranslateString("Rename to '{0}'"), baseArg.Name),
                                     s => {
                            s.Rename(arg, baseArg.Name);
                        }
                                     ));
                    }
                }
            }
Exemplo n.º 8
0
            public override void VisitMethodDeclaration(MethodDeclaration methodDeclaration)
            {
                if (!methodDeclaration.HasModifier(Modifiers.Override))
                {
                    return;
                }
                var lastParam = methodDeclaration.Parameters.LastOrDefault();

                if (lastParam == null || lastParam.ParameterModifier != ParameterModifier.Params)
                {
                    return;
                }
                var type = lastParam.Type as ComposedType;

                if (type == null || !type.ArraySpecifiers.Any())
                {
                    return;
                }
                var rr = ctx.Resolve(methodDeclaration) as MemberResolveResult;

                if (rr == null)
                {
                    return;
                }
                var baseMember = InheritanceHelper.GetBaseMember(rr.Member) as IMethod;

                if (baseMember == null || baseMember.Parameters.Count == 0 || baseMember.Parameters.Last().IsParams)
                {
                    return;
                }
                AddIssue(new CodeIssue(
                             lastParam.GetChildByRole(ParameterDeclaration.ParamsModifierRole),
                             ctx.TranslateString("'params' is always ignored in overrides"),
                             ctx.TranslateString("Remove 'params' modifier"),
                             script => {
                    var p = (ParameterDeclaration)lastParam.Clone();
                    p.ParameterModifier = ParameterModifier.None;
                    script.Replace(lastParam, p);
                }
                             )
                {
                    IssueMarker = IssueMarker.GrayOut
                });
            }
Exemplo n.º 9
0
        private static CompilerRule[] GetVirtualMemberRules(IEmitter emitter, IEntity entity)
        {
            if (entity is IMember member)
            {
                if (member.IsOverride)
                {
                    var baseMember = InheritanceHelper.GetBaseMember(member);
                    return(new[] { Rules.Get(emitter, baseMember) });
                }

                if (member.ImplementedInterfaceMembers.Count > 0)
                {
                    var interfaceMember = member.ImplementedInterfaceMembers.First();
                    return(Rules.GetClassRules(emitter, interfaceMember.DeclaringTypeDefinition));
                }
            }

            return(null);
        }
Exemplo n.º 10
0
            void CheckTypeCast(Expression typeCastNode, Expression expr, TextLocation castStart, TextLocation castEnd)
            {
                var outerTypeCastNode = typeCastNode;

                while (outerTypeCastNode.Parent != null && outerTypeCastNode.Parent is ParenthesizedExpression)
                {
                    outerTypeCastNode = (Expression)outerTypeCastNode.Parent;
                }
                IMember accessingMember;
                var     expectedType = GetExpectedType(outerTypeCastNode, out accessingMember);
                var     exprType     = ctx.Resolve(expr).Type;

                if (expectedType.Kind == TypeKind.Interface && IsExplicitImplementation(exprType, expectedType, outerTypeCastNode))
                {
                    return;
                }
                var baseTypes = exprType.GetAllBaseTypes().ToList();

                if (!baseTypes.Any(t => t.Equals(expectedType)))
                {
                    return;
                }

                // check if the called member doesn't change it's virtual slot when changing types
                if (accessingMember != null)
                {
                    var baseMember = InheritanceHelper.GetBaseMember(accessingMember);
                    foreach (var bt in baseTypes)
                    {
                        foreach (var member in bt.GetMembers(m => m.Name == accessingMember.Name))
                        {
                            if (InheritanceHelper.GetBaseMember(member) != baseMember)
                            {
                                return;
                            }
                        }
                    }
                }
                AddIssue(outerTypeCastNode, typeCastNode, expr, castStart, castEnd);
            }
Exemplo n.º 11
0
            public override void VisitMethodDeclaration(MethodDeclaration methodDeclaration)
            {
                if (!methodDeclaration.HasModifier(Modifiers.Override))
                {
                    return;
                }
                var lastParam = methodDeclaration.Parameters.LastOrDefault();

                if (lastParam == null || lastParam.ParameterModifier == ParameterModifier.Params)
                {
                    return;
                }
                var type = lastParam.Type as ComposedType;

                if (type == null || !type.ArraySpecifiers.Any())
                {
                    return;
                }
                var rr = ctx.Resolve(methodDeclaration) as MemberResolveResult;

                if (rr == null)
                {
                    return;
                }
                var baseMember = InheritanceHelper.GetBaseMember(rr.Member) as IMethod;

                if (baseMember == null || baseMember.Parameters.Count == 0 || !baseMember.Parameters.Last().IsParams)
                {
                    return;
                }
                AddIssue(
                    lastParam.NameToken,
                    string.Format(ctx.TranslateString("Base method '{0}' has a 'params' modifier"), baseMember.FullName),
                    ctx.TranslateString("Add 'params' modifier"),
                    script => {
                    script.ChangeModifier(lastParam, ParameterModifier.Params);
                }
                    );
            }
Exemplo n.º 12
0
        protected override CodeAction GetAction(RefactoringContext context, MethodDeclaration node)
        {
            if (node == null || !node.HasModifier(Modifiers.Override))
            {
                return(null);
            }
            if (!node.NameToken.Contains(context.Location))
            {
                return(null);
            }

            IMethod resolvedMember = (IMethod)(context.Resolve(node) as MemberResolveResult).Member;

            if (resolvedMember == null)
            {
                return(null);
            }

            IMethod originalMember = (IMethod)InheritanceHelper.GetBaseMember(resolvedMember);

            if (originalMember == null || originalMember.Documentation == null)
            {
                return(null);
            }
            string comments = originalMember.Documentation.ToString();

            if (string.IsNullOrEmpty(comments))
            {
                return(null);
            }

            string[] lines = comments.Split(new string[] { Environment.NewLine }, StringSplitOptions.None);
            return(new CodeAction(context.TranslateString("Copy comments from base"), script => {
                foreach (string co in lines)
                {
                    script.InsertBefore(node, new Comment(co, CommentType.Documentation));
                }
            }, node.NameToken));
        }
Exemplo n.º 13
0
        public static IAttribute GetInheritedAttribute(IMember member, string attrName)
        {
            foreach (var attr in member.Attributes)
            {
                if (attr.AttributeType.FullName == attrName)
                {
                    return(attr);
                }
            }

            if (member.IsOverride)
            {
                member = InheritanceHelper.GetBaseMember(member);

                if (member != null)
                {
                    return(Helpers.GetInheritedAttribute(member, attrName));
                }
            }

            return(null);
        }
Exemplo n.º 14
0
        public static string GetMethodName(IMethod method)
        {
            if (!method.IsPublic)
            {
                return(null);
            }

            string           name             = null;
            MethodDefinition methodDefinition = GetMethodDefinition(method);

            if (methodDefinition != null)
            {
                var info = methods_.GetOrDefault(methodDefinition);
                if (info != null)
                {
                    name = info.Name;
                }
            }
            if (name == null)
            {
                if (method.IsOverride)
                {
                    IMethod baseMethod = (IMethod)InheritanceHelper.GetBaseMember(method);
                    name = GetMethodName(baseMethod);
                }
                else if (method.ImplementedInterfaceMembers.Count > 0)
                {
                    foreach (IMethod baseMethod in method.ImplementedInterfaceMembers)
                    {
                        name = GetMethodName(baseMethod);
                        if (name != null)
                        {
                            break;
                        }
                    }
                }
            }
            return(name);
        }
Exemplo n.º 15
0
            void CheckAutomaticToStringCallers(InvocationExpression invocationExpression, IMember member)
            {
                if (member.IsOverride)
                {
                    member = InheritanceHelper.GetBaseMember(member);
                    if (member == null)
                    {
                        return;
                    }
                }
                var key = new Tuple <string, int>(member.ReflectionName, invocationExpression.Arguments.Count);
                Tuple <int, int> checkInfo;

                if (membersCallingToString.TryGetValue(key, out checkInfo))
                {
                    var arguments = invocationExpression.Arguments.ToList();
                    for (int i = checkInfo.Item1; i < Math.Min(invocationExpression.Arguments.Count, checkInfo.Item2 + 1); ++i)
                    {
                        CheckExpressionInAutoCallContext(arguments[i]);
                    }
                }
            }
Exemplo n.º 16
0
        public virtual bool IsTemplateOverride(IMember member)
        {
            if (member.IsOverride)
            {
                member = InheritanceHelper.GetBaseMember(member);

                if (member != null)
                {
                    var  inline   = this.Emitter.GetInline(member);
                    bool isInline = !string.IsNullOrWhiteSpace(inline);
                    if (isInline)
                    {
                        if (member.IsOverride)
                        {
                            return(this.IsTemplateOverride(member));
                        }
                        return(true);
                    }
                }
            }

            return(false);
        }
Exemplo n.º 17
0
        protected virtual List <IProperty> GetPropertyOverloads(List <IProperty> list = null, ITypeDefinition typeDef = null)
        {
            typeDef = typeDef ?? this.TypeDefinition;

            bool isTop = list == null;

            list = list ?? new List <IProperty>();

            if (this.Member != null && this.Member.IsOverride && !this.IsTemplateOverride(this.Member))
            {
                if (this.OriginalMember == null)
                {
                    this.OriginalMember = this.Member;
                }

                this.Member = InheritanceHelper.GetBaseMember(this.Member);
                typeDef     = this.Member.DeclaringTypeDefinition;
            }

            if (typeDef != null)
            {
                bool isMember   = this.Member is IMethod;
                var  properties = typeDef.Properties.Where(p =>
                {
                    if (p.IsExplicitInterfaceImplementation)
                    {
                        return(false);
                    }

                    var canGet = p.CanGet && p.Getter != null;
                    var canSet = p.CanSet && p.Setter != null;

                    if (!this.IncludeInline)
                    {
                        var inline = canGet ? this.Emitter.GetInline(p.Getter) : null;
                        if (!string.IsNullOrWhiteSpace(inline))
                        {
                            return(false);
                        }

                        inline = canSet ? this.Emitter.GetInline(p.Setter) : null;
                        if (!string.IsNullOrWhiteSpace(inline))
                        {
                            return(false);
                        }

                        if (p.IsIndexer && canGet && p.Getter.Attributes.Any(a => a.AttributeType.FullName == "H5.ExternalAttribute"))
                        {
                            return(false);
                        }
                    }

                    bool eq             = false;
                    bool?equalsByGetter = null;

                    if (p.IsStatic == this.Static)
                    {
                        var fieldName = this.Emitter.GetEntityName(p);

                        if (fieldName != null && (fieldName == this.JsName || fieldName == this.AltJsName || fieldName == this.FieldJsName))
                        {
                            eq = true;
                        }

                        if (!eq && p.IsIndexer)
                        {
                            var getterIgnore = canGet && this.Emitter.Validator.IsExternalType(p.Getter);
                            var setterIgnore = canSet && this.Emitter.Validator.IsExternalType(p.Setter);
                            var getterName   = canGet ? Helpers.GetPropertyRef(p, this.Emitter, false, true, true) : null;
                            var setterName   = canSet ? Helpers.GetPropertyRef(p, this.Emitter, true, true, true) : null;

                            if (!getterIgnore && getterName != null && (getterName == this.JsName || getterName == this.AltJsName || getterName == this.FieldJsName))
                            {
                                eq             = true;
                                equalsByGetter = true;
                            }
                            else if (!setterIgnore && setterName != null && (setterName == this.JsName || setterName == this.AltJsName || setterName == this.FieldJsName))
                            {
                                eq             = true;
                                equalsByGetter = false;
                            }
                        }
                    }

                    if (eq)
                    {
                        if (p.IsOverride && !this.IsTemplateOverride(p))
                        {
                            return(false);
                        }

                        if (equalsByGetter.HasValue && isMember && this.AltJsName == null)
                        {
                            this.AltJsName = Helpers.GetPropertyRef(p, this.Emitter, equalsByGetter.Value, true, true);
                        }

                        return(true);
                    }

                    return(false);
                });

                list.AddRange(properties);

                if (this.Inherit)
                {
                    var baseTypeDefinitions = typeDef.DirectBaseTypes.Where(t => t.Kind == typeDef.Kind || (typeDef.Kind == TypeKind.Struct && t.Kind == TypeKind.Class));

                    foreach (var baseTypeDef in baseTypeDefinitions)
                    {
                        list = this.GetPropertyOverloads(list, baseTypeDef.GetDefinition());
                    }
                }
            }

            var returnProperties = isTop ? list.Distinct().ToList() : list;

            return(returnProperties);
        }
Exemplo n.º 18
0
            public override void VisitIndexerDeclaration(IndexerDeclaration indexerDeclaration)
            {
                base.VisitIndexerDeclaration(indexerDeclaration);

                if (!indexerDeclaration.HasModifier(Modifiers.Override))
                {
                    return;
                }

                bool hasGetter = !indexerDeclaration.Getter.IsNull;
                bool hasSetter = !indexerDeclaration.Setter.IsNull;

                if (!hasGetter && !hasSetter)
                {
                    return;
                }

                if (hasGetter && indexerDeclaration.Getter.Body.Statements.Count != 1)
                {
                    return;
                }

                if (hasSetter && indexerDeclaration.Setter.Body.Statements.Count != 1)
                {
                    return;
                }

                var resultIndexer = ctx.Resolve(indexerDeclaration) as MemberResolveResult;

                if (resultIndexer == null)
                {
                    return;
                }
                var baseIndexer = InheritanceHelper.GetBaseMember(resultIndexer.Member) as IProperty;

                if (baseIndexer == null)
                {
                    return;
                }

                bool hasBaseGetter = (baseIndexer.Getter != null);
                bool hasBaseSetter = (baseIndexer.Setter != null);

                if (hasBaseGetter)
                {
                    if (hasGetter)
                    {
                        var expr = indexerDeclaration.Getter.Body.Statements.FirstOrNullObject() as ReturnStatement;

                        if (expr == null)
                        {
                            return;
                        }

                        Expression indexerExpression = expr.Expression;

                        if (indexerExpression == null ||
                            !(indexerExpression.FirstChild is BaseReferenceExpression))
                        {
                            return;
                        }
                    }
                }

                if (hasBaseSetter)
                {
                    if (hasSetter)
                    {
                        var match = setterPattern.Match(indexerDeclaration.Setter.Body.Statements.FirstOrNullObject());
                        if (!match.Success)
                        {
                            return;
                        }
                        var memberReferenceExpression = match.Get("left").Single() as IndexerExpression;
                        if (memberReferenceExpression == null ||
                            !(memberReferenceExpression.FirstChild is BaseReferenceExpression))
                        {
                            return;
                        }
                    }
                }

                var title = ctx.TranslateString("Redundant indexer override");

                AddIssue(new CodeIssue(indexerDeclaration, title, ctx.TranslateString("Remove redundant indexer override"), script => script.Remove(indexerDeclaration))
                {
                    IssueMarker = IssueMarker.GrayOut
                });
            }
Exemplo n.º 19
0
        protected bool EmitMemberAlias(IMember member, IMember interfaceMember)
        {
            bool nonEmpty = false;

            if (member.IsShadowing || !member.IsOverride)
            {
                var baseMember = InheritanceHelper.GetBaseMember(member);

                if (baseMember != null && baseMember.ImplementedInterfaceMembers.Contains(interfaceMember))
                {
                    return(false);
                }
            }

            if (member is IProperty)
            {
                var property = (IProperty)member;
                if (property.CanGet)
                {
                    nonEmpty = true;
                    this.EnsureComma();
                    this.WriteScript(Helpers.GetPropertyRef(member, this.Emitter, false, false, false, true));
                    this.WriteComma();
                    var alias = Helpers.GetPropertyRef(interfaceMember, this.Emitter, false, false, false);

                    if (alias.StartsWith("\""))
                    {
                        this.Write(alias);
                    }
                    else
                    {
                        this.WriteScript(alias);
                    }

                    this.Emitter.Comma = true;
                }

                if (property.CanSet)
                {
                    nonEmpty = true;
                    this.EnsureComma();
                    this.WriteScript(Helpers.GetPropertyRef(member, this.Emitter, true, false, false, true));
                    this.WriteComma();
                    var alias = Helpers.GetPropertyRef(interfaceMember, this.Emitter, true, false, false);

                    if (alias.StartsWith("\""))
                    {
                        this.Write(alias);
                    }
                    else
                    {
                        this.WriteScript(alias);
                    }

                    this.Emitter.Comma = true;
                }
            }
            else if (member is IEvent)
            {
                var ev = (IEvent)member;
                if (ev.CanAdd)
                {
                    nonEmpty = true;
                    this.EnsureComma();
                    this.WriteScript(Helpers.GetEventRef(member, this.Emitter, false, false, false, true));
                    this.WriteComma();
                    var alias = Helpers.GetEventRef(interfaceMember, this.Emitter, false, false, false);

                    if (alias.StartsWith("\""))
                    {
                        this.Write(alias);
                    }
                    else
                    {
                        this.WriteScript(alias);
                    }

                    this.Emitter.Comma = true;
                }

                if (ev.CanRemove)
                {
                    nonEmpty = true;
                    this.EnsureComma();
                    this.WriteScript(Helpers.GetEventRef(member, this.Emitter, true, false, false, true));
                    this.WriteComma();
                    var alias = Helpers.GetEventRef(interfaceMember, this.Emitter, true, false, false);

                    if (alias.StartsWith("\""))
                    {
                        this.Write(alias);
                    }
                    else
                    {
                        this.WriteScript(alias);
                    }

                    this.Emitter.Comma = true;
                }
            }
            else
            {
                nonEmpty = true;
                this.EnsureComma();
                this.WriteScript(OverloadsCollection.Create(Emitter, member).GetOverloadName(false, null, true));
                this.WriteComma();
                var alias = OverloadsCollection.Create(Emitter, interfaceMember).GetOverloadName();

                if (alias.StartsWith("\""))
                {
                    this.Write(alias);
                }
                else
                {
                    this.WriteScript(alias);
                }
            }

            this.Emitter.Comma = true;
            return(nonEmpty);
        }
Exemplo n.º 20
0
        protected bool EmitMemberAlias(IMember member, IMember interfaceMember)
        {
            bool nonEmpty = false;

            if (member.IsShadowing || !member.IsOverride)
            {
                var baseMember = InheritanceHelper.GetBaseMember(member);

                if (baseMember != null && baseMember.ImplementedInterfaceMembers.Contains(interfaceMember))
                {
                    return(false);
                }
            }

            var excludeTypeParam      = OverloadsCollection.ExcludeTypeParameterForDefinition(member);
            var excludeAliasTypeParam = member.IsExplicitInterfaceImplementation && !excludeTypeParam;
            var pair     = false;
            var itypeDef = interfaceMember.DeclaringTypeDefinition;

            if (!member.IsExplicitInterfaceImplementation &&
                MetadataUtils.IsJsGeneric(itypeDef, this.Emitter) &&
                itypeDef.TypeParameters != null &&
                itypeDef.TypeParameters.Any(typeParameter => typeParameter.Variance != VarianceModifier.Invariant))
            {
                pair = true;
            }

            if (member is IProperty && ((IProperty)member).IsIndexer)
            {
                var property = (IProperty)member;
                if (property.CanGet)
                {
                    nonEmpty = true;
                    this.EnsureComma();
                    this.WriteScript(Helpers.GetPropertyRef(member, this.Emitter, false, false, false, excludeTypeParam));
                    this.WriteComma();
                    var alias = Helpers.GetPropertyRef(interfaceMember, this.Emitter, false, false, false, withoutTypeParams: excludeAliasTypeParam);

                    if (pair)
                    {
                        this.WriteOpenBracket();
                    }

                    if (alias.StartsWith("\""))
                    {
                        this.Write(alias);
                    }
                    else
                    {
                        this.WriteScript(alias);
                    }

                    if (pair)
                    {
                        this.WriteComma();
                        this.WriteScript(Helpers.GetPropertyRef(interfaceMember, this.Emitter, withoutTypeParams: true));
                        this.WriteCloseBracket();
                    }

                    this.Emitter.Comma = true;
                }

                if (property.CanSet)
                {
                    nonEmpty = true;
                    this.EnsureComma();
                    this.WriteScript(Helpers.GetPropertyRef(member, this.Emitter, true, false, false, excludeTypeParam));
                    this.WriteComma();
                    var alias = Helpers.GetPropertyRef(interfaceMember, this.Emitter, true, false, false, withoutTypeParams: excludeAliasTypeParam);

                    if (pair)
                    {
                        this.WriteOpenBracket();
                    }

                    if (alias.StartsWith("\""))
                    {
                        this.Write(alias);
                    }
                    else
                    {
                        this.WriteScript(alias);
                    }

                    if (pair)
                    {
                        this.WriteComma();
                        this.WriteScript(Helpers.GetPropertyRef(interfaceMember, this.Emitter, true, withoutTypeParams: true));
                        this.WriteCloseBracket();
                    }

                    this.Emitter.Comma = true;
                }
            }
            else if (member is IEvent)
            {
                var ev = (IEvent)member;
                if (ev.CanAdd)
                {
                    nonEmpty = true;
                    this.EnsureComma();
                    this.WriteScript(Helpers.GetEventRef(member, this.Emitter, false, false, false, excludeTypeParam));
                    this.WriteComma();
                    var alias = Helpers.GetEventRef(interfaceMember, this.Emitter, false, false, false, excludeAliasTypeParam);

                    if (pair)
                    {
                        this.WriteOpenBracket();
                    }

                    if (alias.StartsWith("\""))
                    {
                        this.Write(alias);
                    }
                    else
                    {
                        this.WriteScript(alias);
                    }

                    if (pair)
                    {
                        this.WriteComma();
                        this.WriteScript(Helpers.GetEventRef(interfaceMember, this.Emitter, withoutTypeParams: true));
                        this.WriteCloseBracket();
                    }

                    this.Emitter.Comma = true;
                }

                if (ev.CanRemove)
                {
                    nonEmpty = true;
                    this.EnsureComma();
                    this.WriteScript(Helpers.GetEventRef(member, this.Emitter, true, false, false, excludeTypeParam));
                    this.WriteComma();
                    var alias = Helpers.GetEventRef(interfaceMember, this.Emitter, true, false, false, excludeAliasTypeParam);

                    if (pair)
                    {
                        this.WriteOpenBracket();
                    }

                    if (alias.StartsWith("\""))
                    {
                        this.Write(alias);
                    }
                    else
                    {
                        this.WriteScript(alias);
                    }

                    if (pair)
                    {
                        this.WriteComma();
                        this.WriteScript(Helpers.GetEventRef(interfaceMember, this.Emitter, true, withoutTypeParams: true));
                        this.WriteCloseBracket();
                    }

                    this.Emitter.Comma = true;
                }
            }
            else
            {
                nonEmpty = true;
                this.EnsureComma();
                this.WriteScript(OverloadsCollection.Create(Emitter, member).GetOverloadName(false, null, excludeTypeParam));
                this.WriteComma();
                var alias = OverloadsCollection.Create(Emitter, interfaceMember).GetOverloadName(withoutTypeParams: excludeAliasTypeParam);

                if (pair)
                {
                    this.WriteOpenBracket();
                }

                if (alias.StartsWith("\""))
                {
                    this.Write(alias);
                }
                else
                {
                    this.WriteScript(alias);
                }

                if (pair)
                {
                    this.WriteComma();
                    this.WriteScript(OverloadsCollection.Create(Emitter, interfaceMember).GetOverloadName(withoutTypeParams: true));
                    this.WriteCloseBracket();
                }
            }

            this.Emitter.Comma = true;
            return(nonEmpty);
        }
Exemplo n.º 21
0
            void CheckTypeCast(Expression typeCastNode, Expression expr, TextLocation castStart, TextLocation castEnd)
            {
                var outerTypeCastNode = typeCastNode;

                while (outerTypeCastNode.Parent is ParenthesizedExpression)
                {
                    outerTypeCastNode = (Expression)outerTypeCastNode.Parent;
                }

                IMember accessingMember;
                var     expectedType = GetExpectedType(outerTypeCastNode, out accessingMember);
                var     exprType     = ctx.Resolve(expr).Type;

                if (expectedType.Kind == TypeKind.Interface && IsExplicitImplementation(exprType, expectedType, outerTypeCastNode))
                {
                    return;
                }
                var baseTypes = exprType.GetAllBaseTypes().ToList();

                if (!baseTypes.Any(t => t.Equals(expectedType)))
                {
                    return;
                }

                if (IsBreaking(exprType, expectedType))
                {
                    return;
                }

                var cond = outerTypeCastNode.Parent as ConditionalExpression;

                if (cond != null)
                {
                    if (outerTypeCastNode == cond.TrueExpression)
                    {
                        var rr = ctx.Resolve(cond.FalseExpression).Type;
                        if (rr != exprType)
                        {
                            return;
                        }
                    }
                }

                var bop = outerTypeCastNode.Parent as BinaryOperatorExpression;

                if (bop != null && IsRedundantInBinaryExpression(bop, outerTypeCastNode, exprType))
                {
                    return;
                }

                // check if the called member doesn't change it's virtual slot when changing types
                if (accessingMember != null)
                {
                    var baseMember = InheritanceHelper.GetBaseMember(accessingMember);
                    foreach (var bt in baseTypes)
                    {
                        foreach (var member in bt.GetMembers(m => m.Name == accessingMember.Name))
                        {
                            if (InheritanceHelper.GetBaseMember(member) != baseMember)
                            {
                                return;
                            }
                        }
                    }
                }

                var mrr = ctx.Resolve(typeCastNode.Parent) as CSharpInvocationResolveResult;

                if (mrr != null)
                {
                    if (mrr.Member.SymbolKind == SymbolKind.Constructor)
                    {
                        int nArg = typeCastNode.Parent.Children
                                   .Where(n => n.Role == Roles.Argument)
                                   .TakeWhile(n => n.DescendantNodesAndSelf().All(c => c != typeCastNode))
                                   .Count();

                        if (IsRequiredToSelectOverload(mrr.Member.DeclaringTypeDefinition.GetConstructors(), expectedType, nArg))
                        {
                            return;
                        }
                    }
                    else if (mrr.Member.SymbolKind == SymbolKind.Method)
                    {
                        int nArg = typeCastNode.Parent.Children
                                   .Where(n => n.Role == Roles.Argument)
                                   .TakeWhile(n => n.DescendantNodesAndSelf().All(c => c != typeCastNode))
                                   .Count();
                        if (IsRequiredToSelectOverload(mrr.Member.DeclaringTypeDefinition.GetMethods(m => m.Name == mrr.Member.Name), expectedType, nArg))
                        {
                            return;
                        }
                    }
                }

                AddIssue(outerTypeCastNode, typeCastNode, expr, castStart, castEnd);
            }
Exemplo n.º 22
0
 public static IMethod GetBaseMethod(this IMethod me)
 {
     return((IMethod)InheritanceHelper.GetBaseMember(me));
 }
Exemplo n.º 23
0
 public static IMember GetBaseMember(this IMember me)
 {
     return(InheritanceHelper.GetBaseMember(me));
 }
Exemplo n.º 24
0
            public override void VisitPropertyDeclaration(PropertyDeclaration propertyDeclaration)
            {
                base.VisitPropertyDeclaration(propertyDeclaration);

                if (!propertyDeclaration.HasModifier(Modifiers.Override))
                {
                    return;
                }

                bool hasGetter = !propertyDeclaration.Getter.IsNull;
                bool hasSetter = !propertyDeclaration.Setter.IsNull;

                if (!hasGetter && !hasSetter)
                {
                    return;
                }

                if (hasGetter && propertyDeclaration.Getter.Body.Statements.Count != 1)
                {
                    return;
                }

                if (hasSetter && propertyDeclaration.Setter.Body.Statements.Count != 1)
                {
                    return;
                }

                var resultProperty = ctx.Resolve(propertyDeclaration) as MemberResolveResult;

                if (resultProperty == null)
                {
                    return;
                }
                var baseProperty = InheritanceHelper.GetBaseMember(resultProperty.Member) as IProperty;

                if (baseProperty == null)
                {
                    return;
                }

                bool hasBaseGetter = baseProperty.Getter != null;
                bool hasBaseSetter = baseProperty.Setter != null;

                if (hasBaseGetter)
                {
                    if (hasGetter)
                    {
                        var expr = propertyDeclaration.Getter.Body.Statements.FirstOrNullObject();

                        if (expr == null || !(expr is ReturnStatement))
                        {
                            return;
                        }

                        var memberReferenceExpression = (expr as ReturnStatement).Expression as MemberReferenceExpression;

                        if (memberReferenceExpression == null ||
                            memberReferenceExpression.MemberName != propertyDeclaration.Name ||
                            !(memberReferenceExpression.FirstChild is BaseReferenceExpression))
                        {
                            return;
                        }
                    }
                }

                if (hasBaseSetter)
                {
                    if (hasSetter)
                    {
                        var match = setterPattern.Match(propertyDeclaration.Setter.Body.Statements.FirstOrNullObject());
                        if (!match.Success)
                        {
                            return;
                        }
                        var memberReferenceExpression = match.Get("left").Single() as MemberReferenceExpression;
                        if (memberReferenceExpression == null ||
                            memberReferenceExpression.MemberName != propertyDeclaration.Name ||
                            !(memberReferenceExpression.FirstChild is BaseReferenceExpression))
                        {
                            return;
                        }
                    }
                }

                var title = ctx.TranslateString("Redundant property override");

                AddIssue(new CodeIssue(propertyDeclaration, title, ctx.TranslateString("Remove redundant property override"), script => script.Remove(propertyDeclaration))
                {
                    IssueMarker = IssueMarker.GrayOut
                });
            }
Exemplo n.º 25
0
        protected virtual List <IMethod> GetMethodOverloads(List <IMethod> list = null, ITypeDefinition typeDef = null)
        {
            typeDef = typeDef ?? this.TypeDefinition;

            bool isTop = list == null;

            list = list ?? new List <IMethod>();
            var toStringOverride = (this.JsName == "toString" && this.Member is IMethod && ((IMethod)this.Member).Parameters.Count == 0);

            if (this.Member != null && this.Member.IsOverride && (!this.IsTemplateOverride(this.Member) || toStringOverride))
            {
                if (this.OriginalMember == null)
                {
                    this.OriginalMember = this.Member;
                }

                this.Member = InheritanceHelper.GetBaseMember(this.Member);
                typeDef     = this.Member.DeclaringTypeDefinition;
            }

            if (typeDef != null)
            {
                var  isExternalType = this.Emitter.Validator.IsExternalType(typeDef);
                bool externalFound  = false;

                var oldIncludeInline = this.IncludeInline;
                if (toStringOverride)
                {
                    this.IncludeInline = true;
                }

                var methods = typeDef.Methods.Where(m =>
                {
                    if (m.IsExplicitInterfaceImplementation)
                    {
                        return(false);
                    }

                    if (!this.IncludeInline)
                    {
                        var inline = this.Emitter.GetInline(m);
                        if (!string.IsNullOrWhiteSpace(inline) && !(m.Name == "ToString" && m.Parameters.Count == 0 && !m.IsOverride))
                        {
                            return(false);
                        }
                    }

                    var name = this.Emitter.GetEntityName(m);
                    if ((name == this.JsName || name == this.AltJsName || name == this.FieldJsName) && m.IsStatic == this.Static &&
                        (m.IsConstructor && this.JsName == JS.Funcs.CONSTRUCTOR || m.IsConstructor == this.Constructor))
                    {
                        if (m.IsConstructor != this.Constructor && (m.Parameters.Count > 0 || m.DeclaringTypeDefinition != this.TypeDefinition))
                        {
                            return(false);
                        }

                        if (m.IsOverride && (!this.IsTemplateOverride(m) || m.Name == "ToString" && m.Parameters.Count == 0))
                        {
                            return(false);
                        }

                        if (!isExternalType)
                        {
                            var isExtern = !m.HasBody && !m.IsAbstract || this.Emitter.Validator.IsExternalType(m);
                            if (isExtern)
                            {
                                return(false);
                            }
                        }
                        else
                        {
                            if (externalFound)
                            {
                                return(false);
                            }

                            externalFound = true;
                        }

                        return(true);
                    }

                    return(false);
                });

                this.IncludeInline = oldIncludeInline;

                list.AddRange(methods);

                if (this.Inherit)
                {
                    var baseTypeDefinitions = typeDef.DirectBaseTypes.Where(t => t.Kind == typeDef.Kind || (typeDef.Kind == TypeKind.Struct && t.Kind == TypeKind.Class));

                    foreach (var baseTypeDef in baseTypeDefinitions)
                    {
                        list = this.GetMethodOverloads(list, baseTypeDef.GetDefinition());
                    }
                }
            }

            var returnMethods = isTop ? list.Distinct().ToList() : list;

            return(returnMethods);
        }