Beispiel #1
0
        private static List <NameRule> GetSpecialRules(NameSemantic semantic)
        {
            var rules = new List <NameRule>();

            var nameAttr = Helpers.GetInheritedAttribute(semantic.Entity, "H5.NameAttribute");

            if (nameAttr != null)
            {
                var rule = new NameRule();
                rule.Level = NameRuleLevel.Member;
                var value = nameAttr.PositionalArguments.First().ConstantValue;
                if (value is bool)
                {
                    rule.Notation = (bool)value ? Notation.CamelCase : Notation.None;
                }
                else if (value is string)
                {
                    rule.CustomName = (string)value;
                }
                semantic.IsCustomName = true;
                rules.Add(rule);
            }
            else
            {
                if (semantic.Entity is IMethod method && method.IsConstructor)
                {
                    semantic.IsCustomName = true;
                    rules.Add(NameConvertor.ConstructorRule);
                }
            }

            return(rules);
        }
Beispiel #2
0
        private static NameRule ToRule(IAttribute attribute, NameRuleLevel level = NameRuleLevel.None)
        {
            var rule = new NameRule();

            if (attribute.PositionalArguments.Count > 0)
            {
                rule.Notation = (Notation)(int)attribute.PositionalArguments[0].ConstantValue;
            }

            if (attribute.PositionalArguments.Count > 1)
            {
                rule.Target = (ConventionTarget)(int)attribute.PositionalArguments[1].ConstantValue;
            }

            foreach (var argument in attribute.NamedArguments)
            {
                var member = argument.Key;
                var value  = argument.Value;

                switch (member.Name)
                {
                case "Notation":
                    rule.Notation = (Notation)(int)value.ConstantValue;
                    break;

                case "Target":
                    rule.Target = (ConventionTarget)(int)value.ConstantValue;
                    break;

                case "Member":
                    rule.Member = (ConventionMember)(int)value.ConstantValue;
                    break;

                case "Accessibility":
                    rule.Accessibility = (ConventionAccessibility)(int)value.ConstantValue;
                    break;

                case "Filter":
                    rule.Filter = value.ConstantValue as string;
                    break;

                case "Priority":
                    rule.Priority = (int)value.ConstantValue;
                    break;

                default:
                    throw new NotSupportedException($"Property {member.Name} is not supported in {attribute.AttributeType.FullName}");
                }
            }

            rule.Level = level;
            return(rule);
        }
Beispiel #3
0
        private static List <NameRule> GetAttributeRules(NameSemantic semantic)
        {
            NameRule memberRule = null;

            NameRule[] classRules     = null;
            NameRule[] assemblyRules  = null;
            NameRule[] interfaceRules = null;

            if (semantic.Entity is IMember)
            {
                var attr = Helpers.GetInheritedAttribute(semantic.Entity, NameConvertor.ConventionAttrName);

                if (attr != null)
                {
                    memberRule = NameConvertor.ToRule(attr, NameRuleLevel.Member);
                }

                var typeDef = semantic.Entity.DeclaringTypeDefinition;

                if (typeDef != null)
                {
                    classRules = NameConvertor.GetClassRules(semantic, typeDef);
                }

                interfaceRules = NameConvertor.GetVirtualMemberRules(semantic);
            }
            else if (semantic.Entity is ITypeDefinition)
            {
                classRules = NameConvertor.GetClassRules(semantic, (ITypeDefinition)semantic.Entity);
            }

            var assembly = semantic.Entity.ParentAssembly;

            if (semantic.Emitter.AssemblyNameRuleCache.ContainsKey(assembly))
            {
                assemblyRules = semantic.Emitter.AssemblyNameRuleCache[assembly];
            }
            else
            {
                IAttribute[] assemblyAttrs = assembly.AssemblyAttributes.Where(a => a.AttributeType.FullName == NameConvertor.ConventionAttrName).ToArray();
                assemblyRules = new NameRule[assemblyAttrs.Length];
                for (int i = 0; i < assemblyAttrs.Length; i++)
                {
                    assemblyRules[i] = NameConvertor.ToRule(assemblyAttrs[i], NameRuleLevel.Assembly);
                }

                Array.Sort(assemblyRules, (item1, item2) => - item1.Priority.CompareTo(item2.Priority));

                semantic.Emitter.AssemblyNameRuleCache.Add(assembly, assemblyRules);
            }

            var rules = new List <NameRule>();

            if (memberRule != null)
            {
                rules.Add(memberRule);
            }

            if (classRules != null && classRules.Length > 0)
            {
                rules.AddRange(classRules);
            }

            if (interfaceRules != null && interfaceRules.Length > 0)
            {
                rules.AddRange(interfaceRules);
            }

            if (assemblyRules != null && assemblyRules.Length > 0)
            {
                rules.AddRange(assemblyRules);
            }

            return(rules);
        }
