19 changed files with 1009 additions and 193 deletions
@ -1,100 +0,0 @@
@@ -1,100 +0,0 @@
|
||||
|
||||
// ConvertToStaticMethodAction.cs
|
||||
//
|
||||
// Author:
|
||||
// Ciprian Khlud <ciprian.mustiata@yahoo.com>
|
||||
//
|
||||
// Copyright (c) 2013 Ciprian Khlud <ciprian.mustiata@yahoo.com>
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in
|
||||
// all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
// THE SOFTWARE.
|
||||
using System.Collections.Generic; |
||||
using ICSharpCode.NRefactory.CSharp.Refactoring.ExtractMethod; |
||||
using ICSharpCode.NRefactory.CSharp.Resolver; |
||||
using ICSharpCode.NRefactory.Semantics; |
||||
using System.Linq; |
||||
using ICSharpCode.NRefactory.TypeSystem; |
||||
|
||||
namespace ICSharpCode.NRefactory.CSharp.Refactoring |
||||
{ |
||||
[ContextAction("Make this method static", Description = "This method doesn't use any non static members so it can be made static")] |
||||
public class ConvertToStaticMethodAction : ICodeActionProvider |
||||
{ |
||||
public IEnumerable<CodeAction> GetActions(RefactoringContext context) |
||||
{ |
||||
// TODO: Invert if without else
|
||||
// ex. if (cond) DoSomething () == if (!cond) return; DoSomething ()
|
||||
// beware of loop contexts return should be continue then.
|
||||
var methodDeclaration = GetMethodDeclaration(context); |
||||
if (methodDeclaration == null) |
||||
yield break; |
||||
|
||||
var resolved = context.Resolve(methodDeclaration) as MemberResolveResult; |
||||
if (resolved == null) |
||||
yield break; |
||||
var isImplementingInterface = resolved.Member.ImplementedInterfaceMembers.Any(); |
||||
|
||||
if (isImplementingInterface) |
||||
yield break; |
||||
yield return new CodeAction(context.TranslateString(string.Format("Make '{0}' static", methodDeclaration.Name)), script => |
||||
{ |
||||
var clonedDeclaration = (MethodDeclaration)methodDeclaration.Clone(); |
||||
clonedDeclaration.Modifiers |= Modifiers.Static; |
||||
script.Replace(methodDeclaration, clonedDeclaration); |
||||
var rr = context.Resolve (methodDeclaration) as MemberResolveResult; |
||||
var method = (IMethod)rr.Member; |
||||
//method.ImplementedInterfaceMembers.Any(m => methodGroupResolveResult.Methods.Contains((IMethod)m));
|
||||
|
||||
script.DoGlobalOperationOn(rr.Member, (fctx, fscript, fnode) => { |
||||
if (fnode is MemberReferenceExpression) { |
||||
var memberReference = new MemberReferenceExpression ( |
||||
new TypeReferenceExpression (fctx.CreateShortType (rr.Member.DeclaringType)), |
||||
rr.Member.Name |
||||
); |
||||
fscript.Replace (fnode, memberReference); |
||||
} else if (fnode is InvocationExpression) { |
||||
var invoke = (InvocationExpression)fnode; |
||||
if (!(invoke.Target is MemberReferenceExpression)) |
||||
return; |
||||
var memberReference = new MemberReferenceExpression ( |
||||
new TypeReferenceExpression (fctx.CreateShortType (rr.Member.DeclaringType)), |
||||
rr.Member.Name |
||||
); |
||||
fscript.Replace (invoke.Target, memberReference); |
||||
} |
||||
}); |
||||
}, methodDeclaration); |
||||
} |
||||
|
||||
static MethodDeclaration GetMethodDeclaration(RefactoringContext context) |
||||
{ |
||||
var result = context.GetNode <MethodDeclaration>(); |
||||
if (result == null) |
||||
return null; |
||||
//unsafe transformation for now. For all other public instances the code should
|
||||
//replace the variable.Call(...) to ClassName.Call()
|
||||
const Modifiers ignoredModifiers = Modifiers.Override | Modifiers.Virtual | Modifiers.Static; |
||||
if ((result.Modifiers & ignoredModifiers) != 0) |
||||
return null; |
||||
|
||||
var usesNonStatic = StaticVisitor.UsesNotStaticMember(context, result); |
||||
if (usesNonStatic) return null; |
||||
return result; |
||||
} |
||||
} |
||||
} |
||||
@ -0,0 +1,110 @@
@@ -0,0 +1,110 @@
|
||||
//
|
||||
// RemoveField.cs
|
||||
//
|
||||
// Author:
|
||||
// Ciprian Khlud <ciprian.mustiata@yahoo.com>
|
||||
//
|
||||
// Copyright (c) 2013 Ciprian Khlud
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in
|
||||
// all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
// THE SOFTWARE.
|
||||
using System; |
||||
using ICSharpCode.NRefactory.CSharp.Refactoring; |
||||
using System.Collections.Generic; |
||||
using ICSharpCode.NRefactory.CSharp.Resolver; |
||||
using ICSharpCode.NRefactory.Semantics; |
||||
|
||||
namespace ICSharpCode.NRefactory.CSharp |
||||
{ |
||||
[ContextAction("Removes a field from a class", Description = "It removes also the empty assingments and the usages")] |
||||
public class RemoveFieldRefactoryAction : ICodeActionProvider |
||||
{ |
||||
public IEnumerable<CodeAction> GetActions(RefactoringContext context) |
||||
{ |
||||
var fieldDeclaration = GetFieldDeclaration(context); |
||||
if(fieldDeclaration==null) |
||||
yield break; |
||||
|
||||
|
||||
yield return new CodeAction(string.Format(context.TranslateString("Remove field '{0}'"), fieldDeclaration.Name) |
||||
, script => GenerateNewScript( |
||||
script, fieldDeclaration, context), fieldDeclaration); |
||||
} |
||||
|
||||
|
||||
void GenerateNewScript(Script script, FieldDeclaration fieldDeclaration, RefactoringContext context) |
||||
{ |
||||
var firstOrNullObject = fieldDeclaration.Variables.FirstOrNullObject(); |
||||
if(firstOrNullObject==null) |
||||
return; |
||||
var matchedNodes = ComputeMatchNodes(context, firstOrNullObject); |
||||
|
||||
foreach (var matchNode in matchedNodes) |
||||
{ |
||||
var parent = matchNode.Parent; |
||||
if (matchNode is VariableInitializer) |
||||
{ |
||||
script.Remove(parent); |
||||
} |
||||
else |
||||
if (matchNode is IdentifierExpression) |
||||
{ |
||||
if(parent is AssignmentExpression) |
||||
{ |
||||
script.Remove(parent.Parent); |
||||
} |
||||
else |
||||
{ |
||||
var clone = (IdentifierExpression)matchNode.Clone(); |
||||
clone.Identifier = "TODO"; |
||||
script.Replace(matchNode, clone); |
||||
} |
||||
} |
||||
} |
||||
} |
||||
|
||||
private static List<AstNode> ComputeMatchNodes(RefactoringContext context, VariableInitializer firstOrNullObject) |
||||
{ |
||||
var referenceFinder = new FindReferences(); |
||||
var matchedNodes = new List<AstNode>(); |
||||
|
||||
var resolveResult = context.Resolver.Resolve(firstOrNullObject); |
||||
var member = resolveResult as MemberResolveResult; |
||||
if (member == null)//not a member is unexpected case, so is better to return no match than to break the code
|
||||
return matchedNodes; |
||||
|
||||
FoundReferenceCallback callback = (matchNode, result) => matchedNodes.Add(matchNode); |
||||
|
||||
var searchScopes = referenceFinder.GetSearchScopes(member.Member); |
||||
referenceFinder.FindReferencesInFile(searchScopes, |
||||
context.UnresolvedFile, |
||||
context.RootNode as SyntaxTree, |
||||
context.Compilation, callback, |
||||
context.CancellationToken); |
||||
return matchedNodes; |
||||
} |
||||
|
||||
FieldDeclaration GetFieldDeclaration(RefactoringContext context) |
||||
{ |
||||
var result = context.GetNode<FieldDeclaration>(); |
||||
|
||||
return result; |
||||
} |
||||
} |
||||
} |
||||
|
||||
@ -0,0 +1,121 @@
@@ -0,0 +1,121 @@
|
||||
//
|
||||
// ConvertToStaticMethodIssue.cs
|
||||
//
|
||||
// Author:
|
||||
// Ciprian Khlud <ciprian.mustiata@yahoo.com>
|
||||
//
|
||||
// Copyright (c) 2013 Ciprian Khlud
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in
|
||||
// all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
// THE SOFTWARE.
|
||||
|
||||
using System.Collections.Generic; |
||||
using ICSharpCode.NRefactory.Semantics; |
||||
using System.Linq; |
||||
using ICSharpCode.NRefactory.TypeSystem; |
||||
|
||||
namespace ICSharpCode.NRefactory.CSharp.Refactoring |
||||
{ |
||||
[IssueDescription("Make this method static", |
||||
Description = "This method doesn't use any non static members so it can be made static", |
||||
Severity = Severity.Hint, |
||||
IssueMarker = IssueMarker.Underline)] |
||||
public class ConvertToStaticMethodIssue : ICodeIssueProvider |
||||
{ |
||||
public IEnumerable<CodeIssue> GetIssues(BaseRefactoringContext context) |
||||
{ |
||||
return new GatherVisitor(context).GetIssues(); |
||||
} |
||||
|
||||
private class GatherVisitor : GatherVisitorBase<ConvertToStaticMethodIssue> |
||||
{ |
||||
private bool initializerInvoked; |
||||
private ConstructorInitializer initializer; |
||||
|
||||
public GatherVisitor(BaseRefactoringContext context) |
||||
: base(context) { |
||||
} |
||||
|
||||
public override void VisitMethodDeclaration(MethodDeclaration declaration) |
||||
{ |
||||
// TODO: Invert if without else
|
||||
// ex. if (cond) DoSomething () == if (!cond) return; DoSomething ()
|
||||
// beware of loop contexts return should be continue then.
|
||||
var context = ctx; |
||||
var methodDeclaration = declaration; |
||||
|
||||
var resolved = context.Resolve(methodDeclaration) as MemberResolveResult; |
||||
if (resolved == null) |
||||
return; |
||||
var isImplementingInterface = resolved.Member.ImplementedInterfaceMembers.Any(); |
||||
|
||||
if (isImplementingInterface) |
||||
return; |
||||
|
||||
AddIssue(methodDeclaration.NameToken.StartLocation, methodDeclaration.NameToken.EndLocation, |
||||
context.TranslateString(string.Format("Make '{0}' static", methodDeclaration.Name)), |
||||
script => ExecuteScriptToFixStaticMethodIssue(script, context, methodDeclaration)); |
||||
} |
||||
|
||||
private static void ExecuteScriptToFixStaticMethodIssue(Script script, |
||||
BaseRefactoringContext context, |
||||
AstNode methodDeclaration) |
||||
{ |
||||
var clonedDeclaration = (MethodDeclaration) methodDeclaration.Clone(); |
||||
clonedDeclaration.Modifiers |= Modifiers.Static; |
||||
script.Replace(methodDeclaration, clonedDeclaration); |
||||
var rr = context.Resolve(methodDeclaration) as MemberResolveResult; |
||||
var method = (IMethod) rr.Member; |
||||
//method.ImplementedInterfaceMembers.Any(m => methodGroupResolveResult.Methods.Contains((IMethod)m));
|
||||
|
||||
script.DoGlobalOperationOn(rr.Member, |
||||
(fctx, fscript, fnode) => { DoStaticMethodGlobalOperation(fnode, fctx, rr, fscript); }); |
||||
} |
||||
|
||||
private static void DoStaticMethodGlobalOperation(AstNode fnode, RefactoringContext fctx, MemberResolveResult rr, |
||||
Script fscript) |
||||
{ |
||||
if (fnode is MemberReferenceExpression) { |
||||
var memberReference = new MemberReferenceExpression |
||||
( |
||||
new TypeReferenceExpression( |
||||
fctx.CreateShortType(rr.Member.DeclaringType)), |
||||
rr.Member.Name |
||||
); |
||||
fscript.Replace(fnode, memberReference); |
||||
} |
||||
else { |
||||
var invoke = fnode as InvocationExpression; |
||||
if (invoke == null) return; |
||||
if ((invoke.Target is MemberReferenceExpression)) |
||||
return; |
||||
var memberReference = new MemberReferenceExpression |
||||
( |
||||
new TypeReferenceExpression( |
||||
fctx.CreateShortType( |
||||
rr.Member.DeclaringType)), |
||||
rr.Member.Name |
||||
); |
||||
fscript.Replace(invoke.Target, memberReference); |
||||
} |
||||
} |
||||
} |
||||
|
||||
} |
||||
} |
||||
|
||||
@ -0,0 +1,188 @@
@@ -0,0 +1,188 @@
|
||||
//
|
||||
// DuplicateBodyMethodIssue.cs
|
||||
//
|
||||
// Author:
|
||||
// Ciprian Khlud <ciprian.mustiata@yahoo.com>
|
||||
//
|
||||
// Copyright (c) 2013 Ciprian Khlud
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in
|
||||
// all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
// THE SOFTWARE.
|
||||
|
||||
using System.Collections.Generic; |
||||
using System.Text; |
||||
using ICSharpCode.NRefactory.PatternMatching; |
||||
using ICSharpCode.NRefactory.Semantics; |
||||
using System.Linq; |
||||
|
||||
namespace ICSharpCode.NRefactory.CSharp.Refactoring |
||||
{ |
||||
[IssueDescription("Methods have duplicate body", |
||||
Description = "One method has the same body as other method", |
||||
Severity = Severity.Hint, |
||||
IssueMarker = IssueMarker.Underline)] |
||||
public class DuplicateBodyMethodIssue : ICodeIssueProvider |
||||
{ |
||||
#region ICodeIssueProvider implementation
|
||||
|
||||
public IEnumerable<CodeIssue> GetIssues(BaseRefactoringContext context) |
||||
{ |
||||
var visitor = new GatherVisitor(context); |
||||
visitor.GetMethods(); |
||||
visitor.ComputeConflicts(); |
||||
return visitor.GetIssues(); |
||||
} |
||||
|
||||
#endregion
|
||||
|
||||
private class GatherVisitor : GatherVisitorBase<DuplicateBodyMethodIssue> |
||||
{ |
||||
public List<MethodDeclaration> DeclaredMethods; |
||||
|
||||
public GatherVisitor(BaseRefactoringContext context) |
||||
: base(context) |
||||
{ |
||||
DeclaredMethods = new List<MethodDeclaration>(); |
||||
} |
||||
|
||||
|
||||
static string GetMethodDescriptor(MethodDeclaration methodDeclaration) { |
||||
var sb = new StringBuilder(); |
||||
sb.Append(methodDeclaration.ReturnType); |
||||
sb.Append(";"); |
||||
foreach (var parameter in methodDeclaration.Parameters) { |
||||
sb.AppendFormat("{0}:{1};", parameter.Name, parameter.Type); |
||||
} |
||||
sb.Append(methodDeclaration.Modifiers); |
||||
return sb.ToString(); |
||||
} |
||||
|
||||
public void GetMethods() |
||||
{ |
||||
ctx.RootNode.AcceptVisitor(this); |
||||
} |
||||
|
||||
internal void ComputeConflicts() |
||||
{ |
||||
var dict = new Dictionary<string, List<MethodDeclaration>>(); |
||||
foreach (var declaredMethod in DeclaredMethods) |
||||
{ |
||||
var methodDescriptor = GetMethodDescriptor(declaredMethod); |
||||
List<MethodDeclaration> listMethods; |
||||
if (!dict.TryGetValue(methodDescriptor, out listMethods)) |
||||
{ |
||||
listMethods = new List<MethodDeclaration>(); |
||||
dict[methodDescriptor] = listMethods; |
||||
} |
||||
listMethods.Add(declaredMethod); |
||||
} |
||||
DeclaredMethods.Clear(); |
||||
|
||||
foreach (var list in dict.Values.Where(list => list.Count >= 2)) |
||||
{ |
||||
for (var i = 0; i < list.Count - 1; i++) |
||||
{ |
||||
var firstMethod = list[i]; |
||||
for (var j = i + 1; j < list.Count; j++) |
||||
{ |
||||
var secondMethod = list[j]; |
||||
if (firstMethod.Body.IsMatch(secondMethod.Body)) |
||||
{ |
||||
AddIssue(secondMethod.NameToken, |
||||
string.Format("Method '{0}' has the same with '{1}' ", secondMethod.Name, |
||||
firstMethod.Name), |
||||
script => { InvokeMethod(script, firstMethod, secondMethod); } |
||||
); |
||||
} |
||||
} |
||||
} |
||||
} |
||||
} |
||||
|
||||
readonly InsertParenthesesVisitor _insertParenthesesVisitor = new InsertParenthesesVisitor(); |
||||
private TypeDeclaration _parentType; |
||||
|
||||
private void InvokeMethod(Script script, MethodDeclaration firstMethod, MethodDeclaration secondMethod) |
||||
{ |
||||
var statement = |
||||
new ExpressionStatement(new InvocationExpression(new IdentifierExpression(firstMethod.Name), |
||||
firstMethod.Parameters.Select( |
||||
declaration => |
||||
GetArgumentExpression(declaration).Clone()))); |
||||
statement.AcceptVisitor(_insertParenthesesVisitor); |
||||
if(firstMethod.ReturnType.ToString()!="System.Void"){ |
||||
var returnStatement = new ReturnStatement(statement.Expression.Clone()); |
||||
|
||||
script.Replace(secondMethod.Body, new BlockStatement { returnStatement }); |
||||
} |
||||
else { |
||||
script.Replace(secondMethod.Body, new BlockStatement { statement }); |
||||
} |
||||
} |
||||
|
||||
static Expression GetArgumentExpression(ParameterDeclaration parameter) |
||||
{ |
||||
var identifierExpr = new IdentifierExpression(parameter.Name); |
||||
switch (parameter.ParameterModifier) |
||||
{ |
||||
case ParameterModifier.Out: |
||||
return new DirectionExpression(FieldDirection.Out, identifierExpr); |
||||
case ParameterModifier.Ref: |
||||
return new DirectionExpression(FieldDirection.Ref, identifierExpr); |
||||
} |
||||
return identifierExpr; |
||||
} |
||||
|
||||
public override void VisitMethodDeclaration(MethodDeclaration declaration) |
||||
{ |
||||
var context = ctx; |
||||
var methodDeclaration = declaration; |
||||
|
||||
var resolved = context.Resolve(methodDeclaration) as MemberResolveResult; |
||||
if (resolved == null) |
||||
return; |
||||
var isImplementingInterface = resolved.Member.ImplementedInterfaceMembers.Any(); |
||||
|
||||
if (isImplementingInterface) |
||||
return; |
||||
if (declaration.Body.IsNull) |
||||
return; |
||||
|
||||
var parentType = declaration.Parent as TypeDeclaration; |
||||
if (parentType == null) |
||||
return; |
||||
if (_parentType == null) |
||||
_parentType = parentType; |
||||
else |
||||
{ |
||||
//if we are here, it means that we switched from one class to another, so it means that we should compute
|
||||
//the duplicates up-to now, then reset the list of methods
|
||||
if (parentType != _parentType) |
||||
{ |
||||
ComputeConflicts(); |
||||
DeclaredMethods.Add(declaration); |
||||
_parentType = parentType; |
||||
return; |
||||
} |
||||
} |
||||
|
||||
DeclaredMethods.Add(declaration); |
||||
} |
||||
} |
||||
} |
||||
} |
||||
@ -0,0 +1,156 @@
@@ -0,0 +1,156 @@
|
||||
//
|
||||
// ParameterCanBeIEnumerableIssue.cs
|
||||
//
|
||||
// Author:
|
||||
// Ciprian Khlud <ciprian.mustiata@yahoo.com>
|
||||
//
|
||||
// Copyright (c) 2013 Ciprian Khlud
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in
|
||||
// all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
// THE SOFTWARE.
|
||||
|
||||
using System.Collections.Generic; |
||||
using ICSharpCode.NRefactory.TypeSystem; |
||||
using ICSharpCode.NRefactory.Semantics; |
||||
using System.Linq; |
||||
|
||||
namespace ICSharpCode.NRefactory.CSharp.Refactoring |
||||
{ |
||||
|
||||
[IssueDescription("A parameter can IEnumerable/ICollection/IList<T>", |
||||
Description = "Finds parameters that can be demoted to a generic list.", |
||||
Category = IssueCategories.Opportunities, |
||||
Severity = Severity.Suggestion |
||||
)] |
||||
public class ParameterCanBeIEnumerableIssue : ICodeIssueProvider |
||||
{ |
||||
readonly bool tryResolve; |
||||
|
||||
public ParameterCanBeIEnumerableIssue() : this (true) |
||||
{ |
||||
} |
||||
|
||||
public ParameterCanBeIEnumerableIssue(bool tryResolve) |
||||
{ |
||||
this.tryResolve = tryResolve; |
||||
} |
||||
|
||||
#region ICodeIssueProvider implementation
|
||||
public IEnumerable<CodeIssue> GetIssues(BaseRefactoringContext context) |
||||
{ |
||||
var gatherer = new GatherVisitor(context, tryResolve); |
||||
var issues = gatherer.GetIssues(); |
||||
return issues; |
||||
} |
||||
#endregion
|
||||
|
||||
class GatherVisitor : GatherVisitorBase<ParameterCanBeDemotedIssue> |
||||
{ |
||||
bool tryResolve; |
||||
|
||||
public GatherVisitor(BaseRefactoringContext context, bool tryResolve) : base (context) |
||||
{ |
||||
this.tryResolve = tryResolve; |
||||
} |
||||
|
||||
public override void VisitMethodDeclaration(MethodDeclaration methodDeclaration) |
||||
{ |
||||
methodDeclaration.Attributes.AcceptVisitor(this); |
||||
if (HasEntryPointSignature(methodDeclaration)) |
||||
return; |
||||
var eligibleParameters = methodDeclaration.Parameters |
||||
.Where(p => p.ParameterModifier != ParameterModifier.Out && p.ParameterModifier != ParameterModifier.Ref) |
||||
.ToList(); |
||||
if (eligibleParameters.Count == 0) |
||||
return; |
||||
var declarationResolveResult = ctx.Resolve(methodDeclaration) as MemberResolveResult; |
||||
if (declarationResolveResult == null) |
||||
return; |
||||
var member = declarationResolveResult.Member; |
||||
if (member.IsOverride || member.IsOverridable || member.ImplementedInterfaceMembers.Any()) |
||||
return; |
||||
|
||||
var collector = new TypeCriteriaCollector(ctx); |
||||
methodDeclaration.AcceptVisitor(collector); |
||||
|
||||
foreach (var parameter in eligibleParameters) { |
||||
ProcessParameter(parameter, methodDeclaration.Body, collector); |
||||
} |
||||
} |
||||
|
||||
bool HasEntryPointSignature(MethodDeclaration methodDeclaration) |
||||
{ |
||||
if (!methodDeclaration.Modifiers.HasFlag(Modifiers.Static)) |
||||
return false; |
||||
var returnType = ctx.Resolve(methodDeclaration.ReturnType).Type; |
||||
if (!returnType.IsKnownType(KnownTypeCode.Int32) && !returnType.IsKnownType(KnownTypeCode.Void)) |
||||
return false; |
||||
var parameterCount = methodDeclaration.Parameters.Count; |
||||
if (parameterCount == 0) |
||||
return true; |
||||
if (parameterCount != 1) |
||||
return false; |
||||
var parameterType = ctx.Resolve(methodDeclaration.Parameters.First()).Type as ArrayType; |
||||
return parameterType != null && parameterType.ElementType.IsKnownType(KnownTypeCode.String); |
||||
} |
||||
|
||||
void ProcessParameter(ParameterDeclaration parameter, AstNode rootResolutionNode, TypeCriteriaCollector collector) |
||||
{ |
||||
var localResolveResult = ctx.Resolve(parameter) as LocalResolveResult; |
||||
if (localResolveResult == null) |
||||
return; |
||||
var variable = localResolveResult.Variable; |
||||
var typeKind = variable.Type.Kind; |
||||
if (!(typeKind == TypeKind.Class || |
||||
typeKind == TypeKind.Struct || |
||||
typeKind == TypeKind.Interface || |
||||
typeKind == TypeKind.Array) || |
||||
!collector.UsedVariables.Contains(variable)) |
||||
{ |
||||
return; |
||||
} |
||||
|
||||
var candidateTypes = localResolveResult.Type.GetAllBaseTypes() |
||||
.Where(t => t.IsParameterized) |
||||
.ToList(); |
||||
|
||||
var validTypes = |
||||
(from type in candidateTypes |
||||
where !tryResolve || ParameterCanBeDemotedIssue.TypeChangeResolvesCorrectly(ctx, parameter, rootResolutionNode, type) |
||||
select type).ToList(); |
||||
if (!validTypes.Any()) return; |
||||
|
||||
var foundType = validTypes.FirstOrDefault(); |
||||
if (foundType == null) |
||||
return; |
||||
|
||||
AddIssue(parameter.NameToken, string.Format(ctx.TranslateString("Parameter can be {0}"), |
||||
foundType.Name), |
||||
script => script.Replace(parameter.Type, CreateShortType(ctx, foundType, parameter))); |
||||
} |
||||
|
||||
public static AstType CreateShortType(BaseRefactoringContext refactoringContext, IType expressionType, AstNode node) |
||||
{ |
||||
|
||||
var csResolver = refactoringContext.Resolver.GetResolverStateBefore(node); |
||||
var builder = new TypeSystemAstBuilder(csResolver); |
||||
return builder.ConvertType(expressionType); |
||||
} |
||||
} |
||||
} |
||||
} |
||||
@ -0,0 +1,96 @@
@@ -0,0 +1,96 @@
|
||||
//
|
||||
// RemoveFieldRefactoryActionTests.cs
|
||||
//
|
||||
// Author:
|
||||
// Ciprian Khlud <ciprian.mustiata@yahoo.com>
|
||||
//
|
||||
// Copyright (c) 2013 Ciprian Khlud
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in
|
||||
// all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
// THE SOFTWARE.
|
||||
|
||||
using NUnit.Framework; |
||||
|
||||
namespace ICSharpCode.NRefactory.CSharp.CodeActions |
||||
{ |
||||
[TestFixture] |
||||
public class RemoveFieldRefactoryActionTests : ContextActionTestBase |
||||
{ |
||||
[Test] |
||||
public void RemoveOneField() |
||||
{ |
||||
Test<RemoveFieldRefactoryAction>( |
||||
@"
|
||||
class A { |
||||
int $field; |
||||
} |
||||
",
|
||||
@"
|
||||
class A { |
||||
} |
||||
"
|
||||
);} |
||||
|
||||
[Test] |
||||
public void RemoveOneFieldAndAssignmentValue() |
||||
{ |
||||
Test<RemoveFieldRefactoryAction>( |
||||
@"
|
||||
class A { |
||||
int $field; |
||||
A() { |
||||
field = 3; |
||||
} |
||||
} |
||||
",
|
||||
@"
|
||||
class A { |
||||
A() { |
||||
} |
||||
} |
||||
"
|
||||
); |
||||
} |
||||
|
||||
[Test] |
||||
public void RemoveOneFieldAndAssignmentValueAndMethodCall() |
||||
{ |
||||
Test<RemoveFieldRefactoryAction>( |
||||
@"
|
||||
class A { |
||||
int $field; |
||||
A() { |
||||
field = 3; |
||||
if(field!=0) |
||||
Method(field); |
||||
} |
||||
} |
||||
",
|
||||
@"
|
||||
class A { |
||||
A() { |
||||
if(TODO!=0) |
||||
Method(TODO); |
||||
} |
||||
} |
||||
"
|
||||
); |
||||
} |
||||
} |
||||
} |
||||
|
||||
@ -0,0 +1,92 @@
@@ -0,0 +1,92 @@
|
||||
//
|
||||
// DuplicateBodyMethodIssueTests.cs
|
||||
//
|
||||
// Author:
|
||||
// Ciprian Khlud <ciprian.mustiata@yahoo.com>
|
||||
//
|
||||
// Copyright (c) 2013 Ciprian Khlud
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in
|
||||
// all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
// THE SOFTWARE.
|
||||
|
||||
using NUnit.Framework; |
||||
using ICSharpCode.NRefactory.CSharp.Refactoring; |
||||
|
||||
namespace ICSharpCode.NRefactory.CSharp.CodeIssues |
||||
{ |
||||
|
||||
[TestFixture] |
||||
public class DuplicateBodyMethodIssueTests : InspectionActionTestBase |
||||
{ |
||||
|
||||
[Test] |
||||
public void Test() |
||||
{ |
||||
Test<DuplicateBodyMethodIssue>( |
||||
@"class TestClass {
|
||||
void Test () |
||||
{ |
||||
int a = 2; |
||||
} |
||||
void $Test2 () { |
||||
int a = 2; |
||||
} |
||||
}",
|
||||
@"class TestClass {
|
||||
void Test () |
||||
{ |
||||
int a = 2; |
||||
} |
||||
void Test2 () { |
||||
Test (); |
||||
} |
||||
} |
||||
"
|
||||
); |
||||
} |
||||
[Test] |
||||
public void TestNonVoid() |
||||
{ |
||||
Test<DuplicateBodyMethodIssue>( |
||||
@"class TestClass {
|
||||
int Test () |
||||
{ |
||||
int a = 2; |
||||
return a; |
||||
} |
||||
int $Test2 () { |
||||
int a = 2; |
||||
return a; |
||||
} |
||||
}",
|
||||
@"class TestClass {
|
||||
int Test () |
||||
{ |
||||
int a = 2; |
||||
return a; |
||||
} |
||||
int Test2 () { |
||||
return Test (); |
||||
} |
||||
} |
||||
"
|
||||
); |
||||
} |
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,98 @@
@@ -0,0 +1,98 @@
|
||||
//
|
||||
// ParameterCanBeIEnumerableTests.cs
|
||||
//
|
||||
// Author:
|
||||
// Ciprian Khlud <ciprian.mustiata@yahoo.com>
|
||||
//
|
||||
// Copyright (c) 2013 Ciprian Khlud
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in
|
||||
// all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
// THE SOFTWARE.
|
||||
|
||||
using System; |
||||
using NUnit.Framework; |
||||
using ICSharpCode.NRefactory.CSharp.CodeActions; |
||||
using ICSharpCode.NRefactory.CSharp.Refactoring; |
||||
|
||||
namespace ICSharpCode.NRefactory.CSharp.CodeIssues |
||||
{ |
||||
[TestFixture] |
||||
public class ParameterCanBeIEnumerableTests : InspectionActionTestBase |
||||
{ |
||||
[Test] |
||||
public void CheckIEnumerable() |
||||
{ |
||||
var input = @"
|
||||
using System.Collections.Generic; |
||||
class TestClass |
||||
{ |
||||
void Write(string[] texts) |
||||
{ |
||||
foreach(var item in texts) { } |
||||
} |
||||
}";
|
||||
TestRefactoringContext context; |
||||
var issues = GetIssues(new ParameterCanBeIEnumerableIssue(), input, out context); |
||||
Assert.AreEqual(1, issues.Count); |
||||
var issue = issues[0]; |
||||
// Suggested types: IList<T> and IReadOnlyList<T>
|
||||
Assert.AreEqual(1, issue.Actions.Count); |
||||
|
||||
CheckFix(context, issues[0], @"
|
||||
using System.Collections.Generic; |
||||
class TestClass |
||||
{ |
||||
void Write(IEnumerable<string> texts) |
||||
{ |
||||
foreach(var item in texts) { } |
||||
} |
||||
}");
|
||||
} |
||||
[Test] |
||||
public void CheckIList() |
||||
{ |
||||
var input = @"
|
||||
using System.Collections.Generic; |
||||
class TestClass |
||||
{ |
||||
void Write(List<string> texts) |
||||
{ |
||||
if(texts.Count == 0)return; |
||||
foreach(var item in texts) { } |
||||
} |
||||
}";
|
||||
TestRefactoringContext context; |
||||
var issues = GetIssues(new ParameterCanBeIEnumerableIssue(), input, out context); |
||||
Assert.AreEqual(1, issues.Count); |
||||
var issue = issues[0]; |
||||
// Suggested types: IList<T> and IReadOnlyList<T>
|
||||
Assert.AreEqual(1, issue.Actions.Count); |
||||
|
||||
CheckFix(context, issues[0], @"
|
||||
using System.Collections.Generic; |
||||
class TestClass |
||||
{ |
||||
void Write(ICollection<string> texts) |
||||
{ |
||||
if(texts.Count == 0)return; |
||||
foreach(var item in texts) { } |
||||
} |
||||
}");
|
||||
} |
||||
} |
||||
} |
||||
Binary file not shown.
Loading…
Reference in new issue