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

Added new rule for newline at end of file #61

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@
<Compile Include="Rules\CombinationTest.cs" />
<Compile Include="Rules\ExplicitThisRuleTests.cs" />
<Compile Include="Rules\ExplicitVisibilityRuleTests.cs" />
<Compile Include="Rules\HasNewLineAtEndOfFileFormattingRuleTests.cs" />
<Compile Include="Rules\HasNewLineBeforeFirstNamespaceFormattingRuleTests.cs" />
<Compile Include="Rules\HasNoIllegalHeadersFormattingRuleTests.cs" />
<Compile Include="Rules\HasNewLineBeforeFirstUsingFormattingRuleTests.cs" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using Xunit;

namespace Microsoft.DotNet.CodeFormatting.Tests
{
public class HasNewLineAtEndOfFileFormattingRuleTests : SyntaxRuleTestBase
{
internal override ISyntaxFormattingRule Rule
{
get { return new Rules.HasNewLineAtEndOfFileFormattingRule(); }
}

[Fact]
public void ShouldAddNewLine()
{
var source = "public class C { }";

var expected = "public class C { }\r\n";

Verify(source, expected);
}

[Fact]
public void ShouldRemoveExtraNewLines()
{
var source = "public class C { }\r\n\r\n\n\r\n\n\r";

var expected = "public class C { }\r\n";

Verify(source, expected);
}

[Fact]
public void ShouldNotCareAboutExistingCarriageReturn()
{
var source = "public class C { }\r";

var expected = "public class C { }\r\n";

Verify(source, expected);
}

[Fact]
public void ShouldNotCareAboutExistingLineFeed()
{
var source = "public class C { }\n";

var expected = "public class C { }\r\n";

Verify(source, expected);
}

[Fact]
public void ShouldHandleEmptyDocument()
{
var source = string.Empty;

var expected = "\r\n";

Verify(source, expected);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@
<Compile Include="Rules\BraceNewLineRule.cs" />
<Compile Include="Rules\ExplicitVisibilityRule.cs" />
<Compile Include="Rules\HasCopyrightHeaderFormattingRule.cs" />
<Compile Include="Rules\HasNewLineAtEndOfFileFormattingRule.cs" />
<Compile Include="Rules\HasNewLineBeforeFirstNamespaceFormattingRule.cs" />
<Compile Include="Rules\HasNewLineBeforeFirstUsingFormattingRule.cs" />
<Compile Include="Rules\HasNoIllegalHeadersFormattingRule.cs" />
Expand All @@ -112,4 +113,4 @@
</Target>
<Target Name="AfterBuild">
</Target>-->
</Project>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System.Linq;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;

namespace Microsoft.DotNet.CodeFormatting.Rules
{
[SyntaxRuleOrder(SyntaxRuleOrder.HasNewLineAtEndOfFile)]
internal sealed class HasNewLineAtEndOfFileFormattingRule : ISyntaxFormattingRule
{
public SyntaxNode Process(SyntaxNode syntaxRoot)
{
var node = syntaxRoot.DescendantNodes().LastOrDefault();

if (node != null)
{
syntaxRoot = syntaxRoot.ReplaceNode(node, RemoveNewLines(node));
}

var token = syntaxRoot.DescendantTokens().Single(x => x.IsKind(SyntaxKind.EndOfFileToken));

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.


return syntaxRoot.ReplaceToken(token, AdjustNewLines(token));
}

private static SyntaxNode RemoveNewLines(SyntaxNode node)
{
var newTrivia = Enumerable.Empty<SyntaxTrivia>();

if (node.HasTrailingTrivia)
{
newTrivia = node.GetTrailingTrivia().Where(x => !x.IsKind(SyntaxKind.EndOfLineTrivia));
}

return node.WithTrailingTrivia(newTrivia);
}

private static SyntaxToken AdjustNewLines(SyntaxToken token)
{
var newTrivia = Enumerable.Empty<SyntaxTrivia>();

if (token.HasLeadingTrivia)
{
newTrivia = token.LeadingTrivia.Where(x => !x.IsKind(SyntaxKind.EndOfLineTrivia));

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

}

return token.WithLeadingTrivia(newTrivia.AddNewLine());
}

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

}
}
1 change: 1 addition & 0 deletions src/Microsoft.DotNet.CodeFormatting/Rules/RuleOrder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ internal static class SyntaxRuleOrder
public const int HasNewLineBeforeFirstNamespaceFormattingRule = 5;
public const int BraceNewLineRule = 6;
public const int NonAsciiChractersAreEscapedInLiterals = 7;
public const int HasNewLineAtEndOfFile = 8;
}

// Please keep these values sorted by number, not rule name.
Expand Down