Beispiel #4
0
        private static bool IsRuleAcceptable(NameSemantic semantic, NameRule rule)
        {
            var acceptable = true;
            var entity     = semantic.Entity;

            if (rule.Target != ConventionTarget.All)
            {
                var    typeDef      = entity as ITypeDefinition;
                string externalAttr = "H5.ExternalAttribute";
                string virtualAttr  = "H5.VirtualAttribute";

                if (typeDef == null && rule.Target.HasFlag(ConventionTarget.External))
                {
                    acceptable = entity.GetAttribute(new FullTypeName(externalAttr), false) != null ||
                                 entity.GetAttribute(new FullTypeName(virtualAttr), false) != null;

                    if (!acceptable)
                    {
                        typeDef = entity.DeclaringTypeDefinition;
                    }
                }
                else if (rule.Target.HasFlag(ConventionTarget.Member) && !(entity is IMember))
                {
                    acceptable = false;
                }
                else if (rule.Target.HasFlag(ConventionTarget.Anonymous))
                {
                    if (entity is IMember && (entity.DeclaringType == null || entity.DeclaringType.Kind != TypeKind.Anonymous))
                    {
                        acceptable = false;
                    }
                    else if (entity is IType && ((IType)entity).Kind != TypeKind.Anonymous)
                    {
                        acceptable = false;
                    }
                }
                else if (typeDef == null && rule.Member != ConventionMember.All)
                {
                    typeDef = entity.DeclaringTypeDefinition;
                }

                if (typeDef != null)
                {
                    foreach (var notationType in GetFlags(rule.Target))
                    {
                        if (notationType == ConventionTarget.Member)
                        {
                            continue;
                        }

                        acceptable = NameConvertor.IsAcceptableTarget(semantic, notationType, typeDef);
                        if (acceptable)
                        {
                            break;
                        }
                    }
                }
                else if (acceptable && !rule.Target.HasFlag(ConventionTarget.Member) && !rule.Target.HasFlag(ConventionTarget.External) && !rule.Target.HasFlag(ConventionTarget.Anonymous))
                {
                    acceptable = false;
                }

                if (!acceptable)
                {
                    return(false);
                }
            }

            if (rule.Accessibility != ConventionAccessibility.All)
            {
                if (!(rule.Accessibility.HasFlag(ConventionAccessibility.Public) && entity.IsPublic ||
                      rule.Accessibility.HasFlag(ConventionAccessibility.Protected) && entity.IsProtected ||
                      rule.Accessibility.HasFlag(ConventionAccessibility.ProtectedInternal) && entity.IsProtectedOrInternal ||
                      rule.Accessibility.HasFlag(ConventionAccessibility.Private) && entity.IsPrivate ||
                      rule.Accessibility.HasFlag(ConventionAccessibility.Internal) && entity.IsInternal))
                {
                    acceptable = false;
                }

                if (!acceptable)
                {
                    return(false);
                }
            }

            if (rule.Member != ConventionMember.All)
            {
                if (entity is IField field)
                {
                    if (!(rule.Member.HasFlag(ConventionMember.Field) && !field.IsConst ||
                          rule.Member.HasFlag(ConventionMember.EnumItem) && field.IsConst && semantic.Entity.DeclaringTypeDefinition.Kind == TypeKind.Enum ||
                          rule.Member.HasFlag(ConventionMember.Const) && field.IsConst && semantic.Entity.DeclaringTypeDefinition.Kind != TypeKind.Enum))
                    {
                        acceptable = false;
                    }
                }
                else if (!(rule.Member.HasFlag(ConventionMember.Event) && entity is IEvent ||
                           rule.Member.HasFlag(ConventionMember.Method) && entity is IMethod ||
                           rule.Member.HasFlag(ConventionMember.Property) && entity is IProperty))
                {
                    acceptable = false;
                }

                if (!acceptable)
                {
                    return(false);
                }
            }

            if (!string.IsNullOrEmpty(rule.Filter))
            {
                var fullName = entity.FullName;
                var parts    = rule.Filter.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
                acceptable = false;

                foreach (var part in parts)
                {
                    string pattern;
                    bool   exclude = part.StartsWith("!");

                    if (part.StartsWith("regex:"))
                    {
                        pattern = part.Substring(6);
                    }
                    else
                    {
                        pattern = "^" + Regex.Escape(part).Replace("\\*", ".*").Replace("\\?", ".") + "$";
                    }

                    if (Regex.IsMatch(fullName, pattern))
                    {
                        acceptable = !exclude;
                    }
                }

                if (!acceptable)
                {
                    return(false);
                }
            }

            return(acceptable);
        }
