-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathInitializerRewriter.cs
executable file
·75 lines (60 loc) · 3.34 KB
/
InitializerRewriter.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
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory;
namespace ConsoleApplication
{
public class InitializerRewriter : CSharpSyntaxRewriter
{
private readonly SemanticModel SemanticModel;
public InitializerRewriter(SemanticModel semanticModel)
{
this.SemanticModel = semanticModel;
}
public override SyntaxNode VisitVariableDeclaration(VariableDeclarationSyntax node)
{
// determination of the type of the variable(s)
var typeSymbol = (ITypeSymbol)this.SemanticModel.GetSymbolInfo(node.Type).Symbol;
bool changed = false;
// you could declare more than one variable with one expression
SeparatedSyntaxList<VariableDeclaratorSyntax> vs = node.Variables;
// we create a space to improve readability
SyntaxTrivia space = SyntaxFactory.SyntaxTrivia(SyntaxKind.WhitespaceTrivia, " ");
for (var i = 0; i < node.Variables.Count; i++)
{
// there is not an initialization
if (this.SemanticModel.GetSymbolInfo(node.Type).Symbol.ToString() == "int" &&
node.Variables[i].Initializer == null)
{
// we create a new espression "42"
// preceded by the space we create earlier
ExpressionSyntax es = SyntaxFactory.ParseExpression("42")
.WithLeadingTrivia(Space);
// basically we create an assignment to the espression we just created
EqualsValueClauseSyntax evc = SyntaxFactory.EqualsValueClause(es)
.WithLeadingTrivia(space);
// we replace the null initializer with ours
vs = vs.Replace(vs.ElementAt(i), vs.ElementAt(i).WithInitializer(evc));
changed = true;
}
// there is an initialization but it's not to 42
if (this.SemanticModel.GetSymbolInfo(node.Type).Symbol.ToString() == "int" &&
node.Variables[i].Initializer != null &&
!node.Variables[i].Initializer.Value.IsEquivalentTo(SyntaxFactory.ParseExpression("42")))
{
ExpressionSyntax es = SyntaxFactory.ParseExpression("42")
.WithLeadingTrivia(Space);
EqualsValueClauseSyntax evc = SyntaxFactory.EqualsValueClause(es);
vs = vs.Replace(vs.ElementAt(i), vs.ElementAt(i).WithInitializer(evc));
changed = true;
}
}
if(changed == true)
return node.WithVariables(vs);
return base.VisitVariableDeclaration(node);
}
}
}