Skip to content
This repository was archived by the owner on Apr 8, 2019. It is now read-only.

Commit 6157d51

Browse files
committed
If the unshipped API file is empty, treat it as ending with a blank line
1 parent 5e6ce6a commit 6157d51

File tree

2 files changed

+37
-13
lines changed

2 files changed

+37
-13
lines changed

PublicApiAnalyzer/PublicApiAnalyzer.CodeFixes/ApiDesign/DeclarePublicAPIFix.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,8 @@ private static SourceText RemoveSymbolNamesFromSourceText(SourceText sourceText,
8787

8888
var sortedLines = newLines.OrderBy(s => s, StringComparer.Ordinal);
8989

90-
var newSourceText = sourceText.Replace(new TextSpan(0, sourceText.Length), string.Join(Environment.NewLine, sortedLines) + GetEndOfFileText(sourceText));
90+
string newText = sortedLines.Any() ? string.Join(Environment.NewLine, sortedLines) + GetEndOfFileText(sourceText) : string.Empty;
91+
var newSourceText = sourceText.Replace(new TextSpan(0, sourceText.Length), newText);
9192
return newSourceText;
9293
}
9394

@@ -117,7 +118,8 @@ private static string GetEndOfFileText(SourceText sourceText)
117118
{
118119
if (sourceText.Length == 0)
119120
{
120-
return string.Empty;
121+
// An empty file is treated as though it ends with an empty line
122+
return Environment.NewLine;
121123
}
122124

123125
var lastLine = sourceText.Lines[sourceText.Lines.Count - 1];

PublicApiAnalyzer/PublicApiAnalyzer.Test/ApiDesign/DeclarePublicAPIAnalyzerTests.cs

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ private C() { }
4141
var expected = this.CSharpDiagnostic(DeclarePublicAPIAnalyzer.DeclareNewApiRule).WithArguments("C").WithLocation(2, 14);
4242
await this.VerifyCSharpDiagnosticAsync(source, expected, CancellationToken.None).ConfigureAwait(false);
4343

44-
string fixedApi = "C";
44+
string fixedApi = "C" + Environment.NewLine;
4545
var updatedApi = await this.GetUpdatedApiAsync(source, 0, CancellationToken.None).ConfigureAwait(false);
4646

4747
Assert.Equal(fixedApi, updatedApi.ToString());
@@ -89,27 +89,27 @@ public void Method() { }
8989

9090
await this.VerifyCSharpDiagnosticAsync(source, expected, CancellationToken.None).ConfigureAwait(false);
9191

92-
string fixedApi = "C";
92+
string fixedApi = "C" + Environment.NewLine;
9393
var updatedApi = await this.GetUpdatedApiAsync(source, 0, CancellationToken.None).ConfigureAwait(false);
9494
Assert.Equal(fixedApi, updatedApi.ToString());
9595

96-
fixedApi = "C.C() -> void";
96+
fixedApi = "C.C() -> void" + Environment.NewLine;
9797
updatedApi = await this.GetUpdatedApiAsync(source, 1, CancellationToken.None).ConfigureAwait(false);
9898
Assert.Equal(fixedApi, updatedApi.ToString());
9999

100-
fixedApi = "C.Field -> int";
100+
fixedApi = "C.Field -> int" + Environment.NewLine;
101101
updatedApi = await this.GetUpdatedApiAsync(source, 2, CancellationToken.None).ConfigureAwait(false);
102102
Assert.Equal(fixedApi, updatedApi.ToString());
103103

104-
fixedApi = "C.Property.get -> int";
104+
fixedApi = "C.Property.get -> int" + Environment.NewLine;
105105
updatedApi = await this.GetUpdatedApiAsync(source, 3, CancellationToken.None).ConfigureAwait(false);
106106
Assert.Equal(fixedApi, updatedApi.ToString());
107107

108-
fixedApi = "C.Property.set -> void";
108+
fixedApi = "C.Property.set -> void" + Environment.NewLine;
109109
updatedApi = await this.GetUpdatedApiAsync(source, 4, CancellationToken.None).ConfigureAwait(false);
110110
Assert.Equal(fixedApi, updatedApi.ToString());
111111

112-
fixedApi = "C.Method() -> void";
112+
fixedApi = "C.Method() -> void" + Environment.NewLine;
113113
updatedApi = await this.GetUpdatedApiAsync(source, 5, CancellationToken.None).ConfigureAwait(false);
114114
Assert.Equal(fixedApi, updatedApi.ToString());
115115
}
@@ -730,7 +730,8 @@ private C() { }
730730

731731
this.shippedText = string.Empty;
732732
this.unshippedText = string.Empty;
733-
var fixedUnshippedText = @"C";
733+
var fixedUnshippedText = @"C
734+
";
734735

735736
await this.VerifyCSharpUnshippedFileFixAsync(source, fixedUnshippedText, cancellationToken: CancellationToken.None).ConfigureAwait(false);
736737
}
@@ -752,7 +753,8 @@ public class C2 { }
752753
var fixedUnshippedText = @"C
753754
C.Field -> int
754755
C2
755-
C2.C2() -> void";
756+
C2.C2() -> void
757+
";
756758

757759
await this.VerifyCSharpUnshippedFileFixAsync(source, fixedUnshippedText, cancellationToken: CancellationToken.None).ConfigureAwait(false);
758760
}
@@ -848,7 +850,8 @@ public class C2 { }
848850
C.CC.Field -> int
849851
C.Field -> int
850852
C2
851-
C2.C2() -> void";
853+
C2.C2() -> void
854+
";
852855

853856
await this.VerifyCSharpUnshippedFileFixAsync(source, fixedUnshippedText, cancellationToken: CancellationToken.None).ConfigureAwait(false);
854857
}
@@ -993,7 +996,8 @@ public class C2 { }
993996
var fixedUnshippedText = @"C
994997
C.Field -> int
995998
C2
996-
C2.C2() -> void";
999+
C2.C2() -> void
1000+
";
9971001

9981002
await this.VerifyCSharpUnshippedFileFixAsync(source, fixedUnshippedText, cancellationToken: CancellationToken.None).ConfigureAwait(false);
9991003
}
@@ -1030,6 +1034,24 @@ public class C2 { }
10301034
await this.VerifyCSharpUnshippedFileFixAsync(source, fixedUnshippedText, numberOfFixAllIterations: 2, cancellationToken: CancellationToken.None).ConfigureAwait(false);
10311035
}
10321036

1037+
[Fact]
1038+
public async Task TestAddTrailingNewlineByDefaultAsync()
1039+
{
1040+
var source = @"
1041+
public class C
1042+
{
1043+
}
1044+
";
1045+
1046+
this.shippedText = string.Empty;
1047+
this.unshippedText = string.Empty;
1048+
var fixedUnshippedText = @"C
1049+
C.C() -> void
1050+
";
1051+
1052+
await this.VerifyCSharpUnshippedFileFixAsync(source, fixedUnshippedText, cancellationToken: CancellationToken.None).ConfigureAwait(false);
1053+
}
1054+
10331055
[Theory]
10341056
[InlineData("", "")]
10351057
[InlineData("\r\n", "\r\n")]

0 commit comments

Comments
 (0)