Skip to content

Update method parameter with VisitMethod #7472

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

Merged
merged 6 commits into from
May 28, 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 @@ -114,6 +114,8 @@ public void Update(
return updated.Accept(visitor);
}

Signature = updated.Signature;

if (BodyExpression != null)
{
var expression = BodyExpression.Accept(visitor, this);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,28 +19,28 @@ namespace Microsoft.TypeSpec.Generator.Providers
[DebuggerDisplay("{GetDebuggerDisplay(),nq}")]
public sealed class ParameterProvider : IEquatable<ParameterProvider>
{
public string Name { get; }
public FormattableString Description { get; }
public string Name { get; private set; }
public FormattableString Description { get; private set; }
public CSharpType Type { get; set; }

/// <summary>
/// The default value of the parameter.
/// </summary>
public ValueExpression? DefaultValue { get; set; }
public ValueExpression? InitializationValue { get; init; }
public ParameterValidationType Validation { get; init; } = ParameterValidationType.None;
public bool IsRef { get; }
public bool IsOut { get; }
public bool IsParams { get; }
public ValueExpression? InitializationValue { get; private set; }
public ParameterValidationType Validation { get; set; } = ParameterValidationType.None;
public bool IsRef { get; private set; }
public bool IsOut { get; private set; }
public bool IsParams { get; private set; }

internal IReadOnlyList<AttributeStatement> Attributes { get; } = [];
public WireInformation WireInfo { get; }
public ParameterLocation Location { get; }
internal IReadOnlyList<AttributeStatement> Attributes { get; private set; } = [];
public WireInformation WireInfo { get; private set; }
public ParameterLocation Location { get; private set; }

/// <summary>
/// This property tracks which property this parameter is constructed from.
/// </summary>
public PropertyProvider? Property { get; }
public PropertyProvider? Property { get; private set; }

/// <summary>
/// This property tracks which field this parameter is constructed from.
Expand Down Expand Up @@ -237,5 +237,98 @@ internal ParameterProvider WithRef()
_asVariable = AsExpression,
};
}

/// <summary>
/// Updates the parameter with the given name.
/// </summary>
public void Update(
string? name = null,
FormattableString? description = null,
CSharpType? type = null,
ValueExpression? defaultValue = null,
bool? isRef = null,
bool? isOut = null,
bool? isParams = null,
IReadOnlyList<AttributeStatement>? attributes = null,
PropertyProvider? property = null,
FieldProvider? field = null,
ValueExpression? initializationValue = null,
ParameterLocation? location = null,
WireInformation? wireInfo = null,
ParameterValidationType? validation = null)
{
if (name is not null)
{
Name = name;
_asVariable?.Update(name: name);
}

if (description is not null)
{
Description = description;
}

if (type is not null)
{
Type = type;
_asVariable?.Update(type: type);
}

if (defaultValue is not null)
{
DefaultValue = defaultValue;
}

if (isRef is not null)
{
IsRef = isRef.Value;
_asVariable?.Update(isRef: IsRef);
}

if (isOut is not null)
{
IsOut = isOut.Value;
}

if (isParams is not null)
{
IsParams = isParams.Value;
}

if (attributes is not null)
{
Attributes = attributes;
}

if (property is not null)
{
Property = property;
}

if (field is not null)
{
Field = field;
}

if (initializationValue is not null)
{
InitializationValue = initializationValue;
}

if (location is not null)
{
Location = location.Value;
}

if (wireInfo is not null)
{
WireInfo = wireInfo;
}

if (validation is not null)
{
Validation = validation.Value;
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using System.Linq;
using Microsoft.TypeSpec.Generator.Primitives;
using Microsoft.TypeSpec.Generator.Providers;
using Microsoft.TypeSpec.Generator.Snippets;
Expand Down Expand Up @@ -185,5 +186,35 @@ public void DoesNotVisitFieldsWhenTypeIsNulledOut()
_mockVisitor.Protected().Verify<TypeProvider>("VisitType", Times.Once(), _mockTypeProvider.Object);
_mockVisitor.Protected().Verify<FieldProvider>("VisitField", Times.Never(), mockFieldProvider.Object);
}

[Test]
public void VisitMethodToRenameParameterName()
{
var parameter = new ParameterProvider("oldName", $"", typeof(string));
var testMethod = new MethodProvider(
new MethodSignature("Test", $"", MethodSignatureModifiers.Public, null, $"", [parameter]),
Snippet.Return(parameter), new TestTypeProvider());

testMethod.Accept(new MethodVisitor());

Assert.AreEqual("newName", testMethod.Signature.Parameters.First().Name);
Assert.AreEqual("return newName;\n", testMethod?.BodyStatements!.ToDisplayString());
}

private class MethodVisitor : LibraryVisitor
{
protected internal override MethodProvider? VisitMethod(MethodProvider method)
{
// Rename the parameter to "newName"
foreach (var parameter in method.Signature.Parameters)
{
if (parameter.Name == "oldName")
{
parameter.Update("newName");
}
}
return base.VisitMethod(method);
}
}
}
}
Loading