-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSyntaxRewriter.cs
More file actions
157 lines (126 loc) · 5.81 KB
/
Copy pathSyntaxRewriter.cs
File metadata and controls
157 lines (126 loc) · 5.81 KB
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
using System.Collections.Generic;
using System.Linq;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using NETMetaCoder.Abstractions;
namespace NETMetaCoder
{
/// <summary>
/// This type is responsible for rewriting a compilation unit's syntax.
/// </summary>
public static class SyntaxRewriter
{
/// <summary>
/// Constructs a new <see cref="SyntaxRewriter"/> instance.
/// </summary>
/// <param name="tree"></param>
/// <param name="attributeNames"></param>
/// <param name="syntaxEnvelope"></param>
/// <returns></returns>
public static SyntaxRewriteResult RewriteSyntaxTree(
SyntaxTree tree, IEnumerable<string> attributeNames, SyntaxEnvelope.SyntaxEnvelope syntaxEnvelope) =>
new SyntaxRewriterWithContext(tree, attributeNames, syntaxEnvelope).RewriteSyntaxTree();
private sealed class SyntaxRewriterWithContext : CSharpSyntaxRewriter
{
private readonly SyntaxTree _tree;
private readonly IEnumerable<string> _attributeNames;
private readonly HashSet<ushort> _targetNodeIndices;
private ushort _nodeIndex;
private bool _hasChanges;
public SyntaxRewriterWithContext(SyntaxTree tree, IEnumerable<string> attributeNames,
SyntaxEnvelope.SyntaxEnvelope syntaxEnvelope)
{
_tree = tree;
_attributeNames = attributeNames.ToList();
_targetNodeIndices = syntaxEnvelope.GatherNodeIndices();
}
public SyntaxRewriteResult RewriteSyntaxTree()
{
if (_tree.GetDiagnostics().Any(d => d.Severity == DiagnosticSeverity.Error))
{
throw new NETMetaCoderException("The syntax tree has errors and no rewriting will take place.");
}
if (!_targetNodeIndices.Any())
{
return new SyntaxRewriteResult
{
SyntaxTree = _tree,
HasChanges = false
};
}
var newTree = _tree.WithRootAndOptions(Visit(_tree.GetCompilationUnitRoot()), _tree.Options);
return new SyntaxRewriteResult
{
SyntaxTree = newTree,
HasChanges = _hasChanges
};
}
public override SyntaxNode VisitNamespaceDeclaration(NamespaceDeclarationSyntax node)
{
_nodeIndex++;
return base.VisitNamespaceDeclaration(node);
}
public override SyntaxNode VisitClassDeclaration(ClassDeclarationSyntax node)
{
_nodeIndex++;
if (!_targetNodeIndices.Contains(_nodeIndex))
{
return base.VisitClassDeclaration(node);
}
node = node.WithPartialKeywordPrefix();
return base.VisitClassDeclaration(node);
}
public override SyntaxNode VisitStructDeclaration(StructDeclarationSyntax node)
{
_nodeIndex++;
if (!_targetNodeIndices.Contains(_nodeIndex))
{
return base.VisitStructDeclaration(node);
}
node = node.WithPartialKeywordPrefix();
return base.VisitStructDeclaration(node);
}
public override SyntaxNode VisitMethodDeclaration(MethodDeclarationSyntax node)
{
_nodeIndex++;
if (!_targetNodeIndices.Contains(_nodeIndex))
{
return node;
}
var attributeNamesFound = node.FindAttributes(_attributeNames, null, null);
var isTarget = attributeNamesFound.Any();
_hasChanges = isTarget;
if (isTarget)
{
var attributeNameNeedles = attributeNamesFound
.Select(StringExtensions.ToAttributeNameNeedle)
.Distinct()
.ToList();
attributeNameNeedles.Sort();
var newMethodName = node.Identifier.ToString();
newMethodName = attributeNameNeedles.Aggregate(newMethodName,
(methodName, attributeNameNeedle) => methodName.Replace(attributeNameNeedle, ""));
newMethodName = attributeNameNeedles.Aggregate(newMethodName,
(methodName, attributeNameNeedle) => methodName + attributeNameNeedle);
var attributeLists = new SyntaxList<AttributeListSyntax>(node.AttributeLists.Select(al =>
{
var attributes = al.Attributes.Select(a =>
a.IsMethodObsoletionAttribute()
? a.WithArgumentList(SyntaxFactory.AttributeArgumentList())
: a);
return al.WithAttributes(SyntaxFactory.SeparatedList(attributes));
}).Where(al => al.Attributes.Any()));
var newModifiers = new SyntaxTokenList(node.Modifiers.Where(modifier =>
!modifier.IsKind(SyntaxKind.OverrideKeyword) && !modifier.IsKind(SyntaxKind.SealedKeyword)));
node = node
.WithAttributeLists(attributeLists)
.WithModifiers(newModifiers)
.WithExplicitInterfaceSpecifier(null)
.WithIdentifier(SyntaxFactory.Identifier(newMethodName));
}
return base.VisitMethodDeclaration(node);
}
}
}
}