public CSharpSyntaxNode Convert(RegularExpressionLiteral node)
        {
            ExpressionSyntax patternExpr         = SyntaxFactory.LiteralExpression(SyntaxKind.StringLiteralExpression, SyntaxFactory.Literal(node.Pattern));
            ObjectCreationExpressionSyntax csObj = SyntaxFactory.ObjectCreationExpression(
                SyntaxFactory.IdentifierName("RegExp"))
                                                   .AddArgumentListArguments(SyntaxFactory.Argument(patternExpr));

            if (!string.IsNullOrEmpty(node.SearchFlags))
            {
                ExpressionSyntax flagsExpr = SyntaxFactory.LiteralExpression(SyntaxKind.StringLiteralExpression, SyntaxFactory.Literal(node.SearchFlags));
                csObj = csObj.AddArgumentListArguments(SyntaxFactory.Argument(flagsExpr));
            }
            return(csObj);
        }
        private static bool GetCreationExpression(GeneratorExecutionContext context, ClassDefinition classDefinition,
                                                  TypeSyntax objectType, ICollection <PropertyDefinition> propertiesSetViaConstructor,
                                                  ICollection <StatementSyntax> buildMethodStatements,
                                                  out ObjectCreationExpressionSyntax creationExpression)
        {
            creationExpression = ObjectCreationExpression(objectType);
            if (classDefinition.ConstructorToUse is { } constructorToUse)
            {
                var arguments = new ArgumentSyntax[constructorToUse.Parameters.Length];
                var blocked   = false;
                for (var i = 0; i < constructorToUse.Parameters.Length; i++)
                {
                    var parameterName    = constructorToUse.Parameters[i].Name;
                    var matchingProperty = classDefinition.Properties.FirstOrDefault(p =>
                                                                                     p.PropertyName.Equals(parameterName, StringComparison.OrdinalIgnoreCase));
                    if (matchingProperty == null)
                    {
                        context.ReportDiagnostic(Diagnostic.Create(CannotInferBuilderPropertyFromArgumentDiagnostic,
                                                                   constructorToUse.Parameters[i].Locations[0]));
                        blocked = true;
                    }
                    else
                    {
                        arguments[i] = Argument(matchingProperty.PropertyAccessAndDefaultingExpression());
                        propertiesSetViaConstructor.Add(matchingProperty);
                    }

                    foreach (var property in propertiesSetViaConstructor.Where(x => x.IsReferenceType && !x.IsNullable))
                    {
                        var throwStatement = ThrowStatement(
                            ObjectCreationExpression(ParseTypeName("System.InvalidOperationException"))
                            .AddArgumentListArguments(Argument(LiteralExpression(
                                                                   StringLiteralExpression,
                                                                   Literal(
                                                                       $"No value present for required property '{property.PropertyName}'.")))));
                        buildMethodStatements.Add(IfStatement(IdentifierName(property.FieldName).NullCheck(),
                                                              throwStatement));
                    }
                }

                if (blocked)
                {
                    return(false);
                }

                creationExpression = creationExpression.AddArgumentListArguments(arguments);
            }
예제 #3
0
        public void SetUp()
        {
            var workspace = new AdhocWorkspace();
            var language  = LanguageNames.CSharp;

            _syntaxGenerator = SyntaxGenerator.GetGenerator(workspace, language);
            _objectCreationExpressionActions = new ObjectCreationExpressionActions();
            _node = _syntaxGenerator.ObjectCreationExpression(SyntaxFactory.ParseTypeName("StringBuilder"))
                    .NormalizeWhitespace() as ObjectCreationExpressionSyntax;

            _node = _node.AddArgumentListArguments(SyntaxFactory.Argument(
                                                       SyntaxFactory.LiteralExpression(
                                                           SyntaxKind.StringLiteralExpression,
                                                           SyntaxFactory.Literal(
                                                               SyntaxFactory.TriviaList(),
                                                               "\"SomeText\"",
                                                               "\"SomeText\"",
                                                               SyntaxFactory.TriviaList()))));
        }
예제 #4
0
 public static ObjectCreationExpressionSyntax AddArgumentListArguments(this ObjectCreationExpressionSyntax syntax, IEnumerable <ArgumentSyntax> arguments)
 {
     return(syntax.AddArgumentListArguments(arguments.ToArray()));
 }
예제 #5
0
 public static ObjectCreationExpressionSyntax WithArguments(this ObjectCreationExpressionSyntax expr, IEnumerable <ExpressionSyntax> args)
 {
     return(expr.AddArgumentListArguments(args.Select(Argument)));
 }
예제 #6
0
 public static ObjectCreationExpressionSyntax WithArguments(this ObjectCreationExpressionSyntax expr, params ExpressionSyntax[] args)
 {
     return(expr.AddArgumentListArguments(args.Select(Argument)));
 }