Example #1
0
        public static TypeStatement Identify(
            Node node,
            TypeScriptAST ast,
            ClassMetadata classMetadata,
            TypeOverrideDetails typeOverrideDetails
            )
        {
            // Check if Class
            var classesCache = ast.RootNode.OfKind(
                SyntaxKind.ClassDeclaration
                );

            if (node is ClassDeclaration classDeclaration &&
                classDeclaration.HeritageClauses != null &&
                classDeclaration.HeritageClauses.Any())
            {
                var herited = classDeclaration.HeritageClauses.First();
                if (herited != null)
                {
                    var identifiedClass = GenericTypeIdentifier.Identify(
                        herited.First,
                        classMetadata,
                        ast,
                        typeOverrideDetails
                        );
                    if (classesCache.Any(a => a.IdentifierStr == identifiedClass.Name))
                    {
                        return(identifiedClass);
                    }
                }
            }

            return(null);
        }
        public void ShouldReturnExpectedIdentificationOfInterfaceWhenCalledMultipleTimes()
        {
            // Given
            var interfaceIdentifierString = "ExampleInterface";

            var sourceFile = "interface.ts";
            var source     = File.ReadAllText($"./SourceFiles/{sourceFile}");
            var ast        = new TypeScriptAST(source, sourceFile);

            // When
            var cachedInterfaceIdentifier = new InterfaceResponseTypeIdentifierCached();

            // First Identify
            cachedInterfaceIdentifier.Identify(
                interfaceIdentifierString,
                ast
                ).Should() // Then
            .BeTrue();

            // Second Identify
            cachedInterfaceIdentifier.Identify(
                interfaceIdentifierString,
                ast
                ).Should() // Then
            .BeTrue();
        }
        public static TypeStatement Identify(
            string typeIdentifier,
            ClassMetadata classMetadata,
            TypeScriptAST ast,
            TypeOverrideDetails typeOverrideDetails
            )
        {
            // Get node from typeIdentifier
            var node = ast.RootNode.OfKind(
                SyntaxKind.TypeAliasDeclaration
                ).FirstOrDefault(
                child => child.IdentifierStr == typeIdentifier
                );

            if (node == null)
            {
                return(null);
            }

            return(GenericTypeIdentifier.Identify(
                       node.Last,
                       classMetadata,
                       ast,
                       typeOverrideDetails
                       ));
        }
 public override bool Identify(
     string identifierString,
     TypeScriptAST ast
     )
 {
     if (!_isCachedSetup)
     {
         var classDeclarations = ast.RootNode.OfKind(
             SyntaxKind.ClassDeclaration
             );
         foreach (var classDeclaration in classDeclarations)
         {
             _cacheClassDeclaration.Add(classDeclaration.IdentifierStr);
         }
         var interfaceDeclarations = ast.RootNode.OfKind(
             SyntaxKind.InterfaceDeclaration
             );
         foreach (var interfaceDeclaration in interfaceDeclarations)
         {
             _cacheInterfaceDeclaration.Add(interfaceDeclaration.IdentifierStr);
         }
         _isCachedSetup = true;
     }
     return(!_cacheClassDeclaration.Contains(identifierString) &&
            _cacheInterfaceDeclaration.Contains(identifierString));
 }
Example #5
0
        public static List <CodeItem> Map(string filePath, ICodeViewUserControl control)
        {
            _control = control;

            var jsString = File.ReadAllText(filePath);

            var ast = new TypeScriptAST(jsString, filePath);

            return(new List <CodeItem>
            {
                new CodeNamespaceItem
                {
                    Id = "Namespace" + filePath,
                    Kind = CodeItemKindEnum.Namespace,
                    Members = new List <CodeItem>
                    {
                        new CodeClassItem
                        {
                            Id = filePath,
                            Kind = CodeItemKindEnum.Class,
                            Access = CodeItemAccessEnum.Public,
                            Moniker = IconMapper.MapMoniker(CodeItemKindEnum.Class, CodeItemAccessEnum.Public),
                            Name = Path.GetFileNameWithoutExtension(filePath),
                            BorderColor = Colors.DarkGray,
                            Members = ast.RootNode.Children?.SelectMany(MapMember)?.ToList() ?? new List <CodeItem>()
                        }
                    }
                }
            });
        }
