forked from Azhaguthasan/TypescriptCodeDom
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTypescriptCodeGenerator.cs
More file actions
128 lines (113 loc) · 4.99 KB
/
TypescriptCodeGenerator.cs
File metadata and controls
128 lines (113 loc) · 4.99 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
using System;
using System.CodeDom;
using System.CodeDom.Compiler;
using System.IO;
using System.Linq;
using TypescriptCodeDom.CodeExpressions;
using TypescriptCodeDom.CodeNamespaces;
using TypescriptCodeDom.CodeStatements;
using TypescriptCodeDom.CodeTypeMembers;
using TypescriptCodeDom.Common.Keyword;
using TypescriptCodeDom.Common.TypeMapper;
namespace TypescriptCodeDom
{
public class TypescriptCodeGenerator : ICodeGenerator
{
private readonly IMemberFactory _memberFactory;
private readonly ITypescriptKeywordsHandler _typescriptKeywordsHandler;
private readonly ITypescriptTypeMapper _typescriptTypeMapper;
private readonly IExpressionFactory _expressionFactory;
private readonly ITypescriptNamespace _typescriptNamespace;
private readonly IStatementFactory _statementFactory;
public TypescriptCodeGenerator(
IMemberFactory memberFactory,
IStatementFactory statementFactory,
IExpressionFactory expressionFactory,
ITypescriptNamespace typescriptNamespace,
ITypescriptTypeMapper typescriptTypeMapper,
ITypescriptKeywordsHandler typescriptKeywordsHandler)
{
_memberFactory = memberFactory;
_typescriptKeywordsHandler = typescriptKeywordsHandler;
_typescriptTypeMapper = typescriptTypeMapper;
_expressionFactory = expressionFactory;
_typescriptNamespace = typescriptNamespace;
_statementFactory = statementFactory;
System.Diagnostics.Debug.WriteLine("TypescriptCodeGenerator Created");
}
public bool IsValidIdentifier(string value)
{
return _typescriptKeywordsHandler.IsValidIdentifier(value);
}
public void ValidateIdentifier(string value)
{
_typescriptKeywordsHandler.ValidateIdentifier(value);
}
public string CreateEscapedIdentifier(string value)
{
return _typescriptKeywordsHandler.CreateEscapedIdentifier(value);
}
public string CreateValidIdentifier(string value)
{
return _typescriptKeywordsHandler.CreateValidIdentifier(value);
}
public string GetTypeOutput(CodeTypeReference type)
{
return _typescriptTypeMapper.GetTypeOutput(type);
}
public bool Supports(GeneratorSupport supports)
{
var supportedOperations = GetSupportedOperations();
return (supports & supportedOperations) != 0 ;
}
private GeneratorSupport GetSupportedOperations()
{
return GeneratorSupport.ArraysOfArrays
| GeneratorSupport.MultidimensionalArrays
| GeneratorSupport.TryCatchStatements
| GeneratorSupport.DeclareValueTypes
| GeneratorSupport.DeclareEnums
| GeneratorSupport.GotoStatements
| GeneratorSupport.StaticConstructors
| GeneratorSupport.DeclareInterfaces
| GeneratorSupport.DeclareDelegates
| GeneratorSupport.DeclareEvents
| GeneratorSupport.NestedTypes
| GeneratorSupport.MultipleInterfaceMembers
| GeneratorSupport.ComplexExpressions
| GeneratorSupport.PartialTypes
| GeneratorSupport.GenericTypeReference
| GeneratorSupport.GenericTypeDeclaration
| GeneratorSupport.DeclareIndexerProperties;
}
public void GenerateCodeFromExpression(CodeExpression codeExpression, TextWriter textWriter, CodeGeneratorOptions options)
{
var expression = _expressionFactory.GetExpression(codeExpression, options);
textWriter.Write(expression.Evaluate());
}
public void GenerateCodeFromStatement(CodeStatement codeStatement, TextWriter textWriter, CodeGeneratorOptions options)
{
var statement = _statementFactory.GetStatement(codeStatement, options);
textWriter.Write(statement.Expand());
}
public void GenerateCodeFromNamespace(CodeNamespace codeNamespace, TextWriter textWriter, CodeGeneratorOptions options)
{
var namespaces = _typescriptNamespace.Expand(codeNamespace, options);
textWriter.Write(namespaces);
}
public void GenerateCodeFromType(CodeTypeDeclaration member, TextWriter textWriter, CodeGeneratorOptions options)
{
var typeMember = _memberFactory.GetMember(member, options);
textWriter.Write(typeMember.Expand());
}
public void GenerateCodeFromCompileUnit(CodeCompileUnit codeCompileUnit, TextWriter textWriter, CodeGeneratorOptions options)
{
var namespaces = codeCompileUnit.Namespaces
.OfType<CodeNamespace>()
.Select(ns => _typescriptNamespace.Expand(ns, options))
.ToList();
var namespacesExpression = string.Join(Environment.NewLine, namespaces);
textWriter.Write(namespacesExpression);
}
}
}