Skip to content

Commit 6b56c22

Browse files
committed
Refactor null checks and modernize code style
Replaced manual null checks with ArgumentNullException.ThrowIfNull for clarity and consistency. Updated unit tests to use object initializers. Simplified dictionary initialization with collection expressions and improved code style with var usage. Removed unnecessary comments, whitespace, and the <NoWarn> property from the project file. No significant documentation changes.
1 parent 4bf3c7d commit 6b56c22

File tree

9 files changed

+47
-64
lines changed

9 files changed

+47
-64
lines changed

examples/Tools/FluentUI.Demo.DocApiGen.Tests/Models/AllMode/ComponentInfoTests.cs

Lines changed: 30 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,11 @@ public void Constructor_ShouldInitializeWithDefaults()
3434
public void Name_ShouldBeSettable()
3535
{
3636
// Arrange
37-
var component = new ComponentInfo();
38-
39-
// Act
40-
component.Name = "FluentButton";
37+
var component = new ComponentInfo
38+
{
39+
// Act
40+
Name = "FluentButton"
41+
};
4142

4243
// Assert
4344
Assert.Equal("FluentButton", component.Name);
@@ -47,10 +48,11 @@ public void Name_ShouldBeSettable()
4748
public void FullName_ShouldBeSettable()
4849
{
4950
// Arrange
50-
var component = new ComponentInfo();
51-
52-
// Act
53-
component.FullName = "Microsoft.FluentUI.AspNetCore.Components.FluentButton";
51+
var component = new ComponentInfo
52+
{
53+
// Act
54+
FullName = "Microsoft.FluentUI.AspNetCore.Components.FluentButton"
55+
};
5456

5557
// Assert
5658
Assert.Equal("Microsoft.FluentUI.AspNetCore.Components.FluentButton", component.FullName);
@@ -60,10 +62,11 @@ public void FullName_ShouldBeSettable()
6062
public void Summary_ShouldBeSettable()
6163
{
6264
// Arrange
63-
var component = new ComponentInfo();
64-
65-
// Act
66-
component.Summary = "A button component";
65+
var component = new ComponentInfo
66+
{
67+
// Act
68+
Summary = "A button component"
69+
};
6770

6871
// Assert
6972
Assert.Equal("A button component", component.Summary);
@@ -73,10 +76,11 @@ public void Summary_ShouldBeSettable()
7376
public void Category_ShouldBeSettable()
7477
{
7578
// Arrange
76-
var component = new ComponentInfo();
77-
78-
// Act
79-
component.Category = "Forms";
79+
var component = new ComponentInfo
80+
{
81+
// Act
82+
Category = "Forms"
83+
};
8084

8185
// Assert
8286
Assert.Equal("Forms", component.Category);
@@ -86,10 +90,11 @@ public void Category_ShouldBeSettable()
8690
public void IsGeneric_ShouldBeSettable()
8791
{
8892
// Arrange
89-
var component = new ComponentInfo();
90-
91-
// Act
92-
component.IsGeneric = true;
93+
var component = new ComponentInfo
94+
{
95+
// Act
96+
IsGeneric = true
97+
};
9398

9499
// Assert
95100
Assert.True(component.IsGeneric);
@@ -112,10 +117,11 @@ public void BaseClass_ShouldBeSettableToNull()
112117
public void BaseClass_ShouldBeSettableToValue()
113118
{
114119
// Arrange
115-
var component = new ComponentInfo();
116-
117-
// Act
118-
component.BaseClass = "FluentComponentBase";
120+
var component = new ComponentInfo
121+
{
122+
// Act
123+
BaseClass = "FluentComponentBase"
124+
};
119125

120126
// Assert
121127
Assert.Equal("FluentComponentBase", component.BaseClass);

examples/Tools/FluentUI.Demo.DocApiGen/FluentUI.Demo.DocApiGen.csproj

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66
<ImplicitUsings>enable</ImplicitUsings>
77
<Nullable>enable</Nullable>
88
<GenerateDocumentationFile>true</GenerateDocumentationFile>
9-
<!-- Suppress code analysis rules for null checks -->
10-
<NoWarn>$(NoWarn);CA1510;IDE0005;IDE0007;IDE0011;CS1591</NoWarn>
119
</PropertyGroup>
1210

1311
<!-- Code Analysis -->

examples/Tools/FluentUI.Demo.DocApiGen/Formatters/CSharpOutputFormatter.cs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,7 @@ public class CSharpOutputFormatter : IOutputFormatter
2020
/// <inheritdoc/>
2121
public string Format(object data)
2222
{
23-
if (data == null)
24-
{
25-
throw new ArgumentNullException(nameof(data));
26-
}
23+
ArgumentNullException.ThrowIfNull(data);
2724

2825
if (data is not SummaryDocumentationData summaryData)
2926
{
@@ -84,7 +81,7 @@ private static string GenerateCSharpCode(SummaryDocumentationData data)
8481
var key = EscapeString(entry.Key);
8582
var summary = EscapeString(entry.Value.Summary);
8683
var signature = EscapeString(entry.Value.Signature);
87-
84+
8885
#pragma warning disable CA1305 // Specify IFormatProvider
8986
sb.AppendLine($" {{ \"{key}\", (\"{summary}\", \"{signature}\") }},");
9087
#pragma warning restore CA1305 // Specify IFormatProvider

examples/Tools/FluentUI.Demo.DocApiGen/Formatters/JsonOutputFormatter.cs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,7 @@ public JsonOutputFormatter(bool indented = true, bool useCompactFormat = true)
3636
/// <inheritdoc/>
3737
public string Format(object data)
3838
{
39-
if (data == null)
40-
{
41-
throw new ArgumentNullException(nameof(data));
42-
}
39+
ArgumentNullException.ThrowIfNull(data);
4340

4441
// Si c'est un SummaryDocumentationData et qu'on veut le format compact (Summary mode standard)
4542
if (_useCompactFormat && data is SummaryDocumentationData summaryData)
@@ -82,9 +79,11 @@ private static string FormatSummary(SummaryDocumentationData data)
8279
// Format de la clé: "Namespace.TypeName.__summary__" ou "Namespace.TypeName.MemberName"
8380
var fullKey = kvp.Key;
8481
var lastDotIndex = fullKey.LastIndexOf('.');
85-
82+
8683
if (lastDotIndex == -1)
84+
{
8785
continue;
86+
}
8887

8988
var beforeLastDot = fullKey[..lastDotIndex];
9089
var memberName = fullKey[(lastDotIndex + 1)..];
@@ -95,7 +94,7 @@ private static string FormatSummary(SummaryDocumentationData data)
9594

9695
if (!componentsByType.TryGetValue(typeName, out var members))
9796
{
98-
members = new Dictionary<string, string>();
97+
members = [];
9998
componentsByType[typeName] = members;
10099
}
101100

@@ -109,14 +108,14 @@ private static string FormatSummary(SummaryDocumentationData data)
109108
sb.AppendLine(CultureInfo.InvariantCulture, $" \"{EscapeJson(typeEntry.Key)}\": {{");
110109

111110
var membersList = typeEntry.Value.OrderBy(x => x.Key).ToList();
112-
for (int i = 0; i < membersList.Count; i++)
111+
for (var i = 0; i < membersList.Count; i++)
113112
{
114113
var member = membersList[i];
115114
var isLast = i == membersList.Count - 1;
116115

117116
var escapedValue = EscapeJson(member.Value);
118117
sb.Append(CultureInfo.InvariantCulture, $" \"{EscapeJson(member.Key)}\": \"{escapedValue}\"");
119-
118+
120119
if (!isLast)
121120
{
122121
sb.AppendLine(",");

examples/Tools/FluentUI.Demo.DocApiGen/Generators/AllDocumentationGenerator.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,7 @@ public AllDocumentationGenerator(Assembly assembly, FileInfo xmlDocumentation)
3636
/// <inheritdoc/>
3737
public override string Generate(IOutputFormatter formatter)
3838
{
39-
if (formatter == null)
40-
{
41-
throw new ArgumentNullException(nameof(formatter));
42-
}
39+
ArgumentNullException.ThrowIfNull(formatter);
4340

4441
if (formatter.FormatName != "json")
4542
{

examples/Tools/FluentUI.Demo.DocApiGen/Generators/DocumentationGeneratorBase.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,7 @@ public virtual void SaveToFile(string filePath, IOutputFormatter formatter)
5252
throw new ArgumentException("File path cannot be null or empty.", nameof(filePath));
5353
}
5454

55-
if (formatter == null)
56-
{
57-
throw new ArgumentNullException(nameof(formatter));
58-
}
55+
ArgumentNullException.ThrowIfNull(formatter);
5956

6057
var output = Generate(formatter);
6158

examples/Tools/FluentUI.Demo.DocApiGen/Generators/DocumentationGeneratorFactory.cs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,9 @@ public static IDocumentationGenerator Create(
2626
Assembly assembly,
2727
FileInfo xmlDocumentation)
2828
{
29-
if (assembly == null)
30-
{
31-
throw new ArgumentNullException(nameof(assembly));
32-
}
29+
ArgumentNullException.ThrowIfNull(assembly);
3330

34-
if (xmlDocumentation == null)
35-
{
36-
throw new ArgumentNullException(nameof(xmlDocumentation));
37-
}
31+
ArgumentNullException.ThrowIfNull(xmlDocumentation);
3832

3933
return mode switch
4034
{

examples/Tools/FluentUI.Demo.DocApiGen/Generators/SummaryDocumentationGenerator.cs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,7 @@ public SummaryDocumentationGenerator(Assembly assembly, FileInfo xmlDocumentatio
3535
/// <inheritdoc/>
3636
public override string Generate(IOutputFormatter formatter)
3737
{
38-
if (formatter == null)
39-
{
40-
throw new ArgumentNullException(nameof(formatter));
41-
}
38+
ArgumentNullException.ThrowIfNull(formatter);
4239

4340
var data = BuildDocumentationData();
4441
return formatter.Format(data);
@@ -124,13 +121,13 @@ private SummaryDocumentationData BuildDocumentationData()
124121
private (string Version, string Date) GetAssemblyInfo()
125122
{
126123
var version = "Unknown";
127-
124+
128125
var versionAttribute = Assembly.GetCustomAttribute<AssemblyInformationalVersionAttribute>();
129126
if (versionAttribute != null)
130127
{
131128
var versionString = versionAttribute.InformationalVersion;
132129
var plusIndex = versionString.IndexOf('+');
133-
130+
134131
if (plusIndex >= 0 && plusIndex + 9 < versionString.Length)
135132
{
136133
version = versionString[..(plusIndex + 9)];
@@ -142,7 +139,7 @@ private SummaryDocumentationData BuildDocumentationData()
142139
}
143140

144141
var date = DateTime.UtcNow.ToString("yyyy-MM-dd HH:mm", CultureInfo.InvariantCulture);
145-
142+
146143
return (version, date);
147144
}
148145
}

examples/Tools/FluentUI.Demo.DocApiGen/ReadMe.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,5 +57,3 @@ The tool supports different JSON formats depending on the generation mode:
5757
- CamelCase property names
5858
- Full type names with namespaces
5959
- Includes properties, methods, events, and enums
60-
61-
See [GENERATION_MODES.md](GENERATION_MODES.md) for detailed documentation about generation modes and formats.

0 commit comments

Comments
 (0)