Skip to content

Commit

Permalink
Fix issue #33
Browse files Browse the repository at this point in the history
  • Loading branch information
Dương Kafka Đinh Hoàng committed Apr 11, 2023
1 parent 53b2148 commit 7b8507d
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 7 deletions.
5 changes: 2 additions & 3 deletions BindableProps/BindableProps.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<PackageLicenseFile>LICENSE.txt</PackageLicenseFile>
<PackageTags>source-generator;maui;net-6;utility;helper</PackageTags>
<PackageProjectUrl>https://github.com/KafkaWannaFly/BindableProps</PackageProjectUrl>
<Version>1.3.4</Version>
<Version>1.3.5-beta</Version>
<RepositoryUrl>https://github.com/KafkaWannaFly/BindableProps</RepositoryUrl>
<RepositoryType>git</RepositoryType>
<PackageReadmeFile>README.md</PackageReadmeFile>
Expand Down Expand Up @@ -44,8 +44,7 @@
<None Include="..\BindablePropsSG\bin\$(Configuration)\netstandard2.0\BindablePropsSG.dll" PackagePath="analyzers\dotnet\cs" Pack="true" Visible="false" />
</ItemGroup>

<!-- Enable trimming support on .NET 6 -->
<PropertyGroup Condition="'$(TargetFramework)' == 'net6.0'">
<PropertyGroup Condition="'$(TargetFramework)' == 'net7.0'">
<EnableTrimAnalyzer>true</EnableTrimAnalyzer>
<IsTrimmable>true</IsTrimmable>
</PropertyGroup>
Expand Down
9 changes: 8 additions & 1 deletion BindablePropsSG/Generators/AllBindablePropsSG.cs
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,13 @@ protected override void ProcessField(StringBuilder source, ClassDeclarationSynta
var propName = StringUtil.PascalCaseOf(fieldName);
var dataType = fieldSyntax.Declaration.Type;

// typeof operation doesn't accept nullable data type
var unNullableDataType = dataType.ToString();
if (unNullableDataType[unNullableDataType.Length - 1] == '?')
{
unNullableDataType = unNullableDataType.Substring(0, unNullableDataType.Length - 1);
}

var className = classDeclarationSyntax.Identifier.ToString();

var variableDeclaratorSyntax = fieldSyntax.ChildNodes().OfType<VariableDeclarationSyntax>().FirstOrDefault()
Expand All @@ -138,7 +145,7 @@ protected override void ProcessField(StringBuilder source, ClassDeclarationSynta
source.Append($@"
public static readonly BindableProperty {propName}Property = BindableProperty.Create(
nameof({propName}),
typeof({dataType}),
typeof({unNullableDataType}),
typeof({className}),
{defaultValue},
propertyChanged: (bindable, oldValue, newValue) =>
Expand Down
9 changes: 8 additions & 1 deletion BindablePropsSG/Generators/AttachedPropSG.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,13 @@ protected override void ProcessField(StringBuilder source, ClassDeclarationSynta
var fieldSyntax = (FieldDeclarationSyntax)syntaxNode;

var fieldType = fieldSyntax.Declaration.Type;

// typeof operation doesn't accept nullable data type
var unNullableDataType = fieldType.ToString();
if (unNullableDataType[unNullableDataType.Length - 1] == '?')
{
unNullableDataType = unNullableDataType.Substring(0, unNullableDataType.Length - 1);
}

var className = classSyntax.Identifier;

Expand Down Expand Up @@ -60,7 +67,7 @@ protected override void ProcessField(StringBuilder source, ClassDeclarationSynta
source.Append($@"
public static readonly BindableProperty {propName} = BindableProperty.CreateAttached(
""{pascalCaseFieldName}"",
typeof({fieldType}),
typeof({unNullableDataType}),
typeof({className}),
{defaultFieldValue},
(BindingMode){defaultBindingMode},
Expand Down
9 changes: 8 additions & 1 deletion BindablePropsSG/Generators/BindablePropSG.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,13 @@ protected override void ProcessField(StringBuilder source, ClassDeclarationSynta

var fieldSyntax = (FieldDeclarationSyntax)syntaxNode;
var fieldType = fieldSyntax.Declaration.Type;

// typeof operation doesn't accept nullable data type
var unNullableDataType = fieldType.ToString();
if (unNullableDataType[unNullableDataType.Length - 1] == '?')
{
unNullableDataType = unNullableDataType.Substring(0, unNullableDataType.Length - 1);
}

var className = classSyntax.Identifier;

Expand Down Expand Up @@ -61,7 +68,7 @@ protected override void ProcessField(StringBuilder source, ClassDeclarationSynta
source.Append($@"
public static readonly BindableProperty {propName}Property = BindableProperty.Create(
nameof({propName}),
typeof({fieldType}),
typeof({unNullableDataType}),
typeof({className}),
{defaultFieldValue},
(BindingMode){defaultBindingMode},
Expand Down
28 changes: 28 additions & 0 deletions UnitTest/TestData/BindablePropTest/SimpleUsage.expected.txt
Original file line number Diff line number Diff line change
Expand Up @@ -59,5 +59,33 @@ namespace UnitTest.TestData.BindablePropTest
OnPropertyChanged(nameof(Description));
}
}

public static readonly BindableProperty SideNoteProperty = BindableProperty.Create(
nameof(SideNote),
typeof(string),
typeof(SimpleUsage),
default,
(BindingMode)0,
null,
(bindable, oldValue, newValue) =>
((SimpleUsage)bindable).SideNote = (string?)newValue,
null,
null,
null
);

public string? SideNote
{
get => sideNote;
set
{
OnPropertyChanging(nameof(SideNote));

sideNote = value;
SetValue(SimpleUsage.SideNoteProperty, sideNote);

OnPropertyChanged(nameof(SideNote));
}
}
}
}
3 changes: 3 additions & 0 deletions UnitTest/TestData/BindablePropTest/SimpleUsage.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,8 @@

[BindableProp]
string description = "To the moon";

[BindableProp]
string? sideNote;
}
}
2 changes: 1 addition & 1 deletion UnitTest/UnitTest.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>

Expand Down

0 comments on commit 7b8507d

Please sign in to comment.