private static TypeDeclarationSyntax ParseModel(ClassDeclarationSyntax @class, ParameterListSyntax parameters, Scope scope) { var init = new List <ParameterSyntax>(); var props = new List <PropertyDeclarationSyntax>(); foreach (var member in @class.Members) { var field = ParseField(member); if (field == null) { continue; //error has already been registered } var type = field.Declaration.Type; var variable = field.Declaration .Variables .Single(); init.Add(CSharp.Parameter(variable.Identifier) .WithType(type) .WithDefault(CSharp.EqualsValueClause( variable.Initializer != null ? variable.Initializer.Value : CSharp.DefaultExpression(type)))); props.Add(ModelProperty.Get <PropertyDeclarationSyntax>(type) .WithIdentifier(variable.Identifier)); } if (!RoslynCompiler.HasVisibilityModifier(@class.Modifiers)) { @class = @class.AddModifiers(CSharp.Token(SyntaxKind.PublicKeyword)); } return(@class .WithMembers(CSharp.List <MemberDeclarationSyntax>( props.Union(new[] { GenerateConstructor(@class, init) })))); }
private static Func <SyntaxNode, Scope, SyntaxNode> FunctionInjection(SyntaxNode toReplace, SyntaxList <StatementSyntax>?toInject) { return((node, scope) => { var scopeParameter = node .Ancestors() .OfType <MethodDeclarationSyntax>() .FirstOrDefault() ?.ParameterList .Parameters .LastOrDefault(); if (scopeParameter == null || scopeParameter.Type.ToString() != "__Scope") { //td: error return node; } var block = node as BlockSyntax; Debug.Assert(block != null); //td: error var variables = new List <StatementSyntax>(); foreach (var injectionStatement in toInject.Value) { var injectionDeclaration = (injectionStatement as LocalDeclarationStatementSyntax); if (injectionDeclaration == null) { //td: error continue; } if (injectionDeclaration.Declaration.Variables.Count != 1) { //td: error continue; } var injectionVariable = injectionDeclaration .Declaration .Variables .Single(); var type = injectionDeclaration.Declaration.Type; if (type.ToString() == "var") { //td: error continue; } var name = injectionVariable.Identifier; variables.Add(injectionDeclaration .WithDeclaration(injectionDeclaration.Declaration .WithVariables(CSharp.SeparatedList(new[] { injectionVariable.WithInitializer(CSharp.EqualsValueClause( Templates.ScopeGet.Get <ExpressionSyntax>( type, RoslynCompiler.Quoted(name.ToString())))) })))); } return block.WithStatements(CSharp.List( FunctionInjectionStatements(block, toReplace, variables))); }); }