Beispiel #5
0
        private static string ApplyRule(NameSemantic semantic, NameRule rule, string customName)
        {
            semantic.AppliedRule = rule;
            var name = semantic.DefaultName;

            if (rule != null)
            {
                if (!string.IsNullOrWhiteSpace(rule.CustomName))
                {
                    customName = rule.CustomName;
                }

                switch (rule.Notation)
                {
                case Notation.None:
                    break;

                case Notation.UpperCase:
                    name = name.ToUpperInvariant();
                    break;

                case Notation.LowerCase:
                    name = name.ToLowerInvariant();
                    break;

                case Notation.CamelCase:
                    var rejectRule = rule.Level != NameRuleLevel.Member && semantic.Entity is IMember &&
                                     !semantic.IsCustomName &&
                                     semantic.Entity.Name.Length > 1 &&
                                     semantic.Entity.Name.ToUpperInvariant() == semantic.Entity.Name;
                    if (rejectRule)
                    {
                        int upperCount = 0;
                        for (int i = 0; i < semantic.Entity.Name.Length; i++)
                        {
                            if (char.IsUpper(semantic.Entity.Name[i]))
                            {
                                upperCount++;
                            }
                            if (char.IsLower(semantic.Entity.Name[i]))
                            {
                                rejectRule = false;
                                break;
                            }
                        }

                        if (upperCount <= 1)
                        {
                            rejectRule = false;
                        }
                    }

                    if (!rejectRule)
                    {
                        name = name.ToLowerCamelCase();
                    }

                    break;

                case Notation.PascalCase:
                    name = name.ToCamelCase();
                    break;

                default:
                    throw new ArgumentOutOfRangeException(nameof(rule.Notation), rule.Notation, null);
                }
            }

            if (!string.IsNullOrWhiteSpace(customName))
            {
                name = Helpers.ConvertNameTokens(customName, name);
            }

            if (semantic.Entity is IMember)
            {
                bool isIgnore = semantic.Entity.DeclaringTypeDefinition != null && semantic.Emitter.Validator.IsExternalType(semantic.Entity.DeclaringTypeDefinition);
                if (!isIgnore && semantic.Entity.IsStatic && Helpers.IsReservedStaticName(name, false))
                {
                    name = Helpers.ChangeReservedWord(name);
                }
            }

            return(name);
        }