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); }
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())))); }
public static ObjectCreationExpressionSyntax AddArgumentListArguments(this ObjectCreationExpressionSyntax syntax, IEnumerable <ArgumentSyntax> arguments) { return(syntax.AddArgumentListArguments(arguments.ToArray())); }
public static ObjectCreationExpressionSyntax WithArguments(this ObjectCreationExpressionSyntax expr, IEnumerable <ExpressionSyntax> args) { return(expr.AddArgumentListArguments(args.Select(Argument))); }
public static ObjectCreationExpressionSyntax WithArguments(this ObjectCreationExpressionSyntax expr, params ExpressionSyntax[] args) { return(expr.AddArgumentListArguments(args.Select(Argument))); }