Skip to content

Commit

Permalink
chore: Fix argument checks (dotnet#9027)
Browse files Browse the repository at this point in the history
* fix: Add empty filePath check (based on previous doc comment)

dotnet#9024 (comment)

* chore: Fix additional CA1510 rules

* chore: fix redundant names (IDE0020)

Commented on dotnet#9024 (comment)

* fix: Fix wrong behavior changes introduced by previous PR

Commented on dotnet#9024 (comment)

* fix: Change thrown exception type.

Commented on dotnet#9024 (comment)
  • Loading branch information
filzrev authored and p-kostov committed Jun 28, 2024
1 parent 6852854 commit 8e8b09f
Show file tree
Hide file tree
Showing 13 changed files with 29 additions and 28 deletions.
6 changes: 2 additions & 4 deletions src/Docfx.App/Helpers/MetadataMerger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,8 @@ internal class MetadataMerger

public void Merge(MetadataMergeParameters parameters)
{
if (parameters == null)
{
throw new ArgumentNullException(nameof(parameters));
}
ArgumentNullException.ThrowIfNull(parameters);

if (parameters.OutputBaseDir == null)
{
throw new ArgumentException("Output folder cannot be null.", nameof(parameters) + "." + nameof(parameters.OutputBaseDir));
Expand Down
5 changes: 1 addition & 4 deletions src/Docfx.Build.Common/Reference/OverwriteDocumentReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,7 @@ public static FileModel Read(FileAndType file)
/// <returns></returns>
public static IEnumerable<T> Transform<T>(FileModel model, string uid, Func<T, T> itemBuilder) where T : class, IOverwriteDocumentViewModel
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
ArgumentNullException.ThrowIfNull(model);

var overwrites = ((List<OverwriteDocumentModel>)model.Content).Where(s => s.Uid == uid);
return overwrites.Select(s =>
Expand Down
4 changes: 2 additions & 2 deletions src/Docfx.Build.Engine/DocumentBuildContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -461,8 +461,8 @@ public IImmutableList<string> GetTocFileKeySet(string key)
public void RegisterToc(string tocFileKey, string fileKey)
{
#if NET7_0_OR_GREATER
ArgumentNullException.ThrowIfNullOrEmpty(fileKey);
ArgumentNullException.ThrowIfNullOrEmpty(tocFileKey);
ArgumentException.ThrowIfNullOrEmpty(fileKey);
ArgumentException.ThrowIfNullOrEmpty(tocFileKey);
#else
if (string.IsNullOrEmpty(fileKey)) throw new ArgumentNullException(nameof(fileKey));
if (string.IsNullOrEmpty(tocFileKey)) throw new ArgumentNullException(nameof(tocFileKey));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public void ProcessTheme(string outputDirectory, bool overwrite)
private bool TryExportResourceFiles(IEnumerable<string> resourceNames, string outputDirectory, bool overwrite, string? regexFilter = null)
{
#if NET7_0_OR_GREATER
ArgumentNullException.ThrowIfNullOrEmpty(outputDirectory);
ArgumentException.ThrowIfNullOrEmpty(outputDirectory);
#else
if (string.IsNullOrEmpty(outputDirectory))
throw new ArgumentNullException(nameof(outputDirectory));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public TemplateProcessor(ResourceFileReader resourceProvider, DocumentBuildConte
public TemplateBundle GetTemplateBundle(string documentType)
{
#if NET7_0_OR_GREATER
ArgumentNullException.ThrowIfNullOrEmpty(documentType);
ArgumentException.ThrowIfNullOrEmpty(documentType);
#else
if (string.IsNullOrEmpty(documentType)) throw new ArgumentNullException(nameof(documentType));
#endif
Expand All @@ -55,7 +55,7 @@ public TemplateBundle GetTemplateBundle(string documentType)
public bool TryGetFileExtension(string documentType, out string fileExtension)
{
#if NET7_0_OR_GREATER
ArgumentNullException.ThrowIfNullOrEmpty(documentType);
ArgumentException.ThrowIfNullOrEmpty(documentType);
#else
if (string.IsNullOrEmpty(documentType)) throw new ArgumentNullException(nameof(documentType));
#endif
Expand Down
4 changes: 2 additions & 2 deletions src/Docfx.Build.TableOfContents/TocHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public static IEnumerable<FileModel> Resolve(ImmutableList<FileModel> models, IH
public static TocItemViewModel LoadSingleToc(string file)
{
#if NET7_0_OR_GREATER
ArgumentNullException.ThrowIfNullOrEmpty(file);
ArgumentException.ThrowIfNullOrEmpty(file);
#else
if (string.IsNullOrEmpty(file))
{
Expand Down Expand Up @@ -100,7 +100,7 @@ public static TocItemViewModel LoadSingleToc(string file)
public static TocItemViewModel LoadYamlToc(string file)
{
#if NET7_0_OR_GREATER
ArgumentNullException.ThrowIfNullOrEmpty(file);
ArgumentException.ThrowIfNullOrEmpty(file);
#else
if (string.IsNullOrEmpty(file))
{
Expand Down
6 changes: 2 additions & 4 deletions src/Docfx.Common/CompositeDictionary.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,8 @@ void ICollection<KeyValuePair<string, object>>.Add(KeyValuePair<string, object>

public void Add(string key, object value)
{
if (key == null)
{
throw new ArgumentNullException(nameof(key));
}
ArgumentNullException.ThrowIfNull(key);

foreach (var entry in _entries)
{
if (key.StartsWith(entry.Prefix))
Expand Down
2 changes: 1 addition & 1 deletion src/Docfx.Common/Git/GitUtility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public static string RawContentUrlToContentUrl(string rawUrl)
public static GitRepoInfo Parse(string repoUrl)
{
#if NET7_0_OR_GREATER
ArgumentNullException.ThrowIfNullOrEmpty(repoUrl);
ArgumentException.ThrowIfNullOrEmpty(repoUrl);
#else
if (string.IsNullOrEmpty(repoUrl))
{
Expand Down
2 changes: 1 addition & 1 deletion src/Docfx.Common/Path/RelativePath.cs
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ public string GetFileNameWithoutExtension()
public RelativePath ChangeFileName(string fileName)
{
#if NET7_0_OR_GREATER
ArgumentNullException.ThrowIfNullOrEmpty(fileName);
ArgumentException.ThrowIfNullOrEmpty(fileName);
#else
if (string.IsNullOrEmpty(fileName))
{
Expand Down
8 changes: 7 additions & 1 deletion src/Docfx.MarkdigEngine/MarkdigMarkdownService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,13 @@ public MarkupResult Markup(string content, string filePath)
public MarkupResult Markup(string content, string filePath, bool multipleYamlHeader)
{
ArgumentNullException.ThrowIfNull(content);
ArgumentNullException.ThrowIfNull(filePath);

#if NET7_0_OR_GREATER
ArgumentException.ThrowIfNullOrEmpty(filePath);
#else
if(string.IsNullOrEmpty(filePath))
throw new ArgumentNullException(nameof(filePath));
#endif

var pipeline = CreateMarkdownPipeline(isInline: false, multipleYamlHeader);

Expand Down
6 changes: 2 additions & 4 deletions src/Docfx.Plugins/XRefSpec.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,8 @@ public XRefSpec(IDictionary<string, object> dictionary)

public XRefSpec(XRefSpec spec)
{
if (spec == null)
{
throw new ArgumentNullException(nameof(spec));
}
ArgumentNullException.ThrowIfNull(spec);

_dict = new Dictionary<string, object>(spec._dict);
}

Expand Down
2 changes: 1 addition & 1 deletion src/docfx/Models/InitCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,7 @@ public SingleChoiceQuestion(string content, Action<T, DefaultConfigModel, Questi
ArgumentNullException.ThrowIfNull(options);
ArgumentNullException.ThrowIfNull(converter);

if (options.Length == 0) throw new ArgumentNullException(nameof(options));
if (options.Length == 0) throw new ArgumentOutOfRangeException(nameof(options));

_converter = converter;
Options = options;
Expand Down
6 changes: 5 additions & 1 deletion test/Docfx.Tests.Common/TestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,11 @@ protected static string UpdateFile(string fileName, string content, string baseF

protected static string CreateDirectory(string dir, string baseFolder)
{
ArgumentNullException.ThrowIfNull(dir);
if (string.IsNullOrEmpty(dir))
{
return string.Empty;
}

ArgumentNullException.ThrowIfNull(baseFolder);

var subDirectory = Path.Combine(baseFolder, dir);
Expand Down

0 comments on commit 8e8b09f

Please sign in to comment.