Example #6
0
        public void ShouldReturnExpectedIdentificationOfEnumWhenCalledMultipleTimes()
        {
            // Given
            ReadInteropTemplates.SetReadTemplates();
            var enumIdentifierString = "EnumExport";

            var sourceFile = "enum.ts";
            var source     = File.ReadAllText($"./SourceFiles/{sourceFile}");
            var ast        = new TypeScriptAST(source, sourceFile);

            // When
            var notCachedEnumIdentifier = new EnumTypeIdentifierCached();

            // First Identify
            notCachedEnumIdentifier.Identify(
                enumIdentifierString,
                ast
                ).Should() // Then
            .BeTrue();

            // Second Identify
            notCachedEnumIdentifier.Identify(
                enumIdentifierString,
                ast
                ).Should() // Then
            .BeTrue();
        }
        public void ShouldGenerateFormattedString()
        {
            // Given
            var sourceFile      = "CSharpTextFormatter.d.ts";
            var source          = File.ReadAllText($"./Formatter/SourceFiles/{sourceFile}");
            var expected        = File.ReadAllText($"./Formatter/ExpectedResults/CSharpTextFormatter.Expected.txt");
            var ast             = new TypeScriptAST(source, sourceFile);
            var typeOverrideMap = new Dictionary <string, string>();

            // When
            var generated = GenerateInteropClassStatement.Generate(
                "ProjectAssembly",
                "CSharpTextClass",
                ast,
                typeOverrideMap
                );
            var actual = GenerateClassStatementString.Generate(
                generated,
                new CSharpTextFormatter()
                );

            // Then
            actual.Should().Be(
                expected
                );
        }
        public void ShouldGenerateInterfaceString()
        {
            ReadInteropTemplates.SetReadTemplates();
            // Given
            var sourceFile      = "interface.ts";
            var source          = File.ReadAllText($"./SourceFiles/{sourceFile}");
            var expected        = File.ReadAllText($"./ExpectedResults/interface.Expected.txt");
            var ast             = new TypeScriptAST(source, sourceFile);
            var typeOverrideMap = new Dictionary <string, string>();

            // When
            var generated = GenerateInteropClassStatement.Generate(
                "ProjectAssembly",
                "ExampleInterface",
                ast,
                typeOverrideMap
                );
            var actual = GenerateClassStatementString.Generate(
                generated,
                new NoFormattingTextFormatter()
                );

            // Then
            actual.Should().Be(
                expected
                );
        }
        public static int Calculate(TypeScriptAST typeScriptAst, string fileContents)
        {
            typeScriptAst.MakeAST(fileContents);
            var methods   = typeScriptAst.OfKind(Zu.TypeScript.TsTypes.SyntaxKind.MethodDeclaration);
            var functions = typeScriptAst.OfKind(Zu.TypeScript.TsTypes.SyntaxKind.FunctionDeclaration);

            return(functions.Count() + methods.Count());
        }
Example #10
0
        private TypescriptFile(string filename, string rootDirectory)
        {
            Filename = filename;
            Ast      = new TypeScriptAST(File.ReadAllText(filename), filename);

            RootDirectory = rootDirectory;
            RootConstruct = new TypescriptConstruct(this.RootNode, rootDirectory);
        }
Example #11
0
 public static bool Identify(
     string identifierString,
     TypeScriptAST ast
     )
 {
     return(ACTIVE.Identify(
                identifierString,
                ast
                ));
 }
 public override bool Identify(
     TypeStatement type,
     TypeScriptAST ast
     )
 {
     return(base.Identify(
                type,
                ast
                ));
 }
 public static bool Identify(
     Node node,
     TypeScriptAST ast
     )
 {
     return(ACTIVE.Identify(
                node.IdentifierStr,
                ast
                ));
 }
 public static bool Identify(
     TypeStatement type,
     TypeScriptAST ast
     )
 {
     return(ACTIVE.Identify(
                type,
                ast
                ));
 }
