From d9dbd25be7da70aa6ce6eb157ceb32eed4136a7d Mon Sep 17 00:00:00 2001 From: Mansheng Yang Date: Thu, 2 Aug 2012 21:29:50 +0800 Subject: [PATCH] [CodeIssue] Added RedundantArrayInitializerCommaIssue --- .../ICSharpCode.NRefactory.CSharp.csproj | 1 + .../RedundantArrayInitializerCommaIssue.cs | 65 +++++++++++++++++++ ...edundantArrayInitializerCommaIssueTests.cs | 57 ++++++++++++++++ .../ICSharpCode.NRefactory.Tests.csproj | 1 + 4 files changed, 124 insertions(+) create mode 100644 ICSharpCode.NRefactory.CSharp/Refactoring/CodeIssues/RedundantArrayInitializerCommaIssue.cs create mode 100644 ICSharpCode.NRefactory.Tests/CSharp/CodeIssues/RedundantArrayInitializerCommaIssueTests.cs diff --git a/ICSharpCode.NRefactory.CSharp/ICSharpCode.NRefactory.CSharp.csproj b/ICSharpCode.NRefactory.CSharp/ICSharpCode.NRefactory.CSharp.csproj index feac4eda7d..64787b437d 100644 --- a/ICSharpCode.NRefactory.CSharp/ICSharpCode.NRefactory.CSharp.csproj +++ b/ICSharpCode.NRefactory.CSharp/ICSharpCode.NRefactory.CSharp.csproj @@ -301,6 +301,7 @@ + diff --git a/ICSharpCode.NRefactory.CSharp/Refactoring/CodeIssues/RedundantArrayInitializerCommaIssue.cs b/ICSharpCode.NRefactory.CSharp/Refactoring/CodeIssues/RedundantArrayInitializerCommaIssue.cs new file mode 100644 index 0000000000..5a748db76a --- /dev/null +++ b/ICSharpCode.NRefactory.CSharp/Refactoring/CodeIssues/RedundantArrayInitializerCommaIssue.cs @@ -0,0 +1,65 @@ +// +// RedundantArrayInitializerCommaIssue.cs +// +// Author: +// Mansheng Yang +// +// Copyright (c) 2012 Mansheng Yang +// +// 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; + +namespace ICSharpCode.NRefactory.CSharp.Refactoring +{ + [IssueDescription ("Redundant comma in array initializer", + Description = "Redundant comma in array initializer.", + Category = IssueCategories.Redundancies, + Severity = Severity.Suggestion, + IssueMarker = IssueMarker.GrayOut)] + public class RedundantArrayInitializerCommaIssue : ICodeIssueProvider + { + public IEnumerable GetIssues (BaseRefactoringContext context) + { + return new GatherVisitor (context).GetIssues (); + } + + class GatherVisitor : GatherVisitorBase + { + public GatherVisitor (BaseRefactoringContext ctx) + : base (ctx) + { + } + + public override void VisitArrayInitializerExpression (ArrayInitializerExpression arrayInitializerExpression) + { + base.VisitArrayInitializerExpression (arrayInitializerExpression); + + if (arrayInitializerExpression.IsSingleElement) + return; + + var commaToken = arrayInitializerExpression.RBraceToken.PrevSibling as CSharpTokenNode; + if (commaToken == null || commaToken.GetText () != ",") + return; + AddIssue (commaToken, ctx.TranslateString ("Remove redundant comma in array initializer"), + script => script.Remove (commaToken)); + } + } + } +} diff --git a/ICSharpCode.NRefactory.Tests/CSharp/CodeIssues/RedundantArrayInitializerCommaIssueTests.cs b/ICSharpCode.NRefactory.Tests/CSharp/CodeIssues/RedundantArrayInitializerCommaIssueTests.cs new file mode 100644 index 0000000000..cd8d561ed7 --- /dev/null +++ b/ICSharpCode.NRefactory.Tests/CSharp/CodeIssues/RedundantArrayInitializerCommaIssueTests.cs @@ -0,0 +1,57 @@ +// +// RedundantArrayInitializerCommaIssueTests.cs +// +// Author: +// Mansheng Yang +// +// Copyright (c) 2012 Mansheng Yang +// +// 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 ICSharpCode.NRefactory.CSharp.Refactoring; +using NUnit.Framework; + +namespace ICSharpCode.NRefactory.CSharp.CodeIssues +{ + [TestFixture] + public class RedundantArrayInitializerCommaIssueTests : InspectionActionTestBase + { + [Test] + public void Test () + { + var input = @" +class TestClass +{ + void TestMethod () + { + var a = new int[] { 1, 2, }; + } +}"; + var output = @" +class TestClass +{ + void TestMethod () + { + var a = new int[] { 1, 2 }; + } +}"; + Test (input, 1, output); + } + } +} diff --git a/ICSharpCode.NRefactory.Tests/ICSharpCode.NRefactory.Tests.csproj b/ICSharpCode.NRefactory.Tests/ICSharpCode.NRefactory.Tests.csproj index 839b689bc5..1d0390279d 100644 --- a/ICSharpCode.NRefactory.Tests/ICSharpCode.NRefactory.Tests.csproj +++ b/ICSharpCode.NRefactory.Tests/ICSharpCode.NRefactory.Tests.csproj @@ -164,6 +164,7 @@ +