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

Add namespace check to BlazorUI source generator (#9682) #9697

Draft
wants to merge 3 commits into
base: develop
Choose a base branch
from
Draft
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 @@ -42,117 +42,116 @@ private static string GeneratePartialClass(INamedTypeSymbol classSymbol, List<Bl
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Web;

namespace {namespaceName}
namespace {namespaceName};

public partial class {className}
{{
public partial class {className}
{{
");
foreach (var par in twoWayParameters)
{
var sym = par.PropertySymbol;
builder.AppendLine($" private bool {sym.Name}HasBeenSet;");
builder.AppendLine($" [Parameter] public EventCallback<{sym.Type.ToDisplayString()}> {sym.Name}Changed {{ get; set; }}");
builder.AppendLine($" private bool {sym.Name}HasBeenSet;");
builder.AppendLine($" [Parameter] public EventCallback<{sym.Type.ToDisplayString()}> {sym.Name}Changed {{ get; set; }}");
}
if (twoWayParameters.Length > 0) builder.AppendLine("");
builder.AppendLine($@" [global::System.Diagnostics.DebuggerNonUserCode]
[global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
public override Task SetParametersAsync(ParameterView parameters)
{{");
builder.AppendLine($@" [global::System.Diagnostics.DebuggerNonUserCode]
[global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
public override Task SetParametersAsync(ParameterView parameters)
{{");
foreach (var par in twoWayParameters)
{
builder.AppendLine($" {par.PropertySymbol.Name}HasBeenSet = false;");
builder.AppendLine($" {par.PropertySymbol.Name}HasBeenSet = false;");
}
builder.AppendLine(" var parametersDictionary = parameters.ToDictionary() as Dictionary<string, object>;");
builder.AppendLine(" foreach (var parameter in parametersDictionary!)");
builder.AppendLine(" var parametersDictionary = parameters.ToDictionary() as Dictionary<string, object>;");
builder.AppendLine(" foreach (var parameter in parametersDictionary!)");
builder.AppendLine(" {");
builder.AppendLine(" switch (parameter.Key)");
builder.AppendLine(" {");
builder.AppendLine(" switch (parameter.Key)");
builder.AppendLine(" {");
foreach (var par in parameters)
{
var sym = par.PropertySymbol;
var paramName = sym.Name;
var varName = $"@{paramName.ToLower()}";
var paramType = sym.Type.ToDisplayString();
builder.AppendLine($" case nameof({paramName}):");
builder.AppendLine($" case nameof({paramName}):");
if (par.IsTwoWayBound)
{
builder.AppendLine($" {paramName}HasBeenSet = true;");
builder.AppendLine($" {paramName}HasBeenSet = true;");
}
builder.AppendLine($" var {varName} = parameter.Value is null ? default! : ({paramType})parameter.Value;");
builder.AppendLine($" var {varName} = parameter.Value is null ? default! : ({paramType})parameter.Value;");
if (par.ResetClassBuilder || par.ResetStyleBuilder || string.IsNullOrWhiteSpace(par.CallOnSetMethodName) is false)
{
builder.AppendLine($" var notEquals{paramName} = EqualityComparer<{paramType}>.Default.Equals({paramName}, {varName}) is false;");
builder.AppendLine($" var notEquals{paramName} = EqualityComparer<{paramType}>.Default.Equals({paramName}, {varName}) is false;");
}
builder.AppendLine($" {paramName} = {varName};");
builder.AppendLine($" {paramName} = {varName};");
if (par.ResetClassBuilder)
{
builder.AppendLine($" if (notEquals{paramName}) ClassBuilder.Reset();");
builder.AppendLine($" if (notEquals{paramName}) ClassBuilder.Reset();");
}
if (par.ResetStyleBuilder)
{
builder.AppendLine($" if (notEquals{paramName}) StyleBuilder.Reset();");
builder.AppendLine($" if (notEquals{paramName}) StyleBuilder.Reset();");
}
if (string.IsNullOrWhiteSpace(par.CallOnSetMethodName) is false)
{
builder.AppendLine($" if (notEquals{paramName}) {par.CallOnSetMethodName}();");
builder.AppendLine($" if (notEquals{paramName}) {par.CallOnSetMethodName}();");
}
builder.AppendLine(" parametersDictionary.Remove(parameter.Key);");
builder.AppendLine(" break;");
builder.AppendLine(" parametersDictionary.Remove(parameter.Key);");
builder.AppendLine(" break;");
if (par.IsTwoWayBound)
{
paramName = $"{paramName}Changed";
varName = $"@{paramName.ToLower()}";
builder.AppendLine($" case nameof({paramName}):");
builder.AppendLine($" var {varName} = parameter.Value is null ? default! : (EventCallback<{sym.Type.ToDisplayString()}>)parameter.Value;");
builder.AppendLine($" {paramName} = {varName};");
builder.AppendLine(" parametersDictionary.Remove(parameter.Key);");
builder.AppendLine(" break;");
builder.AppendLine($" case nameof({paramName}):");
builder.AppendLine($" var {varName} = parameter.Value is null ? default! : (EventCallback<{sym.Type.ToDisplayString()}>)parameter.Value;");
builder.AppendLine($" {paramName} = {varName};");
builder.AppendLine(" parametersDictionary.Remove(parameter.Key);");
builder.AppendLine(" break;");
}
}
builder.AppendLine(" }");
builder.AppendLine(" }");
builder.AppendLine(" }");
if (isBaseTypeComponentBase)
{
builder.AppendLine(" return base.SetParametersAsync(ParameterView.Empty);");
builder.AppendLine(" return base.SetParametersAsync(ParameterView.Empty);");
}
else
{
builder.AppendLine(" return base.SetParametersAsync(ParameterView.FromDictionary(parametersDictionary as IDictionary<string, object?>));");
builder.AppendLine(" return base.SetParametersAsync(ParameterView.FromDictionary(parametersDictionary as IDictionary<string, object?>));");
}
builder.AppendLine(@" }");
builder.AppendLine(" }");

if (twoWayParameters.Length > 0) builder.AppendLine("");
foreach (var par in twoWayParameters)
{
var paramName = par.PropertySymbol.Name;
var paramType = par.PropertySymbol.Type.ToDisplayString();
builder.AppendLine($@" [global::System.Diagnostics.DebuggerNonUserCode]
[global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
public async Task<bool> Assign{paramName}({paramType} value)
{{");
builder.AppendLine($" if ({paramName}HasBeenSet && {paramName}Changed.HasDelegate is false) return false;");
builder.AppendLine($" if (EqualityComparer<{paramType}>.Default.Equals({paramName}, value) is false)");
builder.AppendLine(" {");
builder.AppendLine($" {paramName} = value;");
builder.AppendLine($" await {paramName}Changed.InvokeAsync(value);");
builder.AppendLine($@" [global::System.Diagnostics.DebuggerNonUserCode]
[global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
public async Task<bool> Assign{paramName}({paramType} value)
{{");
builder.AppendLine($" if ({paramName}HasBeenSet && {paramName}Changed.HasDelegate is false) return false;");
builder.AppendLine($" if (EqualityComparer<{paramType}>.Default.Equals({paramName}, value) is false)");
builder.AppendLine(" {");
builder.AppendLine($" {paramName} = value;");
builder.AppendLine($" await {paramName}Changed.InvokeAsync(value);");
if (par.ResetClassBuilder)
{
builder.AppendLine($" ClassBuilder.Reset();");
builder.AppendLine(" ClassBuilder.Reset();");
}
if (par.ResetStyleBuilder)
{
builder.AppendLine($" StyleBuilder.Reset();");
builder.AppendLine(" StyleBuilder.Reset();");
}
if (string.IsNullOrWhiteSpace(par.CallOnSetMethodName) is false)
{
builder.AppendLine($" {par.CallOnSetMethodName}();");
builder.AppendLine($" {par.CallOnSetMethodName}();");
}
builder.AppendLine(" }");
builder.AppendLine($" return true;");
builder.AppendLine(" }");
builder.AppendLine(" return true;");
builder.AppendLine(" }");
}

builder.AppendLine(" }");
builder.AppendLine("}");

return builder.ToString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,24 @@ public class ComponentSyntaxContextReceiver : ISyntaxContextReceiver

public void OnVisitSyntaxNode(GeneratorSyntaxContext context)
{
if (context.Node is not PropertyDeclarationSyntax propertyDeclarationSyntax || !propertyDeclarationSyntax.AttributeLists.Any()) return;
if (context.Node is not PropertyDeclarationSyntax propertyDeclarationSyntax ||
propertyDeclarationSyntax.AttributeLists.Any() is false) return;

var parent = propertyDeclarationSyntax.Parent;

if (parent is null || parent.IsKind(SyntaxKind.ClassDeclaration) is false) return;

var classDeclarationSyntax = (ClassDeclarationSyntax?)parent;
var classDeclarationSyntax = (ClassDeclarationSyntax)parent;

if (classDeclarationSyntax?.Modifiers.Any(k => k.IsKind(SyntaxKind.PartialKeyword)) is false) return;
if (classDeclarationSyntax.Modifiers.Any(k => k.IsKind(SyntaxKind.PartialKeyword)) is false) return;

var namespaceParent = classDeclarationSyntax.Parent;

if (namespaceParent is null) return;

var namespaceDeclarationSyntax = (BaseNamespaceDeclarationSyntax)namespaceParent;

if (namespaceDeclarationSyntax.Name.ToString().StartsWith("Bit.BlazorUI") is false) return;

var propertySymbol = context.SemanticModel.GetDeclaredSymbol(propertyDeclarationSyntax);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,35 +8,32 @@ namespace Bit.SourceGenerators;

public class BlazorParameterPropertySyntaxReceiver : ISyntaxContextReceiver
{
public IList<IPropertySymbol> Properties { get; } = new List<IPropertySymbol>();
public IList<IPropertySymbol> Properties { get; } = [];

public void OnVisitSyntaxNode(GeneratorSyntaxContext context)
{
if (context.Node is PropertyDeclarationSyntax propertyDeclarationSyntax
&& propertyDeclarationSyntax.AttributeLists.Any())
{
if (context.Node is not PropertyDeclarationSyntax propertyDeclarationSyntax ||
propertyDeclarationSyntax.AttributeLists.Any() is false) return;

var classDeclarationSyntax = (ClassDeclarationSyntax?)propertyDeclarationSyntax.Parent;
var classDeclarationSyntax = (ClassDeclarationSyntax?)propertyDeclarationSyntax.Parent;

if (classDeclarationSyntax?.Modifiers.Any(k => k.IsKind(SyntaxKind.PartialKeyword)) is false)
return;
if (classDeclarationSyntax?.Modifiers.Any(k => k.IsKind(SyntaxKind.PartialKeyword)) is false) return;

var propertySymbol = context.SemanticModel.GetDeclaredSymbol(propertyDeclarationSyntax);
var propertySymbol = context.SemanticModel.GetDeclaredSymbol(propertyDeclarationSyntax);

if (propertySymbol is null) return;
if (propertySymbol is null) return;

var type = propertySymbol.ContainingType;
var type = propertySymbol.ContainingType;

if (type == null) return;
if (type == null) return;

if (type.GetMembers().Any(m => m.Name == "SetParametersAsync")) return;
if (type.GetMembers().Any(m => m.Name == "SetParametersAsync")) return;


if (propertySymbol.GetAttributes().Any(ad => ad.AttributeClass?.ToDisplayString() == "Microsoft.AspNetCore.Components.ParameterAttribute"
|| ad.AttributeClass?.ToDisplayString() == "Microsoft.AspNetCore.Components.CascadingParameterAttribute"))
{
Properties.Add(propertySymbol);
}
if (propertySymbol.GetAttributes().Any(ad => ad.AttributeClass?.ToDisplayString() == "Microsoft.AspNetCore.Components.ParameterAttribute" ||
ad.AttributeClass?.ToDisplayString() == "Microsoft.AspNetCore.Components.CascadingParameterAttribute"))
{
Properties.Add(propertySymbol);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,14 @@ namespace Bit.SourceGenerators;
[Generator]
public class BlazorSetParametersSourceGenerator : ISourceGenerator
{
public void Initialize(GeneratorInitializationContext context)
{
context.RegisterForSyntaxNotifications(() => new BlazorParameterPropertySyntaxReceiver());
}

public void Execute(GeneratorExecutionContext context)
{
if (context.SyntaxContextReceiver is not BlazorParameterPropertySyntaxReceiver receiver)
return;
if (context.SyntaxContextReceiver is not BlazorParameterPropertySyntaxReceiver receiver) return;

foreach (var group in receiver.Properties.GroupBy(symbol => symbol.ContainingType, SymbolEqualityComparer.Default))
{
Expand All @@ -26,11 +30,6 @@ public void Execute(GeneratorExecutionContext context)
}
}

public void Initialize(GeneratorInitializationContext context)
{
context.RegisterForSyntaxNotifications(() => new BlazorParameterPropertySyntaxReceiver());
}

private static string GeneratePartialClassToOverrideSetParameters(INamedTypeSymbol classSymbol, List<BitProperty> properties)
{
string namespaceName = classSymbol.ContainingNamespace.ToDisplayString();
Expand Down
Loading