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 NameRule GetEnumRule(NameSemantic semantic) { int enumMode = -1; if (semantic.Entity is IField && semantic.Entity.DeclaringType.Kind == TypeKind.Enum) { enumMode = Helpers.EnumEmitMode(semantic.Entity.DeclaringType); semantic.EnumMode = enumMode; } switch (enumMode) { case 1: if (semantic.Entity.Name.Length > 1 && semantic.Entity.Name.ToUpperInvariant() == semantic.Entity.Name) { return(NameConvertor.DefaultCaseRule); } return(NameConvertor.LowerCamelCaseRule); case 3: return(NameConvertor.LowerCamelCaseRule); case 2: case 4: case 7: return(NameConvertor.DefaultCaseRule); case 5: case 8: return(NameConvertor.LowerCaseRule); case 6: case 9: return(NameConvertor.UpperCaseRule); } return(null); }
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 IsAcceptableTarget(NameSemantic semantic, ConventionTarget target, ITypeDefinition typeDef) { bool acceptable = true; switch (target) { case ConventionTarget.Class: acceptable = typeDef.Kind == TypeKind.Class; break; case ConventionTarget.Struct: acceptable = typeDef.Kind == TypeKind.Struct; break; case ConventionTarget.Enum: acceptable = typeDef.Kind == TypeKind.Enum; break; case ConventionTarget.Interface: acceptable = typeDef.Kind == TypeKind.Interface; break; case ConventionTarget.Delegate: acceptable = typeDef.Kind == TypeKind.Delegate; break; case ConventionTarget.ObjectLiteral: acceptable = semantic.IsObjectLiteral || typeDef.GetAttribute(new FullTypeName("H5.ObjectLiteralAttribute"), false) != null; break; case ConventionTarget.Anonymous: acceptable = typeDef.Kind == TypeKind.Anonymous; break; case ConventionTarget.External: string externalAttr = "H5.ExternalAttribute"; var has = typeDef.Attributes.Any( attr => attr.Constructor != null && attr.Constructor.DeclaringType.FullName == externalAttr); if (!has && typeDef.DeclaringTypeDefinition != null) { has = typeDef.DeclaringTypeDefinition.Attributes.Any( attr => attr.Constructor != null && attr.Constructor.DeclaringType.FullName == externalAttr); } if (!has) { has = H5Types.IsTypeFromH5Core(typeDef.FullName) || typeDef.ParentAssembly.AssemblyAttributes.Any( attr => attr.Constructor != null && attr.Constructor.DeclaringType.FullName == externalAttr); } acceptable = has; break; default: break; } return(acceptable); }
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); }
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); }