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

Commit b0ab437

Browse files
committed
Merge pull request #31 from dotnet/fixes-perf
Fixes perf issue
2 parents cf4b485 + b78f11d commit b0ab437

9 files changed

+635
-199
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,13 +105,14 @@
105105
</ItemGroup>
106106
<ItemGroup>
107107
<Compile Include="CodeFormattingTestBase.cs" />
108+
<Compile Include="Rules\ExplicitThisRuleTests.cs" />
108109
<Compile Include="Rules\ExplicitVisibilityRuleTests.cs" />
109110
<Compile Include="Rules\HasNewLineBeforeFirstNamespaceFormattingRuleTests.cs" />
110111
<Compile Include="Rules\HasNoIllegalHeadersFormattingRuleTests.cs" />
111112
<Compile Include="Rules\HasNewLineBeforeFirstUsingFormattingRuleTests.cs" />
112113
<Compile Include="Rules\HasNoNewLineAfterOpenBraceFormattingRuleTests.cs" />
113114
<Compile Include="Rules\HasNoNewLineBeforeEndBraceFormattingRuleTests.cs" />
114-
<Compile Include="Rules\HasUnderScoreInPrivateFieldNamesFormattingRuleTests.cs" />
115+
<Compile Include="Rules\PrivateFieldNamingRuleTests.cs" />
115116
<Compile Include="Rules\NonAsciiCharactersAreEscapedInLiteralsRuleTests.cs" />
116117
<Compile Include="Rules\UsesXunitForTestsFormattingRuleTests.cs" />
117118
</ItemGroup>
Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
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 ExplicitThisRuleTests : CodeFormattingTestBase
11+
{
12+
internal override IFormattingRule GetFormattingRule()
13+
{
14+
return new Rules.ExplicitThisRule();
15+
}
16+
17+
[Fact]
18+
public void TestFieldUse()
19+
{
20+
var text = @"
21+
class C1
22+
{
23+
int _field1;
24+
string _field2;
25+
string field3;
26+
27+
void Use(int i) { }
28+
29+
void M()
30+
{
31+
Use(_field1);
32+
Use(_field2);
33+
Use(field3);
34+
Use(this._field1);
35+
Use(this._field2);
36+
Use(this.field3);
37+
}
38+
}
39+
";
40+
41+
var expected = @"
42+
class C1
43+
{
44+
int _field1;
45+
string _field2;
46+
string field3;
47+
48+
void Use(int i) { }
49+
50+
void M()
51+
{
52+
Use(_field1);
53+
Use(_field2);
54+
Use(field3);
55+
Use(_field1);
56+
Use(_field2);
57+
Use(this.field3);
58+
}
59+
}
60+
";
61+
Verify(text, expected, runFormatter: false);
62+
}
63+
64+
[Fact]
65+
public void TestFieldAssignment()
66+
{
67+
var text = @"
68+
class C1
69+
{
70+
int _field1;
71+
string _field2;
72+
string field3;
73+
74+
void M()
75+
{
76+
this._field1 = 0;
77+
this._field2 = null;
78+
this.field3 = null;
79+
}
80+
}
81+
";
82+
83+
var expected = @"
84+
class C1
85+
{
86+
int _field1;
87+
string _field2;
88+
string field3;
89+
90+
void M()
91+
{
92+
_field1 = 0;
93+
_field2 = null;
94+
this.field3 = null;
95+
}
96+
}
97+
";
98+
Verify(text, expected, runFormatter: false);
99+
}
100+
101+
[Fact]
102+
public void TestFieldAssignmentWithTrivia()
103+
{
104+
var text = @"
105+
class C1
106+
{
107+
int _field;
108+
109+
void M()
110+
{
111+
this. /* comment1 */ _field /* comment 2 */ = 0;
112+
}
113+
}
114+
";
115+
116+
var expected = @"
117+
class C1
118+
{
119+
int _field;
120+
121+
void M()
122+
{
123+
/* comment1 */ _field /* comment 2 */ = 0;
124+
}
125+
}
126+
";
127+
Verify(text, expected, runFormatter: false);
128+
}
129+
130+
[Fact]
131+
public void TestFieldBadName()
132+
{
133+
var text = @"
134+
class C1
135+
{
136+
int _field;
137+
138+
void M()
139+
{
140+
// Not a valid field access, can't reliably remove this.
141+
this.field1 = 0;
142+
}
143+
}
144+
";
145+
146+
var expected = @"
147+
class C1
148+
{
149+
int _field;
150+
151+
void M()
152+
{
153+
// Not a valid field access, can't reliably remove this.
154+
this.field1 = 0;
155+
}
156+
}
157+
";
158+
Verify(text, expected, runFormatter: false);
159+
}
160+
}
161+
}

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

