Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use collection expression instead of Array.Empty #8084

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
use collection expression instead of Array.Empty
SimonCropp committed Jun 11, 2024
commit bea116078e80bd89d361a51662b73f22fa16989d
Original file line number Diff line number Diff line change
@@ -16,7 +16,7 @@ private CheckUpdateResult(string? latestVersion, bool isLatest, IManagedTemplate
TemplatePackage = templatePackage;
LatestVersion = latestVersion;
IsLatestVersion = isLatest;
Vulnerabilities = Array.Empty<VulnerabilityInfo>();
Vulnerabilities = [];
}

private CheckUpdateResult(
Original file line number Diff line number Diff line change
@@ -61,7 +61,7 @@ public ParameterSetData(ITemplateInfo templateInfo, IReadOnlyDictionary<string,
/// Empty instance.
/// </summary>
public static IParameterSetData Empty =>
new ParameterSetData(new ParameterDefinitionSet((IReadOnlyDictionary<string, ITemplateParameter>?)null), System.Array.Empty<ParameterData>());
new ParameterSetData(new ParameterDefinitionSet((IReadOnlyDictionary<string, ITemplateParameter>?)null), []);

/// <inheritdoc/>
public IParameterDefinitionSet ParametersDefinition { get; }
Original file line number Diff line number Diff line change
@@ -16,7 +16,7 @@ public static class CppStyleEvaluatorDefinition
{
private const int ReservedTokenCount = 24;
private const int ReservedTokenMaxIndex = ReservedTokenCount - 1;
private static readonly IOperationProvider[] NoOperationProviders = Array.Empty<IOperationProvider>();
private static readonly IOperationProvider[] NoOperationProviders = [];
private static readonly char[] SupportedQuotes = { '"', '\'' };

public static bool EvaluateFromString(ILogger logger, string text, IVariableCollection variables)
Original file line number Diff line number Diff line change
@@ -18,7 +18,7 @@ public abstract class SharedEvaluatorDefinition<TSelf, TTokens>
private static readonly IOperatorMap<Operators, TTokens> Map = Instance.GenerateMap();
private static readonly bool DereferenceInLiteralsSetting = Instance.DereferenceInLiterals;
private static readonly string NullToken = Instance.NullTokenValue;
private static readonly IOperationProvider[] NoOperationProviders = Array.Empty<IOperationProvider>();
private static readonly IOperationProvider[] NoOperationProviders = [];

protected abstract string NullTokenValue { get; }

2 changes: 1 addition & 1 deletion src/Microsoft.TemplateEngine.Core/FileChange.cs
Original file line number Diff line number Diff line change
@@ -12,7 +12,7 @@ public FileChange(string sourceRelativePath, string targetRelativePath, ChangeKi
SourceRelativePath = sourceRelativePath;
TargetRelativePath = targetRelativePath;
ChangeKind = changeKind;
Contents = contents ?? Array.Empty<byte>();
Contents = contents ?? [];
}

public string SourceRelativePath { get; }
Original file line number Diff line number Diff line change
@@ -9,14 +9,14 @@ public class ConditionalTokens
{
public ConditionalTokens()
{
IfTokens = Array.Empty<ITokenConfig>();
ElseTokens = Array.Empty<ITokenConfig>();
ElseIfTokens = Array.Empty<ITokenConfig>();
EndIfTokens = Array.Empty<ITokenConfig>();
ActionableIfTokens = Array.Empty<ITokenConfig>();
ActionableElseTokens = Array.Empty<ITokenConfig>();
ActionableElseIfTokens = Array.Empty<ITokenConfig>();
ActionableOperations = Array.Empty<string>();
IfTokens = [];
ElseTokens = [];
ElseIfTokens = [];
EndIfTokens = [];
ActionableIfTokens = [];
ActionableElseTokens = [];
ActionableElseIfTokens = [];
ActionableOperations = [];
}

public IReadOnlyList<ITokenConfig> IfTokens { get; set; }
Original file line number Diff line number Diff line change
@@ -106,7 +106,7 @@ public int HandleMatch(IProcessorState processor, int bufferLength, ref int curr
ScanToCloseCondition(processor, conditionBytes, ref bufferLength, ref currentBufferPosition);
byte[] condition = conditionBytes.ToArray();
EngineConfig adjustedConfig = new EngineConfig(processor.Config.Logger, processor.Config.Whitespaces, processor.Config.LineEndings, processor.Config.Variables, _definition.VariableFormat);
IProcessorState localState = new ProcessorState(new MemoryStream(condition), new MemoryStream(), conditionBytes.Count, int.MaxValue, adjustedConfig, Array.Empty<IOperationProvider>());
IProcessorState localState = new ProcessorState(new MemoryStream(condition), new MemoryStream(), conditionBytes.Count, int.MaxValue, adjustedConfig, []);
int pos = 0;
int len = conditionBytes.Count;

6 changes: 3 additions & 3 deletions src/Microsoft.TemplateEngine.Core/TokenConfig.cs
Original file line number Diff line number Diff line change
@@ -44,9 +44,9 @@ public TokenConfig OnlyIfBefore(string? suffix)

public IToken ToToken(Encoding encoding)
{
byte[] pre = string.IsNullOrEmpty(After) ? Array.Empty<byte>() : encoding.GetBytes(After);
byte[] post = string.IsNullOrEmpty(Before) ? Array.Empty<byte>() : encoding.GetBytes(Before);
byte[] core = string.IsNullOrEmpty(Value) ? Array.Empty<byte>() : encoding.GetBytes(Value);
byte[] pre = string.IsNullOrEmpty(After) ? [] : encoding.GetBytes(After);
byte[] post = string.IsNullOrEmpty(Before) ? [] : encoding.GetBytes(Before);
byte[] core = string.IsNullOrEmpty(Value) ? [] : encoding.GetBytes(Value);

byte[] buffer = new byte[pre.Length + core.Length + post.Length];

4 changes: 2 additions & 2 deletions src/Microsoft.TemplateEngine.Core/Util/EncodingUtil.cs
Original file line number Diff line number Diff line change
@@ -16,7 +16,7 @@ public static Encoding Detect(byte[] buffer, int currentBufferLength, out byte[]
if (currentBufferLength == 0)
{
//File is zero length - pick something
bom = System.Array.Empty<byte>();
bom = [];
return Encoding.UTF8;
}

@@ -65,7 +65,7 @@ public static Encoding Detect(byte[] buffer, int currentBufferLength, out byte[]
}

//Fallback to UTF-8
bom = System.Array.Empty<byte>();
bom = [];
return Encoding.UTF8;
}
}
2 changes: 1 addition & 1 deletion src/Microsoft.TemplateEngine.Core/VariableCollection.cs
Original file line number Diff line number Diff line change
@@ -9,7 +9,7 @@ namespace Microsoft.TemplateEngine.Core
{
public class VariableCollection : IVariableCollection, IMonitoredVariableCollection
{
private static readonly IEnumerable<string> NoKeys = Array.Empty<string>();
private static readonly IEnumerable<string> NoKeys = [];
private readonly IDictionary<string, object> _values;
private IVariableCollection? _parent;

Original file line number Diff line number Diff line change
@@ -72,7 +72,7 @@ public async Task<IReadOnlyList<TemplatePackageData>> GetInstalledTemplatePackag

if (!_environmentSettings.Host.FileSystem.FileExists(_globalSettingsFile))
{
return Array.Empty<TemplatePackageData>();
return [];
}

for (int i = 0; i < FileReadWriteRetries; i++)
Original file line number Diff line number Diff line change
@@ -137,15 +137,15 @@ public async Task<IReadOnlyList<InstallResult>> InstallAsync(IEnumerable<Install
installRequest,
InstallerErrorCode.UnsupportedRequest,
string.Format(LocalizableStrings.GlobalSettingsTemplatePackageProvider_InstallResult_Error_PackageCannotBeInstalled, installRequest.PackageIdentifier),
Array.Empty<VulnerabilityInfo>());
[]);
}
if (installersThatCanInstall.Count > 1)
{
return InstallResult.CreateFailure(
installRequest,
InstallerErrorCode.UnsupportedRequest,
string.Format(LocalizableStrings.GlobalSettingsTemplatePackageProvider_InstallResult_Error_MultipleInstallersCanBeUsed, installRequest.PackageIdentifier),
Array.Empty<VulnerabilityInfo>());
[]);
}

IInstaller installer = installersThatCanInstall[0];
@@ -220,7 +220,7 @@ private async Task<UpdateResult> UpdateAsync(List<TemplatePackageData> packages,
(InstallerErrorCode result, string message) = await EnsureInstallPrerequisites(packages, updateRequest.TemplatePackage.Identifier, updateRequest.Version, updateRequest.TemplatePackage.Installer, cancellationToken, update: true).ConfigureAwait(false);
if (result != InstallerErrorCode.Success)
{
return UpdateResult.CreateFailure(updateRequest, result, message, Array.Empty<VulnerabilityInfo>());
return UpdateResult.CreateFailure(updateRequest, result, message, []);
}

UpdateResult updateResult = await updateRequest.TemplatePackage.Installer.UpdateAsync(updateRequest, provider: this, cancellationToken).ConfigureAwait(false);
@@ -302,7 +302,7 @@ private async Task<InstallResult> InstallAsync(List<TemplatePackageData> package
forceUpdate: installRequest.Force).ConfigureAwait(false);
if (result != InstallerErrorCode.Success)
{
return InstallResult.CreateFailure(installRequest, result, message, Array.Empty<VulnerabilityInfo>());
return InstallResult.CreateFailure(installRequest, result, message, []);
}

InstallResult installResult = await installer.InstallAsync(installRequest, this, cancellationToken).ConfigureAwait(false);
Original file line number Diff line number Diff line change
@@ -11,7 +11,7 @@ namespace Microsoft.TemplateEngine.Edge
{
public class DefaultTemplateEngineHost : ITemplateEngineHost
{
private static readonly IReadOnlyList<(Type, IIdentifiedComponent)> NoComponents = Array.Empty<(Type, IIdentifiedComponent)>();
private static readonly IReadOnlyList<(Type, IIdentifiedComponent)> NoComponents = [];
private readonly IReadOnlyDictionary<string, string> _hostDefaults;
private readonly IReadOnlyList<(Type InterfaceType, IIdentifiedComponent Instance)> _hostBuiltInComponents;
private readonly ILoggerFactory _loggerFactory;
4 changes: 2 additions & 2 deletions src/Microsoft.TemplateEngine.Edge/FilterableTemplateInfo.cs
Original file line number Diff line number Diff line change
@@ -74,9 +74,9 @@ private FilterableTemplateInfo(ITemplateInfo source)

public IReadOnlyDictionary<string, string> TagsCollection { get; private set; }

IReadOnlyList<Guid> ITemplateMetadata.PostActions => _source?.PostActions ?? Array.Empty<Guid>();
IReadOnlyList<Guid> ITemplateMetadata.PostActions => _source?.PostActions ?? [];

IReadOnlyList<TemplateConstraintInfo> ITemplateMetadata.Constraints => _source?.Constraints ?? Array.Empty<TemplateConstraintInfo>();
IReadOnlyList<TemplateConstraintInfo> ITemplateMetadata.Constraints => _source?.Constraints ?? [];

public static FilterableTemplateInfo FromITemplateInfo(ITemplateInfo source)
{
Original file line number Diff line number Diff line change
@@ -56,7 +56,7 @@ public Task<InstallResult> InstallAsync(InstallRequest installRequest, IManagedT
return Task.FromResult(InstallResult.CreateSuccess(
installRequest,
new FolderManagedTemplatePackage(_settings, this, provider, installRequest.PackageIdentifier, DateTime.UtcNow),
Array.Empty<VulnerabilityInfo>()));
[]));
}
else
{
@@ -65,7 +65,7 @@ public Task<InstallResult> InstallAsync(InstallRequest installRequest, IManagedT
installRequest,
InstallerErrorCode.PackageNotFound,
string.Format(LocalizableStrings.FolderInstaller_InstallResult_Error_FolderDoesNotExist, installRequest.PackageIdentifier),
Array.Empty<VulnerabilityInfo>()));
[]));
}
}

@@ -98,7 +98,7 @@ public Task<UpdateResult> UpdateAsync(UpdateRequest updateRequest, IManagedTempl
return Task.FromResult(UpdateResult.CreateSuccess(
updateRequest,
new FolderManagedTemplatePackage(_settings, this, provider, updateRequest.TemplatePackage.Identifier, DateTime.UtcNow),
Array.Empty<VulnerabilityInfo>()));
[]));
}
}
}
Original file line number Diff line number Diff line change
@@ -143,7 +143,7 @@ public async Task<IReadOnlyList<CheckUpdateResult>> GetLatestVersionAsync(
package,
InstallerErrorCode.PackageNotFound,
string.Format(LocalizableStrings.NuGetInstaller_Error_FailedToReadPackage, e.PackageIdentifier, string.Join(", ", e.SourcesList)),
Array.Empty<VulnerabilityInfo>());
[]);
}
catch (InvalidNuGetSourceException e)
{
@@ -155,15 +155,15 @@ public async Task<IReadOnlyList<CheckUpdateResult>> GetLatestVersionAsync(
package,
InstallerErrorCode.InvalidSource,
message,
Array.Empty<VulnerabilityInfo>());
[]);
}
catch (OperationCanceledException)
{
return CheckUpdateResult.CreateFailure(
package,
InstallerErrorCode.GenericError,
LocalizableStrings.NuGetInstaller_InstallResult_Error_OperationCancelled,
Array.Empty<VulnerabilityInfo>());
[]);
}
catch (VulnerablePackageException e)
{
@@ -180,7 +180,7 @@ public async Task<IReadOnlyList<CheckUpdateResult>> GetLatestVersionAsync(
package,
InstallerErrorCode.GenericError,
string.Format(LocalizableStrings.NuGetInstaller_InstallResult_Error_UpdateCheckGeneric, package.DisplayName, e.Message),
Array.Empty<VulnerabilityInfo>());
[]);
}
}
else
@@ -189,7 +189,7 @@ public async Task<IReadOnlyList<CheckUpdateResult>> GetLatestVersionAsync(
package,
InstallerErrorCode.UnsupportedRequest,
string.Format(LocalizableStrings.NuGetInstaller_InstallResult_Error_PackageNotSupported, package.DisplayName, Factory.Name),
Array.Empty<VulnerabilityInfo>());
[]);
}
})).ConfigureAwait(false);
}
@@ -205,7 +205,7 @@ public async Task<InstallResult> InstallAsync(InstallRequest installRequest, IMa
installRequest,
InstallerErrorCode.UnsupportedRequest,
string.Format(LocalizableStrings.NuGetInstaller_InstallResult_Error_PackageNotSupported, installRequest.DisplayName, Factory.Name),
Array.Empty<VulnerabilityInfo>());
[]);
}

