Skip to content

Commit 0746739

Browse files
committed
Add refactoring notes during planned usage
1 parent 95cbd82 commit 0746739

5 files changed

Lines changed: 119 additions & 33 deletions

File tree

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
using System.Threading.Tasks;
2+
using AnalyzerTesting.CSharp.Extensions;
3+
using DarkPatterns.Refactoring.Verifiers;
4+
using Microsoft.VisualStudio.TestTools.UnitTesting;
5+
6+
namespace DarkPatterns.Refactoring;
7+
8+
[TestClass]
9+
public class MustRefactorAnalyzerShould
10+
{
11+
public TestContext TestContext { get; set; }
12+
13+
[TestMethod]
14+
public async Task Produce_no_diagnostics_by_default()
15+
{
16+
await TestBuilder.TestCSharp<MustRefactorAnalyzer>()
17+
.AddSources(@"")
18+
.RunAsync(TestContext.CancellationToken);
19+
}
20+
21+
[TestMethod]
22+
public async Task Warn_for_classes_being_refactored()
23+
{
24+
await TestBuilder.TestCSharp<MustRefactorAnalyzer>()
25+
.AddSources("""
26+
[PlannedRefactor("1", "Rewrite internals to use XYZ instead")]
27+
class {|#0:OutdatedClass|}
28+
{
29+
}
30+
""")
31+
.AddExpectedDiagnostics(MustRefactorAnalyzer.Rule.AsResult().WithLocation(0).WithArguments("OutdatedClass", "1", "Rewrite internals to use XYZ instead"))
32+
.RunAsync(TestContext.CancellationToken);
33+
}
34+
35+
[TestMethod]
36+
public async Task Produce_no_diagnostic_on_class_when_planned_for_future()
37+
{
38+
await TestBuilder.TestCSharp<MustRefactorAnalyzer>()
39+
.AddSources("""
40+
[PlannedRefactor("1", "Replace with something else")]
41+
class {|#0:OutdatedClass|}
42+
{
43+
}
44+
""")
45+
.AddRefactorManifest("""
46+
1 - Remove OutdatedClass in favor of something else
47+
""")
48+
.RunAsync(TestContext.CancellationToken);
49+
}
50+
51+
[TestMethod]
52+
public async Task Warn_for_methods_being_refactored()
53+
{
54+
await TestBuilder.TestCSharp<MustRefactorAnalyzer>()
55+
.AddSources("""
56+
class OutdatedClass
57+
{
58+
[PlannedRefactor("1", "Rewrite using common framework")]
59+
public void {|#0:MethodToRemove|}()
60+
{
61+
}
62+
}
63+
""")
64+
.AddExpectedDiagnostics(MustRefactorAnalyzer.Rule.AsResult().WithLocation(0).WithArguments("MethodToRemove", "1", "Rewrite using common framework"))
65+
.RunAsync(TestContext.CancellationToken);
66+
}
67+
68+
[TestMethod]
69+
public async Task Produce_no_diagnostic_on_method_when_planned_for_future()
70+
{
71+
await TestBuilder.TestCSharp<MustRefactorAnalyzer>()
72+
.AddSources("""
73+
class OutdatedClass
74+
{
75+
[PlannedRefactor("1", "Remove this method")]
76+
public void {|#0:MethodToRemove|}()
77+
{
78+
}
79+
}
80+
""")
81+
.AddRefactorManifest("""
82+
1 - Remove OutdatedClass in favor of something else
83+
""")
84+
.RunAsync(TestContext.CancellationToken);
85+
}
86+
}

Analyzers/Analyzers/MustRefactorAnalyzer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ private static void AnalyzeSymbol(SymbolAnalysisContext context, RefactoringMani
5151
if (manifest.PlannedIssues.Contains(plannedRemoval.TicketNumber))
5252
return;
5353

54-
var diagnostic = Diagnostic.Create(Rule, context.Symbol.Locations[0], context.Symbol.Name, plannedRemoval.TicketNumber);
54+
var diagnostic = Diagnostic.Create(Rule, context.Symbol.Locations[0], context.Symbol.Name, plannedRemoval.TicketNumber, plannedRemoval.RefactorInstructions);
5555
context.ReportDiagnostic(diagnostic);
5656
}
5757
}