Example #15
0
        private static List <CodeItem> MapMembers(TypeScriptAST ast)
        {
            if (ast == null ||
                ast.RootNode == null ||
                ast.RootNode.Children == null)
            {
                return(new List <CodeItem>());
            }

            return(ast.RootNode.Children.SelectMany(MapMember).ToList());
        }
 public virtual bool Identify(
     string identifierString,
     TypeScriptAST ast
     )
 {
     return(ast.RootNode.OfKind(
                SyntaxKind.TypeAliasDeclaration
                ).Any(
                child => child.IdentifierStr == identifierString &&
                child.IdentifierStr != JavaScriptTypes.Nullable
                ));
 }
Example #17
0
        public virtual bool Identify(
            string identifierString,
            TypeScriptAST ast
            )
        {
            var hasEnumDeclarations = ast.RootNode.OfKind(
                SyntaxKind.EnumDeclaration
                ).Any(
                child => child.IdentifierStr == identifierString
                );

            return(hasEnumDeclarations);
        }
Example #18
0
        internal static bool Identify(
            Node parameter,
            ClassMetadata classMetadata,
            TypeScriptAST ast,
            TypeOverrideDetails typeOverrideDetails,
            out TypeStatement type
            )
        {
            type = new TypeStatement
            {
                Name = GenerationIdentifiedTypes.Unknown,
            };
            if (IsUnionTypeRule.Check(parameter))
            {
                var unionNode = parameter.OfKind(
                    SyntaxKind.UnionType
                    ).FirstOrDefault()
                                .Children.Where(
                    // Make sure does not contain undefined
                    a => a.Kind != SyntaxKind.UndefinedKeyword &&
                    a.Kind != SyntaxKind.NullKeyword &&
                    a.Kind != SyntaxKind.TypeLiteral &&
                    a.Kind != SyntaxKind.LiteralType
                    ).ToList().FirstOrDefault();

                if (unionNode != null)
                {
                    type = GenericTypeIdentifier.Identify(
                        unionNode,
                        classMetadata,
                        ast,
                        typeOverrideDetails
                        );
                }

                return(true);
            }
            else if (parameter.Kind == SyntaxKind.UnionType)
            {
                type = GenericTypeIdentifier.Identify(
                    parameter.First,
                    classMetadata,
                    ast,
                    typeOverrideDetails
                    );
                return(true);
            }
            return(false);
        }
