Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Write comment statement #6141

Merged
merged 2 commits into from
Feb 25, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1313,7 +1313,7 @@ private ConstructorProvider BuildEmptyConstructor()
var accessibility = _isStruct ? MethodSignatureModifiers.Public : MethodSignatureModifiers.Internal;
return new ConstructorProvider(
signature: new ConstructorSignature(Type, $"Initializes a new instance of {Type:C} for deserialization.", accessibility, Array.Empty<ParameterProvider>()),
bodyStatements: new MethodBodyStatement(),
bodyStatements: MethodBodyStatement.Empty,
this);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace Microsoft.TypeSpec.Generator.Perf
public class MethodProviderBenchmark
{
private MethodSignature Signature { get; }
private MethodBodyStatement BodyStatement = new();
private MethodBodyStatement BodyStatement = MethodBodyStatement.Empty;
private Dictionary<ParameterValidationType, List<ParameterProvider>>? ParamHash;

public MethodProviderBenchmark()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
namespace Microsoft.TypeSpec.Generator.Statements
{
[DebuggerDisplay("{GetDebuggerDisplay(),nq}")]
public class MethodBodyStatement
public abstract class MethodBodyStatement
{
internal virtual void Write(CodeWriter writer) { }
internal abstract void Write(CodeWriter writer);
public static implicit operator MethodBodyStatement(MethodBodyStatement[] statements) => new MethodBodyStatements(statements);
public static implicit operator MethodBodyStatement(List<MethodBodyStatement> statements) => new MethodBodyStatements(statements);

Expand All @@ -21,7 +21,15 @@ internal override void Write(CodeWriter writer)
}
}

public static readonly MethodBodyStatement Empty = new();
private class PrivateEmptyStatement : MethodBodyStatement
{
internal override void Write(CodeWriter writer)
{
// Do nothing
}
}

public static readonly MethodBodyStatement Empty = new PrivateEmptyStatement();
public static readonly MethodBodyStatement EmptyLine = new PrivateEmptyLineStatement();

public string ToDisplayString() => GetDebuggerDisplay();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,10 @@ public SingleLineCommentStatement(FormattableString message)

public SingleLineCommentStatement(string message) : this(FormattableStringHelpers.FromString(message))
{ }

internal override void Write(CodeWriter writer)
{
writer.WriteLine($"// {Message}");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public void ForStatementWithAddMethod()
var condition = True;
var increment = ValueExpression.Empty;
var forStatement = new ForStatement(assignment, condition, increment);
var statementToAdd = new MethodBodyStatement();
var statementToAdd = MethodBodyStatement.Empty;

forStatement.Add(statementToAdd);

Expand Down Expand Up @@ -73,7 +73,7 @@ public void CreateForeachStatement()
public void ForeachStatementWithAddMethod()
{
var foreachStatement = new ForeachStatement(new CSharpType(typeof(int)), "item", ValueExpression.Empty, isAsync: false, out var itemReference);
var statementToAdd = new MethodBodyStatement();
var statementToAdd = MethodBodyStatement.Empty;

foreachStatement.Add(statementToAdd);

Expand All @@ -98,7 +98,7 @@ public void IfStatementWithBoolExpression()
public void IfStatementWithAddMethod()
{
var ifStatement = new IfStatement(True);
var statementToAdd = new MethodBodyStatement();
var statementToAdd = MethodBodyStatement.Empty;

ifStatement.Add(statementToAdd);

Expand Down Expand Up @@ -139,7 +139,7 @@ public void IfStatementAddBracesOptionFalse()
public void IfElseStatementWithIfAndElse()
{
var condition = True;
var elseStatement = new MethodBodyStatement();
var elseStatement = MethodBodyStatement.Empty;

var ifElseStatement = new IfElseStatement(new IfStatement(condition), elseStatement);

Expand All @@ -153,8 +153,8 @@ public void IfElseStatementWithIfAndElse()
public void IfElseStatementWithConditionAndStatements()
{
var condition = True;
var ifStatement = new MethodBodyStatement();
var elseStatement = new MethodBodyStatement();
var ifStatement = MethodBodyStatement.Empty;
var elseStatement = MethodBodyStatement.Empty;

var ifElseStatement = new IfElseStatement(condition, ifStatement, elseStatement);

Expand All @@ -170,7 +170,7 @@ public void SwitchStatementWithSingleCase()
var matchExpression = ValueExpression.Empty;
var switchStatement = new SwitchStatement(matchExpression);

var caseStatement = new MethodBodyStatement();
var caseStatement = MethodBodyStatement.Empty;
var switchCase = new SwitchCaseStatement(ValueExpression.Empty, caseStatement);

switchStatement.Add(switchCase);
Expand All @@ -187,8 +187,8 @@ public void SwitchStatementWithMultipleCases()

var caseStatements = new List<SwitchCaseStatement>
{
new SwitchCaseStatement(ValueExpression.Empty, new MethodBodyStatement()),
new SwitchCaseStatement(ValueExpression.Empty, new MethodBodyStatement())
new SwitchCaseStatement(ValueExpression.Empty, MethodBodyStatement.Empty),
new SwitchCaseStatement(ValueExpression.Empty, MethodBodyStatement.Empty)
};

foreach (var switchCase in caseStatements)
Expand All @@ -207,8 +207,8 @@ public void SwitchStatementEnumeratingCases()

var caseStatements = new List<SwitchCaseStatement>
{
new SwitchCaseStatement(ValueExpression.Empty, new MethodBodyStatement()),
new SwitchCaseStatement(ValueExpression.Empty, new MethodBodyStatement())
new SwitchCaseStatement(ValueExpression.Empty, MethodBodyStatement.Empty),
new SwitchCaseStatement(ValueExpression.Empty, MethodBodyStatement.Empty)
};

foreach (var switchCase in caseStatements)
Expand Down Expand Up @@ -271,6 +271,7 @@ public void TestSwitchStatementWithUsingStatementWrite()
var switchStatement = new SwitchStatement(variableFoo);
var usingStatement = new UsingScopeStatement(null, new CodeWriterDeclaration("x"), New.Instance(typeof(MemoryStream)))
{
new SingleLineCommentStatement("some comment explaining the return"),
Return(variableFoo)
};

Expand Down Expand Up @@ -546,5 +547,12 @@ public void TestFlatten_CorrectNestedOrder()
};
Assert.AreEqual(expectedOrder, result);
}

[Test]
public void SingleLineCommentStatement()
{
var comment = new SingleLineCommentStatement("This is a comment");
Assert.AreEqual("// This is a comment\n", comment.ToDisplayString());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ public bool Foo()
case true:
using (var x = new global::System.IO.MemoryStream())
{
// some comment explaining the return
return foo;
}
default:
Expand Down
Loading