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

Commit 3c16dd4

Browse files
committed
Merge pull request #43 from jaredpar/bug-fixes
Bug fixes
2 parents 2d4a7e2 + 0486516 commit 3c16dd4

14 files changed

+571
-158
lines changed

src/CodeFormatter/Program.cs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ private static int Main(string[] args)
3737
var filenames = new List<string>();
3838
var disableCopyright = false;
3939
var comparer = StringComparer.OrdinalIgnoreCase;
40+
var preprocessorConfigurations = new List<string[]>();
4041

4142
for (int i = 1; i < args.Length; i++)
4243
{
@@ -54,6 +55,12 @@ private static int Main(string[] args)
5455
{
5556
disableCopyright = true;
5657
}
58+
else if (arg.StartsWith("/c:", StringComparison.OrdinalIgnoreCase))
59+
{
60+
var all = arg.Substring(3);
61+
var configs = all.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
62+
preprocessorConfigurations.Add(configs);
63+
}
5764
else
5865
{
5966
ruleTypes.Add(arg);
@@ -65,15 +72,17 @@ private static int Main(string[] args)
6572

6673
Console.CancelKeyPress += delegate { cts.Cancel(); };
6774

68-
RunAsync(projectOrSolutionPath, ruleTypes, filenames, disableCopyright, ct).Wait(ct);
75+
RunAsync(projectOrSolutionPath, ruleTypes, filenames, disableCopyright, ImmutableArray.CreateRange(preprocessorConfigurations), ct).Wait(ct);
6976
Console.WriteLine("Completed formatting.");
7077
return 0;
7178
}
7279

73-
private static async Task RunAsync(string projectOrSolutionPath, IEnumerable<string> ruleTypes, IEnumerable<string> filenames, bool disableCopright, CancellationToken cancellationToken)
80+
private static async Task RunAsync(string projectOrSolutionPath, IEnumerable<string> ruleTypes, IEnumerable<string> filenames, bool disableCopright, ImmutableArray<string[]> preprocessorConfigurations, CancellationToken cancellationToken)
7481
{
7582
var workspace = MSBuildWorkspace.Create();
7683
var engine = FormattingEngine.Create(ruleTypes, filenames);
84+
engine.PreprocessorConfigurations = preprocessorConfigurations;
85+
7786
if (disableCopright)
7887
{
7988
engine.CopyrightHeader = ImmutableArray<string>.Empty;

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

Lines changed: 79 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ class S
122122
}
123123

124124
[Fact]
125-
public void TestNoNewLineBeforeEndBrace04()
125+
public void TestEmptyBraceWhitespaceAfterOpen()
126126
{
127127
var text = @"
128128
class S
@@ -137,6 +137,48 @@ class S
137137
Verify(text, expected);
138138
}
139139

140+
[Fact]
141+
public void TestEmptyBraceNoWhitespaceAfterOpen()
142+
{
143+
var text = @"
144+
class S
145+
{
146+
147+
148+
}";
149+
var expected = @"
150+
class S
151+
{
152+
}";
153+
Verify(text, expected);
154+
}
155+
156+
[Fact]
157+
public void TestBraceSingleMethodCall()
158+
{
159+
var text = @"
160+
class S
161+
{
162+
void M()
163+
{
164+
165+
166+
G();
167+
168+
169+
}
170+
}";
171+
var expected = @"
172+
class S
173+
{
174+
void M()
175+
{
176+
G();
177+
}
178+
}";
179+
Verify(text, expected);
180+
}
181+
140182
[Fact]
141183
public void TestRemoveNewLinesBetweenPragmaAndCloseBrace()
142184
{
@@ -272,5 +314,41 @@ class L
272314

273315
Verify(text, expected);
274316
}
317+
318+
/// <summary>
319+
/// This is a regression test for issue #36
320+
/// </summary>
321+
[Fact]
322+
public void CommentsBeforeCloseBraces()
323+
{
324+
var text = @"
325+
class C
326+
{
327+
void M()
328+
{
329+
if (b) {
330+
G();
331+
332+
// A comment
333+
}
334+
}
335+
}";
336+
337+
var expected = @"
338+
class C
339+
{
340+
void M()
341+
{
342+
if (b)
343+
{
344+
G();
345+
346+
// A comment
347+
}
348+
}
349+
}";
350+
351+
Verify(text, expected, runFormatter: true);
352+
}
275353
}
276354
}

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

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ public CombinationTest()
2626
{
2727
s_formattingEngine.CopyrightHeader = ImmutableArray.Create("", "// header");
2828
s_formattingEngine.FormatLogger = new EmptyFormatLogger();
29+
s_formattingEngine.PreprocessorConfigurations = ImmutableArray<string[]>.Empty;
2930
}
3031

3132
protected override async Task<Document> RewriteDocumentAsync(Document document)
@@ -93,5 +94,133 @@ private void M()
9394

