Skip to content
This repository was archived by the owner on Jul 12, 2022. It is now read-only.

Commit 3fd4df0

Browse files
committed
Merge pull request #72 from jaredpar/bug-fixes
Bug fixes
2 parents e34e67a + 0721187 commit 3fd4df0

8 files changed

+349
-73
lines changed

src/Microsoft.DotNet.CodeFormatting.Tests/Microsoft.DotNet.CodeFormatting.Tests.csproj

+1
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@
128128
<Compile Include="Rules\HasNewLineBeforeFirstUsingFormattingRuleTests.cs" />
129129
<Compile Include="Rules\PrivateFieldNamingRuleTests.cs" />
130130
<Compile Include="Rules\NonAsciiCharactersAreEscapedInLiteralsRuleTests.cs" />
131+
<Compile Include="Rules\UsingLocationRuleTests.cs" />
131132
</ItemGroup>
132133
<ItemGroup>
133134
<Folder Include="Properties\" />

src/Microsoft.DotNet.CodeFormatting.Tests/Rules/ExplicitVisibilityRuleTests.cs

+26
Original file line numberDiff line numberDiff line change
@@ -390,6 +390,32 @@ internal class C
390390

391391
Verify(text, expected, runFormatter: false);
392392
}
393+
394+
[Fact]
395+
public void Issue70()
396+
{
397+
var source = @"
398+
public class MyClass
399+
{
400+
enum MyEnum { }
401+
struct MyStruct
402+
{
403+
public MyStruct(MyEnum e) { }
404+
}
405+
}";
406+
407+
var expected = @"
408+
public class MyClass
409+
{
410+
private enum MyEnum { }
411+
private struct MyStruct
412+
{
413+
public MyStruct(MyEnum e) { }
414+
}
415+
}";
416+
417+
Verify(source, expected);
418+
}
393419
}
394420

395421
public sealed class VisualBasicTests : ExplicitVisibilityRuleTests

src/Microsoft.DotNet.CodeFormatting.Tests/Rules/PrivateFieldNamingRuleTests.cs

+92
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,72 @@ class C
146146

147147
Verify(text, expected, runFormatter: false);
148148
}
149+
150+
[Fact]
151+
public void Issue68()
152+
{
153+
var text = @"
154+
delegate void Action();
155+
class C
156+
{
157+
Action someAction;
158+
void M(C p)
159+
{
160+
someAction();
161+
this.someAction();
162+
p.someAction();
163+
}
164+
}";
165+
166+
var expected = @"
167+
delegate void Action();
168+
class C
169+
{
170+
Action _someAction;
171+
void M(C p)
172+
{
173+
_someAction();
174+
this._someAction();
175+
p._someAction();
176+
}
177+
}";
178+
179+
Verify(text, expected);
180+
}
181+
182+
/// <summary>
183+
/// Ensure that Roslyn properly renames private fields when accessed through a non-this
184+
/// instance within the same type.
185+
/// </summary>
186+
[Fact]
187+
public void Issue69()
188+
{
189+
var text = @"
190+
class C
191+
{
192+
int field;
193+
194+
int M(C p)
195+
{
196+
int x = p.field;
197+
return x;
198+
}
199+
}";
200+
201+
var expected = @"
202+
class C
203+
{
204+
int _field;
205+
206+
int M(C p)
207+
{
208+
int x = p._field;
209+
return x;
210+
}
211+
}";
212+
213+
Verify(text, expected);
214+
}
149215
}
150216

151217
private sealed class VisualBasicFields : PrivateFieldNamingRuleTests
@@ -221,6 +287,32 @@ End Sub
221287

