public static string ConvertToTypescript(string csharpCode, IEnumerable <Func <CSharpSyntaxNode, CSharpSyntaxNode> > funcs = null)
        {
            var mscorlib = MetadataReference.CreateFromFile(typeof(object).Assembly.Location);
            var tree     = (CSharpSyntaxTree)CSharpSyntaxTree.ParseText(csharpCode);

            if (tree.GetDiagnostics().Any(f => f.Severity == DiagnosticSeverity.Error))
            {
                return(null);
            }

            var root = tree.GetRoot();

            if (funcs != null)
            {
                foreach (var func in funcs)
                {
                    root = func(root);
                }
            }

            var translationNode = TF.Get(root, null);

            var compilation = CSharpCompilation.Create("TemporaryCompilation",
                                                       syntaxTrees: new[] { tree }, references: new[] { mscorlib });
            var model = compilation.GetSemanticModel(tree);

            translationNode.Compilation   = compilation;
            translationNode.SemanticModel = model;

            translationNode.ApplyPatch();
            return(translationNode.Translate());
        }
        private string convertToTypescript(string text)
        {
            try
            {
                var tree = (CSharpSyntaxTree)CSharpSyntaxTree.ParseText(text);

                // detect to see if it's actually C# sourcode by checking whether it has any error
                if (tree.GetDiagnostics().Any(f => f.Severity == DiagnosticSeverity.Error))
                {
                    return(null);
                }

                var root            = tree.GetRoot();
                var translationNode = TF.Get(root, null);

                var compilation = CSharpCompilation.Create("TemporaryCompilation",
                                                           syntaxTrees: new[] { tree }, references: new[] { Mscorlib });
                var model = compilation.GetSemanticModel(tree);

                translationNode.Compilation   = compilation;
                translationNode.SemanticModel = model;

                translationNode.ApplyPatch();
                return(translationNode.Translate());
            }
            catch (Exception ex)
            {
                // TODO
                // swallow exception .!!!!!!!!!!!!!!!!!!!!!!!
            }

            return(null);
        }
예제 #3
0
        public string ConvertToTypescript(string text, ConvertSettings settingStore)
        {
            try
            {
                CSharpSyntaxTree text1 = (CSharpSyntaxTree)CSharpSyntaxTree.ParseText(text, null, "", null, new CancellationToken());
                if (text1.GetDiagnostics(new CancellationToken()).Any(f => f.Severity == (DiagnosticSeverity)3))
                {
                    return(null);
                }
                CSharpSyntaxNode syntaxNode = text1.GetRoot(new CancellationToken());
                if (this.IsEmptyRoot(syntaxNode))
                {
                    return(null);
                }

                if (settingStore.IsConvertToInterface)
                {
                    syntaxNode = ClassToInterfaceReplacement.ReplaceClass(syntaxNode);
                }

                if (settingStore.IsConvertMemberToCamelCase)
                {
                    syntaxNode = MakeMemberCamelCase.Make(syntaxNode);
                }

                if (settingStore.IsConvertListToArray)
                {
                    syntaxNode = ListToArrayReplacement.ReplaceList(syntaxNode);
                }

                if (settingStore.ReplacedTypeNameArray.Length != 0)
                {
                    syntaxNode = TypeNameReplacement.Replace(settingStore.ReplacedTypeNameArray, syntaxNode);
                }

                if (settingStore.AddIPrefixInterfaceDeclaration)
                {
                    syntaxNode = AddIPrefixInterfaceDeclaration.AddIPrefix(syntaxNode);
                }

                if (settingStore.IsInterfaceOptionalProperties)
                {
                    syntaxNode = OptionalInterfaceProperties.AddOptional(syntaxNode);
                }

                CSharpSyntaxTree  syntaxTree        = (CSharpSyntaxTree)syntaxNode.SyntaxTree;
                SyntaxTranslation syntaxTranslation = TF.Get(syntaxNode, null);
                CSharpCompilation csharpCompilation = CSharpCompilation.Create("TemporaryCompilation", new[] { syntaxTree });
                SemanticModel     semanticModel     = ((Compilation)csharpCompilation).GetSemanticModel(syntaxTree, false);
                syntaxTranslation.Compilation   = csharpCompilation;
                syntaxTranslation.SemanticModel = semanticModel;
                syntaxTranslation.ApplyPatch();
                return(syntaxTranslation.Translate());
            }
            catch (Exception ex)
            {
            }
            return((string)null);
        }
