-
Notifications
You must be signed in to change notification settings - Fork 245
Added new rule for newline at end of file #61
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 |
---|---|---|
@@ -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)); | ||
|
||
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.
Sorry, something went wrong.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
Sorry, something went wrong. |
||
} | ||
|
||
return token.WithLeadingTrivia(newTrivia.AddNewLine()); | ||
} | ||
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
Sorry, something went wrong. |
||
} | ||
} |
This comment was marked as spam.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
This comment was marked as spam.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
This comment was marked as spam.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
This comment was marked as spam.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
This comment was marked as spam.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
This comment was marked as spam.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
This comment was marked as spam.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
This comment was marked as spam.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.