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); }
private static NameRule[] GetClassRules(NameSemantic semantic, ITypeDefinition typeDef) { if (semantic.Emitter.ClassNameRuleCache.ContainsKey(typeDef)) { return(semantic.Emitter.ClassNameRuleCache[typeDef]); } var td = typeDef; List <NameRule> rules = new List <NameRule>(); while (td != null) { IAttribute[] classAttrs = td.GetAttributes(new FullTypeName(NameConvertor.ConventionAttrName)).ToArray(); for (int i = 0; i < classAttrs.Length; i++) { rules.Add(NameConvertor.ToRule(classAttrs[i], NameRuleLevel.Class)); } td = td.DeclaringTypeDefinition; } var classRules = rules.ToArray(); semantic.Emitter.ClassNameRuleCache.Add(typeDef, classRules); return(classRules); }
private static List <NameRule> GetRules(NameSemantic semantic) { var rules = NameConvertor.GetSpecialRules(semantic); rules.AddRange(NameConvertor.GetAttributeRules(semantic)); rules.AddRange(NameConvertor.GetDefaultRules(semantic)); return(rules); }
public static string Convert(NameSemantic semantic) { var rules = NameConvertor.GetRules(semantic); string customName = null; foreach (var rule in rules) { if (NameConvertor.IsRuleAcceptable(semantic, rule)) { if (!string.IsNullOrWhiteSpace(rule.CustomName)) { customName = rule.CustomName; continue; } return(NameConvertor.ApplyRule(semantic, rule, customName)); } } return(NameConvertor.ApplyRule(semantic, null, customName)); }
private static List <NameRule> GetDefaultRules(NameSemantic semantic) { var rules = new List <NameRule>(); var enumRule = NameConvertor.GetEnumRule(semantic); if (enumRule != null) { rules.Add(enumRule); } var propRule = NameConvertor.GetPropertyRule(semantic); if (propRule != null) { rules.Add(propRule); } rules.AddRange(NameConvertor.defaultRules); return(rules); }
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); }
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); }