From c311ba03ac1798aa6fa760a07dcff0cdd7fafe74 Mon Sep 17 00:00:00 2001 From: Sipke Schoorstra Date: Sat, 23 Nov 2024 15:58:19 +0100 Subject: [PATCH] Refactor syntax for new array initialization Updated new array initialization syntax from `new[] {}` to `[]` across multiple files for consistency and code brevity. This change enhances readability and aligns with modern C# conventions. --- .../Elsa.Workflows.Core/Abstractions/Trigger.cs | 4 ++-- .../Elsa.Workflows.Core/Activities/Complete.cs | 2 +- .../Contexts/ActivityExecutionContext.cs | 2 +- .../Extensions/ActivityPropertyExtensions.cs | 6 +++--- ...BackgroundActivityExecutionContextExtensions.cs | 2 +- .../ExpressionExecutionContextExtensions.cs | 10 ++++------ .../Configurators/CustomConstructorConfigurator.cs | 4 ++-- .../Services/ActivityVisitor.cs | 14 ++++++-------- .../UIHints/CheckList/CheckListUIHintHandler.cs | 2 +- .../UIHints/Dropdown/DropDownUIHintHandler.cs | 2 +- .../UIHints/JsonEditor/JsonEditorUIHintHandler.cs | 2 +- 11 files changed, 23 insertions(+), 27 deletions(-) diff --git a/src/modules/Elsa.Workflows.Core/Abstractions/Trigger.cs b/src/modules/Elsa.Workflows.Core/Abstractions/Trigger.cs index d0bc6e26f..f26460b85 100644 --- a/src/modules/Elsa.Workflows.Core/Abstractions/Trigger.cs +++ b/src/modules/Elsa.Workflows.Core/Abstractions/Trigger.cs @@ -29,7 +29,7 @@ public abstract class Trigger : Activity, ITrigger /// /// Override this method to return trigger data. /// - protected virtual IEnumerable GetTriggerPayloads(TriggerIndexingContext context) => new[] { GetTriggerPayload(context) }; + protected virtual IEnumerable GetTriggerPayloads(TriggerIndexingContext context) => [GetTriggerPayload(context)]; /// /// Override this method to return a trigger datum. @@ -61,7 +61,7 @@ public abstract class Trigger : Activity, ITrigger /// /// Override this method to return a trigger payload. /// - protected virtual IEnumerable GetTriggerPayloads(TriggerIndexingContext context) => new[] { GetTriggerPayload(context) }; + protected virtual IEnumerable GetTriggerPayloads(TriggerIndexingContext context) => [GetTriggerPayload(context)]; /// /// Override this method to return a trigger payload. diff --git a/src/modules/Elsa.Workflows.Core/Activities/Complete.cs b/src/modules/Elsa.Workflows.Core/Activities/Complete.cs index 7cd9b3eaa..7d6f3cecd 100644 --- a/src/modules/Elsa.Workflows.Core/Activities/Complete.cs +++ b/src/modules/Elsa.Workflows.Core/Activities/Complete.cs @@ -37,7 +37,7 @@ public class Complete : Activity, ITerminalNode /// public Complete(Func outcome, [CallerFilePath] string? source = default, [CallerLineNumber] int? line = default) - : this(context => new[] { outcome(context) }, source, line) + : this(context => [outcome(context)], source, line) { } diff --git a/src/modules/Elsa.Workflows.Core/Contexts/ActivityExecutionContext.cs b/src/modules/Elsa.Workflows.Core/Contexts/ActivityExecutionContext.cs index ce457bc0d..f545b0881 100644 --- a/src/modules/Elsa.Workflows.Core/Contexts/ActivityExecutionContext.cs +++ b/src/modules/Elsa.Workflows.Core/Contexts/ActivityExecutionContext.cs @@ -19,7 +19,7 @@ namespace Elsa.Workflows; public partial class ActivityExecutionContext : IExecutionContext, IDisposable { private readonly ISystemClock _systemClock; - private readonly List _bookmarks = new(); + private readonly List _bookmarks = []; private long _executionCount; /// diff --git a/src/modules/Elsa.Workflows.Core/Extensions/ActivityPropertyExtensions.cs b/src/modules/Elsa.Workflows.Core/Extensions/ActivityPropertyExtensions.cs index 387822f05..4d657080a 100644 --- a/src/modules/Elsa.Workflows.Core/Extensions/ActivityPropertyExtensions.cs +++ b/src/modules/Elsa.Workflows.Core/Extensions/ActivityPropertyExtensions.cs @@ -8,9 +8,9 @@ namespace Elsa.Extensions; /// public static class ActivityPropertyExtensions { - private static readonly string[] CanStartWorkflowPropertyName = {"canStartWorkflow", "CanStartWorkflow", }; - private static readonly string[] RunAsynchronouslyPropertyName = {"runAsynchronously", "RunAsynchronously" }; - private static readonly string[] SourcePropertyName = {"source", "Source"}; + private static readonly string[] CanStartWorkflowPropertyName = ["canStartWorkflow", "CanStartWorkflow"]; + private static readonly string[] RunAsynchronouslyPropertyName = ["runAsynchronously", "RunAsynchronously"]; + private static readonly string[] SourcePropertyName = ["source", "Source"]; /// /// Gets a flag indicating whether this activity can be used for starting a workflow. diff --git a/src/modules/Elsa.Workflows.Core/Extensions/BackgroundActivityExecutionContextExtensions.cs b/src/modules/Elsa.Workflows.Core/Extensions/BackgroundActivityExecutionContextExtensions.cs index d0c1d6388..a446f237b 100644 --- a/src/modules/Elsa.Workflows.Core/Extensions/BackgroundActivityExecutionContextExtensions.cs +++ b/src/modules/Elsa.Workflows.Core/Extensions/BackgroundActivityExecutionContextExtensions.cs @@ -76,6 +76,6 @@ public static class BackgroundActivityExecutionContextExtensions /// public static IEnumerable GetBackgroundScheduledActivities(this ActivityExecutionContext activityExecutionContext) { - return activityExecutionContext.GetProperty>("BackgroundScheduledActivities") ?? Enumerable.Empty(); + return activityExecutionContext.GetProperty>("BackgroundScheduledActivities") ?? []; } } \ No newline at end of file diff --git a/src/modules/Elsa.Workflows.Core/Extensions/ExpressionExecutionContextExtensions.cs b/src/modules/Elsa.Workflows.Core/Extensions/ExpressionExecutionContextExtensions.cs index 8614afb6f..a88e84498 100644 --- a/src/modules/Elsa.Workflows.Core/Extensions/ExpressionExecutionContextExtensions.cs +++ b/src/modules/Elsa.Workflows.Core/Extensions/ExpressionExecutionContextExtensions.cs @@ -464,10 +464,9 @@ public static class ExpressionExecutionContextExtensions foreach (var output in activityDescriptor.Outputs) { var outputPascalName = output.Name.Pascalize(); - yield return new ActivityOutputs(activity.Id, activityIdPascalName, new[] - { + yield return new ActivityOutputs(activity.Id, activityIdPascalName, [ outputPascalName - }); + ]); } } } @@ -548,9 +547,8 @@ public static class ExpressionExecutionContextExtensions return obj; var toArrayMethod = typeof(Enumerable).GetMethod("ToArray")!.MakeGenericMethod(elementType); - return toArrayMethod.Invoke(null, new object[] - { + return toArrayMethod.Invoke(null, [ enumerable - })!; + ])!; } } \ No newline at end of file diff --git a/src/modules/Elsa.Workflows.Core/Serialization/Configurators/CustomConstructorConfigurator.cs b/src/modules/Elsa.Workflows.Core/Serialization/Configurators/CustomConstructorConfigurator.cs index 92baedf3d..ff90636eb 100644 --- a/src/modules/Elsa.Workflows.Core/Serialization/Configurators/CustomConstructorConfigurator.cs +++ b/src/modules/Elsa.Workflows.Core/Serialization/Configurators/CustomConstructorConfigurator.cs @@ -34,7 +34,7 @@ public class CustomConstructorConfigurator : SerializationOptionsConfiguratorBas { // If we have a default constructor, use that one. if (!constructor.GetParameters().Any()) - return () => constructor.Invoke(default, Array.Empty())!; + return () => constructor.Invoke(default, [])!; // Else, find a constructor with the following signature: (string?, int?). // Check for a constructor with the following signature: @@ -57,7 +57,7 @@ public class CustomConstructorConfigurator : SerializationOptionsConfiguratorBas parameters[1].DefaultValue != null || (parameters[1].GetCustomAttribute() == null && !isJsonConstructor)) continue; - return () => constructor.Invoke(new object[] { null!, 0 }); + return () => constructor.Invoke([null!, 0]); } return null; diff --git a/src/modules/Elsa.Workflows.Core/Services/ActivityVisitor.cs b/src/modules/Elsa.Workflows.Core/Services/ActivityVisitor.cs index 939621c97..fcdb66519 100644 --- a/src/modules/Elsa.Workflows.Core/Services/ActivityVisitor.cs +++ b/src/modules/Elsa.Workflows.Core/Services/ActivityVisitor.cs @@ -21,14 +21,12 @@ public class ActivityVisitor : IActivityVisitor public async Task VisitAsync(IActivity activity, CancellationToken cancellationToken = default) { var graph = new ActivityNode(activity, "Root"); - var collectedNodes = new HashSet(new[] - { + var collectedNodes = new HashSet([ graph - }); - var collectedActivities = new HashSet(new[] - { + ]); + var collectedActivities = new HashSet([ activity - }); + ]); var visitorContext = new ActivityVisitorContext { CollectedActivities = collectedActivities, @@ -87,7 +85,7 @@ public class ActivityVisitor : IActivityVisitor private class ActivityVisitorContext { - public HashSet CollectedActivities { get; set; } = new(); - public HashSet CollectedNodes { get; set; } = new(); + public HashSet CollectedActivities { get; set; } = []; + public HashSet CollectedNodes { get; set; } = []; } } \ No newline at end of file diff --git a/src/modules/Elsa.Workflows.Core/UIHints/CheckList/CheckListUIHintHandler.cs b/src/modules/Elsa.Workflows.Core/UIHints/CheckList/CheckListUIHintHandler.cs index af5c758ed..1a065ce4b 100644 --- a/src/modules/Elsa.Workflows.Core/UIHints/CheckList/CheckListUIHintHandler.cs +++ b/src/modules/Elsa.Workflows.Core/UIHints/CheckList/CheckListUIHintHandler.cs @@ -11,6 +11,6 @@ public class CheckListUIHintHandler : IUIHintHandler /// public ValueTask> GetPropertyUIHandlersAsync(PropertyInfo propertyInfo, CancellationToken cancellationToken) { - return new(new[] { typeof(StaticCheckListOptionsProvider) }); + return new([typeof(StaticCheckListOptionsProvider)]); } } \ No newline at end of file diff --git a/src/modules/Elsa.Workflows.Core/UIHints/Dropdown/DropDownUIHintHandler.cs b/src/modules/Elsa.Workflows.Core/UIHints/Dropdown/DropDownUIHintHandler.cs index 7b202360e..0a96b2054 100644 --- a/src/modules/Elsa.Workflows.Core/UIHints/Dropdown/DropDownUIHintHandler.cs +++ b/src/modules/Elsa.Workflows.Core/UIHints/Dropdown/DropDownUIHintHandler.cs @@ -11,6 +11,6 @@ public class DropDownUIHintHandler : IUIHintHandler /// public ValueTask> GetPropertyUIHandlersAsync(PropertyInfo propertyInfo, CancellationToken cancellationToken) { - return new(new[] { typeof(StaticDropDownOptionsProvider) }); + return new([typeof(StaticDropDownOptionsProvider)]); } } \ No newline at end of file diff --git a/src/modules/Elsa.Workflows.Core/UIHints/JsonEditor/JsonEditorUIHintHandler.cs b/src/modules/Elsa.Workflows.Core/UIHints/JsonEditor/JsonEditorUIHintHandler.cs index 9a7b9f394..d8033383a 100644 --- a/src/modules/Elsa.Workflows.Core/UIHints/JsonEditor/JsonEditorUIHintHandler.cs +++ b/src/modules/Elsa.Workflows.Core/UIHints/JsonEditor/JsonEditorUIHintHandler.cs @@ -12,6 +12,6 @@ public class JsonEditorUIHintHandler : IUIHintHandler /// public ValueTask> GetPropertyUIHandlersAsync(PropertyInfo propertyInfo, CancellationToken cancellationToken) { - return new(new[] { typeof(JsonCodeOptionsProvider) }); + return new([typeof(JsonCodeOptionsProvider)]); } } \ No newline at end of file