9495
Verify(text, expected, runFormatter: false);
9596
}
97+
98+
[Fact]
99+
public void PreprocessorSymbolNotDefined()
100+
{
101+
var text = @"
102+
class C
103+
{
104+
#if DOG
105+
void M() { }
106+
#endif
107+
}";
108+
109+
var expected = @"
110+
// header
111+
112+
internal class C
113+
{
114+
#if DOG
115+
void M() { }
116+
#endif
117+
}";
118+
119+
Verify(text, expected, runFormatter: false);
120+
}
121+
122+
[Fact]
123+
public void PreprocessorSymbolDefined()
124+
{
125+
var text = @"
126+
internal class C
127+
{
128+
#if DOG
129+
internal void M() {
130+
}
131+
#endif
132+
}";
133+
134+
var expected = @"
135+
// header
136+
137+
internal class C
138+
{
139+
#if DOG
140+
internal void M()
141+
{
142+
}
143+
#endif
144+
}";
145+
146+
s_formattingEngine.PreprocessorConfigurations = ImmutableArray.CreateRange(new[] { new[] { "DOG" } });
147+
Verify(text, expected, runFormatter: false);
148+
}
149+
150+
[Fact]
151+
public void TableCode()
152+
{
153+
var text = @"
154+
class C
155+
{
156+
void G() {
157+
158+
}
159+
160+
#if !DOTNET_FORMATTER
161+
void M() {
162+
}
163+
#endif
164+
}";
165+
166+
var expected = @"
167+
// header
168+
169+
internal class C
170+
{
171+
private void G()
172+
{
173+
}
174+
175+
#if !DOTNET_FORMATTER
176+
void M() {
177+
}
178+
#endif
179+
}";
180+
181+
Verify(text, expected, runFormatter: false);
182+
}
183+
184+
/// <summary>
185+
/// Make sure the code which deals with additional configurations respects the
186+
/// table exception.
187+
/// </summary>
188+
[Fact]
189+
public void TableCodeAndAdditionalConfiguration()
190+
{
191+
var text = @"
192+
class C
193+
{
194+
#if TEST
195+
void G(){
196+
}
197+
#endif
198+
199+
#if !DOTNET_FORMATTER
200+
void M() {
201+
}
202+
#endif
203+
}";
204+
205+
var expected = @"
206+
// header
207+
208+
internal class C
209+
{
210+
#if TEST
211+
void G()
212+
{
213+
}
214+
#endif
215+
216+
#if !DOTNET_FORMATTER
217+
void M() {
218+
}
219+
#endif
220+
}";
221+
222+
s_formattingEngine.PreprocessorConfigurations = ImmutableArray.CreateRange(new[] { new[] { "TEST" } });
223+
Verify(text, expected, runFormatter: false);
224+
}
96225
}
97226
}

src/Microsoft.DotNet.CodeFormatting/Filters/FilenameFilter.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@ public FilenameFilter(IEnumerable<string> filenames)
2121
_filenames = filenames;
2222
}
2323

24-
public Task<bool> ShouldBeProcessedAsync(Document document)
24+
public bool ShouldBeProcessed(Document document)
2525
{
2626
if (!_filenames.Any())
2727
{
28-
return Task.FromResult(true);
28+
return true;
2929
}
3030

3131
string docFilename = Path.GetFileName(document.FilePath);
@@ -34,11 +34,11 @@ public Task<bool> ShouldBeProcessedAsync(Document document)
3434
{
3535
if (filename.Equals(docFilename, StringComparison.InvariantCultureIgnoreCase))
3636
{
37-
return Task.FromResult(true);
37+
return true;
3838
}
3939
}
4040

41-
return Task.FromResult(false);
41+
return false;
4242
}
4343
}
4444
}

src/Microsoft.DotNet.CodeFormatting/Filters/IgnoreDesignerGeneratedCodeFilter.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,15 @@ namespace Microsoft.DotNet.CodeFormatting.Filters
1212
[Export(typeof(IFormattingFilter))]
1313
internal sealed class IgnoreDesignerGeneratedCodeFilter : IFormattingFilter
1414
{
15-
public Task<bool> ShouldBeProcessedAsync(Document document)
15+
public bool ShouldBeProcessed(Document document)
1616
{
1717
if (document.FilePath == null)
1818
{
19-
return Task.FromResult(true);
19+
return true;
2020
}
2121

2222
var isDesignerGenerated = document.FilePath.EndsWith(".Designer.cs", StringComparison.OrdinalIgnoreCase);
23-
return Task.FromResult(!isDesignerGenerated);
23+
return !isDesignerGenerated;
2424
}
2525
}
2626
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
using Microsoft.CodeAnalysis;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.ComponentModel.Composition;
5+
using System.IO;
6+
using System.Linq;
7+
using System.Text;
8+
using System.Threading.Tasks;
9+
10+
namespace Microsoft.DotNet.CodeFormatting.Filters
11+
{
12+
[Export(typeof(IFormattingFilter))]
13+
internal sealed class UsableFileFilter : IFormattingFilter
14+
{
15+
private readonly Options _options;
16+
17+
[ImportingConstructor]
18+
internal UsableFileFilter(Options options)
19+
{
20+
_options = options;
21+
}
22+
23+
public bool ShouldBeProcessed(Document document)
24+
{
25+
if (document.FilePath == null)
26+
{
27+
return true;
28+
}
29+
30+
var fileInfo = new FileInfo(document.FilePath);
31+
if (!fileInfo.Exists || fileInfo.IsReadOnly)
32+
{
33+
_options.FormatLogger.WriteLine("warning: skipping document '{0}' because it {1}.",
34+
document.FilePath,
35+
fileInfo.IsReadOnly ? "is read-only" : "does not exist");
36+
return false;
37+
}
38+
39+
return true;
40+
}
41+
}
42+
}

0 commit comments

Comments
 (0)