void CheckName(TypeDeclaration node, AffectedEntity entity, Identifier identifier, Modifiers accessibilty) { TypeResolveResult resolveResult = ctx.Resolve(node) as TypeResolveResult; if (resolveResult == null) return; var type = resolveResult.Type; if (type.DirectBaseTypes.Any(t => t.FullName == "System.Attribute")) { if (CheckNamedResolveResult(resolveResult, node, AffectedEntity.CustomAttributes, identifier, accessibilty)) { return; } } else if (type.DirectBaseTypes.Any(t => t.FullName == "System.EventArgs")) { if (CheckNamedResolveResult(resolveResult, node, AffectedEntity.CustomEventArgs, identifier, accessibilty)) { return; } } else if (type.DirectBaseTypes.Any(t => t.FullName == "System.Exception")) { if (CheckNamedResolveResult(resolveResult, node, AffectedEntity.CustomExceptions, identifier, accessibilty)) { return; } } var typeDef = type.GetDefinition(); if (typeDef != null && typeDef.Attributes.Any(attr => attr.AttributeType.FullName == "NUnit.Framework.TestFixtureAttribute")) { if (CheckNamedResolveResult(resolveResult, node, AffectedEntity.TestType, identifier, accessibilty)) { return; } } CheckNamedResolveResult(resolveResult, node, entity, identifier, accessibilty); }
public string CheckName(RefactoringContext ctx, string name, AffectedEntity entity, Modifiers accessibilty = Modifiers.Private, bool isStatic = false) { foreach (var rule in Rules) { if (!rule.AffectedEntity.HasFlag(entity)) { continue; } if (!rule.VisibilityMask.HasFlag(accessibilty)) { continue; } if (isStatic && !rule.IncludeStaticEntities || !isStatic && !rule.IncludeInstanceMembers) { continue; } if (!rule.IsValid(name)) { IList <string> suggestedNames; var msg = rule.GetErrorMessage(ctx, name, out suggestedNames); if (suggestedNames.Any()) { return(suggestedNames [0]); } } } return(name); }
public NamingRule(AffectedEntity affectedEntity) { AffectedEntity = affectedEntity; VisibilityMask = Modifiers.VisibilityMask; IncludeStaticEntities = true; IncludeInstanceMembers = true; }
public string CheckName(SemanticModel ctx, string name, AffectedEntity entity, Modifiers accessibilty = Modifiers.Private, bool isStatic = false) { foreach (var rule in Rules) { if (!rule.AffectedEntity.HasFlag(entity)) { continue; } if (!rule.VisibilityMask.HasFlag(accessibilty)) { continue; } if (isStatic && !rule.IncludeStaticEntities || !isStatic && !rule.IncludeInstanceMembers) { continue; } if (!rule.IsValid(name)) { IList<string> suggestedNames; rule.GetErrorMessage(ctx, name, out suggestedNames); if (suggestedNames.Any()) return suggestedNames[0]; } } return name; }
void CheckName(AstNode node, AffectedEntity entity, Identifier identifier, Modifiers accessibilty) { ResolveResult resolveResult = null; if (node != null) { resolveResult = ctx.Resolve(node); } if (resolveResult is TypeResolveResult) { var type = ((TypeResolveResult)resolveResult).Type; if (type.DirectBaseTypes.Any(t => t.FullName == "System.Attribute")) { if (CheckNamedResolveResult(resolveResult, AffectedEntity.CustomAttributes, identifier, accessibilty)) { return; } } else if (type.DirectBaseTypes.Any(t => t.FullName == "System.EventArgs")) { if (CheckNamedResolveResult(resolveResult, AffectedEntity.CustomEventArgs, identifier, accessibilty)) { return; } } else if (type.DirectBaseTypes.Any(t => t.FullName == "System.Exception")) { if (CheckNamedResolveResult(resolveResult, AffectedEntity.CustomExceptions, identifier, accessibilty)) { return; } } var typeDef = type.GetDefinition(); if (typeDef != null && typeDef.Attributes.Any(attr => attr.AttributeType.FullName == "NUnit.Framework.TestFixtureAttribute")) { if (CheckNamedResolveResult(resolveResult, AffectedEntity.TestType, identifier, accessibilty)) { return; } } } else if (resolveResult is MemberResolveResult) { var member = ((MemberResolveResult)resolveResult).Member; if (member.EntityType == EntityType.Method && member.Attributes.Any(attr => attr.AttributeType.FullName == "NUnit.Framework.TestAttribute")) { if (CheckNamedResolveResult(resolveResult, AffectedEntity.TestMethod, identifier, accessibilty)) { return; } } } CheckNamedResolveResult(resolveResult, entity, identifier, accessibilty); }
void CheckName(MethodDeclaration node, AffectedEntity entity, Identifier identifier, Modifiers accessibilty) { ResolveResult resolveResult = null; if (node != null && node.Attributes.Any ()) resolveResult = ctx.Resolve(node); if (resolveResult is MemberResolveResult) { var member = ((MemberResolveResult)resolveResult).Member; if (member.EntityType == EntityType.Method && member.Attributes.Any(attr => attr.AttributeType.FullName == "NUnit.Framework.TestAttribute")) { if (CheckNamedResolveResult(resolveResult, node, AffectedEntity.TestMethod, identifier, accessibilty)) { return; } } } CheckNamedResolveResult(resolveResult, node, entity, identifier, accessibilty); }
public bool HasValidRule(string name, AffectedEntity entity, Modifiers accessibilty = Modifiers.Private, bool isStatic = false) { foreach (var rule in Rules) { if (!rule.AffectedEntity.HasFlag(entity)) { continue; } if (!rule.VisibilityMask.HasFlag(accessibilty)) { continue; } if (isStatic && !rule.IncludeStaticEntities || !isStatic && !rule.IncludeInstanceMembers) { continue; } if (rule.IsValid(name)) { return true; } } return false; }
void CheckName(TypeDeclaration node, AffectedEntity entity, Identifier identifier, Modifiers accessibilty) { TypeResolveResult resolveResult = ctx.Resolve(node) as TypeResolveResult; if (resolveResult == null) { return; } var type = resolveResult.Type; if (type.DirectBaseTypes.Any(t => t.FullName == "System.Attribute")) { if (CheckNamedResolveResult(resolveResult, node, AffectedEntity.CustomAttributes, identifier, accessibilty)) { return; } } else if (type.DirectBaseTypes.Any(t => t.FullName == "System.EventArgs")) { if (CheckNamedResolveResult(resolveResult, node, AffectedEntity.CustomEventArgs, identifier, accessibilty)) { return; } } else if (type.DirectBaseTypes.Any(t => t.FullName == "System.Exception")) { if (CheckNamedResolveResult(resolveResult, node, AffectedEntity.CustomExceptions, identifier, accessibilty)) { return; } } var typeDef = type.GetDefinition(); if (typeDef != null && typeDef.Attributes.Any(attr => attr.AttributeType.FullName == "NUnit.Framework.TestFixtureAttribute")) { if (CheckNamedResolveResult(resolveResult, node, AffectedEntity.TestType, identifier, accessibilty)) { return; } } CheckNamedResolveResult(resolveResult, node, entity, identifier, accessibilty); }
void CheckName(MethodDeclaration node, AffectedEntity entity, Identifier identifier, Modifiers accessibilty) { ResolveResult resolveResult = null; if (node != null && node.Attributes.Any()) { resolveResult = ctx.Resolve(node); } if (resolveResult is MemberResolveResult) { var member = ((MemberResolveResult)resolveResult).Member; if (member.EntityType == EntityType.Method && member.Attributes.Any(attr => attr.AttributeType.FullName == "NUnit.Framework.TestAttribute")) { if (CheckNamedResolveResult(resolveResult, node, AffectedEntity.TestMethod, identifier, accessibilty)) { return; } } } CheckNamedResolveResult(resolveResult, node, entity, identifier, accessibilty); }
bool CheckNamedResolveResult(ResolveResult resolveResult, AstNode node, AffectedEntity entity, Identifier identifier, Modifiers accessibilty) { bool wasHandled = false; foreach (var rule in service.Rules) { if (!rule.AffectedEntity.HasFlag(entity)) { continue; } if (!rule.VisibilityMask.HasFlag(accessibilty)) { continue; } if (!rule.IncludeInstanceMembers || !rule.IncludeStaticEntities) { EntityDeclaration typeSystemEntity; if (node is VariableInitializer) { typeSystemEntity = node.Parent as EntityDeclaration; } else { typeSystemEntity = node as EntityDeclaration; } if (!rule.IncludeInstanceMembers) { if (typeSystemEntity == null || !typeSystemEntity.HasModifier(Modifiers.Static) || typeSystemEntity.HasModifier(Modifiers.Sealed)) { continue; } } if (!rule.IncludeStaticEntities) { if (typeSystemEntity == null || typeSystemEntity.HasModifier(Modifiers.Static) || typeSystemEntity.HasModifier(Modifiers.Sealed)) { continue; } } } wasHandled = true; if (!rule.IsValid(identifier.Name)) { IList <string> suggestedNames; var msg = rule.GetErrorMessage(ctx, identifier.Name, out suggestedNames); var actions = new List <CodeAction>(suggestedNames.Select(n => new CodeAction(string.Format(ctx.TranslateString("Rename to '{0}'"), n), (Script script) => { if (resolveResult == null) { resolveResult = ctx.Resolve(node); } if (resolveResult is MemberResolveResult) { script.Rename(((MemberResolveResult)resolveResult).Member, n); } else if (resolveResult is TypeResolveResult) { var def = ((TypeResolveResult)resolveResult).Type.GetDefinition(); if (def != null) { script.Rename(def, n); } else { script.RenameTypeParameter(((TypeResolveResult)resolveResult).Type, n); } } else if (resolveResult is LocalResolveResult) { script.Rename(((LocalResolveResult)resolveResult).Variable, n); } else { script.Replace(identifier, Identifier.Create(n)); } }))); if (entity != AffectedEntity.Namespace && entity != AffectedEntity.Label) { actions.Add(new CodeAction(string.Format(ctx.TranslateString("Rename '{0}'..."), identifier.Name), (Script script) => { if (resolveResult == null) { resolveResult = ctx.Resolve(node); } if (resolveResult is MemberResolveResult) { script.Rename(((MemberResolveResult)resolveResult).Member); } else if (resolveResult is TypeResolveResult) { var def = ((TypeResolveResult)resolveResult).Type.GetDefinition(); if (def != null) { script.Rename(def); } else { script.RenameTypeParameter(((TypeResolveResult)resolveResult).Type); } } else if (resolveResult is LocalResolveResult) { script.Rename(((LocalResolveResult)resolveResult).Variable); } })); } AddIssue(identifier, msg, actions); } } return(wasHandled); }
void CheckName(AstNode node, AffectedEntity entity, Identifier identifier, Modifiers accessibilty) { CheckNamedResolveResult(null, node, entity, identifier, accessibilty); }
bool CheckNamedResolveResult(ResolveResult resolveResult, AstNode node, AffectedEntity entity, Identifier identifier, Modifiers accessibilty) { bool wasHandled = false; foreach (var rule in service.Rules) { if (!rule.AffectedEntity.HasFlag(entity)) { continue; } if (!rule.VisibilityMask.HasFlag(accessibilty)) { continue; } if (!rule.IncludeInstanceMembers || !rule.IncludeStaticEntities) { EntityDeclaration typeSystemEntity; if (node is VariableInitializer) { typeSystemEntity = node.Parent as EntityDeclaration; } else { typeSystemEntity = node as EntityDeclaration; } if (!rule.IncludeInstanceMembers) { if (typeSystemEntity == null || !typeSystemEntity.HasModifier (Modifiers.Static) || typeSystemEntity.HasModifier (Modifiers.Sealed)) { continue; } } if (!rule.IncludeStaticEntities) { if (typeSystemEntity == null || typeSystemEntity.HasModifier (Modifiers.Static) || typeSystemEntity.HasModifier (Modifiers.Sealed)) { continue; } } } wasHandled = true; if (!rule.IsValid(identifier.Name)) { IList<string> suggestedNames; var msg = rule.GetErrorMessage(ctx, identifier.Name, out suggestedNames); var actions = new List<CodeAction>(suggestedNames.Select(n => new CodeAction(string.Format(ctx.TranslateString("Rename to '{0}'"), n), (Script script) => { if (resolveResult == null) resolveResult = ctx.Resolve (node); if (resolveResult is MemberResolveResult) { script.Rename(((MemberResolveResult)resolveResult).Member, n); } else if (resolveResult is TypeResolveResult) { var def = ((TypeResolveResult)resolveResult).Type.GetDefinition(); if (def != null) { script.Rename(def, n); } else { script.RenameTypeParameter(((TypeResolveResult)resolveResult).Type, n); } } else if (resolveResult is LocalResolveResult) { script.Rename(((LocalResolveResult)resolveResult).Variable, n); } else { script.Replace(identifier, Identifier.Create(n)); } }))); if (entity != AffectedEntity.Namespace && entity != AffectedEntity.Label) { actions.Add(new CodeAction(string.Format(ctx.TranslateString("Rename '{0}'..."), identifier.Name), (Script script) => { if (resolveResult == null) resolveResult = ctx.Resolve (node); if (resolveResult is MemberResolveResult) { script.Rename(((MemberResolveResult)resolveResult).Member); } else if (resolveResult is TypeResolveResult) { var def = ((TypeResolveResult)resolveResult).Type.GetDefinition(); if (def != null) { script.Rename(def); } else { script.RenameTypeParameter(((TypeResolveResult)resolveResult).Type); } } else if (resolveResult is LocalResolveResult) { script.Rename(((LocalResolveResult)resolveResult).Variable); } })); } AddIssue(identifier, msg, actions); } } return wasHandled; }