Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 0d926e6

Browse files
committed
on throw analyzer if not using System still works
1 parent 5a94909 commit 0d926e6

File tree

2 files changed

+44
-33
lines changed

2 files changed

+44
-33
lines changed

src/CodeCracker/RethrowExceptionCodeFixProvider.cs

+13-5
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
using Microsoft.CodeAnalysis.Rename;
1414
using Microsoft.CodeAnalysis.Text;
1515
using Microsoft.CodeAnalysis.Formatting;
16+
using Microsoft.CodeAnalysis.Simplification;
1617

1718
namespace CodeCracker
1819
{
@@ -59,10 +60,18 @@ private async Task<Document> MakeThrowAsync(Document document, ThrowStatementSyn
5960
private async Task<Document> MakeThrowAsInnerAsync(Document document, ThrowStatementSyntax throwStatement, CancellationToken cancellationToken)
6061
{
6162
var semanticModel = await document.GetSemanticModelAsync(cancellationToken);
62-
63-
//var exceptionType = SyntaxFactory.ParseExpression("System.Exception").WithAdditionalAnnotations(Simplifier.Annotation);
64-
//var newThrow = (ThrowStatementSyntax)SyntaxFactory.ThrowStatement(SyntaxFactory.ObjectCreationExpression(exceptionType, SyntaxFactory.ArgumentList(new SeparatedSyntaxList<ArgumentSyntax>() { SyntaxFactory.Argument(SyntaxFactory.ParseExpression("ex")) }), SyntaxFactory.InitializerExpression(SyntaxKind.ObjectCreationExpression)));
65-
var newThrow = (ThrowStatementSyntax)SyntaxFactory.ParseStatement("throw new Exception(\"some reason to rethrow\", ex);")
63+
var ident = throwStatement.Expression as IdentifierNameSyntax;
64+
var exSymbol = semanticModel.GetSymbolInfo(ident).Symbol as ILocalSymbol;
65+
var exceptionType = SyntaxFactory.ParseTypeName("System.Exception").WithAdditionalAnnotations(Simplifier.Annotation);
66+
var objectCreationExpressionSyntax = SyntaxFactory.ObjectCreationExpression(exceptionType).WithArgumentList(
67+
SyntaxFactory.ArgumentList(
68+
SyntaxFactory.SeparatedList(
69+
new[]
70+
{
71+
SyntaxFactory.Argument(SyntaxFactory.ParseExpression("\"some reason to rethrow\"")),
72+
SyntaxFactory.Argument(SyntaxFactory.ParseExpression(exSymbol.Name))
73+
})));
74+
var newThrow = SyntaxFactory.ThrowStatement(objectCreationExpressionSyntax)
6675
.WithLeadingTrivia(throwStatement.GetLeadingTrivia())
6776
.WithTrailingTrivia(throwStatement.GetTrailingTrivia())
6877
.WithAdditionalAnnotations(Formatter.Annotation);
@@ -71,7 +80,6 @@ private async Task<Document> MakeThrowAsInnerAsync(Document document, ThrowState
7180
var newRoot = root.ReplaceNode(throwStatement, newThrow);
7281
var newDocument = document.WithSyntaxRoot(newRoot);
7382
return newDocument;
74-
7583
}
7684
}
7785
}

test/CodeCracker.Test/RethrowExceptionTests.cs

+31-28
Original file line numberDiff line numberDiff line change
@@ -10,28 +10,22 @@ namespace CodeCracker.Test
1010
{
1111
public class RethrowExceptionTests : CodeFixVerifier
1212
{
13-
private const string test = @"
14-
using System;
15-
using System.Collections.Generic;
16-
using System.Linq;
17-
using System.Text;
18-
using System.Threading.Tasks;
19-
using System.Diagnostics;
20-
13+
private const string sourceWithoutUsingSystem = @"
2114
namespace ConsoleApplication1
2215
{
2316
class TypeName
2417
{
2518
public void Foo()
2619
{
2720
try { }
28-
catch (Exception ex)
21+
catch (System.Exception ex)
2922
{
3023
throw ex;
3124
}
3225
}
3326
}
3427
}";
28+
private const string sourceWithUsingSystem = "\n using System;" + sourceWithoutUsingSystem;
3529

3630
[Fact]
3731
public void WhenThrowingOriginalExceptionAnalyzerCreatesDiagnostic()
@@ -43,11 +37,11 @@ public void WhenThrowingOriginalExceptionAnalyzerCreatesDiagnostic()
4337
Severity = DiagnosticSeverity.Error,
4438
Locations =
4539
new[] {
46-
new DiagnosticResultLocation("Test0.cs", 18, 21)
40+
new DiagnosticResultLocation("Test0.cs", 12, 21)
4741
}
4842
};
4943

50-
VerifyCSharpDiagnostic(test, expected);
44+
VerifyCSharpDiagnostic(sourceWithUsingSystem, expected);
5145
}
5246

5347
[Fact]
@@ -56,56 +50,65 @@ public void WhenThrowingOriginalExceptionAndApplyingThrowNewExceptionFix()
5650

5751
var fixtest = @"
5852
using System;
59-
using System.Collections.Generic;
60-
using System.Linq;
61-
using System.Text;
62-
using System.Threading.Tasks;
63-
using System.Diagnostics;
64-
6553
namespace ConsoleApplication1
6654
{
6755
class TypeName
6856
{
6957
public void Foo()
7058
{
7159
try { }
72-
catch (Exception ex)
60+
catch (System.Exception ex)
7361
{
7462
throw new Exception(""some reason to rethrow"", ex);
7563
}
7664
}
7765
}
7866
}";
79-
VerifyCSharpFix(test, fixtest, 0);
67+
VerifyCSharpFix(sourceWithUsingSystem, fixtest, 0);
8068
}
8169

8270
[Fact]
8371
public void WhenThrowingOriginalExceptionAndApplyingRethrowFix()
8472
{
85-
8673
var fixtest = @"
8774
using System;
88-
using System.Collections.Generic;
89-
using System.Linq;
90-
using System.Text;
91-
using System.Threading.Tasks;
92-
using System.Diagnostics;
93-
9475
namespace ConsoleApplication1
9576
{
9677
class TypeName
9778
{
9879
public void Foo()
9980
{
10081
try { }
101-
catch (Exception ex)
82+
catch (System.Exception ex)
10283
{
10384
throw;
10485
}
10586
}
10687
}
10788
}";
108-
VerifyCSharpFix(test, fixtest, 1);
89+
VerifyCSharpFix(sourceWithUsingSystem, fixtest, 1);
90+
}
91+
92+
[Fact]
93+
public void WhenThrowingOriginalExceptionAndApplyingThrowNewExceptionCompleteExceptionDeclationFix()
94+
{
95+
96+
var fixtest = @"
97+
namespace ConsoleApplication1
98+
{
99+
class TypeName
100+
{
101+
public void Foo()
102+
{
103+
try { }
104+
catch (System.Exception ex)
105+
{
106+
throw new System.Exception(""some reason to rethrow"", ex);
107+
}
108+
}
109+
}
110+
}";
111+
VerifyCSharpFix(sourceWithoutUsingSystem, fixtest, 0);
109112
}
110113

111114

0 commit comments

Comments
 (0)