Example #19
0
        public void Slim_ShouldGenerateExpectedObserverable()
        {
            // Given
            var sourceFile      = "observable.ts";
            var source          = File.ReadAllText($"./SourceFiles/{sourceFile}");
            var ast             = new TypeScriptAST(source, sourceFile);
            var typeOverrideMap = new Dictionary <string, string>();

            // When
            var actual = GenerateInteropClassStatement.Generate(
                "ProjectAssembly",
                "Observable",
                ast,
                typeOverrideMap
                );
        }
        public static TypeStatement Identify(
            Node node,
            ClassMetadata classMetadata,
            TypeScriptAST ast,
            TypeOverrideDetails typeOverrideDetails
            )
        {
            var typeIdentifier = TypeIdentifier.Identify(
                node,
                classMetadata
                );
            var isTypeAlias = AliasTypeIdentifier.Identify(
                node,
                ast
                );
            var aliasType = isTypeAlias ? AliasTypeStatementIdentifier.Identify(
                typeIdentifier,
                classMetadata,
                ast,
                typeOverrideDetails
                ) : null;
            var isLiteral = IsTypeLiteralRule.Check(
                node
                ) || TypeLiteralIdentifier.Identify(
                typeIdentifier
                );
            var genericTypes = new List <TypeStatement>();

            if (node is TypeReferenceNode referenceNode &&
                referenceNode.TypeArguments != null &&
                referenceNode.TypeArguments.Any())
            {
                foreach (var typeArgument in referenceNode.TypeArguments)
                {
                    if (typeArgument is Node typeArgumentNode)
                    {
                        genericTypes.Add(
                            Identify(
                                typeArgumentNode,
                                classMetadata,
                                ast,
                                typeOverrideDetails
                                )
                            );
                    }
                }
            }
        public void ValidateGenerateStrings(
            string path,
            string sourceFile,
            string expectedFile,
            string classIdentifier = "ExampleClass"
            )
        {
            // Given
            ReadInteropTemplates.SetReadTemplates();
            GenerateSource.DisableCache();
            var sourcePath = Path.Combine(
                ".",
                "GenerateClassStatementStringTests",
                path
                );
            string expected = File.ReadAllText(Path.Combine(
                                                   sourcePath,
                                                   expectedFile
                                                   ));
            var source = File.ReadAllText(Path.Combine(
                                              sourcePath,
                                              sourceFile
                                              ));
            var ast = new TypeScriptAST(
                source,
                sourceFile
                );
            var typeOverrideMap = new Dictionary <string, string>();

            // When
            var generated = GenerateInteropClassStatement.Generate(
                "ProjectAssembly",
                classIdentifier,
                ast,
                typeOverrideMap
                );
            var actual = GenerateClassStatementString.Generate(
                generated,
                new NoFormattingTextFormatter()
                );

            // Then
            actual.Should().Be(
                expected
                );
        }
Example #22
0
        public static IList <TypeStatement> Identify(
            Node node,
            TypeScriptAST ast,
            ClassMetadata classMetadata,
            TypeOverrideDetails typeOverrideDetails
            )
        {
            var interfaces = new List <TypeStatement>();
            // Check if Class
            var interfaceCache = ast.RootNode.OfKind(
                SyntaxKind.InterfaceDeclaration
                );

            if (node is ClassDeclaration classDeclaration &&
                classDeclaration.HeritageClauses != null &&
                classDeclaration.HeritageClauses.Any())
            {
                foreach (var heritageClauses in classDeclaration.HeritageClauses)
                {
                    foreach (var heritageClauseType in heritageClauses.Types)
                    {
                        var identifiers = heritageClauseType.OfKind(SyntaxKind.Identifier);
                        foreach (var identifier in identifiers)
                        {
                            var interfaceNode = interfaceCache.FirstOrDefault(
                                a => a.IdentifierStr == identifier.IdentifierStr
                                );
                            if (interfaceNode != null)
                            {
                                interfaces.Add(
                                    GenericTypeIdentifier.Identify(
                                        heritageClauseType,
                                        classMetadata,
                                        ast,
                                        typeOverrideDetails
                                        )
                                    );
                                break;
                            }
                        }
                    }
                }
            }

            return(interfaces);
        }
        public virtual bool Identify(
            string identifierString,
            TypeScriptAST ast
            )
        {
            var hasClassDeclarations = ast.RootNode.OfKind(
                SyntaxKind.ClassDeclaration
                ).Any(
                child => child.IdentifierStr == identifierString
                );
            var response = !hasClassDeclarations && ast.RootNode.OfKind(
                SyntaxKind.InterfaceDeclaration
                ).Any(
                child => child.IdentifierStr == identifierString
                );

            return(response);
        }
Example #24
0
 public override bool Identify(
     string identifierString,
     TypeScriptAST ast
     )
 {
     if (!_isCachedSetup)
     {
         var declarations = ast.RootNode.OfKind(
             SyntaxKind.EnumDeclaration
             );
         foreach (var declaration in declarations)
         {
             _cache.Add(declaration.IdentifierStr);
         }
         _isCachedSetup = true;
     }
     return(_cache.Contains(identifierString));
 }
Example #25
0
        private void Button_Click_7(object sender, RoutedEventArgs e)
        {
            _nodeChangeItems.Clear();
            _currentChangeAst      = null;
            _currentAst            = new TypeScriptAST();
            _currentSource         = tbSource.Text;
            _currentSourceFileName = tbFileName.Text;
            if (string.IsNullOrWhiteSpace(_currentSourceFileName))
            {
                _currentAst.MakeAST(_currentSource);
            }
            else
            {
                _currentAst.MakeAST(_currentSource, _currentSourceFileName);
            }

            tbTreeString.Text   = _currentAst.GetTreeString();
            lbNodes.ItemsSource = _currentAst.GetDescendants();
        }
 private static IList <TypeStatement> GetGenericTypes(
     Node node,
     ClassMetadata classMetadata,
     TypeScriptAST ast,
     TypeOverrideDetails typeOverrideDetails
     )
 {
     if (node is ClassDeclaration classDeclaration &&
         classDeclaration.TypeParameters != null
         )
     {
         return(classDeclaration.TypeParameters.Select(
                    typeParam => GenericTypeIdentifier.Identify(
                        typeParam,
                        classMetadata,
                        ast,
                        typeOverrideDetails
                        )
                    ).ToList());
     }
Example #27
0
        public override bool Identify(
            string identifierString,
            TypeScriptAST ast
            )
        {
            if (_cache.TryGetValue(identifierString, out var value))
            {
                return(value);
            }
            var response = base.Identify(
                identifierString,
                ast
                );

            if (identifierString != null)
            {
                _cache[identifierString] = response;
            }
            return(response);
        }
Example #28
0
        private void Button_Click_8(object sender, RoutedEventArgs e)
        {
            var fileName = "parser.ts";

            if (!File.Exists(fileName))
            {
                var openFileDialog = new OpenFileDialog();
                if (openFileDialog.ShowDialog() == true)
                {
                    fileName = openFileDialog.FileName;
                }
                else
                {
                    return;
                }
            }

            var source = File.ReadAllText(fileName);

            var ast = new TypeScriptAST(source, fileName);

            var change = new ChangeAST();

            foreach (var module in ast.GetDescendants().OfType <ModuleDeclaration>())
            {
                var funcs          = module.Body.Children.OfType <FunctionDeclaration>().ToList();
                var enums          = module.Body.Children.OfType <EnumDeclaration>();
                var moduleInfoFunc = $@"
    export function getModuleInfo() {{
        return ""Module {module.IdentifierStr} contains {funcs.Count()} functions ({
                        funcs.Count(v => v.IdentifierStr.StartsWith("parse"))
                    } starts with parse), {enums.Count()} enums ..."";
    }}
";
                change.InsertBefore(module.Body.Children.First(), moduleInfoFunc);
            }
            var newSource = change.GetChangedSource(ast.SourceStr);

            tbExample2Res.Text = newSource;
            File.WriteAllText("parser2.ts", newSource);
        }
        public void ShouldReturnExpectedIdentificationOfInterfaceWhenUsingCachedInstance()
        {
            // Given
            var expected = true;
            var interfaceIdentifierString = "ExampleInterface";

            var sourceFile = "interface.ts";
            var source     = File.ReadAllText($"./SourceFiles/{sourceFile}");
            var ast        = new TypeScriptAST(source, sourceFile);

            // When
            var cachedInterfaceIdentifier = new InterfaceResponseTypeIdentifierCached();
            var actual = cachedInterfaceIdentifier.Identify(
                interfaceIdentifierString,
                ast
                );

            // Then
            actual.Should()
            .Be(expected);
        }
        public void ShouldReturnExpectedIdentificationOfEnumWhenUsingCachedInstance()
        {
            // Given
            var expected             = true;
            var enumIdentifierString = "EnumExport";

            var sourceFile = "enum.ts";
            var source     = File.ReadAllText($"./SourceFiles/{sourceFile}");
            var ast        = new TypeScriptAST(source, sourceFile);

            // When
            var notCachedEnumIdentifier = new EnumTypeIdentifierCached();
            var actual = notCachedEnumIdentifier.Identify(
                enumIdentifierString,
                ast
                );

            // Then
            actual.Should()
            .Be(expected);
        }