222288
Verify(text, expected, runFormatter: false, languageName: LanguageNames.VisualBasic);
223289
}
290+
291+
[Fact]
292+
public void Issue69()
293+
{
294+
var text = @"
295+
Class C1
296+
Private Field As Integer
297+
298+
Function M(p As C1) As Integer
299+
Dim x = p.Field
300+
Return x
301+
End Function
302+
End Class";
303+
304+
var expected = @"
305+
Class C1
306+
Private _field As Integer
307+
308+
Function M(p As C1) As Integer
309+
Dim x = p._field
310+
Return x
311+
End Function
312+
End Class";
313+
314+
Verify(text, expected, languageName: LanguageNames.VisualBasic);
315+
}
224316
}
225317
}
226318
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
using Xunit;
7+
8+
namespace Microsoft.DotNet.CodeFormatting.Tests
9+
{
10+
public sealed class UsingLocationRuleTests : SyntaxRuleTestBase
11+
{
12+
internal override ISyntaxFormattingRule Rule
13+
{
14+
get { return new Rules.UsingLocationRule(); }
15+
}
16+
17+
[Fact]
18+
public void SimpleMove()
19+
{
20+
var source = @"
21+
using NS1;
22+
namespace NS2
23+
{
24+
using NS3;
25+
class C1 { }
26+
}";
27+
28+
var expected = @"
29+
using NS1;
30+
using NS3;
31+
namespace NS2
32+
{
33+
class C1 { }
34+
}";
35+
36+
Verify(source, expected);
37+
}
38+
39+
/// <summary>
40+
/// There is no safe way to move a using outside a namespace when there are
41+
/// multiple namespaces. The rule should punt on this scenario.
42+
/// </summary>
43+
[Fact]
44+
public void SimpleMoveMultipleNamespaces()
45+
{
46+
var source = @"
47+
using NS1;
48+
namespace NS2
49+
{
50+
using NS3;
51+
class C1 { }
52+
}
53+
54+
namespace NS3
55+
{
56+
using NS4;
57+
class C1 { }
58+
}";
59+
60+
Verify(source, source);
61+
}
62+
63+
[Fact]
64+
public void SimpleMoveWithComment()
65+
{
66+
var source = @"
67+
using NS1;
68+
namespace NS2
69+
{
70+
// test
71+
using NS3;
72+
class C1 { }
73+
}";
74+
75+
var expected = @"
76+
using NS1;
77+
// test
78+
using NS3;
79+
namespace NS2
80+
{
81+
class C1 { }
82+
}";
83+
84+
Verify(source, expected);
85+
}
86+
87+
[Fact]
88+
public void SimpleMoveWithBeforeAfterComments()
89+
{
90+
var source = @"
91+
using NS1;
92+
namespace NS2
93+
{
94+
// test1
95+
using NS3;
96+
// test2
97+
class C1 { }
98+
}";
99+
100+
var expected = @"
101+
using NS1;
102+
// test1
103+
using NS3;
104+
namespace NS2
105+
{
106+
// test2
107+
class C1 { }
108+
}";
109+
110+
Verify(source, expected);
111+
}
112+
113+
[Fact]
114+
public void MoveToEmptyList()
115+
{
116+
var source = @"namespace NS2
117+
{
118+
// test
119+
using NS3;
120+
class C1 { }
121+
}";
122+
123+
var expected = @"// test
124+
using NS3;
125+
namespace NS2
126+
{
127+
class C1 { }
128+
}";
129+
130+
Verify(source, expected);
131+
}
132+
133+
/// <summary>
134+
/// In the case a using directive is inside of a #pragma directive there is no
135+
/// way to safely move the using.
136+
/// </summary>
137+
[Fact]
138+
public void Issue71()
139+
{
140+
var source = @"
141+
namespace N
142+
{
143+
#if false
144+
using NS1;
145+
class C { }
146+
#else
147+
using NS2;
148+
using D { }
149+
#endif
150+
}";
151+
152+
Verify(source, source);
153+
}
154+
155+
}
156+
}

src/Microsoft.DotNet.CodeFormatting/Microsoft.DotNet.CodeFormatting.csproj

+1-1
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@
134134
<Compile Include="Rules\HasNoIllegalHeadersFormattingRule.cs" />
135135
<Compile Include="Rules\HasNoCustomCopyrightHeaderFormattingRule.cs" />
136136
<Compile Include="Rules\PrivateFieldNamingRule.cs" />
137-
<Compile Include="Rules\HasUsingsOutsideOfNamespaceFormattingRule.cs" />
137+
<Compile Include="Rules\UsingLocationRule.cs" />
138138
<Compile Include="Rules\FormatDocumentFormattingRule.cs" />
139139
<Compile Include="Rules\NonAsciiCharactersAreEscapedInLiteralsRule.cs" />
140140
<Compile Include="Rules\ExplicitThisRule.cs" />

src/Microsoft.DotNet.CodeFormatting/Rules/HasUsingsOutsideOfNamespaceFormattingRule.cs

-71
This file was deleted.

src/Microsoft.DotNet.CodeFormatting/Rules/RuleOrder.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ internal static class SyntaxRuleOrder
1414
{
1515
public const int HasNoCustomCopyrightHeaderFormattingRule = 1;
1616
public const int CopyrightHeaderRule = 2;
17-
public const int HasUsingsOutsideOfNamespaceFormattingRule = 3;
17+
public const int UsingLocationFormattingRule = 3;
1818
public const int HasNewLineBeforeFirstUsingFormattingRule = 4;
1919
public const int HasNewLineBeforeFirstNamespaceFormattingRule = 5;
2020
public const int BraceNewLineRule = 6;

0 commit comments

Comments
 (0)