Skip to content

Commit

Permalink
Adds null checks
Browse files Browse the repository at this point in the history
  • Loading branch information
Shazwazza committed Oct 21, 2024
1 parent d5930c9 commit 76c7ad1
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 17 deletions.
19 changes: 5 additions & 14 deletions src/Smidge.Core/BundleManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -133,29 +133,22 @@ public bool TryGetValue(string key, out Bundle value)
/// <param name="type"></param>
/// <returns></returns>
public IEnumerable<string> GetBundleNames(WebFileType type)
{
return _bundles.Where(x => x.Value.Files.Any(f => f.DependencyType == type)).Select(x => x.Key);
}
=> _bundles.Where(x => x.Value.Files.Any(f => f.DependencyType == type)).Select(x => x.Key);

/// <summary>
/// Returns all bundles registered
/// </summary>
/// <param name="type"></param>
/// <returns></returns>
public IEnumerable<Bundle> GetBundles(WebFileType type)
{
return _bundles.Where(x => x.Value.Files.Any(f => f.DependencyType == type)).Select(x => x.Value);
}
=> _bundles.Where(x => x.Value.Files.Any(f => f.DependencyType == type)).Select(x => x.Value);

/// <summary>
/// Checks if the bundle exists by name
/// </summary>
/// <param name="bundleName"></param>
/// <returns></returns>
public bool Exists(string bundleName)
{
return TryGetValue(bundleName, out _);
}
public bool Exists(string bundleName) => TryGetValue(bundleName, out _);

/// <summary>
/// Adds an item to the bundle, if the bundle doesn't exist it will be created
Expand All @@ -164,8 +157,7 @@ public bool Exists(string bundleName)
/// <param name="file"></param>
public void AddToBundle(string bundleName, CssFile file)
{
Bundle collection;
if (TryGetValue(bundleName, out collection))
if (TryGetValue(bundleName, out Bundle collection))
{
_logger.LogDebug($"Adding {WebFileType.Css} file '{file.FilePath}' to bundle '{bundleName}'");
collection.Files.Add(file);
Expand All @@ -183,8 +175,7 @@ public void AddToBundle(string bundleName, CssFile file)
/// <param name="file"></param>
public void AddToBundle(string bundleName, JavaScriptFile file)
{
Bundle collection;
if (TryGetValue(bundleName, out collection))
if (TryGetValue(bundleName, out Bundle collection))
{
_logger.LogDebug($"Adding {WebFileType.Js} file '{file.FilePath}' to bundle '{bundleName}'");
collection.Files.Add(file);
Expand Down
38 changes: 35 additions & 3 deletions src/Smidge/SmidgeRequire.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Linq;
using Smidge.Models;

namespace Smidge
Expand All @@ -23,10 +24,20 @@ public SmidgeRequire(string bundleName, IBundleManager bundleManager, WebFileTyp

public ISmidgeRequire RequiresJs(JavaScriptFile file)
{
if (file is null)
{
throw new ArgumentNullException(nameof(file));
}

if (_type == WebFileType.Css)
{
throw new InvalidOperationException("Cannot add css file to a js bundle");
}

if (_requestHelper.IsExternalRequestPath(file.FilePath))
{
throw new InvalidOperationException("Cannot process an external file as part of a bundle");
}

_bundleManager.AddToBundle(_bundleName, file);
return this;
Expand All @@ -35,39 +46,60 @@ public ISmidgeRequire RequiresJs(JavaScriptFile file)
public ISmidgeRequire RequiresJs(params string[] paths)
{
if (_type == WebFileType.Css)
{
throw new InvalidOperationException("Cannot add css file to a js bundle");
}

foreach (var path in paths)
foreach (var path in paths.Where(p => !string.IsNullOrWhiteSpace(p)))
{
if (_requestHelper.IsExternalRequestPath(path))
{
throw new InvalidOperationException("Cannot process an external file as part of a bundle");
}

_bundleManager.AddToBundle(_bundleName, new JavaScriptFile(path));
}
return this;
}

public ISmidgeRequire RequiresCss(CssFile file)
{
if (file is null)
{
throw new ArgumentNullException(nameof(file));
}

if (_type == WebFileType.Js)
{
throw new InvalidOperationException("Cannot add js file to a css bundle");
}

if (_requestHelper.IsExternalRequestPath(file.FilePath))
{
throw new InvalidOperationException("Cannot process an external file as part of a bundle");
}

_bundleManager.AddToBundle(_bundleName, file);
return this;
}

public ISmidgeRequire RequiresCss(params string[] paths)
{
if (_type == WebFileType.Js)
{
throw new InvalidOperationException("Cannot add js file to a css bundle");
}

foreach (var path in paths)
foreach (var path in paths.Where(p => !string.IsNullOrWhiteSpace(p)))
{
if (_requestHelper.IsExternalRequestPath(path))
{
throw new InvalidOperationException("Cannot process an external file as part of a bundle");
}

_bundleManager.AddToBundle(_bundleName, new CssFile(path));
}
return this;
}
}
}
}

0 comments on commit 76c7ad1

Please sign in to comment.