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

Commit 290fbc3

Browse files
author
Lakshmi Priya Sekar
committed
Fix code review comments
1 parent 2cc8b26 commit 290fbc3

File tree

3 files changed

+31
-23
lines changed

3 files changed

+31
-23
lines changed

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

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,21 @@ internal sealed class HasNoCustomCopyrightHeaderFormattingRule : IFormattingRule
2121
private static string StartMarker { get; set;}
2222
private static string EndMarker { get; set; }
2323

24+
private const string FileNotFoundError = "The specified CopyrightHeader.md file was not found.";
25+
26+
private const string FileSyntaxError = "There should be exactly 3 lines in CopyrightHeader.md.";
27+
2428
public async Task<Document> ProcessAsync(Document document, CancellationToken cancellationToken)
2529
{
2630
var syntaxNode = await document.GetSyntaxRootAsync(cancellationToken) as CSharpSyntaxNode;
2731
if (syntaxNode == null)
2832
return document;
2933

30-
var triviaList = syntaxNode.GetLeadingTrivia();
31-
3234
// SetHeaders
33-
SetHeaders();
35+
if (!SetHeaders())
36+
return document;
37+
38+
var triviaList = syntaxNode.GetLeadingTrivia();
3439

3540
SyntaxTrivia start;
3641
SyntaxTrivia end;
@@ -113,25 +118,30 @@ private static bool IsEndOfXmlHeader(SyntaxTrivia trivia, out SyntaxTrivia end)
113118
.FirstOrDefault();
114119
}
115120

116-
private static void SetHeaders()
121+
private static bool SetHeaders()
117122
{
118123
var filePath = Path.Combine(
119124
Path.GetDirectoryName(Uri.UnescapeDataString(new UriBuilder(Assembly.GetExecutingAssembly().CodeBase).Path)),
120125
"CopyrightHeader.md");
121126

122127
if (!File.Exists(filePath))
123128
{
124-
RulerMarker = "";
125-
StartMarker = "";
126-
EndMarker = "";
129+
FileNotFoundError.WriteConsoleError(1, "CopyrightHeader.md");
130+
return false;
127131
}
128-
else
132+
133+
var lines = File.ReadAllLines(filePath).Where(l => !l.StartsWith("##") && !l.Equals("")).ToArray();
134+
if (lines.Count() != 3)
129135
{
130-
var lines = File.ReadAllLines(filePath).Where(l => !l.StartsWith("##") && !l.Equals("")).ToArray();
131-
RulerMarker = lines[0];
132-
StartMarker = lines[1];
133-
EndMarker = lines[2];
136+
FileSyntaxError.WriteConsoleError(1, "CopyrightHeader.md");
137+
return false;
134138
}
135-
}
139+
140+
RulerMarker = lines[0];
141+
StartMarker = lines[1];
142+
EndMarker = lines[2];
143+
144+
return true;
145+
}
136146
}
137147
}

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

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -60,18 +60,15 @@ private static SyntaxTriviaList RemoveTrivia(SyntaxTriviaList leadingTrivia, Syn
6060
{
6161
SyntaxTriviaList newTrivia = leadingTrivia;
6262
var index = leadingTrivia.IndexOf(trivia);
63-
if (leadingTrivia.ElementAt(index + 1).CSharpKind() == SyntaxKind.EndOfLineTrivia)
63+
64+
// Remove trivia
65+
newTrivia = newTrivia.RemoveAt(index);
66+
67+
if (index < newTrivia.Count && newTrivia.ElementAt(index).CSharpKind() == SyntaxKind.EndOfLineTrivia)
6468
{
65-
// Remove trivia
66-
newTrivia = newTrivia.RemoveAt(index);
6769
// Remove end of line after trivia
6870
newTrivia = newTrivia.RemoveAt(index);
6971
}
70-
else
71-
{
72-
// Remove trivia
73-
newTrivia = newTrivia.RemoveAt(index);
74-
}
7572

7673
return newTrivia;
7774
}

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,16 +74,17 @@ private static IEnumerable<SyntaxTrivia> RemoveNewLinesFromBotton(IEnumerable<Sy
7474
}
7575
}
7676

77+
var newTrivia = trivia.Take(elementsToRemoveAtEnd + 1);
78+
7779
if (addWhitespace)
7880
{
79-
var newTrivia = trivia.Take(elementsToRemoveAtEnd + 1);
8081
if (newTrivia.Last().IsDirective)
8182
return newTrivia.AddWhiteSpaceTrivia();
8283

8384
return newTrivia.AddNewLine().AddWhiteSpaceTrivia();
8485
}
8586

86-
return trivia.Take(elementsToRemoveAtEnd + 1);
87+
return newTrivia;
8788
}
8889
}
8990
}

0 commit comments

Comments
 (0)