This repository has been archived by the owner on Jul 12, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 243
Add rules for formatting attributes #64
Open
terrajobst
wants to merge
1
commit into
main
Choose a base branch
from
attributes
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
src/Microsoft.DotNet.CodeFormatting.Tests/Rules/AttributeNoParenthesesRuleTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
using System; | ||
|
||
using Microsoft.DotNet.CodeFormatting.Rules; | ||
|
||
using Xunit; | ||
|
||
namespace Microsoft.DotNet.CodeFormatting.Tests | ||
{ | ||
public class AttributeNoParenthesesRuleTests : SyntaxRuleTestBase | ||
{ | ||
internal override ISyntaxFormattingRule Rule | ||
{ | ||
get { return new AttributeNoParenthesesRule(); } | ||
} | ||
|
||
[Fact] | ||
public void RemoveParenthesesFromAttributes() | ||
{ | ||
var text = @" | ||
[assembly: GlobalAtt()] | ||
[assembly: GlobalAtt(1)] | ||
|
||
namespace Namespace1 | ||
{ | ||
[Serializable(), Category(2), Rule] | ||
class Class1 | ||
{ | ||
[return: SomeAtt(] | ||
[AnotherAtt(1), YetAnotherAtt(), YetAnotherAtt] | ||
public int SomeMethod(SyntaxNode syntaxRoot) | ||
{ | ||
return 42; | ||
} | ||
} | ||
} | ||
"; | ||
var expected = @" | ||
[assembly: GlobalAtt] | ||
[assembly: GlobalAtt(1)] | ||
|
||
namespace Namespace1 | ||
{ | ||
[Serializable, Category(2), Rule] | ||
class Class1 | ||
{ | ||
[return: SomeAtt] | ||
[AnotherAtt(1), YetAnotherAtt, YetAnotherAtt] | ||
public int SomeMethod(SyntaxNode syntaxRoot) | ||
{ | ||
return 42; | ||
} | ||
} | ||
} | ||
"; | ||
Verify(text, expected); | ||
} | ||
} | ||
} |
79 changes: 79 additions & 0 deletions
79
src/Microsoft.DotNet.CodeFormatting.Tests/Rules/AttributeSeparateListsRuleTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
using System; | ||
|
||
using Microsoft.DotNet.CodeFormatting.Rules; | ||
|
||
using Xunit; | ||
|
||
namespace Microsoft.DotNet.CodeFormatting.Tests | ||
{ | ||
public class AttributeSeparateListsRuleTests : SyntaxRuleTestBase | ||
{ | ||
internal override ISyntaxFormattingRule Rule | ||
{ | ||
get { return new AttributeSeparateListsRule(); } | ||
} | ||
|
||
[Fact] | ||
public void ParameterAttributeListsAreNotSeparated() | ||
{ | ||
var text = @" | ||
namespace Namespace1 | ||
{ | ||
class Class1 | ||
{ | ||
public int SomeMethod([In, Out]SomeType someParameter) | ||
{ | ||
return 42; | ||
} | ||
} | ||
}"; | ||
Verify(text, text); | ||
} | ||
|
||
[Fact] | ||
public void AttributeListsAreSeparated() | ||
{ | ||
var text = @" | ||
[assembly: FileVersion(1, 1), AssemblyVersion(1, 1)] | ||
namespace Namespace1 | ||
{ | ||
[ | ||
Serializable, // Good, isn't? | ||
DefaultValue(1) // Is this the right value? | ||
] | ||
class Class1 | ||
{ | ||
[Serializable, DefaultValue(1)] | ||
public int SomeMethod(SomeType someParameter) | ||
{ | ||
return 42; | ||
} | ||
} | ||
}"; | ||
|
||
var expected = @" | ||
[assembly: FileVersion(1, 1)] | ||
[assembly: AssemblyVersion(1, 1)] | ||
namespace Namespace1 | ||
{ | ||
|
||
[Serializable] // Good, isn't? | ||
[DefaultValue(1)] // Is this the right value? | ||
|
||
class Class1 | ||
{ | ||
[Serializable] | ||
[DefaultValue(1)] | ||
public int SomeMethod(SomeType someParameter) | ||
{ | ||
return 42; | ||
} | ||
} | ||
}"; | ||
Verify(text, expected); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
src/Microsoft.DotNet.CodeFormatting/Rules/AttributeNoParenthesesRule.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
using System; | ||
using System.Linq; | ||
|
||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.CSharp.Syntax; | ||
|
||
namespace Microsoft.DotNet.CodeFormatting.Rules | ||
{ | ||
[SyntaxRuleOrder(SyntaxRuleOrder.AttributeNoParenthesesRule)] | ||
internal sealed class AttributeNoParenthesesRule : ISyntaxFormattingRule | ||
{ | ||
public SyntaxNode Process(SyntaxNode syntaxRoot) | ||
{ | ||
var attributes = syntaxRoot.DescendantNodes() | ||
.OfType<AttributeSyntax>() | ||
.Where(a => a.ArgumentList != null && | ||
a.ArgumentList.Arguments.Count == 0 && | ||
(!a.ArgumentList.OpenParenToken.IsMissing || !a.ArgumentList.CloseParenToken.IsMissing)); | ||
|
||
return syntaxRoot.ReplaceNodes(attributes, (a, n) => a.WithArgumentList(null)); | ||
} | ||
} | ||
} |
84 changes: 84 additions & 0 deletions
84
src/Microsoft.DotNet.CodeFormatting/Rules/AttributeSeparateListsRule.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
|
||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.CSharp; | ||
using Microsoft.CodeAnalysis.CSharp.Syntax; | ||
|
||
namespace Microsoft.DotNet.CodeFormatting.Rules | ||
{ | ||
[SyntaxRuleOrder(SyntaxRuleOrder.AttributeSeparateListsRule)] | ||
internal sealed class AttributeSeparateListsRule : ISyntaxFormattingRule | ||
This comment was marked as spam.
Sorry, something went wrong. |
||
{ | ||
public SyntaxNode Process(SyntaxNode syntaxRoot) | ||
{ | ||
var rewriter = new AttributeListRewriter(); | ||
return rewriter.Visit(syntaxRoot); | ||
} | ||
|
||
private sealed class AttributeListRewriter : CSharpSyntaxRewriter | ||
{ | ||
public override SyntaxNode VisitParameter(ParameterSyntax node) | ||
{ | ||
// We don't want to flatten the attribute lists for parameters. Those are | ||
// usually short, such as [In, Out] and collapsing them can actually | ||
// improve readability. | ||
return node; | ||
} | ||
|
||
public override SyntaxList<TNode> VisitList<TNode>(SyntaxList<TNode> list) | ||
{ | ||
list = base.VisitList(list); | ||
|
||
if (typeof (TNode) != typeof (AttributeListSyntax)) | ||
return list; | ||
|
||
var attributeLists = (SyntaxList<AttributeListSyntax>) (object) list; | ||
return (SyntaxList<TNode>) (object) VisitAttributeLists(attributeLists); | ||
} | ||
|
||
private static SyntaxList<AttributeListSyntax> VisitAttributeLists(SyntaxList<AttributeListSyntax> attributeLists) | ||
{ | ||
var result = new List<AttributeListSyntax>(); | ||
|
||
foreach (var attributeList in attributeLists) | ||
{ | ||
var firstIndex = result.Count; | ||
|
||
for (var i = 0; i < attributeList.Attributes.Count; i++) | ||
{ | ||
var attribute = attributeList.Attributes[i]; | ||
var separatorTrivia = i < attributeList.Attributes.Count - 1 | ||
? attributeList.Attributes.GetSeparator(i).GetAllTrivia() | ||
: Enumerable.Empty<SyntaxTrivia>(); | ||
|
||
var attributeWithoutTrivia = attribute.WithoutLeadingTrivia().WithoutTrailingTrivia(); | ||
var singletonList = SyntaxFactory.AttributeList(attributeList.Target, SyntaxFactory.SeparatedList(new[] { attributeWithoutTrivia })) | ||
.WithLeadingTrivia(attribute.GetLeadingTrivia()) | ||
.WithTrailingTrivia(attribute.GetTrailingTrivia().Concat(separatorTrivia)); | ||
result.Add(singletonList); | ||
} | ||
|
||
var lastIndex = result.Count - 1; | ||
|
||
var leadingTrivia = attributeList.GetLeadingTrivia() | ||
.Concat(attributeList.OpenBracketToken.TrailingTrivia) | ||
.Concat(result[firstIndex].GetLeadingTrivia()); | ||
|
||
var trailingTrivia = result[lastIndex].GetTrailingTrivia() | ||
.Concat(attributeList.CloseBracketToken.LeadingTrivia) | ||
.Concat(attributeList.GetTrailingTrivia()); | ||
|
||
result[firstIndex] = result[firstIndex].WithLeadingTrivia(leadingTrivia); | ||
result[lastIndex] = result[lastIndex].WithTrailingTrivia(trailingTrivia); | ||
This comment was marked as spam.
Sorry, something went wrong. |
||
} | ||
|
||
return SyntaxFactory.List(result); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
Sorry, something went wrong.