Skip to content

Commit

Permalink
Blazorise constants with numeric values. Fix for enums from different…
Browse files Browse the repository at this point in the history
… namespaces (e.g. Snackbar)
  • Loading branch information
tesar-tech committed Nov 28, 2024
1 parent a5a402c commit 4ea62c6
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ private static Optional<object> TryToGetDefaultValueFromGetter( Compilation comp

ExpressionSyntax fieldInitializerValue = variableDeclarator.Initializer.Value;

string expressionString = QualifyExpressionSyntax( compilation, fieldInitializerValue );
object expressionString = QualifyExpressionSyntax( compilation, fieldInitializerValue );
return new Optional<object>( expressionString );
}

Expand All @@ -128,17 +128,27 @@ private static Optional<object> TryToGetConstantValueFromPropertyInitializer( Co
// Get the expression representing the initializer value
ExpressionSyntax initializerValue = propertySyntax.Initializer.Value;

string expressionString = QualifyExpressionSyntax( compilation, initializerValue );
object expressionString = QualifyExpressionSyntax( compilation, initializerValue );
return new Optional<object>( expressionString );
}

private static string QualifyExpressionSyntax( Compilation compilation, ExpressionSyntax expression )
private static object QualifyExpressionSyntax( Compilation compilation, ExpressionSyntax expression )
{
var semanticModel = compilation.GetSemanticModel( expression.SyntaxTree );

var rewriter = new FullyQualifiedNameRewriter( semanticModel );
var qualifiedExpression = rewriter.Visit( expression );

if ( qualifiedExpression.ToString().Contains( "Blazorise" ) )
{//this will process constants, but only Blazorise constants. Blazorise.Snackbar.Constants.DefaultIntervalBeforeClose will return value (5000), otherwise the constant name (int.MaxValue)
var symbolInfo = semanticModel.GetSymbolInfo( expression );
if ( symbolInfo.Symbol is IFieldSymbol { IsConst: true } fieldSymbol )
{
// If the symbol is a constant field, return its value
return fieldSymbol.ConstantValue;
}
}

return qualifiedExpression.ToString();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public override SyntaxNode VisitMemberAccessExpression(MemberAccessExpressionSyn
// Parse the fully qualified name into an expression
var fullyQualifiedExpression = SyntaxFactory.ParseExpression(fullyQualifiedName)
.WithTriviaFrom(node);

return fullyQualifiedExpression;
}
// Reconstruct the member access expression with updated components
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public static ApiDocsForComponentProperty GetPropertyDetails( Compilation compil
};
string defaultValueAsString = property.Type.Name == "String" ? defaultValueString : $""""
$$"""
{StringHelpers.TypeToStringDetails( defaultValueString, propertyDetails.TypeName )}
{StringHelpers.TypeToStringDetails( defaultValueString, propertyDetails.Type )}
"""
"""";
propertyDetails.DefaultValueString = defaultValueAsString;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,19 +43,33 @@ public static string FormatProperly( object value )
const string toReplace = "global::";
const string toReplace2 = "Blazorise.";

public static string TypeToStringDetails( string value, string propertyDetailsTypeName )
public static string TypeToStringDetails( string value, string propertyDetailsType )
{

if ( value.StartsWith( toReplace ) )
{
value = value.Substring( toReplace.Length );
}

if ( value.Contains( toReplace2 ) )
{
string propertyDetailsTypeNameWithDot = propertyDetailsType.Replace(toReplace,"") + ".";//From Color.Default => Default

if ( value.Contains( propertyDetailsTypeNameWithDot ) )
{
value = value.Substring( propertyDetailsTypeNameWithDot.Length );
Logger.Log($"Returning {value}");

return value;
}
}

if ( value.StartsWith( toReplace2 ) )
{
value = value.Substring( toReplace2.Length );
string propertyDetailsTypeNameWithDot = propertyDetailsTypeName + ".";//From Color.Default => Default
string propertyDetailsTypeNameWithDot = propertyDetailsType + ".";//From Color.Default => Default
if ( value.StartsWith( propertyDetailsTypeNameWithDot ) )
value = value.Substring( propertyDetailsTypeNameWithDot.Length );
value = value.Substring( toReplace2.Length );
}
return value;

Expand Down

0 comments on commit 4ea62c6

Please sign in to comment.