This repository was 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
/
Copy pathMemberThisRule.cs
178 lines (152 loc) · 6.69 KB
/
MemberThisRule.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
namespace Microsoft.DotNet.CodeFormatting.Rules
{
[LocalSemanticRule(MemberThisRule.Name, MemberThisRule.Description, LocalSemanticRuleOrder.MemberThisRule, DefaultRule = false)]
internal sealed class MemberThisRule : CSharpOnlyFormattingRule, ILocalSemanticFormattingRule
{
internal const string Name = "MemberThis";
internal const string Description = "Add this/Me prefixes on all member references";
private sealed class MemberThisRewriter : CSharpSyntaxRewriter
{
private SemanticModel semanticModel;
private readonly CancellationToken cancellationToken;
internal bool AddedAnnotations
{
get; private set;
}
internal MemberThisRewriter(SemanticModel semanticModel, CancellationToken cancellationToken)
{
this.semanticModel = semanticModel;
this.cancellationToken = cancellationToken;
}
public override SyntaxNode VisitGenericName(GenericNameSyntax name)
{
if (this.ShouldModify(name))
{
this.AddedAnnotations = true;
// Create a copy of this name
GenericNameSyntax newName = SyntaxFactory.GenericName(name.Identifier, name.TypeArgumentList);
// Add the "this" to the expression
return SyntaxFactory.MemberAccessExpression(
SyntaxKind.SimpleMemberAccessExpression,
SyntaxFactory.ThisExpression(),
newName.WithoutTrivia())
.WithLeadingTrivia(name.GetLeadingTrivia())
.WithTrailingTrivia(name.GetTrailingTrivia());
}
return base.VisitGenericName(name);
}
public override SyntaxNode VisitIdentifierName(IdentifierNameSyntax name)
{
if (this.ShouldModify(name))
{
this.AddedAnnotations = true;
// Create a copy of this name
IdentifierNameSyntax newName = SyntaxFactory.IdentifierName(name.Identifier);
// Add the "this" to the expression
return SyntaxFactory.MemberAccessExpression(
SyntaxKind.SimpleMemberAccessExpression,
SyntaxFactory.ThisExpression(),
newName.WithoutTrivia())
.WithLeadingTrivia(name.GetLeadingTrivia())
.WithTrailingTrivia(name.GetTrailingTrivia());
}
return base.VisitIdentifierName(name);
}
private bool ShouldModify(SimpleNameSyntax name)
{
// Ignore cases that don't make sense
if (name.Parent is MemberAccessExpressionSyntax)
{
MemberAccessExpressionSyntax parent = (MemberAccessExpressionSyntax)name.Parent;
if (parent.Name == name)
{
return false;
}
}
else if (name.Parent is QualifiedNameSyntax)
{
QualifiedNameSyntax parent = (QualifiedNameSyntax)name.Parent;
if (parent.Left == name)
{
return false;
}
}
else if (name.Parent is MemberBindingExpressionSyntax || name.Parent is AliasQualifiedNameSyntax)
{
return false;
}
return this.IsLocalMember(name);
}
// Check if the name referes to a non-static member on the same type.
private bool IsLocalMember(SimpleNameSyntax name)
{
ISymbol symbol = semanticModel.GetSymbolInfo(name, this.cancellationToken).Symbol;
// Only add the "this" to non-static member references
if (symbol == null || symbol.IsStatic || !IsMember(symbol))
{
return false;
}
// Make sure the reference is in the same type
return IsDerivedType(this.GetParentType(name), symbol.ContainingType);
}
private static bool IsDerivedType(INamedTypeSymbol type, INamedTypeSymbol baseType)
{
if (type == null)
return false;
if (object.Equals(type, baseType))
return true;
return IsDerivedType(type.BaseType, baseType);
}
private static bool IsMember(ISymbol symbol)
{
switch (symbol.Kind)
{
case SymbolKind.Event:
case SymbolKind.Field:
case SymbolKind.Method:
case SymbolKind.Property:
return true;
default:
return false;
}
}
// Find the type this node resides in
private INamedTypeSymbol GetParentType(SyntaxNode node)
{
while (node != null)
{
if (node.IsKind(SyntaxKind.ClassDeclaration) || node.IsKind(SyntaxKind.StructDeclaration))
{
return (INamedTypeSymbol)semanticModel.GetDeclaredSymbol(node);
}
node = node.Parent;
}
return null;
}
}
/// <summary>
/// Entry point to the rule
/// </summary>
public async Task<SyntaxNode> ProcessAsync(Document document, SyntaxNode syntaxNode, CancellationToken cancellationToken)
{
SemanticModel semanticModel = await document.GetSemanticModelAsync(cancellationToken);
// Visit all the nodes in the model
MemberThisRewriter rewriter = new MemberThisRewriter(semanticModel, cancellationToken);
SyntaxNode newNode = rewriter.Visit(syntaxNode);
// If changes are not mode return the original model
if (!rewriter.AddedAnnotations)
{
return syntaxNode;
}
return newNode;
}
}
}