Skip to content

Commit c55d88d

Browse files
committed
Use string.IsNullOrWhiteSpace instead of null check or IsNullOrEmpty
1 parent 6ecdbb8 commit c55d88d

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed

Flow.Launcher.Localization.SourceGenerators/Localize/EnumSourceGenerator.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,8 @@ private static ImmutableArray<EnumField> GetEnumFields(SourceProductionContext s
131131
var key = keyAttr?.ConstructorArguments.FirstOrDefault().Value?.ToString() ?? string.Empty;
132132
var value = valueAttr?.ConstructorArguments.FirstOrDefault().Value?.ToString() ?? string.Empty;
133133

134-
if (keyAttrExist && !string.IsNullOrEmpty(key))
134+
// Users may use " " as a key, so we need to check if the key is not empty and not whitespace
135+
if (keyAttrExist && !string.IsNullOrWhiteSpace(key))
135136
{
136137
// If localization key exists and is valid, use it
137138
enumFields.Add(new EnumField(enumFieldName, key, valueAttrExist ? value : null));
@@ -316,7 +317,8 @@ private static void GenerateUpdateLabelsMethod(
316317
sb.AppendLine($"{tabString}{{");
317318
sb.AppendLine($"{tabString}{tabString}foreach (var item in options)");
318319
sb.AppendLine($"{tabString}{tabString}{{");
319-
sb.AppendLine($"{tabString}{tabString}{tabString}if (!string.IsNullOrEmpty(item.LocalizationKey))");
320+
// Users may use " " as a key, so we need to check if the key is not empty and not whitespace
321+
sb.AppendLine($"{tabString}{tabString}{tabString}if (!string.IsNullOrWhiteSpace(item.LocalizationKey))");
320322
sb.AppendLine($"{tabString}{tabString}{tabString}{{");
321323
sb.AppendLine($"{tabString}{tabString}{tabString}{tabString}item.Display = {getTranslation}(item.LocalizationKey);");
322324
sb.AppendLine($"{tabString}{tabString}{tabString}}}");
@@ -334,7 +336,8 @@ public class EnumField
334336
public string LocalizationKey { get; set; }
335337
public string LocalizationValue { get; set; }
336338

337-
public bool UseLocalizationKey => LocalizationKey != null;
339+
// Users may use " " as a key, so we need to check if the key is not empty and not whitespace
340+
public bool UseLocalizationKey => !string.IsNullOrWhiteSpace(LocalizationKey);
338341

339342
public EnumField(string enumFieldName, string localizationValue) : this(enumFieldName, null, localizationValue)
340343
{

0 commit comments

Comments
 (0)