public void ParserTest() { var parser = new TypeScriptParser(); var types = new List <TypeScriptParser.TypeInfo>(); StringBuilder sb = new StringBuilder(); parser.ReportToken += (token) => { if (token.Type != TokenType.WhiteSpace && token.Type != TokenType.EndOfLine) { sb.AppendLine(JSON.StringifyIndented(token)); } ; }; parser.ReportType += (type) => { types.Add(type); }; parser.Parse(File.ReadAllText(@"P:\Sandbox\Serene\Serenity\Serenity.Script.Core\Resources\Serenity.CoreLib.ts")); throw new Exception(JSON.StringifyIndented(types) + sb.ToString()); }
public void SetsIsDeclarationCorrectly() { var parser = new TypeScriptParser(); var types = new List <TypeScriptParser.TypeInfo>(); parser.ReportType += (type) => { types.Add(type); }; parser.Parse(Input_Declaration); Assert.Equal(6, types.Count); var t0 = types[0]; Assert.Equal("A", t0.Name); Assert.False(t0.IsDeclaration); Assert.Equal("Normal", t0.Namespace.Name); Assert.False(t0.Namespace.IsDeclaration); var t1 = types[1]; Assert.Equal("B", t1.Name); Assert.False(t1.IsDeclaration); Assert.Equal("Normal.Sub", t1.Namespace.Name); Assert.False(t1.Namespace.IsDeclaration); var t2 = types[2]; Assert.Equal("C", t2.Name); Assert.True(t2.IsDeclaration); Assert.Equal("Declared", t2.Namespace.Name); Assert.True(t2.Namespace.IsDeclaration); var t3 = types[3]; Assert.Equal("S", t3.Name); Assert.True(t3.IsDeclaration); Assert.Equal("Declared.DSub", t3.Namespace.Name); Assert.True(t3.Namespace.IsDeclaration); var t4 = types[4]; Assert.Equal("D", t4.Name); Assert.False(t4.IsDeclaration); Assert.Equal("", t4.Namespace.Name); Assert.False(t4.Namespace.IsDeclaration); var t5 = types[5]; Assert.Equal("E", t5.Name); Assert.True(t5.IsDeclaration); Assert.Equal("", t5.Namespace.Name); Assert.False(t5.Namespace.IsDeclaration); }
public void ParserDoesntFailOnEmptyFile() { var parser = new TypeScriptParser(); Token reportedToken = null; parser.ReportToken += (token) => { reportedToken = token; }; parser.Parse(""); Assert.Equal(TokenType.End, reportedToken.Type); }
public void Parse(string text, string fileName = @"d:\projects\MyProject\test.ts") { ProjectContent = MockRepository.GenerateStub <IProjectContent>(); var textBuffer = new StringTextBuffer(text); var scriptLoader = new ParseTestScriptLoader(); var logger = new LanguageServiceLogger(); ITypeScriptContextFactory contextFactory = MockRepository.GenerateStub <ITypeScriptContextFactory>(); contextFactory .Stub(f => f.CreateContext()) .Return(new TypeScriptContext(scriptLoader, logger)); var parser = new TypeScriptParser(contextFactory); CompilationUnit = parser.Parse(ProjectContent, fileName, textBuffer); }
public void Parse(string text, string fileName = @"d:\projects\MyProject\test.ts") { Project = MockRepository.GenerateStub <IProject>(); var fileContent = new TextDocument(text); var scriptLoader = new ParseTestScriptLoader(); var logger = new LanguageServiceLogger(); ITypeScriptContextFactory contextFactory = MockRepository.GenerateStub <ITypeScriptContextFactory>(); contextFactory .Stub(f => f.CreateContext()) .Return(new TypeScriptContext(javaScriptContext, scriptLoader, logger)); var parser = new TypeScriptParser(contextFactory); ParseInfo = parser.Parse(new FileName(fileName), fileContent, null, new TypeScriptFile[0]); }
public void CanParseGenericClassWithExtendsConstraint() { var parser = new TypeScriptParser(); var types = new List<TypeScriptParser.TypeInfo>(); parser.ReportType += (type) => { types.Add(type); }; parser.Parse(@" interface ServiceOptions<TResponse extends ServiceResponse> extends JQueryAjaxSettings { request?: any; }"); Assert.Equal(1, types.Count); Assert.Equal("ServiceOptions<TResponse extends ServiceResponse>", types[0].Name); }
public void CanParseGenericClassWithExtendsConstraint() { var parser = new TypeScriptParser(); var types = new List <TypeScriptParser.TypeInfo>(); parser.ReportType += (type) => { types.Add(type); }; parser.Parse(@" interface ServiceOptions<TResponse extends ServiceResponse> extends JQueryAjaxSettings { request?: any; }"); Assert.Equal(1, types.Count); Assert.Equal("ServiceOptions<TResponse extends ServiceResponse>", types[0].Name); }
public void CanParseInterfaceDialogFormatter() { var parser = new TypeScriptParser(); var types = new List<TypeScriptParser.TypeInfo>(); parser.ReportType += (type) => { types.Add(type); }; parser.Parse(Input_InterfaceDialogFormatter); Assert.Equal(3, types.Count); var t0 = types[0]; Assert.True(t0.IsInterface); Assert.Equal("CustomerRow", t0.Name); Assert.Equal("", t0.Namespace.Name); Assert.Equal("", t0.Extends); Assert.Equal("", t0.Implements); }
public void CanParseInterfaceDialogFormatter() { var parser = new TypeScriptParser(); var types = new List <TypeScriptParser.TypeInfo>(); parser.ReportType += (type) => { types.Add(type); }; parser.Parse(Input_InterfaceDialogFormatter); Assert.Equal(3, types.Count); var t0 = types[0]; Assert.True(t0.IsInterface); Assert.Equal("CustomerRow", t0.Name); Assert.Equal("", t0.Namespace.Name); Assert.Equal("", t0.Extends); Assert.Equal("", t0.Implements); }
public void CanParseGenericClass() { var parser = new TypeScriptParser(); var types = new List<TypeScriptParser.TypeInfo>(); parser.ReportType += (type) => { types.Add(type); }; parser.Parse(@" class EntityDialog<TEntity> { dialogOpen(): void; loadByIdAndOpenDialog(id: any): void; }"); Assert.Equal(1, types.Count); var t0 = types[0]; Assert.Equal("EntityDialog<TEntity>", t0.Name); Assert.Equal(2, t0.Members.Count); }
public void CanParseMembers() { var parser = new TypeScriptParser(); var types = new List<TypeScriptParser.TypeInfo>(); parser.ReportType += (type) => { types.Add(type); }; parser.Parse(Input_Lookup); Assert.Equal(1, types.Count); var t0 = types[0]; Assert.Equal("Lookup<TItem>", t0.Name); Assert.False(t0.IsDeclaration); Assert.Equal("Serenity", t0.Namespace.Name); Assert.False(t0.Namespace.IsDeclaration); Assert.Equal(6, t0.Members.Count); }
public void CanParseMembers() { var parser = new TypeScriptParser(); var types = new List <TypeScriptParser.TypeInfo>(); parser.ReportType += (type) => { types.Add(type); }; parser.Parse(Input_Lookup); Assert.Equal(1, types.Count); var t0 = types[0]; Assert.Equal("Lookup<TItem>", t0.Name); Assert.False(t0.IsDeclaration); Assert.Equal("Serenity", t0.Namespace.Name); Assert.False(t0.Namespace.IsDeclaration); Assert.Equal(6, t0.Members.Count); }
public void CanParseGenericClass() { var parser = new TypeScriptParser(); var types = new List <TypeScriptParser.TypeInfo>(); parser.ReportType += (type) => { types.Add(type); }; parser.Parse(@" class EntityDialog<TEntity> { dialogOpen(): void; loadByIdAndOpenDialog(id: any): void; }"); Assert.Equal(1, types.Count); var t0 = types[0]; Assert.Equal("EntityDialog<TEntity>", t0.Name); Assert.Equal(2, t0.Members.Count); }
static void Main(string[] args) { Console.WriteLine("Starting..."); string location = ""; //if (args.Length > 0) // location = args[0]; //else { Console.WriteLine("Enter Typescript file path\r\n>"); location = Console.ReadLine(); } //const string argStart = "--converter:"; //string converterV = args.FirstOrDefault(v => v.StartsWith(argStart)); //if (converterV == null) // Console.WriteLine(@"What software would you like to convert for? // Currently Supported: // Bridge, // DuoCode"); //string converterValue = converterV == null ? Console.ReadLine() : converterV.Substring(argStart.Length); //ConversionSoftware conversionSoftware = (ConversionSoftware)Enum.Parse(typeof(ConversionSoftware), converterValue); string file; Console.WriteLine("Reading file..."); try { file = File.ReadAllText(location); } catch (Exception e) { Console.WriteLine(e.Message); return; } CSharpProcessor context = new CSharpProcessor(); TypeScriptParserSettings settings = new TypeScriptParserSettings() { PublicAll = true, Using = new string[] { //"static Retyped.leaflet" }, RenameType = new Dictionary <string, string>() { //{ "Icon", "Icon<object>" }, //{ "leaflet", "leafletDraw" }, //{ "FeatureGroup", "FeatureGroup<object>" }, ////{ "Circle", "Circle<object>" }, ////{ "CircleMarker", "CircleMarker<object>" }, ////{ "Marker", "Marker<object>" }, ////{ "Polygon", "Polygon<object>" }, ////{ "Polyline", "Polyline<object>" }, ////{ "Rectangle", "Rectangle<object>" }, //{ "LayerGroup", "LayerGroup<object>" }, //{ "L", "leaflet" }, } }; var parser = new TypeScriptParser(file, context, settings); Console.WriteLine("Parsing Typescript..."); var @object = parser.Parse(Path.GetFileNameWithoutExtension(location)); @object.PreConvert(); Console.WriteLine("Building References..."); context.BuildReference(); //Console.WriteLine("Implemeting Interfaces..."); //context.ImplementInterfaces(); //Console.WriteLine("Creating POCO Classes..."); //context.CreatePOCOClasses(); Console.WriteLine("Resolving name conflicts..."); if (settings.ResolveConflictingNames) { context.ResolveNameConfilict(); } Console.WriteLine("Transpiling..."); string cSharp = context.Transpile($@" using System; using TSCS; using Date = System.DateTime; {string.Join("\r\n", settings.Using.Select(u=> $"using {u};"))} #pragma warning disable CS0626 #pragma warning disable CS0824 #pragma warning disable CS0108 #pragma warning disable IDE1006 ", null, false); Console.WriteLine("Writing..."); File.WriteAllText($"{Path.GetFileNameWithoutExtension(location)}.cs", cSharp); Console.WriteLine("Exiting..."); }
public void ParserTest() { var parser = new TypeScriptParser(); var types = new List<TypeScriptParser.TypeInfo>(); StringBuilder sb = new StringBuilder(); parser.ReportToken += (token) => { if (token.Type != TokenType.WhiteSpace && token.Type != TokenType.EndOfLine) sb.AppendLine(JSON.StringifyIndented(token)); ; }; parser.ReportType += (type) => { types.Add(type); }; parser.Parse(File.ReadAllText(@"P:\Sandbox\Serene\Serenity\Serenity.Script.Core\Resources\Serenity.CoreLib.ts")); throw new Exception(JSON.StringifyIndented(types) + sb.ToString()); }
public void SetsIsDeclarationCorrectly() { var parser = new TypeScriptParser(); var types = new List<TypeScriptParser.TypeInfo>(); parser.ReportType += (type) => { types.Add(type); }; parser.Parse(Input_Declaration); Assert.Equal(6, types.Count); var t0 = types[0]; Assert.Equal("A", t0.Name); Assert.False(t0.IsDeclaration); Assert.Equal("Normal", t0.Namespace.Name); Assert.False(t0.Namespace.IsDeclaration); var t1 = types[1]; Assert.Equal("B", t1.Name); Assert.False(t1.IsDeclaration); Assert.Equal("Normal.Sub", t1.Namespace.Name); Assert.False(t1.Namespace.IsDeclaration); var t2 = types[2]; Assert.Equal("C", t2.Name); Assert.True(t2.IsDeclaration); Assert.Equal("Declared", t2.Namespace.Name); Assert.True(t2.Namespace.IsDeclaration); var t3 = types[3]; Assert.Equal("S", t3.Name); Assert.True(t3.IsDeclaration); Assert.Equal("Declared.DSub", t3.Namespace.Name); Assert.True(t3.Namespace.IsDeclaration); var t4 = types[4]; Assert.Equal("D", t4.Name); Assert.False(t4.IsDeclaration); Assert.Equal("", t4.Namespace.Name); Assert.False(t4.Namespace.IsDeclaration); var t5 = types[5]; Assert.Equal("E", t5.Name); Assert.True(t5.IsDeclaration); Assert.Equal("", t5.Namespace.Name); Assert.False(t5.Namespace.IsDeclaration); }