Beispiel #1
0
 public static ConstructorDeclarationSyntax AddParameters(
     this ConstructorDeclarationSyntax declaration,
     IEnumerable <ParameterDescription> parametersDescription)
 {
     return(declaration.AddParameterListParameters(
                CreateParameters(parametersDescription)));
 }
Beispiel #2
0
        private static SyntaxNode AddParameterToConstructor(SyntaxNode root, FieldDeclarationSyntax fieldDecl, ConstructorDeclarationSyntax ctorDecl)
        {
            var varDecl       = fieldDecl.Declaration.Variables.FirstOrDefault();
            var fieldName     = varDecl.Identifier.Text;
            var parameterName = fieldName.If(f => f.StartsWith("_"), f => f.Substring("_".Length));

            var newCtor = ctorDecl
                          .AddParameterListParameters(
                SyntaxFactory.Parameter(
                    SyntaxFactory.List <AttributeListSyntax>(),
                    SyntaxFactory.TokenList(),
                    fieldDecl.Declaration.Type,
                    SyntaxFactory.Identifier(parameterName),
                    null))
                          .WithBody(ctorDecl.Body.AddStatements(
                                        SyntaxFactory.ExpressionStatement(
                                            SyntaxFactory.AssignmentExpression(
                                                SyntaxKind.SimpleAssignmentExpression,
                                                fieldName == parameterName
                                                                ? SyntaxFactory.IdentifierName(fieldName).OfThis()
                                                                : (ExpressionSyntax)SyntaxFactory.IdentifierName(fieldName),
                                                SyntaxFactory.IdentifierName(parameterName)))))
                          .WithAdditionalAnnotations(Formatter.Annotation, Simplifier.Annotation);

            root = root.ReplaceNode(ctorDecl, newCtor);

            return(root);
        }
Beispiel #3
0
        public CSharpSyntaxNode Convert(Constructor node)
        {
            if (!(node.Parent is ClassDeclaration tsClassNode))
            {
                return(null);
            }

            ConstructorDeclarationSyntax csCtor = SyntaxFactory.ConstructorDeclaration(tsClassNode.Name.Text);

            csCtor = csCtor.AddModifiers(node.Modifiers.ToCsNodes <SyntaxToken>());
            csCtor = csCtor.AddParameterListParameters(node.Parameters.ToCsNodes <ParameterSyntax>());

            CallExpression baseNode =
                node.Base is ExpressionStatement
                ? ((ExpressionStatement)node.Base).Expression as CallExpression
                : node.Base as CallExpression;

            if (baseNode != null)
            {
                ArgumentSyntax[] csArgs = this.ToArgumentList(baseNode.Arguments);
                csCtor = csCtor.WithInitializer(SyntaxFactory.ConstructorInitializer(SyntaxKind.BaseConstructorInitializer).AddArgumentListArguments(csArgs));
            }

            if (node.JsDoc.Count > 0)
            {
                csCtor = csCtor.WithLeadingTrivia(SyntaxFactory.Trivia(node.JsDoc[0].ToCsNode <DocumentationCommentTriviaSyntax>()));
            }

            return(csCtor.WithBody(node.Body.ToCsNode <BlockSyntax>()));
        }
Beispiel #4
0
        public override SyntaxNode?VisitConstructorDeclaration(ConstructorDeclarationSyntax node)
        {
            if (node.ParameterList.Parameters.All(x => x.Identifier.Text != "logger"))
            {
                if (!HasLoggerUsage(node.Parent))
                {
                    var className = node.Identifier.ValueText;
                    _logger.LogWarning("skipping class '{className}' for adding new logger constructor parameter", className);
                    return(base.VisitConstructorDeclaration(node));
                }

                _logger.LogInformation("adding new logger constructor parameter");

                var loggerParameter = Parameter(Identifier(TriviaList(Space), "logger", TriviaList()))
                                      .WithType(
                    GenericName(Identifier(TriviaList(Space), "ILogger", TriviaList()))
                    .WithTypeArgumentList(
                        TypeArgumentList(
                            SingletonSeparatedList <TypeSyntax>(IdentifierName(node.Identifier.Text))
                            )
                        )
                    );
                node = node.AddParameterListParameters(loggerParameter);

                if (node.Body == null)
                {
                    node = node.WithBody(Block());
                }

                _logger.LogInformation("adding new logger assign statement");
                var loggerAssignExpression = ExpressionStatement(
                    AssignmentExpression(
                        SyntaxKind.SimpleAssignmentExpression,
                        IdentifierName("_logger"),
                        IdentifierName("logger")
                        )
                    )
                                             .NormalizeWhitespace()
                                             .WithLeadingTrivia(node.Body.Statements.FirstOrDefault()?.GetLeadingTrivia() ?? TriviaList(Whitespace("            ")))
                                             .WithSemicolonToken(
                    Token(
                        TriviaList(),
                        SyntaxKind.SemicolonToken,
                        TriviaList(
                            LineFeed
                            )
                        )
                    );

                node = node.WithBody(node.Body.AddStatements(loggerAssignExpression));
            }

            return(base.VisitConstructorDeclaration(node));
        }
Beispiel #5
0
        private static ConstructorDeclarationSyntax UpdateConstructor(ConstructorDeclarationSyntax constructor, ParameterSyntax parameter, ExpressionStatementSyntax assignment)
        {
            var constructorWithParams = constructor.AddParameterListParameters(parameter);

            var body = constructorWithParams.Body ?? Block();

            var newBody = body.Statements.Insert(0, assignment);

            return(constructorWithParams
                   .WithBody(body.WithStatements(newBody))
                   .WithLeadingTrivia(constructor.GetLeadingTrivia())
                   .WithTrailingTrivia(constructor.GetTrailingTrivia()));
        }
        public CompilationUnitSyntax ComputeRoot(CompilationUnitSyntax root)
        {
            var originalName = _parameter.Name;

            var parameterName = originalName.ToParameterName();
            var parameter     = parameterName.ToParameter(_parameter.ParameterType);

            var assignment = originalName
                             .ToIdentifierName(qualifyWithThis: originalName == parameterName)
                             .AssignWith(parameterName.ToIdentifierName());

            var newCtor = _ctor
                          .AddParameterListParameters(parameter)
                          .WithBody(_ctor.Body.AddStatements(assignment));

            return(root.ReplaceNode(_ctor, newCtor.Nicefy()));
        }
Beispiel #7
0
 public static ConstructorDeclarationSyntax AddParameterListParameters(this ConstructorDeclarationSyntax syntax, IEnumerable <ParameterSyntax> parameters)
 {
     return(syntax.AddParameterListParameters(parameters.ToArray()));
 }