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

Commit ffe20fe

Browse files
committed
Merge pull request #32 from dotnet/usesimplifier
Use simplifier to remove 'this.' usage
2 parents b0ab437 + 2b6c2c1 commit ffe20fe

File tree

4 files changed

+25
-18
lines changed

4 files changed

+25
-18
lines changed

src/Microsoft.DotNet.CodeFormatting.Tests/Rules/ExplicitThisRuleTests.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,9 @@ class C1
109109
void M()
110110
{
111111
this. /* comment1 */ _field /* comment 2 */ = 0;
112+
// before comment
113+
this._field = 42;
114+
// after comment
112115
}
113116
}
114117
";
@@ -120,7 +123,10 @@ class C1
120123
121124
void M()
122125
{
123-
/* comment1 */ _field /* comment 2 */ = 0;
126+
_field /* comment 2 */ = 0;
127+
// before comment
128+
_field = 42;
129+
// after comment
124130
}
125131
}
126132
";

src/Microsoft.DotNet.CodeFormatting.Tests/Rules/NonAsciiCharactersAreEscapedInLiteralsRuleTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
namespace Microsoft.DotNet.CodeFormatting.Tests
77
{
8-
public class NonAsciiChractersAreEscapedInLiteralsTests : CodeFormattingTestBase
8+
public class NonAsciiCharactersAreEscapedInLiteralsTests : CodeFormattingTestBase
99
{
1010
[Fact]
1111
public void CanUseNonAsciiCharactersInComments()
@@ -65,7 +65,7 @@ class Test
6565

6666
internal override IFormattingRule GetFormattingRule()
6767
{
68-
return new Rules.NonAsciiChractersAreEscapedInLiterals();
68+
return new Rules.NonAsciiCharactersAreEscapedInLiterals();
6969
}
7070
}
7171
}

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

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using Microsoft.CodeAnalysis;
88
using Microsoft.CodeAnalysis.CSharp;
99
using Microsoft.CodeAnalysis.CSharp.Syntax;
10+
using Microsoft.CodeAnalysis.Simplification;
1011

1112
namespace Microsoft.DotNet.CodeFormatting.Rules
1213
{
@@ -17,6 +18,12 @@ private sealed class ExplicitThisRewriter : CSharpSyntaxRewriter
1718
{
1819
private readonly SemanticModel _semanticModel;
1920
private readonly CancellationToken _cancellationToken;
21+
private bool _addedAnnotations;
22+
23+
internal bool AddedAnnotations
24+
{
25+
get { return _addedAnnotations; }
26+
}
2027

2128
internal ExplicitThisRewriter(SemanticModel semanticModel, CancellationToken cancellationToken)
2229
{
@@ -38,25 +45,14 @@ public override SyntaxNode VisitMemberAccessExpression(MemberAccessExpressionSyn
3845
var field = (IFieldSymbol)symbolInfo.Symbol;
3946
if (field.DeclaredAccessibility == Accessibility.Private)
4047
{
41-
return RemoveQualification(node);
48+
_addedAnnotations = true;
49+
return node.WithAdditionalAnnotations(Simplifier.Annotation);
4250
}
4351
}
4452
}
4553

4654
return node;
4755
}
48-
49-
private static NameSyntax RemoveQualification(MemberAccessExpressionSyntax memberSyntax)
50-
{
51-
var thisSyntax = memberSyntax.Expression;
52-
var nameSyntax = memberSyntax.Name;
53-
var triviaList = thisSyntax
54-
.GetLeadingTrivia()
55-
.AddRange(thisSyntax.GetTrailingTrivia())
56-
.AddRange(memberSyntax.OperatorToken.GetAllTrivia())
57-
.AddRange(nameSyntax.GetLeadingTrivia());
58-
return nameSyntax.WithLeadingTrivia(triviaList);
59-
}
6056
}
6157

6258
public async Task<Document> ProcessAsync(Document document, CancellationToken cancellationToken)
@@ -70,7 +66,12 @@ public async Task<Document> ProcessAsync(Document document, CancellationToken ca
7066
var semanticModel = await document.GetSemanticModelAsync(cancellationToken);
7167
var rewriter = new ExplicitThisRewriter(semanticModel, cancellationToken);
7268
var newNode = rewriter.Visit(syntaxNode);
73-
return document.WithSyntaxRoot(newNode);
69+
if (!rewriter.AddedAnnotations)
70+
{
71+
return document;
72+
}
73+
74+
return await Simplifier.ReduceAsync(document.WithSyntaxRoot(newNode), cancellationToken: cancellationToken);
7475
}
7576
}
7677
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
namespace Microsoft.DotNet.CodeFormatting.Rules
1717
{
1818
[RuleOrder(RuleOrder.NonAsciiChractersAreEscapedInLiterals)]
19-
internal sealed class NonAsciiChractersAreEscapedInLiterals : IFormattingRule
19+
internal sealed class NonAsciiCharactersAreEscapedInLiterals : IFormattingRule
2020
{
2121
public async Task<Document> ProcessAsync(Document document, CancellationToken cancellationToken)
2222
{

0 commit comments

Comments
 (0)