try
@@ -218,7 +218,7 @@ public async Task<InstallResult> InstallAsync(InstallRequest installRequest, IMa
}
else
{
string[] additionalNuGetSources = Array.Empty<string>();
string[] additionalNuGetSources = [];
if (installRequest.Details != null && installRequest.Details.TryGetValue(InstallerConstants.NuGetSourcesKey, out string nugetSources))
{
additionalNuGetSources = nugetSources.Split(InstallerConstants.NuGetSourcesSeparator);
@@ -261,15 +261,15 @@ public async Task<InstallResult> InstallAsync(InstallRequest installRequest, IMa
installRequest,
InstallerErrorCode.DownloadFailed,
string.Format(LocalizableStrings.NuGetInstaller_InstallResult_Error_DownloadFailed, installRequest.DisplayName, packageLocation),
Array.Empty<VulnerabilityInfo>());
[]);
}
catch (PackageNotFoundException e)
{
return InstallResult.CreateFailure(
installRequest,
InstallerErrorCode.PackageNotFound,
string.Format(LocalizableStrings.NuGetInstaller_Error_FailedToReadPackage, e.PackageIdentifier, string.Join(", ", e.SourcesList)),
Array.Empty<VulnerabilityInfo>());
[]);
}
catch (InvalidNuGetSourceException e)
{
@@ -281,15 +281,15 @@ public async Task<InstallResult> InstallAsync(InstallRequest installRequest, IMa
installRequest,
InstallerErrorCode.InvalidSource,
message,
Array.Empty<VulnerabilityInfo>());
[]);
}
catch (InvalidNuGetPackageException e)
{
return InstallResult.CreateFailure(
installRequest,
InstallerErrorCode.InvalidPackage,
string.Format(LocalizableStrings.NuGetInstaller_InstallResult_Error_InvalidPackage, e.PackageLocation),
Array.Empty<VulnerabilityInfo>());
[]);
}
catch (VulnerablePackageException e)
{
@@ -305,7 +305,7 @@ public async Task<InstallResult> InstallAsync(InstallRequest installRequest, IMa
installRequest,
InstallerErrorCode.GenericError,
LocalizableStrings.NuGetInstaller_InstallResult_Error_OperationCancelled,
Array.Empty<VulnerabilityInfo>());
[]);
}
catch (Exception e)
{
@@ -314,7 +314,7 @@ public async Task<InstallResult> InstallAsync(InstallRequest installRequest, IMa
installRequest,
InstallerErrorCode.GenericError,
string.Format(LocalizableStrings.NuGetInstaller_InstallResult_Error_InstallGeneric, installRequest.DisplayName, e.Message),
Array.Empty<VulnerabilityInfo>());
[]);
}
}

@@ -374,7 +374,7 @@ public async Task<UpdateResult> UpdateAsync(UpdateRequest updateRequest, IManage
{
throw new InvalidOperationException($"{nameof(uninstallResult.ErrorMessage)} cannot be null when {nameof(uninstallResult.Success)} is 'true'");
}
return UpdateResult.CreateFailure(updateRequest, uninstallResult.Error, uninstallResult.ErrorMessage, Array.Empty<VulnerabilityInfo>());
return UpdateResult.CreateFailure(updateRequest, uninstallResult.Error, uninstallResult.ErrorMessage, []);
}

Dictionary<string, string> installationDetails = new Dictionary<string, string>();
@@ -443,7 +443,7 @@ private NuGetPackageInfo ReadPackageInformation(string packageLocation)
null,
nuspec.GetId(),
nuspec.GetVersion().ToNormalizedString(),
Array.Empty<VulnerabilityInfo>());
[]);
}
}
}
Loading
Loading