예제 #4
0
        public string ConvertToTypescript(string text, ISettingStore settingStore)
        {
            try
            {
                CSharpSyntaxTree tree = (CSharpSyntaxTree)CSharpSyntaxTree.ParseText(text);

                // detect to see if it's actually C# sourcode by checking whether it has any error
                if (tree.GetDiagnostics().Any(f => f.Severity == DiagnosticSeverity.Error))
                {
                    return(null);
                }

                CSharpSyntaxNode root = tree.GetRoot();

                // if it only contains comments, just return the original texts
                if (IsEmptyRoot(root))
                {
                    return(null);
                }

                if (settingStore.IsConvertToInterface)
                {
                    root = ClassToInterfaceReplacement.ReplaceClass(root);
                }

                if (settingStore.IsConvertMemberToCamelCase)
                {
                    root = MakeMemberCamelCase.Make(root);
                }

                if (settingStore.IsConvertListToArray)
                {
                    root = ListToArrayReplacement.ReplaceList(root);
                }

                if (settingStore.ReplacedTypeNameArray.Length > 0)
                {
                    root = TypeNameReplacement.Replace(settingStore.ReplacedTypeNameArray, root);
                }

                if (settingStore.AddIPrefixInterfaceDeclaration)
                {
                    root = AddIPrefixInterfaceDeclaration.AddIPrefix(root);
                }

                if (settingStore.IsInterfaceOptionalProperties)
                {
                    root = OptionalInterfaceProperties.AddOptional(root);
                }

                tree = (CSharpSyntaxTree)root.SyntaxTree;

                var translationNode = TF.Get(root, null);

                var compilation = CSharpCompilation.Create(
                    "TemporaryCompilation",
                    syntaxTrees: new[] { tree },
                    references: new[] { Mscorlib }
                    );

                var model = compilation.GetSemanticModel(tree);

                translationNode.Compilation   = compilation;
                translationNode.SemanticModel = model;

                translationNode.ApplyPatch();
                return(translationNode.Translate());
            }
            catch (Exception ex)
            {
                // TODO
                // swallow exception .!!!!!!!!!!!!!!!!!!!!!!!
                Console.WriteLine(ex.Message);
                Console.WriteLine(ex.StackTrace);
            }

            return(null);
        }
예제 #5
0
        public string ConvertToTypescript(string text, ISettingStore settingStore = null)
        {
            if (settingStore == null)
            {
                settingStore = new DefaultSettingStore();
            }

            try
            {
                var tree = (CSharpSyntaxTree)CSharpSyntaxTree.ParseText(text);

                if (tree.GetDiagnostics().Any(f => f.Severity == DiagnosticSeverity.Error))
                {
                    return(null);
                }

                var root = tree.GetRoot();

                if (IsEmptyRoot(root))
                {
                    return(null);
                }

                if (settingStore.IsConvertToInterface)
                {
                    root = ClassToInterfaceReplacement.ReplaceClass(root);
                }

                if (settingStore.IsConvertMemberToCamelCase)
                {
                    root = MakeMemberCamelCase.Make(root);
                }

                if (settingStore.IsConvertListToArray)
                {
                    root = ListToArrayReplacement.ReplaceList(root);
                }

                if (settingStore.AddIPrefixInterfaceDeclaration)
                {
                    root = AddIPrefixInterfaceDeclaration.AddIPrefix(root);
                }

                if (settingStore.IsInterfaceOptionalProperties)
                {
                    root = OptionalInterfaceProperties.AddOptional(root);
                }

                tree = (CSharpSyntaxTree)root.SyntaxTree;

                var translationNode = TF.Get(root, null);

                var compilation = CSharpCompilation.Create("TemporaryCompilation",
                                                           syntaxTrees: new[] { tree }, references: new[] { Mscorlib });
                var model = compilation.GetSemanticModel(tree);

                translationNode.Compilation   = compilation;
                translationNode.SemanticModel = model;

                translationNode.ApplyPatch();
                return(translationNode.Translate());
            }
            catch (Exception ex)
            {
            }

            return(null);
        }