56 changed files with 2204 additions and 473 deletions
@ -0,0 +1,103 @@
@@ -0,0 +1,103 @@
|
||||
//
|
||||
// BaseRefactoringContext.cs
|
||||
//
|
||||
// Author:
|
||||
// Mike Krüger <mkrueger@xamarin.com>
|
||||
//
|
||||
// Copyright (c) 2012 Xamarin <http://xamarin.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; |
||||
using System.Linq; |
||||
using System.Threading; |
||||
using ICSharpCode.NRefactory.CSharp.Resolver; |
||||
using ICSharpCode.NRefactory.CSharp.TypeSystem; |
||||
using ICSharpCode.NRefactory.Semantics; |
||||
using ICSharpCode.NRefactory.TypeSystem; |
||||
using ICSharpCode.NRefactory.TypeSystem.Implementation; |
||||
using ICSharpCode.NRefactory.Editor; |
||||
|
||||
namespace ICSharpCode.NRefactory.CSharp.Refactoring |
||||
{ |
||||
public abstract class BaseRefactoringContext |
||||
{ |
||||
protected readonly CSharpAstResolver resolver; |
||||
readonly CancellationToken cancellationToken; |
||||
|
||||
public virtual bool Supports(Version version) |
||||
{ |
||||
return true; |
||||
} |
||||
|
||||
public CancellationToken CancellationToken { |
||||
get { return cancellationToken; } |
||||
} |
||||
|
||||
public virtual AstNode RootNode { |
||||
get { return resolver.RootNode; } |
||||
} |
||||
|
||||
public ICompilation Compilation { |
||||
get { return resolver.Compilation; } |
||||
} |
||||
|
||||
public BaseRefactoringContext (ICSharpCode.NRefactory.CSharp.Resolver.CSharpAstResolver resolver, System.Threading.CancellationToken cancellationToken) |
||||
{ |
||||
this.resolver = resolver; |
||||
this.cancellationToken = cancellationToken; |
||||
} |
||||
|
||||
#region Resolving
|
||||
public ResolveResult Resolve (AstNode node) |
||||
{ |
||||
return resolver.Resolve (node, cancellationToken); |
||||
} |
||||
|
||||
public CSharpResolver GetResolverStateBefore(AstNode node) |
||||
{ |
||||
return resolver.GetResolverStateBefore (node, cancellationToken); |
||||
} |
||||
|
||||
public CSharpResolver GetResolverStateAfter(AstNode node) |
||||
{ |
||||
return resolver.GetResolverStateAfter (node, cancellationToken); |
||||
} |
||||
|
||||
public IType ResolveType (AstType type) |
||||
{ |
||||
return resolver.Resolve (type, cancellationToken).Type; |
||||
} |
||||
|
||||
public IType GetExpectedType (Expression expression) |
||||
{ |
||||
return resolver.GetExpectedType(expression, cancellationToken); |
||||
} |
||||
|
||||
public Conversion GetConversion (Expression expression) |
||||
{ |
||||
return resolver.GetConversion(expression, cancellationToken); |
||||
} |
||||
#endregion
|
||||
|
||||
public abstract Script StartScript(); |
||||
|
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,36 @@
@@ -0,0 +1,36 @@
|
||||
//
|
||||
// IInspector.cs
|
||||
//
|
||||
// Author:
|
||||
// Mike Krüger <mkrueger@xamarin.com>
|
||||
//
|
||||
// Copyright (c) 2012 Xamarin <http://xamarin.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; |
||||
using System.Collections.Generic; |
||||
|
||||
namespace ICSharpCode.NRefactory.CSharp.Refactoring |
||||
{ |
||||
public interface IInspector |
||||
{ |
||||
IEnumerable<InspectionIssue> Run (BaseRefactoringContext context); |
||||
} |
||||
} |
||||
|
||||
@ -0,0 +1,61 @@
@@ -0,0 +1,61 @@
|
||||
//
|
||||
// InspectionIssue.cs
|
||||
//
|
||||
// Author:
|
||||
// Mike Krüger <mkrueger@xamarin.com>
|
||||
//
|
||||
// Copyright (c) 2012 Xamarin <http://xamarin.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; |
||||
|
||||
namespace ICSharpCode.NRefactory.CSharp.Refactoring |
||||
{ |
||||
public class InspectionIssue |
||||
{ |
||||
public string Title { |
||||
get; |
||||
private set; |
||||
} |
||||
|
||||
public TextLocation Start { |
||||
get; |
||||
private set; |
||||
} |
||||
|
||||
public TextLocation End { |
||||
get; |
||||
private set; |
||||
} |
||||
|
||||
public System.Action Fix { |
||||
get; |
||||
private set; |
||||
} |
||||
|
||||
public InspectionIssue (string title, TextLocation start, TextLocation end, System.Action fix) |
||||
{ |
||||
this.Title = title; |
||||
this.Start = start; |
||||
this.End = end; |
||||
this.Fix = fix; |
||||
} |
||||
} |
||||
} |
||||
|
||||
@ -0,0 +1,98 @@
@@ -0,0 +1,98 @@
|
||||
//
|
||||
// ConditionalToNullCoalescingInspector.cs
|
||||
//
|
||||
// Author:
|
||||
// Mike Krüger <mkrueger@xamarin.com>
|
||||
//
|
||||
// Copyright (c) 2012 Xamarin <http://xamarin.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; |
||||
using System.Collections.Generic; |
||||
using System.Linq; |
||||
using ICSharpCode.NRefactory.PatternMatching; |
||||
|
||||
namespace ICSharpCode.NRefactory.CSharp.Refactoring |
||||
{ |
||||
/// <summary>
|
||||
/// Checks for "a != null ? a : other"<expr>
|
||||
/// Converts to: "a ?? other"<expr>
|
||||
/// </summary>
|
||||
public class ConditionalToNullCoalescingInspector : IInspector |
||||
{ |
||||
static readonly Pattern pattern = new Choice { |
||||
// a != null ? a : other
|
||||
new ConditionalExpression( |
||||
PatternHelper.CommutativeOperator(new AnyNode("a"), BinaryOperatorType.InEquality, new NullReferenceExpression()), |
||||
new Backreference("a"), |
||||
new AnyNode("other") |
||||
), |
||||
// a == null ? other : a
|
||||
new ConditionalExpression( |
||||
PatternHelper.CommutativeOperator(new AnyNode("a"), BinaryOperatorType.Equality, new NullReferenceExpression()), |
||||
new AnyNode("other"), |
||||
new Backreference("a") |
||||
), |
||||
}; |
||||
|
||||
string title = "Convert to '??' expression"; |
||||
|
||||
public string Title { |
||||
get { |
||||
return title; |
||||
} |
||||
set { |
||||
title = value; |
||||
} |
||||
} |
||||
|
||||
public IEnumerable<InspectionIssue> Run (BaseRefactoringContext context) |
||||
{ |
||||
var visitor = new GatherVisitor (context, this); |
||||
context.RootNode.AcceptVisitor (visitor); |
||||
return visitor.FoundIssues; |
||||
} |
||||
|
||||
class GatherVisitor : GatherVisitorBase |
||||
{ |
||||
readonly ConditionalToNullCoalescingInspector inspector; |
||||
|
||||
public GatherVisitor (BaseRefactoringContext ctx, ConditionalToNullCoalescingInspector inspector) : base (ctx) |
||||
{ |
||||
this.inspector = inspector; |
||||
} |
||||
|
||||
public override void VisitConditionalExpression(ConditionalExpression conditionalExpression) |
||||
{ |
||||
Match m = pattern.Match(conditionalExpression); |
||||
if (m.Success) { |
||||
var a = m.Get<Expression>("a").Single(); |
||||
var other = m.Get<Expression>("other").Single(); |
||||
AddIssue(conditionalExpression, inspector.Title, delegate { |
||||
using (var script = ctx.StartScript ()) { |
||||
var expr = new BinaryOperatorExpression (a.Clone (), BinaryOperatorType.NullCoalescing, other.Clone ()); |
||||
script.Replace (conditionalExpression, expr); |
||||
} |
||||
}); |
||||
} |
||||
base.VisitConditionalExpression (conditionalExpression); |
||||
} |
||||
} |
||||
} |
||||
} |
||||
@ -0,0 +1,63 @@
@@ -0,0 +1,63 @@
|
||||
//
|
||||
// GatherVisitorBase.cs
|
||||
//
|
||||
// Author:
|
||||
// Mike Krüger <mkrueger@xamarin.com>
|
||||
//
|
||||
// Copyright (c) 2012 Xamarin <http://xamarin.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; |
||||
using ICSharpCode.NRefactory.CSharp.Refactoring; |
||||
using System.Collections.Generic; |
||||
|
||||
namespace ICSharpCode.NRefactory.CSharp |
||||
{ |
||||
class GatherVisitorBase : DepthFirstAstVisitor |
||||
{ |
||||
protected readonly BaseRefactoringContext ctx; |
||||
|
||||
public readonly List<InspectionIssue> FoundIssues = new List<InspectionIssue> (); |
||||
|
||||
public GatherVisitorBase (BaseRefactoringContext ctx) |
||||
{ |
||||
this.ctx = ctx; |
||||
} |
||||
|
||||
protected override void VisitChildren (AstNode node) |
||||
{ |
||||
if (ctx.CancellationToken.IsCancellationRequested) |
||||
return; |
||||
base.VisitChildren (node); |
||||
} |
||||
|
||||
protected void AddIssue (AstNode node, string title, System.Action fix = null) |
||||
{ |
||||
FoundIssues.Add (new InspectionIssue (title, node.StartLocation, node.EndLocation, fix)); |
||||
} |
||||
|
||||
protected void AddIssue(TextLocation start, TextLocation end, string title, System.Action fix = null) |
||||
{ |
||||
FoundIssues.Add (new InspectionIssue (title, start, end, fix)); |
||||
} |
||||
} |
||||
|
||||
|
||||
} |
||||
|
||||
@ -0,0 +1,77 @@
@@ -0,0 +1,77 @@
|
||||
//
|
||||
// NotImplementedExceptionInspector.cs
|
||||
//
|
||||
// Author:
|
||||
// Mike Krüger <mkrueger@xamarin.com>
|
||||
//
|
||||
// Copyright (c) 2012 Xamarin <http://xamarin.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; |
||||
using ICSharpCode.NRefactory.PatternMatching; |
||||
using System.Collections.Generic; |
||||
using ICSharpCode.NRefactory.TypeSystem; |
||||
|
||||
namespace ICSharpCode.NRefactory.CSharp.Refactoring |
||||
{ |
||||
/// <summary>
|
||||
/// This inspector just shows that there is a not implemented exception. It doesn't offer a fix.
|
||||
/// Should only be shown in overview bar, no underlining.
|
||||
/// </summary>
|
||||
public class NotImplementedExceptionInspector : IInspector |
||||
{ |
||||
string title = "NotImplemented exception thrown"; |
||||
|
||||
public string Title { |
||||
get { |
||||
return title; |
||||
} |
||||
set { |
||||
title = value; |
||||
} |
||||
} |
||||
|
||||
public IEnumerable<InspectionIssue> Run (BaseRefactoringContext context) |
||||
{ |
||||
var visitor = new GatherVisitor (context, this); |
||||
context.RootNode.AcceptVisitor (visitor); |
||||
return visitor.FoundIssues; |
||||
} |
||||
|
||||
class GatherVisitor : GatherVisitorBase |
||||
{ |
||||
readonly NotImplementedExceptionInspector inspector; |
||||
|
||||
public GatherVisitor (BaseRefactoringContext ctx, NotImplementedExceptionInspector inspector) : base (ctx) |
||||
{ |
||||
this.inspector = inspector; |
||||
} |
||||
|
||||
public override void VisitThrowStatement(ThrowStatement throwStatement) |
||||
{ |
||||
var result = ctx.Resolve (throwStatement.Expression); |
||||
if (result.Type.Equals (ctx.Compilation.FindType (typeof(System.NotImplementedException)))) |
||||
AddIssue (throwStatement, inspector.Title); |
||||
|
||||
base.VisitThrowStatement(throwStatement); |
||||
} |
||||
} |
||||
} |
||||
} |
||||
|
||||
@ -0,0 +1,82 @@
@@ -0,0 +1,82 @@
|
||||
//
|
||||
// RedundantInternalInspector.cs
|
||||
//
|
||||
// Author:
|
||||
// Mike Krüger <mkrueger@xamarin.com>
|
||||
//
|
||||
// Copyright (c) 2012 Xamarin <http://xamarin.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; |
||||
using ICSharpCode.NRefactory.PatternMatching; |
||||
using System.Collections.Generic; |
||||
using ICSharpCode.NRefactory.TypeSystem; |
||||
|
||||
namespace ICSharpCode.NRefactory.CSharp.Refactoring |
||||
{ |
||||
/// <summary>
|
||||
/// Finds redundant internal modifiers.
|
||||
/// </summary>
|
||||
public class RedundantInternalInspector : IInspector |
||||
{ |
||||
string title = "Remove redundant 'internal' modifier"; |
||||
|
||||
public string Title { |
||||
get { |
||||
return title; |
||||
} |
||||
set { |
||||
title = value; |
||||
} |
||||
} |
||||
|
||||
public IEnumerable<InspectionIssue> Run (BaseRefactoringContext context) |
||||
{ |
||||
var visitor = new GatherVisitor (context, this); |
||||
context.RootNode.AcceptVisitor (visitor); |
||||
return visitor.FoundIssues; |
||||
} |
||||
|
||||
class GatherVisitor : GatherVisitorBase |
||||
{ |
||||
readonly RedundantInternalInspector inspector; |
||||
|
||||
public GatherVisitor (BaseRefactoringContext ctx, RedundantInternalInspector inspector) : base (ctx) |
||||
{ |
||||
this.inspector = inspector; |
||||
} |
||||
|
||||
public override void VisitTypeDeclaration(TypeDeclaration typeDeclaration) |
||||
{ |
||||
foreach (var token_ in typeDeclaration.ModifierTokens) { |
||||
var token = token_; |
||||
if (token.Modifier == Modifiers.Internal) { |
||||
AddIssue(token, inspector.Title, delegate { |
||||
using (var script = ctx.StartScript ()) { |
||||
script.Remove(token); |
||||
} |
||||
}); |
||||
} |
||||
} |
||||
} |
||||
} |
||||
} |
||||
} |
||||
|
||||
@ -0,0 +1,95 @@
@@ -0,0 +1,95 @@
|
||||
//
|
||||
// RedundantNamespaceUsageInspector.cs
|
||||
//
|
||||
// Author:
|
||||
// Mike Krüger <mkrueger@xamarin.com>
|
||||
//
|
||||
// Copyright (c) 2012 Xamarin <http://xamarin.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; |
||||
using System.Collections.Generic; |
||||
using System.Linq; |
||||
|
||||
using ICSharpCode.NRefactory.Semantics; |
||||
using ICSharpCode.NRefactory.TypeSystem; |
||||
|
||||
namespace ICSharpCode.NRefactory.CSharp.Refactoring |
||||
{ |
||||
/// <summary>
|
||||
/// Finds redundant namespace usages.
|
||||
/// </summary>
|
||||
public class RedundantNamespaceUsageInspector : IInspector |
||||
{ |
||||
string title = "Remove redundant namespace usage"; |
||||
|
||||
public string Title { |
||||
get { |
||||
return title; |
||||
} |
||||
set { |
||||
title = value; |
||||
} |
||||
} |
||||
|
||||
public IEnumerable<InspectionIssue> Run (BaseRefactoringContext context) |
||||
{ |
||||
var visitor = new GatherVisitor (context, this); |
||||
context.RootNode.AcceptVisitor (visitor); |
||||
return visitor.FoundIssues; |
||||
} |
||||
|
||||
class GatherVisitor : GatherVisitorBase |
||||
{ |
||||
readonly RedundantNamespaceUsageInspector inspector; |
||||
|
||||
public GatherVisitor (BaseRefactoringContext ctx, RedundantNamespaceUsageInspector inspector) : base (ctx) |
||||
{ |
||||
this.inspector = inspector; |
||||
} |
||||
|
||||
public override void VisitMemberReferenceExpression(MemberReferenceExpression memberReferenceExpression) |
||||
{ |
||||
base.VisitMemberReferenceExpression(memberReferenceExpression); |
||||
|
||||
var result = ctx.Resolve(memberReferenceExpression.Target); |
||||
if (!(result is NamespaceResolveResult)) { |
||||
return; |
||||
} |
||||
var wholeResult = ctx.Resolve(memberReferenceExpression); |
||||
if (!(wholeResult is TypeResolveResult)) { |
||||
return; |
||||
} |
||||
|
||||
var state = ctx.GetResolverStateBefore(memberReferenceExpression); |
||||
var lookupName = state.LookupSimpleNameOrTypeName(memberReferenceExpression.MemberName, new List<IType> (), SimpleNameLookupMode.Expression); |
||||
|
||||
if (lookupName is TypeResolveResult && !lookupName.IsError && wholeResult.Type.Equals(lookupName.Type)) { |
||||
AddIssue(memberReferenceExpression.StartLocation, memberReferenceExpression.MemberNameToken.StartLocation, inspector.Title, delegate { |
||||
using (var script = ctx.StartScript ()) { |
||||
script.Replace(memberReferenceExpression, RefactoringAstHelper.RemoveTarget(memberReferenceExpression)); |
||||
} |
||||
} |
||||
); |
||||
} |
||||
} |
||||
} |
||||
} |
||||
} |
||||
@ -0,0 +1,143 @@
@@ -0,0 +1,143 @@
|
||||
//
|
||||
// RedundantPrivateInspector.cs
|
||||
//
|
||||
// Author:
|
||||
// Mike Krüger <mkrueger@xamarin.com>
|
||||
//
|
||||
// Copyright (c) 2012 Xamarin <http://xamarin.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; |
||||
using ICSharpCode.NRefactory.PatternMatching; |
||||
using System.Collections.Generic; |
||||
using ICSharpCode.NRefactory.TypeSystem; |
||||
|
||||
namespace ICSharpCode.NRefactory.CSharp.Refactoring |
||||
{ |
||||
/// <summary>
|
||||
/// Finds redundant internal modifiers.
|
||||
/// </summary>
|
||||
public class RedundantPrivateInspector : IInspector |
||||
{ |
||||
string title = "Remove redundant 'private' modifier"; |
||||
|
||||
public string Title { |
||||
get { |
||||
return title; |
||||
} |
||||
set { |
||||
title = value; |
||||
} |
||||
} |
||||
|
||||
public IEnumerable<InspectionIssue> Run (BaseRefactoringContext context) |
||||
{ |
||||
var visitor = new GatherVisitor (context, this); |
||||
context.RootNode.AcceptVisitor (visitor); |
||||
return visitor.FoundIssues; |
||||
} |
||||
|
||||
class GatherVisitor : GatherVisitorBase |
||||
{ |
||||
readonly RedundantPrivateInspector inspector; |
||||
|
||||
public GatherVisitor (BaseRefactoringContext ctx, RedundantPrivateInspector inspector) : base (ctx) |
||||
{ |
||||
this.inspector = inspector; |
||||
} |
||||
|
||||
void CheckNode(EntityDeclaration node) |
||||
{ |
||||
foreach (var token_ in node.ModifierTokens) { |
||||
var token = token_; |
||||
if (token.Modifier == Modifiers.Private) { |
||||
AddIssue(token, inspector.Title, delegate { |
||||
using (var script = ctx.StartScript ()) { |
||||
script.Remove(token); |
||||
} |
||||
}); |
||||
} |
||||
} |
||||
} |
||||
|
||||
public override void VisitMethodDeclaration(MethodDeclaration methodDeclaration) |
||||
{ |
||||
base.VisitMethodDeclaration(methodDeclaration); |
||||
CheckNode(methodDeclaration); |
||||
} |
||||
|
||||
public override void VisitFieldDeclaration(FieldDeclaration fieldDeclaration) |
||||
{ |
||||
base.VisitFieldDeclaration(fieldDeclaration); |
||||
CheckNode(fieldDeclaration); |
||||
} |
||||
|
||||
public override void VisitPropertyDeclaration(PropertyDeclaration propertyDeclaration) |
||||
{ |
||||
base.VisitPropertyDeclaration(propertyDeclaration); |
||||
CheckNode(propertyDeclaration); |
||||
} |
||||
|
||||
public override void VisitIndexerDeclaration(IndexerDeclaration indexerDeclaration) |
||||
{ |
||||
base.VisitIndexerDeclaration(indexerDeclaration); |
||||
CheckNode(indexerDeclaration); |
||||
} |
||||
|
||||
public override void VisitEventDeclaration(EventDeclaration eventDeclaration) |
||||
{ |
||||
base.VisitEventDeclaration(eventDeclaration); |
||||
CheckNode(eventDeclaration); |
||||
} |
||||
|
||||
public override void VisitCustomEventDeclaration(CustomEventDeclaration eventDeclaration) |
||||
{ |
||||
base.VisitCustomEventDeclaration(eventDeclaration); |
||||
CheckNode(eventDeclaration); |
||||
} |
||||
|
||||
public override void VisitConstructorDeclaration(ConstructorDeclaration constructorDeclaration) |
||||
{ |
||||
base.VisitConstructorDeclaration(constructorDeclaration); |
||||
CheckNode(constructorDeclaration); |
||||
} |
||||
|
||||
public override void VisitOperatorDeclaration(OperatorDeclaration operatorDeclaration) |
||||
{ |
||||
base.VisitOperatorDeclaration(operatorDeclaration); |
||||
CheckNode(operatorDeclaration); |
||||
} |
||||
|
||||
public override void VisitFixedFieldDeclaration(FixedFieldDeclaration fixedFieldDeclaration) |
||||
{ |
||||
base.VisitFixedFieldDeclaration(fixedFieldDeclaration); |
||||
CheckNode(fixedFieldDeclaration); |
||||
} |
||||
|
||||
public override void VisitTypeDeclaration(TypeDeclaration typeDeclaration) |
||||
{ |
||||
if (!(typeDeclaration.Parent is TypeDeclaration)) { |
||||
CheckNode(typeDeclaration); |
||||
} |
||||
base.VisitTypeDeclaration(typeDeclaration); |
||||
} |
||||
} |
||||
} |
||||
} |
||||
@ -0,0 +1,119 @@
@@ -0,0 +1,119 @@
|
||||
//
|
||||
// RedundantThisInspector.cs
|
||||
//
|
||||
// Author:
|
||||
// Mike Krüger <mkrueger@xamarin.com>
|
||||
//
|
||||
// Copyright (c) 2012 Xamarin <http://xamarin.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; |
||||
using ICSharpCode.NRefactory.PatternMatching; |
||||
using System.Collections.Generic; |
||||
using ICSharpCode.NRefactory.TypeSystem; |
||||
using ICSharpCode.NRefactory.Semantics; |
||||
using ICSharpCode.NRefactory.CSharp.Resolver; |
||||
using System.Linq; |
||||
|
||||
|
||||
namespace ICSharpCode.NRefactory.CSharp.Refactoring |
||||
{ |
||||
/// <summary>
|
||||
/// Finds redundant namespace usages.
|
||||
/// </summary>
|
||||
public class RedundantThisInspector : IInspector |
||||
{ |
||||
string title = "Remove redundant 'this.'"; |
||||
|
||||
public string Title { |
||||
get { |
||||
return title; |
||||
} |
||||
set { |
||||
title = value; |
||||
} |
||||
} |
||||
|
||||
public IEnumerable<InspectionIssue> Run (BaseRefactoringContext context) |
||||
{ |
||||
var visitor = new GatherVisitor (context, this); |
||||
context.RootNode.AcceptVisitor (visitor); |
||||
return visitor.FoundIssues; |
||||
} |
||||
|
||||
class GatherVisitor : GatherVisitorBase |
||||
{ |
||||
readonly RedundantThisInspector inspector; |
||||
|
||||
public GatherVisitor (BaseRefactoringContext ctx, RedundantThisInspector inspector) : base (ctx) |
||||
{ |
||||
this.inspector = inspector; |
||||
} |
||||
|
||||
static IMember GetMember (ResolveResult result) |
||||
{ |
||||
if (result is MemberResolveResult) { |
||||
return ((MemberResolveResult)result).Member; |
||||
} else if (result is MethodGroupResolveResult) { |
||||
return ((MethodGroupResolveResult)result).Methods.FirstOrDefault (); |
||||
} |
||||
|
||||
return null; |
||||
} |
||||
|
||||
public override void VisitThisReferenceExpression (ThisReferenceExpression thisReferenceExpression) |
||||
{ |
||||
base.VisitThisReferenceExpression (thisReferenceExpression); |
||||
var memberReference = thisReferenceExpression.Parent as MemberReferenceExpression; |
||||
if (memberReference == null) { |
||||
return; |
||||
} |
||||
|
||||
var state = ctx.GetResolverStateAfter (thisReferenceExpression); |
||||
var wholeResult = ctx.Resolve (memberReference); |
||||
|
||||
IMember member = GetMember (wholeResult); |
||||
if (member == null) { |
||||
return; |
||||
} |
||||
|
||||
var result = state.LookupSimpleNameOrTypeName (memberReference.MemberName, EmptyList<IType>.Instance, SimpleNameLookupMode.Expression); |
||||
|
||||
bool isRedundant; |
||||
if (result is MemberResolveResult) { |
||||
isRedundant = ((MemberResolveResult)result).Member.Region.Equals(member.Region); |
||||
} else if (result is MethodGroupResolveResult) { |
||||
isRedundant = ((MethodGroupResolveResult)result).Methods.Any(m => m.Region.Equals(member.Region)); |
||||
} else { |
||||
return; |
||||
} |
||||
|
||||
if (isRedundant) { |
||||
AddIssue(thisReferenceExpression.StartLocation, memberReference.MemberNameToken.StartLocation, inspector.Title, delegate { |
||||
using (var script = ctx.StartScript ()) { |
||||
script.Replace(memberReference, RefactoringAstHelper.RemoveTarget(memberReference)); |
||||
} |
||||
} |
||||
); |
||||
} |
||||
} |
||||
} |
||||
} |
||||
} |
||||
@ -0,0 +1,77 @@
@@ -0,0 +1,77 @@
|
||||
//
|
||||
// RedundantUsingInspector.cs
|
||||
//
|
||||
// Author:
|
||||
// Mike Krüger <mkrueger@xamarin.com>
|
||||
//
|
||||
// Copyright (c) 2012 Xamarin <http://xamarin.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; |
||||
using ICSharpCode.NRefactory.PatternMatching; |
||||
using System.Collections.Generic; |
||||
using ICSharpCode.NRefactory.TypeSystem; |
||||
using ICSharpCode.NRefactory.Semantics; |
||||
|
||||
namespace ICSharpCode.NRefactory.CSharp.Refactoring |
||||
{ |
||||
/// <summary>
|
||||
/// Finds redundant using declarations.
|
||||
/// </summary>
|
||||
public class RedundantUsingInspector : IInspector |
||||
{ |
||||
string title = "Remove redundant using"; |
||||
|
||||
public string Title { |
||||
get { |
||||
return title; |
||||
} |
||||
set { |
||||
title = value; |
||||
} |
||||
} |
||||
|
||||
public IEnumerable<InspectionIssue> Run (BaseRefactoringContext context) |
||||
{ |
||||
var visitor = new GatherVisitor (context, this); |
||||
context.RootNode.AcceptVisitor (visitor); |
||||
return visitor.FoundIssues; |
||||
} |
||||
|
||||
class GatherVisitor : GatherVisitorBase |
||||
{ |
||||
readonly RedundantUsingInspector inspector; |
||||
|
||||
public GatherVisitor (BaseRefactoringContext ctx, RedundantUsingInspector inspector) : base (ctx) |
||||
{ |
||||
this.inspector = inspector; |
||||
} |
||||
|
||||
public override void VisitUsingDeclaration(UsingDeclaration usingDeclaration) |
||||
{ |
||||
base.VisitUsingDeclaration(usingDeclaration); |
||||
// TODO
|
||||
// return cSharpResolver.usedScopes
|
||||
// .OfType<ITypeOrNamespaceReference> ()
|
||||
// .Any (u => u.ResolveNamespace (ctx).NamespaceName == ns) || additionalNamespaces.Contains (ns);
|
||||
} |
||||
} |
||||
} |
||||
} |
||||
@ -0,0 +1,121 @@
@@ -0,0 +1,121 @@
|
||||
//
|
||||
// StringIsNullOrEmptyInspector.cs
|
||||
//
|
||||
// Author:
|
||||
// Mike Krüger <mkrueger@xamarin.com>
|
||||
//
|
||||
// Copyright (c) 2012 Xamarin <http://xamarin.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; |
||||
using System.Collections.Generic; |
||||
using System.Linq; |
||||
using ICSharpCode.NRefactory.PatternMatching; |
||||
|
||||
namespace ICSharpCode.NRefactory.CSharp.Refactoring |
||||
{ |
||||
/// <summary>
|
||||
/// Checks for str == null && str == ""
|
||||
/// Converts to: string.IsNullOrEmpty (str)
|
||||
/// </summary>
|
||||
public class StringIsNullOrEmptyInspector : IInspector |
||||
{ |
||||
static readonly Pattern pattern = new Choice { |
||||
// str == null || str == ""
|
||||
new BinaryOperatorExpression ( |
||||
PatternHelper.CommutativeOperator(new AnyNode ("str"), BinaryOperatorType.Equality, new NullReferenceExpression ()), |
||||
BinaryOperatorType.ConditionalOr, |
||||
PatternHelper.CommutativeOperator(new Backreference ("str"), BinaryOperatorType.Equality, new PrimitiveExpression ("")) |
||||
), |
||||
// str == "" || str == null
|
||||
new BinaryOperatorExpression ( |
||||
PatternHelper.CommutativeOperator(new Backreference ("str"), BinaryOperatorType.Equality, new PrimitiveExpression ("")), |
||||
BinaryOperatorType.ConditionalOr, |
||||
PatternHelper.CommutativeOperator(new AnyNode ("str"), BinaryOperatorType.Equality, new NullReferenceExpression ()) |
||||
), |
||||
}; |
||||
|
||||
static readonly Pattern negPattern = new Choice { |
||||
// str != null && str != ""
|
||||
new BinaryOperatorExpression ( |
||||
PatternHelper.CommutativeOperator(new AnyNode ("str"), BinaryOperatorType.InEquality, new NullReferenceExpression ()), |
||||
BinaryOperatorType.ConditionalAnd, |
||||
PatternHelper.CommutativeOperator(new Backreference ("str"), BinaryOperatorType.InEquality, new PrimitiveExpression ("")) |
||||
), |
||||
// str != "" && str != null
|
||||
new BinaryOperatorExpression ( |
||||
PatternHelper.CommutativeOperator(new Backreference ("str"), BinaryOperatorType.InEquality, new PrimitiveExpression ("")), |
||||
BinaryOperatorType.ConditionalAnd, |
||||
PatternHelper.CommutativeOperator(new AnyNode ("str"), BinaryOperatorType.InEquality, new NullReferenceExpression ()) |
||||
), |
||||
}; |
||||
|
||||
string title = "Use string.IsNullOrEmpty"; |
||||
|
||||
public string Title { |
||||
get { |
||||
return title; |
||||
} |
||||
set { |
||||
title = value; |
||||
} |
||||
} |
||||
|
||||
public IEnumerable<InspectionIssue> Run (BaseRefactoringContext context) |
||||
{ |
||||
var visitor = new GatherVisitor (context, this); |
||||
context.RootNode.AcceptVisitor (visitor); |
||||
return visitor.FoundIssues; |
||||
} |
||||
|
||||
class GatherVisitor : GatherVisitorBase |
||||
{ |
||||
readonly StringIsNullOrEmptyInspector inspector; |
||||
|
||||
public GatherVisitor (BaseRefactoringContext ctx, StringIsNullOrEmptyInspector inspector) : base (ctx) |
||||
{ |
||||
this.inspector = inspector; |
||||
} |
||||
|
||||
public override void VisitBinaryOperatorExpression(BinaryOperatorExpression binaryOperatorExpression) |
||||
{ |
||||
base.VisitBinaryOperatorExpression(binaryOperatorExpression); |
||||
Match m = pattern.Match(binaryOperatorExpression); |
||||
bool isNegated = false; |
||||
if (!m.Success) { |
||||
m = negPattern.Match(binaryOperatorExpression); |
||||
isNegated = true; |
||||
} |
||||
if (m.Success) { |
||||
var str = m.Get<Expression>("str").Single(); |
||||
AddIssue(binaryOperatorExpression, inspector.Title, delegate { |
||||
using (var script = ctx.StartScript ()) { |
||||
Expression expr = new PrimitiveType ("string").Invoke("IsNullOrEmpty", str.Clone()); |
||||
if (isNegated) |
||||
expr = new UnaryOperatorExpression (UnaryOperatorType.Not, expr); |
||||
script.Replace(binaryOperatorExpression, expr); |
||||
} |
||||
}); |
||||
return; |
||||
} |
||||
} |
||||
|
||||
} |
||||
} |
||||
} |
||||
@ -0,0 +1,113 @@
@@ -0,0 +1,113 @@
|
||||
//
|
||||
// UseVarKeywordInspector.cs
|
||||
//
|
||||
// Author:
|
||||
// Mike Krüger <mkrueger@xamarin.com>
|
||||
//
|
||||
// Copyright (c) 2012 Xamarin <http://xamarin.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; |
||||
using ICSharpCode.NRefactory.PatternMatching; |
||||
using System.Collections.Generic; |
||||
using ICSharpCode.NRefactory.TypeSystem; |
||||
using ICSharpCode.NRefactory.Semantics; |
||||
using System.Linq; |
||||
|
||||
namespace ICSharpCode.NRefactory.CSharp.Refactoring |
||||
{ |
||||
/// <summary>
|
||||
/// Checks for places where the 'var' keyword can be used. Note that the action is actually done with a context
|
||||
/// action.
|
||||
/// </summary>
|
||||
public class UseVarKeywordInspector : IInspector |
||||
{ |
||||
string title = "Use 'var' keyword"; |
||||
|
||||
public string Title { |
||||
get { |
||||
return title; |
||||
} |
||||
set { |
||||
title = value; |
||||
} |
||||
} |
||||
|
||||
public IEnumerable<InspectionIssue> Run (BaseRefactoringContext context) |
||||
{ |
||||
var visitor = new GatherVisitor (context, this); |
||||
context.RootNode.AcceptVisitor (visitor); |
||||
return visitor.FoundIssues; |
||||
} |
||||
|
||||
class GatherVisitor : GatherVisitorBase |
||||
{ |
||||
readonly UseVarKeywordInspector inspector; |
||||
|
||||
public GatherVisitor (BaseRefactoringContext ctx, UseVarKeywordInspector inspector) : base (ctx) |
||||
{ |
||||
this.inspector = inspector; |
||||
} |
||||
|
||||
public override void VisitVariableDeclarationStatement(VariableDeclarationStatement variableDeclarationStatement) |
||||
{ |
||||
base.VisitVariableDeclarationStatement(variableDeclarationStatement); |
||||
if (variableDeclarationStatement.Type is PrimitiveType) { |
||||
return; |
||||
} |
||||
if (variableDeclarationStatement.Type is SimpleType && ((SimpleType)variableDeclarationStatement.Type).Identifier == "var") { |
||||
return; |
||||
} |
||||
if (variableDeclarationStatement.Variables.Count != 1) { |
||||
return; |
||||
} |
||||
|
||||
//only checks for cases where the type would be obvious - assignment of new, cast, etc.
|
||||
//also check the type actually matches else the user might want to assign different subclasses later
|
||||
var v = variableDeclarationStatement.Variables.Single(); |
||||
|
||||
var arrCreate = v.Initializer as ArrayCreateExpression; |
||||
if (arrCreate != null) { |
||||
var n = variableDeclarationStatement.Type as ComposedType; |
||||
//FIXME: check the specifier compatibility
|
||||
if (n != null && n.ArraySpecifiers.Any() && n.BaseType.IsMatch(arrCreate.Type)) { |
||||
AddIssue(variableDeclarationStatement); |
||||
} |
||||
} |
||||
var objCreate = v.Initializer as ObjectCreateExpression; |
||||
if (objCreate != null && objCreate.Type.IsMatch(variableDeclarationStatement.Type)) { |
||||
AddIssue(variableDeclarationStatement); |
||||
} |
||||
var asCast = v.Initializer as AsExpression; |
||||
if (asCast != null && asCast.Type.IsMatch(variableDeclarationStatement.Type)) { |
||||
AddIssue(variableDeclarationStatement); |
||||
} |
||||
var cast = v.Initializer as CastExpression; |
||||
if (cast != null && cast.Type.IsMatch(variableDeclarationStatement.Type)) { |
||||
AddIssue(variableDeclarationStatement); |
||||
} |
||||
} |
||||
|
||||
void AddIssue(VariableDeclarationStatement variableDeclarationStatement) |
||||
{ |
||||
AddIssue(variableDeclarationStatement.Type, inspector.Title); |
||||
} |
||||
} |
||||
} |
||||
} |
||||
@ -0,0 +1,40 @@
@@ -0,0 +1,40 @@
|
||||
// Copyright (c) AlphaSierraPapa for the SharpDevelop Team
|
||||
//
|
||||
// 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.PatternMatching; |
||||
|
||||
namespace ICSharpCode.NRefactory.CSharp.Refactoring |
||||
{ |
||||
/// <summary>
|
||||
/// Helper class for constructing pattern ASTs.
|
||||
/// </summary>
|
||||
public class PatternHelper |
||||
{ |
||||
/// <summary>
|
||||
/// Produces a choice pattern for <c>expr1 op expr2</c> or <c>expr2 op expr1</c>.
|
||||
/// </summary>
|
||||
public static Expression CommutativeOperator(Expression expr1, BinaryOperatorType op, Expression expr2) |
||||
{ |
||||
return new Choice { |
||||
new BinaryOperatorExpression(expr1, op, expr2), |
||||
new BinaryOperatorExpression(expr2.Clone(), op, expr1.Clone()) |
||||
}; |
||||
} |
||||
} |
||||
} |
||||
@ -0,0 +1,49 @@
@@ -0,0 +1,49 @@
|
||||
// Copyright (c) AlphaSierraPapa for the SharpDevelop Team
|
||||
//
|
||||
// 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 System.Collections.Generic; |
||||
using System.Linq; |
||||
|
||||
namespace ICSharpCode.NRefactory.CSharp.Refactoring |
||||
{ |
||||
/// <summary>
|
||||
/// Helper methods for constructing ASTs for refactoring.
|
||||
/// These helpers work with frozen ASTs, i.e. they clone input nodes.
|
||||
/// </summary>
|
||||
public class RefactoringAstHelper |
||||
{ |
||||
/// <summary>
|
||||
/// Removes the target from a member reference while preserving the identifier and type arguments.
|
||||
/// </summary>
|
||||
public static IdentifierExpression RemoveTarget(MemberReferenceExpression mre) |
||||
{ |
||||
IdentifierExpression ident = new IdentifierExpression(mre.MemberName); |
||||
ident.TypeArguments.AddRange(mre.TypeArguments.Select(t => t.Clone())); |
||||
return ident; |
||||
} |
||||
|
||||
/// <summary>
|
||||
/// Removes the target from a member reference while preserving the identifier and type arguments.
|
||||
/// </summary>
|
||||
public static SimpleType RemoveTarget(MemberType memberType) |
||||
{ |
||||
return new SimpleType(memberType.MemberName, memberType.TypeArguments.Select(t => t.Clone())); |
||||
} |
||||
} |
||||
} |
||||
Loading…
Reference in new issue