public override SyntaxNode VisitModuleBlock(ModuleBlockSyntax node) { var initInvocations = new SyntaxList <StatementSyntax>(); var space = SyntaxFactory.SyntaxTrivia(SyntaxKind.WhitespaceTrivia, " "); var newline = SyntaxFactory.SyntaxTrivia(SyntaxKind.EndOfLineTrivia, Environment.NewLine); for (int a = 0; a < node.Members.Count; a++) { if (node.Members[a].IsKind(SyntaxKind.FieldDeclaration)) { foreach (var d in (node.Members[a] as FieldDeclarationSyntax).Declarators) { if (StructuresWithInitializer.Contains( d?.AsClause?.Type().WithoutTrivia().ToFullString())) { foreach (var name in d.Names) { if (name.ArrayBounds != null) { initInvocations = initInvocations.Add(CreateInitializer(name.Identifier.ToFullString().Trim(), d.AsClause.Type().WithoutTrivia().ToString(), true).WithTrailingTrivia(newline)); } else { initInvocations = initInvocations.Add(CreateInitializer(name.Identifier.ToFullString().Trim(), d.AsClause.Type().WithoutTrivia().ToString(), false).WithTrailingTrivia(newline)); } } } } } } if (initInvocations.Count > 0) { var subStart = SyntaxFactory.SubStatement(SyntaxFactory.Identifier("New()").WithLeadingTrivia(space)).WithLeadingTrivia(newline).WithTrailingTrivia(newline); var subEnd = SyntaxFactory.EndSubStatement( SyntaxFactory.Token(SyntaxKind.EndKeyword, "End ").WithLeadingTrivia(newline), SyntaxFactory.Token(SyntaxKind.SubKeyword, "Sub")).WithTrailingTrivia(newline); var moduleConstructor = SyntaxFactory.SubBlock(subStart, initInvocations, subEnd); node = node.WithMembers(node.Members.Add(moduleConstructor)); } return(base.VisitModuleBlock(node)); }
public override SyntaxNode VisitModuleBlock(ModuleBlockSyntax node) { var initInvocations = new SyntaxList <StatementSyntax>(); var space = SyntaxFactory.SyntaxTrivia(SyntaxKind.WhitespaceTrivia, " "); var newline = SyntaxFactory.SyntaxTrivia(SyntaxKind.EndOfLineTrivia, Environment.NewLine); for (int a = 0; a < node.Members.Count; a++) { if (node.Members[a].IsKind(SyntaxKind.FieldDeclaration)) { foreach (var declarators in (node.Members[a] as FieldDeclarationSyntax).Declarators) { // Checks for variable declaration string item = declarators?.AsClause?.Type().WithoutTrivia().ToString(); if (structuresWithInit.Contains(item)) { foreach (var name in declarators.Names) { if (name.ArrayBounds != null) { initInvocations.Add( CreateInitializer( name.Identifier.ToFullString().Trim(), item, true) .WithTrailingTrivia(newline)); } else { initInvocations.Add( CreateInitializer( name.Identifier.ToFullString().Trim(), item, false) .WithTrailingTrivia(newline)); } } } // end check of structures } } // end check of field declarations } if (initInvocations.Count > 0) { // Sub New() Statement var subStart = SyntaxFactory.SubStatement( SyntaxFactory.Identifier("New()").WithLeadingTrivia(space)) .WithLeadingTrivia(newline) .WithTrailingTrivia(newline); // End Sub statement var subEnd = SyntaxFactory.EndSubStatement( SyntaxFactory.Token(SyntaxKind.EndIfKeyword, "End ").WithLeadingTrivia(newline), SyntaxFactory.Token(SyntaxKind.SubKeyword, "Sub")) .WithTrailingTrivia(newline); // Initializer for the module part of the transpiled file var moduleConstructor = SyntaxFactory.SubBlock(subStart, initInvocations, subEnd); // WithMembers => adds the specified members to the node declaration // it makes sense because the node is the root (I think) node = node.WithMembers(node.Members.Add(moduleConstructor)); } // Since we want to continue the visit, we call the base method return(base.VisitModuleBlock(node)); }