forked from joao-neves95/CSharpToTypescript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathListToArrayReplacement.cs
106 lines (82 loc) · 3.3 KB
/
ListToArrayReplacement.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
/*
* Copyright (c) 2019 João Pedro Martins Neves (shivayl) - All Rights Reserved.
*
* CSharpToTypescript is licensed under the GNU Lesser General Public License (LGPL),
* version 3, located in the root of this project, under the name "LICENSE.md".
*
*/
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using System.Linq;
namespace CSharpToTypescript
{
class ListToArrayReplacementRewriter : CSharpSyntaxRewriter
{
public bool IsChangeInObjectCreation { get; private set; }
public ListToArrayReplacementRewriter(bool isChangeInObjectCreation = false)
{
IsChangeInObjectCreation = isChangeInObjectCreation;
}
public override SyntaxNode VisitGenericName(GenericNameSyntax node)
{
if (!IsList( node ))
{
return base.VisitGenericName( node );
}
return ToArray( node );
}
public override SyntaxNode VisitQualifiedName(QualifiedNameSyntax node)
{
GenericNameSyntax foundNode = FindListNode( node );
if (foundNode == null)
{
return base.VisitQualifiedName( node );
}
return ToArray( foundNode );
//return node.ReplaceNode(foundNode, arrayNode);
}
public override SyntaxNode VisitObjectCreationExpression(ObjectCreationExpressionSyntax node)
{
if (IsChangeInObjectCreation)
{
return base.VisitObjectCreationExpression( node );
}
var found = FindListNode( node );
if (found == null)
{
return base.VisitObjectCreationExpression( node );
}
ListToArrayReplacementRewriter rewriter = new ListToArrayReplacementRewriter( true );
return rewriter.Visit( node );
}
private bool IsList(GenericNameSyntax syntax)
{
return syntax.Identifier.ValueText == "List" || syntax.Identifier.ValueText == "IList";
}
private SyntaxNode ToArray(GenericNameSyntax node)
{
if (IsChangeInObjectCreation)
{
return node.ReplaceToken( node.Identifier, SyntaxFactory.Identifier( "Array" ) );
}
var firstTypeSyntax = node.TypeArgumentList.Arguments.First();
var typeName = (TypeSyntax)new ListToArrayReplacementRewriter().Visit( firstTypeSyntax );
return SyntaxFactory.ArrayType( typeName,
SyntaxFactory.List( new ArrayRankSpecifierSyntax[] { SyntaxFactory.ArrayRankSpecifier() } ) );
}
private GenericNameSyntax FindListNode(SyntaxNode node)
{
return node.DescendantNodes().Where( f => f is GenericNameSyntax && IsList( (GenericNameSyntax)f ) )
.OfType<GenericNameSyntax>()
.FirstOrDefault();
}
}
public class ListToArrayReplacement
{
public static CSharpSyntaxNode ReplaceList(CSharpSyntaxNode syntaxNode)
{
return (CSharpSyntaxNode)new ListToArrayReplacementRewriter().Visit( syntaxNode );
}
}
}