Lines changed: 0 additions & 51 deletions
This file was deleted.
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
// Copyright (c) Microsoft. All rights reserved.
2+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
3+
4+
using Xunit;
5+
6+
namespace Microsoft.DotNet.CodeFormatting.Tests
7+
{
8+
public class PrivateFieldNamingRuleTests : CodeFormattingTestBase
9+
{
10+
internal override IFormattingRule GetFormattingRule()
11+
{
12+
return new Rules.PrivateFieldNamingRule();
13+
}
14+
15+
[Fact]
16+
public void TestUnderScoreInPrivateFields()
17+
{
18+
var text = @"
19+
using System;
20+
class T
21+
{
22+
private static int x;
23+
private static int s_y;
24+
// some trivia
25+
private static int m_z;
26+
// some trivia
27+
private int k = 1, m_s = 2, rsk_yz = 3, x_y_z;
28+
// some trivia
29+
[ThreadStatic] static int r;
30+
[ThreadStaticAttribute] static int b_r;
31+
}";
32+
var expected = @"
33+
using System;
34+
class T
35+
{
36+
private static int s_x;
37+
private static int s_y;
38+
// some trivia
39+
private static int s_z;
40+
// some trivia
41+
private int _k = 1, _s = 2, _rsk_yz = 3, _y_z;
42+
// some trivia
43+
[ThreadStatic]
44+
static int t_r;
45+
[ThreadStaticAttribute]
46+
static int t_r;
47+
}";
48+
Verify(text, expected);
49+
}
50+
51+
[Fact]
52+
public void CornerCaseNames()
53+
{
54+
var text = @"
55+
class C
56+
{
57+
private int x_;
58+
private int _;
59+
private int __;
60+
private int m_field1;
61+
private int field2_;
62+
";
63+
64+
var expected = @"
65+
class C
66+
{
67+
private int _x;
68+
private int _;
69+
private int __;
70+
private int _field1;
71+
private int _field2;
72+
";
73+
74+
Verify(text, expected, runFormatter: false);
75+
}
76+
77+
[Fact]
78+
public void MultipleDeclarators()
79+
{
80+
var text = @"
81+
class C1
82+
{
83+
private int field1, field2, field3;
84+
}
85+
86+
class C2
87+
{
88+
private static int field1, field2, field3;
89+
}
90+
91+
class C3
92+
{
93+
internal int field1, field2, field3;
94+
}
95+
";
96+
97+
var expected = @"
98+
class C1
99+
{
100+
private int _field1, _field2, _field3;
101+
}
102+
103+
class C2
104+
{
105+
private static int s_field1, s_field2, s_field3;
106+
}
107+
108+
class C3
109+
{
110+
internal int field1, field2, field3;
111+
}
112+
";
113+
114+
Verify(text, expected, runFormatter: true);
115+
}
116+
}
117+
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,11 +89,12 @@
8989
<Compile Include="Rules\HasNoNewLineAfterOpenBraceFormattingRule.cs" />
9090
<Compile Include="Rules\HasNoNewLineBeforeEndBraceFormattingRule.cs" />
9191
<Compile Include="Rules\HasNoCustomCopyrightHeaderFormattingRule.cs" />
92-
<Compile Include="Rules\HasUnderScoreInPrivateFieldNamesFormattingRule.cs" />
92+
<Compile Include="Rules\PrivateFieldNamingRule.cs" />
9393
<Compile Include="Rules\HasUsingsOutsideOfNamespaceFormattingRule.cs" />
9494
<Compile Include="Rules\IsFormattedFormattingRule.cs" />
9595
<Compile Include="Rules\IsSimplifiedFormattingRule.cs" />
9696
<Compile Include="Rules\NonAsciiCharactersAreEscapedInLiteralsRule.cs" />
97+
<Compile Include="Rules\ExplicitThisRule.cs" />
9798
<Compile Include="Rules\RuleExtensions.cs" />
9899
<Compile Include="Rules\RuleOrder.cs" />
99100
<Compile Include="Rules\UsesXunitForTestsFormattingRule.cs" />

0 commit comments

Comments
 (0)