Fix multi-line SyntaxBody

This commit is contained in:
2024-04-10 18:34:18 +02:00
parent 575bca3278
commit 91b688e2be
5 changed files with 15 additions and 6 deletions
+4 -2
View File
@@ -20,6 +20,7 @@ namespace TestNamespace
TestClass(string fieldString) : base(fieldString)
{
this.fieldString = fieldString ?? throw new ArgumentNullException();
this.PropertyString = string.Empty;
}
[SomeAttribute2]
@@ -30,6 +31,7 @@ namespace TestNamespace
[SomeAttribute4(0.5F)]
public string GetFieldString()
{
this.fieldString = 0;
return this.fieldString;
}
}
@@ -61,7 +63,7 @@ namespace TestNamespace
SyntaxBuilder.CreateAttribute("SomeAttribute5")
.WithNullArgument())
.WithParameter("string", "fieldString")
.WithBody("this.fieldString = fieldString ?? throw new ArgumentNullException();"))
.WithBody("this.fieldString = fieldString ?? throw new ArgumentNullException();\rthis.PropertyString = string.Empty;"))
.WithField(
SyntaxBuilder.CreateField("string", "fieldString")
.WithAttribute(SyntaxBuilder.CreateAttribute("SomeAttribute2"))
@@ -81,7 +83,7 @@ namespace TestNamespace
.WithModifier("public")
.WithAttribute(SyntaxBuilder.CreateAttribute("SomeAttribute4")
.WithArgument(0.5f))
.WithBody("return this.fieldString;")))
.WithBody("this.fieldString = 0;\r\nreturn this.fieldString;")))
.Build();
var namespaceString = namespaceSyntax.ToFullString();
+2 -1
View File
@@ -2,6 +2,7 @@
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using System;
using System.Linq;
namespace Sybil
{
@@ -40,7 +41,7 @@ namespace Sybil
_ = string.IsNullOrWhiteSpace(body) ? throw new ArgumentNullException(nameof(body)) : body;
this.ArrowExpressionClauseSyntax = null;
this.BlockSyntax = SyntaxFactory.Block(SyntaxFactory.ParseStatement(body));
this.BlockSyntax = SyntaxFactory.Block(body.Split('\n', '\r').Where(s => !string.IsNullOrWhiteSpace(s)).Select(s => SyntaxFactory.ParseStatement(s)));
return this;
}
+2 -1
View File
@@ -77,7 +77,8 @@ namespace Sybil
{
_ = string.IsNullOrWhiteSpace(body) ? throw new ArgumentNullException(nameof(body)) : body;
this.ConstructorDeclarationSyntax = this.ConstructorDeclarationSyntax.WithBody(SyntaxFactory.Block(SyntaxFactory.ParseStatement(body)));
this.ConstructorDeclarationSyntax = this.ConstructorDeclarationSyntax.WithBody(
SyntaxFactory.Block(body.Split('\n', '\r').Where(s => !string.IsNullOrWhiteSpace(s)).Select(s => SyntaxFactory.ParseStatement(s))));
return this;
}
+6 -1
View File
@@ -87,8 +87,13 @@ namespace Sybil
public MethodBuilder WithBody(string body)
{
if (string.IsNullOrWhiteSpace(body))
{
throw new ArgumentNullException(nameof(body));
}
this.BlockBody = SyntaxFactory.Block(
SyntaxFactory.ParseStatement(body));
body.Split('\n', '\r').Where(s => !string.IsNullOrWhiteSpace(s)).Select(s => SyntaxFactory.ParseStatement(s)));
this.ArrowExpression = null;
return this;
+1 -1
View File
@@ -2,7 +2,7 @@
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<Version>0.4.0</Version>
<Version>0.4.1</Version>
</PropertyGroup>
<PropertyGroup>