Skip to content

Commit

Permalink
Replace AsExpression and explicit casting with extension methods (#4940)
Browse files Browse the repository at this point in the history
Resolves #4925

Remove the unnecessary casting and AsExpression invoke.
  • Loading branch information
live1206 authored Nov 7, 2024
1 parent e984e74 commit 86f02c6
Show file tree
Hide file tree
Showing 13 changed files with 30 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ protected override MethodProvider[] BuildMethods()
this);

var charValueParameter = new ParameterProvider("value", FormattableStringHelpers.Empty, typeof(char));
var value = charValueParameter.AsExpression.As<char>();
var value = charValueParameter.As<char>();
var writeStringChar = new MethodProvider(
new MethodSignature(
Name: WriteStringValueMethodName,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ private MethodProvider BuildExplicitFromClientResult()
var result = new ParameterProvider("result", $"The {ClientModelPlugin.Instance.TypeFactory.ClientResponseApi.ClientResponseType:C} to deserialize the {Type:C} from.", ClientModelPlugin.Instance.TypeFactory.ClientResponseApi.ClientResponseType);
var modifiers = MethodSignatureModifiers.Public | MethodSignatureModifiers.Static | MethodSignatureModifiers.Explicit | MethodSignatureModifiers.Operator;
// using PipelineResponse response = result.GetRawResponse();
var responseDeclaration = UsingDeclare("response", ClientModelPlugin.Instance.TypeFactory.HttpResponseApi.HttpResponseType, result.AsExpression.ToApi<ClientResponseApi>().GetRawResponse(), out var response);
var responseDeclaration = UsingDeclare("response", ClientModelPlugin.Instance.TypeFactory.HttpResponseApi.HttpResponseType, result.ToApi<ClientResponseApi>().GetRawResponse(), out var response);
// using JsonDocument document = JsonDocument.Parse(response.Content);
var document = UsingDeclare(
"document",
Expand Down Expand Up @@ -206,7 +206,7 @@ private MethodProvider BuildImplicitToBinaryContent()
new MethodSignature(ClientModelPlugin.Instance.TypeFactory.RequestContentApi.RequestContentType.FrameworkType.Name, null, modifiers, null, null, [model]),
new MethodBodyStatement[]
{
!_isStruct ? new IfStatement(model.AsExpression.Equal(Null)) { Return(Null) } : MethodBodyStatement.Empty,
!_isStruct ? new IfStatement(model.Equal(Null)) { Return(Null) } : MethodBodyStatement.Empty,
ClientModelPlugin.Instance.TypeFactory.RequestContentApi.ToExpression().Create(model)
},
this);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@
using System.Linq;
using Microsoft.Generator.CSharp.Expressions;
using Microsoft.Generator.CSharp.Providers;
using Microsoft.Generator.CSharp.Snippets;

namespace Microsoft.Generator.CSharp.Primitives
{
public sealed record ConstructorInitializer(bool IsBase, IReadOnlyList<ValueExpression> Arguments)
{
public ConstructorInitializer(bool isBase, IEnumerable<ParameterProvider> arguments) : this(isBase, [.. arguments.Select(p => p.AsExpression)]) { }
public ConstructorInitializer(bool isBase, IEnumerable<ParameterProvider> arguments) : this(isBase, [.. arguments.Select(p => p.AsExpression())]) { }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ private MethodProvider BuildAssertNotNullOrEmptyCollection()

private static ScopedApi<bool> IsCollectionEmpty(ParameterProvider valueParam, VariableExpression collection)
{
return valueParam.AsExpression.Is(new DeclarationExpression(collection)).And(new MemberExpression(collection, "Count").Equal(Literal(0)));
return valueParam.Is(new DeclarationExpression(collection)).And(new MemberExpression(collection, "Count").Equal(Literal(0)));
}

private MethodBodyStatement ThrowArgumentException(ValueExpression expression)
Expand Down Expand Up @@ -266,7 +266,7 @@ private MethodProvider BuildAssertNotNull()

private IfStatement AssertNotNullSnippet(ParameterProvider value)
{
return new IfStatement(value.AsExpression.Is(Null))
return new IfStatement(value.Is(Null))
{
Throw(New.ArgumentNullException(_nameParam, false))
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ private ConstructorProvider ConstructorWithDictionary()
var signature = new ConstructorSignature(Type, null, MethodSignatureModifiers.Public, [dictionary]);
return new ConstructorProvider(signature, new MethodBodyStatement[]
{
new IfStatement(dictionary.AsExpression.Equal(Null))
new IfStatement(dictionary.Equal(Null))
{
Return()
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ protected override MethodProvider[] BuildMethods()
// public override bool Equals(object obj) => obj is EnumType other && Equals(other);
methods.Add(new(
equalsSignature,
objParameter.AsExpression
objParameter
.Is(new DeclarationExpression(Type, "other", out var other))
.And(This.Invoke(nameof(Equals), [other])),
this));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -594,7 +594,7 @@ private ValueExpression GetExpressionForCtor(ParameterProvider parameter, HashSe
return null;
}

var discriminatorExpression = property.AsParameter.AsExpression;
var discriminatorExpression = property.AsParameter;
var type = property.Type;

if (!type.IsFrameworkType && type.IsEnum)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ private MethodProvider BuildIsReadOnlyDictionaryDefined()

return new MethodProvider(signature, new MethodBodyStatement[]
{
Return(Not(collectionParam.AsExpression.Is(changeTrackingDeclarationExpression)
Return(Not(collectionParam.Is(changeTrackingDeclarationExpression)
.And(new MemberExpression(changeTrackingReference, "IsUndefined"))))
},
this);
Expand All @@ -127,7 +127,7 @@ private MethodProvider BuildIsDictionaryDefined()

return new MethodProvider(signature, new MethodBodyStatement[]
{
Return(Not(collectionParam.AsExpression.Is(changeTrackingDeclarationExpression)
Return(Not(collectionParam.Is(changeTrackingDeclarationExpression)
.And(new MemberExpression(changeTrackingReference, "IsUndefined"))))
},
this);
Expand All @@ -142,7 +142,7 @@ private MethodProvider BuildIsListDefined()

return new MethodProvider(signature, new MethodBodyStatement[]
{
Return(Not(collectionParam.AsExpression.Is(changeTrackingDeclarationExpression)
Return(Not(collectionParam.Is(changeTrackingDeclarationExpression)
.And(new MemberExpression(changeTrackingReference, "IsUndefined"))))
},
this);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ private static VariableExpression GetVariableExpression(ParameterProvider parame
}

private VariableExpression? _asVariable;
public VariableExpression AsExpression => _asVariable ??= this;
private VariableExpression AsExpression => _asVariable ??= this;

public TypeProvider? SpreadSource { get; set; }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,17 @@ namespace Microsoft.Generator.CSharp.Snippets
{
public static partial class Snippet
{
public static ScopedApi As(this ParameterProvider parameter, CSharpType type) => parameter.AsExpression.As(type);
public static ScopedApi<T> As<T>(this ParameterProvider parameter) => parameter.AsExpression.As<T>();
public static ScopedApi<T> As<T>(this PropertyProvider property) => ((MemberExpression)property).As<T>();
public static ScopedApi<T> As<T>(this FieldProvider field) => ((MemberExpression)field).As<T>();
public static ScopedApi<bool> Equal(this ParameterProvider parameter, ValueExpression other) => new BinaryOperatorExpression("==", parameter, other).As<bool>();
public static ScopedApi<bool> Is(this ParameterProvider parameter, ValueExpression other) => new BinaryOperatorExpression("is", parameter, other).As<bool>();

public static ScopedApi As(this ParameterProvider parameter, CSharpType type) => ((ValueExpression)parameter).As(type);
public static ScopedApi<T> As<T>(this ParameterProvider parameter) => ((ValueExpression)parameter).As<T>();
public static ScopedApi<T> As<T>(this PropertyProvider property) => ((ValueExpression)property).As<T>();
public static ScopedApi<T> As<T>(this FieldProvider field) => ((ValueExpression)field).As<T>();

public static ValueExpression NullConditional(this ParameterProvider parameter) => new NullConditionalExpression(parameter);

public static ValueExpression NullCoalesce(this ParameterProvider parameter, ValueExpression value) => parameter.AsExpression.NullCoalesce(value);
public static ValueExpression NullCoalesce(this ParameterProvider parameter, ValueExpression value) => new BinaryOperatorExpression("??", parameter, value);
public static ValueExpression PositionalReference(this ParameterProvider parameter, ValueExpression value)
=> new PositionalParameterReferenceExpression(parameter.Name, value);

Expand Down Expand Up @@ -143,5 +146,7 @@ public static ValueExpression Invoke(this PropertyProvider property,

public static ScopedApi<bool> NotEqual(this ParameterProvider parameter, ValueExpression other)
=> new BinaryOperatorExpression("!=", parameter, other).As<bool>();

public static VariableExpression AsExpression(this ParameterProvider variableExpression) => (VariableExpression)variableExpression;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using Microsoft.Generator.CSharp.Expressions;
using Microsoft.Generator.CSharp.Primitives;
using Microsoft.Generator.CSharp.Providers;
using Microsoft.Generator.CSharp.Snippets;
using Microsoft.Generator.CSharp.Statements;
using static Microsoft.Generator.CSharp.Snippets.Snippet;

Expand Down Expand Up @@ -281,7 +282,7 @@ public void WriteProperty(PropertyProvider property, bool isPublicContext = fals
if (property is IndexPropertyProvider indexer)
{
indexerScope = AmbientScope();
Append($"{indexer.Name}[{indexer.IndexerParameter.Type} {indexer.IndexerParameter.AsExpression.Declaration}]");
Append($"{indexer.Name}[{indexer.IndexerParameter.Type} {indexer.IndexerParameter.AsExpression().Declaration}]");
}
else
{
Expand Down Expand Up @@ -406,7 +407,7 @@ public void WriteParameter(ParameterProvider parameter)
AppendRawIf("out ", parameter.IsOut);
AppendRawIf("ref ", parameter.IsRef);

Append($"{parameter.Type} {parameter.AsExpression.Declaration}");
Append($"{parameter.Type} {parameter.AsExpression().Declaration}");
if (parameter.DefaultValue != null)
{
AppendRaw(" = ");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ public void ToPublicInputParameterCopiesProperties()
var parameter = CodeModelPlugin.Instance.TypeFactory.CreateParameter(inputType);
var publicParameter = parameter.ToPublicInputParameter();

Assert.AreEqual(parameter.AsExpression, publicParameter.AsExpression);
Assert.AreEqual(parameter, publicParameter);
Assert.AreEqual(parameter.Attributes, publicParameter.Attributes);
Assert.AreEqual(parameter.DefaultValue, publicParameter.DefaultValue);
Assert.AreEqual(parameter.Description, publicParameter.Description);
Expand All @@ -127,7 +127,7 @@ public void WithRefCopiesProperties()
var parameter = CodeModelPlugin.Instance.TypeFactory.CreateParameter(inputType);
var refParemeter = parameter.WithRef();

Assert.AreEqual(parameter.AsExpression, refParemeter.AsExpression);
Assert.AreEqual(parameter, refParemeter);
Assert.AreEqual(parameter.Attributes, refParemeter.Attributes);
Assert.AreEqual(parameter.DefaultValue, refParemeter.DefaultValue);
Assert.AreEqual(parameter.Description, refParemeter.Description);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ public void ScopeDeclaredTwiceForMethodSignatureParam()

using var codeWriter = new CodeWriter();
codeWriter.WriteMethod(method);
var declScopes = GetDeclarationScopes(param.AsExpression.Declaration);
var declScopes = GetDeclarationScopes(param.AsExpression().Declaration);
Assert.AreEqual(2, declScopes.Count);
Assert.AreEqual(Helpers.GetExpectedFromFile(), codeWriter.ToString(false));
}
Expand Down

0 comments on commit 86f02c6

Please sign in to comment.