Analyzers/Analyzers/Resources.Designer.cs

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Analyzers/Analyzers/Resources.resx

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<root>
3-
<!--
4-
Microsoft ResX Schema
5-
3+
<!--
4+
Microsoft ResX Schema
5+
66
Version 2.0
7-
8-
The primary goals of this format is to allow a simple XML format
9-
that is mostly human readable. The generation and parsing of the
10-
various data types are done through the TypeConverter classes
7+
8+
The primary goals of this format is to allow a simple XML format
9+
that is mostly human readable. The generation and parsing of the
10+
various data types are done through the TypeConverter classes
1111
associated with the data types.
12-
12+
1313
Example:
14-
14+
1515
... ado.net/XML headers & schema ...
1616
<resheader name="resmimetype">text/microsoft-resx</resheader>
1717
<resheader name="version">2.0</resheader>
@@ -26,36 +26,36 @@
2626
<value>[base64 mime encoded string representing a byte array form of the .NET Framework object]</value>
2727
<comment>This is a comment</comment>
2828
</data>
29-
30-
There are any number of "resheader" rows that contain simple
29+
30+
There are any number of "resheader" rows that contain simple
3131
name/value pairs.
32-
33-
Each data row contains a name, and value. The row also contains a
34-
type or mimetype. Type corresponds to a .NET class that support
35-
text/value conversion through the TypeConverter architecture.
36-
Classes that don't support this are serialized and stored with the
32+
33+
Each data row contains a name, and value. The row also contains a
34+
type or mimetype. Type corresponds to a .NET class that support
35+
text/value conversion through the TypeConverter architecture.
36+
Classes that don't support this are serialized and stored with the
3737
mimetype set.
38-
39-
The mimetype is used for serialized objects, and tells the
40-
ResXResourceReader how to depersist the object. This is currently not
38+
39+
The mimetype is used for serialized objects, and tells the
40+
ResXResourceReader how to depersist the object. This is currently not
4141
extensible. For a given mimetype the value must be set accordingly:
42-
43-
Note - application/x-microsoft.net.object.binary.base64 is the format
44-
that the ResXResourceWriter will generate, however the reader can
42+
43+
Note - application/x-microsoft.net.object.binary.base64 is the format
44+
that the ResXResourceWriter will generate, however the reader can
4545
read any of the formats listed below.
46-
46+
4747
mimetype: application/x-microsoft.net.object.binary.base64
48-
value : The object must be serialized with
48+
value : The object must be serialized with
4949
: System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
5050
: and then encoded with base64 encoding.
51-
51+
5252
mimetype: application/x-microsoft.net.object.soap.base64
53-
value : The object must be serialized with
53+
value : The object must be serialized with
5454
: System.Runtime.Serialization.Formatters.Soap.SoapFormatter
5555
: and then encoded with base64 encoding.
5656
5757
mimetype: application/x-microsoft.net.object.bytearray.base64
58-
value : The object must be serialized into a byte array
58+
value : The object must be serialized into a byte array
5959
: using a System.ComponentModel.TypeConverter
6060
: and then encoded with base64 encoding.
6161
-->
@@ -122,7 +122,7 @@
122122
<comment>An optional longer localizable description of the diagnostic.</comment>
123123
</data>
124124
<data name="MustRefactorMessageFormat" xml:space="preserve">
125-
<value>Symbol '{0}' is marked for refactoring and '{1}' is not marked as planned</value>
125+
<value>Symbol '{0}' is marked for refactoring during '{1}'; notes: {2}</value>
126126
<comment>The format-able message the diagnostic displays.</comment>
127127
</data>
128128
<data name="MustRefactorTitle" xml:space="preserve">

examples/RefactoringExample/OutdatedClassExample.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
namespace DarkPatterns.Refactoring;
22

3-
[PlannedRemoval("1", "This class is outdated; use NewerClass instead")]
3+
[PlannedRemoval("1", "Use NewerClass instead")]
44
public class OutdatedClass
55
{
66

0 commit comments

Comments
 (0)