Skip to content

Commit 77074f5

Browse files
bsundsboCopilot
andauthored
Supporting Obsolete properties (#19)
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
1 parent b071427 commit 77074f5

File tree

5 files changed

+39
-8
lines changed

5 files changed

+39
-8
lines changed

Source/FluentApi.Generator/FluentCodeGenerator/FluentExtensionCodeGenerator.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,11 @@ private void GenerateExtensionMethods(List<IExtensionTemplateModel> propertyTemp
9494
continue;
9595
}
9696

97+
if (propertyModel is IExtensionPropertyTemplateModel propertyTemplate && propertyTemplate.IsObsolete)
98+
{
99+
sourceBuilder.AppendLineWithIndent(1, $"[Obsolete(\"{propertyTemplate.PropertyName} is obsolete. See reason and workaround on the property itself.\")]");
100+
}
101+
97102
var output = template.Render(propertyModel, MemberRenamer);
98103
sourceBuilder.AppendLine(output);
99104
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
namespace Bars.Mvvm.FluentApi.Generator.Extensions.Models;
2+
3+
/// <summary>
4+
/// Interface for property template models that have an obsolete attribute.
5+
/// </summary>
6+
internal interface IExtensionPropertyTemplateModel : IExtensionTemplateModel
7+
{
8+
/// <summary>
9+
/// Indicates whether the property is obsolete and should be marked as such in the generated code.
10+
/// </summary>
11+
bool IsObsolete { get; }
12+
13+
/// <summary>
14+
/// Get the name of the property.
15+
/// </summary>
16+
string PropertyName { get; }
17+
}
Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
using Microsoft.CodeAnalysis;
2+
using System.Linq;
23

34
namespace Bars.Mvvm.FluentApi.Generator.Extensions.Models;
45

56
/// <summary>
67
/// Base model for specific property template models. All these properties are used in various templates when generating code.
78
/// </summary>
8-
internal record PropertyTemplateModel : IExtensionTemplateModel
9+
internal record PropertyTemplateModel : IExtensionPropertyTemplateModel
910
{
1011
public PropertyTemplateModel(INamedTypeSymbol classSymbol, IPropertySymbol propertySymbol)
1112
{
@@ -14,35 +15,41 @@ public PropertyTemplateModel(INamedTypeSymbol classSymbol, IPropertySymbol prope
1415
ParameterName = char.ToLowerInvariant(this.PropertyName[0]) + this.PropertyName.Substring(1);
1516
ClassName = classSymbol.Name;
1617
NamespaceName = classSymbol.ContainingNamespace.ToDisplayString();
18+
var obsoleteAttribute = propertySymbol.GetAttributes()
19+
.FirstOrDefault(a => a.AttributeClass?.ToDisplayString() == "System.ObsoleteAttribute");
20+
IsObsolete = obsoleteAttribute != null;
1721
}
1822

23+
/// <inheritdoc />
24+
public bool IsObsolete { get; }
25+
1926
/// <summary>
2027
/// Namespace name of the class that contains the class where the property is declared.
2128
/// </summary>
22-
public object NamespaceName { get; init; }
29+
public string NamespaceName { get; }
2330

2431
/// <summary>
2532
/// Class declaring the property.
2633
/// </summary>
27-
public object ClassName { get; init; }
34+
public string ClassName { get; }
2835

2936
/// <summary>
3037
/// Formatted parameter name for the property, which is <see cref="PropertyName"/> in camel case.
3138
/// </summary>
32-
public string ParameterName { get; init; }
39+
public string ParameterName { get; }
3340

3441
/// <summary>
3542
/// Type of the property.
3643
/// </summary>
37-
public string PropertyType { get; init; }
44+
public string PropertyType { get; }
3845

3946
/// <summary>
4047
/// Name of the property generating extension method for.
4148
/// </summary>
42-
public string PropertyName { get; init; }
49+
public string PropertyName { get; }
4350

4451
public override string ToString()
4552
{
46-
return $"{nameof(NamespaceName)}: {NamespaceName}, {nameof(ClassName)}: {ClassName}, {nameof(ParameterName)}: {ParameterName}, {nameof(PropertyType)}: {PropertyType}, {nameof(PropertyName)}: {PropertyName}";
53+
return $"{nameof(NamespaceName)}: {NamespaceName}, {nameof(ClassName)}: {ClassName}, {nameof(ParameterName)}: {ParameterName}, {nameof(PropertyType)}: {PropertyType}, {nameof(PropertyName)}: {PropertyName}, {nameof(IsObsolete)}: {IsObsolete}";
4754
}
4855
}

Source/FluentApi.Generator/Package-README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ new BarToggleButtonViewModel(key)
1717
```
1818
Release Notes:
1919

20+
* 0.3.1
21+
* Extension methods marked as [Obsolete] will also be marked as obsolete. Projects treating obsolete warnings as errors will no longer fail, and consumer will be informed about the obsolete properties.
2022
* 0.3.0
2123
* Renamed package to `Bars.Mvvm.FluentApi.Generator` to better reflect terminology, rather than `Bars.Mvvm.FluidApi.Generator`. No changes to functionality.
2224
* Continuing existing version history to maintain consistency with previous releases.

Version.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"$schema": "https://raw.githubusercontent.com/dotnet/Nerdbank.GitVersioning/main/src/NerdBank.GitVersioning/version.schema.json",
3-
"version": "0.3.0",
3+
"version": "0.3.1",
44
"nuGetPackageVersion": {
55
"semVer": 2.0
66
},

0 commit comments

Comments
 (0)